本文共 6982 字,大约阅读时间需要 23 分钟。
Python控制台版五子棋
1、写在前面 最近从各种渠道接触到了一些有趣的Python小练习题,适合对Python基础有一定了解的童鞋练习,因为问题都来源于生活,所以码起来有一些趣味,自己也断断续续把代码敲完,现在自娱自乐地码出来吧,理解错的地方请多包涵。另外,反正我也知道没人看,就当是打发时间,给自己的学习过程留一点印记吧。先来写一个Python控制台版的五子棋O(∩_∩)O~ 2、棋盘构建 棋盘默认设置10*10大小,这里其实可用类来自定义棋盘大小,但是用了后不太会给棋盘的轴线标号,所以暂时将(tou)就(xia)下(lan)吧。def Get_Checherboard(): checkerboard = [] for i in range(10): checkerboard.append([]) for j in range(10): checkerboard[i].append('-') return checkerboard
3、显示棋盘
下棋嘛肯定是要看得见棋盘,不然一直下盲棋谁顶得住?该部分在Python控制台打印棋盘,包括之后落子后的棋盘也是用这个显示。首先打印一行数字坐标(1-10),之后在每行前面加上字母坐标,再打印棋盘checkerboard中的内容。def Show(): print(' 1 2 3 4 5 6 7 8 9 10') for i in range(len(checkerboard)): print(chr(i + ord('A')) + ' ' ,end = '') for j in range(len(checkerboard)): print(checkerboard[i][j] + ' ',end = '') print()
效果显示
1 2 3 4 5 6 7 8 9 10A - - - - - - - - - - B - - - - - - - - - - C - - - - - - - - - - D - - - - - - - - - - E - - - - - - - - - - F - - - - - - - - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - - Please input your choice(exit-0):
4、胜负判断
这部分是代码最麻烦的部分,最开始想算法时是想每次下棋都逐行逐列逐斜每个棋子检查,这样每次检查时就和落子地点无关,直接调用就行。但是后来发现这样效率低,而且代码不好写,就主动放弃(应证了古话世上无难事只要肯放弃)。后来借鉴别人的思路,每次落子时检查四个部分(行、列、两斜)(假设落子地方为(x,y),A-J为x,1-10为y),只要有一个部分的值为True,那么就获胜。下面检查的方法也是自己琢磨的,索性还挺好用。def Check(pos,color): ''' pos[0]代表x,pos[1]代表y,color 为不同的棋子, 函数输入落子坐标pos和对应棋子color,输出布尔值-是否获胜 ''' if Check_row(pos, color) or Check_columns(pos, color) \ or Check_oblique_1(pos, color) or Check_oblique_2(pos, color): return True else: return False
判断是否获胜需要检查四个方面:
(1)检查行:从(x-4,y)到(x+4,y),是否有五个一样的棋子。但是有可能x-4会超出棋盘的范围,若超出范围,就从(0,y)开始;同理,右边x+4若超出范围,就以(9,y)结束。这样检查的坐标范围是(max(0,x-4),y)到(min(9,x+4),y)def Check_row(pos,color): start_x = max(0,pos[0] - 4) end_x = min(9,pos[0] + 4) count = 0 for pos_x in range(start_x, end_x + 1): if checkerboard[pos_x][pos[1]] == color: count += 1 if count >= 5: return True else: #若前后棋子不一致,计数器归零 count = 0 return False
(2)检查列:原理同上,检查的坐标范围是(x,max(0,y-4))到(x,min(9,y+4))
def Check_columns(pos,color): start_y = max(0,pos[1] - 4) end_y = min(9,pos[1] + 4) count = 0 for pos_y in range(start_y, end_y + 1): if checkerboard[pos[0]][pos_y] == color: count += 1 if count >= 5: return True else: count = 0 return False
(3)检查左上右下方向从(x-4,y-4)到(x+4,y+4):检查斜需要稍动脑筋,设横纵坐标差s = x-y,分两种情况:
1、若x>=y,那么找左端的检查起始点时,若x-4>0,y-4>0,那么检查的起点无疑就是(s+(y-4),y-4),但是若起点靠边了,那么检查起始点的纵坐标y就为0(因为x>y,所以肯定是y方向先靠边),横坐标为0+s,即点(0+s,0)。所以综合两种情况:左边检查的起始点是(max(0,y-4)+s,max(0,y-4))。同理,若x+4<9,y+4<9,那么右边检查结束点为(x+4,x+4-s),若终点靠边了,那么检查的终点横坐标x就为9(因为x>y,所以是x方向先靠边),纵坐标为9-s,所以终点的坐标为(min(9,x+4),min(9,x+4)-s)。 2、若x<y,推理同上,左边检查的起始点就是(max(0,x-4),max(0,x-4)-s),右边检查的终点是(min(9,y+4)+s,min(9,y+4))。有了起点和终点,生成x和y方向的起止序列range(start_point_x,end_point_x + 1)和range(start_point_y,end_point_y + 1)),再用zip将横纵坐标对应打包起来检查即可。def Check_oblique_1(pos,color): if pos[0] >= pos[1]: start_point_y = max(0,pos[1] - 4) start_point_x = start_point_y + (pos[0] - pos[1]) end_point_x = min(9,pos[0] + 4) end_point_y = end_point_x - (pos[0] - pos[1]) else: start_point_x = max(0,pos[0] - 4) start_point_y = start_point_x - (pos[0] - pos[1]) end_point_y = min(9,pos[1] + 4) end_point_x = end_point_y + (pos[0] - pos[1]) count = 0 for i,j in zip(range(start_point_x,end_point_x + 1),range(start_point_y,end_point_y + 1)): if checkerboard[i][j] == color: count += 1 if count >= 5: return True else: count = 0 return False
(4)检查右上左下方向
情况也分两种:1、x+y<=9 和 2、x+y>9,坐标在代码中应该看得清。注意在生成x坐标序列时应注意结束点坐标要小于开始点,所以序列是range(end_point_x,start_point_x + 1)。由于水平有限,只能转换成列表翻转后再和y坐标序列打包。def Check_oblique_2(pos,color): s = sum(pos) if pos[0] + pos[1] <= 9: start_point_y = max(0,pos[1] - 4) start_point_x = s -start_point_y end_point_x = max(0,pos[0] - 4) end_point_y = s - end_point_x else: start_point_x = min(9,pos[0] + 4) start_point_y = s - start_point_x end_point_y = min(9,pos[1] + 4) end_point_x = s - end_point_y count = 0 tmp_lst = list(range(end_point_x,start_point_x + 1)) tmp_lst.reverse() for i,j in zip(tmp_lst,list(range(start_point_y,end_point_y + 1))): if checkerboard[i][j] == color: count += 1 if count >= 5: return True else: count = 0 return False
(5)主函数部分
设置好棋盘,定义棋子形状为M和O,每次取color中的第一个元素(color[0])落子,落子后color反转,表示双方交替落子,最后加上一些异常处理,就是下面这段代码了。本来逻辑很好理解,但加上异常处理代码看起来比较乱。这也是没办法,因为总会有些人在输入时搞幺蛾子,故意逗你玩。checkerboard = Get_Checherboard()def main(): color = ['M','O'] dic = { 'A':0,'B':1,'C':2,'D':3,'E':4,'F':5,'G':6,'H':7,'I':8,'J':9} Show() Mark = True while Mark: choice = input('Please input your choice(exit-0):') if choice == '0': break elif not choice[0].isalpha() or not choice[1:].isdigit(): print('Please input right position!') continue else: choice_y = choice[0].title() choice_x = int(choice[1:]) - 1 try: if 0 <= dic[choice_y] <= 9 and 0 <= choice_x <= 9: if checkerboard[dic[choice_y]][choice_x] != '-': print('Chess piece already exists in this position!') else: if color[0] == 'M': checkerboard[dic[choice_y]][choice_x] = 'M' if Check([dic[choice_y],choice_x], color[0]): print('Congs!Player 1 win!') Mark = False Show() color.reverse() else: checkerboard[dic[choice_y]][choice_x] = 'O' if Check([dic[choice_y],choice_x], color[0]): print('Congs!Player 2 win!') Mark = False Show() color.reverse() else: print('Position out of range!') except: print('Please input right position!') if __name__ == "__main__": main()
6、效果显示
Congs!Player 1 win! 1 2 3 4 5 6 7 8 9 10A - M - - - - - - - - B - - M O - M - - - - C - - M M O O - - - - D - - O M M M - - - - E - - - O O M - - - - F - - - - O - O - - - G - - - - - - - - - - H - - - - - - - - - - I - - - - - - - - - - J - - - - - - - - - -
这样就是尽我所能写的啦,本来还想加入悔棋的设置的,限于水平和精力就不写了,反正没人看,后面还有几个有趣的小练习题,后面陆续码出来。如果屏幕前的你能看到这篇博客,并且能看到底,那说明你我是有缘分的O(∩_∩)O,哈哈,就这样啦,有缘人再见!
转载地址:http://khtg.baihongyu.com/