圈叉棋 Tic Tac Toe实现与AI分析

简介

井字棋 圈叉棋 Tic Tac Toe
圈叉棋 Tic Tac Toe实现与AI分析_第1张图片

原理

用数组代表棋子的位置,可以1x9也可以3x3

判断胜利

井字棋有八种胜利的方式,三横三竖两对角线,逐个遍历即可判定是否胜利,参考一就是遍历六次加上对角线,个人觉得参考三的七个or更优美简洁一点吧。

AI算法

算法思路在书上写的很明白

The AI’s algorithm will have the following steps:

  1.  First, see if there’s a move the computer can make that will win the game. If there is, take that move. Otherwise, go to step 2.
    
  2.  See if there’s a move the player can make that will cause the computer to lose the game. If there is, move there to block the player. Otherwise, go to step 3.
    
  3.  Check if any of the corner spaces (spaces 1, 3, 7, or 9) are free. If so, move there. If no corner piece is free, then go to step 4.
    
  4.  Check if the center is free. If so, move there. If it isn’t, then go to step 5.
    
  5.  Move on any of the side pieces (spaces 2, 4, 6, or 8). There are no more steps, because if the execution reaches step 5 the side spaces are the only spaces left.
    

参考

  • C语言小游戏——圈叉游戏
  • 圈叉棋
  • Chapter 10TIC TAC TOE:getBoardCopy(board)可以用copy替代,没必要造轮子

你可能感兴趣的:(Algorithm,算法,游戏)