A CLI program to play the game Connect Four.
npm i
To run a game with a stared not empty board, you need to provide a json file to load an initial state of the game board. (please read part 'How to use it with a given json file')
npm run dev ./src/testConfig.json
To run a fresh game with en empty board : (please read part 'How to use it without any starting state')
npm run dev ./src/testConfig.json
npm test
docker build -t ts-cli-app .
docker run ts-cli-app
Based on this model:
[
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
]
It's a representation of the game grid: each 0 represents an empty space. To place a token, you have to replace the 0 character:
- 0 for empty space
- 1 for a player 1 token
- 2 for a player 2 token
For example, if player 1 placed a token in the third column :
[
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
]
... and if player 2 places a token in the same column :
[
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 2, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
]
... and so on.
Like this wrong exemple:
[
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 2, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
]
=> tokens must fall to the bottom of the column.
Each player must have the same number of tokens +/-1 (depending on who have played first).
Save the game grid where you want, in the format shown above, in a text (json) file.
Ex: myGameGrid.json
Once the game started, the board displayed, representing the provided json file.
Your token character is indicated below -- You are player 2: x --
.
To add a token you have to choose a column number according to the display 1 2 3 4 5 6 7
.
Put your choice Please enter the column number you want to play your token o [1-7]:
then press enter
It's now the second player turn.
Once the game has started, a player is chosen at random. Then each player in turn plays a token.
You have 3 end game conditions :
- Player 1 win
- Player 2 win
- It's a game draw
The game automatically detects victory and highlights the 4 victorious tokens in a row.