Skip to content

Instantly share code, notes, and snippets.

@billychappell
Last active August 29, 2015 14:07
Show Gist options
  • Save billychappell/c83cf81d574265cc5e62 to your computer and use it in GitHub Desktop.
Save billychappell/c83cf81d574265cc5e62 to your computer and use it in GitHub Desktop.
RPS AI Arrays not holding value increases between throws
<?php
//Current state is chosen based on the player's last move. The value of each element is equal to the number of times a player has chosen ROCK/PAPER/SCISSORS after the same previous move.
$initialState = array('ROCK'=>1, 'PAPER'=>1, 'SCISSORS'=>1);
$rockState = array('ROCK'=>1, 'PAPER'=>1, 'SCISSORS'=>1);
$paperState = array('ROCK'=>1, 'PAPER'=>1, 'SCISSORS'=>1);
$scissorState = array('ROCK'=>1, 'PAPER'=>1, 'SCISSORS'=>1);
$currentState =& $initialState;
if (isset($_POST['button'])==1) {
//This takes the current state and uses the value of each element to populate a new array with that many elements. So, 'ROCK'=>2, 'PAPER'=>1 would create array('ROCK', 'ROCK', 'PAPER').
$newState = array();
foreach ($currentState as $element=>$value) {
$newState = array_merge($newState, array_fill(0, $value, $element)); }
//A key is selected from the new array using array_rand, which guesses the player's next move. The switch statement then chooses the computer's pick; if the player is likely to play rock, then the computer will play paper.
$compGuess = $newState[array_rand($newState)];
switch ($compGuess) {
case 'ROCK':
$compThrow = 'PAPER'; break;
case 'PAPER':
$compThrow = 'SCISSORS'; break;
case 'SCISSORS':
$compThrow = 'ROCK'; break;
}
//Setting initial variables
$throwCount = $_POST['throwCount'];
$winCount = $_POST['winCount'];
$lossCount = $_POST['lossCount'];
$drawCount = $_POST['drawCount'];
$playerThrow = $_POST['playerThrow'];
$rock = $_POST['ROCK'];
$paper = $_POST['PAPER'];
$scissors = $_POST['SCISSORS'];
//These if statements compare the computer and player's choices to determine the outcome of the game. Then, the result is added to the tally.
//$currentState['ELEMENT']++ is meant to increase the value of the element corresponding to the user's choice (e.g. if the current state is rockState and the player chose paper, then the value of paper should change from =>1 to =>2.)
//Lastly, the current state is changed to reflect the players latest move (so choosing paper after rock would result in rockState['PAPER']++ followed by the current state being changed to paper.
if ($compThrow == $playerThrow) {
$winLossDraw = 'DRAW';
$drawCount++;
//************* v DEFINITELY QUESTIONABLE v ********************
$currentState[$compThrow]++;
//************* ^ ELEVATED QUESTIONABILITY ENDS HERE ^ ********************
}
if ($compThrow == 'ROCK' && $playerThrow == 'PAPER') {
$winLossDraw = 'YOU WIN';
$winCount++;
$currentState['PAPER']++;
$currentState =& $paperState;
}
if ($compThrow == 'PAPER' && $playerThrow == 'SCISSORS') {
$winLossDraw = 'YOU WIN';
$winCount++;
$currentState['SCISSORS']++;
$currentState =& $scissorState;
}
if ($compThrow == 'SCISSORS' && $playerThrow == 'ROCK') {
$winLossDraw = 'YOU WIN';
$winCount++;
$currentState['ROCK']++;
$currentState =& $rockState;
}
if ($compThrow == 'SCISSORS' && $playerThrow == 'PAPER') {
$winLossDraw = 'YOU LOST';
$lossCount++;
$currentState['PAPER']++;
$currentState =& $paperState;
}
if ($compThrow == 'PAPER' && $playerThrow == 'ROCK') {
$winLossDraw = 'YOU LOST';
$lossCount++;
$currentState['ROCK']++;
$currentState =& $rockState;
}
if ($compThrow == 'ROCK' && $playerThrow == 'SCISSORS') {
$winLossDraw = 'YOU LOST';
$lossCount++;
$currentState['SCISSORS']++;
$currentState =& $scissorState;
}
$throwCount++;}
else {
$throwCount = 0;
$winCount = 0;
$lossCount = 0;
$drawCount = 0;
$winLossDraw = "No games played yet";
$playerThrow = "N/A";
$compThrow = "N/A";
$rock = "N/A";
$paper = "N/A";
$scissors = "N/A";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Rock Paper Scissors AI!</title>
</head>
<body>
<form action="rpsai.php" method="post">
<input name="button" type="hidden" value="1" />
<input name="throwCount" type="hidden" value="<?php echo $throwCount; ?>" />
<input name="winCount" type="hidden" value="<?php echo $winCount; ?>" />
<input name="lossCount" type="hidden" value="<?php echo $lossCount; ?>" />
<input name="drawCount" type="hidden" value="<?php echo $drawCount; ?>" />
<label><input type="submit" name="playerThrow" value="ROCK" id="ROCK"></label>
<label><input type="submit" name="playerThrow" value="PAPER" id="PAPER"></label>
<label><input type="submit" name="playerThrow" value="SCISSORS" id="SCISSORS"></label>
</form>
<p>CURRENT GAME STATUS: <?php echo $winLossDraw; ?></p>
<p>You threw: <?php echo $playerThrow; ?></p>
<p>Computer threw: <?php echo $compThrow; ?></p>
<p>Total Throws: <?php echo $throwCount; ?></p>
<p>Wins: <?php echo $winCount; ?></p>
<p>Losses: <?php echo $lossCount; ?></p>
<p>Draws: <?php echo $drawCount; ?></p>
<p>Current State: <?php echo var_dump($currentState); ?></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment