Checking for a Win
In this lesson, the final version of the tic-tac-toe game will be implemented.
Requirements
- after a player's move, determine whether they won
- display a victory message Player {player name} won!!!
- end the game
Checking if a player won
To win, a player must fill an entire row, column, or diagonal. Let's list all winning combinations:
- first row
- second row
- third row
- first column
- second column
- third column
- first diagonal
- second diagonal
Determining the function signature
The function should determine whether a player won, so the return value will be
bool, and we'll call it CheckIsWin. To do its job, the function needs to know the player's name and see the board — these will be its parameters. The function body will check winning combinations. This gives us the signature bool CheckIsWin(string name, string[] board) Line 1, Char 1
Exercises
- Complete the remaining winning combinations.
Line 1, Char 1
- After a player makes a move, check whether they won. If they did, print a message like Player {player name} won!!! and end the game (exit the loop). Implement this behavior.
Line 1, Char 1
- The game is ready! Play it. Does it work correctly? Do you like everything about it? You can always improve the program and make the computer work however you want.
Line 1, Char 1