对上次的代码作了一些修改。在码的过程中发现,最核心的部分是在横向、竖向和两个对角方向上找到5个以上相同的数字。
自己的思路是将x行y列所在的x行、y列,以及以此为交叉点的两点对角线上的数字,转化成字符串(这部分是程序中get4str()的功能),然后利用字符串相关函数进行处理(待完成)。
最新修改代码如下:
import random # for random.randrange()
import os # for input()
import string # for string.count() ballColorNum = 7 # 7 colors
RowNum = 6 # chesspad have 10 rows
ColNum = 10 # chesspad have 10 cols chPad = [] # save chesspad state
Scores = 0 # save scores class ball():
def __init__(self):
self.color = random.randrange(1,ballColorNum)
self.x = random.randrange(0,RowNum)
self.y = random.randrange(0,ColNum) #---------------------------------------
# initial chesspad state, all 0, COL*ROW
#---------------------------------------
def chesspad_init():
chPad = [[0 for x in range(ColNum)] for x in range(RowNum)]
return chPad
#---------------------------------------
# update chesspad with a ball
#---------------------------------------
def chesspad_update(chPad, ball):
chPad[ball.x][ball.y] = ball.color
return chPad
#---------------------------------------
# redraw chesspad
#---------------------------------------
def chesspad_flush(chPad):
for i in range(len(chPad)):
for c in chPad[i]:
print('%d'%c,end=' ') #for win
#print('\33[1;%dm%d'%(30+c,c),end=' ') #for linux
print('\n')
#---------------------------------------
# count the number of unused spaces in chesspad
#---------------------------------------
def countNull(chPad):
n = 0
for i in range(len(chPad)):
for j in range(len(chPad[i])):
if chPad[i][j] == 0:
n += 1
return n
#---------------------------------------
# move from x1,y1 to x2,y2
#---------------------------------------
def move(chpad,x1,y1,x2,y2):
if x1 > RowNum-1 or x2 > RowNum-1 \
or y2 > ColNum-1 or y1 > ColNum-1:
print('input error') else:
chpad[x1][y1],chpad[x2][y2] = \
chpad[x2][y2],chpad[x1][y1] #---------------------------------------
# calculate_Score
#---------------------------------------
def calculate_Score(chpad):
pass #---------------------------------------
# find
#---------------------------------------
def compare_str(str4dict,x,y):
pass
#---------------------------------------
# generate 4 strings and return a dict:
# FORMART:
# {'RW':'...','CL':'...',
# 'LR':'...','RL':'...'}
# compare with chpad[x][y] which aspears
# 5 times
#---------------------------------------
def get4str(chpad,x,y): sx=sy=slr=srl='' # ROW(X)
for c in chpad[x]:
sx += str(c) # COL(Y)
for i in range(RowNum):
sy += str(chpad[i][y]) # from left-top to right-buttom
for i in range(x):
if x-i <= 0 or y-i <= 0:
break
else:
slr += str(chpad[x-i-1][y-i-1])
slr[::-1] # reverse for i in range(RowNum-x):
if x+i >= RowNum or y+i >= ColNum:
break
else:
slr += str(chpad[x+i][y+i]) # from right-top to left-buttom
for i in range(1,x):
if x-i <= 0 or y+i >= ColNum:
break
else:
srl += str(chpad[x-i][y+i])
srl[::-1] # reverse for i in range(RowNum-x):
if x+i > RowNum-1 or y-i < 0:
break
else:
srl += str(chpad[x+i][y-i]) return {'RW':sx,'CL':sy,'LR':slr,'RL':srl} def main():
print('\n-------------------GAME------------------\n')
pad = chesspad_init() while 1:
n = 1
while 1:
b = ball()
if pad[b.x][b.y] == 0:
print('(%d,%d:%d)'%(b.x, b.y, b.color),end=' ')
pad = chesspad_update(pad, b)
if countNull(pad) == 0:
print("\n\nGAME OVER!")
exit()
n += 1
if n > 3:
break
print('\n') chesspad_flush(pad) x1=y1=x2=y2=0
a = input('Move (x1,y2) to (x2,y2):').split(' ')
x1,y1,x2,y2 = int(a[0]),int(a[1]),int(a[2]),int(a[3])
move(pad,x1,y1,x2,y2 )
print(get4str(pad, x2, y2)) chesspad_flush(pad) if __name__=='__main__':
main()
以上代码后面有修改,98行、112行本来是自己写的一个反转字符串的函数实现,后来才知道用切片str[::-1]实现更简洁。