欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python小項目之五子棋游戲

 更新時間:2019年12月26日 08:45:06   作者:程序員lamed  
這篇文章主要為大家詳細(xì)介紹了python小項目之五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了python五子棋游戲的具體代碼,供大家參考,具體內(nèi)容如下

1.項目簡介

在剛剛學(xué)習(xí)完python套接字的時候做的一個五子棋小游戲,可以在局域網(wǎng)內(nèi)雙人對戰(zhàn),也可以和電腦對戰(zhàn)

2.實現(xiàn)思路

局域網(wǎng)對戰(zhàn)

對于局域網(wǎng)功能來說,首先建立連接(tcp),然后每次下棋時將棋子的坐標(biāo)發(fā)送給對方,當(dāng)接收到坐標(biāo)后實例化成棋子對象,這個接收時用了select函數(shù),因為pygame需要循環(huán)渲染圖片,所以要用非阻塞方式接收消息

select()的機(jī)制中提供一fd_set的數(shù)據(jù)結(jié)構(gòu),實際上是一long類型的數(shù)組, 每一個數(shù)組元素都能與一打開的文件句柄(不管是Socket句柄,還是其他文件或命名管道或設(shè)備句柄)建立聯(lián)系,建立聯(lián)系的工作由程序員完成, 當(dāng)調(diào)用select()時,由內(nèi)核根據(jù)IO狀態(tài)修改fd_set的內(nèi)容,由此來通知執(zhí)行了select()的進(jìn)程哪一Socket或文件可讀或可寫,主要用于Socket通信當(dāng)中

主要代碼如下:

# 接收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

電腦對戰(zhàn)

電腦對戰(zhàn)的思路也很簡單,用了應(yīng)該是最常見的也是最簡單的方法,就是循環(huán)遍歷棋盤的每一個點,判斷該點的價值,選取價值最大的點落子,這個需要對五子棋的棋型有一定了解,這里介紹幾種常見的棋型(約定1為己方棋子,2為對方棋子,0為空)

活四(011110):這時候四顆棋子相連,同時兩端為空,已經(jīng)阻止不了一方的勝利了,此時價值應(yīng)該設(shè)置最高
死四(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

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

# 電腦選取落子的位置
def ai(white_chesses, black_chesses, chesses):
 value = max1 = max2 = 0
 pos1 = pos2 = ()
 # 進(jìn)攻時
 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.游戲截圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深入理解Python內(nèi)置函數(shù)eval的使用

    深入理解Python內(nèi)置函數(shù)eval的使用

    在Python中,eval函數(shù)是一個內(nèi)置函數(shù),用于將字符串解析并執(zhí)行為Python表達(dá)式,本文將詳細(xì)介紹eval函數(shù)的使用方法和注意事項,需要的可以參考一下
    2023-06-06
  • Python人工智能實戰(zhàn)之以圖搜圖的實現(xiàn)

    Python人工智能實戰(zhàn)之以圖搜圖的實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了如何基于vgg網(wǎng)絡(luò)和Keras深度學(xué)習(xí)框架實現(xiàn)以圖搜圖功能。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下
    2022-05-05
  • 教你如何在Pytorch中使用TensorBoard

    教你如何在Pytorch中使用TensorBoard

    TensorBoard是TensorFlow中強(qiáng)大的可視化工具,今天通過本文給大家介紹如何在Pytorch中使用TensorBoard,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友一起看看吧
    2021-08-08
  • 關(guān)于NumPy中asarray的用法及說明

    關(guān)于NumPy中asarray的用法及說明

    這篇文章主要介紹了關(guān)于NumPy中asarray的用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python實現(xiàn)獲取郵箱內(nèi)容并解析的方法示例

    Python實現(xiàn)獲取郵箱內(nèi)容并解析的方法示例

    這篇文章主要介紹了Python實現(xiàn)獲取郵箱內(nèi)容并解析的方法,結(jié)合完整實例形式分析了Python登陸pop3服務(wù)器并解析獲取郵箱內(nèi)容相關(guān)操作技巧,需要的朋友可以參考下
    2018-06-06
  • python實現(xiàn)的生成隨機(jī)迷宮算法核心代碼分享(含游戲完整代碼)

    python實現(xiàn)的生成隨機(jī)迷宮算法核心代碼分享(含游戲完整代碼)

    這篇文章主要介紹了python實現(xiàn)的隨機(jī)迷宮生成算法核心代碼分享,本文包含一個簡單迷宮游戲完整代碼,需要的朋友可以參考下
    2014-07-07
  • Flask框架利用Echarts實現(xiàn)繪制圖形

    Flask框架利用Echarts實現(xiàn)繪制圖形

    echarts是百度推出的一款開源的基于JavaScript的可視化圖表庫,該開發(fā)庫目前發(fā)展非常不錯,且支持各類圖形的繪制可定制程度高。如下演示案例中,將分別展示運用該繪圖庫如何前后端交互繪制(餅狀圖,柱狀圖,折線圖)這三種最基本的圖形,需要的可以參考一下
    2022-10-10
  • python的numpy模塊實現(xiàn)邏輯回歸模型

    python的numpy模塊實現(xiàn)邏輯回歸模型

    這篇文章主要為大家詳細(xì)介紹了python的numpy模塊實現(xiàn)邏輯回歸模型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • python服務(wù)器與android客戶端socket通信實例

    python服務(wù)器與android客戶端socket通信實例

    這篇文章主要介紹了python服務(wù)器與android客戶端socket通信的實現(xiàn)方法,實例形式詳細(xì)講述了Python的服務(wù)器端實現(xiàn)原理與方法,以及對應(yīng)的Android客戶端實現(xiàn)方法,需要的朋友可以參考下
    2014-11-11
  • Python內(nèi)存映射文件讀寫方式

    Python內(nèi)存映射文件讀寫方式

    這篇文章主要介紹了Python內(nèi)存映射文件讀寫方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04

最新評論