Python(四):数字连珠2

对上次的代码作了一些修改。在码的过程中发现,最核心的部分是在横向、竖向和两个对角方向上找到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(str4dict,x,y):
 71     pass
 72 #---------------------------------------
 73 # generate 4 strings and return a dict:
 74 # FORMART:
 75 # {'RW':'...','CL':'...',
 76 #  'LR':'...','RL':'...'}
 77 # compare with chpad[x][y] which aspears
 78 #  5 times
 79 #---------------------------------------
 80 def get4str(chpad,x,y):
 81     
 82     sx=sy=slr=srl=''
 83     
 84     # ROW(X)
 85     for c in chpad[x]:
 86         sx += str(c)
 87     
 88     # COL(Y)
 89     for i in range(RowNum):
 90         sy += str(chpad[i][y])
 91     
 92     # from left-top to right-buttom
 93     for i in range(x):
 94         if x-i <= 0 or y-i <= 0:
 95             break
 96         else:
 97             slr += str(chpad[x-i-1][y-i-1])
 98     slr[::-1]  # reverse
 99     
100     for i in range(RowNum-x):
101         if x+i >= RowNum or y+i >= ColNum:
102             break
103         else:
104             slr += str(chpad[x+i][y+i])
105     
106     # from right-top to left-buttom
107     for i in range(1,x):
108         if x-i <= 0 or y+i >= ColNum:
109             break
110         else:
111             srl += str(chpad[x-i][y+i])
112     srl[::-1]  # reverse
113     
114     for i in range(RowNum-x):
115         if x+i > RowNum-1 or y-i < 0:
116             break
117         else:
118             srl += str(chpad[x+i][y-i])
119     
120     return {'RW':sx,'CL':sy,'LR':slr,'RL':srl}
121     
122 def main():
123     print('\n-------------------GAME------------------\n')
124     pad = chesspad_init()
125     
126     while 1:
127         n = 1
128         while 1:
129             b = ball()
130             if pad[b.x][b.y] == 0:
131                 print('(%d,%d:%d)'%(b.x, b.y, b.color),end=' ')
132                 pad = chesspad_update(pad, b)
133                 if countNull(pad) == 0:
134                     print("\n\nGAME OVER!")
135                     exit()
136                 n += 1
137             if n > 3:
138                 break
139         print('\n')    
140             
141         chesspad_flush(pad)
142         
143         x1=y1=x2=y2=0        
144         a = input('Move (x1,y2) to (x2,y2):').split(' ')
145         x1,y1,x2,y2 = int(a[0]),int(a[1]),int(a[2]),int(a[3])
146         move(pad,x1,y1,x2,y2 )
147         print(get4str(pad, x2, y2))
148         
149         chesspad_flush(pad)
150     
151 if __name__=='__main__':
152     main()            

 

以上代码后面有修改,98行、112行本来是自己写的一个反转字符串的函数实现,后来才知道用切片str[::-1]实现更简洁。

posted @ 2016-06-13 08:13  缁衣  阅读(330)  评论(0编辑  收藏  举报