This is basically the same as Version 2 except that now the cards are shown in ASCII art. The cards and imaging system are imported from some other modules that I wrote. Enjoy the game!
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | ================================================================================
war_game_3.py
================================================================================
from random import randint, seed
from time import time
# region: change
from window import *
from cards import *
card_list = [card_0, card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8, card_9]
# endregion
def game():
print 'Welcome to WAR V3!'
print
asking = True
while asking:
try:
players = int(raw_input('How many players are there? '))
if players < 2:
print 'There must be at least two players.'
else:
asking = False
except:
print 'You must enter a number.'
print
names = []
# region: change
longest_name = 0
for name in range(players):
names.append(raw_input('What is the name of player ' + str(name + 1) + '? '))
if len(names[-1]) > longest_name:
longest_name = len(names[-1])
# endregion
deck = []
for card in range(10):
for player in range(players):
deck.append(card)
hands = []
seed(time())
for player in range(players):
hand = ([], [])
for card in range(10):
index = randint(0, len(deck) - 1)
hand[0].append(deck[index])
del deck[index]
hand[0].sort()
hands.append(hand)
for round in range(1, 11):
table = []
will_play = []
high_card = 0
for player in range(players):
will_play.append(player)
for turn in range(players):
for line in range(50):
print
index = randint(0, len(will_play) - 1)
now_play = will_play[index]
del will_play[index]
print 'Round', round
raw_input('It is ' + names[now_play] + "'s turn to play.")
print
# region: change
if len(table) == 0:
print 'There are no cards on the table.\n'
else:
table_window = window_v1(len(table) * 6, longest_name + 13)
for card in range(len(table)):
name_page = page_v1(1, len(names[table[card][0]]) + 9)
name_page.mutate(0, 0, names[table[card][0]] + ' played')
table_window.append(name_page, [card * 6, 0])
table_window.append(card_list[table[card][1]], [card * 6, len(names[table[card][0]]) + 8])
print table_window
print 'These are your playing cards:'
playing_window = window_v1(7, len(hands[now_play][0]) * 6)
for index in range(len(hands[now_play][0])):
playing_window.append(card_list[hands[now_play][0][index]], [1, index * 6 + 1])
print playing_window
if len(hands[now_play][1]) > 0:
hands[now_play][1].sort()
print 'These are your captured cards:'
capture_window = window_v1(7, len(hands[now_play][1]) * 6)
for index in range(len(hands[now_play][1])):
capture_window.append(card_list[hands[now_play][1][index]], [1, index * 6 + 1])
print capture_window
# endregion
asking = True
while asking:
try:
card = int(raw_input('What card do you want to play? '))
if card >= 0 and card <= 9:
try:
hands[now_play][0].remove(card)
table.append((now_play, card))
if card > high_card:
high_card = card
asking = False
except:
print 'You do not have that card.'
else:
print 'You must enter a value between -1 and 10.'
except:
print 'You must enter a number.'
for line in range(50):
print
#region: change
table_window = window_v1(len(table) * 6, longest_name + 13)
for card in range(len(table)):
name_page = page_v1(1, len(names[table[card][0]]) + 9)
name_page.mutate(0, 0, names[table[card][0]] + ' played')
table_window.append(name_page, [card * 6, 0])
table_window.append(card_list[table[card][1]], [card * 6, len(names[table[card][0]]) + 8])
print table_window
# endregion
hand_out = []
for index in range(players):
if table[index][1] == high_card:
hand_out.append(table[index][0])
while len(table) > 0:
hands[hand_out[randint(0, len(hand_out) - 1)]][1].append(table[0][1])
del table[0]
for player in range(players):
if len(hands[player][1]) > 0:
print names[player] + ' has captured ' + str(len(hands[player][1])) + ' cards.'
print
raw_input('End Of Round ' + str(round))
for line in range(50):
print
high_score = 0
scores = []
for player in range(players):
total = 0
for card in range(len(hands[player][1])):
total += hands[player][1][card]
if total > high_score:
high_score = total
if len(scores) == 0 or scores[len(scores) - 1][1] <= total:
scores.append((player, total))
else:
for index in range(len(scores)):
if total > scores[index][1]:
scores.insert((player, total))
break
for player in range(players):
print names[scores[player][0]] + ' received ' + str(scores[player][1]) + ' points.'
print
for index in range(10):
raw_input('GAME OVER ... ' + str(9 - index))
if __name__ == '__main__':
game()
================================================================================
window.py
================================================================================
# This is the first version of the page class.
class page_v1:
def __init__(self, rows, columns, default = None):
# (page_v1, int, int, str)
if default is None:
default = ' '
self.__page = list()
for index in range(rows):
self.__page.append(list(default[0] * columns))
def mutate(self, row, column, string):
# (page_v1, int, int, str)
try:
if row >= 0:
for index in range(len(string)):
if column + index >= 0:
self.__page[row][column + index] = string[index]
except:
pass
def access(self, row, column, length = 1):
# (page_v1, int, int, int)
string = str()
try:
for index in range(length):
string += self.__page[row][column + index]
except:
pass
return string
def internal(self):
# (page_v1)
array = list()
for row in self.__page:
array.append(row[:])
return array
def __str__(self):
# (page_v1)
string = str()
for row in self.__page:
for character in row:
string += character
string += '\n'
return string[:-1]
# This is the first version of a theoretical window.
class window_v1:
def __init__(self, height, width, border = None, background = None):
# (window_v1, int, int, str, str)
self.__height = height
self.__width = width
self.__border = border
self.__background = background
self.__draw = True
self.__buffer = None
self.__contents = list()
def append(self, instance, position, visible = True, index = None):
# (window_v1, page_v1 OR window_v1, [int, int], bool, int)
self.__draw = True
if index is None:
self.__contents.append([instance, position, visible])
else:
self.__contents.insert(index, [instance, position, visible])
def remove(self, instance):
# (window_v1, page_v1 OR window_v1)
for index in range(len(self.__contents)):
if instance is self.__contents[index][0]:
self.__draw = True
del self.__contents[index]
def __getitem__(self, index):
# (window_v1, int)
self.__draw = True
return self.__contents[index]
def __delitem__(self, index):
# (window_v1, int)
self.__draw = True
del self.__contents[index]
def size(self, height = None, width = None):
# (window_v1, int, int)
if height is not None:
self.__draw = True
self.__height = height
if width is not None:
self.__draw = True
self.__width = width
if height is None and width is None:
return self.__height, self.__width
def look(self, border = 0, background = 0):
# (window_v1, str, str)
if border is not 0:
self.__draw = True
self.__border = border
if background is not 0:
self.__draw = True
self.__background = background
if border is 0 and background is 0:
return self.__border, self.__background
def __update(self):
# (window_v1)
if self.__draw:
self.__draw = False
self.__buffer = page_v1(self.__height, self.__width, self.__background)
for item in self.__contents:
if item[2]:
internal = item[0].internal()
for row in range(len(internal)):
for column in range(len(internal[0])):
self.__buffer.mutate(row + item[1][0], column + item[1][1], internal[row][column])
if self.__border is not None:
self.__buffer.mutate(0, 0, self.__border[0] * self.__width)
self.__buffer.mutate(self.__height - 1, 0, self.__border[0] * self.__width)
for row in range(1, self.__height - 1):
self.__buffer.mutate(row, 0, self.__border[0])
self.__buffer.mutate(row, self.__width - 1, self.__border[0])
def internal(self):
# (window_v1)
self.__update()
return self.__buffer.internal()
def __str__(self):
# (window_v1)
self.__update()
return str(self.__buffer)
================================================================================
cards.py
================================================================================
from window import page_v1
card_0 = page_v1(5, 5)
card_0.mutate(0, 0, '+---+')
card_0.mutate(1, 0, '| |')
card_0.mutate(2, 0, '| 0 |')
card_0.mutate(3, 0, '| |')
card_0.mutate(4, 0, '+---+')
card_1 = page_v1(5, 5)
card_1.mutate(0, 0, '+---+')
card_1.mutate(1, 0, '| |')
card_1.mutate(2, 0, '| 1 |')
card_1.mutate(3, 0, '| |')
card_1.mutate(4, 0, '+---+')
card_2 = page_v1(5, 5)
card_2.mutate(0, 0, '+---+')
card_2.mutate(1, 0, '| |')
card_2.mutate(2, 0, '| 2 |')
card_2.mutate(3, 0, '| |')
card_2.mutate(4, 0, '+---+')
card_3 = page_v1(5, 5)
card_3.mutate(0, 0, '+---+')
card_3.mutate(1, 0, '| |')
card_3.mutate(2, 0, '| 3 |')
card_3.mutate(3, 0, '| |')
card_3.mutate(4, 0, '+---+')
card_4 = page_v1(5, 5)
card_4.mutate(0, 0, '+---+')
card_4.mutate(1, 0, '| |')
card_4.mutate(2, 0, '| 4 |')
card_4.mutate(3, 0, '| |')
card_4.mutate(4, 0, '+---+')
card_5 = page_v1(5, 5)
card_5.mutate(0, 0, '+---+')
card_5.mutate(1, 0, '| |')
card_5.mutate(2, 0, '| 5 |')
card_5.mutate(3, 0, '| |')
card_5.mutate(4, 0, '+---+')
card_6 = page_v1(5, 5)
card_6.mutate(0, 0, '+---+')
card_6.mutate(1, 0, '| |')
card_6.mutate(2, 0, '| 6 |')
card_6.mutate(3, 0, '| |')
card_6.mutate(4, 0, '+---+')
card_7 = page_v1(5, 5)
card_7.mutate(0, 0, '+---+')
card_7.mutate(1, 0, '| |')
card_7.mutate(2, 0, '| 7 |')
card_7.mutate(3, 0, '| |')
card_7.mutate(4, 0, '+---+')
card_8 = page_v1(5, 5)
card_8.mutate(0, 0, '+---+')
card_8.mutate(1, 0, '| |')
card_8.mutate(2, 0, '| 8 |')
card_8.mutate(3, 0, '| |')
card_8.mutate(4, 0, '+---+')
card_9 = page_v1(5, 5)
card_9.mutate(0, 0, '+---+')
card_9.mutate(1, 0, '| |')
card_9.mutate(2, 0, '| 9 |')
card_9.mutate(3, 0, '| |')
card_9.mutate(4, 0, '+---+')
card_10 = page_v1(5, 5)
card_10.mutate(0, 0, '+---+')
card_10.mutate(1, 0, '| |')
card_10.mutate(2, 0, '|1 0|')
card_10.mutate(3, 0, '| |')
card_10.mutate(4, 0, '+---+')
card_A = page_v1(5, 5)
card_A.mutate(0, 0, '+---+')
card_A.mutate(1, 0, '| |')
card_A.mutate(2, 0, '| A |')
card_A.mutate(3, 0, '| |')
card_A.mutate(4, 0, '+---+')
card_J = page_v1(5, 5)
card_J.mutate(0, 0, '+---+')
card_J.mutate(1, 0, '| |')
card_J.mutate(2, 0, '| J |')
card_J.mutate(3, 0, '| |')
card_J.mutate(4, 0, '+---+')
card_Q = page_v1(5, 5)
card_Q.mutate(0, 0, '+---+')
card_Q.mutate(1, 0, '| |')
card_Q.mutate(2, 0, '| Q |')
card_Q.mutate(3, 0, '| |')
card_Q.mutate(4, 0, '+---+')
card_K = page_v1(5, 5)
card_K.mutate(0, 0, '+---+')
card_K.mutate(1, 0, '| |')
card_K.mutate(2, 0, '| K |')
card_K.mutate(3, 0, '| |')
card_K.mutate(4, 0, '+---+')
|
The window.py and cards.py are re-usable. Please post your comments and have fun with the game!
Tags: programs