用python写一段五子棋游戏的代码
时间: 2023-08-31 20:44:17 浏览: 120
### 回答1:
# 定义棋盘
board = [
[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]
]# 定义棋子
BLACK = 1
WHITE = 2# 定义游戏状态
CONTINUE = 0
BLACK_WIN = 1
WHITE_WIN = 2# 定义棋子放置函数
def put_piece(x, y, color):
board[x][y] = color# 定义判断输赢函数
def judge(x, y, color):
# 判断横向
count = 0
for i in range(5):
if board[x][i] == color:
count += 1
else:
count = 0
if count == 5:
return color
# 判断纵向
count = 0
for i in range(5):
if board[i][y] == color:
count += 1
else:
count = 0
if count == 5:
return color
# 判断正斜向
count = 0
for i in range(5):
if (x + i < 5) and (y + i < 5) and (board[x + i][y + i] == color):
count += 1
else:
count = 0
if count == 5:
return color
# 判断反斜向
count = 0
for i in range(5):
if (x + i < 5) and (y - i >= 0) and (board[x + i][y - i] == color):
count += 1
else:
count = 0
if count == 5:
return color
# 判断平局
flag = 1
for i in range(5):
for j in range(5):
if board[i][j] == 0:
flag = 0
if flag == 1:
return 3
return 0# 定义主函数
if __name__ == '__main__':
# 定义变量
color = BLACK
result = CONTINUE
# 游戏开始
while result == CONTINUE:
# 输入
x = int(input('x:'))
y = int(input('y:'))
# 放置棋子
put_piece(x, y, color)
# 判断输赢
result = judge(x, y, color)
# 轮换
if color == BLACK:
color = WHITE
else:
color = BLACK
# 输出结果
if result == BLACK_WIN:
print('黑棋获胜')
elif result == WHITE_WIN:
print('白棋获胜')
else:
print('平局')
### 回答2:
下面是一段用Python编写的五子棋游戏代码:
```python
import numpy as np
# 创建棋盘
def create_board():
board = np.zeros((15, 15), dtype=int)
return board
# 判断是否胜利
def check_win(board, row, col, player):
# 横向判断
for i in range(col-4, col+1):
if i >= 0 and i + 4 < 15 and \
board[row][i] == board[row][i+1] == board[row][i+2] == board[row][i+3] == board[row][i+4] == player:
return True
# 纵向判断
for i in range(row-4, row+1):
if i >= 0 and i + 4 < 15 and \
board[i][col] == board[i+1][col] == board[i+2][col] == board[i+3][col] == board[i+4][col] == player:
return True
# 左上到右下判断
for i in range(-4, 1):
if row+i >= 0 and row+i+4 < 15 and col+i >= 0 and col+i+4 < 15 and \
board[row+i][col+i] == board[row+i+1][col+i+1] == board[row+i+2][col+i+2] == board[row+i+3][col+i+3] == board[row+i+4][col+i+4] == player:
return True
# 左下到右上判断
for i in range(-4, 1):
if row+i >= 0 and row+i+4 < 15 and col-i >= 0 and col-i-4 < 15 and \
board[row+i][col-i] == board[row+i+1][col-i-1] == board[row+i+2][col-i-2] == board[row+i+3][col-i-3] == board[row+i+4][col-i-4] == player:
return True
return False
# 打印棋盘
def print_board(board):
for row in board:
row_str = ""
for cell in row:
if cell == 0:
row_str += " _"
elif cell == 1:
row_str += " X"
else:
row_str += " O"
print(row_str)
# 主函数
def main():
board = create_board()
player = 1
while True:
print_board(board)
row = int(input("请输入行号(1-15): ")) - 1
col = int(input("请输入列号(1-15): ")) - 1
if board[row][col] == 0:
board[row][col] = player
if check_win(board, row, col, player):
print_board(board)
print("玩家 {} 胜利!".format(player))
break
else:
player = 2 if player == 1 else 1
else:
print("该位置已经有棋子,请重新输入!")
if __name__ == "__main__":
main()
```
这段代码实现了一个简单的五子棋游戏。开局时,会创建一个15×15的棋盘,并要求玩家依次输入行号和列号来下棋。每个玩家的棋子分别用1和2代表,玩家间轮流下棋。当有一方在横、竖、斜线上连续下了5个棋子时,判定该玩家胜利并游戏结束。棋盘上的空位用下划线"_"表示,下了1号玩家的棋子用X表示,下了2号玩家的棋子用O表示。
### 回答3:
下面是一个简单的用Python编写的五子棋游戏的代码:
```python
# 导入所需的库
import numpy as np
# 创建棋盘
board = np.zeros((15, 15), dtype=int)
player = 1 # 玩家1为黑子,玩家2为白子
def print_board(board):
for row in board:
for piece in row:
print(piece, end=' ')
print()
def check_win(board, row, col):
# 检查行
count = 0
for i in range(max(0, col-4), min(15, col+5)):
if board[row][i] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 检查列
count = 0
for i in range(max(0, row-4), min(15, row+5)):
if board[i][col] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 检查正对角线
count = 0
for i in range(-4, 5):
if (row+i >= 0 and row+i < 15) and (col+i >= 0 and col+i < 15):
if board[row+i][col+i] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 检查反对角线
count = 0
for i in range(-4, 5):
if (row-i >= 0 and row-i < 15) and (col+i >= 0 and col+i < 15):
if board[row-i][col+i] == player:
count += 1
if count == 5:
return True
else:
count = 0
return False
while True:
print_board(board)
# 玩家输入下棋的位置
row = int(input("请输入要落子的行数:"))
col = int(input("请输入要落子的列数:"))
# 检查位置是否合法
if row >= 0 and row < 15 and col >= 0 and col < 15 and board[row][col] == 0:
board[row][col] = player
# 检查玩家是否取得胜利
if check_win(board, row, col):
print_board(board)
print("玩家", player, "获胜!")
break
# 切换玩家
player = 2 if player == 1 else 1
else:
print("无效的位置,请重新输入!")
```
这是一个简单的五子棋游戏代码,玩家可以依次在棋盘上输入要落子的行数和列数,程序会检查是否符合规则并判断玩家是否获胜。程序会在有玩家获胜时打印出胜利消息,并结束游戏。
阅读全文