-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
64 lines (50 loc) · 1.99 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
import pygame
from checkers.constants import *
from checkers.game import Game
from Ai_algorithm.alpha_beta import alpha_beta_algo
#To render the game in a fixed frames number per second
FPS = 60
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
#Name of the game
pygame.display.set_caption('Checkers')
#Start get_row_col_from_mouse method to get the position of the piece that mouse clicked
def get_row_col_from_mouse(pos):
x, y = pos
row = y // SQUARE_SIZE
col = x // SQUARE_SIZE
return row, col
#End get_row_col_from_mouse method to get the position of the piece that mouse clicked
def main():
run = True
#To make the game run in fixed speed
clock = pygame.time.Clock()
#To create a board and start the game
game = Game(WIN)
#Event Loop
while run:
#To run the game in a fixed speed -> (f/s)
clock.tick(FPS)
#Start To make the ai play
if game.turn == BLUE:
#The higher the number of depth, the higher the complexity of the ai
#value, new_board = minimax(game.get_board(), 4, BLUE, game)
value, new_board = alpha_beta_algo(game.get_board(), 4, float('-inf'), float('inf'), BLUE, game)
game.ai_move(new_board)
#End To make the ai play
if game.winner() != None:
print(game.winner)
run = False #To quit the game if someone is win
#Start check if any event happen in the current time
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos() #return tuple
row, col = get_row_col_from_mouse(pos)
game.select(row, col)
#End check if any event happen in the current time
game.update() #To update any change that happens in the game board
#Quit from the game
pygame.quit()
#To call the main function and run the code inside it
main()