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

            MinMax(ref bestMove, player, null);
            MonoHost.ScheduleCoroutine(AnnounceMove(bestMove));
        }
Exemplo 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]);
         }
     }
 }
Exemplo 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);
         }
     }
 }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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;
 }
Exemplo 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);
 }
Exemplo n.º 8
0
 public bool FindWinner(FieldCell lastCell)
 {
     return(FindWinnerColumn(lastCell) || FindWinnerRow(lastCell) ||
            FindWinnerDiagonal(lastCell) || FindWinnerAntidiagonal(lastCell));
 }
Exemplo 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);
            }
        }
Exemplo n.º 10
0
        private IEnumerator AnnounceMove(FieldCell move)
        {
            yield return(new WaitForSeconds(0.3f));

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