Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lostsh/morpion
Simple PHP TIC-TAC-TOE game
https://github.com/lostsh/morpion
care game grid heroku heroku-app heroku-deployment morpion online-game php php7 player
Last synced: 17 days ago
JSON representation
Simple PHP TIC-TAC-TOE game
- Host: GitHub
- URL: https://github.com/lostsh/morpion
- Owner: lostsh
- License: mit
- Created: 2020-11-21T16:20:33.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-21T13:15:19.000Z (almost 3 years ago)
- Last Synced: 2025-01-17T10:42:25.012Z (20 days ago)
- Topics: care, game, grid, heroku, heroku-app, heroku-deployment, morpion, online-game, php, php7, player
- Language: PHP
- Homepage: https://more-pion.herokuapp.com/
- Size: 49.8 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Morpion
ceci est une version très minimaliste du jeu du morpion (TIC-TAC-TOE), basée sur l'utilisation des `sessons` en PHP.If you want to play it, it is available on [this site](https://more-pion.herokuapp.com/). You just have to click "play now".
The project repository site is available at [this address](https://lostsh.github.io/morpion/), and the repository code is [here on github](https://github.com/lostsh/morpion).
![Box just for fun](box.jpg)
## operating mode
The principle could not be simpler:
Let's build the page first, and we'll take care of making it work with the sessions in a second step.
1. With php we display an html table, in each cell we put a ` ` tag with as a link the number of the row on which the cell is located, and the number of the column (for pass them using a GET request)
```html
jouer
jouer
jouer
jouer
jouer
jouer
jouer
jouer
jouer
```
2. Once we have a nice grid we will add an `if` without putting any conditions there we can set it to` true` we will take care of the condition when we add the sessions. The objective is to obtain a grid of this type:
```html
X
X
jouer
O
jouer
jouer
O
jouer
O
```
To build something like this:
```php
echo("");
for($i=0;$i<3;$i++){
echo("");
for($j=0;$j<3;$j++){
if(true){echo("jouer");
}else{ //we will never go to the else, but it's ready for later
if(true){
echo("X");
}else{
echo("O");
}
}
}
echo("");
}
echo("");
```Now we can take care of the sessions to really make the noughts and crosses work, to simplify the task we are not going to worry about player 1 / player 2 we will do that later.
To store the state of the game we are going to use an array of 3 boxes by 3 boxes (nobody expected it eh?). And we're just going to ask our session to store our table, and give it back to us when we reload the page.
To do this, we can do a function which aims to initialize a 2D table filled with 0.
```
[0][0][0]
[0][0][0]
[0][0][0]
```
The function `intiTab()` return a 2D array.
```php
function initTab(){
$tab = array();
for($i=0;$i<3;$i++){
for($j=0;$j<3;$j++){
$tab[$i][$j] = 0;
}
}
return $tab;
}
```
So let's go to the grid itself, we will be able to put a condition in the `if` earlier.
```php
echo("");
for($i=0;$i<3;$i++){
echo("");
for($j=0;$j<3;$j++){
if($gameStatus[$i][$j] == 0){
echo("jouer");
}else{
if($gameStatus[$i][$j] == 1){
echo("X");
}else{
echo("O");
}
}
}
echo("");
}
echo("");
```Now, we must put this magnificent game board in our session, if it exists we update it, otherwise we create it, we could see something like this:
```php
$gameStatus = initTab();
if(isset($_SESSION['gameStatus'])){
$gameStatus = $_SESSION['gameStatus'];
}else{
$_SESSION['gameStatus'] = $gameStatus;
}
```So now, when we use the GET parameters that we put when we click on a box. To do so, we will simply say that if GET exists, then update the games teablau, and update the game board that we put in the session!
```php
if(
isset($_GET['col']) &&
isset($_GET['lig']) &&
isset($_GET['player'])
)
{
$gameStatus[$_GET['lig']][$_GET['col']] = 'X';
$_SESSION['gameStatus'] = $gameStatus;
}
```Now it works! well done ! now we will be able to play each turn, we will simply add a player attribute in our session which will be worth either 1 or 2
We create it like this:
```php
if(isset($_SESSION['player'])){//this allows to reverse the current player compared to the previous player
if($_SESSION['player'] == 1){
$_SESSION['player'] = 2;
}else{
$_SESSION['player'] = 1;
}}else{
$_SESSION['player'] = random(1,2);
}
```you have to update the array so that it is given as a parameter in the get like this:
```php
echo("");
for($i=0;$i<3;$i++){
echo("");
for($j=0;$j<3;$j++){
if($gameStatus[$i][$j] == 0){
echo("jouer");
}else{
if($gameStatus[$i][$j] == 1){
echo("X");
}else{
echo("O");
}
}
}
echo("");
}
echo("");
```**Well now the game seems to be working more or less well, let's move on to victory conditions!**
Good the most method is also the ugliest, but tempis I will come back to it later! For the moment it works.
A function which returns 0 if the whole row does not have the same value, and if in the whole row we have the same value then we return this value. And even functoin for a colone.
```php
/**
* Return the numer
* if there is the same numer on all the $line of the $tab
* else retrurn 0
*/
function isLineSameValue($line, $tab){
$value = $tab[$line][0];
for($i=0;$i