public void OnePiece(BoardClone board) { bool fill = true; for (int i = 0; i < 6; i++) { if (WinPositions[i].state == (Tile.TileState)PlayerNumber) { fill = false; } } if (!fill) { foreach (var piece in WinPositions) { bool inGoal = false; for (int i = 0; i < PlayerPieces.Count; i++) { if (piece.column == PlayerPieces[i].column && piece.row == PlayerPieces[i].row) { inGoal = true; } } if (!inGoal) { GoalPos = new Vector2Int(piece.row, piece.column); } } } }
void PlayAI() { int playerIndex = playerList.IndexOf(currentPlayer); if (playerIndex >= playerList.Count) { playerIndex = 0; } IPlayer player = playerList[playerIndex]; int otherPlayerIndex = playerList.IndexOf(currentPlayer) - 1; if (playerList.IndexOf(currentPlayer) - 1 < 0) { otherPlayerIndex = playerList.Count; } IPlayer otherPlayer = playerList[otherPlayerIndex]; BoardClone newBoard = (BoardClone)MiniMax.Select(new BoardClone(EnumMatris(), this), player, otherPlayer, playerList[playerIndex].Depth, true); previousBoards.Add(newBoard); previous = newBoard; if (newBoard.OneLeft(player)) { playerList[playerIndex].OnePiece(newBoard); } Tile prev = null; Tile nex = null; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (matris[i, j] != null) { if (matris[i, j].state != newBoard.board[i, j]) { if (matris[i, j].state == Tile.TileState.open) { nex = matris[i, j]; } if (matris[i, j].state == (Tile.TileState)((Player)player).PlayerNumber) { prev = matris[i, j]; } matris[i, j].SetState((int)newBoard.board[i, j]); } } } } if (prev != null && nex != null) { ChangeList(prev, nex, ((Player)player).PlayerNumber); } StartCoroutine(timer(speed)); }
public List <IState> Expand(IPlayer player, IPlayer otherPlayer) //Clones the board for the AI to use, one clone for each individual move the AI can make { List <IState> output = new List <IState>(); foreach (var piece in ((Player)player).PlayerPieces) { boardController.CheckForValidMoves(piece, false, board); // check on current board foreach (var validMove in boardController.GetValidMovesList()) { //clones the board for each possible move and then makes the move on the clone-board BoardClone newBoard = new BoardClone((Tile.TileState[, ])board.Clone(), boardController); newBoard.SetTile(piece.row, piece.column, Tile.TileState.open); newBoard.SetTile(validMove.x, validMove.y, (Tile.TileState)((Player)player).PlayerNumber); output.Add(newBoard); } boardController.ResetSelectedBall(); } return(output); }
bool SameBoard(BoardClone boardOne, BoardClone boardTwo, IPlayer player) { if (boardOne == null || boardTwo == null) { return(false); } for (int i = 0; i < MAXROW; i++) { for (int j = 0; j < MAXCOL; j++) { if (boardOne.GetTileState(i, j) == ((Player)player).PlayerNumber)// && != boardTwo.GetTileState(i, j)) { if (boardTwo.GetTileState(i, j) != ((Player)player).PlayerNumber) { return(false); } } } } return(true); }
void ActivatePlayerAI() { IPlayer player = playerList[0]; IPlayer otherPlayer = playerList[1]; BoardClone newBoard = (BoardClone)MiniMax.Select(new BoardClone(EnumMatris(), this), player, otherPlayer, 1, true); Tile prev = null; Tile nex = null; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { if (matris[i, j] != null) { if (matris[i, j].state != newBoard.board[i, j]) { if (matris[i, j].state == Tile.TileState.open) { nex = matris[i, j]; } if (matris[i, j].state == (Tile.TileState)((Player)player).PlayerNumber) { prev = matris[i, j]; } matris[i, j].SetState((int)newBoard.board[i, j]); } } } } if (prev != null && nex != null) { ChangeList(prev, nex, ((Player)player).PlayerNumber); } StartCoroutine(timer(speed)); }