Tic Tac Toe, Help/Determine Winner

前端 未结 4 873
离开以前
离开以前 2021-01-28 23:42

I\'m creating a TicTacToe class that contains a 3 by 3 rectangular array of integers & is played by 2 human players. \"1\" is used for the first player\'s move & \"2\" i

4条回答
  •  无人共我
    2021-01-29 00:18

    I will try to answer how to detect the draw, as all others have focused only on checking if someone won.

    The game can only be drawn when the board is fully completed, and no other winning combination is formed. So it's not difficult to detect after you checked for winner and haven't found one.

    • Check for winner, if not found, continue detecting for ties.
    • Scan the whole array of the board, no matter in what order.
    • If found an empty spot, it's NOT a tie (further moves are possible), interrupt the loop and continue playing.
    • Once you've reached the end of the loop, declare a draw (all spaces are filled without a winning combination).

提交回复
热议问题