python小專案之五子棋遊戲
本文例項為大家分享了python五子棋遊戲的具體程式碼,供大家參考,具體內容如下
1.專案簡介
在剛剛學習完python套接字的時候做的一個五子棋小遊戲,可以在區域網內雙人對戰,也可以和電腦對戰
2.實現思路
區域網對戰
對於區域網功能來說,首先建立連線(tcp),然後每次下棋時將棋子的座標傳送給對方,當接收到座標後例項化成棋子物件,這個接收時用了select函式,因為pygame需要迴圈渲染圖片,所以要用非阻塞方式接收訊息
select()的機制中提供一fd_set的資料結構,實際上是一long型別的陣列, 每一個數組元素都能與一開啟的檔案控制代碼(不管是Socket控制代碼,還是其他檔案或命名管道或裝置控制代碼)建立聯絡,建立聯絡的工作由程式設計師完成, 當呼叫select()時,由核心根據IO狀態修改fd_set的內容,由此來通知執行了select()的程序哪一Socket或檔案可讀或可寫,主要用於Socket通訊當中
主要程式碼如下:
# 接收cli的訊息 if is_people: rs,ws,es = select.select(inputs,[],0) for r in rs: if r is tcpclisock: try: data = r.recv(BUFSIZ) islink = True print(data.decode('utf8')) if data.decode('utf8') == 'again': is_recieve1 = True if data.decode('utf8') == 'yes': is_playagain = True is_play = True if data.decode('utf8') == 'no': is_recieve2 = True islink = False if not is_play and not result: me = storn.Storn_Black(eval(data)) black_chesses.append(me) chesses.append(me) is_play = True except error: islink = False
電腦對戰
電腦對戰的思路也很簡單,用了應該是最常見的也是最簡單的方法,就是迴圈遍歷棋盤的每一個點,判斷該點的價值,選取價值最大的點落子,這個需要對五子棋的棋型有一定了解,這裡介紹幾種常見的棋型(約定1為己方棋子,2為對方棋子,0為空)
活四(011110):這時候四顆棋子相連,同時兩端為空,已經阻止不了一方的勝利了,此時價值應該設定最高
死四(011112|10111|11011):四顆棋子,只有一個地方能形成連五,如果是自己的棋可以贏,是對方的也可以阻止對方贏棋,此時價值次高
就這樣把每種棋型判斷一下,獲得該點的價值,主要程式碼如下:
# 判斷每個點的價值 def point_value(pos,white_chesses,black_chesses,identify1,identify2): value = 0 for i in range(1,9): # *1111_ 活四 if get_point(pos,i,1,black_chesses) == identify1 and \ get_point(pos,2,3,4,5,black_chesses) == 0: value += 40000 # *11112 死四1 if get_point(pos,black_chesses) == identify2: value += 30000 # 1*111 死四2 if get_point(pos,-1,black_chesses) == identify1: value += 30000 # 11*11 死四3 if get_point(pos,-2,black_chesses) == identify1: value += 30000 # *111_ 活三1 if get_point(pos,black_chesses) == 0: value += 20000 # *1_11_ 活三2 if get_point(pos,black_chesses) == 0 and \ get_point(pos,black_chesses) == 0: value += 20000 # *1112 死三1 if get_point(pos,black_chesses) == identify2: value += 15000 # _1_112 死三2 if get_point(pos,black_chesses) == identify2: value += 15000 # _11_12 死三3 if get_point(pos,black_chesses) == identify2: value += 15000 # 1__11 死三4 if get_point(pos,black_chesses) == identify1: value += 15000 # 1_1_1 死三5 if get_point(pos,black_chesses) == identify1: value += 15000 # 2_111_2 死三6 if get_point(pos,black_chesses) == identify2 and \ get_point(pos,black_chesses) == identify2: value += 15000 # __11__ 活二1 if get_point(pos,black_chesses) == 0: value += 1000 # _1_1_ 活二2 if get_point(pos,black_chesses) == 0: value += 1000 # *1__ if get_point(pos,black_chesses) == 0: value += 30 # *1_ if get_point(pos,black_chesses) == 0: value += 20 # *1 if get_point(pos,black_chesses) == identify1: value += 10 return value
電腦選擇落子位置時,要判斷是進攻還是防守,需要兩次遍歷棋盤,獲取進攻時的最大價值和防守的最大價值,主要程式碼如下:
# 電腦選取落子的位置 def ai(white_chesses,chesses): value = max1 = max2 = 0 pos1 = pos2 = () # 進攻時 for i in range(0,15): row = 28 + i * 40 for j in range(0,15): col = 28 + j * 40 pos = (row,col) if is_empty(pos,chesses): continue value = point_value(pos,2) if value > max1: max1 = value pos1 = (row,col) # 防守時 for i in range(0,15): for j in range(0,15): row = 28 + i * 40 col = 28 + j * 40 if is_empty((row,col),chesses): continue value = point_value((row,1) if value > max2: max2 = value pos2 = (row,col) if max1 > max2: return pos1 else: return pos2
3.遊戲截圖
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。