Exemplo n.º 1
0
        public Move GetBestAiMoveMedium(IMatrixAlgorithm ticTacToeMatrix)
        {
            Move bestMove = new Move
            {
                Row = -1,
                Col = -1
            };
            Int32 bestScore = Int32.MinValue;

            for (Int32 x = 0; x < ticTacToeMatrix.BoardSize; x++)
            {
                for (Int32 y = 0; y < ticTacToeMatrix.BoardSize; y++)
                {
                    if (ticTacToeMatrix.Board[x, y] == PlayerType.Unassigned)
                    {
                        ticTacToeMatrix.Board[x, y] = PlayerType.X;

                        Int32 score = this.GetMiniMaxMedium(ticTacToeMatrix, 0, false);

                        ticTacToeMatrix.Board[x, y] = PlayerType.Unassigned;

                        if (score > bestScore)
                        {
                            bestScore    = score;
                            bestMove.Row = x;
                            bestMove.Col = y;
                        }
                    }
                }
            }

            return(bestMove);
        }
Exemplo n.º 2
0
        public PlayerType[,] WriteCurrentStringToBoard(String BoardData, IMatrixAlgorithm matrixAlgorithm)
        {
            if (String.IsNullOrWhiteSpace(BoardData))
            {
                throw new ArgumentOutOfRangeException(nameof(BoardData), "Board data may not be empty, null or whitespace");
            }

            if (matrixAlgorithm is null)
            {
                throw new ArgumentNullException(nameof(matrixAlgorithm));
            }

            for (Int32 board = 0; board < matrixAlgorithm.BoardSize * matrixAlgorithm.BoardSize; board++)
            {
                BoardData = BoardData.Replace("}", String.Empty);
                BoardData = BoardData.Replace("{", String.Empty);
                String[] boardDataArray = BoardData.Split(',');

                Int32 col = board / matrixAlgorithm.BoardSize;
                Int32 row = board % matrixAlgorithm.BoardSize;

                matrixAlgorithm.Board[col, row] = (PlayerType)Enum.Parse(typeof(PlayerType), boardDataArray[board]);
            }

            return(matrixAlgorithm.Board);
        }
Exemplo n.º 3
0
        public PointIndex GetAiHardPointIndex(IMatrixAlgorithm ticTacToeMatrix)
        {
            if (ticTacToeMatrix is null)
            {
                throw new ArgumentNullException(nameof(ticTacToeMatrix));
            }

            this.GetMiniMaxAiHard(ticTacToeMatrix.Board, PlayerType.O, ticTacToeMatrix);
            return(this.BestPointMove);
        }
Exemplo n.º 4
0
        private void RestartGame()
        {
            if (this.matrixAlgorithm != null)
            {
                this.matrixAlgorithm.GameEnd -= this.TicTacToeGameEnd;
                this.matrixAlgorithm          = null;
            }

            this.aiMove = (IAiMove)GlobalFactory.Create(typeof(IAiMove));
            this.SetupBoard(this.boardSizeTrackBar.Value);
        }
Exemplo n.º 5
0
        public Data ReadIni(String fileName)
        {
            if (String.IsNullOrWhiteSpace(fileName))
            {
                throw new ArgumentOutOfRangeException(nameof(fileName), "file name may not be empty, null or whitespace");
            }

            this.MatrixAlgorithm = (IMatrixAlgorithm)GlobalFactory.Create(typeof(IMatrixAlgorithm));
            FileIniDataParser parser         = new FileIniDataParser();
            IniData           iniData        = parser.ReadFile(fileName);
            SavedGameState    savedGameState = new SavedGameState
            {
                BoardSize        = this.MatrixAlgorithm.BoardSize,
                CurrentTurn      = this.MatrixAlgorithm.CurrentTurn,
                CurrentTurnCount = this.MatrixAlgorithm.CurrentTurnCount,
            };
            Data data = new Data
            {
                CurrentGame = savedGameState,
                HistoryList = new List <History>()
            };

            data.CurrentGame.CurrentTurnCount = Convert.ToInt32(iniData["CurrentGame"]["CurrentTurnCount"]);
            data.CurrentGame.BoardSize        = Convert.ToInt32(iniData["CurrentGame"]["BoardSize"]);
            data.Round      = Convert.ToInt32(iniData["Settings"]["Difficutly"]);
            data.Difficulty = (Difficulty)Enum.Parse(typeof(Difficulty), iniData["Settings"]["Difficulty"]);
            Int32 sectionIndex = 0;

            using (StreamReader sr = File.OpenText(fileName))
            {
                foreach (SectionData section in iniData.Sections)
                {
                    sectionIndex++;
                }
            }

            for (Int32 i = 0; i < sectionIndex - 2; i++)
            {
                History history = new History
                {
                    Winner     = iniData[$"HistoryList-{i}"]["Winner"],
                    RoundCount = Convert.ToInt32(iniData[$"HistoryList-{i}"]["RoundCount"])
                };
                data.HistoryList.Add(history);
            }

            String key   = $"Board";
            String value = iniData["CurrentGame"][key];

            data.CurrentGame.BoardData = value;

            return(data);
        }
