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); }
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(); }