Jump to content

tic tac toe game help


sheeps

Recommended Posts

I have a tic tac toe game that I've been working on for a school project. When I attempt to run it a parse error comes up saying "syntax error, unexpected $end" on line 311. If anyone wants to try and run it. go here http://itstudents.ccs.uconn.edu/~iskm218st38/newnewnewtictactoe.html I'm trying to make the computer randomly select a tile after the human player selects but the first issue I need help with is why i'm getting this syntax error on a line that doesnt have code on.

 

This is the html gameboard

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Untitled</title>
<meta name="generator" content="BBEdit 16.0" />
</head>
<body>
<center>
<?php
session_start()
?>
<form action="tictactoe.php" method="post">
<table>
<tr>
<td colspan="3" align="center">
<input type="text" size="9" name="message" style="border:none; text-align:center;" /></td>
</tr>
<tr align="center">
<tr>
<td>
<input type="text" size="1" name="box1" pattern="[X|O{2}]" title="X or O" /></td>
<td align="center">
<input type="text" size="1" name="box2" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box3" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box4" pattern="[X|O{2}]" title="X or O" /></td>
</tr>
<tr>
<td>
<input type="text" size="1" name="box5" pattern="[X|O{2}]" title="X or O" /></td>
<td align="center">
<input type="text" size="1" name="box6" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box7" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box8" pattern="[X|O{2}]" title="X or O" /></td>
</tr>
<tr>
<td>
<input type="text" size="1" name="box9" pattern="[X|O{2}]" title="X or O" /></td>
<td align="center">
<input type="text" size="1" name="box10" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box11" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box12" pattern="[X|O{2}]" title="X or O" /></td>
</tr>
<tr>
<td>
<input type="text" size="1" name="box13" pattern="[X|O{2}]" title="X or O" /></td>
<td align="center">
<input type="text" size="1" name="box14" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box15" pattern="[X|O{2}]" title="X or O" /></td>
<td align="right">
<input type="text" size="1" name="box16" pattern="[X|O{2}]" title="X or O" /></td>

</tr>
<tr>
<td colspan="3"><input type="button" value="new game"
                       onclick="new_game();
                                play();" /></td>
<td colspan="3"><input type="button" value="submit"
                       onclick="submit();" /></td>
</tr>
</center>
</body>
</html>

 

 

And this is the tictactoe.php file.

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Untitled</title>
<meta name="generator" content="BBEdit 16.0" />
</head>
<body>
<center>
<?php

//refer to the comment below

$box1 = '';
$box2 = '';
$box3 = '';
$box4 = '';
$box5 = '';
$box6 = '';
$box7 = '';
$box8 = '';
$box9 = '';
$box10 = '';
$box11 = '';
$box12 = '';
$box13 = '';
$box14 = '';
$box15 = '';
$box16 = '';

$human = '';
$computer = '';

$players = array ($human, $computer);

$gameboard = array(
$box1,
$box2,
$box3,
$box4,
$box5,
$box6,
$box7,
$box8,
$box9,
$box10,
$box11,
$box12,
$box13,
$box14,
$box15,
$box16,
);

switch ($box1) {
    case "O":
        echo $_POST["box1"];
        break;
    case "X":
        echo $_POST["box1"];
        break;
}

switch ($box2) {
    case "O":
        echo $_POST["box2"];
        break;
    case "X":
        echo $_POST["box2"];
        break;
}
switch ($box3) {
    case "O":
        echo $_POST["box3"];
        break;
    case "X":
        echo $_POST["box3"];
        break;
}
switch ($box4) {
    case "O":
        echo $_POST["box4"];
        break;
    case "X":
        echo $_POST["box4"];
        break;
}
switch ($box5) {
    case "O":
        echo $_POST["box5"];
        break;
    case "X":
        echo $_POST["box5"];
        break;
}
switch ($box6) {
    case "O":
        echo $_POST["box6"];
        break;
    case "X":
        echo $_POST["box6"];
        break;
}
switch ($box7) {
    case "O":
        echo $_POST["box7"];
        break;
    case "X":
        echo $_POST["box7"];
        break;
}
switch ($box8) {
    case "O":
        echo $_POST["box8"];
        break;
    case "X":
        echo $_POST["box8"];
        break;
}
switch ($box9) {
    case "O":
        echo $_POST["box9"];
        break;
    case "X":
        echo $_POST["box9"];
        break;
}
switch ($box10) {
    case "O":
        echo $_POST["box10"];
        break;
    case "X":
        echo $_POST["box10"];
        break;
}
switch ($box11) {
    case "O":
        echo $_POST["box11"];
        break;
    case "X":
        echo $_POST["box11"];
        break;
}
switch ($box12) {
    case "O":
        echo $_POST["box12"];
        break;
    case "X":
        echo $_POST["box12"];
        break;
}
switch ($box13) {
    case "O":
        echo $_POST["box13"];
        break;
    case "X":
        echo $_POST["box13"];
        break;
}
switch ($box14) {
    case "O":
        echo $_POST["box14"];
        break;
    case "X":
        echo $_POST["box14"];
        break;
}
switch ($box15) {
    case "O":
        echo $_POST["box15"];
        break;
    case "X":
        echo $_POST["box15"];
        break;
}

