public void removeItem1(DifFigures.tip r) { TakenFigures t = new TakenFigures(); TakenFigures l = new TakenFigures(); foreach (var p in listBox1.Items) { t = (TakenFigures)p; if (t.v.Equals(r)) { l = t; } } listBox1.Items.Remove(l); l.taken--; if (l.taken > 0) { listBox1.Items.Add(l); } }
void figureHandlers(object sender, EventArgs e) { PictureBox ptt = (PictureBox)sender; ValidMoves k = new ValidMoves(Ostanati[kliknato], kliknato, matrica, Enpassant); if (PlayerTurn[3]) { kliknato2 = Array.IndexOf(figures, ptt); if (kliknato2 == -1) { // Ако кликнеш надвор од шахот kliknato2 = 0; PlayerTurn[2] = true; PlayerTurn[3] = false; } else if (k.Zafateni.Whitte[kliknato2 / 8][kliknato2 % 8] == false) { // Ако се премислиш која фигура да ја поместиш PlayerTurn[2] = true; } if ((kliknato2 / 8 == 3 && Dva[0] / 8 == 1 && matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] && Ostanati[Dva[0]].t == DifFigures.tip.Pawn)) { // Валидирање за enpassant move Enpassant[1][Dva[0] % 8] = true; for (int i = 0; i < 8; i++) { if (Enpassant[1][i] && i != Dva[0] % 8) { Enpassant[1][i] = false; // Сите останати enpassant се false } } } // Рокада за црн лев топ && крал if (Castling[4] && Castling[5] && (Castling[6] || Castling[7])) { if (((kliknato2 == 0 && Dva[0] == 4) || (kliknato2 == 4 && Dva[0] == 0)) && Castling[6]) { Castling t = new Castling(matrica, false, true); if (t.ValidirajCastle()) { figures[Dva[0]].Image = null; figures[2].Image = Properties.Resources.Piece4; figures[2].SizeMode = PictureBoxSizeMode.StretchImage; figures[3].Image = Properties.Resources.Piece0; figures[3].SizeMode = PictureBoxSizeMode.StretchImage; figures[kliknato2].Image = null; for (int i = 5; i < 8; i++) { Castling[i] = false; } FinishedPlayer = false; PlayerTurn[3] = false; PlayerTurn[0] = true; this.SpecialDraw(); DifFigures pomosna = Ostanati[Dva[0]]; DifFigures pomosna2 = Ostanati[kliknato2]; Ostanati[3].t = DifFigures.tip.Rook; Ostanati[2].t = DifFigures.tip.King; Ostanati[0].t = DifFigures.tip.None; Ostanati[4].t = DifFigures.tip.None; matrica.ZafateniMesta[0][2] = true; matrica.ZafateniMesta[0][3] = true; matrica.ZafateniMesta[0][0] = false; matrica.ZafateniMesta[0][4] = false; matrica.Whitte[0][2] = false; matrica.Whitte[0][3] = false; Ostanati[2].White = false; Ostanati[3].White = false; label1.Text = "White's Turn"; } } // Рокада за крал и десен црн топ else if (((kliknato2 == 4 && Dva[0] == 7) || (kliknato2 == 7 && Dva[0] == 4)) && Castling[7]) { Castling t = new Castling(matrica, false, false); if (t.ValidirajCastle()) { figures[Dva[0]].Image = null; figures[5].Image = Properties.Resources.Piece0; figures[5].SizeMode = PictureBoxSizeMode.StretchImage; figures[6].Image = Properties.Resources.Piece4; figures[6].SizeMode = PictureBoxSizeMode.StretchImage; figures[kliknato2].Image = null; for (int i = 5; i < 8; i++) { Castling[i] = false; } FinishedPlayer = false; PlayerTurn[3] = false; PlayerTurn[0] = true; // Да ги отселектира валидните потези this.SpecialDraw(); DifFigures pomosna = Ostanati[Dva[0]]; DifFigures pomosna2 = Ostanati[kliknato2]; Ostanati[5].t = DifFigures.tip.Rook; Ostanati[6].t = DifFigures.tip.King; Ostanati[7].t = DifFigures.tip.None; Ostanati[4].t = DifFigures.tip.None; matrica.ZafateniMesta[0][5] = true; matrica.ZafateniMesta[0][6] = true; matrica.ZafateniMesta[0][4] = false; matrica.ZafateniMesta[0][7] = false; matrica.Whitte[0][5] = false; matrica.Whitte[0][6] = false; Ostanati[5].White = false; Ostanati[6].White = false; label1.Text = "White's Turn"; } } } // Проверка дали спротивниот крал е во Шах позиција if (k.figura.t == DifFigures.tip.King) { matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] = false; matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = true; matrica.Whitte[kliknato2 / 8][kliknato2 % 8] = false; SahMat t = new SahMat(kliknato2, Ostanati, matrica, false); if (t.Value()) { k.Valid[kliknato2 / 8][kliknato2 % 8] = false; } matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = false; } // Менување pictureboxes и зачувани фигури во array листата if (k.Valid[kliknato2 / 8][kliknato2 % 8]) { figures[kliknato2].Image = Pom1.Image; figures[kliknato2].SizeMode = PictureBoxSizeMode.StretchImage; figures[Dva[0]].Image = null; bool GoIma = false; int indGoIma = 0; TakenFigures pomosna = new TakenFigures(); // Додавање во листа, доколку е земена фигура if (Ostanati[kliknato2].t != DifFigures.tip.None) { TakenFigures l = new TakenFigures(); foreach (TakenFigures p in listBox2.Items) { pomosna = (TakenFigures)p; if (pomosna.v.Equals(Ostanati[kliknato2].t)) { GoIma = true; indGoIma = listBox2.Items.IndexOf(p); l = p; } } if (GoIma) { listBox2.Items.Remove(l); l.taken++; listBox2.Items.Add(l); } if (!GoIma) { WhiteTaken.Add(Ostanati[kliknato2].t); if (Ostanati[kliknato2].t == DifFigures.tip.Pawn) { listBox2.Items.Add(new TakenFigures(DifFigures.tip.Pawn, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Knight) { listBox2.Items.Add(new TakenFigures(DifFigures.tip.Knight, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Bishop) { listBox2.Items.Add(new TakenFigures(DifFigures.tip.Bishop, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Rook) { listBox2.Items.Add(new TakenFigures(DifFigures.tip.Rook, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Queen) { listBox2.Items.Add(new TakenFigures(DifFigures.tip.Queen, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.King) { listBox2.Items.Add(new TakenFigures(DifFigures.tip.King, 1)); } } } // Ако сум го поместил кралот, или некој од топовите, оневозможување на рокада if (FinishedPlayer) { if (Dva[0] == 4) { Castling[5] = false; } if (Dva[0] == 0) { Castling[6] = false; } if (Dva[0] == 7) { Castling[7] = false; } bool enpa = false; // За enpassante дали е валиден if ((Dva[0] % 8 != kliknato2 % 8 && Dva[0] / 8 == 4 && kliknato2 / 8 == 5 && Ostanati[kliknato2].t == DifFigures.tip.None && Ostanati[Dva[0]].t == DifFigures.tip.Pawn && Ostanati[kliknato2 - 8].t == DifFigures.tip.Pawn)) { Ostanati[kliknato2 - 8].t = DifFigures.tip.None; Ostanati[kliknato2].t = DifFigures.tip.Pawn; Ostanati[Dva[0]].t = DifFigures.tip.None; if (ShowValid) { this.DrawIt(); } matrica.ZafateniMesta[kliknato2 / 8 - 1][kliknato2 % 8] = false; matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = true; matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] = false; Ostanati[kliknato2].White = false; figures[kliknato2 - 8].Image = null; enpa = true; PlayerTurn[3] = false; PlayerTurn[0] = true; FinishedPlayer = false; bool goIma = false; TakenFigures t = new TakenFigures(); foreach (TakenFigures p in listBox2.Items) { if (p.v.Equals(DifFigures.tip.Pawn)) { goIma = true; t = p; } } if (goIma) { listBox2.Items.Remove(t); t.taken++; listBox2.Items.Add(t); } else { listBox2.Items.Add(new TakenFigures(DifFigures.tip.Pawn, 1)); } } // Ако enpa е true, тогаш е завршен потегот и не треба да влегува во овој иф if (!enpa) { Ostanati[kliknato2].t = Ostanati[Dva[0]].t; Ostanati[kliknato2].White = false; Ostanati[Dva[0]].White = false; Ostanati[Dva[0]].t = DifFigures.tip.None; PlayerTurn[3] = false; PlayerTurn[0] = true; FinishedPlayer = false; matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = true; matrica.Whitte[kliknato2 / 8][kliknato2 % 8] = false; matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] = false; if (ShowValid) { this.DrawIt(); } } label1.Text = "White's Turn"; } bool Nekoja = false; // Го барам белиот крал за да проверам дали е во Шах позиција, доколку не го најдам, завршена е играта for (int i = 0; i < 64; i++) { if (Ostanati[i].t == DifFigures.tip.King && Ostanati[i].White) { indGoIma = i; Nekoja = true; } } if (Nekoja) { SahMat Krall = new SahMat(indGoIma, Ostanati, matrica, true); Krall.Val(); } else if (!Nekoja) { PlayerTurn[0] = PlayerTurn[1] = PlayerTurn[2] = PlayerTurn[3] = false; label1.Text = "Victory!!!"; File.AppendAllText("HighScores.txt", Environment.NewLine + vtorIgrac + " " + textBox2.Text + " " + prvIgrac + " " + textBox1.Text); MessageBox.Show("" + vtorIgrac + " победи! Честитки!", "Победа"); } // Доколку пионот стигне на другиот крај од шаховската табла if (kliknato2 / 8 == 7 && Ostanati[kliknato2].t == DifFigures.tip.Pawn) { MessageBox.Show("Pawn Promotion! Одберете друга фигура којашто сакате да ја замените наместо пионот"); PawnPromotion Promotion = new PawnPromotion(this, false, kliknato2, BlackTaken); Promotion.Show(); } } } if (PlayerTurn[2] && FinishedPlayer) { Keep = 0; k = new ValidMoves(Ostanati[kliknato], kliknato, matrica, Enpassant); Pom1.Image = figures[kliknato].Image; kliknato = Array.IndexOf(figures, ptt); if (Keep == 1) { Keep = 0; } Dva[Keep] = kliknato; Keep++; // Ако сум одбрал црна фигура и белите фигури завршиле if (!Ostanati[kliknato].t.Equals(DifFigures.tip.None) && PlayerTurn[2] && Ostanati[kliknato].White == false) { PictureBox pttt = (PictureBox)sender; Pom1.Image = pttt.Image; PlayerTurn[3] = true; PlayerTurn[2] = false; if (ShowValid) { DrawIt(); } } /* else if (!PlayerTurn[3] && (Ostanati[kliknato].t.Equals(DifFigures.tip.None) || Ostanati[kliknato].White)) * { * PlayerTurn[1] = false; * } */ } else if (PlayerTurn[1]) { kliknato2 = Array.IndexOf(figures, ptt); if (kliknato2 == -1) { kliknato2 = 0; PlayerTurn[1] = false; PlayerTurn[0] = true; } // Aко кликнам бело, селектирај ја новата фигура else if (k.Zafateni.Whitte[kliknato2 / 8][kliknato2 % 8] == true) { PlayerTurn[0] = true; } // Enpassante за бели фигури if ((kliknato2 / 8 == 4 && Dva[0] / 8 == 6 && matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] && Ostanati[Dva[0]].t == DifFigures.tip.Pawn)) { try { if (kliknato % 8 - 1 >= 0 && matrica.ZafateniMesta[4][kliknato2 % 8 - 1] && !matrica.Whitte[4][kliknato2 % 8 - 1] || (kliknato % 8 + 1 < 8) && matrica.ZafateniMesta[4][kliknato2 % 8 + 1] && !matrica.Whitte[4][kliknato2 % 8 + 1]) { Enpassant[0][Dva[0] % 8] = true; for (int i = 0; i < 8; i++) { if (Enpassant[0][i] && i != Dva[0] % 8) { Enpassant[0][i] = false; } } } } catch (Exception ent) { } } // Рокада за бел крал и десен бел топ if (Castling[0] && Castling[1] && (Castling[2] || Castling[3])) { if (((kliknato2 == 63 && Dva[0] == 60) || (kliknato2 == 60 && Dva[0] == 63)) && Castling[3]) { Castling t = new Castling(matrica, true, false); if (t.ValidirajCastle()) { this.SpecialDraw(); figures[Dva[0]].Image = null; figures[61].Image = Properties.Resources.Piece63; figures[61].SizeMode = PictureBoxSizeMode.StretchImage; figures[62].Image = Properties.Resources.Piece60; figures[62].SizeMode = PictureBoxSizeMode.StretchImage; figures[kliknato2].Image = null; for (int i = 0; i < 4; i++) { Castling[i] = false; } FinishedPlayer = true; PlayerTurn[1] = false; PlayerTurn[2] = true; DifFigures pomosna = Ostanati[Dva[0]]; DifFigures pomosna2 = Ostanati[kliknato2]; Ostanati[61].t = DifFigures.tip.Rook; Ostanati[62].t = DifFigures.tip.King; Ostanati[60].t = DifFigures.tip.None; Ostanati[63].t = DifFigures.tip.None; matrica.ZafateniMesta[7][5] = true; matrica.ZafateniMesta[7][6] = true; matrica.ZafateniMesta[7][7] = false; matrica.ZafateniMesta[7][4] = false; matrica.Whitte[7][5] = true; matrica.Whitte[7][6] = true; Ostanati[61].White = true; Ostanati[62].White = true; label1.Text = "Black's Turn"; } } // Рокада за лев бел топ и бел крал else if (((kliknato2 == 56 && Dva[0] == 60) || (kliknato2 == 60 && Dva[0] == 56)) && Castling[2]) { Castling t = new Castling(matrica, true, true); if (t.ValidirajCastle()) { this.SpecialDraw(); figures[Dva[0]].Image = null; figures[58].Image = Properties.Resources.Piece60; figures[58].SizeMode = PictureBoxSizeMode.StretchImage; figures[59].Image = Properties.Resources.Piece63; figures[59].SizeMode = PictureBoxSizeMode.StretchImage; figures[kliknato2].Image = null; for (int i = 0; i < 4; i++) { Castling[i] = false; } FinishedPlayer = true; PlayerTurn[1] = false; PlayerTurn[2] = true; DifFigures pomosna = Ostanati[Dva[0]]; DifFigures pomosna2 = Ostanati[kliknato2]; Ostanati[59].t = DifFigures.tip.Rook; Ostanati[58].t = DifFigures.tip.King; Ostanati[56].t = DifFigures.tip.None; Ostanati[60].t = DifFigures.tip.None; matrica.ZafateniMesta[7][2] = true; matrica.ZafateniMesta[7][3] = true; matrica.ZafateniMesta[7][0] = false; matrica.ZafateniMesta[7][4] = false; matrica.Whitte[7][2] = true; matrica.Whitte[7][3] = true; Ostanati[59].White = true; Ostanati[58].White = true; label1.Text = "Black's Turn"; } } } // Проверка дали кралот е уште во игра if (k.figura.t == DifFigures.tip.King) { matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] = false; matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = true; matrica.Whitte[kliknato2 / 8][kliknato2 % 8] = true; SahMat t = new SahMat(kliknato2, Ostanati, matrica, true); if (t.Value()) { k.Valid[kliknato2 / 8][kliknato2 % 8] = false; } matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = false; } if (k.Valid[kliknato2 / 8][kliknato2 % 8] && !FinishedPlayer) { figures[kliknato2].Image = Pom1.Image; figures[kliknato2].SizeMode = PictureBoxSizeMode.StretchImage; figures[Dva[0]].Image = null; TakenFigures pomosna = new TakenFigures(); bool GoIma = false; int indGoIma = 0; // Ако е земена фигура додај ја во листа if (Ostanati[kliknato2].t != DifFigures.tip.None) { TakenFigures l = new TakenFigures(); foreach (TakenFigures p in listBox1.Items) { pomosna = (TakenFigures)p; if (pomosna.v.Equals(Ostanati[kliknato2].t)) { GoIma = true; indGoIma = listBox1.Items.IndexOf(p); l = p; } } // Ако ја има фигурата зголеми го само бројот во листата if (GoIma) { listBox1.Items.Remove(l); pomosna.taken++; listBox1.Items.Add(l); } // Ако не, додај if (!GoIma) { BlackTaken.Add(Ostanati[kliknato2].t); if (Ostanati[kliknato2].t == DifFigures.tip.Pawn) { listBox1.Items.Add(new TakenFigures(DifFigures.tip.Pawn, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Knight) { listBox1.Items.Add(new TakenFigures(DifFigures.tip.Knight, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Bishop) { listBox1.Items.Add(new TakenFigures(DifFigures.tip.Bishop, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Rook) { listBox1.Items.Add(new TakenFigures(DifFigures.tip.Rook, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.Queen) { listBox1.Items.Add(new TakenFigures(DifFigures.tip.Queen, 1)); } else if (Ostanati[kliknato2].t == DifFigures.tip.King) { listBox1.Items.Add(new TakenFigures(DifFigures.tip.King, 1)); } } } // Ако е поместен топ или крал, постави го соодветната булова за рокада false if (!FinishedPlayer) { if (Dva[0] == 60) { Castling[1] = false; } if (Dva[0] == 56) { Castling[2] = false; } if (Dva[0] == 63) { Castling[3] = false; } bool enpa = false; // Enpassante движење if ((Dva[0] % 8 != kliknato2 % 8 && Dva[0] / 8 == 3 && kliknato2 / 8 == 2 && Ostanati[kliknato2].t == DifFigures.tip.None && Ostanati[Dva[0]].t == DifFigures.tip.Pawn && Ostanati[kliknato2 + 8].t == DifFigures.tip.Pawn)) { Ostanati[kliknato2 + 8].t = DifFigures.tip.None; Ostanati[kliknato2].t = DifFigures.tip.Pawn; Ostanati[Dva[0]].t = DifFigures.tip.None; matrica.ZafateniMesta[kliknato2 / 8 + 1][kliknato2 % 8] = false; matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = true; matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] = false; Ostanati[kliknato2].White = true; figures[kliknato2 + 8].Image = null; enpa = true; PlayerTurn[1] = false; PlayerTurn[2] = true; FinishedPlayer = true; if (ShowValid) { this.DrawIt(); } bool goIma = false; TakenFigures t = new TakenFigures(); foreach (TakenFigures p in listBox1.Items) { if (p.v.Equals(DifFigures.tip.Pawn)) { goIma = true; t = p; } } // Ако ја има, зголеми бројач if (goIma) { listBox1.Items.Remove(t); t.taken++; listBox1.Items.Add(t); } else { listBox1.Items.Add(new TakenFigures(DifFigures.tip.Pawn, 1)); } } // Ако не се случило enpassante движење (односно не е завршен играчот) if (!enpa & k.Valid[kliknato2 / 8][kliknato2 % 8]) { Ostanati[kliknato2].t = Ostanati[Dva[0]].t; Ostanati[kliknato2].White = true; Ostanati[Dva[0]].White = false; Ostanati[Dva[0]].t = DifFigures.tip.None; PlayerTurn[1] = false; PlayerTurn[2] = true; FinishedPlayer = true; matrica.ZafateniMesta[kliknato2 / 8][kliknato2 % 8] = true; matrica.Whitte[kliknato2 / 8][kliknato2 % 8] = true; matrica.ZafateniMesta[Dva[0] / 8][Dva[0] % 8] = false; matrica.Whitte[Dva[0] / 8][Dva[0] % 8] = false; if (ShowValid) { this.DrawIt(); } } bool Nekoja = false; // Барај го кралот for (int i = 0; i < 64; i++) { if (Ostanati[i].t == DifFigures.tip.King && !Ostanati[i].White) { indGoIma = i; Nekoja = true; } } // Провери дали е во шах label1.Text = "Black's Turn"; if (Nekoja) { SahMat Krall = new SahMat(indGoIma, Ostanati, matrica, false); Krall.Val(); } // Кралот е погубен else if (!Nekoja) { PlayerTurn[0] = PlayerTurn[1] = PlayerTurn[2] = PlayerTurn[3] = false; label1.Text = "Victory!!!"; MessageBox.Show("" + prvIgrac + " победи! Честитки!", "Победа"); File.AppendAllText("HighScores.txt", Environment.NewLine + prvIgrac + " " + textBox1.Text + " " + vtorIgrac + " " + textBox2.Text); } // Пиончето стигнало на крајот од шаховската табла if (kliknato2 / 8 == 0 && Ostanati[kliknato2].t == DifFigures.tip.Pawn) { MessageBox.Show("Pawn Promotion! Одберете друга фигура којашто сакате да ја замените наместо пионот"); PawnPromotion Promotion = new PawnPromotion(this, true, kliknato2, WhiteTaken); Promotion.Show(); } } } } if (PlayerTurn[0]) { k = new ValidMoves(Ostanati[kliknato], kliknato, matrica, Enpassant); Pom1.SizeMode = PictureBoxSizeMode.StretchImage; Pom1.Image = figures[kliknato].Image; kliknato = Array.IndexOf(figures, ptt); if (Keep >= 1) { Keep = 0; } Dva[Keep] = kliknato; Keep++; if (Keep == 1) { Keep = 0; } if (kliknato == -1) { kliknato = 0; PlayerTurn[2] = false; PlayerTurn[0] = true; } else if (!Ostanati[kliknato].t.Equals(DifFigures.tip.None) && PlayerTurn[0] && Ostanati[kliknato].White == true) { if (ShowValid) { this.DrawIt(); } PictureBox pttt = (PictureBox)sender; Pom1.Image = pttt.Image; PlayerTurn[1] = true; PlayerTurn[0] = false; } // Ако е кликнато нешто што не е бела фигура else if (!PlayerTurn[1] && (Ostanati[kliknato].t.Equals(DifFigures.tip.None) || !Ostanati[kliknato].White)) { PlayerTurn[1] = false; } } }