亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python小項(xiàng)目之五子棋游戲

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

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

1.項(xiàng)目簡(jiǎn)介

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

2.實(shí)現(xiàn)思路

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

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

select()的機(jī)制中提供一fd_set的數(shù)據(jù)結(jié)構(gòu),實(shí)際上是一long類型的數(shù)組, 每一個(gè)數(shù)組元素都能與一打開的文件句柄(不管是Socket句柄,還是其他文件或命名管道或設(shè)備句柄)建立聯(lián)系,建立聯(lián)系的工作由程序員完成, 當(dāng)調(diào)用select()時(shí),由內(nèi)核根據(jù)IO狀態(tài)修改fd_set的內(nèi)容,由此來(lá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

電腦對(duì)戰(zhàn)

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

活四(011110):這時(shí)候四顆棋子相連,同時(shí)兩端為空,已經(jīng)阻止不了一方的勝利了,此時(shí)價(jià)值應(yīng)該設(shè)置最高
死四(011112|10111|11011):四顆棋子,只有一個(gè)地方能形成連五,如果是自己的棋可以贏,是對(duì)方的也可以阻止對(duì)方贏棋,此時(shí)價(jià)值次高

就這樣把每種棋型判斷一下,獲得該點(diǎn)的價(jià)值,主要代碼如下:

# 判斷每個(gè)點(diǎn)的價(jià)值
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

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

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

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

相關(guān)文章

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

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

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

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

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

    教你如何在Pytorch中使用TensorBoard

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論