Python-井字棋游戏

x33g5p2x  于2021-11-09 转载在 Python  
字(2.6k)|赞(0)|评价(0)|浏览(538)

1.先进行数据建模

  1. # 游戏棋盘
  2. theBoard = {'top-L': '', 'top-M': '', 'top-R': '',
  3. 'mid-L': '', 'mid-M': '', 'mid-R': '',
  4. 'low-L': '', 'low-M': '', 'low-R': ''
  5. }
  6. # 棋盘坐标
  7. print("----------棋盘坐标---------------")
  8. print('top-L|top-M|top-R')
  9. print('mid-L|mid-M|mid-R')
  10. print('low-L|low-M|low-R')
  11. # 打印棋盘
  12. print("----------实际棋盘---------------")
  13. print(theBoard['top-L'] + '|' + theBoard['top-M'] + '|' + theBoard['top-R'])
  14. print('-+-+-')
  15. print(theBoard['mid-L'] + '|' + theBoard['mid-M'] + '|' + theBoard['mid-R'])
  16. print('-+-+-')
  17. print(theBoard['low-L'] + '|' + theBoard['low-M'] + '|' + theBoard['low-R'])

效果如下:

有了棋盘建模了,那么我们就需要写逻辑了,让棋盘动起来

2.使用逻辑让数据动起来

完整代码如下:

  1. def printBoard(theBoard):
  2. pd = 1 # 我方和敌方
  3. while True:
  4. # 效验棋盘是否满了
  5. checkerboard_count = 9 # 井字游戏棋盘棋子的个数是9
  6. for v in theBoard.values():
  7. v = v.strip() # 去除空格
  8. if v != '' and len(v) > 0:
  9. checkerboard_count -= 1
  10. if checkerboard_count == 0:
  11. print("棋盘已满游戏结束---平局")
  12. return None
  13. print("请" + str(pd) + "号选手输入下棋位置:")
  14. str_v = input()
  15. # 判断是否有这个棋子的坐标
  16. if str_v not in theBoard.keys():
  17. print("----对不起没有此棋子坐标请从新输入:")
  18. continue
  19. # 判断棋子是否已经存在
  20. if theBoard[str_v].strip()!='' or len(theBoard[str_v].strip())>0:
  21. print("已下过次棋-请从新选择下棋位置")
  22. continue
  23. if pd == 1:
  24. theBoard[str_v] = 'O'
  25. else:
  26. theBoard[str_v] = 'X'
  27. print(theBoard['top-L'] + '|' + theBoard['top-M'] + '|' + theBoard['top-R'])
  28. print('-+-+-')
  29. print(theBoard['mid-L'] + '|' + theBoard['mid-M'] + '|' + theBoard['mid-R'])
  30. print('-+-+-')
  31. print(theBoard['low-L'] + '|' + theBoard['low-M'] + '|' + theBoard['low-R'])
  32. win = pd_win(theBoard)
  33. if win == 'X':
  34. print("2号选手胜利-游戏结束")
  35. return None
  36. elif win == 'O':
  37. print("1号选手胜利-游戏结束")
  38. return None
  39. # 转换为2|1号选手
  40. if pd == 1:
  41. pd = 2
  42. elif pd == 2:
  43. pd = 1
  44. def pd_win(theBoard):
  45. # 效验是否胜利
  46. win_chess = [[0, 4, 8], [2, 4, 6], [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8]]
  47. for i in win_chess: # 每次执行一次可能性
  48. pd_board_3 = 0 # 记录子循环次数
  49. record1 = 0 # X成绩记录
  50. record2 = 0 # O成绩记录
  51. for k, v in theBoard.items():
  52. if pd_board_3 in i:
  53. if v == 'X':
  54. record1 += 1
  55. if v == 'O':
  56. record2 += 1
  57. pd_board_3 += 1
  58. if record1 == 3:
  59. return 'X'
  60. elif record2 == 3:
  61. return 'O'
  62. if __name__ == '__main__':
  63. # 游戏棋盘
  64. theBoard = {'top-L': '', 'top-M': '', 'top-R': '',
  65. 'mid-L': '', 'mid-M': '', 'mid-R': '',
  66. 'low-L': '', 'low-M': '', 'low-R': ''
  67. }
  68. # 棋盘坐标
  69. print("----------棋盘坐标---------------")
  70. print('top-L|top-M|top-R')
  71. print('mid-L|mid-M|mid-R')
  72. print('low-L|low-M|low-R')
  73. # 打印棋盘
  74. print("----------实际棋盘---------------")
  75. print(theBoard['top-L'] + '|' + theBoard['top-M'] + '|' + theBoard['top-R'])
  76. print('-+-+-')
  77. print(theBoard['mid-L'] + '|' + theBoard['mid-M'] + '|' + theBoard['mid-R'])
  78. print('-+-+-')
  79. print(theBoard['low-L'] + '|' + theBoard['low-M'] + '|' + theBoard['low-R'])
  80. # 提示
  81. print("井字棋规则: 选手分为1号选手和二号选手, 一号选手先下棋(自行分配)----游戏即将开始")
  82. # 用户输入
  83. printBoard(theBoard)

效果演示

至于其他可能性我都写了,这里就不一一展示了
比如: 棋盘满了----(ok),重复下棋----(ok)
纯手写----给点鼓励(点赞 -收藏-关注)有其他问题在评论区讨论-或者私信我-收到会在第一时间回复如有侵权,请私信联系我感谢,配合,希望我的努力对你有帮助^_^

相关文章