Termination Modules
Learn how can we terminate our game.
We'll cover the following
What we have
We have completed all the steps to be able to play the game.
Considering these steps, we won’t be able to end the game because there is still no check when the game will end.
Requirements of this module
After placing each move, we will have to check for the game’s end. Let’s divide the process into two steps.
-
Check for the win
-
Check for the tie
Once again, let’s plan the major steps of deciding the termination of the game by implementing the gameEnds()
function. We can define the details later while defining the subordinate modules.
-
Check if any player wins by probing through the updated board by calling the function
checkWin(board, currentPlayer)
. If the answer is YES (true
) then:-
Display the updated board (because we are not going to display it in the next iteration of the loop).
-
Display a message on the new line “Player X wins” or “Player O wins” as per current player.
-
Return
true
because the game ends here.
-
-
Check if there is no empty cell on the updated board by calling the function
checkTie(board)
. If the answer is YES (true
) then:-
Display the updated board (because we are not going to display it in the next iteration of the loop).
-
Display a message on the new line “The game is a tie.”.
-
Return
true
because the game ends here.
-
-
Return
false
(because the game did not end as per above checks).
Note: We should not worry about the details of subordinate functions (
checkWin(board, currentPlayer)
andcheckTie(board)
) at this stage.
Try it yourself
All the functions are already defined. You just have to implement the given function and execute the code.
Get hands-on with 1400+ tech skills courses.