public bool InitGame()
        {
            if (player == null)
            {
                player = new List <IPlayer>();
            }

            gameStart = false;

            List <List <ICell> > cellsGame = new List <List <ICell> >();

            for (int i = 0; i < MAX_CELLS_PER_LINE; i++)
            {
                List <ICell> cellsPerLine = new List <ICell>();

                for (int j = 0; j < MAX_CELLS_PER_COLUMN; j++)
                {
                    cellsPerLine.Add(new TicTacTardCell(new Vector2Int(i, j), CellType.Empty));
                }

                cellsGame.Add(cellsPerLine);
            }

            currentState = new TicTacTardState();
            currentState.SetCells(cellsGame);
            isInit = true;
            return(true);
        }
        public IEnumerator StartGame()
        {
            gameStart = true;

            do
            {
                while (lastIntent == Intent.Nothing)
                {
                    yield return(new WaitForSeconds(0.1f));
                }

                TicTacTardState newState =
                    PlayAction(currentState, currentPlayer, lastIntent, true);

                lastIntent = Intent.Nothing;

                if (newState != null)
                {
                    currentState = newState;
                    Debug.Log(newState.nbActionPlayed + " " + (player[0] as TicTacTardPlayer).playerWon + " " + (player[1] as TicTacTardPlayer).playerWon);

                    if ((playerWon = currentPlayer.playerWon) || currentState.nbActionPlayed == 9)
                    {
                        break;
                    }

                    ChangePlayer();
                }
            } while (true);

            EndGame();
        }
        public TicTacTardState(TicTacTardState state, Vector2Int newToken, string tokenToPlace, bool updateDisplay)
        {
            visits   = state.visits;
            winScore = state.winScore;

            List <List <ICell> > oldGridBoard = state.Grid;

            gridBoard      = new List <List <ICell> >();
            nbActionPlayed = state.nbActionPlayed + 1;

            for (int i = 0; i < oldGridBoard.Count; ++i)
            {
                gridBoard.Add(new List <ICell>());

                for (int j = 0; j < oldGridBoard[i].Count; ++j)
                {
                    gridBoard[i].Add(new TicTacTardCell(oldGridBoard[i][j] as TicTacTardCell, updateDisplay));

                    if (i == newToken.x && j == newToken.y)
                    {
                        ((TicTacTardCell)gridBoard[i][j]).token = tokenToPlace;
                    }
                }
            }

            if (updateDisplay)
            {
                gridBoard[newToken.x][newToken.y].GetCellGameObject().transform.GetChild(0).GetComponent <TextMeshPro>().text = tokenToPlace;
            }
        }
Beispiel #4
0
        private Intent GetBestIntent(TicTacTardStateWithAction action)
        {
            float  maxValue = -1;
            Intent intent   = action.intent;

            TicTacTardPlayer fakePlayer = new TicTacTardPlayer(2, "1");

            for (int i = 5; i < 14; ++i)
            {
                Intent tempIntent = (Intent)i;

                if (TicTacTardGame.CanPlayIntent(action, tempIntent))
                {
                    TicTacTardState nextState = TicTacTardGame.PlayAction(action, fakePlayer, tempIntent, false);

                    TicTacTardState calculatedState = ticTacTardStateWithActions.Find(state => state.IsSameState(nextState));

                    if (calculatedState != null && calculatedState.value > maxValue)
                    {
                        maxValue = calculatedState.value;
                        intent   = tempIntent;
                    }
                }
            }

            return(intent);
        }
Beispiel #5
0
        public override Intent GetPlayerIntent(int currentX, int currentY)
        {
            TicTacTardState state = TicTacTardGame.currentState;

            Intent intent = TicTacTardGame.GetRandomPossibleMove(state);

            foreach (TicTacTardStateWithAction action in ticTacTardStateWithActions)
            {
                if (action.IsSameState(state) && TicTacTardGame.CanPlayIntent(state, action.intent))
                {
                    Debug.Log("Found intent !");
                    intent = action.intent;
                    break;
                }
            }

            return(intent);
        }
        public static Intent GetRandomPossibleMove(TicTacTardState state)
        {
            bool   foundPossibleMove = false;
            Intent intent            = Intent.Nothing;
            int    x = 0;
            int    y = 0;
            int    safeLoopIteration = 0;

            List <Intent> testedIntent = new List <Intent>();

            while (!foundPossibleMove && safeLoopIteration < 50 && state.nbActionPlayed != 9)
            {
                ++safeLoopIteration;
                intent = (Intent)Random.Range(5, 14);

                if (testedIntent.Contains(intent))
                {
                    continue;
                }
                else
                {
                    testedIntent.Add(intent);
                }

                if (CanPlayIntent(state, intent))
                {
                    break;
                }

                if (safeLoopIteration >= 50)
                {
                    state.DisplayGrid();
                    Debug.LogError("SafeLoopIteration triggered : Exit Get random intent ");
                }

                if (testedIntent.Count == 9)
                {
                    break;
                }
            }

            return(intent);
        }
        public TicTacTardState(TicTacTardState state)
        {
            visits         = state.visits;
            winScore       = state.winScore;
            nbActionPlayed = state.nbActionPlayed;

            List <List <ICell> > oldGridBoard = state.Grid;

            gridBoard = new List <List <ICell> >();

            for (int i = 0; i < oldGridBoard.Count; ++i)
            {
                gridBoard.Add(new List <ICell>());

                for (int j = 0; j < oldGridBoard[i].Count; ++j)
                {
                    gridBoard[i].Add(new TicTacTardCell(oldGridBoard[i][j] as TicTacTardCell, false));
                }
            }
        }
