标签:
对上次的代码作了一些修改。在码的过程中发现,最核心的部分是在横向、竖向和两个对角方向上找到5个以上相同的数字。
自己的思路是将x行y列所在的x行、y列,以及以此为交叉点的两点对角线上的数字,转化成字符串(这部分是程序中get4str()的功能),然后利用字符串查找相关模块进行处理(待完成)。
最新修改代码如下:
1 import random # for random.randrange() 2 import os # for input() 3 import string # for string.count() 4 5 ballColorNum = 7 # 7 colors 6 RowNum = 6 # chesspad have 10 rows 7 ColNum = 10 # chesspad have 10 cols 8 9 chPad = [] # save chesspad state 10 Scores = 0 # save scores 11 12 class ball(): 13 def __init__(self): 14 self.color = random.randrange(1,ballColorNum) 15 self.x = random.randrange(0,RowNum) 16 self.y = random.randrange(0,ColNum) 17 18 #--------------------------------------- 19 # initial chesspad state, all 0, COL*ROW 20 #--------------------------------------- 21 def chesspad_init(): 22 chPad = [[0 for x in range(ColNum)] for x in range(RowNum)] 23 return chPad 24 #--------------------------------------- 25 # update chesspad with a ball 26 #--------------------------------------- 27 def chesspad_update(chPad, ball): 28 chPad[ball.x][ball.y] = ball.color 29 return chPad 30 #--------------------------------------- 31 # redraw chesspad 32 #--------------------------------------- 33 def chesspad_flush(chPad): 34 for i in range(len(chPad)): 35 for c in chPad[i]: 36 #print(‘%d‘%c,end=‘ ‘) #for win 37 print(‘\33[1;%dm%d‘%(30+c,c),end=‘ ‘) #for linux 38 print(‘\n‘) 39 #--------------------------------------- 40 # count the number of unused spaces in chesspad 41 #--------------------------------------- 42 def countNull(chPad): 43 n = 0 44 for i in range(len(chPad)): 45 for j in range(len(chPad[i])): 46 if chPad[i][j] == 0: 47 n += 1 48 return n 49 #--------------------------------------- 50 # move from x1,y1 to x2,y2 51 #--------------------------------------- 52 def move(chpad,x1,y1,x2,y2): 53 if x1 > RowNum-1 or x2 > RowNum-1 54 or y2 > ColNum-1 or y1 > ColNum-1: 55 print(‘input error‘) 56 57 else: 58 chpad[x1][y1],chpad[x2][y2] = 59 chpad[x2][y2],chpad[x1][y1] 60 61 #--------------------------------------- 62 # calculate_Score 63 #--------------------------------------- 64 def calculate_Score(chpad): 65 pass 66 67 #--------------------------------------- 68 # find 69 #--------------------------------------- 70 def compare_str(chpad,str4dict,x,y): 71 pass 72 73 #--------------------------------------- 74 #reverse a sting. e.g. ‘ABC‘ to ‘CBA‘ 75 #--------------------------------------- 76 def reverse_str(s): 77 l = [] 78 sr = ‘‘ 79 for c in s: 80 l.append(c) 81 l.reverse() 82 for c in l: 83 sr += c 84 return sr 85 #--------------------------------------- 86 # generate 4 strings and return a dict: 87 # FORMART: 88 # {‘RW‘:‘...‘,‘CL‘:‘...‘, 89 # ‘LR‘:‘...‘,‘RL‘:‘...‘} 90 # compare with chpad[x][y] which aspears 91 # 5 times 92 #--------------------------------------- 93 def get4str(chpad,x,y): 94 95 sx=sy=slr=srl=‘‘ 96 97 # ROW(X) 98 for c in chpad[x]: 99 sx += str(c) 100 101 # COL(Y) 102 for i in range(RowNum): 103 sy += str(chpad[i][y]) 104 105 # from left-top to right-buttom 106 for i in range(x): 107 if x-i <= 0 or y-i <= 0: 108 break 109 else: 110 slr += str(chpad[x-i-1][y-i-1]) 111 slr = reverse_str(slr) 112 for i in range(RowNum-x): 113 if x+i >= RowNum or y+i >= ColNum: 114 break 115 else: 116 slr += str(chpad[x+i][y+i]) 117 118 # from right-top to left-buttom 119 for i in range(1,x): 120 if x-i <= 0 or y+i >= ColNum: 121 break 122 else: 123 srl += str(chpad[x-i][y+i]) 124 srl = reverse_str(srl) 125 for i in range(RowNum-x): 126 if x+i > RowNum-1 or y-i < 0: 127 break 128 else: 129 srl += str(chpad[x+i][y-i]) 130 131 return {‘RW‘:sx,‘CL‘:sy,‘LR‘:slr,‘RL‘:srl} 132 133 def main(): 134 print(‘\n-------------------GAME------------------\n‘) 135 pad = chesspad_init() 136 while 1: 137 ‘‘‘generate 3 balls with random locations and colors 138 and update chesspad. 139 ‘‘‘ 140 n = 1 141 while 1: 142 b = ball() 143 if pad[b.x][b.y] == 0: 144 print(‘(%d,%d:%d)‘%(b.x, b.y, b.color),end=‘ ‘) 145 pad = chesspad_update(pad, b) 146 if countNull(pad) == 0: 147 print("\n\nGAME OVER!") 148 exit() 149 n += 1 150
标签:
原文地址:http://www.cnblogs.com/py520ziyi/p/5579367.html