Compare commits

..

No commits in common. "70c8ff13087ad131adff0391fd5e7f80a0773947" and "9500a68ae790102bb945e0ec078e664c0bba06d2" have entirely different histories.

8 changed files with 12 additions and 63 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,49 +0,0 @@
class NimGame:
"""A Nim game class based off NimGameStub.
"""
def get_initial_state(self):
''' Constructs the game board and has player 1 start the game. '''
return {
"board": [1, 3, 5, 7],
"first_player": True
}
def get_next_state(self, state, action):
''' Creates a copy of the current board and then makes adjustments
based on the chosen action from a player's turn. '''
new_board = state["board"].copy()
new_board[action[0]] = new_board[action[0]] - action[1]
return {
"board": new_board,
"first_player": not state["first_player"],
}
def get_actions(self, state):
''' Construct the list of actions that can be taken by a player.
Is there a more efficient way of doign this? '''
actions = []
new_board = state["board"].copy()
for i in range(4): # check the four rows of the board
if new_board[i] > 0: # if a row is not empty
for j in range(1,4): # check if you can remove 1, 2, or 3 ticks in the row
if j <= new_board[i]: # if so
actions.append((i,j)) # include that as an option
return actions
def get_reward(self, state):
''' Reports who wins. '''
if state["first_player"]:
return 1 # If it is the player's turn and there is nothing on the board, then it was the robot who crossed off the last line.
return 0 # Otherwise, the first player made the last move and lost.
def is_over(self, state):
''' Reports true if there are no more lines to cross. '''
if all(ticks == 0 for ticks in state["board"]):
return True
return False
def get_objective(self, state):
''' Reports the desired obejctive to help choose the move that
yields the optimal reward under the lookahead_strategy for
computer players. '''
return max if state["first_player"] else min

View File

@ -1,16 +1,16 @@
from nim.game import NimGame from nim.game_stub import NimGameStub
from strategy.lookahead_strategy import LookaheadStrategy from strategy.lookahead_strategy import LookaheadStrategy
class HumanNimPlayer: class HumanNimPlayer:
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.game = NimGame() self.game = NimGameStub()
def choose_action(self, state): def choose_action(self, state):
actions = self.game.get_actions(state) actions = self.game.get_actions(state)
for i, action in enumerate(actions): for i, action in enumerate(actions):
row, lines_to_remove = action row, lines_to_remove = action
print(f"{i}. Remove {lines_to_remove} from row {row+1}.") print(f"{i}. Remove {lines_to_remove} from row {row}.")
choice = self.get_int(len(actions)) choice = self.get_int(len(actions))
return actions[choice] return actions[choice]
@ -26,10 +26,10 @@ class HumanNimPlayer:
class ComputerNimPlayer: class ComputerNimPlayer:
def __init__(self, name): def __init__(self, name):
self.name = name self.name = name
self.strategy = LookaheadStrategy(NimGame(), max_depth=5, deterministic=False) self.strategy = LookaheadStrategy(NimGameStub(), max_depth=3, deterministic=False)
def choose_action(self, state): def choose_action(self, state):
action = self.strategy.choose_action(state) action = self.strategy.choose_action(state)
row, lines_to_remove = action row, lines_to_remove = action
print(f"{self.name} removes {lines_to_remove} from row {row+1}") print(f"{self.name} removes {lines_to_remove} from row {row}")
return action return action

View File

@ -1,9 +1,9 @@
from nim.game import NimGame from nim.game_stub import NimGameStub
class NimView: class NimView:
def __init__(self, player0, player1): def __init__(self, player0, player1):
self.players = [player0, player1] self.players = [player0, player1]
self.game = NimGame() self.game = NimGameStub()
def greet(self): def greet(self):
print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.") print(f"{self.players[0].name} and {self.players[1].name}, welcome to Nim.")

View File

@ -37,5 +37,4 @@ For the first one, I would put an X on the rightmost cell in the middle row. Thi
You can get the inital game state using game.get_initial_state(). You can get the inital game state using game.get_initial_state().
What is the current and future reward for this state? What does this mean? What is the current and future reward for this state? What does this mean?
The current and future reward for this state is 1. This means the state favors player X assuming both players continue using the same look ahead strategy.

View File

@ -1,11 +1,11 @@
from nim.game import NimGame from nim.game_stub import NimGameStub
from nim.view import NimView from nim.view import NimView
from nim.player import HumanNimPlayer, ComputerNimPlayer from nim.player import HumanNimPlayer, ComputerNimPlayer
player0 = HumanNimPlayer(input("What's your name? ")) player0 = HumanNimPlayer(input("What's your name? "))
player1 = ComputerNimPlayer("Robot") player1 = ComputerNimPlayer("Robot")
view = NimView(player0, player1) view = NimView(player0, player1)
game = NimGame() game = NimGameStub()
view.greet() view.greet()
state = game.get_initial_state() state = game.get_initial_state()

View File

@ -2,8 +2,8 @@ from ttt.game import TTTGame
from ttt.view import TTTView from ttt.view import TTTView
from ttt.player import TTTHumanPlayer, TTTComputerPlayer from ttt.player import TTTHumanPlayer, TTTComputerPlayer
player0 = TTTHumanPlayer("Pleayer 1") player0 = TTTHumanPlayer("Player 1")
player1 = TTTComputerPlayer("Robot 1") player1 = TTTHumanPlayer("Player 2")
game = TTTGame() game = TTTGame()
view = TTTView(player0, player1) view = TTTView(player0, player1)

View File

@ -1,5 +1,4 @@
from click import Choice, prompt from click import Choice, prompt
from strategy.lookahead_strategy import LookaheadStrategy
from strategy.random_strategy import RandomStrategy from strategy.random_strategy import RandomStrategy
from ttt.game import TTTGame from ttt.game import TTTGame
import random import random
@ -25,7 +24,7 @@ class TTTComputerPlayer:
def __init__(self, name): def __init__(self, name):
"Sets up the player." "Sets up the player."
self.name = name self.name = name
self.strategy = LookaheadStrategy(TTTGame(),deterministic=False) self.strategy = RandomStrategy(TTTGame())
def choose_action(self, state): def choose_action(self, state):
"Chooses a random move from the moves available." "Chooses a random move from the moves available."