-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameMaster.py
140 lines (122 loc) · 4.13 KB
/
GameMaster.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'''Game manager for the program'''
USE_HTML = True
import abhismKInARow as player1
import abhismKInARow as player2
from winTesterForK import winTesterForK
from time import sleep
if USE_HTML: import gameToHTML
GAME_TYPE = 'TIC-TAC-TOE'
MULTI_INITIAL_STATE = \
[[['-',' ',' ',' ',' ',' ',' ',' ',' ','-'],
[' ',' ','O',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ','-',' ',' ',' ',' ',' '],
[' ','O',' ',' ',' ',' ','-',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ','-',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ','-',' ',' ',' ','-',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
[' ',' ',' ',' ',' ',' ',' ',' ',' ',' '],
['-',' ',' ',' ',' ',' ',' ',' ',' ','-']], "X"]
K = 7
#K = 3
INITIAL_STATE = MULTI_INITIAL_STATE
#INITIAL_STATE = TTT_INITIAL_STATE
TIME_PER_MOVE = 10
N = len(INITIAL_STATE[0]) # height of board
M = len(INITIAL_STATE[0][0]) # width of board
GAME_TYPE = str(K)+" in a Row on"+str(M)+" "+str(N)+" Board with Corners Forbidden"
FINISHED = False
def runGame():
currentState = INITIAL_STATE
if USE_HTML:
gameToHTML.startHTML(player1.nickname(), player2.nickname(), GAME_TYPE, 1)
try:
p1comment = player1.prepare(INITIAL_STATE, K, 'X', player2.nickname(), mode='Normal')
except:
report = 'Player 1 ('+player1.nickname()+' failed to prepare, and loses by default.'
print(report)
if USE_HTML: gameToHTML.reportResult(report)
report = 'Congratulations to Player 2 ('+player2.nickname()+')!'
print(report)
if USE_HTML: gameToHTML.reportResult(report)
if USE_HTML: gameToHTML.endHTML()
return
try:
p2comment = player2.prepare(INITIAL_STATE, K, 'O', player1.nickname(), mode='Normal')
except:
report = 'Player 2 ('+player2.nickname()+' failed to prepare, and loses by default.'
print(report)
if USE_HTML: gameToHTML.reportResult(report)
report = 'Congratulations to Player 1 ('+player1.nickname()+')!'
print(report)
if USE_HTML: gameToHTML.reportResult(report)
if USE_HTML: gameToHTML.endHTML()
return
return
print('The Gamemaster says, "Let\'s Play!"')
print('The initial state is...')
currentRemark = "The game is starting."
if USE_HTML: gameToHTML.stateToHTML(currentState)
XsTurn = True
name = None
global FINISHED
FINISHED = False
turnCount = 0
printState(currentState)
while not FINISHED:
who = currentState[1]
if XsTurn:
playerResult = player1.makeMove(currentState, currentRemark, TIME_PER_MOVE)
name = player1.nickname()
XsTurn = False
else:
playerResult = player2.makeMove(currentState, currentRemark, TIME_PER_MOVE)
name = player2.nickname()
XsTurn = True
moveAndState, currentRemark = playerResult
if moveAndState==None:
FINISHED = True; continue
move, currentState = moveAndState
moveReport = "Move is by "+who+" to "+str(move)
print(moveReport)
utteranceReport = name +' says: '+currentRemark
# print(utteranceReport)
# sleep(0.3)
if USE_HTML: gameToHTML.reportResult(moveReport)
if USE_HTML: gameToHTML.reportResult(utteranceReport)
possibleWin = winTesterForK(currentState, move, K)
if possibleWin != "No win":
FINISHED = True
printState(currentState)
if USE_HTML: gameToHTML.stateToHTML(currentState, finished=True)
print(possibleWin)
if USE_HTML: gameToHTML.reportResult(possibleWin)
if USE_HTML: gameToHTML.endHTML()
return
printState(currentState)
if USE_HTML: gameToHTML.stateToHTML(currentState)
turnCount += 1
#if turnCount == 9: FINISHED=True
printState(currentState)
if USE_HTML: gameToHTML.stateToHTML(currentState)
who = currentState[1]
print("Game over.")
if USE_HTML: gameToHTML.reportResult("Game Over; it's a draw")
if USE_HTML: gameToHTML.endHTML()
def printState(s):
global FINISHED
board = s[0]
who = s[1]
horizontalBorder = "+"+3*N*"-"+"+"
print(horizontalBorder)
for row in board:
print("|",end="")
for item in row:
print(" "+item+" ", end="")
print("|")
print(horizontalBorder)
if not FINISHED:
print("It is "+who+"'s turn to move.\n")
runGame()