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); }
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); }
public PointIndex GetAiHardPointIndex(IMatrixAlgorithm ticTacToeMatrix) { if (ticTacToeMatrix is null) { throw new ArgumentNullException(nameof(ticTacToeMatrix)); } this.GetMiniMaxAiHard(ticTacToeMatrix.Board, PlayerType.O, ticTacToeMatrix); return(this.BestPointMove); }
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); }
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); }
public PointIndex GetAiMediumPointIndex(IMatrixAlgorithm ticTacToeMatrix) { Move bestMove = this.GetBestAiMoveMedium(ticTacToeMatrix); PointIndex point = new PointIndex { X = bestMove.Row, Y = bestMove.Col }; return(point); }
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(); }
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(); }
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); }
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()); }
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); } }
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); }