把下面代码换成Python#includeiostream#includecstdlib#includectimeusingnamespacestd;储存棋盘状态的二维数组intchessBoard88=0000000000000000000000000000000000000000000000000000000000000000;判断胜负boolisWinintcolor判断横向连续五个棋子fo
chessBoard = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]
def isWin(color): # 判断横向连续五个棋子 for i in range(8): for j in range(4): if chessBoard[i][j] == color and chessBoard[i][j+1] == color and chessBoard[i][j+2] == color and chessBoard[i][j+3] == color and chessBoard[i][j+4] == color: return True # 判断纵向连续五个棋子 for i in range(4): for j in range(8): if chessBoard[i][j] == color and chessBoard[i+1][j] == color and chessBoard[i+2][j] == color and chessBoard[i+3][j] == color and chessBoard[i+4][j] == color: return True # 判断正斜向连续五个棋子 for i in range(4): for j in range(4): if chessBoard[i][j] == color and chessBoard[i+1][j+1] == color and chessBoard[i+2][j+2] == color and chessBoard[i+3][j+3] == color and chessBoard[i+4][j+4] == color: return True # 判断反斜向连续五个棋子 for i in range(4): for j in range(4, 8): if chessBoard[i][j] == color and chessBoard[i+1][j-1] == color and chessBoard[i+2][j-2] == color and chessBoard[i+3][j-3] == color and chessBoard[i+4][j-4] == color: return True return False
def evaluate(color, x, y): score = 0 count = 0 # 横向 for i in range(x-1, -1, -1): if chessBoard[y][i] == color: count += 1 else: break for i in range(x+1, 8): if chessBoard[y][i] == color: count += 1 else: break if count == 1: score += 1 elif count == 2: score += 10 elif count == 3: score += 100 elif count == 4: score += 1000 count = 0 # 纵向 for i in range(y-1, -1, -1): if chessBoard[i][x] == color: count += 1 else: break for i in range(y+1, 8): if chessBoard[i][x] == color: count += 1 else: break if count == 1: score += 1 elif count == 2: score += 10 elif count == 3: score += 100 elif count == 4: score += 1000 count = 0 # 正斜向 for i, j in zip(range(x-1, -1, -1), range(y-1, -1, -1)): if chessBoard[j][i] == color: count += 1 else: break for i, j in zip(range(x+1, 8), range(y+1, 8)): if chessBoard[j][i] == color: count += 1 else: break if count == 1: score += 1 elif count == 2: score += 10 elif count == 3: score += 100 elif count == 4: score += 1000 count = 0 # 反斜向 for i, j in zip(range(x+1, 8), range(y-1, -1, -1)): if chessBoard[j][i] == color: count += 1 else: break for i, j in zip(range(x-1, -1, -1), range(y+1, 8)): if chessBoard[j][i] == color: count += 1 else: break if count == 1: score += 1 elif count == 2: score += 10 elif count == 3: score += 100 elif count == 4: score += 1000 return score
def humanPlayer(): x, y = map(int, input("请落子(x,y):\n").split()) while chessBoard[y][x] != 0: x, y = map(int, input("该位置已有棋子,请重新落子(x,y):\n").split()) chessBoard[y][x] = 1
def computerPlayer(): maxScore = 0 for i in range(8): for j in range(8): if chessBoard[i][j] == 0: score = evaluate(2, j, i) if score > maxScore: maxScore = score x, y = j, i chessBoard[y][x] = 2
def printChessBoard(): print(" ", end="") for i in range(8): print(i, end="") print() for i in range(8): print(i, end="") for j in range(8): if chessBoard[i][j] == 0: print("+", end="") elif chessBoard[i][j] == 1: print("●", end="") else: print("○", end="") print()
turn = 1 # 1表示人类玩家,2表示电脑玩家 while not isWin(1) and not isWin(2): printChessBoard() if turn == 1: humanPlayer() else: computerPlayer() turn = 3 - turn # 切换玩家
printChessBoard() if isWin(1): print("人类玩家获胜!") else: print("电脑玩家获胜!"
原文地址: https://www.cveoy.top/t/topic/eDI8 著作权归作者所有。请勿转载和采集!