写一个Python语言的五子棋小游戏,只要源代码
时间: 2023-03-16 12:43:56 浏览: 142
我可以给你提供一个简单的Python语言的五子棋小游戏的源代码:# 定义棋盘
board = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]# 定义棋子
x = 1
o = -1# 判断当前棋盘是否有赢家
def isWin():
for i in range(3):
if board[i][0] + board[i][1] + board[i][2] == 3:
return True
elif board[0][i] + board[1][i] + board[2][i] == 3:
return True
if board[0][0] + board[1][1] + board[2][2] == 3:
return True
elif board[2][0] + board[1][1] + board[0][2] == 3:
return True
else:
return False# 玩家落子
def player(x, y):
if board[x][y] == 0:
board[x][y] = o# 电脑落子
def computer():
for i in range(3):
for j in range(3):
if board[i][j] == 0:
board[i][j] = x
if isWin():
return
board[i][j] = 0# 开始游戏
def start():
while True:
print("请玩家落子:")
x = int(input("请输入横坐标:"))
y = int(input("请输入纵坐标:"))
player(x, y)
if isWin():
print("玩家赢了!")
break
computer()
if isWin():
print("电脑赢了!")
break# 开始游戏
start()
阅读全文