-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquoridor.py
120 lines (88 loc) · 3.29 KB
/
quoridor.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
#!/bin/env python
# -*- coding: utf-8 -*-
import os
import pygame
from pygame.locals import *
from pygame import Color
import threading
import argparse
from helpers import log, LogLevel
import config as cfg
import core
from entities.board import Board
def dispatch(events, board: Board):
for event in events:
if event.type == QUIT:
return False
if hasattr(event, 'key'):
if event.key == K_ESCAPE or board.finished:
return False
if board.computing or board.finished or board.current_player.is_network_player:
continue
if event.type == MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
board.onMouseClick(x, y)
if event.type == MOUSEMOTION:
x, y = pygame.mouse.get_pos()
board.onMouseMotion(x, y)
return True
def main() -> int:
core.init()
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--level",
help="AI player Level. Default is 0 (Easy). Higher is harder)",
default=cfg.LEVEL, type=int)
parser.add_argument('-d', '--debug',
help="Debug mode", action='store_true')
parser.add_argument('-C', '--cache',
help="Enable persistent memoize cache", action='store_true')
options = parser.parse_args()
cfg.LEVEL = options.level
cfg.__DEBUG__ = options.debug
cfg.CACHE_ENABLED = options.cache
log('Quoridor AI game, (C) 2009 by Jose Rodriguez (a.k.a. Boriel)')
log('This program is Free')
log('Initializing system...')
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_mode((800, 600))
pygame.display.set_caption(cfg.GAME_TITLE)
screen = pygame.display.get_surface()
screen.fill(Color(255, 255, 255))
board = core.BOARD = Board(screen)
board.draw()
log('System initialized OK')
if cfg.CACHE_ENABLED:
if not os.path.exists(cfg.CACHE_DIR):
log('Cache directory {} not found. Creating it...'.format(cfg.CACHE_DIR))
os.makedirs(cfg.CACHE_DIR, exist_ok=True)
if not os.path.isdir(cfg.CACHE_DIR):
log('Could not create cache directory {}. Caching disabled'.format(cfg.CACHE_DIR), LogLevel.ERROR)
cfg.CACHE_ENABLED = False
cont = True
while cont:
clock.tick(cfg.FRAMERATE)
pygame.display.flip()
if not board.computing and not board.finished:
if board.current_player.AI:
board.computing = True
thread = threading.Thread(target=board.computer_move)
thread.start()
cont = dispatch(pygame.event.get(), board)
del board.rows
pygame.quit()
if cfg.NETWORK_ENABLED:
board.server.terminate()
if cfg.CACHE_ENABLED:
for pawn in board.pawns:
if pawn.AI is not None:
pawn.AI.flush_cache()
log('Memoized nodes: %i' % core.MEMOIZED_NODES)
log('Memoized nodes hits: %i' % core.MEMOIZED_NODES_HITS)
for pawn in board.pawns:
log('Memoized distances for [%i]: %i' % (pawn.id, pawn.distances.MEMO_COUNT))
log('Memoized distances hits for [%i]: %i' % (pawn.id, pawn.distances.MEMO_HITS))
log('Exiting. Bye!')
return 0
if __name__ == '__main__':
main()