Пример #1
0
        protected override int Find(ComputerBrain computerBrain, Layout currentGame)
        {
            var probabilityManager = new ProbabilityManager();

            var currentGameArray = currentGame.Positions.ToArray();

            var beforeEqualGames = computerBrain.Memory.Where(i => i.Positions.SequenceEqual(currentGame.Positions));

            var lostGames     = beforeEqualGames.Where(i => !i.ComputerWin);
            var failMoves     = GetNextTurnMoves(computerBrain, currentGame, lostGames);
            var lostPositions = GetPositionMoves(currentGameArray, failMoves);

            lostPositions.ForEach(position => probabilityManager.Increment(position, false));

            var winGames     = beforeEqualGames.Where(i => i.ComputerWin);
            var correctMoves = GetNextTurnMoves(computerBrain, currentGame, winGames);
            var winPositions = GetPositionMoves(currentGameArray, correctMoves);

            winPositions.ForEach(position => probabilityManager.Increment(position, true));

            var findedPosition = probabilityManager.BestPosition(probabilityManager.ShouldMoveToWin());

            if (currentGame.CanMove(findedPosition))
            {
                return(findedPosition);
            }

            return(AnyPosition(currentGame));
        }
Пример #2
0
        public FindBestMovementTest()
        {
            _findBestMovement = new FindBestMovement(null);

            _currentGameId = Guid.NewGuid();

            _memory = new List <Layout>()
            {
                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', '\0', '\0',
                                   '\0', '\0', '\0', }, _currentGameId, 2, 1),

                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', 'X', '\0',
                                   '\0', '\0', '\0', }, _currentGameId, 3, 4),

                new Layout(new[] { 'X', 'O', 'O',
                                   '\0', 'X', '\0',
                                   '\0', '\0', '\0', }, _currentGameId, 4, 2),

                new Layout(new[] { 'X', 'O', 'O',
                                   '\0', 'X', '\0',
                                   '\0', '\0', 'X', }, _currentGameId, 5, 8),
            };

            _memory.ForEach(i => i.Win(true));

            _computerBrain = new ComputerBrain(_memory);
        }
Пример #3
0
        protected override int Find(ComputerBrain computerBrain, Layout currentGame)
        {
            if (currentGame.CanMove(CENTER))
            {
                return(CENTER);
            }

            if (currentGame.CanMove(TOPLEFT))
            {
                return(TOPLEFT);
            }
            if (currentGame.CanMove(TOPRIGHT))
            {
                return(TOPRIGHT);
            }
            if (currentGame.CanMove(DOWNLEFT))
            {
                return(DOWNLEFT);
            }
            if (currentGame.CanMove(DOWNRIGHT))
            {
                return(DOWNRIGHT);
            }

            for (int i = 0; i < 9; i++)
            {
                if (currentGame.CanMove(i))
                {
                    return(i);
                }
            }

            throw new InvalidOperationException("Eita! não é possivel mover para qualquer posição");
        }
        public FindBestMovement2Test()
        {
            _findBestMovement = new FindBestMovement2();

            _currentGameId = Guid.NewGuid();

            _memory = new List <Layout>()
            {
                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', '\0', '\0',
                                   '\0', '\0', '\0', }, _currentGameId, 0, 1),

                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', 'X', '\0',
                                   '\0', '\0', '\0', }, _currentGameId, 1, 4),

                new Layout(new[] { 'X', 'O', 'O',
                                   '\0', 'X', '\0',
                                   '\0', '\0', '\0', }, _currentGameId, 2, 2),

                new Layout(new[] { 'X', 'O', 'O',
                                   '\0', 'X', '\0',
                                   '\0', '\0', 'X', }, _currentGameId, 3, 8),
            };

            _computerBrain = new ComputerBrain(_memory);
        }
        public void SaveMemory(ComputerBrain brain)
        {
            var layoutDTOs = brain.Memory.Select(layout => new LayoutDTO(layout));

            var memoryData = JsonConvert.SerializeObject(layoutDTOs);

            _dataManager.SaveData(GetMemoryPath(), memoryData);
        }