switch ($box16) {
    case "O":
        echo $_POST["box16"];
        break;
    case "X":
        echo $_POST["box16"];
        break;
}

/*$gameboard = array_rand($box1, $box2, $box3, $box4, $box5, $box6, $box7, $box8, $box9, $box10, $box11, $box12, $box13, $box14, $box15, $box16 );
foreach ($gameboard as &$value) {
    $value = 'O';
}*/
// $gameboard is now array_rand() as &$value = O 
unset($value); // break the reference with the last element

if ($box1=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box2=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box3=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box4=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box5=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box6=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box7=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box8=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box9=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box10=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box11=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box12=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box13=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box14=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box15=="X")
echo $gameboard[$rand_keys[0]] . "\n";

if ($box16=="X")
echo $gameboard[$rand_keys[0]] . "\n";



/*
unset($b);
$b[] = "box1|box2|box3|box4";
$b[] = "box5|box6|box7|box8";
$b[] = "box9|box10|box11|box12";
$b[] = "box13|box14|box15|box16";
*/
/*
   $boardtile = ();
   $gameboard = array( array(box1, box2, box3),
    				   array(box4, box5, box6),
    			       array(box7, box8, box9));
    public static GUI_Box
*/
    $Human = true;
  function check_input( $box1 ) {
  if( preg_match( "X|O", $text ) ) {
    return false;
  }
  else {
    return true;

    // human wins if 
    if(	//horizontal
($box1 == $human && $box2 == $human && $box3 == $human && $box4 == $human) or
($box5 == $human && $box6 == $human && $box7 == $human && $box8 == $human) or
($box9 == $human && $box10 == $human && $box11 == $human && $box12 == $human) or
($box9 == $human && $box10 == $human && $box11 == $human && $box12 == $human) or

//vertical

($box1 == $human && $box5 == $human && $box9 == $human && $box13 == $human) or
($box2 == $human && $box6 == $human && $box10 == $human && $box14 == $human) or
($box3 == $human && $box7 == $human && $box11 == $human && $box15 == $human) or
($box4 == $human && $box8 == $human && $box12 == $human && $box16 == $human) or
//diagonal

($box4 == $human && $box7 == $human && $box10 == $human && $box13 == $human) or
($box1 == $human && $box6 == $human && $box11 == $human && $box16 == $human))

	echo("human "+$players[0]+" won")
;
//computer wins if 
if(	//horizontal
($box1 == $computer && $box2 == $computer && $box3 == $computer && $box4 == $computer) or
($box5 == $computer && $box6 == $computer && $box7 == $computer && $box8 == $computer) or
($box9 == $computer && $box10 == $computer && $box11 == $computer && $box12 == $computer) or
($box9 == $computer && $box10 == $computer && $box11 == $computer && $box12 == $computer) or 

//vertical

($box1 == $computer && $box5 == $computer && $box9 == $computer && $box13 == $computer) or
($box2 == $computer && $box6 == $computer && $box10 == $computer && $box14 == $computer) or
($box3 == $computer && $box7 == $computer && $box11 == $computer && $box15 == $computer) or
($box4 == $computer && $box8 == $computer && $box12 == $computer && $box16 == $computer) or	
//diagonal

($box4 == $computer && $box7 == $computer && $box10 == $computer && $box13 == $computer) or
($box1 == $computer && $box6 == $computer && $box11 == $computer && $box16 == $computer))

	echo("computer "+$players[1]+" won")
;
}	
?>
</center>
</body>
</html>

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

There is no closing bracket } for the function check_input().  When you see "unexpected $end" it is usually a bracket problem.  There are many other problems with the script though.

 

1. $rand_keys is not defined.

2. case statements make no sense.  You are echoing the $_POST['box#'] no matter what the case is, so there is no need for it.

3. concantenation in PHP is with the period( . ) not with the plus sign (+).

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.