def set_board(self, player_num): if player_num == 1: self.board = Board(Square(20, 100), Square(380, 460)) elif player_num == 2: self.board = Board(Square(460, 100), Square(820, 460)) self.board.squares_not_shot_list() self.board.available_squares_dict(self)
def __init__(self, start_board: Board = None, seed: int = random.randint(0, 999999)): if (start_board == None): self._board = Board(None, 1, GamePhase.PLACEMENT) else: self._board = start_board self._node = self._init_node random.seed(seed)
def setup_all(self): player = Player() self.setup_player(player) map_game = Board() self.map_game = map_game client = Client() self.client = client
def __init__(self): # current player step self.curPlayer = 'X' self.isPause = False self.isDraw = False self.winner = None self.msg = '' self.board = Board()
def __init__(self): # Setup players pl1 = Player(1, "Carl", "Red", self) pl2 = Player(2, "Naish", "Blue", self) pl3 = Player(3, "Mark", "Yellow", self) pl4 = Player(4, "Greeny", "Green", self) SetupGame.players = [pl1, pl2, pl3, pl4] # Setup Game Board SetupGame.mainBoard = Board(12, 10) # Select next player SetupGame.currentPlayer = math.ceil( (random() * len(SetupGame.players))) SetupGame.nextPlayer = (SetupGame.currentPlayer + 1 - 1) % len( SetupGame.players) + 1 # Set starting season season = math.ceil(random() * 4)
def __init__(self, color: str, parameters: List[float]): """ TODO This method is called by the referee once at the beginning of the game to initialise your player. You should use this opportunity to set up your own internal representation of the board, and any other state you would like to maintain for the duration of the game. The input parameter colour is a string representing the piece colour your program will control for this game. It can take one of only two values: the string 'white' (if you are the White player for this game) or the string 'black' (if you are the Black player for this game). """ self.parameters = parameters self._board = Board(None, 0, GamePhase.PLACEMENT) if (color.lower() == "white"): self._color = PlayerColor.WHITE else: self._color = PlayerColor.BLACK random.seed(Player._SEED)
import pygame import sys import time from pygame.locals import * from Classes.Board import Board from globals import * pygame.init() #variables pyTime = pygame.time.Clock() initializeTime = time.time() board = Board() sc = pygame.display.set_mode((W, H)) #functions def onKeyDown(event): #player 1 if (event.key == K_DOWN): board.player1.changeDir(False) if (event.key == K_UP): board.player1.changeDir(True) #player 2 if (event.key == K_w): board.player2.changeDir(True)
from Classes.Die import Die from Classes.Board import Board #initialize all dice in game redDie = Die() yellowDie = Die() greenDie = Die() blueDie = Die() whiteDie1 = Die() whiteDie2 = Die() board1 = Board() print(board1.board) board1.checkBox('red', 6) board1.checkBox('red', 7) board1.checkBox('red', 8) board1.checkBox('red', 9) board1.checkBox('red', 12) print(board1.board) print(board1.canLockRow(0)) print(board1.scoreBoard())
class MCTSAgent(): """ Contains the main driver functions used for this AI. Contains functions that, together, implement the Monte Carlo Tree Search algorithm. """ _EXPLORATION_MULTIPLIER: float = sqrt(2) # A reference to the root node in the tree that's being searched by MCTS. tree_root: Node _board: Board _init_board: Board = Board(None, 1, GamePhase.PLACEMENT) def __init__(self, tree_root: Node, start_board: Board = _init_board, seed: int = None): self.tree_root = tree_root self._board = start_board if (seed is not None): random.seed(seed) def train(self, duration_seconds: int): end_time: float = time.time() + duration_seconds while (time.time() < end_time): print(self.tree_root.wins, self.tree_root.num_simulations) # TODO Remove self._simulate() self._board = self._init_board # break # TODO Remove print(self.tree_root.wins, self.tree_root.num_simulations) # TODO Remove def _select(self, node: Node, total_num_simulations: int) -> Node: scores: List[Tuple[Node, float]] = [] unexplored_nodes_score: float = Utils.UCB1( 1, 2, total_num_simulations, MCTSAgent._EXPLORATION_MULTIPLIER) # A list of all deltas which have already been explored at least once. Therefore, they are nodes. children: List[Node] = node.children # A list of all valid deltas from the given board. deltas: List[Delta] = self._board.get_all_possible_deltas( Utils.get_player(self._board.round_num)) if (len(children) > 0): for child in children: # Since some deltas have already been explored and are therefore included in 'children', remove them # from 'deltas' so that it only contains unexplored moves. deltas.remove(child.delta) scores.append((child, Utils.UCB1(child.wins, child.num_simulations, total_num_simulations, MCTSAgent._EXPLORATION_MULTIPLIER))) # Since there are no unexplored options available, we'll set its score to -1 such that the algorithm won't # attempt to choose an unexplored option (since there are none). if len(deltas) == 0: unexplored_nodes_score = -1 # Order by highest scoring nodes. scores = sorted(scores, key=lambda x: x[1], reverse=True) for child, score in scores: if (score > unexplored_nodes_score): # This is to avoid re-exploring a leaf node that resulted in a win or loss. We want to explore new # options. Otherwise we'd have wasted this simulation or back-propagated the same result twice. if (self._board.get_next_board( child.delta).phase == GamePhase.FINISHED): continue else: return child else: # We've now reached a (node : score) pair that has a lower score than all the unexplored moves. # Therefore, stop iterating through existing nodes so we can instead select an unexplored move. break random_delta: Delta = random.choice(deltas) new_child_node: Node = Node(node, random_delta) node.children.append(new_child_node) return new_child_node def _simulate(self): leaf: Node = self._select(self.tree_root, self.tree_root.num_simulations) self._board = self._board.get_next_board(leaf.delta) while (self._board.phase != GamePhase.FINISHED): leaf = self._select(leaf, self.tree_root.num_simulations) self._board = self._board.get_next_board(leaf.delta) if (self._board != 1): # TODO Remove this selection = "({}, {}) -> NODE" if leaf.num_simulations > 2 else "({}, {}) -> EXPLORE" print("{:3}: {} : {}".format( self._board.round_num - 1, leaf.delta.player, selection.format(leaf.wins, leaf.num_simulations))) if (leaf.delta.move_origin is not None): print("{} -> ".format(leaf.delta.move_origin.pos), end="") print("{}".format(leaf.delta.move_target.pos)) print(self._board) print("") self._back_propagate(leaf, self._board.winner) def _back_propagate(self, node: Node, winner: PlayerColor): """ TODO Can be done recursively, but no point in using a stack. Iteratively is more memory efficient. :param node: :param winner: :return: """ while (node is not None): node.num_simulations += 1 if ((node.parent is None and node.children[0].delta.player == winner) or (node.delta is not None and node.delta.player == winner)): node.wins += 1 elif (winner is None): # Must have been a tie. node.wins += 0.5 # TODO Remove this (temp for printing/debugging) # if (node.board.round_num != 1): # print("{:3}: {}: ".format(node.board.round_num - 1, node.delta.player), end="") # if (node.delta.move_origin is not None): # print("{} -> ".format(node.delta.move_origin.pos), end="") # print("{}".format(node.delta.move_target.pos)) # # print(node.board) # print("") node = node.parent