中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python小項目之五子棋游戲

發布時間:2020-09-20 23:37:32 來源:腳本之家 閱讀:144 作者:程序員lamed 欄目:開發技術

本文實例為大家分享了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, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == 0:
   value += 40000
  # *11112 死四1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 30000
  # 1*111 死四2
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 30000
  # 11*11 死四3
  if get_point(pos, i, -2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1:
   value += 30000
  # *111_ 活三1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 20000
  # *1_11_ 活三2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == 0:
   value += 20000
  # *1112 死三1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify2:
   value += 15000
  # _1_112 死三2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # _11_12 死三3
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # 1__11 死三4
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 15000
  # 1_1_1 死三5
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1:
   value += 15000
  # 2_111_2 死三6
  if get_point(pos, i, -1, white_chesses, black_chesses) == identify2 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 5, white_chesses, black_chesses) == identify2:
   value += 15000
  # __11__ 活二1
  if get_point(pos, i, -1, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 1000
  # _1_1_ 活二2
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 4, white_chesses, black_chesses) == 0:
   value += 1000
  # *1__
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0 and \
   get_point(pos, i, 3, white_chesses, black_chesses) == 0:
   value += 30
  # *1_
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1 and \
   get_point(pos, i, 2, white_chesses, black_chesses) == 0:
   value += 20
  # *1
  if get_point(pos, i, 1, white_chesses, black_chesses) == identify1:
   value += 10
 return value

電腦選擇落子位置時,要判斷是進攻還是防守,需要兩次遍歷棋盤,獲取進攻時的最大價值和防守的最大價值,主要代碼如下:

# 電腦選取落子的位置
def ai(white_chesses, black_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, white_chesses, black_chesses, 1, 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,col), white_chesses, black_chesses, 2, 1)
    if value > max2:
     max2 = value
     pos2 = (row,col)
  if max1 > max2:
   return pos1
  else:
   return pos2

3.游戲截圖

python小項目之五子棋游戲

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

临洮县| 格尔木市| 桃源县| 永嘉县| 简阳市| 会同县| 江源县| 偏关县| 雅安市| 崇明县| 拉孜县| 金湖县| 廉江市| 柏乡县| 桐柏县| 大荔县| 灵寿县| 四平市| 涟水县| 睢宁县| 穆棱市| 濉溪县| 敦化市| 贞丰县| 上思县| 平昌县| 项城市| 富阳市| 耿马| 仁寿县| 平江县| 定南县| 西乌珠穆沁旗| 武宣县| 长子县| 千阳县| 余干县| 塘沽区| 岑巩县| 隆化县| 嘉善县|