基于python实现五子棋游戏。棋盘大小为15.15,对弈双方分别使用黑白两色棋子,下在棋盘直线与横线的交叉点上。行棋时两方轮流著棋子下于棋盘空点处,先形成任何方向(横/纵/斜)的5子以上连线者获胜。 2本项目要求实现一个与人他弈的五子棋程序:程序为行棋的一方,用户方通过交互完成与程序的五子棋对弈。 基本功能: 完成的系统需要实现功能: 1用户可以选择先手还是后手: 2胜负状态的判断; 3.有棋子落下后棋盘状态的更新与显示; 4有简易的UI界面 5.双方平局的判断(无空位落子)。
时间: 2024-03-09 08:50:02 浏览: 126
python实现五子棋游戏
好的,下面是一个基于Python实现的五子棋游戏的代码示例:
```python
import tkinter as tk
class ChessBoard:
def __init__(self, master, size=15):
self.master = master
self.size = size
self.current_player = 1
self.game_over = False
self.chessboard = [[0 for i in range(size)] for j in range(size)]
self.canvas = tk.Canvas(master, width=size*30, height=size*30, bg='#EBDDB2')
self.canvas.pack()
for i in range(size):
self.canvas.create_line(15, 15+i*30, size*30-15, 15+i*30, width=2)
self.canvas.create_line(15+i*30, 15, 15+i*30, size*30-15, width=2)
self.canvas.bind('<Button-1>', self.play)
self.reset_button = tk.Button(master, text='重新开始', command=self.reset)
self.reset_button.pack()
def play(self, event):
if self.game_over:
return
x, y = int(round(event.x/30)-1), int(round(event.y/30)-1)
if x < 0 or x >= self.size or y < 0 or y >= self.size:
return
if self.chessboard[x][y] != 0:
return
if self.current_player == 1:
color = 'black'
self.current_player = 2
self.master.title('五子棋(轮到白方下)')
else:
color = 'white'
self.current_player = 1
self.master.title('五子棋(轮到黑方下)')
self.chessboard[x][y] = self.current_player
self.canvas.create_oval(x*30+2, y*30+2, x*30+28, y*30+28, fill=color)
if self.check_win(x, y):
self.game_over = True
if self.current_player == 1:
self.master.title('五子棋(黑方胜)')
else:
self.master.title('五子棋(白方胜)')
elif self.check_draw():
self.game_over = True
self.master.title('五子棋(平局)')
def check_win(self, x, y):
color = self.chessboard[x][y]
count = 1
i = x-1
while i >= 0 and self.chessboard[i][y] == color:
count += 1
i -= 1
i = x+1
while i < self.size and self.chessboard[i][y] == color:
count += 1
i += 1
if count >= 5:
return True
count = 1
i = y-1
while i >= 0 and self.chessboard[x][i] == color:
count += 1
i -= 1
i = y+1
while i < self.size and self.chessboard[x][i] == color:
count += 1
i += 1
if count >= 5:
return True
count = 1
i, j = x-1, y-1
while i >= 0 and j >= 0 and self.chessboard[i][j] == color:
count += 1
i -= 1
j -= 1
i, j = x+1, y+1
while i < self.size and j < self.size and self.chessboard[i][j] == color:
count += 1
i += 1
j += 1
if count >= 5:
return True
count = 1
i, j = x-1, y+1
while i >= 0 and j < self.size and self.chessboard[i][j] == color:
count += 1
i -= 1
j += 1
i, j = x+1, y-1
while i < self.size and j >= 0 and self.chessboard[i][j] == color:
count += 1
i += 1
j -= 1
if count >= 5:
return True
return False
def check_draw(self):
for i in range(self.size):
for j in range(self.size):
if self.chessboard[i][j] == 0:
return False
return True
def reset(self):
self.current_player = 1
self.game_over = False
self.chessboard = [[0 for i in range(self.size)] for j in range(self.size)]
self.canvas.delete('all')
self.master.title('五子棋(轮到黑方下)')
if __name__ == '__main__':
root = tk.Tk()
root.title('五子棋(轮到黑方下)')
board = ChessBoard(root)
root.mainloop()
```
这个代码使用了Tkinter库来创建了一个简单的GUI界面,可以让用户与程序进行交互。用户可以通过点击鼠标来下棋,程序会判断并更新当前局面的状态,并在GUI界面上显示出来。程序还实现了胜负状态的判断和重新开始的功能。
阅读全文