-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
81 lines (65 loc) · 2.15 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
from copy import deepcopy
import pygame
import pygame_menu
from game.board import Board
from game.constants import ROWS, COLS, SQUARE_SIZE, WIDTH, HEIGHT, RED, WHITE
from game.game import Game
from algorithm.minimax import minimax
from tkinter import *
from tkinter import messagebox
FPS = 60
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
ai_dificulty = 2
ai_player = False
pygame.init()
pygame.display.set_caption('4 in a row')
def get_row_col_from_mouse(pos):
x, y = pos
row = y // SQUARE_SIZE
col = x // SQUARE_SIZE
return row, col
def set_dificulty(dificulty, value):
global ai_dificulty
ai_dificulty = value
def set_player(player, value):
global ai_player
ai_player = value
def main():
run = True
clock = pygame.time.Clock()
game = Game(WIN)
print (ai_player)
while run:
clock.tick(FPS)
if ai_player:
if game.turn == WHITE:
value, new_board = minimax(game.board, ai_dificulty, False, game)
game.ai_move(new_board)
if game.check_winner(game.board) != None:
Tk().wm_withdraw() #to hide the main window
if game.check_winner(game.board) == RED:
messagebox.showinfo('Game end !','RED PLAYER WON')
else:
messagebox.showinfo('Game end !','WHITE PLAYER WON')
print(game.check_winner(game.board))
run = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
row, col = get_row_col_from_mouse(pos)
game.make_move(row, col)
game.update()
menu.mainloop(WIN)
'''
Main menu
'''
menu = pygame_menu.Menu('Welcome', 500, 500,
theme=pygame_menu.themes.THEME_DARK)
menu.add.button('Play', main)
menu.add.selector('Enemy :', [('Player', False), ('AI', True)], onchange=set_player)
menu.add.selector('AI level :', [('Normal', 2), ('Good', 3)], onchange=set_dificulty)
menu.add.button('Quit', pygame_menu.events.EXIT)
menu.mainloop(WIN)
main()