日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)AI教程:AI人工智能模擬游戲

一個機器人玩最后的硬幣

在這場比賽中,會有一堆硬幣。 每個玩家必須從該堆中取出一些硬幣。這場比賽的目標是避免拿下最后一枚硬幣。 我們將使用繼承自 easyAI 庫的 TwoPlayersGame 類的 LastCoinStanding 類。 以下代碼顯示了此游戲的 Python代碼 -

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),龍門企業(yè)網(wǎng)站建設(shè),龍門品牌網(wǎng)站建設(shè),網(wǎng)站定制,龍門網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,龍門網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

如下所示導入所需的軟件包 -

from easyAI import TwoPlayersGame, id_solve, Human_Player, AI_Player
from easyAI.AI import TT

現(xiàn)在,繼承 TwoPlayerGame 類中的類來處理游戲的所有操作 -

class LastCoin_game(TwoPlayersGame):
    def __init__(self, players):

定義要玩家并開始游戲。

self.players = players
self.nplayer = 1

定義游戲中的硬幣數(shù)量,這里使用15個硬幣進行游戲。

self.num_coins = 15

定義玩家在移動中可以獲得的最大硬幣數(shù)量。

self.max_coins = 4

現(xiàn)在有一些東西需要定義,如下面的代碼所示。 定義可能的移動。

def possible_moves(self):
   return [str(a) for a in range(1, self.max_coins + 1)]

定義硬幣的清除 -

def make_move(self, move):
   self.num_coins -= int(move)

定義誰拿走了最后一枚硬幣。

def win_game(self):
   return self.num_coins <= 0

定義何時停止游戲,即何時有人獲勝。

def is_over(self):
   return self.win()

定義如何計算分數(shù)。

def score(self):
   return 100 if self.win_game() else 0

定義堆中剩余的硬幣數(shù)量。

def show(self):
   print(self.num_coins, 'coins left in the pile')
if __name__ == "__main__":
   tt = TT()
   LastCoin_game.ttentry = lambda self: self.num_coins

用下面的代碼塊解決游戲 -

r, d, m = id_solve(LastCoin_game,
   range(2, 20), win_score=100, tt=tt)
print(r, d, m)

決定誰將開始游戲

game = LastCoin_game([AI_Player(tt), Human_Player()])
game.play()

下面的輸出演示這個游戲的簡單玩法 -

d:2, a:0, m:1
d:3, a:0, m:1
d:4, a:0, m:1
d:5, a:0, m:1
d:6, a:100, m:4
1 6 4
15 coins left in the pile
Move #1: player 1 plays 4 :
11 coins left in the pile
Player 2 what do you play ? 2
Move #2: player 2 plays 2 :
9 coins left in the pile
Move #3: player 1 plays 3 :
6 coins left in the pile
Player 2 what do you play ? 1
Move #4: player 2 plays 1 :
5 coins left in the pile
Move #5: player 1 plays 4 :
1 coins left in the pile
Player 2 what do you play ? 1
Move #6: player 2 plays 1 :
0 coins left in the pile

機器人玩井字游戲

Tic-Tac-Toe 非常熟悉,是最受歡迎的游戲之一。我們通過使用 Python 中的 easyAI 庫來創(chuàng)建這個游戲。 以下代碼是這款游戲的 Python 代碼 -

如下所示導入軟件包 -

from easyAI import TwoPlayersGame, AI_Player, Negamax
from easyAI.Player import Human_Player

繼承 TwoPlayerGame 中的類來處理游戲的所有操作 -

class TicTacToe_game(TwoPlayersGame):
   def __init__(self, players):

現(xiàn)在,定義玩家并開始游戲 -

self.players = players
self.nplayer = 1

定義板的類型 -

self.board = [0] * 9

定義可能的舉措(動作)

def possible_moves(self):
    return [x + 1 for x, y in enumerate(self.board) if y == 0]

定義一個玩家的舉措(動作) -

def make_move(self, move):
    self.board[int(move) - 1] = self.nplayer

定義一個玩家何時進行移動 -

def umake_move(self, move):
   self.board[int(move) - 1] = 0

定義輸條件是對手在一條線上有三個 -

def condition_for_lose(self):
   possible_combinations = [[1,2,3], [4,5,6], [7,8,9],
      [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]]return any([all([(self.board[z-1] == self.nopponent)
      for z in combination]) for combination in possible_combinations])

定義游戲結(jié)束的條件 -

def is_over(self):
   return (self.possible_moves() == []) or self.condition_for_lose()

顯示玩家在游戲中的當前位置 -

def show(self):
   print('\n'+'\n'.join([' '.join([['.', 'O', 'X'][self.board[3*j + i]]for i in range(3)]) for j in range(3)]))

計算分數(shù)代碼 -

def scoring(self):
   return -100 if self.condition_for_lose() else 0

定義定義算法并開始游戲的主要方法 -

if __name__ == "__main__":
   algo = Negamax(7)
   TicTacToe_game([Human_Player(), AI_Player(algo)]).play()

可以看到下面的輸出和這個游戲的簡單玩法 -

. . .
. . .
. . .
Player 1 what do you play ? 1
Move #1: player 1 plays 1 :
O . .
. . .
. . .
Move #2: player 2 plays 5 :
O . .
. X .
121
. . .
Player 1 what do you play ? 3
Move #3: player 1 plays 3 :
O . O
. X .
. . .
Move #4: player 2 plays 2 :
O X O
. X .
. . .
Player 1 what do you play ? 4
Move #5: player 1 plays 4 :
O X O
O X .
. . .
Move #6: player 2 plays 8 :
O X O
O X .
. X .

當前名稱:創(chuàng)新互聯(lián)AI教程:AI人工智能模擬游戲
標題URL:http://m.5511xx.com/article/djjjscs.html