Пример #6
0
    public void StartGame()
    {
        if (turn == 0)
        {
            bPos     = new Vector3[boardSize.x, boardSize.y];
            bRot     = new Quaternion[boardSize.x, boardSize.y];
            bSprites = new SpriteRenderer[boardSize.x, boardSize.y];

            //Instantiate placeholders & get coordinates
            sheetObject = Instantiate(new GameObject(), transform);
            GameObject boardObj = Instantiate(boardPrefab, sheetObject.transform);

            for (int i = 0; i < boardSize.x; i++)
            {
                for (int j = 0; j < boardSize.y; j++)
                {
                    float angle = 2 * j * Mathf.PI * 0.125f + rotOffset;
                    bPos[i, j] = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * vertSpacing[i];
                    bRot[i, j] = Quaternion.Euler(new Vector3(90, -Mathf.Rad2Deg * angle + 90, 0));
                    GameObject tempObj = Instantiate(placeholderPrefab, bPos[i, j], bRot[i, j], sheetObject.transform);

                    bSprites[i, j] = tempObj.GetComponent <SpriteRenderer>();
                    //bSprites[i,j].sprite = objects[3]; //Peenify
                }
            }

            placeholder = Instantiate(placeholderPrefab).GetComponent <SpriteRenderer>();

            boardController            = boardObj.GetComponent <BoardController>();
            boardController.boardSize  = boardSize;
            boardController.board      = new int[boardSize.x, boardSize.y];
            boardController.tetrisMode = tetrisMode;

            championScores = new float[numPlayers + 1];
            timerValue     = new float[numPlayers];

            if (!isMultiplayer)
            {
                computer = GetComponent <ComputerBrain>();
                computer.ComputerStart();
                computer.difficulty = 2 * difficulty + 1;
                numPlayers          = 2;
            }

            cameraAnim.SetBool("gameStarted", true);

            ChangeTurn();
            ResetTimer();
        }
        else
        {
            print("fast as heck boi");
        }
    }
Пример #7
0
        public int FindPosition(ComputerBrain computerBrain, Layout currentGame)
        {
            var findedPosision = Find(computerBrain, currentGame);

            if (findedPosision >= 0 || _next == null)
            {
                return(findedPosision);
            }

            return(_next.FindPosition(computerBrain, currentGame));
        }
Пример #8
0
        public TicTacToe(char playerIcon, char computerIcon)
        {
            _playerIcon   = playerIcon;
            _computerIcon = computerIcon;

            _computerPhysicalBrain = new ComputerPhysicalBrain();

            _computerBrain = _computerPhysicalBrain.LoadMemory();

            CreateInteligence();

            Reset();
        }
Пример #9
0
        private static List <Layout> GetNextTurnMoves(ComputerBrain computerBrain, Layout currentGame, IEnumerable <Layout> beforeGames)
        {
            List <Layout> moves = new List <Layout>(beforeGames.Count());

            foreach (var current in beforeGames)
            {
                var beforeGame = computerBrain.Memory.Where(i => i.GameId == current.GameId);

                var currentTurn = beforeGame.FirstOrDefault(i => i.Turn == currentGame.Turn + 1);

                if (currentTurn != null)
                {
                    moves.Add(currentTurn);
                }
            }

            return(moves);
        }
Пример #10
0
        protected override int Find(ComputerBrain computerBrain, Layout currentGame)
        {
            var afterWinners = computerBrain.Memory.Where(i => i.Turn >= currentGame.Turn && WinnerChecker.CheckAll(i));

            List <int?> possibleMovements = new List <int?>(afterWinners.Count());

            foreach (var afterWinner in afterWinners)
            {
                var nextTurn = computerBrain.Memory.FirstOrDefault(i => i.GameId == afterWinner.GameId && i.Turn == currentGame.Turn + 1);

                if (currentGame.CanMove(nextTurn.TurnPosition))
                {
                    possibleMovements.Add(nextTurn.TurnPosition);
                }
            }

            var bestMovement = possibleMovements.GroupBy(i => i).OrderByDescending(i => i.Count()).FirstOrDefault();

            return(bestMovement?.First().Value ?? -1);
        }
