Skip to content

Commit

Permalink
Merge pull request #3 from smessie/random-agent
Browse files Browse the repository at this point in the history
Implement a RandomAgent
  • Loading branch information
smessie authored Nov 10, 2020
2 parents 955e250 + e368735 commit 1dfb843
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
29 changes: 29 additions & 0 deletions agents/random_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import random

from game.agent import Agent
from game.table import Table
from game.player import Player
from typing import List, TYPE_CHECKING

if TYPE_CHECKING:
from game.card import Card


class RandomAgent(Agent):
def __init__(self):
super().__init__(Player())

def make_move(self, table: Table) -> None:
"""
Let the agent make a random move from all possible moves in his state.
"""
possible_moves: List[List[Card]] = self.player.get_all_possible_moves(table)
table.try_move(self, random.choice(possible_moves))

def get_preferred_card_order(self, table: Table) -> List[Card]:
"""
Returns the preferred cards to exchange in the beginning of a round in random order.
"""
possible_cards = list(set(table.deck.card_stack) - set(self.player.hand))
random.shuffle(possible_cards)
return possible_cards
4 changes: 0 additions & 4 deletions game/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ def make_move(self, table: Table) -> None:
"""
pass

def receive_event(self, move, event) -> None:
pass

def get_preferred_card_order(self, table: Table) -> List[Card]:
"""
Function used by President to exchange cards at the beginning of a round. Most wanted card should be in front.
"""
return sorted(table.deck.card_stack, reverse=True)
4 changes: 2 additions & 2 deletions game/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def __init__(self):
self.player_id: int = Player._player_id
Player._player_id += 1

def get_all_possible_moves(self, table: Table) -> [[Card]]:
possible_moves = []
def get_all_possible_moves(self, table: Table) -> List[List[Card]]:
possible_moves = [[]]
for amount_of_cards in range(len(table.last_move()[0]) if table.last_move() else 1, len(self.hand) + 1):
for potential_move in combinations(self.hand, amount_of_cards):
if table.game.valid_move(list(potential_move)):
Expand Down

0 comments on commit 1dfb843

Please sign in to comment.