forked from pascscha/connect4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharena.py
More file actions
145 lines (121 loc) · 4.76 KB
/
Copy patharena.py
File metadata and controls
145 lines (121 loc) · 4.76 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import time
from gameboard.implementations import *
class Arena:
""" Arena Class. This is where the games are managed"""
@classmethod
def play_game(cls, playerClsRed, playerClsYellow, gameBoardCls, params):
""" Plays a game between two Player Classes using a GameBoard Class."""
# Initialize Classe
gb = gameBoardCls()
playerRed = playerClsRed(gb.RED, params)
playerYellow = playerClsYellow(gb.YELLOW, params)
if params.verbose:
print("\nWelcome to the Epic battle of {} vs {}!".format(playerRed.get_name(), playerYellow.get_name()))
print("Move #0 - {} ({}):".format(gb.get_occupation_string(playerRed.color),
playerRed.get_name()))
print(gb)
# Red (1st Player) can start
redsTurn = True
move = 0
while not gb.is_finished():
# Coose active player class
if redsTurn:
active_player = playerRed
else:
active_player = playerYellow
# Measure Time
start_time = time.time()
# Make Move
try:
legal, mv = cls.make_move(active_player, gb)
except Exception as e:
return Outcome(gb, red_won=not redsTurn, error=e)
# Illegal Move
if not legal:
return Outcome(gb, red_won=not redsTurn, last_move=mv, illegal=True)
# Timeout (Only for non-Human players)
elif params.timeout is not None and not active_player.IS_HUMAN and time.time() - start_time > params.timeout:
return Outcome(gb, red_won=not redsTurn, timeout=True)
# Check if player won
elif gb.has_won(active_player.color):
return Outcome(gb, red_won=redsTurn)
# Output
if params.verbose:
print("\nMove #{} - {} ({}):".format(move,
gb.get_occupation_string(active_player.color),
active_player.get_name()))
for r in range(gb.ROWS):
if r == mv:
print("v", end=" ")
else:
print(" ", end=" ")
print()
print(gb)
# Switch players
redsTurn = not redsTurn
move += 1
# Noone has won, it's a tie
return Outcome(gb, red_won=False, tie=True)
@classmethod
def make_move(cls, player, gb):
"""Let a player make a move and check wether it's legal or not"""
move = player.next_move(gb)
if not gb.is_legal(move):
return False, move
else:
gb.place_stone(move, player.color)
return True, move
class Outcome:
"""Holds the outcome (result) of a game"""
def __init__(self, gb, red_won=True, illegal=False, last_move=None, timeout=False, tie=False, error=None):
self.gb = gb
self.red_won = red_won
self.illegal = illegal
self.tie = tie
self.timeout = timeout
self.last_move = last_move
self.error = error
def __str__(self):
"""Verbose toString Method describing the outcome of the game"""
if self.error is not None:
if self.red_won:
who = self.gb.YELLOW
else:
who = self.gb.RED
return "{} had an Error: {}".format(self.gb.get_occupation_string(who), self.error)
elif self.tie:
return "Tie"
elif self.illegal:
if self.red_won:
who = self.gb.YELLOW
else:
who = self.gb.RED
return "{} made an Illegal move ({})".format(self.gb.get_occupation_string(who), self.last_move)
elif self.timeout:
if self.red_won:
who = self.gb.YELLOW
else:
who = self.gb.RED
return "{} timed out".format(self.gb.get_occupation_string(who))
elif self.red_won:
who = self.gb.RED
else:
who = self.gb.YELLOW
return "{} Won".format(self.gb.get_occupation_string(who))
def get_char(self):
"""Get a single char describing the outcome.
(Used for the tournament matrix)"""
if self.tie:
return "T"
elif self.illegal or self.timeout:
return "E"
elif self.red_won:
return "<"
else:
return "^"
class GameParameters:
"""Parameters for a connect 4 game"""
def __init__(self, timeout=None, verbose=True, gameBoardCls=BitBoard7x6):
self.timeout = timeout
self.verbose = verbose
self.gameBoardCls = gameBoardCls