Ejemplo n.º 1
0
 public HumanPlayer(string name, BoardSymbol my_symbol) : base(name, my_symbol)
 {
     // the association must be made only once
     if (KEY_POSITION_MAP == null)
     {
         initializeKeyPositionMap();
     }
 }
Ejemplo n.º 2
0
        // set the value of a cell. value must always be O or X
        public void makeMove(int r, int c, BoardSymbol value)
        {
            if (board[r, c] != BoardSymbol.Empty)
            {
                throw new ArgumentException("Another value in this cell aldready exists");
            }

            board[r, c] = value;
        }
Ejemplo n.º 3
0
    //Method only called after knowing if the game is already over;
    public bool IWin(ulong playerId, BoardSymbol winnerSymbol)
    {
        BoardSymbol mySymbol = PlayerController.Instance._playerIds.IndexOf(playerId) == 0 ? BoardSymbol.Circle : BoardSymbol.Cross;

        if (mySymbol == winnerSymbol)
        {
            return(true);
        }

        return(false);
    }
Ejemplo n.º 4
0
    public void MakePlay(ulong playerId, int line, int column)
    {
        if (statusGame == true)
        {
            Debug.LogFormat("Player {0} wants to make play at {1}, {2}", playerId, line, column);

            if (playerId != PlayerController.Instance._currentPlayerId)
            {
                Debug.LogFormat("But it1s not their turn! ({0} != {1})", playerId, PlayerController.Instance._currentPlayerId);
                return;
            }

            if (_board[line, column] != BoardSymbol.None)
            {
                Debug.LogFormat("But the space is not empty! (Current: {0})", _board[line, column]);
                return;
            }

            BoardSymbol symbolToSet = PlayerController.Instance._currentPlayerIndex == 0 ? BoardSymbol.Circle : BoardSymbol.Cross;
            OnUpdateBoard?.Invoke(line, column, symbolToSet);

            _board[line, column] = symbolToSet;

            PlayerController.Instance._currentPlayerIndex = 1 - PlayerController.Instance._currentPlayerIndex;
        }

        var winner = GetWinner();

        if (winner == BoardSymbol.Circle)
        {
            print("Bird vencedor");
            OnMatchEnd?.Invoke(BoardSymbol.Circle);
        }
        if (winner == BoardSymbol.Cross)
        {
            print("Pig vencedor");
            OnMatchEnd?.Invoke(BoardSymbol.Cross);
        }
        if (winner == BoardSymbol.None)
        {
            print("Ninguem venceu");
        }

        _numberOfPlays += 1;
        if (_numberOfPlays == BoardSize * BoardSize)
        {
            OnMatchEnd?.Invoke(BoardSymbol.None);
        }
    }
Ejemplo n.º 5
0
        private BoardSymbol getOppositeSymbol(BoardSymbol symb)
        {
            BoardSymbol opposite = BoardSymbol.Empty;   // invalid value

            if (symb == BoardSymbol.Circle)
            {
                opposite = BoardSymbol.Cross;
            }
            else
            {
                opposite = BoardSymbol.Circle;
            }

            return(opposite);
        }
Ejemplo n.º 6
0
        // helper class, converts string BoardSymbole enum value to it's string representative
        private string enumToStr(BoardSymbol symb)
        {
            string symbol = "";

            if (symb == BoardSymbol.Cross)
            {
                symbol = "X";
            }
            else if (symb == BoardSymbol.Circle)
            {
                symbol = "O";
            }
            else if (symb == BoardSymbol.Empty)
            {
                symbol = "-";
            }

            return(symbol);
        }
Ejemplo n.º 7
0
    public void MatchEnd(ulong playerId, BoardSymbol winnerSymbol)
    {
        _numberOfPlays = 0;
        EndScreen endScreen = UIController.Instance.GetScreen(UIScreen.EndGame).GetComponent <EndScreen>();

        if (winnerSymbol == BoardSymbol.None)
        {
            endScreen.ShowDraw();
        }
        else if (IWin(playerId, winnerSymbol))
        {
            endScreen.ShowWin();
        }
        else
        {
            endScreen.ShowLose();
        }

        UIController.Instance.GoToScreen(UIScreen.EndGame);
    }
