War Game --字符游戏

::-- ZoomQuiet [2005-10-27 01:51:00]

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 
       2 # This is the first version of the page class.
       3 class page_v1:
       4 
       5     def __init__(self, rows, columns, default = None):
       6         # (page_v1, int, int, str)
       7         if default is None:
       8             default = ' '
       9         self.__page = list()
      10         for index in range(rows):
      11             self.__page.append(list(default[0] * columns))
      12 
      13     def mutate(self, row, column, string):
      14         # (page_v1, int, int, str)
      15         try:
      16             if row >= 0:
      17                 for index in range(len(string)):
      18                     if column + index >= 0:
      19                         self.__page[row][column + index] = string[index]
      20         except:
      21             pass
      22 
      23     def access(self, row, column, length = 1):
      24         # (page_v1, int, int, int)
      25         string = str()
      26         try:
      27             for index in range(length):
      28                 string += self.__page[row][column + index]
      29         except:
      30             pass
      31         return string
      32 
      33     def internal(self):
      34         # (page_v1)
      35         array = list()
      36         for row in self.__page:
      37             array.append(row[:])
      38         return array
      39 
      40     def __str__(self):
      41         # (page_v1)
      42         string = str()
      43         for row in self.__page:
      44             for character in row:
      45                 string += character
      46             string += '\n'
      47         return string[:-1]
      48 
      49 # This is the first version of a theoretical window.
      50 class window_v1:
      51 
      52     def __init__(self, height, width, border = None, background = None):
      53         # (window_v1, int, int, str, str)
      54         self.__height = height
      55         self.__width = width
      56         self.__border = border
      57         self.__background = background
      58         self.__draw = True
      59         self.__buffer = None
      60         self.__contents = list()
      61 
      62     def append(self, instance, position, visible = True, index = None):
      63         # (window_v1, page_v1 OR window_v1, [int, int], bool, int)
      64         self.__draw = True
      65         if index is None:
      66             self.__contents.append([instance, position, visible])
      67         else:
      68             self.__contents.insert(index, [instance, position, visible])
      69 
      70     def remove(self, instance):
      71         # (window_v1, page_v1 OR window_v1)
      72         for index in range(len(self.__contents)):
      73             if instance is self.__contents[index][0]:
      74                 self.__draw = True
      75                 del self.__contents[index]
      76 
      77     def __getitem__(self, index):
      78         # (window_v1, int)
      79         self.__draw = True
      80         return self.__contents[index]
      81 
      82     def __delitem__(self, index):
      83         # (window_v1, int)
      84         self.__draw = True
      85         del self.__contents[index]
      86 
      87     def size(self, height = None, width = None):
      88         # (window_v1, int, int)
      89         if height is not None:
      90             self.__draw = True
      91             self.__height = height
      92         if width is not None:
      93             self.__draw = True
      94             self.__width = width
      95         if height is None and width is None:
      96             return self.__height, self.__width
      97 
      98     def look(self, border = 0, background = 0):
      99         # (window_v1, str, str)
     100         if border is not 0:
     101             self.__draw = True
     102             self.__border = border
     103         if background is not 0:
     104             self.__draw = True
     105             self.__background = background
     106         if border is 0 and background is 0:
     107             return self.__border, self.__background
     108 
     109     def __update(self):
     110         # (window_v1)
     111         if self.__draw:
     112             self.__draw = False
     113             self.__buffer = page_v1(self.__height, self.__width, self.__background)
     114             for item in self.__contents:
     115                 if item[2]:
     116                     internal = item[0].internal()
     117                     for row in range(len(internal)):
     118                         for column in range(len(internal[0])):
     119                             self.__buffer.mutate(row + item[1][0], column + item[1][1], internal[row][column])
     120             if self.__border is not None:
     121                 self.__buffer.mutate(0, 0, self.__border[0] * self.__width)
     122                 self.__buffer.mutate(self.__height - 1, 0, self.__border[0] * self.__width)
     123                 for row in range(1, self.__height - 1):
     124                     self.__buffer.mutate(row, 0, self.__border[0])
     125                     self.__buffer.mutate(row, self.__width - 1, self.__border[0])
     126 
     127     def internal(self):
     128         # (window_v1)
     129         self.__update()
     130         return self.__buffer.internal()
     131 
     132     def __str__(self):
     133         # (window_v1)
     134         self.__update()
     135         return str(self.__buffer)
    
  • cards.py

       1 
       2 from window import page_v1
       3 
       4 card_0 = page_v1(5, 5)
       5 card_0.mutate(0, 0, '+---+')
       6 card_0.mutate(1, 0, '|   |')
       7 card_0.mutate(2, 0, '| 0 |')
       8 card_0.mutate(3, 0, '|   |')
       9 card_0.mutate(4, 0, '+---+')
      10 
      11 card_1 = page_v1(5, 5)
      12 card_1.mutate(0, 0, '+---+')
      13 card_1.mutate(1, 0, '|   |')
      14 card_1.mutate(2, 0, '| 1 |')
      15 card_1.mutate(3, 0, '|   |')
      16 card_1.mutate(4, 0, '+---+')
      17 
      18 card_2 = page_v1(5, 5)
      19 card_2.mutate(0, 0, '+---+')
      20 card_2.mutate(1, 0, '|   |')
      21 card_2.mutate(2, 0, '| 2 |')
      22 card_2.mutate(3, 0, '|   |')
      23 card_2.mutate(4, 0, '+---+')
      24 
      25 card_3 = page_v1(5, 5)
      26 card_3.mutate(0, 0, '+---+')
      27 card_3.mutate(1, 0, '|   |')
      28 card_3.mutate(2, 0, '| 3 |')
      29 card_3.mutate(3, 0, '|   |')
      30 card_3.mutate(4, 0, '+---+')
      31 
      32 card_4 = page_v1(5, 5)
      33 card_4.mutate(0, 0, '+---+')
      34 card_4.mutate(1, 0, '|   |')
      35 card_4.mutate(2, 0, '| 4 |')
      36 card_4.mutate(3, 0, '|   |')
      37 card_4.mutate(4, 0, '+---+')
      38 
      39 card_5 = page_v1(5, 5)
      40 card_5.mutate(0, 0, '+---+')
      41 card_5.mutate(1, 0, '|   |')
      42 card_5.mutate(2, 0, '| 5 |')
      43 card_5.mutate(3, 0, '|   |')
      44 card_5.mutate(4, 0, '+---+')
      45 
      46 card_6 = page_v1(5, 5)
      47 card_6.mutate(0, 0, '+---+')
      48 card_6.mutate(1, 0, '|   |')
      49 card_6.mutate(2, 0, '| 6 |')
      50 card_6.mutate(3, 0, '|   |')
      51 card_6.mutate(4, 0, '+---+')
      52 
      53 card_7 = page_v1(5, 5)
      54 card_7.mutate(0, 0, '+---+')
      55 card_7.mutate(1, 0, '|   |')
      56 card_7.mutate(2, 0, '| 7 |')
      57 card_7.mutate(3, 0, '|   |')
      58 card_7.mutate(4, 0, '+---+')
      59 
      60 card_8 = page_v1(5, 5)
      61 card_8.mutate(0, 0, '+---+')
      62 card_8.mutate(1, 0, '|   |')
      63 card_8.mutate(2, 0, '| 8 |')
      64 card_8.mutate(3, 0, '|   |')
      65 card_8.mutate(4, 0, '+---+')
      66 
      67 card_9 = page_v1(5, 5)
      68 card_9.mutate(0, 0, '+---+')
      69 card_9.mutate(1, 0, '|   |')
      70 card_9.mutate(2, 0, '| 9 |')
      71 card_9.mutate(3, 0, '|   |')
      72 card_9.mutate(4, 0, '+---+')
      73 
      74 card_10 = page_v1(5, 5)
      75 card_10.mutate(0, 0, '+---+')
      76 card_10.mutate(1, 0, '|   |')
      77 card_10.mutate(2, 0, '|1 0|')
      78 card_10.mutate(3, 0, '|   |')
      79 card_10.mutate(4, 0, '+---+')
      80 
      81 card_A = page_v1(5, 5)
      82 card_A.mutate(0, 0, '+---+')
      83 card_A.mutate(1, 0, '|   |')
      84 card_A.mutate(2, 0, '| A |')
      85 card_A.mutate(3, 0, '|   |')
      86 card_A.mutate(4, 0, '+---+')
      87 
      88 card_J = page_v1(5, 5)
      89 card_J.mutate(0, 0, '+---+')
      90 card_J.mutate(1, 0, '|   |')
      91 card_J.mutate(2, 0, '| J |')
      92 card_J.mutate(3, 0, '|   |')
      93 card_J.mutate(4, 0, '+---+')
      94 
      95 card_Q = page_v1(5, 5)
      96 card_Q.mutate(0, 0, '+---+')
      97 card_Q.mutate(1, 0, '|   |')
      98 card_Q.mutate(2, 0, '| Q |')
      99 card_Q.mutate(3, 0, '|   |')
     100 card_Q.mutate(4, 0, '+---+')
     101 
     102 card_K = page_v1(5, 5)
     103 card_K.mutate(0, 0, '+---+')
     104 card_K.mutate(1, 0, '|   |')
     105 card_K.mutate(2, 0, '| K |')
     106 card_K.mutate(3, 0, '|   |')
     107 card_K.mutate(4, 0, '+---+')