/// <summary> /// Indicates if the player has won /// </summary> /// <param name="player">The player</param> private bool HasWon(BoardTileType player) { int pattern = 0; for (int y = 0; y < this.Size; ++y) { for (int x = 0; x < this.size; ++x) { if (this.GetTile(x, y) == player) { pattern |= (1 << (y * this.size + x)); } } } foreach (int winningPattern in winningPatterns) { if ((pattern & winningPattern) == winningPattern) { return(true); } } return(false); }
/// <summary> /// Makes a move for the given player in the given board /// </summary> /// <param name="board">The board</param> /// <param name="player">The player</param> public BoardMove MakeMove(Board board, BoardTileType player) { var move = new BoardMove(-1, -1); this.Minimax(player, board, player, out move); return(move); }
/// <summary> /// Creates a new board tile position /// </summary> /// <param name="x">The x position</param> /// <param name="y">The y position</param> /// <param name="tileType">The type of the tile</param> public BoardTile(int x, int y, BoardTileType tileType) : this() { this.X = x; this.Y = y; this.TileType = tileType; }
/// <summary> /// Creates a new board agent /// </summary> /// <param name="placementTile">The placement tile</param> public BoardAgent(BoardTileType placementTile) { if (placementTile == BoardTileType.Empty) { throw new ArgumentException("The placement tile cannot be empty."); } this.placementTile = placementTile; }
/// <summary> /// Makes the given move /// </summary> /// <param name="x">The x position</param> /// <param name="y">The y position</param> /// <param name="tile">The tile to place</param> /// <returns>True if moved else false</returns> public bool Move(int x, int y, BoardTileType tile) { if (this.GetTile(x, y) == BoardTileType.Empty) { this.SetTile(x, y, tile); return(true); } return(false); }
/// <summary> /// Returns the next player /// </summary> /// <param name="player">The current player</param> private BoardTileType NextPlayer(BoardTileType player) { if (player == BoardTileType.Cross) { return(BoardTileType.Circle); } else { return(BoardTileType.Cross); } }
/// <summary> /// Returns a new board with the given move /// </summary> /// <param name="x">The x position</param> /// <param name="y">The y position</param> /// <param name="tile">The tile to place</param> /// <returns>A new board with the move</returns> public Board WithMove(int x, int y, BoardTileType tile) { var newBoard = new Board(this.Size); newBoard.tiles = this.tiles; if (newBoard.GetTile(x, y) == BoardTileType.Empty) { newBoard.SetTile(x, y, tile); } return(newBoard); }
private void populateBoardWithRandomValues() { Random random = new Random(); for (int i = 0; i < board.boardWidth; i++) { for (int j = 0; j < board.boardHeight; j++) { int randomValue = random.Next(0, 2); BoardTileType tile = randomValue == 0 ? BoardTileType.safe : BoardTileType.mine; setBoardTileValueAtPosition(tile, new BoardPosition(i, j)); } } }
/// <summary> /// Returns the winner for the given tile /// </summary> /// <param name="tile">The tile</param> private GameWinner?WinnerForTile(BoardTileType tile) { switch (tile) { case BoardTileType.Cross: return(GameWinner.Cross); case BoardTileType.Circle: return(GameWinner.Circle); default: return(null); } }
/// <summary> /// Places the given tile at the given position /// </summary> /// <param name="x">The x position</param> /// <param name="y">The y position</param> /// <param name="tile">The tile to place</param> /// <returns>True if placed else false</returns> public bool Place(int x, int y, BoardTileType tile) { if (this.board.Move(x, y, tile)) { foreach (var agent in this.Agents) { agent.State = new BoardState(this.board); } return(true); } return(false); }
/// <summary> /// Indicates if the given tile and winner are equal /// </summary> /// <param name="tile">The tile</param> /// <param name="winner">The winner</param> public static bool AreEqual(BoardTileType tile, GameWinner winner) { if (tile == BoardTileType.Circle && winner == GameWinner.Circle) { return(true); } else if (tile == BoardTileType.Cross && winner == GameWinner.Cross) { return(true); } else { return(false); } }
/// <summary> /// Calculates the score /// </summary> /// <param name="player">The player</param> /// <param name="winner">The winner</param> private int Score(BoardTileType player, GameWinner winner) { if (winner == GameWinner.Tie) { return(0); } else if (Board.AreEqual(player, winner)) { return(10); } else { return(-10); } }
/// <summary> /// Calculates the best move for the given player /// </summary> /// <param name="player">The player</param> /// <param name="board">The board</param> /// <param name="turnPlayer">The current player in the turn</param> /// <param name="move">The move to make</param> /// <returns>The score</returns> private int Minimax(BoardTileType player, Board board, BoardTileType turnPlayer, out BoardMove move) { var winner = board.GetWinner(); if (winner != null) { move = new BoardMove(-1, -1); return(this.Score(player, winner.Value)); } var scores = new List <int>(); var moves = new List <BoardMove>(); //Go through all possible games from the current board foreach (var possibleMove in board.GetMoves()) { var possibleBoard = board.WithMove(possibleMove.X, possibleMove.Y, turnPlayer); scores.Add(this.Minimax(player, possibleBoard, this.NextPlayer(turnPlayer), out move)); moves.Add(possibleMove); } if (player == turnPlayer) { //Max calculation var maxIndex = FindBestIndex(scores, (x, y) => x > y); move = moves[maxIndex]; return(scores[maxIndex]); } else { //Min calculation var minIndex = FindBestIndex(scores, (x, y) => x < y); move = moves[minIndex]; return(scores[minIndex]); } }
/// <summary> /// Sets the given tile /// </summary> /// <param name="x">The x position</param> /// <param name="y">The y position</param> /// <param name="tile">The tile</param> public void SetTile(int x, int y, BoardTileType tile) { this.tiles |= (int)tile << (x + y * this.size) * 2; }
/// <summary> /// Makes a move for the given player in the given board /// </summary> /// <param name="board">The board</param> /// <param name="player">The player</param> public BoardMove MakeMove(Board board, BoardTileType player) { return(board.RandomEmptyPosition(this.random) ?? new BoardMove(0, 0)); }
public Tile(int xPos, int yPos, BoardTileType tileType) { Position = new Vector2Int(xPos, yPos); TileType = tileType; Neighbors = new List <Tile>(); }
public BoardTile(BoardTileType type, bool upPath, bool rightPath) { this.upPath = upPath; this.rightPath = rightPath; this.type = type; }
/// <summary> /// Advances the turn to the next player /// </summary> private void Next() { this.turnPlayer = this.NextPlayer(this.turnPlayer); }
public void setBoardTileValueAtPosition(BoardTileType tile, BoardPosition boardPosition) { board.setTileForPosition(boardPosition, tile); }
public void setTileForPosition(BoardPosition position, BoardTileType tileType) { board[position.x, position.y] = tileType; }