Exemplo n.º 6
0
        public PointIndex GetAiMediumPointIndex(IMatrixAlgorithm ticTacToeMatrix)
        {
            Move bestMove = this.GetBestAiMoveMedium(ticTacToeMatrix);

            PointIndex point = new PointIndex
            {
                X = bestMove.Row,
                Y = bestMove.Col
            };

            return(point);
        }
Exemplo n.º 7
0
        private void SetupBoard(Int32 dimension)
        {
            if (dimension < 2)
            {
                throw new ArgumentOutOfRangeException(nameof(dimension));
            }

            this.matrixAlgorithm          = (IMatrixAlgorithm)GlobalFactory.Create(typeof(IMatrixAlgorithm));
            this.matrixAlgorithm.GameEnd += this.TicTacToeGameEnd;
            this.gamePanel.Difficulty     = this.ChooseDifficulty();


            this.mainTableLayloutPanel.Controls.Clear();

            this.mainTableLayloutPanel.RowCount    = dimension;
            this.mainTableLayloutPanel.ColumnCount = dimension;

            this.gamePanel.ButtonList = new List <Button>();

            for (Int32 row = 0; row < dimension; row++)
            {
                for (Int32 col = 0; col < dimension; col++)
                {
                    Button current = new Button
                    {
                        Height = 62,
                        Width  = 62,
                        Tag    = new PointIndex(col, row)
                    };
                    this.gamePanel.ButtonList.Add(current);

                    current.Click += this.PlayerButtonOnClick;

                    this.mainTableLayloutPanel.Controls.Add(current, col, row);
                }
            }

            this.matrixAlgorithm.InitializeBoard(dimension);

            if (this.gamePanel.Difficulty == Difficulty.Middle)
            {
                this.ClickAiMiddle();
            }

            this.ShowStats();
        }
Exemplo n.º 8
0
 public MainWindow(IMatrixAlgorithm matrixAlgorithm, IAiMove aiMove, ISerializeData serialize,
                   IDeSerializeData deserializeData, IIniParseData iniParseData, IDataBaseWriter dataBaseWriter)
 {
     this.matrixAlgorithm = matrixAlgorithm ?? throw new ArgumentNullException(nameof(matrixAlgorithm));
     this.aiMove          = aiMove ?? throw new ArgumentNullException(nameof(aiMove));
     this.serialize       = serialize ?? throw new ArgumentNullException(nameof(serialize));
     this.deserializeData = deserializeData ?? throw new ArgumentNullException(nameof(deserializeData));
     this.iniParseData    = iniParseData ?? throw new ArgumentNullException(nameof(iniParseData));
     this.dataBaseWriter  = dataBaseWriter ?? throw new ArgumentNullException(nameof(dataBaseWriter));
     this.settings        = new Settings
     {
         HistoryList = new List <History>()
     };
     this.gamePanel = new GamePanel();
     this.InitializeComponent();
     this.txtBoxTrackBar.Text = this.boardSizeTrackBar.Value.ToString();
 }
