Python 井字棋4*4棋盘人机
时间: 2023-07-29 07:07:20 浏览: 182
好的,让我们来实现一个人机井字棋游戏吧!首先,我们需要安装Pygame模块来创建游戏界面和处理用户交互。你可以在命令行中使用以下命令来安装Pygame:
```
pip install pygame
```
接下来,我们先定义一些全局变量和常量:
```python
import pygame
import random
# 游戏界面尺寸
WIDTH = 400
HEIGHT = 400
# 棋盘尺寸
ROWS = 4
COLS = 4
CELL_SIZE = WIDTH // COLS
# 颜色常量
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# 初始化游戏界面
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("井字棋")
font = pygame.font.SysFont(None, 48)
# 初始化棋盘
board = [[0 for j in range(COLS)] for i in range(ROWS)]
```
接着,我们需要定义以下函数来绘制游戏界面:
```python
def draw_board():
screen.fill(WHITE)
for i in range(ROWS):
for j in range(COLS):
x = j * CELL_SIZE
y = i * CELL_SIZE
pygame.draw.rect(screen, BLACK, (x, y, CELL_SIZE, CELL_SIZE), 1)
if board[i][j] == 1:
pygame.draw.circle(screen, BLUE, (x + CELL_SIZE // 2, y + CELL_SIZE // 2), CELL_SIZE // 2 - 5)
elif board[i][j] == -1:
pygame.draw.line(screen, RED, (x + 5, y + 5), (x + CELL_SIZE - 5, y + CELL_SIZE - 5), 2)
pygame.draw.line(screen, RED, (x + CELL_SIZE - 5, y + 5), (x + 5, y + CELL_SIZE - 5), 2)
pygame.display.update()
```
该函数通过遍历棋盘中的每一个格子来绘制棋盘和棋子。其中,1代表玩家的棋子,-1代表电脑的棋子,0代表空格子。
接下来,我们需要定义以下函数来判断游戏是否结束:
```python
def is_game_over():
# 判断是否有一方获胜
for i in range(ROWS):
if sum(board[i]) == ROWS:
return 1
elif sum(board[i]) == -ROWS:
return -1
if sum([board[j][i] for j in range(ROWS)]) == ROWS:
return 1
elif sum([board[j][i] for j in range(ROWS)]) == -ROWS:
return -1
if sum([board[i][i] for i in range(ROWS)]) == ROWS:
return 1
elif sum([board[i][i] for i in range(ROWS)]) == -ROWS:
return -1
if sum([board[i][ROWS - i - 1] for i in range(ROWS)]) == ROWS:
return 1
elif sum([board[i][ROWS - i - 1] for i in range(ROWS)]) == -ROWS:
return -1
# 判断是否平局
if all([all(row) for row in board]):
return 0
return None
```
该函数通过遍历所有行、列和对角线来检查是否有一方获胜,或者是否平局。如果游戏还没有结束,返回None,否则返回1、-1或0。
接着,我们需要定义以下函数来让电脑下棋:
```python
def computer_move():
empty_cells = [(i, j) for i in range(ROWS) for j in range(COLS) if board[i][j] == 0]
if not empty_cells:
return
i, j = random.choice(empty_cells)
board[i][j] = -1
```
该函数首先找出所有空格子的坐标,然后随机选择一个坐标来下棋。
最后,我们需要在主循环中处理用户输入和游戏逻辑:
```python
def main():
player_turn = True
game_over = False
draw_board()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
break
elif event.type == pygame.MOUSEBUTTONDOWN and player_turn:
x, y = event.pos
i, j = y // CELL_SIZE, x // CELL_SIZE
if board[i][j] == 0:
board[i][j] = 1
player_turn = False
draw_board()
winner = is_game_over()
if winner is not None:
game_over = True
else:
computer_move()
player_turn = True
draw_board()
winner = is_game_over()
if winner is not None:
game_over = True
if game_over:
if winner == 1:
text = font.render("你赢了!", True, BLUE)
elif winner == -1:
text = font.render("电脑赢了!", True, RED)
else:
text = font.render("平局!", True, BLACK)
screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2))
pygame.display.update()
pygame.time.wait(3000)
pygame.quit()
if __name__ == "__main__":
main()
```
在主循环中,我们首先处理退出事件。然后,如果是玩家回合,我们等待玩家点击一个空格子,并将该格子设置为玩家的棋子。接着,我们检查游戏是否结束,如果结束了,我们将game_over标记为True,否则让电脑下棋。最后,我们再次检查游戏是否结束,如果结束了,我们显示游戏结果,并在3秒后退出游戏。
好啦,现在你可以运行这个程序,和电脑下一盘井字棋啦!
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)