Jump to content

Tic Tack Toe game help


webdevdea
Go to solution Solved by requinix,

Recommended Posts

I have the code working well but it does not always work when there is a tie, could someone help me to figure out my bug?

<?php
$winner = 'n';
$box = array('','','','','','','','','');
if (isset($_POST["submitbtn"])) {
	$box[0] = $_POST['box0'];
	$box[1] = $_POST['box1'];
	$box[2] = $_POST['box2'];
	$box[3] = $_POST['box3'];
	$box[4] = $_POST['box4'];
	$box[5] = $_POST['box5'];
	$box[6] = $_POST['box6'];
	$box[7] = $_POST['box7'];
	$box[8] = $_POST['box8'];
	
	//print_r($box);
	if (($box[0] =='x' && $box[1]=='x' && $box[2] =='x') || ($box[3] =='x' && $box[4]=='x' && $box[5] =='x') || ($box[6] =='x' && $box[7]=='x' && $box[8] =='x') || ($box[0] =='x' && $box[3]=='x' && $box[6] =='x') || ($box[1] =='x' && $box[5]=='x' && $box[7] =='x') ||($box[2] =='x' && $box[6]=='x' && $box[8] =='x') || ($box[0] =='x' && $box[4]=='x' && $box[8] =='x') ||($box[2] =='x' && $box[4]=='x' && $box[6] =='x')){
	$winner = 'x';
	print("X wins");
	}
	$blank = 0;
	for ($i=0; $i<=8; $i++){
		if ($box[$i] == '') {
			$blank = 1;
	}
}
if ($blank == 1 && $winner == 'n') {
	$i = rand() % 8;
	while ($box[$i] != '') {
			$i = rand() % 8;
		}//end while loop
		$box[$i] = 'o';
			if (($box[0] =='o' && $box[1]=='o' && $box[2] =='o') || ($box[3] =='o' && $box[4]=='o' && $box[5] =='o') || ($box[6] =='o' && $box[7]=='o' && $box[8] =='o') || ($box[0] =='o' && $box[3]=='o' && $box[6] =='o') || ($box[1] =='o' && $box[5]=='o' && $box[7] =='o') ||($box[2] =='o' && $box[6]=='o' && $box[8] =='o') || ($box[0] =='o' && $box[4]=='o' && $box[8] =='o') ||($box[2] =='o' && $box[4]=='o' && $box[6] =='o')) {
				$winner = 'o';
	print("O wins");
			}
	} else if ($winner =='n'){
	$winner = 't';
	print("Tied Game");
	}
}
?>
<html>
<head>
<title>game</title>
<style type="text/css">

#box{
		background-color :#99FFCC;
		border: 1px solid #008000;
		width: 100px;
		height: 100px;
		font-size: 66px;
		text-align: center;
		
}
#go {
width:200px;
font-family: 'Comic Sans MS';
font-size: 40px;
}
</style>
</head>
<body bgcolor="pink">
<body>
<form name="tictactoe" method="post" action="http://localhost/Assignments php class/game.php">
<?php
for ($i=0; $i<=8; $i++) {
	printf('<input type="text" name="box%s" value ="%s" id = "box">', $i, $box[$i]);
	if ($i == 2 || $i == 5 || $i == 8 ) {
	print('<br>');
	}
} // end for loop
if ($winner == 'n') {
print('<p><input type="submit" name="submitbtn" value="fire" id="go"></p>');
} else {
	print('<input type="button" name="newgamebtn" value= "New Game" 
onclick ="window.location.href=\'http:localhost/Assignments php class/game.php\'"><');
}
?>
</form>
</body>
</html>

Link to comment
Share on other sites

it does not always work

Meaning what? You need to explain things like that because not everybody will read through code looking for bugs when they don't really know what they're looking for.

 

With that said,

$i = rand() % 8;
Bug

$box[1] =='x' && $box[5]=='x' && $box[7] =='x'
$box[1] =='o' && $box[5]=='o' && $box[7] =='o'
Bug

$box[2] =='x' && $box[6]=='x' && $box[8] =='x'
$box[2] =='o' && $box[6]=='o' && $box[8] =='o'
Bug

 

And your New Game button is a bit wonky with the onclick. And there's an extra <.>

Link to comment
Share on other sites

Ok I got this fixed, I was trying to center things earlier and left something in that was supposed to come out,

but on the other bug I do not understand what Is wrong? The code works except when there is a tie, 

I cant see what is wrong with the above bugs, 

	print('<input type="button" name="newgamebtn" value= "New Game" 
onclick ="window.location.href=\'game.php\'">');
}
?>
Edited by webdevdea
Link to comment
Share on other sites

  • Solution

 

$i = rand() % 8;
Bug

 

You have to modulo by the number of things, not by the highest number you want to get. So % 9. Using %8 would only give you numbers 0-7, leaving out 8 in the bottom-right corner.

 

 

$box[1] =='x' && $box[5]=='x' && $box[7] =='x'
$box[1] =='o' && $box[5]=='o' && $box[7] =='o'
Bug

$box[2] =='x' && $box[6]=='x' && $box[8] =='x'
$box[2] =='o' && $box[6]=='o' && $box[8] =='o'
Bug

 

+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
Edited by requinix
Link to comment
Share on other sites

Here is where I uploaded it, its not an assignment though it was just something i was doing to try to understand an assignment we were given to create the battleship game.. just a stepping stone. Thank you so much for your help, I couldnt get it to tie and I have to go get ready for school and walk the dogs but I will think it is solved as good as it can be at this point, I have to get to working on the functions for my project.. again thanks.. 

http://www.ctcsports.org/upload/Summer2013/CIST2351/900104329/Assignment35/game.php

Link to comment
Share on other sites

 

your game is not working properly yet ::)

I won, but the game is not finished yet.

001
 1  
10

How was it not working?

please let me know so I can adjust it accorningly….

I am trying to get a handle on this battleship game, I have some functions or blocks of code that I am working on and trying to pull it all together.. thank you so much for your help.. 

ps did you get it to say tie??????

Link to comment
Share on other sites

Keep in mind you don't have to just put in "X" values: you can set anything in any box you want.

 

Seems like there's still an infinite loop when "O" tries to fill in the bottom-right corner, as the code you posted earlier would demonstrate. What is your code now?

Link to comment
Share on other sites

I have to get back with you after school, I am going to finish it up and try to get that battleship thing going.. im like seriously this is my first programming class and you want me to make a one man battleship game  .. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.