-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTerminalDisplayManager.py
317 lines (273 loc) · 12.4 KB
/
TerminalDisplayManager.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# class for managing the rendering of game elements to a terminal window
from Tile import Tile
from Board import Board
import sys
from termcolor import colored, cprint
# https://pypi.python.org/pypi/termcolor
class TerminalDisplayManager:
tileHeight = 4;
tileWidth = 10;
innerTileHeight = 3;
innerTileWidth = 8;
# constructor
def __init__(self):
# print("Created a TerminalDisplayManager instance");
pass;
def drawSplashScreen(self, menuOptions):
self.drawMenu(menuOptions);
def drawPlayerSetupScreen(self, maxPlayers, playersList, tokens, selectedTokens):
while True:
userInput = input('How many players (0 - {})?'.format(maxPlayers));
try:
int(userInput)
except ValueError:
print("Please make a valid menu selection: ", end='')
continue
if int(userInput) not in range(0, maxPlayers + 1):
print("Please make a valid menu selection: ", end='')
continue
else:
break
for i in range(len(playersList)):
if i < int(userInput):
playersList[i].ai = False;
else:
playersList[i].ai = True;
for index, player in enumerate(playersList):
if player.ai != True:
self.setPlayerName(player);
self.setPlayerToken(player, tokens, selectedTokens);
else:
player.name = "Bot " + str(index)
print('Player {} is now known as {}'.format(index + 1, player.name))
for token in tokens:
if token not in selectedTokens:
player.token = token;
selectedTokens.append(token);
print('{} has been allocated {} for their game token '.format(player.name,
player.token))
break
def setPlayerName(self, player):
valid = False;
while valid == False:
player.name = input('{} enter your name: '.format(player.name));
if len(player.name) <= 10 and len(player.name) >= 1:
valid = True;
break;
print("Name must be between 1 and 10 characters");
def setPlayerToken(self, player, tokens, selectedTokens):
print("{} select your game token: ".format(player.name));
for index, token in enumerate(tokens):
if token not in selectedTokens:
print("{} {} ".format((index + 1), token));
else:
print("{} {} ".format((index + 1), '(taken)'));
valid = False
while valid == False:
try:
playerInput = int(input("Enter token number: "));
if tokens[playerInput - 1] in selectedTokens[:]:
print('Token {} is not available, try again:'.format(tokens[playerInput - 1]));
continue
elif playerInput < 1:
raise IndexError
else:
player.token = tokens[playerInput - 1];
selectedTokens.append(tokens[playerInput - 1]);
print("{} has selected {}".format((player.name), player.token));
valid = True;
break
except IndexError:
print("Please make a valid menu selection: ", end='')
continue
except ValueError:
print("Please make a valid menu selection: ", end='')
continue
def drawTile(self, row, tile):
# draws the requested row number of the tile
# draws any relevant tile data (players present, tile number, portals etc)
# Params:
# row=the row of the tile to draw (int)
# tile=instance of a Tile object (Tile)
" "
tileCapRow = "---------";
playersRow = self.generatePlayersRow(tile);
tileNumberRow = self.generateTileNumberRow(tile);
portalRow = self.generatePortalRow(tile);
rows = [
tileCapRow,
playersRow,
tileNumberRow,
portalRow
]
sys.stdout.write(rows[row]);
def generatePlayersRow(self, tile):
# creates a string to represent the players in a tile
# has appropriate padding if there are no players present
playerString = " ";
for player in tile.players:
if player.isActive:
playerString = colored(player.token, attrs=["reverse", "blink"]) + playerString;
else:
playerString = player.token + playerString;
playerString = playerString[:len(playerString)-1] + playerString[len(playerString):];
playersRow = "| " + playerString + " ";
return playersRow;
def generatePortalRow(self, tile):
portalString = " "
padding = " ";
symbol = "";
if tile.portal:
#if the tile has a portal
#because the two tiles share the same portal instance we need to find out
#if the tile is the origin or the destination and draw the right number
#and because tile index is zero based but tile number is not we need to +- 1
if tile.portal.origin == tile.tileNumber - 1:
#this means the tile is the origin so we want to draw out the destination of the portal
destinationName = tile.portal.destination + 1;
else:
#vice versa, must draw the origin of the portal
destinationName = tile.portal.origin + 1;
if tile.portal.destination < tile.tileNumber:
# change the symbol if its leading backwards
if 0 <= tile.tileNumber <= 8 or 17 <= tile.tileNumber <= 24 or 33 <= tile.tileNumber <= 40:
symbol = colored("<-" + str(destinationName), "red", "on_grey", attrs=["bold", "reverse"]);
else:
symbol = colored("->" + str(destinationName), "red", "on_grey", attrs=["bold", "reverse"]);
if tile.portal.origin > 9:
padding = "";
else:
#symbol is forward and blue
if 0 <= tile.tileNumber <= 8 or 17 <= tile.tileNumber <= 24 or 33 <= tile.tileNumber <= 40:
symbol = colored("->" + str(destinationName), "cyan", "on_grey", attrs=["bold", "reverse"]);
else:
symbol = colored("<-" + str(destinationName), "cyan", "on_grey", attrs=["bold", "reverse"]);
if tile.portal.destination > 9:
padding = "";
portalString = symbol + padding;
return "| " + portalString + " "
def generateTileNumberRow(self, tile):
# just returns a space if the number is smaller than 10
# its padding for single digit numbers in the tile rendering
padding = "";
if tile.tileNumber< 10:
padding = " ";
#Adds arrows next to the tile number to indicate direction of play.
if 0 < tile.tileNumber < 8 or 17 <= tile.tileNumber < 24 or 33 <= tile.tileNumber < 40:
return "| " + str(tile.tileNumber) + padding + " >";
elif 9 <= tile.tileNumber < 16 or 25 <= tile.tileNumber < 32:
return "|< " + str(tile.tileNumber) + padding + " ";
elif 16 == tile.tileNumber or tile.tileNumber == 32:
return "|v " + str(tile.tileNumber) + padding + " ";
elif tile.tileNumber == 40:
return "| " + str(tile.tileNumber) + padding + " :)";
else:
return "| " + str(tile.tileNumber) + padding + " v";
#return "| " + str(tile.tileNumber) + padding + " ";
def drawBoard(self, board, menuOptions=None, isAi=False):
# draws the game board
# Params:
# board=instance of the board to draw (Board)
input("Press Enter to continue...")
for row in range(0, board.height):
#for each row of tiles
for tileRow in range(0, self.tileHeight):
#for each row in a tile (row of characters)
for col in range(0, board.width):
#for each column of tiles
if row % 2:
#on odd rows the highest number in the range needs to come first
#have to use -1 and +1 because of starting at zero (2nd row needs to multiply by 2, not 1)
index = (board.width * (row + 1)) - (col + 1);
self.drawTile(tileRow, board.tiles[index]);
else:
#get current col (from 0 to board width)
#find a row multiple and add it to col
index = col + (board.width * row);
self.drawTile(tileRow, board.tiles[index]);
sys.stdout.write("\n");
if isAi == True:
menuOptions["options"][0]["method"]()
else:
if menuOptions != None:
self.drawMenu(menuOptions)
def drawMenu(self, menu):
print(menu["heading"]);
for index, option in enumerate(menu["options"]):
print(index + 1, option["name"])
print("\nPlease enter an option: ", end='')
while True:
selection = input();
try:
option = menu["options"][int(selection) -1];
except IndexError:
# print(str(e));
print("Please make a valid menu selection: ", end='')
continue
except ValueError:
print("Please make a valid menu selection: ", end='')
continue
if int(selection) <= 0:
print("Please make a valid menu selection: ", end='')
continue
else:
break
option["method"]();
def drawEndGameScenario(self, playerlist, player, menuOptions):
maxlength = max(max(len(p.name) for p in playerlist), 8)+2
print("\n\nWinner winner chicken dinner! Congratulations " + player.name);
print("\nGame Stats\n----------\n")
print("Player:\t", end="\t")
for p in playerlist:
print("{0:>{1}}".format(p.name, maxlength), end='')
print("\nTurns: \t", end="\t")
for p in playerlist:
print("{0:{1}}".format(p.turncount, maxlength), end='')
print("\nPortals: \t", end="")
for p in playerlist:
print("{0:{1}}".format(p.portalsactivated, maxlength), end='')
print("\nTiles: \t", end="\t")
for p in playerlist:
print("{0:{1}}".format(p.tilesmoved, maxlength), end='')
print("\nRemaining: ", end="\t")
for p in playerlist:
print("{0:{1}}".format(39 - p.location, maxlength), end='')
print("\n")
# print("\n\nWinner winner chicken dinner! Congratulations " + player.name);
# print("\nGame Stats\n----------\n")
#
# print("Player:\t", end="\t")
# for p in self.controller.model.board.players:
# padding = ""
# nameLen = len(p.name)
# if (nameLen < 10):
# for j in range(0, 10 - nameLen):
# padding = padding + " "
# print(p.name + padding, end="\t")
#
# print("\nBot: \t", end="\t")
# for p in self.controller.model.board.players:
# print(p.ai, end="\t\t")
#
# print("\nTurns: \t", end="\t")
# for p in self.controller.model.board.players:
# print(p.turncount, end="\t\t\t")
#
# print("\nPortals: \t", end="")
# for p in self.controller.model.board.players:
# print(p.portalsactivated, end="\t\t\t")
#
# print("\nTiles: \t", end="\t")
# for p in self.controller.model.board.players:
# print(p.tilesmoved, end="\t\t\t")
#
# print("\nRemaining: ", end="\t")
# for p in self.controller.model.board.players:
# print(39 - p.location, end="\t\t\t")
#
# print("\n")
# test code to just run through the methods
self.drawMenu(menuOptions);
def quitGame(self):
#this is just for the graphical manager to play nice
pass