코인 게임 — 마지막 코인을 빼는 자가 지는 게임

 

# This is a variant of the Game of Bones recipe given in the easyAI library

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

class LastCoinStanding(TwoPlayersGame):
(space)def __init__(self, players):

# Define the players. Necessary parameter.
(space)(space)self.players = players

# Define who starts the game. Necessary parameter.
(space)(space)self.nplayer = 1

# Overall number of coins in the pile
(space)(space)self.num_coins = 25

# Define max number of coins per move
(space)(space)self.max_coins = 4

# Define possible moves
(space)def possible_moves(self):
(space)(space)return [str(x) for x in range(1, self.max_coins + 1)]

# Remove coins
(space)def make_move(self, move):
(space)(space)self.num_coins -= int(move)

# Did the opponent take the last coin?
(space)def win(self):
(space)(space)return self.num_coins <= 0

# Stop the game when somebody wins
(space)def is_over(self):
(space)(space)return self.win()

# Compute score
(space)def scoring(self):
(space)(space)return 100 if self.win() else 0

# Show number of coins remaining in the pile
(space)def show(self):
(space)(space)print(self.num_coins, ‘coins left in the pile’)

if __name__ == “__main__”:

# Define the transposition table
(space)tt = TT()

# Define the method
(space)LastCoinStanding.ttentry = lambda self: self.num_coins

# Solve the game
(space)result, depth, move = id_solve(LastCoinStanding,
(space)(space)range(2, 20), win_score=100, tt=tt)
(space)print(result, depth, move)

# Start the game
(space)game = LastCoinStanding([AI_Player(tt), Human_Player()])
(space)game.play()

 

 

Tic Tac Toe 게임 — 일종의 삼목 게임

 

# This is a variant of the Tic Tac Toe recipe given in the easyAI library

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

class GameController(TwoPlayersGame):
(space)def __init__(self, players):

# Define the players
(space)(space)self.players = players

# Define who starts the game
(space)(space)self.nplayer = 1

# Define the board
(space)(space)self.board = [0] * 9

# Define possible moves
(space)def possible_moves(self):
(space)(space)return [a + 1 for a, b in enumerate(self.board) if b == 0]

# Make a move
(space)def make_move(self, move):
(space)(space)self.board[int(move) – 1] = self.nplayer

# Does the opponent have three in a line?
(space)def loss_condition(self):
(space)(space)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]]

(space)(space)return any([all([(self.board[i-1] == self.nopponent)
(space)(space)(space)for i in combination]) for combination in possible_combinations])

# Check if the game is over
(space)def is_over(self):
(space)(space)return (self.possible_moves() == []) or self.loss_condition()

# Show current position
(space)def show(self):
(space)(space)print(‘\n’+’\n’.join([‘ ‘.join([[‘.’, ‘O’, ‘X’][self.board[3*j + i]]
(space)(space)(space)for i in range(3)]) for j in range(3)]))

# Compute the score
(space)def scoring(self):
(space)(space)return -100 if self.loss_condition() else 0

if __name__ == “__main__”:

# Define the algorithm
(space)algorithm = Negamax(7)

# Start the game
(space)GameController([Human_Player(), AI_Player(algorithm)]).play()

 

 

 

 

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *