patternphpModerate
Simple Tic-Tac-Toe PHP/Pure HTML
Viewed 0 times
simplepuretoephptictachtml
Problem
I've been doing a simple Tic-Tac-Toe with PHP, generating only HTML code.
A few notes:
Now I don't know if I did everything correctly, here are my concern:
A few notes:
- I didn't bother yet to give an AI to the opponents (the Os), and it is intentional.
- There is no CSS style to it, I intend to append it with a css file.
- tictactoe.php is naturally the name of the script itself, so each links are refering to the page itself.
Now I don't know if I did everything correctly, here are my concern:
- If I want to expand the functionalities of the game in the future (adding javascript, opponent AIs, new game features), I fear that the structure of the code is not adapted for it. Should I change my code so it is Object Oriented? EDIT I have in mind to add new feature to the game, like powers who would change the state of the board, like rewind to a previous state and; some css and javascript in a non-intrusive way and add some way to check if options are disabled (like javascript/flash), or check for the version of the browser for html5/css3 features.
- As for now, it is easy to "cheat" and directly introduce the wished board state. I would like to prevent that, and if possible without using javascript, as I want the game to be playable also without it.
';}
echo '';
//representation of the player token, depending on the ID
if($values[$i]==1){
echo 'X';
}else if($values[$i]==-1){
echo 'O';
}else{
//If noone put a token on this, and if noone won, make a link to allow player X to
//put its token here. Otherwise, empty space.
if($winstate==0){
$values_link = $values;
$values_link[$i]=1;
echo ' ';
}else{
echo ' ';
}
}
echo '';
//end of a row
if(fmod($i,3)==2){echo '';}
}
?>
Player '.(($winstate==1)?'X':'O').' won!';
}
?>Solution
There isn't really a reason to make this "more object-oriented" if you don't need to. To add more AI constructs, for example, you can do:
Adding more features could be done in a similiar manner. I'm not sure exacly wht you have in mind, so I can only make some general comments. Break down everything into functions, and make sure that it's easy to see the flow of the program. The way you've structured your code is good. It should not be difficult to add more features if you use similiar style.
CSS won't affect your script; it just changes how the page looks. You'll just need to be sure you use the right elements when outputting html. Javascript may be trickier, but there are many ways of doing that including form elements (possibly hidden) and page submits. There may be other ways that I'm not aware of as I'm not primarily a web programmer.
I believe when you are talking about cheating, you're saying that you can send a winning board value set to the script by typing the URL yourself. To prevent this you need a way to preserve state. A cookie would be an easy way to start; store the current state of the board in it after every page call, and check that the only change was the placing of another piece. A more robust, but somewhat more involved, solution would be to use session variables to store the state. This would avoid the "cookys are bad" problem and the possibility that someone might fake the cooky!
switch ($aiLevel) {
case 1: $values = $OsTurn($values);
case 2: $values = $betterAi($values);
case 3: $values = $unbeatableAi($values);
}Adding more features could be done in a similiar manner. I'm not sure exacly wht you have in mind, so I can only make some general comments. Break down everything into functions, and make sure that it's easy to see the flow of the program. The way you've structured your code is good. It should not be difficult to add more features if you use similiar style.
CSS won't affect your script; it just changes how the page looks. You'll just need to be sure you use the right elements when outputting html. Javascript may be trickier, but there are many ways of doing that including form elements (possibly hidden) and page submits. There may be other ways that I'm not aware of as I'm not primarily a web programmer.
I believe when you are talking about cheating, you're saying that you can send a winning board value set to the script by typing the URL yourself. To prevent this you need a way to preserve state. A cookie would be an easy way to start; store the current state of the board in it after every page call, and check that the only change was the placing of another piece. A more robust, but somewhat more involved, solution would be to use session variables to store the state. This would avoid the "cookys are bad" problem and the possibility that someone might fake the cooky!
Code Snippets
switch ($aiLevel) {
case 1: $values = $OsTurn($values);
case 2: $values = $betterAi($values);
case 3: $values = $unbeatableAi($values);
}Context
StackExchange Code Review Q#366, answer score: 10
Revisions (0)
No revisions yet.