Пример #11
0
        public void Deveria_retornar_posicao_com_maior_probabilidade_de_vitoria_com_base_no_historico_quando_dois_movimentos_possiveis()
        {
            var newGameId = Guid.NewGuid();

            var otherGame = new List <Layout>()
            {
                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', '\0', '\0',
                                   '\0', '\0', '\0', }, newGameId, 2, 1),

                new Layout(new[] { 'X', 'O', '\0',
                                   'X', '\0', '\0',
                                   '\0', '\0', '\0', }, newGameId, 3, 3),

                new Layout(new[] { 'X', 'O', 'O',
                                   'X', '\0', '\0',
                                   '\0', '\0', '\0', }, newGameId, 4, 2),

                new Layout(new[] { 'X', 'O', 'O',
                                   'X', '\0', '\0',
                                   'X', '\0', '\0', }, newGameId, 5, 6),
            };

            otherGame.ForEach(i => i.Win(true));

            _memory.AddRange(otherGame);
            _memory.AddRange(otherGame);

            var currentGame = new Layout();

            currentGame.Move(0, 'X');
            currentGame.Move(1, 'O');

            _computerBrain = new ComputerBrain(_memory);

            var position = _findBestMovement.FindPosition(_computerBrain, currentGame);

            position.Should().Be(3, because: "Dois dos três jogos vencedores jogaram na posição 3 neste movimento");
        }
Пример #12
0
        public void Deveria_retornar_posicao_com_base_em_falhas_quando_probabilidade_de_falha_maior_que_de_vitoria()
        {
            var newGameId = Guid.NewGuid();

            var otherGame = new List <Layout>()
            {
                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', '\0', '\0',
                                   '\0', '\0', '\0', }, newGameId, 2, 1),

                new Layout(new[] { 'X', 'O', '\0',
                                   '\0', 'X', '\0',
                                   '\0', '\0', '\0', }, newGameId, 3, 4),

                new Layout(new[] { 'X', 'O', 'O',
                                   '\0', 'X', '\0',
                                   '\0', '\0', '\0', }, newGameId, 4, 2),

                new Layout(new[] { 'X', 'O', 'O',
                                   '\0', 'X', '\0',
                                   'X', '\0', '\0', }, newGameId, 5, 6),
            };

            otherGame.ForEach(i => i.Win(false));

            _memory.AddRange(otherGame);
            _memory.AddRange(otherGame);

            _computerBrain = new ComputerBrain(_memory);

            var currentGame = new Layout();

            currentGame.Move(0, 'X');
            currentGame.Move(1, 'O');

            var position = _findBestMovement.FindPosition(_computerBrain, currentGame);

            position.Should().NotBe(4, because: "Pois falhou quando colocou nesse");
        }
Пример #13
0
        protected override int Find(ComputerBrain computerBrain, Layout currentGame)
        {
            var currentGameArray = currentGame.Positions.ToArray();

            var rivalIcon = currentGameArray[currentGame.TurnPosition];

            var computerIcon = currentGame.Player1Icon == rivalIcon ? currentGame.Player2Icon : currentGame.Player1Icon;

            if (computerIcon == null)
            {
                return(-1);
            }

            var computerWinPosition = Win(currentGame, computerIcon.Value);

            if (computerWinPosition != -1)
            {
                return(computerWinPosition);
            }

            var rivalWillWinPosition = Win(currentGame, rivalIcon);

            return(rivalWillWinPosition);
        }
Пример #14
0
 protected abstract int Find(ComputerBrain computerBrain, Layout currentGame);