import os
import time
board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
player = 1
########胜负标志##########
Win = 1
Draw = -1
Running = 0
Stop = 1
###########################
Game = Running
Mark = 'X'
# 画棋盘
def DrawBoard():
print(" %c | %c | %c " % (board[1], board[2], board[3]))
print("___|___|___")
print(" %c | %c | %c " % (board[4], board[5], board[6]))
print("___|___|___")
print(" %c | %c | %c " % (board[7], board[8], board[9]))
print(" | | ")
# 检查棋盘的空位
def CheckPosition(x):
if (board[x] == ' '):
return True
else:
return False
# 判断胜负
def CheckWin():
global Game
# 横着方向赢
if (board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
Game = Win
elif (board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
Game = Win
elif (board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
Game = Win
# 竖着方向赢
elif (board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
Game = Win
elif (board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
Game = Win
elif (board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
Game = Win
# 对角线方向赢
elif (board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
Game = Win
elif (board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
Game = Win
# 平局
elif (board[1] != ' ' and board[2] != ' ' and board[3] != ' ' and board[4] != ' ' and board[5] != ' ' and board[
6] != ' ' and board[7] != ' ' and board[8] != ' ' and board[9] != ' '):
Game = Draw
else:
Game = Running
print("井字棋双人小游戏")
print("如有错误还望指出,谢谢")
print("Player 1 [X] --- Player 2 [O]\n")
print("等一下,马上好...")
time.sleep(3)
while (Game == Running):
os.system('cls')
DrawBoard()
if (player % 2 != 0):
print("该Player 1了")
Mark = 'X'
else:
print("该player2了")
Mark = 'O'
choice = int(input("输入数字 [1-9] "))
if (CheckPosition(choice)):
board[choice] = Mark
player += 1
CheckWin()
os.system('cls')
DrawBoard()
if (Game == Draw):
print("平局")
elif (Game == Win):
player -= 1
if (player % 2 != 0):
print("Player 1 赢了")
else:
print("Player 2 赢了")
如题,代码如上,球球了