编写反四子棋的代码
时间: 2023-07-02 22:08:49 浏览: 32
反四子棋是一种棋类游戏,玩家需要在棋盘上落子,目标是先将自己的四子连成一线。下面是一个简单的反四子棋代码实现,使用 Python 语言编写:
```python
import random
# 定义棋盘大小和玩家信息
BOARD_SIZE = 15
PLAYER_BLACK = 1
PLAYER_WHITE = 2
# 定义棋盘类
class Board:
def __init__(self):
self.board = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
self.current_player = PLAYER_BLACK
self.is_game_over = False
# 落子函数
def move(self, x, y):
if self.board[x][y] == 0:
self.board[x][y] = self.current_player
if self.check_win(x, y):
self.is_game_over = True
self.current_player = PLAYER_BLACK if self.current_player == PLAYER_WHITE else PLAYER_WHITE
# 检查是否胜利
def check_win(self, x, y):
# 检查行
count = 0
for i in range(BOARD_SIZE):
if self.board[i][y] == self.current_player:
count += 1
else:
count = 0
if count == 4:
return True
# 检查列
count = 0
for j in range(BOARD_SIZE):
if self.board[x][j] == self.current_player:
count += 1
else:
count = 0
if count == 4:
return True
# 检查主对角线
count = 0
i, j = x, y
while i > 0 and j > 0:
i -= 1
j -= 1
while i < BOARD_SIZE and j < BOARD_SIZE:
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count == 4:
return True
i += 1
j += 1
# 检查副对角线
count = 0
i, j = x, y
while i > 0 and j < BOARD_SIZE - 1:
i -= 1
j += 1
while i < BOARD_SIZE and j >= 0:
if self.board[i][j] == self.current_player:
count += 1
else:
count = 0
if count == 4:
return True
i += 1
j -= 1
return False
# 打印棋盘
def print_board(self):
for i in range(BOARD_SIZE):
row = ''
for j in range(BOARD_SIZE):
if self.board[i][j] == PLAYER_BLACK:
row += 'X '
elif self.board[i][j] == PLAYER_WHITE:
row += 'O '
else:
row += '- '
print(row)
# 定义游戏类
class Game:
def __init__(self):
self.board = Board()
# 运行游戏
def run(self):
while not self.board.is_game_over:
self.board.print_board()
x, y = self.get_next_move()
self.board.move(x, y)
self.board.print_board()
print('Player', self.board.current_player, 'wins!')
# 随机落子函数
def get_next_move(self):
while True:
x = random.randint(0, BOARD_SIZE - 1)
y = random.randint(0, BOARD_SIZE - 1)
if self.board.board[x][y] == 0:
return x, y
# 开始游戏
game = Game()
game.run()
```
这个代码实现的反四子棋游戏是一个控制台程序,玩家和 AI 都是随机落子的。你可以根据需要对代码进行修改,例如添加更智能的 AI 策略、优化落子规则等。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)