Created
October 31, 2016 06:52
-
-
Save MadalinNitu/7e47ac31902bdae2bc4fe594af1ee3ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Threading; | |
using System.Windows.Forms; | |
using System.Drawing; | |
namespace Chess | |
{ | |
public class Conditions | |
{ | |
//variabile glomare | |
public Conditions() { } | |
public int[,] chess { get; set; } | |
public PictureBox[,] pic { get; set; } | |
public int rocade_1 = 0; | |
public int rocade_2 = 0; | |
//========================================================= | |
//== TOATE FUNCTIILE DE MAI JOS VERIFICA CORECTITUDINEA=== | |
//== MUTARILOR PENTRU A NU SE INCALCA REGULILE JOCULUI === | |
//========================================================= | |
//functie verificat pion | |
public bool pawn(int old_i, int old_j, int new_i, int new_j, int player) | |
{ | |
if (old_i == 1 || old_i == 6) //daca e prima mutare | |
{ | |
if (player > 0) | |
{ | |
if (new_i == 0 && chess[old_i, old_j] == 1) | |
{ | |
var frm = new Up_Pices(this); | |
if (frm.ShowDialog() == DialogResult.OK) | |
{ | |
chess[old_i, old_j] = frm.seend_val; | |
pic[old_i, old_j].Image = frm.seend; | |
} | |
} | |
if (chess[new_i, new_j] > 0) | |
return false; | |
if ((old_j == new_j) && ((old_i - 2 == new_i) || (old_i - 1 == new_i)) && chess[new_i, new_j] == 0) | |
{ | |
if (new_i == 4) | |
{ | |
if (chess[5, new_j] == 0) | |
return true; | |
else | |
return false; | |
} | |
else return true; | |
} | |
else if (old_j != new_j && (chess[new_i, new_j] < 0 && (old_j + 1 == new_j || old_j - 1 == new_j))) | |
{ | |
return true; | |
} | |
else return false; | |
} | |
else//player 2 | |
{ | |
if (chess[new_i, new_j] < 0) | |
return false; | |
if (new_i == 7 && chess[old_i, old_j] == -1) | |
{ | |
var frm = new Up_Pices(this); | |
if (frm.ShowDialog() == DialogResult.OK) | |
{ | |
chess[old_i, old_j] = frm.seend_val; | |
pic[old_i, old_j].Image = frm.seend; | |
} | |
} | |
if ((old_j == new_j) && ((old_i + 2 == new_i) || (old_i + 1 == new_i)) && chess[new_i, new_j] == 0) | |
{ | |
if (new_i == 3) | |
{ | |
if (chess[2, new_j] == 0) | |
return true; | |
else | |
return false; | |
} | |
else return true; | |
} | |
else if (old_j != new_j && (chess[new_i, new_j] > 0 && ((old_j+1 == new_j || old_j-1 == new_j)&& old_i+1 == new_i))) | |
{ | |
return true; | |
} | |
else return false; | |
} | |
} | |
else//daca nu e prima mutare | |
{ | |
if((new_i == 0 || new_i == 7)) //daca se ajunge la capatul tablei | |
{ | |
return true; | |
} | |
else | |
{ | |
if (player > 0) | |
{ | |
if (chess[new_i, new_j] > 0) | |
return false; | |
if (old_i < new_i) | |
return false; | |
if ((old_j == new_j) && (old_i - 1 == new_i)&&chess[new_i,new_j] == 0) | |
return true; | |
else if (old_j != new_j && (chess[new_i, new_j] < 0 && (old_j + 1 == new_j || old_j - 1 == new_j))) | |
{ | |
return true; | |
} | |
else return false; | |
} | |
else //player 2 | |
{ | |
if (chess[new_i, new_j] < 0) | |
return false; | |
if (old_i > new_i) | |
return false; | |
if ((old_j == new_j) && ((old_i + 1 == new_i)) && chess[new_i, new_j] == 0) | |
return true; | |
else if (old_j != new_j && (chess[new_i, new_j] > 0 && ((old_j+1 == new_j || old_j-1 == new_j)))) | |
{ | |
return true; | |
} | |
else return false; | |
} | |
} | |
} | |
} | |
//functie verificat cal | |
public bool knight(int old_i, int old_j, int new_i, int new_j, int player) | |
{ | |
if (player > 0) | |
{ | |
if (Math.Abs(new_i - old_i) + Math.Abs(new_j - old_j) == 3 && chess[new_i,new_j]<=0) | |
return true; | |
else return false; | |
} | |
else | |
{ | |
if (Math.Abs(new_i - old_i) + Math.Abs(new_j - old_j) == 3 && chess[new_i, new_j] >= 0) | |
return true; | |
else return false; | |
} | |
} | |
//functie verificat tura | |
public bool rook(int old_i, int old_j, int new_i, int new_j, int player) | |
{ | |
if (player > 0)//player 1 | |
{ | |
if ((old_i == new_i) && (old_j != new_j)) | |
{ | |
if(old_j<new_j) | |
{ | |
for (int i = old_j + 1; i < new_j; i++) | |
if (chess[old_i, i] != 0) | |
return false; | |
if (chess[old_i, new_j] <= 0) | |
return true; | |
else return false; | |
} | |
else | |
{ | |
for (int i = new_j + 1; i < old_j; i++) | |
if (chess[old_i, i] != 0) | |
return false; | |
if (chess[old_i, new_j] <= 0) | |
return true; | |
else return false; | |
} | |
} | |
else if ((old_j == new_j) && (old_i != new_i)) | |
{ | |
if (old_i < new_i) | |
{ | |
for (int i = old_i + 1; i < new_i; i++) | |
if (chess[i, old_j] != 0) | |
return false; | |
if (chess[new_i, new_j] <= 0) | |
return true; | |
else return false; | |
} | |
else | |
{ | |
for (int i = new_i + 1; i < old_i; i++) | |
if (chess[i, old_j] != 0) | |
return false; | |
if (chess[new_i, new_j] <= 0) | |
return true; | |
else return false; | |
} | |
} | |
else return false; | |
} | |
else//player 2 | |
{ | |
if ((old_i == new_i) && (old_j != new_j)) | |
{ | |
if (old_j < new_j) | |
{ | |
for (int i = old_j + 1; i < new_j; i++) | |
if (chess[old_i, i] != 0) | |
return false; | |
if (chess[old_i, new_j] >= 0) | |
return true; | |
else return false; | |
} | |
else | |
{ | |
for (int i = new_j + 1; i < old_j; i++) | |
if (chess[old_i, i] != 0) | |
return false; | |
if (chess[old_i, new_j] >= 0) | |
return true; | |
else return false; | |
} | |
} | |
else if ((old_j == new_j) && (old_i != new_i)) | |
{ | |
if (old_i < new_i) | |
{ | |
for (int i = old_i + 1; i < new_i; i++) | |
if (chess[i, old_j] != 0) | |
return false; | |
if (chess[new_i, new_j] >= 0) | |
return true; | |
else return false; | |
} | |
else | |
{ | |
for (int i = new_i + 1; i < old_i; i++) | |
if (chess[i, old_j] != 0) | |
return false; | |
if (chess[new_i, new_j] >= 0) | |
return true; | |
else return false; | |
} | |
} | |
else return false; | |
} | |
} | |
//functie verificat nebun | |
public bool bishop(int old_i, int old_j, int new_i, int new_j, int player) | |
{ | |
if (player > 0) | |
{ | |
int i, j; | |
if (Math.Abs(old_i - new_i) == Math.Abs(old_j - new_j)) | |
{ | |
if (chess[new_i, new_j] > 0) | |
return false; | |
if (old_i < new_i) | |
{ | |
if (old_j < new_j) | |
{ | |
for (i = old_i + 1, j = old_j + 1; i != new_i && j != new_j; i++, j++) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
else | |
{ | |
for (i = old_i + 1, j = old_j - 1; i != new_i && j != new_j; i++, j--) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
} | |
else | |
{ | |
if (old_j < new_j) | |
{ | |
for (i = old_i - 1, j = old_j + 1; i != new_i && j != new_j; i--, j++) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
else | |
{ | |
for (i = old_i - 1, j = old_j - 1; i != new_i && j != new_j; i--, j--) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
} | |
} | |
} | |
else//player 2 | |
{ | |
int i, j; | |
if (Math.Abs(old_i - new_i) == Math.Abs(old_j - new_j)) | |
{ | |
if (chess[new_i, new_j] < 0) | |
return false; | |
if (old_i < new_i) | |
{ | |
if (old_j < new_j) | |
{ | |
for (i = old_i + 1, j = old_j + 1; i != new_i && j != new_j; i++, j++) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
else | |
{ | |
for (i = old_i + 1, j = old_j - 1; i != new_i && j != new_j; i++, j--) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
} | |
else | |
{ | |
if (old_j < new_j) | |
{ | |
for (i = old_i - 1, j = old_j + 1; i != new_i && j != new_j; i--, j++) | |
if (chess[i, j] != 0) | |
return false; | |
return true; | |
} | |
else | |
{ | |
for (i = old_i - 1, j = old_j - 1; i != new_i && j != new_j; i--, j--) | |
if (chess[i,j] != 0) | |
return false; | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
} | |
//functie verificat rege | |
public bool king(int old_i, int old_j, int new_i, int new_j, int player) | |
{ | |
if (player > 0) | |
{ | |
if (chess[new_i, new_j] > 0) | |
{ | |
return false; | |
} | |
if ((old_i - new_i == 1) || (old_i - new_i == -1) || (old_j - new_j == 1) || (old_j - new_j == -1)) | |
return true; | |
else if (((old_j - new_j == 2) || (old_j - new_j == -2)) && ((old_i == new_i) && ((old_i == 7) || (old_i == 0)))) | |
{ | |
if (rocade_1 == 0 && player > 0) | |
{ | |
if ((chess[7, 1] == 0 && chess[7, 2] == 0 && chess[7, 3] == 0) || (chess[7, 5] == 0 && chess[7, 6] == 0) && (chess[7, 0] == 2 || chess[7, 7] == 2)) | |
{ | |
if(new_j ==6) | |
{ | |
Image aux = pic[7, 7].Image; | |
pic[7, 7].Image = pic[7, 5].Image; | |
pic[7, 5].Image = aux; | |
int aux1 = chess[7, 7]; | |
chess[7, 7] = chess[7, 5]; | |
chess[7, 5] = aux1; | |
return true; | |
} | |
else if(new_j == 2) | |
{ | |
Image aux = pic[7, 0].Image; | |
pic[7, 0].Image = pic[7, 3].Image; | |
pic[7, 3].Image = aux; | |
int aux1 = chess[7, 0]; | |
chess[7, 0] = chess[7, 3]; | |
chess[7, 3] = aux1; | |
return true; | |
} | |
rocade_1 = 1; | |
return true; | |
} | |
else return false; | |
}//trebuie umblat la rocada la pozitionarea pieselor | |
else return false; | |
} | |
else return false; | |
} | |
else | |
{ | |
if (chess[new_i, new_j] < 0) | |
{ | |
return false; | |
} | |
if ((old_i - new_i == 1) || (old_i - new_i == -1) || (old_j - new_j == 1) || (old_j - new_j == -1)) | |
return true; | |
else if (((old_j - new_j == 2) || (old_j - new_j == -2)) && ((old_i == new_i) && ((old_i == 7) || (old_i == 0)))) | |
{ | |
if (rocade_2 == 0 && player < 0) | |
{ | |
if ((chess[0, 1] == 0 && chess[0, 2] == 0 && chess[0, 3] == 0) || (chess[0, 5] == 0 && chess[0, 6] == 0) && (chess[0, 0] == -2 || chess[0, 7] == -2)) | |
{ | |
if (new_j == 6) | |
{ | |
Image aux = pic[0, 7].Image; | |
pic[0, 7].Image = pic[0, 5].Image; | |
pic[0, 5].Image = aux; | |
int aux1 = chess[0, 7]; | |
chess[0, 7] = chess[0, 5]; | |
chess[0, 5] = aux1; | |
return true; | |
} | |
else if (new_j == 2) | |
{ | |
Image aux = pic[0, 0].Image; | |
pic[0, 0].Image = pic[0, 3].Image; | |
pic[0, 3].Image = aux; | |
int aux1 = chess[0, 0]; | |
chess[0, 0] = chess[0, 3]; | |
chess[0, 3] = aux1; | |
return true; | |
} | |
rocade_2 = 1; | |
return true; | |
} | |
else return false; | |
} | |
else return false; | |
} | |
else return false; | |
} | |
} | |
//functie verificat regina | |
public bool queen(int old_i, int old_j, int new_i, int new_j, int player) | |
{ | |
if (rook(old_i, old_j, new_i, new_j, player) || bishop(old_i, old_j, new_i, new_j, player)) | |
return true; | |
else return false; | |
} | |
//functie verificat daca e in saj (SUPLIMENTARA NU E FOLOSITA) | |
public bool cheek(int poz_i,int poz_j) | |
{ | |
if (pic[poz_i, poz_j].Image == null) | |
return false; | |
else return true; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment