public GameTurn(Action action, Board board, Player player, int roundNo) { OnTurnEnded = action; Board = board; Map = new UnityBoardMap (board); TurnPlayer = player; RoundNo = roundNo; }
/// <summary> /// Recreates the game board and state in preparation of a new game /// </summary> public void Reset() { Board = Board.Create (); round = 0; roundStarter = Player.P1; Messenger.Invoke ("game.begin"); newTurn (roundStarter); }
public void SetupBoardForTests() { board = Board.Create (); board.PlacePiece (Player.P1, PieceType.ROUND, board.Squares [4, 1]); board.PlacePiece (Player.P1, PieceType.ROUND, board.Squares [4, 2]); board.PlacePiece (Player.P1, PieceType.SQUARE, board.Squares [4, 3]); board.PlacePiece (Player.P2, PieceType.ROUND, board.Squares [5, 2]); board.PlacePiece (Player.P2, PieceType.SQUARE, board.Squares [5, 3]); board.PlacePiece (Player.P2, PieceType.ROUND, board.Squares [5, 4]); }
public static Board AIClone(Board board) { Board newBoard = Pool.Acquire (); ReclaimPooledBoard (newBoard); foreach (Piece existingPiece in board.ActualPieces) { Coords existingPos = existingPiece.Occupies.Pos; Piece pieceToPlace = Piece.AcquirePooledPiece (existingPiece.Type); pieceToPlace.Reclaim (newBoard, existingPiece.Owner); pieceToPlace.Place (newBoard.Squares [existingPos.x, existingPos.y]); newBoard.ActualPieces.Add (pieceToPlace); } if (board.TheAnchor.SitsAtop != null) { Coords aPos = board.TheAnchor.SitsAtop.Occupies.Pos; newBoard.TheAnchor .MoveAnchor (newBoard.Pieces [newBoard.Squares [aPos.x, aPos.y]]); } return newBoard; }
public void ClearBoard() { if (board == null) { return; } Board.Pool.Release (board); board = null; }
private void RecordNodeInDB(Board board, float p) { byte[] dbrec = BitConverter.GetBytes (p); foreach (string guid in board.AllGUIDs()) { DB.Set (guid, dbrec); } dbrec = BitConverter.GetBytes (-p); foreach (string guid in board.AllGUIDs(true)) { DB.Set (guid, dbrec); } }
List<ActionChain> PlayOneTurn(Player p, Board root) { List<ActionChain> allActions = new List<ActionChain> (3000); List<ActionChain> moveActions = new List<ActionChain> (1500); foreach (ActionChain firstMove in DoMovement(p, root)) { if (firstMove.LastMoveSkipped ()) { moveActions.Add (firstMove); continue; } if (IsStupidMove (firstMove)) { firstMove.ClearBoard (); continue; } foreach (ActionChain secondMove in DoMovement(p, firstMove.Board)) { secondMove.Link (firstMove); if (secondMove.actions.Last ().fromLoc.Equals (firstMove.actions.Last ().toLoc)) { secondMove.ClearBoard (); continue; } if (IsStupidMove (secondMove)) { secondMove.ClearBoard (); continue; } moveActions.Add (secondMove); } } foreach (ActionChain moveSet in moveActions) { foreach (ActionChain pushSet in DoPushes(p, moveSet.Board)) { pushSet.Link (moveSet); if (pushSet.actions.Last().action == ActionTaken.SKIPPED) pushSet.Board.NotifyWinner(p.Other()); allActions.Add (pushSet); } } return allActions; }
void PerformPush(Board moveBoard, ActionPair move) { moveBoard.Pieces [moveBoard.Squares [move.fromLoc.x, move.fromLoc.y]] .Push.Push (moveBoard.Squares [move.toLoc.x, move.toLoc.y]); }
List<ActionChain> DoPushes(Player p, Board board) { return DoAction (p, board, AllPushes, PerformPush); }
List<ActionChain> DoMovement(Player p, Board board) { return DoAction (p, board, AllMoves, PerformMove); }
List<ActionChain> DoAction(Player p, Board board, Func<List<Piece>,IEnumerable<ActionPair>> checkActionsFn, Action<Board,ActionPair> actionFn) { List<ActionChain> actions = new List<ActionChain> (4096); List<Piece> myPieces = new List<Piece> (); foreach (Piece pc in board.Pieces.Values) { if (pc.Owner == p) { myPieces.Add (pc); } } foreach (ActionPair move in checkActionsFn(myPieces)) { ActionChain chain = new ActionChain (); chain.actions.Add (move); Board moveBoard = Board.AIClone (board); actionFn (moveBoard, move); chain.Board = moveBoard; actions.Add (chain); } ActionChain skipChain = new ActionChain (); skipChain.actions.Add (new ActionPair () {action = ActionTaken.SKIPPED}); skipChain.Board = Board.AIClone (board); actions.Add (skipChain); return actions; }
static float ScoreBoard(Player p, Board brd) { if (brd.Winner == p) { return IS_WIN; } else if (brd.Winner == p.Other ()) { return IS_LOSS; } List<Piece> myPieces = new List<Piece> (); List<Piece> theirPieces = new List<Piece> (); foreach (Piece pc in brd.Pieces.Values) { if (pc.Owner == p) { myPieces.Add (pc); } else { theirPieces.Add (pc); } } float score = 0; myPieces.ForEach (piece => score += CalculateValue (piece)); theirPieces.ForEach (piece => score -= CalculateValue (piece)); return score; }
public UnityBoardMap(Board board) { Board = board; }
private static void ReclaimPooledBoard(Board newBoard) { newBoard.ActualPieces.ForEach (piece => Piece.ReturnPooledPiece (piece)); newBoard.ActualPieces.Clear (); newBoard.TheAnchor.Reset (); newBoard.NotifyDirty (); newBoard.Winner = null; }
private static Board CreateFromData(string[] boardLines) { Board board = new Board (); BoardSquare[,] squares = new BoardSquare[boardLines [0].Length, boardLines.Length]; for (int y = 0; y < boardLines.Length; y++) { for (int x = 0; x < boardLines[y].Length; x++) { char incoming = boardLines [y] [x]; if (incoming.Equals ('_')) { squares [x, y] = new BoardSquare (board, BoardSquareType.EDGE, new Coords (){x = x, y = y}); } else if (incoming.Equals ('=')) { squares [x, y] = new BoardSquare (board, BoardSquareType.RAIL, new Coords (){x = x, y = y}); } else if (incoming.Equals ('#')) { squares [x, y] = new BoardSquare (board, BoardSquareType.NORMAL, new Coords (){x = x, y = y}); } else { throw new NotSupportedException ("Board Map includes tile that was not recognised."); } } } board.SetSquares (squares, (square) => { if (square.Pos.x < squares.GetLength (0) / 2) { return Player.P1; } else { return Player.P2; } } ); return board; }
public static void SetupBoardPool(Board template) { if (Pool != null) { return; } Pool = new Pooling.Pool<Board> (AMT_BOARDS_NEEDED_PER_SEARCH * Environment.ProcessorCount, pool => { Board newBoard = new Board (); BoardSquare[,] squares = new BoardSquare[template.Width, template.Height]; for (int y = 0; y < template.Height; y++) { for (int x = 0; x < template.Width; x++) { squares [x, y] = new BoardSquare (newBoard, template.Squares [x, y].Type, new Coords () { x = x, y = y }); } } newBoard.SetSquares (squares, template.TerritoryOf); return newBoard; }, Pooling.LoadingMode.Eager, Pooling.AccessMode.FIFO); }