Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question One case not working in my if thens - please help

    Hi all,

    I'm pretty new and self-learning php, so forgive me if I've missed something basic here, but for the life of me I can't find the error in my code. I've made a game to play the Rock Paper Scissors game, and everything works, but someone found a bug and I can't figure it out. I'm hoping someone more experienced can spot it.

    The problem seems to be in the show_stats function, which outputs several lines of text after each game is played. For one particular result (player chooses Rock and wins), one of the lines of output doesn't display (the one that should say "Rock crushes Scissors". Everything else works perfectly in all results and outcomes.

    Code below.
    Many, many Thanks,
    Kevin

    Code:
    // process results into correct terms etc and output
    function show_stats($pscore,$cscore,$pchoice,$cchoice,$res){
    	global $fontface, $fontcol;
    	// assign correct term for wraps/crushes/cuts/matches	
    	if ($res != drew) {
    		if ($cchoice != "Scissors") {
    			if ($pchoice != "Scissors") {
    				$winway = 'wraps';
    			}
    		}
    		elseif ($cchoice != "Rock") {
    			if ($pchoice != "Rock") {
    				$winway = 'cuts';
    		$winchoice = "Scissors";
    		$losechoice = "Paper";
    			}
    		}
    		elseif ($cchoice != "Paper") {
    			if ($pchoice != "Paper") {
    				$winway = 'crushes';
    			}
    		}
    	}
    	
    	// assign correct text message for win/lose/draw	
    	if ($res == "won"){
    		$text = "You Won!!";
    	}
    	elseif ($res == "lost"){
    		$text = "You Lost!!";
    	}
    	elseif ($res == "drew"){
    		$winway = 'matches';
    		$text = "It's A Draw!!";
    	}
    
    	// assign correct terms for winner and loser choices	
    	if ($winway == 'cuts') {
    		$winchoice = "Scissors";
    		$losechoice = "Paper";
    	}
    	elseif ($winway == 'crushes') {
    		$winchoice = "Rock";
    		$losechoice = "Scissors";
    	}
    	elseif ($winway == 'wraps') {
    		$winchoice = "Paper";
    		$losechoice = "Rock";
    	}
    	elseif ($winway == 'matches') {
    		$winchoice = $cchoice;
    		$losechoice = $cchoice;
    	}
    
    	// output the formatted results	
    	echo "<table width='420' border='0' cellpadding='2' cellspacing='10' bgcolor='#FFFFFF'>"
     . " <tr><td align='center' colspan='2'>Results For This Round:</td></tr>"
     . " <tr><td width='50%' align='right'>Your Choice:</td><td width='50%' align='center'>$pchoice</td></tr>"
     . " <tr><td align='right'>Computer Choice:</td><td align='center'>$cchoice</td></tr>"
     . " <tr><td colspan='2' align='center'>$winchoice $winway $losechoice<br><br><font color='$fontcol' face='$fontface' size='6'>$text</td></tr></table>"
     . " <table width='420' border='0' cellpadding='2' cellspacing='10' bgcolor='#FFFFFF'><tr><td width='300' align='right'>Your Tournament Score:</td><td align='left'>$pscore</td></tr>"
     . " <tr><td width='300' align='right'>Computer Tournament Score:</td><td align='left'>$cscore</td></tr></table>";
    	return;
    // end of function show_stats
    }
    Last edited by scapes; 06-10-2005 at 10:03 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Since the only code you've presented is a function, we'll need a bit more information.

    Namely, we'll need all the values of the parameters that are passed into show_stats() that cause this "bug". Then we can trace the function to see what the output is and why it's that way and determine how to fix it.

    Sadiq.

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Next to each if cchoice statement are the possible values that will cause that statement to be true, so Rock is always being caught by if Scissors. I would consider revising the if statements to a switch for each _exact_ choice of $cchoice.
    Code:
    		if ($cchoice != "Scissors") { //Paper || Rock
    			if ($pchoice != "Scissors") {
    				$winway = 'wraps';
    			}
    		} elseif ($cchoice != "Rock") {  //Paper || Scissors
    			if ($pchoice != "Rock") {
    				$winway = 'cuts';
    				$winchoice = "Scissors";
    				$losechoice = "Paper";
    			}
    		} elseif ($cchoice != "Paper") { //Rock || Scissors
    			if ($pchoice != "Paper") {
    				$winway = 'crushes';
    			}
    		}

  • #4
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi again,

    No problem

    Here is the other main function which gathers the values and plays the game. I know, you'll all think I'm over-reliant of ifs and elses (I'm sure there's much better ways, but, like I said - NEWBIE!)

    Thanks again
    Kevin


    Code:
    function play_game($memid)
    {
    	//Get Game Configuration Values
    	$rocksconfig = @mysql_fetch_array(@mysql_query("Select * from ".mysql_prefix."rocks_config"));
    	//If html characters are used
    	$rnote =html_entity_decode($rocksconfig[1]);
    	//Strip slashes from ' or " if used
    	$ro_msg = stripslashes($rnote);
    	$rocks_msg = nl2br($ro_msg);
    
    	// Import all necessary functions and POST variables
    	// require('rock.inc.php');
    	$pscore = $_POST['playscore'];
    	$cscore = $_POST['compscore'];
    	$guess = $_POST['guess'];
    	$wager = $_POST['wager'];
    	 
    	// Output site name/header/image
    	echo "<B><P align='center'>";
    	?><? site_name(); ?>
    	<? echo " Presents</B>";
    	   echo "<p align='center'><IMG src='rpsban.jpg'></p><p align='center'>";
    	   echo $rocks_msg." <BR><BR>";
    	
    	// Check for empty scores and reset to zero if needed
    	if ($pscore ==""){
    		$pscore='0';
    	}
    	if ($cscore ==""){
    		$cscore='0';
    	}
    	// Check for empty wager var and if so set flag for first run through
    	if ($wager ==""){
    		$firsttime = 1;
    		}
    	
    	// Test do they have negative balance - if so, set bet to zero 
    	$pb = @mysql_query("Select sum(amount) from ".mysql_prefix."accounting where username = '$memid' and type ='points';");
    	$pbalance = mysql_fetch_array($pb); $gpoints = ($pbalance[0]/100000);
    		if ($firsttime != 1){
    			if ($wager >= 1){
    				if ($gpoints <= $realstake)
    				{
    					echo "<b><font color='red'>Your Point Balance is ";
    					echo $gpoints;
    					echo "<br>Which is less than your bet of ";
    					$realwager = $wager/100000;
    					echo $realwager;
    					echo " points. <br>Your bet was therefore cancelled</font></b>";
    					$wager = 0;
    				}
    			}
    		}
    	
    	// Logic Section - Pick Computer choice, determine who won and adjust scores accordingly 
    	$number = rand(1,3);
    	$pchoice = $guess;
    	if ($pchoice == 'Paper') {
    		if ($number == 1) {
    			$cchoice = "Paper";
    			$result=drew;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    		if ($number == 2) {
    			$cscore = $cscore + 1;
    			$cchoice = "Scissors";
    			$result=lost;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    		if ($number == 3) {
    			$pscore = $pscore + 1;
    			$cchoice = "Rock";
    			$result=won;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    	}
    	if ($pchoice == 'Scissors') {
    		if ($number == 1) {
    			$pscore = $pscore + 1;
    			$cchoice = "Paper";
    			$result=won;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    		if ($number == 2) {
    			$cchoice = "Scissors";
    			$result=drew;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    		if ($number == 3) {
    			$cscore = $cscore + 1;
    			$cchoice = "Rock";
    			$result=lost;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    	}
    	if ($pchoice == 'Rock') {
    		if ($number == 1) {
    			$cscore = $cscore + 1;
    			$cchoice = "Paper";
    			$result=lost;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    		if ($number == 2) {
    			$pscore = $pscore + 1;
    			$cchoice = "Scissors";
    			$result=won;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    		if ($number == 3) {
    			$cchoice = "Rock";
    			$result=drew;
    			show_stats($pscore,$cscore,$pchoice,$cchoice,$result);
    		}
    	}
    	
    	if ($result == won) {
    		$winamt = $wager*2;
    		$realwin = $winamt/100000;
    		echo "<font color='$fontcol' face='$fontface'><b>You Have Been Rewarded ";
    		echo $realwin;
    		echo " Points For This Round</b></font>" ;
    		//First check to see if there is already a RPS credit, if so, use it
    			$precredit = @mysql_query("select * from ".mysql_prefix."accounting where username='".$memid."' and description='Rock,Paper,Scissors Winnings' LIMIT 1");
    			$pcredrows = @mysql_num_rows($precredit);
    	
    			if ($pcredrows >= 1){
    				$pcredit = @mysql_fetch_array($precredit);
    				//Assign local vars to db information
    				$oldcred = $pcredit[amount];
    				$newcred = ($oldcred + $winamt);
    				$tran = $pcredit[transid];
    				@mysql_query("update ".mysql_prefix."accounting set amount='".$newcred."', unixtime = '".unixtime."' where transid ='".$tran."'");
    			} else {
    				//First credit made for RPS  
    				$pickedtransid = maketransid($memid);
    				@mysql_query("insert into ".mysql_prefix."accounting set transid='".$pickedtransid."',username='".$memid."',
    				unixtime='".unixtime."', description='Rock,Paper,Scissors Winnings', type = 'points', amount ='".$winamt."'");
    			} 
    		}
    	
    	
    	if ($wager != 0) {
    		//Deduct wager from Member Account
    		$debwager = -$wager;
    		//First check to see if there is already a RPS debit, if so use it
    		$predebit = @mysql_query("select * from ".mysql_prefix."accounting where username='".$memid."' and description='Rock,Paper,Scissors Stakes' LIMIT 1");
    		$pdebrows = @mysql_num_rows($predebit);
    		if ($pdebrows >= 1){
    			$pdebit = @mysql_fetch_array($predebit);
    			//Assign local vars to db information
    			$olddeb = $pdebit[amount];
    			$newdeb = ($olddeb + $debwager);
    			$tran = $pdebit[transid];
    			@mysql_query("update ".mysql_prefix."accounting set amount='".$newdeb."', unixtime = '".unixtime."' where transid ='".$tran."'");
    		} else {
    			//If first debit made for RPS  
    			$pickedtransid = maketransid($memid);
    			@mysql_query("insert into ".mysql_prefix."accounting set transid='".$pickedtransid."',username='".$memid."',
    			unixtime='".unixtime."', description='Rock,Paper,Scissors Stakes', type = 'points', amount ='".$debwager."'");
    			} 
    		}
    	
    	// Gather user guess and submit to POST - present Radio options so they are 'sticky'
    	echo "<FORM NAME='play' METHOD=post ACTION=\"$PHP_SELF\"> ";
    	echo "<table cellspacing='25' bgcolor='#FFFFFF' width='420' border='0'><tr valign='bottom' align='center'><td><img src='rock_r.gif'><br><input type = 'radio' name='guess' "; if ($_POST[guess] == 'Rock') { echo ' checked'; } echo " value='Rock'>Rock</td>";
    	echo "<td><img src='rock_p.gif'><br><input type = 'radio' name='guess' "; if ($_POST[guess] == 'Paper') { echo ' checked'; } echo " value='Paper'>Paper</td>";
    	echo "<td><img src='rock_s.gif'><br><input type = 'radio' name='guess' "; if ($_POST[guess] == 'Scissors') { echo ' checked'; } echo " value='Scissors'>Scissors</td></tr></table>";
    	echo "<table width='480'><tr><td>Place Your Bet: <select name='wager' size='1'><option value='0'>No Bet - Just for Fun</option><option value='100000'>1 Point</option><option value='200000'>2 Point</option><option value='500000'>5 Points</option></select></td>";
    	echo "<td><input name = 'submit' type = 'submit' value=\"Play Game Now!\" ></td></tr></table>" ;
    	echo "<input name = 'playscore' type= 'hidden' VALUE='$pscore'> ";
    	echo "<input name = 'compscore' type= 'hidden' VALUE='$cscore'> ";
    	echo "</form> ";
    	
    	//Second form to reset Game (if game has been played in this tournament)
    	if ($guess != ""){
    		echo "<FORM NAME='reset' METHOD=post ACTION=\"$PHP_SELF\"> ";
    		echo "<input name = 'submit' type = 'submit' value=\"Start A New Tournament!\" >" ;
    		echo "<input name = 'guess' type= 'hidden' VALUE=''> ";
    		echo "<input name = 'playscore' type= 'hidden' VALUE=''> ";
    		echo "<input name = 'compscore' type= 'hidden' VALUE=''> ";
    		echo "</form> ";
    	}
    	
    // end of play game function
    }

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You need to read our posting guidelines as we have requested of everyone to do before posting in our forums.

    http://www.codingforums.com/postguide.htm

    Specifically #2
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #6
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    haha spookster it's like you're spamming every topic with the same reply.

    unfortunately, it is warranted.

  • #7
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Apologies

    Didn't read posting guidelines and just dived in. Thanks for the responses anyway. Next time I'll be more careful and descriptive in the title.

    Kevin


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •