-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.py
More file actions
193 lines (162 loc) · 6.07 KB
/
functions.py
File metadata and controls
193 lines (162 loc) · 6.07 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import pickle
import pygame
import copy
from classes import *
# continue.pkl을 불러온다. 없다면 False 리턴
def load_continue():
if not os.path.isfile('files/continue.pkl'):
return []
else:
with open('files/continue.pkl', 'rb') as file:
# pickle.load() 함수를 사용하여 객체를 로드합니다.
return pickle.load(file)
# 뒤로 가기 등을 눌렀을 때 continue_states 배열을 저장
def save_continue(continue_states, player, difficulty, remained_undo):
if continue_states:
save_infos = [continue_states,player, difficulty, remained_undo]
else: save_infos = []
with open('files/continue.pkl', 'wb') as file:
# pickle.dump() 함수를 사용하여 객체를 저장합니다.
pickle.dump(save_infos, file)
def load_review():
if not os.path.isfile('files/review.pkl'):
return []
else:
with open('files/review.pkl', 'rb') as file:
# pickle.load() 함수를 사용하여 객체를 로드합니다.
return pickle.load(file)
def save_review(states, player, difficulty):
save_infos = [states,player, difficulty]
with open('files/review.pkl', 'wb') as file:
# pickle.dump() 함수를 사용하여 객체를 저장합니다.
pickle.dump(save_infos, file)
'''
게임 기록을 불러오고 저장하는 함수
record = {
'easy':[win, draw, lose],
'normal':[win, draw, lose],
'hard':[win, draw, lose]
}
'''
def load_record():
if not os.path.isfile('files/record.pkl'):
init_record = {
'easy':[0,0,0],
'normal':[0,0,0],
'hard':[0,0,0]
}
with open('files/record.pkl', 'wb') as file:
pickle.dump(init_record, file)
return init_record
else:
with open('files/record.pkl', 'rb') as file:
# pickle.load() 함수를 사용하여 객체를 로드합니다.
return pickle.load(file)
def save_record(record):
with open('files/record.pkl', 'wb') as file:
# pickle.dump() 함수를 사용하여 객체를 저장합니다.
pickle.dump(record, file)
def load_setting():
if not os.path.isfile('files/setting.pkl'):
init_setting = {
'first_player':3,
'p1_color':(255,0,0),
'p2_color':(255,228,0),
'music':1,
'effect':1
}
with open('files/setting.pkl', 'wb') as file:
pickle.dump(init_setting, file)
with open('files/default_setting.pkl', 'wb') as file:
pickle.dump(init_setting, file)
return init_setting
else:
with open('files/setting.pkl', 'rb') as file:
# pickle.load() 함수를 사용하여 객체를 로드합니다.
return pickle.load(file)
def save_setting(setting):
with open('files/setting.pkl', 'wb') as file:
# pickle.dump() 함수를 사용하여 객체를 저장합니다.
pickle.dump(setting, file)
def draw_table(surface):
w,h = surface.get_size()
block_size = (w-100)/7
for i in range(8):
pygame.draw.line(surface, BLACK, (50+block_size*i,100), (50+block_size*i,100+6*block_size), width=5)
for i in range(7):
pygame.draw.line(surface, BLACK, (50,100+block_size*i), (50+block_size*7,100+block_size*i), width=5)
# board 상의 좌표를 SCREEN의 좌표로 변경
def coord2pos(surface, coord):
y,x = coord
# y = 5-y
w,h = surface.get_size()
r = (w-100)/7/2
# (0,0) 일 때의 position
pos = [50+r,100+r]
pos[0] += 2*x*r
pos[1] += 2*y*r
return pos
def x2col(surface, x):
w,h = surface.get_size()
block_size = (w-100)/7
x -= 50
col = -1
while x >= 0:
x -= block_size
col += 1
if not 0<=col<=6:
col = -1
return col
def is_valid_x(surface, x, y, *buttons):
w,h = surface.get_size()
if x<50 or w-50<x:
return False
for button in buttons:
cx, cy, w, h = button.cx, button.cy, button.width, button.height
if cx-w/2<=x<=cx+w/2 and cy-h/2<=y<=cy+h/2:
return False
else: return True
# (board, column, player)를 받아서 (next board, next player, pos, valid move) 를 리턴
def get_next_state(board, col,player):
if col == -1:
return board, player, (None, None), False
if board[0][col] != 0:
return board, player, (None, None), False
next_board = copy.deepcopy(board)
for row in range(5,-1,-1):
if next_board[row][col] == 0:
next_board[row][col] = player
return next_board, 2//player, (row, col), True
# made by chatgpt and I edit little bit.
# 가로, 세로, 대각선에 완성된 줄이 있는지를 체크한다.
# 연결된 4개를 표시하기 위해 return을 수정하였다.
# return (이긴 플레이어, 좌표1, 좌표2, 좌표3, 좌표4)
def is_win(board, player):
for i in range(6):
for j in range(7):
if board[i][j] == player:
# horizontal
if j + 3 < 7 and board[i][j+1] == board[i][j+2] == board[i][j+3] == player:
return player, (i,j), (i,j+1), (i,j+2), (i,j+3)
# vertical
if i + 3 < 6 and board[i+1][j] == board[i+2][j] == board[i+3][j] == player:
return player, (i,j), (i+1,j), (i+2,j), (i+3,j)
# diagonal (down right)
if i + 3 < 6 and j + 3 < 7 and board[i+1][j+1] == board[i+2][j+2] == board[i+3][j+3] == player:
return player, (i,j), (i+1,j+1), (i+2,j+2), (i+3,j+3)
# diagonal (up right)
if i - 3 >= 0 and j + 3 < 7 and board[i-1][j+1] == board[i-2][j+2] == board[i-3][j+3] == player:
return player, (i,j), (i-1,j+1), (i-2,j+2), (i-3,j+3)
if 0 not in board[0,:]:
player = 3
return player, None, None, None, None
return 0, None, None, None, None
def calculate_win_rate(w,d,l):
if l==0: return 0
else:
win_rate = w/(w+d+l)*100
if win_rate%1==0:
return int(win_rate)
else:
return round(w/(w+d+l)*100, 2)