This repository has been archived by the owner on Dec 24, 2019. It is now read-only.
forked from rgruener/othello
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathothello.py
executable file
·122 lines (102 loc) · 3.93 KB
/
othello.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
#!/usr/bin/env python
import board
import player
from deep_learning_player import DeepLearningPlayer
import numpy as np
# from gui import Gui
from config import BLACK, WHITE, HUMAN, COMPUTER
from game_state_logger import Logger
from heuristic import OthelloHeuristic
from random import randint
import properties
class Othello:
# Default values, they are overridden by values in the properties file
headless = True
timeout = 5;
number_of_games = 100
try:
LOG = properties.LOG
except Exception:
LOG = True;
if hasattr(properties, "timeout"):
timeout = properties.timeout
if hasattr(properties, "games"):
number_of_games = properties.games
def __init__(self):
if self.headless:
self.setup_headless_game()
else:
#self.gui = Gui()
self.setup_game()
self.setup_headless_game()
def setup_headless_game(self):
self.headless = True
# player one, same as in game_state_logger.py
self.now_playing = player.RandomPlayer(color=BLACK, time_limit=self.timeout, headless=self.headless)
# player two, same as in game_state_logger.py
self.other_player = DeepLearningPlayer(color=WHITE, time_limit=self.timeout, headless=self.headless, epochs=0)
# self.other_player = DeepLearningPlayer(color=WHITE, time_limit=self.timeout, headless=self.headless)
self.board = board.Board()
Logger.set_player_names([self.now_playing.name, self.other_player.name])
def setup_game(self):
options = self.gui.show_options()
if options['player_1'] == COMPUTER:
self.now_playing = player.ComputerPlayer(BLACK, int(options['player_1_time']), self.gui)
else:
self.now_playing = player.HumanPlayer(BLACK, gui=self.gui)
if options['player_2'] == COMPUTER:
self.other_player = player.ComputerPlayer(WHITE, int(options['player_2_time']), self.gui)
else:
self.other_player = player.HumanPlayer(WHITE, gui=self.gui)
if options.has_key('load_file'):
self.board = board.Board(self.read_board_file(options['load_file']))
else:
self.board = board.Board()
def read_board_file(self, file_name):
f = open(file_name)
lines = [line.strip() for line in f]
f.close()
board = np.zeros((8, 8), dtype=np.integer)
# Read In Board File
i = 0
for line in lines[:8]:
j = 0
for char in line.split():
board[i][j] = int(char)
j += 1
i += 1
# Set Current Turn
if int(lines[8]) == WHITE:
self.now_playing, self.other_player = self.other_player, self.now_playing
return board
def run(self, games=1):
print "Game started: %s vs %s, time limit: %is" % (self.now_playing.name, self.other_player.name, self.timeout)
if not self.headless:
self.gui.show_game(self.board)
while True:
winner = self.board.game_won()
if winner is not None:
if self.LOG:
Logger.report_winner(winner)
break
self.now_playing.set_current_board(self.board)
if self.board.get_valid_moves(self.now_playing.color) != []:
self.board = self.now_playing.get_move()
if not self.headless:
self.gui.update(self.board, self.other_player)
self.now_playing, self.other_player = self.other_player, self.now_playing
if not self.headless:
self.gui.show_winner(winner, self.board)
def restart(self):
if self.headless:
self.setup_headless_game()
else:
self.setup_game()
def main():
game = Othello()
while(game.number_of_games > 0):
game.run(Othello.number_of_games)
game.restart()
game.number_of_games -= 1
if __name__ == '__main__':
main()