您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何用Python實現簡易五子棋游戲的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇如何用Python實現簡易五子棋游戲文章都會有所收獲,下面我們一起來看看吧。
具體代碼如下
class 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
簡化圖:
關于“如何用Python實現簡易五子棋游戲”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“如何用Python實現簡易五子棋游戲”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。