War Game --字符游戏
::-- ZoomQuiet [2005-10-27 01:51:00]
Contents
1. War Game (Version 3)
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.1. src
The window.py and cards.py are re-usable. Please post your comments and have fun with the game!
war_game_3.py
1 from random import randint, seed 2 from time import time 3 # region: change 4 from window import * 5 from cards import * 6 card_list = [card_0, card_1, card_2, card_3, card_4, card_5, card_6, card_7, card_8, card_9] 7 # endregion 8 9 def game(): 10 print 'Welcome to WAR V3!' 11 print 12 asking = True 13 while asking: 14 try: 15 players = int(raw_input('How many players are there? ')) 16 if players < 2: 17 print 'There must be at least two players.' 18 else: 19 asking = False 20 except: 21 print 'You must enter a number.' 22 print 23 names = [] 24 # region: change 25 longest_name = 0 26 for name in range(players): 27 names.append(raw_input('What is the name of player ' + str(name + 1) + '? ')) 28 if len(names[-1]) > longest_name: 29 longest_name = len(names[-1]) 30 # endregion 31 deck = [] 32 for card in range(10): 33 for player in range(players): 34 deck.append(card) 35 hands = [] 36 seed(time()) 37 for player in range(players): 38 hand = ([], []) 39 for card in range(10): 40 index = randint(0, len(deck) - 1) 41 hand[0].append(deck[index]) 42 del deck[index] 43 hand[0].sort() 44 hands.append(hand) 45 for round in range(1, 11): 46 table = [] 47 will_play = [] 48 high_card = 0 49 for player in range(players): 50 will_play.append(player) 51 for turn in range(players): 52 for line in range(50): 53 print 54 index = randint(0, len(will_play) - 1) 55 now_play = will_play[index] 56 del will_play[index] 57 print 'Round', round 58 raw_input('It is ' + names[now_play] + "'s turn to play.") 59 print 60 # region: change 61 if len(table) == 0: 62 print 'There are no cards on the table.\n' 63 else: 64 table_window = window_v1(len(table) * 6, longest_name + 13) 65 for card in range(len(table)): 66 name_page = page_v1(1, len(names[table[card][0]]) + 9) 67 name_page.mutate(0, 0, names[table[card][0]] + ' played') 68 table_window.append(name_page, [card * 6, 0]) 69 table_window.append(card_list[table[card][1]], [card * 6, len(names[table[card][0]]) + 8]) 70 print table_window 71 print 'These are your playing cards:' 72 playing_window = window_v1(7, len(hands[now_play][0]) * 6) 73 for index in range(len(hands[now_play][0])): 74 playing_window.append(card_list[hands[now_play][0][index]], [1, index * 6 + 1]) 75 print playing_window 76 if len(hands[now_play][1]) > 0: 77 hands[now_play][1].sort() 78 print 'These are your captured cards:' 79 capture_window = window_v1(7, len(hands[now_play][1]) * 6) 80 for index in range(len(hands[now_play][1])): 81 capture_window.append(card_list[hands[now_play][1][index]], [1, index * 6 + 1]) 82 print capture_window 83 # endregion 84 asking = True 85 while asking: 86 try: 87 card = int(raw_input('What card do you want to play? ')) 88 if card >= 0 and card <= 9: 89 try: 90 hands[now_play][0].remove(card) 91 table.append((now_play, card)) 92 if card > high_card: 93 high_card = card 94 asking = False 95 except: 96 print 'You do not have that card.' 97 else: 98 print 'You must enter a value between -1 and 10.' 99 except: 100 print 'You must enter a number.' 101 for line in range(50): 102 print 103 #region: change 104 table_window = window_v1(len(table) * 6, longest_name + 13) 105 for card in range(len(table)): 106 name_page = page_v1(1, len(names[table[card][0]]) + 9) 107 name_page.mutate(0, 0, names[table[card][0]] + ' played') 108 table_window.append(name_page, [card * 6, 0]) 109 table_window.append(card_list[table[card][1]], [card * 6, len(names[table[card][0]]) + 8]) 110 print table_window 111 # endregion 112 hand_out = [] 113 for index in range(players): 114 if table[index][1] == high_card: 115 hand_out.append(table[index][0]) 116 while len(table) > 0: 117 hands[hand_out[randint(0, len(hand_out) - 1)]][1].append(table[0][1]) 118 del table[0] 119 for player in range(players): 120 if len(hands[player][1]) > 0: 121 print names[player] + ' has captured ' + str(len(hands[player][1])) + ' cards.' 122 print 123 raw_input('End Of Round ' + str(round)) 124 for line in range(50): 125 print 126 high_score = 0 127 scores = [] 128 for player in range(players): 129 total = 0 130 for card in range(len(hands[player][1])): 131 total += hands[player][1][card] 132 if total > high_score: 133 high_score = total 134 if len(scores) == 0 or scores[len(scores) - 1][1] <= total: 135 scores.append((player, total)) 136 else: 137 for index in range(scores): 138 if total > scores[index][1]: 139 scores.insert((player, total)) 140 break 141 for player in range(players): 142 print names[scores[player][0]] + ' received ' + str(scores[player][1]) + ' points.' 143 print 144 for index in range(10): 145 raw_input('GAME OVER ... ' + str(9 - index)) 146 147 if __name__ == '__main__': 148 game()
window.py
1 # This is the first version of the page class. 2 class page_v1: 3 4 def __init__(self, rows, columns, default = None): 5 # (page_v1, int, int, str) 6 if default is None: 7 default = ' ' 8 self.__page = list() 9 for index in range(rows): 10 self.__page.append(list(default[0] * columns)) 11 12 def mutate(self, row, column, string): 13 # (page_v1, int, int, str) 14 try: 15 if row >= 0: 16 for index in range(len(string)): 17 if column + index >= 0: 18 self.__page[row][column + index] = string[index] 19 except: 20 pass 21 22 def access(self, row, column, length = 1): 23 # (page_v1, int, int, int) 24 string = str() 25 try: 26 for index in range(length): 27 string += self.__page[row][column + index] 28 except: 29 pass 30 return string 31 32 def internal(self): 33 # (page_v1) 34 array = list() 35 for row in self.__page: 36 array.append(row[:]) 37 return array 38 39 def __str__(self): 40 # (page_v1) 41 string = str() 42 for row in self.__page: 43 for character in row: 44 string += character 45 string += '\n' 46 return string[:-1] 47 48 # This is the first version of a theoretical window. 49 class window_v1: 50 51 def __init__(self, height, width, border = None, background = None): 52 # (window_v1, int, int, str, str) 53 self.__height = height 54 self.__width = width 55 self.__border = border 56 self.__background = background 57 self.__draw = True 58 self.__buffer = None 59 self.__contents = list() 60 61 def append(self, instance, position, visible = True, index = None): 62 # (window_v1, page_v1 OR window_v1, [int, int], bool, int) 63 self.__draw = True 64 if index is None: 65 self.__contents.append([instance, position, visible]) 66 else: 67 self.__contents.insert(index, [instance, position, visible]) 68 69 def remove(self, instance): 70 # (window_v1, page_v1 OR window_v1) 71 for index in range(len(self.__contents)): 72 if instance is self.__contents[index][0]: 73 self.__draw = True 74 del self.__contents[index] 75 76 def __getitem__(self, index): 77 # (window_v1, int) 78 self.__draw = True 79 return self.__contents[index] 80 81 def __delitem__(self, index): 82 # (window_v1, int) 83 self.__draw = True 84 del self.__contents[index] 85 86 def size(self, height = None, width = None): 87 # (window_v1, int, int) 88 if height is not None: 89 self.__draw = True 90 self.__height = height 91 if width is not None: 92 self.__draw = True 93 self.__width = width 94 if height is None and width is None: 95 return self.__height, self.__width 96 97 def look(self, border = 0, background = 0): 98 # (window_v1, str, str) 99 if border is not 0: 100 self.__draw = True 101 self.__border = border 102 if background is not 0: 103 self.__draw = True 104 self.__background = background 105 if border is 0 and background is 0: 106 return self.__border, self.__background 107 108 def __update(self): 109 # (window_v1) 110 if self.__draw: 111 self.__draw = False 112 self.__buffer = page_v1(self.__height, self.__width, self.__background) 113 for item in self.__contents: 114 if item[2]: 115 internal = item[0].internal() 116 for row in range(len(internal)): 117 for column in range(len(internal[0])): 118 self.__buffer.mutate(row + item[1][0], column + item[1][1], internal[row][column]) 119 if self.__border is not None: 120 self.__buffer.mutate(0, 0, self.__border[0] * self.__width) 121 self.__buffer.mutate(self.__height - 1, 0, self.__border[0] * self.__width) 122 for row in range(1, self.__height - 1): 123 self.__buffer.mutate(row, 0, self.__border[0]) 124 self.__buffer.mutate(row, self.__width - 1, self.__border[0]) 125 126 def internal(self): 127 # (window_v1) 128 self.__update() 129 return self.__buffer.internal() 130 131 def __str__(self): 132 # (window_v1) 133 self.__update() 134 return str(self.__buffer)
cards.py
1 from window import page_v1 2 3 card_0 = page_v1(5, 5) 4 card_0.mutate(0, 0, '+---+') 5 card_0.mutate(1, 0, '| |') 6 card_0.mutate(2, 0, '| 0 |') 7 card_0.mutate(3, 0, '| |') 8 card_0.mutate(4, 0, '+---+') 9 10 card_1 = page_v1(5, 5) 11 card_1.mutate(0, 0, '+---+') 12 card_1.mutate(1, 0, '| |') 13 card_1.mutate(2, 0, '| 1 |') 14 card_1.mutate(3, 0, '| |') 15 card_1.mutate(4, 0, '+---+') 16 17 card_2 = page_v1(5, 5) 18 card_2.mutate(0, 0, '+---+') 19 card_2.mutate(1, 0, '| |') 20 card_2.mutate(2, 0, '| 2 |') 21 card_2.mutate(3, 0, '| |') 22 card_2.mutate(4, 0, '+---+') 23 24 card_3 = page_v1(5, 5) 25 card_3.mutate(0, 0, '+---+') 26 card_3.mutate(1, 0, '| |') 27 card_3.mutate(2, 0, '| 3 |') 28 card_3.mutate(3, 0, '| |') 29 card_3.mutate(4, 0, '+---+') 30 31 card_4 = page_v1(5, 5) 32 card_4.mutate(0, 0, '+---+') 33 card_4.mutate(1, 0, '| |') 34 card_4.mutate(2, 0, '| 4 |') 35 card_4.mutate(3, 0, '| |') 36 card_4.mutate(4, 0, '+---+') 37 38 card_5 = page_v1(5, 5) 39 card_5.mutate(0, 0, '+---+') 40 card_5.mutate(1, 0, '| |') 41 card_5.mutate(2, 0, '| 5 |') 42 card_5.mutate(3, 0, '| |') 43 card_5.mutate(4, 0, '+---+') 44 45 card_6 = page_v1(5, 5) 46 card_6.mutate(0, 0, '+---+') 47 card_6.mutate(1, 0, '| |') 48 card_6.mutate(2, 0, '| 6 |') 49 card_6.mutate(3, 0, '| |') 50 card_6.mutate(4, 0, '+---+') 51 52 card_7 = page_v1(5, 5) 53 card_7.mutate(0, 0, '+---+') 54 card_7.mutate(1, 0, '| |') 55 card_7.mutate(2, 0, '| 7 |') 56 card_7.mutate(3, 0, '| |') 57 card_7.mutate(4, 0, '+---+') 58 59 card_8 = page_v1(5, 5) 60 card_8.mutate(0, 0, '+---+') 61 card_8.mutate(1, 0, '| |') 62 card_8.mutate(2, 0, '| 8 |') 63 card_8.mutate(3, 0, '| |') 64 card_8.mutate(4, 0, '+---+') 65 66 card_9 = page_v1(5, 5) 67 card_9.mutate(0, 0, '+---+') 68 card_9.mutate(1, 0, '| |') 69 card_9.mutate(2, 0, '| 9 |') 70 card_9.mutate(3, 0, '| |') 71 card_9.mutate(4, 0, '+---+') 72 73 card_10 = page_v1(5, 5) 74 card_10.mutate(0, 0, '+---+') 75 card_10.mutate(1, 0, '| |') 76 card_10.mutate(2, 0, '|1 0|') 77 card_10.mutate(3, 0, '| |') 78 card_10.mutate(4, 0, '+---+') 79 80 card_A = page_v1(5, 5) 81 card_A.mutate(0, 0, '+---+') 82 card_A.mutate(1, 0, '| |') 83 card_A.mutate(2, 0, '| A |') 84 card_A.mutate(3, 0, '| |') 85 card_A.mutate(4, 0, '+---+') 86 87 card_J = page_v1(5, 5) 88 card_J.mutate(0, 0, '+---+') 89 card_J.mutate(1, 0, '| |') 90 card_J.mutate(2, 0, '| J |') 91 card_J.mutate(3, 0, '| |') 92 card_J.mutate(4, 0, '+---+') 93 94 card_Q = page_v1(5, 5) 95 card_Q.mutate(0, 0, '+---+') 96 card_Q.mutate(1, 0, '| |') 97 card_Q.mutate(2, 0, '| Q |') 98 card_Q.mutate(3, 0, '| |') 99 card_Q.mutate(4, 0, '+---+') 100 101 card_K = page_v1(5, 5) 102 card_K.mutate(0, 0, '+---+') 103 card_K.mutate(1, 0, '| |') 104 card_K.mutate(2, 0, '| K |') 105 card_K.mutate(3, 0, '| |') 106 card_K.mutate(4, 0, '+---+')