Closed kgondim closed 2 years ago
Hi @kgondim
The first approach you described sounds like it could work. Do you still have the code for this approach that I could take a look at in Github and see why it wasn't adding up as expected?
The p2 revisions commit on Oct 23rd has the code for the first approach. The p2 revisions commit from Oct 24th has another version of that approach along with the others described.
Thanks!
Ok, got it!
You were on the right track in setting a session variable to track number of wins - the step you were missing was retrieving this value once it was set so that you could add on to it.
Instead of setting $num
to 0
in process.php (line 13), you should look in the session to see if you already have a value for num/my_wins and if so, set it to that value. Otherwise, then you can default it to 0. Like so:
if (isset($_SESSION['my_wins'])) {
$my_wins = $_SESSION['my_wins'];
$num = $my_wins['points'];
} else {
$num = 0;
}
On a someone unrelated note, I have a couple suggestions for code style/design:
When counting the number of wins, you use three different identifiers for this value: my_wins
, points
, and num
.
To avoid confusion, I suggest picking one identifier that is clear/descriptive and being consistent with it, for example: winCount
.
Also, the session value that tracks the win count can be set to an integer instead of an array. We used an array for the session value that tracks results because it was tracking multiple values (diceRoll1, diceRoll2, winner, guess, etc.), but that's not necessary for the win count.
Applying the above tips, your process.php could look like this:
<?php
session_start();
# user input guess
$guess = $_POST['guess'];
# Create array for each di
$dice1 = [1, 2, 3, 4, 5, 6];
$dice2 = [1, 2, 3, 4, 5, 6];
if (isset($_SESSION['winCount'])) {
$winCount = $_SESSION['winCount'];
} else {
$winCount = 0;
}
# FYI: The above if/else construct could be shortened to a null coalescing operator:
# $winCount = $_SESSION['winCount'] ?? 0;
# choose a random element from each di and assign to variable
$diceRoll1 = $dice1[rand(0, 5)];
$diceRoll2 = $dice2[rand(0, 5)];
# Total numbers from "roll" and allocate to Player 1
$playerTotal = $diceRoll1 + $diceRoll2;
# Determine winner
$winner = $guess == $playerTotal;
if ($winner) {
$winCount++;
}
$_SESSION['results'] = [
'diceRoll1' => $diceRoll1,
'diceRoll2' => $diceRoll2,
'player' => $playerTotal,
'winner' => $winner,
'guess' => $guess
];
# This session value can be set to the integer $winCount - it does not need to be set to an array
$_SESSION['winCount'] = $winCount;
header('Location: index.php');
And index.php:
<?php
session_start();
if (isset($_SESSION['results'])) {
$results = $_SESSION['results'];
$diceRoll1 = $results['diceRoll1'];
$diceRoll2 = $results['diceRoll2'];
$playerTotal = $results['player'];
$winner = $results['winner'];
$guess = $results['guess'];
$_SESSION['results'] = null;
}
if (isset($_SESSION['winCount'])) {
$winCount = $_SESSION['winCount'];
}
require 'index-view.php';
And finally you'd update index-view.php so it outputs $winCount
instead of $my_wins['points'];
Let me know if you have any questions about the above.
I put off tracking score until we get to persistent storage... I'm thinking that will be what I'll want to use a database to track because it will facilitate historical data over multiple visits to the game site. Thanks for asking this question though.
@susanBuck Thanks so much for this! I hope to be able to dig into it this weekend, and I'll follow up if I have any questions
I won't post any code here, but looking for ideas to solve a problem I was encountering with the projects. I was trying to keep some type of cumulative score over multiple rounds of the game. I'm pretty sure the code was on the right track for a counter, but I think setting the global session variable to null would clear it out each time. To address that, I declared a separate global session variable that would be just for the counter and that I would not set to null after using. That worked to count the score the first time, but then the points weren't adding up.
As an alternative, I tried counting the times played by creating the global server and global post variable that would basically count every time input was posted. That didn't work either--the guidance I was finding seemed a bit over my head for php. The last alternative was to use a global sessions variable to count the sessions, but it would not reset when you refresh page, instead it would count the refresh and keep going.
Looking for some ideas on how to solve this within the scope of what we have been doing. Some other solutions I found involved databases, etc. I feel like this would have been very easy to do with a programming language like JavaScript.