Python实现简易五子棋游戏
本文实例为大家分享了Python实现五子棋游戏的具体代码,供大家参考,具体内容如下
class CheckerBoard():'''棋盘类'''def __init__(self,col=0,row=0,piece_type='--'):self.col = colself.row = rowself.piece_type = piece_typeself.labelx = range(self.col)self.labely = range(self.row)def chessboard(self):'''棋盘初始化数据(棋子的状态)'''checkerboardl = []for y in self.labely:heading_list = []checkerboardl.append(heading_list)for x in self.labelx:heading_list.append(self.piece_type)return checkerboardldef chess_show(self,checkerboardl):'''显示棋子的状态'''print('\0\0\0',end=' ')for x_title in self.labelx:print(str(x_title).zfill(2),end=' ')for y_title in range(len(checkerboardl)):print('\n',str(y_title).zfill(2),end=' ')for instans in checkerboardl[y_title]:print(instans,end=' ')class User():'''玩家类'''def __init__(self,id,name,piece_type):'''构造函数id :玩家 id标识name:玩家名称piece_type:棋子的状态'''self.id = idself.name = nameself.piece_type = piece_typedef play(self,x,y,pool,data):'''play():玩家下棋子x:下棋子的x坐标y:下棋子的y坐标pool:棋子的状态集合data:棋盘上的棋子状态数据'''if data[y][x] not in pool:data[y][x] = self.piece_typereturn data,Trueelse:print('\n位置错误,已经有棋子了,重新输入:')return data,Falsedef __col_list(self, x, y, col,row, data):'''获取下棋子的x轴所有棋子的状态x:下棋子的x坐标y:下棋子的y坐标col:棋盘的最大横向坐标row:棋盘的最大纵向坐标data:棋盘中棋子的状态数据'''# slist = []slist = data[y]print(slist,'__col_list')return slistdef __row_list(self, x, y, col,row, data):'''获取下棋子的y轴所有棋子的状态x:下棋子的x坐标y:下棋子的y坐标col:棋盘的最大横向坐标row:棋盘的最大纵向坐标data:棋盘中棋子的状态数据'''slist = []for i in data:slist.append(i[x])print(slist, '__row_list')return slistdef __left_cut_list(self, x, y, col,row, data):'''获取下棋子的左斜角所有棋子的状态x:下棋子的x坐标y:下棋子的y坐标col:棋盘的最大横向坐标row:棋盘的最大纵向坐标data:棋盘中棋子的状态数据'''slist = []if 0 <= x+y androw > x+y:x_val_init = x+yy_val_init = 0while x_val_init >= 0 and y_val_init <= row-1:val = data[y_val_init][x_val_init]slist.append(val)x_val_init -= 1y_val_init += 1else:x_val_init = col-1y_val_init = ywhile x_val_init > 0 and y_val_init <= row-1:val = data[y_val_init][x_val_init]slist.append(val)x_val_init -= 1y_val_init += 1print(slist, '__left_cut_list')return slistdef __right_cut_list(self, x, y, col,row, data):'''获取下棋子的右斜角所有棋子的状态x:下棋子的x坐标y:下棋子的y坐标col:棋盘的最大横向坐标row:棋盘的最大纵向坐标data:棋盘中棋子的状态数据'''slist = []if 0 <= x-y :x_val_init = x-yy_val_init = 0while x_val_init <= col-1 and y_val_init <= row-1:val = data[y_val_init][x_val_init]slist.append(val)x_val_init += 1y_val_init += 1else:x_val_init = 0y_val_init = y-xwhile x_val_init <= col-1 and y_val_init <= row-1:val = data[y_val_init][x_val_init]slist.append(val)x_val_init += 1y_val_init += 1print(slist, '__right_cut_list')return slistdef fif_diff(slef,list5):'''判断连续五个棋子状态都一样'''# list5:5个元素的列表piece_type = slef.piece_typefor i in range(len(list5)):if list5[i] != piece_type:return Falsereturn Truedef rule(self,x, y, col,row, data):'''规则:连续5个棋子状态都一样时,返回Truex:下棋子的x坐标y:下棋子的y坐标col:棋盘的最大横向坐标row:棋盘的最大纵向坐标data:棋盘中棋子的状态数据'''status = Falserule_list = [self.__col_list,self.__row_list,self.__left_cut_list,self.__right_cut_list]for i in range(len(rule_list)):get = rule_list[i]slist = get(x, y, col,row, data)if len(slist) >= 5:start = 0end = 5while end < len(slist) + 4:fif_ele_list = slist[start:end]if self.fif_diff(fif_ele_list) :print('%s赢了' %self.name,'11111111111111')status = Truebreakelse:start += 1end += 1return statusdef inputbox(obj, pool, checkerboardl_init):''' 输入横向纵向坐标'''print('\n*************************************************')x = int(input('%s请输入横坐标:'%obj.name).strip())if not 0 <= x < 15:x = int(input('%s请输入横坐标:'%obj.name).strip())y = int(input('%s请输入纵坐标:'%obj.name).strip())if not 0 <= y < 15:y = int(input('%s请输入纵坐标:'%obj.name).strip())checkerboardl,static = obj.play(x, y, pool, checkerboardl_init)if static:s = obj.rule(x, y, 15, 15, checkerboardl)chess.chess_show(checkerboardl)if s:return Trueelse:inputbox(obj, pool, checkerboardl_init)pool = []checkerboardl=[]static = ''if __name__ == '__main__':ST = Truechess = CheckerBoard(15,15)checkerboardl_init = chess.chessboard()chess.chess_show(checkerboardl_init)s1 = User(1,'jack','@@')pool.append(s1.piece_type)s2 = User(2, 'chen', '##')pool.append(s2.piece_type)z =1whileST:if z == 1:a = inputbox(s1, pool, checkerboardl_init)if a :breakz = 2continueif z == 2:b = inputbox(s2, pool, checkerboardl_init)if b :breakz = 1
【Python实现简易五子棋游戏】简化图:
文章图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 用python实现五子棋实例
- oeasy教您玩转python - 012 - # 刷新时间
- 通用mapper和分类实现
- 其他|C语言实现一维小波变换
- 结合实例分析Android MVP的实现
- Python MongoDB连接操作实例
- Python|假期用Python写8个小游戏(附源码)
- python魂斗罗源码_Python魂斗罗小游戏源代码
- python|机器学习之朴素贝叶斯算法+应用实例
- python在使用绝对路径时出现OSError: [Errno 22] Invalid argument: ‘E:\python\python扩展\text_file\pi_digits.txt