-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameDriver.py
101 lines (80 loc) · 2.88 KB
/
GameDriver.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
'''
Erich Kramer - April 2017
Apache License
If using this code please cite creator.
'''
from Players import *
import sys
import OthelloBoard
class GameDriver:
def __init__(self, p1type, p2type, num_rows, num_cols):
if p1type.lower() in "human":
self.p1 = HumanPlayer('X')
elif p1type.lower() in "minimax" or p1type in "ai":
self.p1 = MinimaxPlayer('X')
else:
print("Invalid player 1 type!")
exit(-1)
if p2type.lower() in "human":
self.p2 = HumanPlayer('O')
elif p2type.lower() in "minimax" or p1type in "ai":
self.p2 = MinimaxPlayer('O')
else:
print("Invalid player 2 type!")
exit(-1)
self.board = OthelloBoard.OthelloBoard(num_rows, num_cols, self.p1.symbol, self.p2.symbol)
self.board.initialize();
def display(self):
print("Player 1 (", self.p1.symbol, ") score: ", \
self.board.count_score(self.p1.symbol))
def process_move(self, curr_player, opponent):
invalid_move = True
while(invalid_move):
(col, row) = curr_player.get_move(self.board)
if( not self.board.is_legal_move(col, row, curr_player.symbol)):
print("Invalid move")
else:
print("Move:", [col,row], "\n")
self.board.play_move(col,row,curr_player.symbol)
return;
def run(self):
current = self.p1
opponent = self.p2
self.board.display();
cant_move_counter, toggle = 0, 0
#main execution of game
print("Player 1(", self.p1.symbol, ") move:")
while True:
if self.board.has_legal_moves_remaining(current.symbol):
cant_move_counter = 0
self.process_move(current, opponent);
self.board.display()
else:
print("Can't move")
if(cant_move_counter == 1):
break
else:
cant_move_counter +=1
toggle = (toggle + 1) % 2
if toggle == 0:
current, opponent = self.p1, self.p2
print("Player 1(", self.p1.symbol, ") move:")
else:
current, opponent = self.p2, self.p1
print("Player 2(", self.p2.symbol, ") move:")
#decide win/lose/tie state
state = self.board.count_score(self.p1.symbol) - self.board.count_score(self.p2.symbol)
if( state == 0):
print("Tie game!!")
elif state >0:
print("Player 1 Wins!")
else:
print("Player 2 Wins!")
def main():
if(len(sys.argv)) != 3:
print("Usage: python3 GameDriver.py <player1 type> <player2 type>")
exit(1)
game = GameDriver(sys.argv[1], sys.argv[2], 4, 4)
game.run();
return 0
main()