Beispiel #8
0
        public bool FoundSameStateUntilIndex(int idx, TicTacTardState state)
        {
            bool foundSameState = false;

            if (idx == -1)
            {
                return(false);
            }

            for (int i = 0; i < idx; ++i)
            {
                foundSameState = state.IsSameState(EpisodeStates[i]);

                if (foundSameState)
                {
                    break;
                }
            }

            return(foundSameState);
        }
        // return true if intent is correct
        public static TicTacTardState PlayAction(TicTacTardState state, TicTacTardPlayer player, Intent intent, bool updateDisplay)
        {
            Vector2Int       vector2Int = new Vector2Int(0, 0);
            bool             endGame    = false;
            List <Direction> directions = new List <Direction>();

            switch (intent)
            {
            case Intent.BotCenter:
                vector2Int.x = 1;
                vector2Int.y = 0;

                directions.Add(Direction.Line1);
                directions.Add(Direction.Column2);
                break;

            case Intent.BotLeft:
                vector2Int.x = 0;
                vector2Int.y = 0;
                directions.Add(Direction.Line1);
                directions.Add(Direction.Column1);
                directions.Add(Direction.Diagonal1);
                break;

            case Intent.BotRight:
                vector2Int.x = 2;
                vector2Int.y = 0;
                directions.Add(Direction.Line1);
                directions.Add(Direction.Column3);
                directions.Add(Direction.Diagonal2);
                break;

            case Intent.MidCenter:
                vector2Int.x = 1;
                vector2Int.y = 1;
                directions.Add(Direction.Line2);
                directions.Add(Direction.Column2);
                directions.Add(Direction.Diagonal1);
                directions.Add(Direction.Diagonal2);
                break;

            case Intent.MidLeft:
                vector2Int.x = 0;
                vector2Int.y = 1;
                directions.Add(Direction.Line2);
                directions.Add(Direction.Column1);
                break;

            case Intent.MidRight:
                vector2Int.x = 2;
                vector2Int.y = 1;
                directions.Add(Direction.Line2);
                directions.Add(Direction.Column3);
                break;

            case Intent.TopCenter:
                vector2Int.x = 1;
                vector2Int.y = 2;
                directions.Add(Direction.Line3);
                directions.Add(Direction.Column2);
                break;

            case Intent.TopLeft:
                vector2Int.x = 0;
                vector2Int.y = 2;
                directions.Add(Direction.Line3);
                directions.Add(Direction.Column1);
                directions.Add(Direction.Diagonal2);
                break;

            case Intent.TopRight:
                vector2Int.x = 2;
                vector2Int.y = 2;
                directions.Add(Direction.Line3);
                directions.Add(Direction.Column3);
                directions.Add(Direction.Diagonal1);
                break;
            }

            TicTacTardCell currentCell = state.Grid[vector2Int.x][vector2Int.y] as TicTacTardCell;

            if (currentCell?.token == "-1")
            {
                TicTacTardState newState = new TicTacTardState(state, vector2Int, player.Token, updateDisplay);
                player.IncrementScore(directions);

                return(newState);
            }
            else
            {
                if (updateDisplay)
                {
                    Debug.Log("Token déjà placé");
                }
            }

            return(null);
        }
        public static bool CanPlayIntent(TicTacTardState state, Intent intent)
        {
            int x = 0;
            int y = 0;

            switch (intent)
            {
            case Intent.BotCenter:
                x = 1;
                y = 0;
                break;

            case Intent.BotLeft:
                x = 0;
                y = 0;
                break;

            case Intent.BotRight:
                x = 2;
                y = 0;
                break;

            case Intent.MidCenter:
                x = 1;
                y = 1;
                break;

            case Intent.MidLeft:
                x = 0;
                y = 1;
                break;

            case Intent.MidRight:
                x = 2;
                y = 1;
                break;

            case Intent.TopCenter:
                x = 1;
                y = 2;
                break;

            case Intent.TopLeft:
                x = 0;
                y = 2;
                break;

            case Intent.TopRight:
                x = 2;
                y = 2;
                break;
            }

            TicTacTardCell currentCell = state.Grid[x][y] as TicTacTardCell;

            if (currentCell?.token == "-1")
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public TicTacTardStateWithAction(TicTacTardState state, Intent intent) : base(state)
 {
     this.intent = intent;
 }