-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
191 lines (161 loc) · 6.22 KB
/
main.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
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
import pygame
import numpy as np
import sys
import math
# Configurações do jogo
ROWS, COLS = 7, 8
SQUARESIZE = 100
RADIUS = SQUARESIZE // 2 - 5
plyDepth = 2
aiAlgorithm = "minimax"
stateCache = {}
# Cores
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# Inicializa o Pygame
pygame.init()
# Tamanho da janela
width = COLS * SQUARESIZE
height = (ROWS + 1) * SQUARESIZE
size = (width, height)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Connect 4 com IA")
# Funções de lógica do jogo
def create_board():
return np.zeros((ROWS, COLS))
def is_valid_location(board, col):
return board[ROWS - 1][col] == 0
def get_next_open_row(board, col):
for r in range(ROWS):
if board[r][col] == 0:
return r
def drop_piece(board, row, col, piece):
board[row][col] = piece
def check_victory(board, piece):
# Verificar todas as condições de vitória (horizontal, vertical, diagonal)
for c in range(COLS - 3):
for r in range(ROWS):
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
return True
for c in range(COLS):
for r in range(ROWS - 3):
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
return True
for c in range(COLS - 3):
for r in range(ROWS - 3):
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece:
return True
for c in range(COLS - 3):
for r in range(3, ROWS):
if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece:
return True
return False
def evaluate_board(board):
# Avaliar o tabuleiro para pontuar peças em sequência
score = 0
# Avaliação aqui pode ser detalhada com pesos específicos para configurações
return score
def is_terminal_node(board):
return check_victory(board, 1) or check_victory(board, -1) or len(get_valid_locations(board)) == 0
def minimax(board, depth, maximizingPlayer):
valid_locations = get_valid_locations(board)
is_terminal = is_terminal_node(board)
if depth == 0 or is_terminal:
if is_terminal:
if check_victory(board, -1):
return (None, 100000000000000)
elif check_victory(board, 1):
return (None, -10000000000000)
else:
return (None, 0)
else:
return (None, evaluate_board(board))
if maximizingPlayer:
value = -math.inf
column = np.random.choice(valid_locations)
for col in valid_locations:
row = get_next_open_row(board, col)
temp_board = board.copy()
drop_piece(temp_board, row, col, -1)
new_score = minimax(temp_board, depth-1, False)[1]
if new_score > value:
value = new_score
column = col
return column, value
else:
value = math.inf
column = np.random.choice(valid_locations)
for col in valid_locations:
row = get_next_open_row(board, col)
temp_board = board.copy()
drop_piece(temp_board, row, col, 1)
new_score = minimax(temp_board, depth-1, True)[1]
if new_score < value:
value = new_score
column = col
return column, value
def get_valid_locations(board):
valid_locations = []
for col in range(COLS):
if is_valid_location(board, col):
valid_locations.append(col)
return valid_locations
# Funções de exibição do jogo
def draw_board(board):
for c in range(COLS):
for r in range(ROWS):
pygame.draw.rect(screen, BLUE, (c*SQUARESIZE, (r+1)*SQUARESIZE, SQUARESIZE, SQUARESIZE))
pygame.draw.circle(screen, BLACK, (c*SQUARESIZE + SQUARESIZE//2, (r+1)*SQUARESIZE + SQUARESIZE//2), RADIUS)
for c in range(COLS):
for r in range(ROWS):
if board[r][c] == 1:
pygame.draw.circle(screen, RED, (c*SQUARESIZE + SQUARESIZE//2, height - (r+1)*SQUARESIZE + SQUARESIZE//2), RADIUS)
elif board[r][c] == -1:
pygame.draw.circle(screen, YELLOW, (c*SQUARESIZE + SQUARESIZE//2, height - (r+1)*SQUARESIZE + SQUARESIZE//2), RADIUS)
pygame.display.update()
# Função principal do jogo
def play_game():
board = create_board()
game_over = False
turn = 0
draw_board(board)
pygame.display.update()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
posx = event.pos[0]
if turn == 0:
pygame.draw.circle(screen, RED, (posx, SQUARESIZE//2), RADIUS)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
# Jogada do jogador 1
if turn == 0:
posx = event.pos[0]
col = posx // SQUARESIZE
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, 1)
if check_victory(board, 1):
print("Jogador Vermelho Vence!")
game_over = True
# Jogada do jogador 2 (IA)
else:
col, minimax_score = minimax(board, plyDepth, True)
if is_valid_location(board, col):
row = get_next_open_row(board, col)
drop_piece(board, row, col, -1)
if check_victory(board, -1):
print("IA Amarelo Vence!")
game_over = True
draw_board(board)
turn += 1
turn %= 2
if game_over:
pygame.time.wait(3000)
play_game()