Python 五子棋小游戏
时间: 2023-11-05 10:19:40 浏览: 133
Python五子棋小游戏是使用pygame模块编写的,使用的是一个15x15的棋盘。游戏的主要逻辑是通过不断刷新画布来实现。在初始化游戏窗口大小为670x670的情况下,每个棋子之间会存在一定的间隔,以使整个游戏界面更加美观。你可以通过在图形界面上点击鼠标来放置棋子,当棋局达到五子连珠的条件时,游戏会结束。你可以在文中的示例代码中详细了解如何实现Python五子棋小游戏。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python小游戏:五子棋](https://blog.csdn.net/2201_75362610/article/details/130899139)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关问题
python五子棋小游戏代码
下面是一个简单的 Python 五子棋小游戏代码,可以在控制台中运行:
```python
import numpy as np
# 定义棋盘大小
board_size = 15
# 定义棋盘
board = np.zeros((board_size, board_size), dtype=int)
# 定义棋子类型
EMPTY = 0
BLACK = 1
WHITE = 2
# 定义黑白棋子符号
stones = {EMPTY: "·", BLACK: "●", WHITE: "○"}
# 定义落子函数
def make_move(x, y, player):
global board
board[x, y] = player
# 定义判断胜负函数
def check_win(player):
global board
# 判断横向是否有五子相连
for i in range(board_size):
for j in range(board_size - 4):
if np.all(board[i, j:j+5] == player):
return True
# 判断纵向是否有五子相连
for i in range(board_size - 4):
for j in range(board_size):
if np.all(board[i:i+5, j] == player):
return True
# 判断正对角线是否有五子相连
for i in range(board_size - 4):
for j in range(board_size - 4):
if np.all(board[i:i+5, j:j+5].diagonal() == player):
return True
# 判断反对角线是否有五子相连
for i in range(4, board_size):
for j in range(board_size - 4):
if np.all(board[i-4:i+1, j:j+5].diagonal()[::-1] == player):
return True
return False
# 定义打印棋盘函数
def print_board():
global board
for i in range(board_size):
row = []
for j in range(board_size):
row.append(stones[board[i][j]])
print(" ".join(row))
# 主函数
def main():
player = BLACK
while True:
# 打印棋盘
print_board()
# 等待玩家输入落子位置
move = input("请玩家 {} 输入落子位置(x, y): ".format(stones[player]))
x, y = map(int, move.split(","))
# 判断落子位置是否合法
if x < 0 or x >= board_size or y < 0 or y >= board_size or board[x, y] != EMPTY:
print("落子位置不合法,请重新输入!")
continue
# 落子
make_move(x, y, player)
# 判断胜负
if check_win(player):
print_board()
print("玩家 {} 获胜!".format(stones[player]))
break
# 切换玩家
player = WHITE if player == BLACK else BLACK
if __name__ == "__main__":
main()
```
在控制台中运行该代码,会输出一个 15x15 的棋盘,提示黑方玩家输入落子位置。玩家可以输入类似 `3,4` 这样的坐标来落子,程序会自动判断落子是否合法,判断胜负并输出结果。
python五子棋小游戏的源码
以下是一个简单的 Python 五子棋小游戏的源码:
```python
import numpy as np
BOARD_SIZE = 15 # 棋盘大小
EMPTY = 0 # 空棋子
BLACK = 1 # 黑棋子
WHITE = 2 # 白棋子
class Board:
def __init__(self):
self.board = np.zeros((BOARD_SIZE, BOARD_SIZE), dtype=int)
self.current_player = BLACK
def place(self, x, y):
if self.board[x][y] == EMPTY:
self.board[x][y] = self.current_player
self.current_player = WHITE if self.current_player == BLACK else BLACK
return True
else:
return False
def check_win(self):
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if self.board[i][j] != EMPTY:
# 横向
if j <= BOARD_SIZE - 5:
if self.board[i][j] == self.board[i][j+1] == self.board[i][j+2] == self.board[i][j+3] == self.board[i][j+4]:
return self.board[i][j]
# 纵向
if i <= BOARD_SIZE - 5:
if self.board[i][j] == self.board[i+1][j] == self.board[i+2][j] == self.board[i+3][j] == self.board[i+4][j]:
return self.board[i][j]
# 左上到右下
if i <= BOARD_SIZE - 5 and j <= BOARD_SIZE - 5:
if self.board[i][j] == self.board[i+1][j+1] == self.board[i+2][j+2] == self.board[i+3][j+3] == self.board[i+4][j+4]:
return self.board[i][j]
# 右上到左下
if i >= 4 and j <= BOARD_SIZE - 5:
if self.board[i][j] == self.board[i-1][j+1] == self.board[i-2][j+2] == self.board[i-3][j+3] == self.board[i-4][j+4]:
return self.board[i][j]
return EMPTY
class Game:
def __init__(self):
self.board = Board()
def start(self):
while True:
self.print_board()
x, y = input(f"请 {self.board.current_player} 玩家下棋(x,y):").split(',')
x, y = int(x), int(y)
if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE:
if self.board.place(x, y):
winner = self.board.check_win()
if winner != EMPTY:
self.print_board()
print(f"{winner} 玩家获胜!")
break
else:
print("此处已经有棋子了,请重新下棋!")
else:
print("坐标范围不正确,请重新输入!")
def print_board(self):
print(" ", end="")
for i in range(BOARD_SIZE):
print(i, end=" ")
print()
for i in range(BOARD_SIZE):
print(i, end=" ")
for j in range(BOARD_SIZE):
if self.board.board[i][j] == BLACK:
print("●", end=" ")
elif self.board.board[i][j] == WHITE:
print("○", end=" ")
else:
print("+", end=" ")
print()
if __name__ == '__main__':
game = Game()
game.start()
```
使用方法:
- 下载源码并保存到本地;
- 在终端或命令行窗口中进入源码所在目录;
- 输入 `python gomoku.py` 命令运行程序;
- 根据提示输入坐标下棋。
注意:此程序为简单版本,可能存在一些小问题,欢迎大家提出改进意见。
阅读全文