-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path六子棋.py
153 lines (134 loc) · 5.84 KB
/
六子棋.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import pygame
import sys
import json
from datetime import datetime
# 游戏基本设置
WINDOW_SIZE = 800 # 窗口大小
BOARD_SIZE = 15 # 棋盘大小(15x15)
GRID_SIZE = WINDOW_SIZE // (BOARD_SIZE + 1) # 格子大小
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BROWN = (139, 69, 19)
class connect6:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
pygame.display.set_caption('六子棋')
self.reset_game()
def reset_game(self):
self.board = [[None for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
self.current_player = 'black'
self.history = [] # 用于悔棋
self.game_over = False
self.winner = None
def check_win(self, x, y):
directions = [(1,0), (0,1), (1,1), (1,-1)] # 横、竖、正斜、反斜
player = self.board[y][x]
for dx, dy in directions:
count = 1
# 正向检查
for i in range(1, 6):
new_x, new_y = x + dx*i, y + dy*i
if not (0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE):
break
if self.board[new_y][new_x] != player:
break
count += 1
# 反向检查
for i in range(1, 6):
new_x, new_y = x - dx*i, y - dy*i
if not (0 <= new_x < BOARD_SIZE and 0 <= new_y < BOARD_SIZE):
break
if self.board[new_y][new_x] != player:
break
count += 1
if count >= 6:
return True
return False
def undo_move(self):
if self.history:
last_move = self.history.pop()
self.board[last_move[1]][last_move[0]] = None
self.current_player = 'white' if self.current_player == 'black' else 'black'
self.game_over = False
self.winner = None
def save_game(self):
game_state = {
'board': self.board,
'current_player': self.current_player,
'history': self.history
}
filename = f"game_save_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
with open(filename, 'w') as f:
json.dump(game_state, f)
def load_game(self, filename):
try:
with open(filename, 'r') as f:
game_state = json.load(f)
self.board = game_state['board']
self.current_player = game_state['current_player']
self.history = game_state['history']
self.game_over = False
self.winner = None
except:
print("无法加载游戏存档")
def draw_game_over(self):
if self.game_over:
font = pygame.font.Font(None, 74)
text = f"{'黑方' if self.winner == 'black' else '白方'}胜利!"
text_surface = font.render(text, True, (255, 0, 0))
text_rect = text_surface.get_rect(center=(WINDOW_SIZE//2, WINDOW_SIZE//2))
self.screen.blit(text_surface, text_rect)
def draw_board(self):
self.screen.fill(BROWN)
# 画网格线
for i in range(BOARD_SIZE):
pygame.draw.line(self.screen, BLACK,
(GRID_SIZE, GRID_SIZE * (i + 1)),
(WINDOW_SIZE - GRID_SIZE, GRID_SIZE * (i + 1)))
pygame.draw.line(self.screen, BLACK,
(GRID_SIZE * (i + 1), GRID_SIZE),
(GRID_SIZE * (i + 1), WINDOW_SIZE - GRID_SIZE))
def draw_stones(self):
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if self.board[i][j]:
color = BLACK if self.board[i][j] == 'black' else WHITE
pygame.draw.circle(self.screen, color,
(GRID_SIZE * (j + 1), GRID_SIZE * (i + 1)),
GRID_SIZE // 2 - 2)
def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_z: # Z键悔棋
self.undo_move()
elif event.key == pygame.K_s: # S键保存
self.save_game()
elif event.key == pygame.K_l: # L键读取最新存档
# 这里简化处理,实际应该让用户选择存档
self.load_game("最新的存档文件名.json")
if not self.game_over and event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
grid_x = round((x - GRID_SIZE) / GRID_SIZE)
grid_y = round((y - GRID_SIZE) / GRID_SIZE)
if 0 <= grid_x < BOARD_SIZE and 0 <= grid_y < BOARD_SIZE:
if self.board[grid_y][grid_x] is None:
self.board[grid_y][grid_x] = self.current_player
self.history.append((grid_x, grid_y))
if self.check_win(grid_x, grid_y):
self.game_over = True
self.winner = self.current_player
else:
self.current_player = 'white' if self.current_player == 'black' else 'black'
self.draw_board()
self.draw_stones()
if self.game_over:
self.draw_game_over()
pygame.display.flip()
if __name__ == '__main__':
game = connect6()
game.run()