Exemplo n.º 9
0
        public Boolean GetMovesLeft(IMatrixAlgorithm ticTacToeMatrix)
        {
            if (ticTacToeMatrix is null)
            {
                throw new ArgumentNullException(nameof(ticTacToeMatrix));
            }

            for (Int32 x = 0; x < ticTacToeMatrix.BoardSize; x++)
            {
                for (Int32 y = 0; y < ticTacToeMatrix.BoardSize; y++)
                {
                    if (ticTacToeMatrix.Board[x, y] == PlayerType.Unassigned)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 10
0
        public String WriteCurrentBoardToString(IMatrixAlgorithm matrixAlgorithm)
        {
            if (matrixAlgorithm is null)
            {
                throw new ArgumentNullException(nameof(matrixAlgorithm));
            }

            StringBuilder stringBuilder = new StringBuilder();

            for (Int32 x = 0; x < matrixAlgorithm.Board.GetLength(0); x++)
            {
                stringBuilder.Append(",{");
                for (Int32 y = 0; y < matrixAlgorithm.Board.GetLength(1); y++)
                {
                    stringBuilder.AppendFormat("{0},", matrixAlgorithm.Board[x, y]);
                }

                stringBuilder.Append('}');
            }
            stringBuilder.Replace(",}", "}").Remove(0, 1);

            return(stringBuilder.ToString());
        }
Exemplo n.º 11
0
        public Int32 GetMiniMaxMedium(IMatrixAlgorithm ticTacToeMatrix, Int32 depth, Boolean isMax)
        {
            if (ticTacToeMatrix is null)
            {
                throw new ArgumentNullException(nameof(ticTacToeMatrix));
            }

            if (ticTacToeMatrix.Board is null)
            {
                throw new ArgumentNullException(nameof(ticTacToeMatrix));
            }


            // TODO: KEIN PLAYER CHECK!!!!! AI SONST KAPUTT
            Int32 score = this.EvaluateAiMediumMoves(ticTacToeMatrix.Board) - depth;

            if (score == 10)
            {
                return(score - depth);
            }

            if (score == -10)
            {
                return(score);
            }

            if (this.GetMovesLeft(ticTacToeMatrix))
            {
                return(score);
            }

            depth += 1;
            if (!isMax)
            {
                Int32 best = Int32.MinValue;

                for (Int32 x = 0; x < ticTacToeMatrix.BoardSize; x++)
                {
                    for (Int32 y = 0; y < ticTacToeMatrix.BoardSize; y++)
                    {
                        if (ticTacToeMatrix.Board[x, y] == PlayerType.Unassigned)
                        {
                            ticTacToeMatrix.Board[x, y] = PlayerType.O;

                            best = Math.Max(best, this.GetMiniMaxMedium(ticTacToeMatrix, depth, true));

                            ticTacToeMatrix.Board[x, y] = PlayerType.Unassigned;
                        }
                    }
                }
                return(best);
            }
            else
            {
                Int32 best = Int32.MaxValue;

                for (Int32 x = 0; x < ticTacToeMatrix.BoardSize; x++)
                {
                    for (Int32 y = 0; y < ticTacToeMatrix.BoardSize; y++)
                    {
                        if (ticTacToeMatrix.Board[x, y] == PlayerType.Unassigned)
                        {
                            ticTacToeMatrix.Board[x, y] = PlayerType.X;

                            best = Math.Min(best, this.GetMiniMaxMedium(ticTacToeMatrix, depth, false));

                            ticTacToeMatrix.Board[x, y] = PlayerType.Unassigned;
                        }
                    }
                }
                return(best);
            }
        }
Exemplo n.º 12
0
        public Int32 GetMiniMaxAiHard(PlayerType[,] newBoard, PlayerType state, IMatrixAlgorithm ticTacToeMatrix)
        {
            if (ticTacToeMatrix is null)
            {
                throw new ArgumentNullException(nameof(ticTacToeMatrix));
            }

            this.FunctionCalls++;

            if (this.EvaluateAiHardMode(newBoard) && ticTacToeMatrix.CurrentTurn == PlayerType.X)
            {
                return(-10);
            }
            else if (this.EvaluateAiHardMode(newBoard) && ticTacToeMatrix.CurrentTurn == PlayerType.O)
            {
                return(10);
            }
            else if (this.GetMovesLeftAiHard(ticTacToeMatrix, newBoard))
            {
                return(0);
            }
            this.ScoreList = new List <NextMove>();

            for (Int32 x = 0; x < ticTacToeMatrix.BoardSize; x++)
            {
                for (Int32 y = 0; y < ticTacToeMatrix.BoardSize; y++)
                {
                    NextMove nextMove = new NextMove();
                    if (newBoard[x, y] == PlayerType.Unassigned)
                    {
                        nextMove.PointIndex = new PointIndex {
                            X = x, Y = y
                        };
                        newBoard[x, y] = state;

                        if (state == PlayerType.X)
                        {
                            Int32 result = this.GetMiniMaxAiHard(newBoard, PlayerType.O, ticTacToeMatrix);
                            nextMove.Score = result;
                        }
                        else
                        {
                            Int32 result = this.GetMiniMaxAiHard(newBoard, PlayerType.X, ticTacToeMatrix);
                            nextMove.Score = result;
                        }

                        newBoard[x, y] = PlayerType.Unassigned;

                        this.ScoreList.Add(nextMove);
                    }
                }
            }

            switch (state)
            {
            case PlayerType.X:
                this.CalcMinValueAiHard();
                break;

            case PlayerType.O:
                this.CalcMaxValueAiHard();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state));
            }

            return(this.BestPointIndex);
        }