Ejemplo n.º 8
0
 public void SetSymbol(BoardSymbol symbol)
 {
     ImageCross.SetActive(symbol == BoardSymbol.Circle);
     ImageCircle.SetActive(symbol == BoardSymbol.Cross);
 }
Ejemplo n.º 9
0
        private int findBestMove(Board board, bool maximize, BoardSymbol symbol, int depth)
        {
            if (board.isWinningState())
            {
                // a win state here means whoever made the previous move wins
                // the depth is used to alter the winning and loosing score
                // the faster a state can be reached the higher it's score
                int score = maximize ?  LOSE_STATE_SCORE + depth: WIN_STATE_SCORE - depth;
                return(score);
            }
            else if (board.countEmptyCells() == 0)
            {
                // if this is a tie state
                return(TIE_STATE_SCORE - depth);
            }

            BoardSymbol next_symbol = getOppositeSymbol(symbol);
            int         minmax      = maximize ? -10000: 10000; // best score for maximizer or minimizer
            int         minmax_r    = 0;                        //next move row position for best score
            int         minmax_c    = 0;                        //next move column position for best score

            for (int r = 0; r < 3; r++)
            {
                for (int c = 0; c < 3; c++)
                {
                    // if this cell is already taken, skip
                    if (board.getCell(r, c) != BoardSymbol.Empty)
                    {
                        continue;
                    }

                    // make move
                    board.makeMove(r, c, symbol);

                    // find the best decision for the opposing player
                    // if this is maximizer, make call to minimize and vice versa
                    int score = findBestMove(board, !maximize, next_symbol, depth + 1);

                    // undo move
                    board.clearCell(r, c);

                    //print("Out",score,minmax, r, c, maximize);

                    // get the best value for maximizer or minimizer
                    if (maximize)
                    {
                        if (score > minmax)
                        {
                            minmax   = score;
                            minmax_r = r;
                            minmax_c = c;


                            //print(minmax, r, c, maximize);
                        }
                    }
                    else if (score < minmax)
                    {
                        minmax   = score;
                        minmax_r = r;
                        minmax_c = c;
                    }
                }
            }

            // theis is where the next move cell position is stored
            if (maximize)
            {
                best_move_r = minmax_r;
                best_move_c = minmax_c;
            }

            return(minmax);
        }
Ejemplo n.º 10
0
 public SmartAI(string name, BoardSymbol my_symbol) : base(name, my_symbol)
 {
     best_move_r = 0;
     best_move_r = 0;
 }
Ejemplo n.º 11
0
 public DumbAI(string name, BoardSymbol my_symbol) : base(name, my_symbol)
 {
 }
Ejemplo n.º 12
0
 public void UpdateBoardVisuals(int line, int column, BoardSymbol symbol)
 {
     _slots[line, column].SetSymbol(symbol);
 }
Ejemplo n.º 13
0
 public Player(string name, BoardSymbol my_symbol)
 {
     this.player_name = name;
     this.my_symbol   = my_symbol;
 }
 private void UpdateBoard(int line, int column, BoardSymbol symbol)
 {
     UpdateBoardClientRpc(line, column, symbol);
 }
 public void MatchEndClientRpc(BoardSymbol symbol)
 {
     BoardController.Instance.MatchEnd(NetworkManager.LocalClientId, symbol);
 }
 public void UpdateBoardClientRpc(int line, int column, BoardSymbol symbol)
 {
     BoardController.Instance.UpdateBoardVisuals(line, column, symbol);
 }
 private void MatchEnd(BoardSymbol symbol)
 {
     MatchEndClientRpc(symbol);
 }