Jump to content

WORKING ON TIC TAC TOE GAME


Go to solution Solved by ignace,

Recommended Posts

HERE Is the link

http://dandewebwonders.com/game.php

 you can play around .. it will say 'o' wins when they obviously dont and when 'x' wins it does not acknowledge it.. 

Trying to put this on my site and cant get it to work correctly.. please and thank you .. 

<?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><CENTER>
<H1>TIC-TAC-TOE</H1>
<P> PLACE AN X IN A SQUARE, THEN CLICK FIRE FOR THE COMPUTER TO TAKE A TURN</P>
<form name="tictactoe" method="post" action="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('<input type="submit" name="submitbtn" value="fire" id="go">');
} else {
	print('<input type="button" name="newgamebtn" value= "New Game" 
onclick ="window.location.href=\'game.php\'">');
}
?>
</form></CENTER>
</body>
</html>
 



Link to comment
https://forums.phpfreaks.com/topic/283294-working-on-tic-tac-toe-game/
Share on other sites

  • Solution
if (isset($_POST['submitbtn'])) {

$hasBlank = false;
foreach (range(0, as $i) {
$val = array_key_exists('box' . $i, $_POST)
? $_POST['box' . $i]
: '';
if (empty($val) && $hasBlank === false) $hasBlank = true;
$box[] = $val;
}

function check($box, $x) {
// [ 0, 1, 2 ]
// [ 3, 4, 5 ]
// [ 6, 7, 8 ]

// horizontal
return ($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)
// vertical
|| ($box[0] == $x && $box[3] == $x && $box[6] == $x)
|| ($box[1] == $x && $box[4] == $x && $box[7] == $x)
|| ($box[2] == $x && $box[5] == $x && $box[8] == $x)
// diagonal
|| ($box[0] == $x && $box[4] == $x && $box[8] == $x)
|| ($box[2] == $x && $box[4] == $x && $box[6] == $x);
}

function doComputerTurn(&$box, $x) {
$empties = array_keys($box, '');
if (!$empties) return;
$key = array_rand($empties);
$box[$empties[$key]] = $x;
}

if ($hasBlank) {
echo 'keep playing!';
} elseif (check($box, 'o') && check($box, 'x')) {
echo 'draw!';
} elseif (check($box, 'x')) {
echo 'x wins!';
} elseif (check($box, 'o')) {
echo 'o wins!';
}

}
Edited by ignace
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.