/    Sign up×
Community /Pin to ProfileBookmark

need help in php

So I have managed to get this far with the code. I’m making a simple tic tac toe game for my school project and I would be very happy if someone could help me with my code.

[code=php]
<!DOCTYPE html>
<?php
$WINNER = ‘N’;
$box = array(”, ”, ”, ”, ”, ”, ”, ”, ”);
if (isset($_POST[“sb”])) {
$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”];

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[4] == ‘x’ && $box[7] == ‘x’) ||
($box[2] == ‘x’ && $box[5] == ‘x’ && $box[8] == ‘x’) ||
($box[0] == ‘x’ && $box[4] == ‘x’ && $box[8] == ‘x’) ||
($box[2] == ‘x’ && $box[4] == ‘x’ && $box[6] == ‘x’)) {
$WINNER = ‘x’;
echo ‘WINNER IS: X’;
}

$blanck = 0;
for ($i = 0; $i <= 8; $i++) {
if ($box[$i] == ”) {
$blanck = 1;
}
}

if ($blanck == 1 && $WINNER == ‘n’) {
$i = rand() % 8;
while ($box[$i] != ”) {
$i = rand() % 8;
}
$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[4] == ‘o’ && $box[7] == ‘o’) ||
($box[2] == ‘o’ && $box[5] == ‘o’ && $box[8] == ‘o’) ||
($box[0] == ‘o’ && $box[4] == ‘o’ && $box[8] == ‘o’) ||
($box[2] == ‘o’ && $box[4] == ‘o’ && $box[6] == ‘o’)) {
$WINNER = ‘o’;
echo ‘WINNER IS: O’;
}
} else {
$WINNER =’t’;
print ‘Tied game’;
}
}
?>
<html>
<head>
<meta charset=”UTF-8″>
<title></title>
</head>
<body>
<form name=”tictactoe” action=”index.php” method=”POST”>
<?php

for ($i = 0; $i <= 8; $i++) {

printf(‘<input type=”text” name=”box%s” value=”%s” />’, $i, $box[$i]);
if ($i == 2 || $i == 5 || $i == 8) {
echo ‘</br>’;
}
}
if ($WINNER == ‘n’) {
printf(‘<input type = “submit” value = “GO” name = “sb” />’);
} else {
printf(‘<input type=”reset” value=”Play again” name=”newGame” onclick=”window.location.href=’index.php'” />’);
}
?>
</form>
</body>
</html>
[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@GravyJun 09.2014 — Please use [php ] [/php ] tags around your code, it makes it much more simple to read.

It may also help to mention what is wrong. What exactly do you need help with?
Copy linkTweet thisAlerts:
@NogDogJun 09.2014 — I would only use printf() if you are going to use place-holders in the string with additional arguments, otherwise just use echo.

I would try to come up with an algorithm that detected 3-in-a-row conditions that could be applied to a function call, returning the winner if there is one, else null or false. That would help with the DRY principle (Don't Repeat Yourself).

As in the previous reply, though, do you have any specific problem/question you need addressed?
Copy linkTweet thisAlerts:
@deathshadowJun 09.2014 — Some advice:

1) as of PHP 5.4, you don't have to say "array" anymore.

2) I'd make it a 2 dimensional array, it's easier to keep track of [2,2] than it is [9].

3) I'd set the array as empty on an else, so you aren't wasting time setting it if $_POST is set for it.

4) I'd functionalize the check since you're doing the same set of 'if' for both 'x' and 'o'.

5) and yeah I'm with @NogDog on this, I'd lose the printF. It's slow and often hard to follow.

6) if you made the names of your input elements name="box[0][0]", you could just do $box = $_POST['box'] instead of the massive list of manual copies.

7) I'd consider making empty cells be radio buttons so the user can simply click one cell, then use some JS to trap 'onchange' to submit the form. Likewise I'd consider making filled out fields "readonly", or not even send that data client-side and instead track it in sessions. That last option could work our really well and would have a lot less overhead.

If I have time later this week I'll try to see if I can spare a few to toss together a demo of that approach.
Copy linkTweet thisAlerts:
@NogDogJun 09.2014 — [url=http://rowdy.msudenver.edu/~gordona/cs1050/progs/tictactoermccsc.pdf]A General Algorithm for Tic-Tac-Toe Board Evaluation[/url] (PDF)

Okay, back to work for me, even though that looks more interesting.
×

Success!

Help @jaka_moto75 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...