简易五子棋游戏。设计程序实现一个人机对奕的简单五子棋游戏。游戏规则如下::在19*19的围棋棋盘的交叉点上,对奕双方轮流放子,最先在棋盘上摆成连续五子的一方为胜方
时间: 2024-05-01 19:23:17 浏览: 158
简单的五子棋游戏
好的,我可以为您提供 Python 代码实现一个简易五子棋游戏。以下是代码实现:
```python
import numpy as np
# 定义棋盘大小
BOARD_SIZE = 19
# 定义棋子类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义游戏状态
GAME_OVER = -1
GAME_CONTINUE = 0
class Chessboard:
def __init__(self):
# 初始化棋盘
self.board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=np.int32)
def print_board(self):
# 打印棋盘
print(" ", end="")
for i in range(BOARD_SIZE):
print(chr(ord('A') + i), end=" ")
print()
for i in range(BOARD_SIZE):
print("%2d" % (i + 1), end=" ")
for j in range(BOARD_SIZE):
if self.board[i][j] == BLACK:
print("●", end=" ")
elif self.board[i][j] == WHITE:
print("○", end=" ")
else:
print("+", end=" ")
print()
def is_valid_move(self, x, y):
# 判断落子是否合法
if x >= BOARD_SIZE or y >= BOARD_SIZE:
return False
if self.board[x][y] != EMPTY:
return False
return True
def make_move(self, x, y, chess_type):
# 落子
if self.is_valid_move(x, y):
self.board[x][y] = chess_type
return True
return False
def check_win(self, chess_type):
# 判断是否五子连珠
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if self.board[i][j] == chess_type:
if j + 4 < BOARD_SIZE and self.board[i][j + 1] == chess_type and self.board[i][j + 2] == chess_type and self.board[i][j + 3] == chess_type and self.board[i][j + 4] == chess_type:
return True
if i + 4 < BOARD_SIZE and self.board[i + 1][j] == chess_type and self.board[i + 2][j] == chess_type and self.board[i + 3][j] == chess_type and self.board[i + 4][j] == chess_type:
return True
if i + 4 < BOARD_SIZE and j + 4 < BOARD_SIZE and self.board[i + 1][j + 1] == chess_type and self.board[i + 2][j + 2] == chess_type and self.board[i + 3][j + 3] == chess_type and self.board[i + 4][j + 4] == chess_type:
return True
if i - 4 >= 0 and j + 4 < BOARD_SIZE and self.board[i - 1][j + 1] == chess_type and self.board[i - 2][j + 2] == chess_type and self.board[i - 3][j + 3] == chess_type and self.board[i - 4][j + 4] == chess_type:
return True
return False
class Player:
def __init__(self, chess_type):
self.chess_type = chess_type
def get_move(self):
# 玩家输入落子坐标
while True:
try:
move = input("请输入落子坐标(如 A1):")
x = int(move[1:]) - 1
y = ord(move[0].upper()) - ord('A')
return x, y
except:
print("输入格式错误,请重新输入!")
def make_move(self, chessboard):
# 玩家落子
while True:
x, y = self.get_move()
if chessboard.make_move(x, y, self.chess_type):
break
else:
print("此处不能落子,请重新输入!")
def print_win_message(self):
print("恭喜你获胜了!")
class AI(Player):
def __init__(self, chess_type):
super().__init__(chess_type)
def make_move(self, chessboard):
# AI 落子
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if chessboard.make_move(i, j, self.chess_type):
if chessboard.check_win(self.chess_type):
return
chessboard.board[i][j] = EMPTY
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if chessboard.make_move(i, j, BLACK if self.chess_type == WHITE else WHITE):
if chessboard.check_win(BLACK if self.chess_type == WHITE else WHITE):
chessboard.make_move(i, j, self.chess_type)
return
chessboard.board[i][j] = EMPTY
while True:
x = np.random.randint(BOARD_SIZE)
y = np.random.randint(BOARD_SIZE)
if chessboard.make_move(x, y, self.chess_type):
return
def play_game():
# 初始化棋盘和玩家
chessboard = Chessboard()
player = Player(BLACK)
ai = AI(WHITE)
# 游戏开始
print("游戏开始!")
chessboard.print_board()
while True:
player.make_move(chessboard)
chessboard.print_board()
if chessboard.check_win(player.chess_type):
player.print_win_message()
return
if np.count_nonzero(chessboard.board == EMPTY) == 0:
print("平局!")
return
ai.make_move(chessboard)
print("AI 落子:")
chessboard.print_board()
if chessboard.check_win(ai.chess_type):
print("AI 获胜!")
return
if np.count_nonzero(chessboard.board == EMPTY) == 0:
print("平局!")
return
if __name__ == "__main__":
play_game()
```
运行后,会提示输入落子坐标,例如输入 A1 表示在第一行第一列落子。玩家与 AI 轮流落子,最先连成五子的一方获胜。如果棋盘填满仍未分出胜负,则为平局。
阅读全文