Esempio n. 1
0
        public void MakeMove()
        {
            FieldCell bestMove = new FieldCell(0, 0);

            MinMax(ref bestMove, player, null);
            MonoHost.ScheduleCoroutine(AnnounceMove(bestMove));
        }
Esempio n. 2
0
 public FieldState(FieldState other)
 {
     for (int i = 0; i < cells.GetLength(0); ++i)
     {
         for (int j = 0; j < cells.GetLength(1); ++j)
         {
             cells[i, j] = new FieldCell(other.cells[i, j]);
         }
     }
 }
Esempio n. 3
0
 public FieldState()
 {
     for (int x = 0; x < Dimension; ++x)
     {
         for (int y = 0; y < Dimension; ++y)
         {
             cells[x, y] = new FieldCell(x, y);
         }
     }
 }
Esempio n. 4
0
        private bool FindWinnerColumn(FieldCell lastCell)
        {
            Player lastPlayer = Player.None;

            for (int x = 0; x < cells.GetLength(0); ++x)
            {
                FieldCell current = cells[x, lastCell.Column];
                if (current.Free || ((x != 0) && (current.OwnedBy != lastPlayer)))
                {
                    return(false);
                }
                lastPlayer = current.OwnedBy;
            }
            return(true);
        }
Esempio n. 5
0
        private bool FindWinnerRow(FieldCell lastCell)
        {
            Player lastPlayer = Player.None;

            for (int y = 0; y < cells.GetLength(0); ++y)
            {
                FieldCell current = cells[lastCell.Row, y];
                if (current.Free || ((y != 0) && (current.OwnedBy != lastPlayer)))
                {
                    return(false);
                }
                lastPlayer = current.OwnedBy;
            }
            return(true);
        }
Esempio n. 6
0
 private void OnCellSelected(FieldCell cell)
 {
     cell.OwnedBy = currentPlayer;
     Debug.Log(Field);
     fieldVisualizer.UpdateCell(cell.Row, cell.Column, currentPlayer);
     if (Field.FindWinner(cell))
     {
         state = MatchState.Won;
     }
     else if (Field.FindFreeCells().Count == 0)
     {
         state = MatchState.Tie;
     }
     moveFinished = true;
 }
Esempio n. 7
0
 private bool FindWinnerAntidiagonal(FieldCell lastCell)
 {
     if (lastCell.Row == cells.GetLength(0) - 1 - lastCell.Column)
     {
         Player lastPlayer = Player.None;
         for (int y = 0; y < cells.GetLength(0); ++y)
         {
             FieldCell current = cells[y, cells.GetLength(0) - 1 - y];
             if (current.Free || ((y != 0) && (current.OwnedBy != lastPlayer)))
             {
                 return(false);
             }
             lastPlayer = current.OwnedBy;
         }
         return(true);
     }
     return(false);
 }
Esempio n. 8
0
 public bool FindWinner(FieldCell lastCell)
 {
     return(FindWinnerColumn(lastCell) || FindWinnerRow(lastCell) ||
            FindWinnerDiagonal(lastCell) || FindWinnerAntidiagonal(lastCell));
 }
Esempio n. 9
0
        private int MinMax(ref FieldCell bestMove, Player currentPlayer, FieldCell cell, int depth = 0, int alpha = int.MinValue, int beta = int.MaxValue)
        {
            List <FieldCell> freeCells = field.FindFreeCells();

            if ((cell != null) && field.FindWinner(cell))
            {
                if (cell.OwnedBy == player)
                {
                    return(10 - depth);
                }
                else
                {
                    return(depth - 10);
                }
            }
            if (freeCells.Count == 0)
            {
                return(0);
            }

            if (currentPlayer == player)
            {
                int score = int.MinValue;
                foreach (FieldCell freeCell in freeCells)
                {
                    freeCell.OwnedBy = currentPlayer;
                    if (currentPlayer == player)
                    {
                        int minmax = MinMax(ref bestMove, player == Player.Player1 ? Player.Player2 : Player.Player1, freeCell, depth + 1, alpha, beta);
                        freeCell.OwnedBy = Player.None;
                        if (minmax > score)
                        {
                            score = minmax;
                            if (depth == 0)
                            {
                                bestMove = freeCell;
                            }
                        }
                        alpha = Mathf.Max(alpha, score);
                        if (beta <= alpha)
                        {
                            break;
                        }
                    }
                }
                return(score);
            }
            else
            {
                int score = int.MaxValue;
                foreach (FieldCell freeCell in freeCells)
                {
                    freeCell.OwnedBy = currentPlayer;
                    score            = Mathf.Min(score, MinMax(ref bestMove, player, freeCell, depth + 1, alpha, beta));
                    freeCell.OwnedBy = Player.None;
                    beta             = Mathf.Min(beta, score);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(score);
            }
        }
Esempio n. 10
0
        private IEnumerator AnnounceMove(FieldCell move)
        {
            yield return(new WaitForSeconds(0.3f));

            finishCallback(move);
        }
Esempio n. 11
0
 public FieldCell(FieldCell other) : this(other.Row, other.Column, other.OwnedBy)
 {
 }