日期:2022年5月13日
    介绍:1、这是一个简单的井字棋游戏;
       2、玩家通过输入字符控制下棋的位置;
       3、游戏具备判断输赢的功能;
       4、游戏具备判断输入正确性的功能;

    1. ###############################################################
    2. # 日期:2022年5月13日
    3. # 介绍:1、这是一个简单的井字棋游戏;
    4. #    2、玩家通过输入字符控制下棋的位置;
    5. #    3、游戏具备判断输赢的功能;
    6. #    4、游戏具备判断输入正确性的功能;
    7. ###############################################################
    8. import sys
    9. # 创建一个字典,用来存储棋盘位置
    10. theBoard = {'topL': ' ', 'topM': ' ', 'topR': ' ',
    11. 'midL': ' ', 'midM': ' ', 'midR': ' ',
    12. 'lowL': ' ', 'lowM': ' ', 'lowR': ' '}
    13. def printBoard(board): # 生成一个空白的棋盘的函数
    14. print('=================')
    15. print(' ' + board['topL'] + ' | ' + board['topM'] + ' | ' + board['topR'] + ' ')
    16. print('-----+-----+-----')
    17. print(' ' + board['midL'] + ' | ' + board['midM'] + ' | ' + board['midR'] + ' ')
    18. print('-----+-----+-----')
    19. print(' ' + board['lowL'] + ' | ' + board['lowM'] + ' | ' + board['lowR'] + ' ')
    20. print('=================')
    21. def whoWins(whichSide): # 判断哪一方赢得了游戏的函数
    22. while True:
    23. if theBoard['topL'] == theBoard['topM'] and theBoard['topM'] == theBoard['topR'] and theBoard['topM'] == str(whichSide):
    24. break
    25. if theBoard['midL'] == theBoard['midM'] and theBoard['midM'] == theBoard['midR'] and theBoard['midM'] == str(whichSide):
    26. break
    27. if theBoard['lowL'] == theBoard['lowM'] and theBoard['lowM'] == theBoard['lowR'] and theBoard['lowM'] == str(whichSide):
    28. break
    29. if theBoard['topL'] == theBoard['midL'] and theBoard['midL'] == theBoard['lowL'] and theBoard['midL'] == str(whichSide):
    30. break
    31. if theBoard['topM'] == theBoard['midM'] and theBoard['midM'] == theBoard['lowM'] and theBoard['midM'] == str(whichSide):
    32. break
    33. if theBoard['topR'] == theBoard['midR'] and theBoard['midR'] == theBoard['lowR'] and theBoard['midR'] == str(whichSide):
    34. break
    35. if theBoard['topL'] == theBoard['midM'] and theBoard['midM'] == theBoard['lowR'] and theBoard['midM'] == str(whichSide):
    36. break
    37. if theBoard['topR'] == theBoard['midM'] and theBoard['midM'] == theBoard['lowL'] and theBoard['midM'] == str(whichSide):
    38. break
    39. else:
    40. return
    41. print(str(whichSide) + ' 赢得了游戏!')
    42. return sys.exit()
    43. printBoard(theBoard)
    44. sideToPlay = 'X'
    45. gameTimes = 0
    46. boardLeft = ['topL', 'topM', 'topR', 'midL', 'midM', 'midR', 'lowL', 'lowM', 'lowR']
    47. while True:
    48. gameTimes += 1
    49. # 在屏幕上打印出游戏方
    50. print('现在轮到 ' + sideToPlay + ' 下棋')
    51. # 提示剩余的棋盘空格
    52. print('请输入以下步骤之一: ', end='')
    53. for i in range(len(boardLeft)):
    54. print(boardLeft[i], end=', ')
    55. print()
    56. # 判输入的步骤是否可选
    57. while True:
    58. position = input() # 输入棋子位置
    59. if position not in boardLeft:
    60. print('请输入正确的步骤!')
    61. else:
    62. boardLeft.remove(position) # 将棋子位置从可选项中删除
    63. break
    64. theBoard[position] = sideToPlay
    65. printBoard(theBoard)
    66. # 判断此轮赛比是否有人获胜
    67. whoWins(sideToPlay)
    68. # 棋盘铺满仍无人获胜是,宣布平局
    69. if gameTimes == 9:
    70. print('游戏结束,平局!')
    71. break
    72. # 交换游戏双方
    73. if sideToPlay == 'X':
    74. sideToPlay = 'O'
    75. else:
    76. sideToPlay = 'X'