Python五子棋游戏
Jacky-008 人气:0class CheckerBoard(): '''棋盘类''' def __init__(self,col=0,row=0,piece_type='--'): self.col = col self.row = row self.piece_type = piece_type self.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 checkerboardl def 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 = id self.name = name self.piece_type = piece_type def 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_type return data,True else: print('\n位置错误,已经有棋子了,重新输入:') return data,False def __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 slist def __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 slist def __left_cut_list(self, x, y, col,row, data): '''获取下棋子的左斜角所有棋子的状态 x:下棋子的x坐标 y:下棋子的y坐标 col:棋盘的最大横向坐标 row:棋盘的最大纵向坐标 data:棋盘中棋子的状态数据 ''' slist = [] if 0 <= x+y and row > x+y: x_val_init = x+y y_val_init = 0 while x_val_init >= 0 and y_val_init <= row-1: val = data[y_val_init][x_val_init] slist.append(val) x_val_init -= 1 y_val_init += 1 else: x_val_init = col-1 y_val_init = y while x_val_init > 0 and y_val_init <= row-1: val = data[y_val_init][x_val_init] slist.append(val) x_val_init -= 1 y_val_init += 1 print(slist, '__left_cut_list') return slist def __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-y y_val_init = 0 while 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 += 1 y_val_init += 1 else: x_val_init = 0 y_val_init = y-x while 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 += 1 y_val_init += 1 print(slist, '__right_cut_list') return slist def fif_diff(slef,list5): '''判断连续五个棋子状态都一样''' # list5:5个元素的列表 piece_type = slef.piece_type for i in range(len(list5)): if list5[i] != piece_type: return False return True def rule(self,x, y, col,row, data): '''规则:连续5个棋子状态都一样时,返回True x:下棋子的x坐标 y:下棋子的y坐标 col:棋盘的最大横向坐标 row:棋盘的最大纵向坐标 data:棋盘中棋子的状态数据 ''' status = False rule_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 = 0 end = 5 while end < len(slist) + 4: fif_ele_list = slist[start:end] if self.fif_diff(fif_ele_list) : print('%s赢了' %self.name,'11111111111111') status = True break else: start += 1 end += 1 return status def 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 True else: inputbox(obj, pool, checkerboardl_init) pool = [] checkerboardl=[] static = '' if __name__ == '__main__': ST = True chess = 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 =1 while ST: if z == 1: a = inputbox(s1, pool, checkerboardl_init) if a : break z = 2 continue if z == 2: b = inputbox(s2, pool, checkerboardl_init) if b : break z = 1
简化图:
加载全部内容