public Game(GameStartConfig gs, Label statusLabel, Dictionary<int, Button> MapButton, GameForm fm) { sl = statusLabel; btnMap = MapButton; form = fm; switch (gs.GM) { case GameMode.HumanVSHuman: p1 = new Human() { Name = gs.Player1Name, ID = "X" }; p2 = new Human() { Name = gs.Player2Name, ID = "O" }; break; case GameMode.HumanVSAI: p1 = new Human() { Name = gs.Player1Name, ID = "X" }; p2 = new AI() { Name = "[AI] " + RandomName.GetName(p1.Name), ID = "O", currentDifficulty = gs.Difficulty }; break; case GameMode.AIVSAI: p1 = new AI() { Name = "[AI] " + RandomName.GetName(""), ID = "X", currentDifficulty = gs.Difficulty }; p2 = new AI() { Name = "[AI] " + RandomName.GetName(""), ID = "O", currentDifficulty = gs.Difficulty }; break; default: break; } Players = new LinkedList<Player>(); Players.AddLast(p1); Players.AddLast(p2); pCur = Players.First; fm.Text = string.Format("{0} VS {1}", p1.Name, p2.Name); WriteTurn(); if (p1 is AI) p1.Move(this); }
private IEnumerable<int> AIMovementLogic(Player p, Player others, Dictionary<int, Button> btnMap, int Threshold = 2) { //1 2 3 //4 5 6 //7 8 9 int State = 0; List<int> Checked = new List<int>(); List<int> Valid = new List<int>(); //Diagonal 1.5.9 || 3.5.7 for (int i = 1; i <= 9; i += 4) { Checked.Add(i); if (btnMap[i].Text == p.ID) { State++; Valid.Add(i); } } if (State == Threshold) { if (!Checked.Any(fx => btnMap[fx].Text == others.ID)) { foreach (int index in Checked.Where(fx => !Valid.Any(fy => fy == fx))) { if (string.IsNullOrWhiteSpace(btnMap[index].Text)) yield return index; } } } State = 0; Checked.Clear(); Valid.Clear(); for (int i = 3; i <= 7; i += 2) { Checked.Add(i); if (btnMap[i].Text == p.ID) { State++; Valid.Add(i); } } if (State == Threshold) { if (!Checked.Any(fx => btnMap[fx].Text == others.ID)) { foreach (int index in Checked.Where(fx => !Valid.Any(fy => fy == fx))) { if (string.IsNullOrWhiteSpace(btnMap[index].Text)) yield return index; } } } State = 0; Checked.Clear(); Valid.Clear(); //Horizontal 1.2.3 || 4.5.6 || 7.8.9 for (int i = 1; i <= 7; i += 3) { for (int j = i; j <= i + 2; j++) { Checked.Add(j); if (btnMap[j].Text == p.ID) { State++; Valid.Add(j); } } if (State == Threshold) { if (!Checked.Any(fx => btnMap[fx].Text == others.ID)) { foreach (int index in Checked.Where(fx => !Valid.Any(fy => fy == fx))) { if (string.IsNullOrWhiteSpace(btnMap[index].Text)) yield return index; } } } State = 0; Checked.Clear(); Valid.Clear(); } //Vertical 1.4.7 || 2.5.8 || 3.6.9 for (int i = 1; i <= 3; i++) { for (int j = i; j <= i + 6; j += 3) { Checked.Add(j); if (btnMap[j].Text == p.ID) { State++; Valid.Add(j); } } if (State == Threshold) { if (!Checked.Any(fx => btnMap[fx].Text == others.ID)) { foreach (int index in Checked.Where(fx => !Valid.Any(fy => fy == fx))) { if (string.IsNullOrWhiteSpace(btnMap[index].Text)) yield return index; } } } State = 0; Checked.Clear(); Valid.Clear(); } }
private bool CheckWin(Player p, out Queue<int> btnIndex) { //1 2 3 //4 5 6 //7 8 9 btnIndex = new Queue<int>(); int State = 0; //Diagonal 1.5.9 || 3.5.7 for (int i = 1; i <= 9; i += 4) if (btnMap[i].Text == p.ID) { btnIndex.Enqueue(i); State++; } if (State == 3) return true; else State = 0; btnIndex.Clear(); for (int i = 3; i <= 7; i += 2) if (btnMap[i].Text == p.ID) { btnIndex.Enqueue(i); State++; } if (State == 3) return true; else State = 0; btnIndex.Clear(); //Horizontal 1.2.3 || 4.5.6 || 7.8.9 for (int i = 1; i <= 7; i += 3) { State = 0; for (int j = i; j <= i + 2; j++) { if (btnMap[j].Text == p.ID) { btnIndex.Enqueue(j); State++; } } if (State == 3) return true; else State = 0; btnIndex.Clear(); } //Vertical 1.4.7 || 2.5.8 || 3.6.9 for (int i = 1; i <= 3; i++) { State = 0; for (int j = i; j <= i + 6; j += 3) { if (btnMap[j].Text == p.ID) { btnIndex.Enqueue(j); State++; } } if (State == 3) return true; else State = 0; btnIndex.Clear(); } return false; }