0% found this document useful (0 votes)
38 views6 pages

Sah

The document defines a Windows forms application for a chess game. It initializes a 8x8 grid of picture boxes to represent the chess board. It includes methods to color the picture boxes to represent the movement of different chess pieces like the king, queen, bishop, etc. when the user clicks on a square. It also resets the board to the starting position.

Uploaded by

itsbigbraintime6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views6 pages

Sah

The document defines a Windows forms application for a chess game. It initializes a 8x8 grid of picture boxes to represent the chess board. It includes methods to color the picture boxes to represent the movement of different chess pieces like the king, queen, bishop, etc. when the user clicks on a square. It also resets the board to the starting position.

Uploaded by

itsbigbraintime6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp18
{
public partial class Form1 : Form
{
PictureBox[,] tabla = new PictureBox[8, 8];
int figura = 0;
int nj;
int g;
bool aa = false;
public Form1()
{
InitializeComponent();
int m= 0;
for(int i = 0; i < 8; i++)
{
for(int j= 0; j < 8; j++)
{
m++;
string pc = $"PictureBox{m}";
PictureBox pc2 = (PictureBox)Controls.Find(pc, true)[0];
tabla[i, j] = pc2;
}
}

private void pictureBox_Click(object sender, EventArgs e)


{
PictureBox clickedPictureBox = sender as PictureBox;
if (!aa)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (clickedPictureBox == tabla[i, j])
{
nj = i;
g = j;
Boje(nj, g);
}
}
}
aa = true;
}
else
{
aa= false;
#region restart
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (i % 2 == 0 && j % 2 == 0)
{
tabla[i, j].BackColor = Color.White;
}
if (i % 2 == 0 && j % 2 == 1)
{
tabla[i, j].BackColor = Color.Black;
}
if (i % 2 == 1 && j % 2 == 0)
{
tabla[i, j].BackColor = Color.Black;
}
if (i % 2 == 1 && j % 2 == 1)
{
tabla[i, j].BackColor = Color.White;
}

}
}
#endregion
}

}
public void Pomeranje()
{

}
public void Boje(int m, int n)
{
aa=true;
int m1 = m;
int n1 = n;
switch (figura)
{
case 1:
#region kralj
tabla[m, n].BackColor = Color.RosyBrown;
tabla[m+1, n+1].BackColor = Color.RosyBrown;
tabla[m + 1, n].BackColor = Color.RosyBrown;
tabla[m+1, n-1].BackColor = Color.RosyBrown;
tabla[m, n + 1].BackColor = Color.RosyBrown;
tabla[m, n-1].BackColor = Color.RosyBrown;
tabla[m-1, n].BackColor = Color.RosyBrown;
tabla[m-1, n-1].BackColor = Color.RosyBrown;
tabla[m-1, n + 1].BackColor = Color.RosyBrown;
m = m1;
n = n1;
#endregion

break;
case 2:
Lovac(m, n);
Top(m, n);
break;
case 3:
Lovac(m, n);
break;
case 4:
m = m1;
n = n1;
if (m + 2 < 8)
{
if (n + 1 < 8)
{
tabla[m + 2, n + 1].BackColor = Color.RosyBrown;
}
if (n -1 >-1)
{
tabla[m + 2, n -1].BackColor = Color.RosyBrown;
}
if (n - 2 > -1)
{
tabla[m + 1, n - 2].BackColor = Color.RosyBrown;
}
if (n + 2 < 8)
{
tabla[m + 1, n +2].BackColor = Color.RosyBrown;
}
}
else if (m + 1 < 8)
{

if (n - 2 > -1)
{
tabla[m + 1, n - 2].BackColor = Color.RosyBrown;
}
if (n + 2 < 8)
{
tabla[m + 1, n + 2].BackColor = Color.RosyBrown;
}
}
if (m - 2 >-1)
{
if (n + 1 < 8)
{
tabla[m - 2, n + 1].BackColor = Color.RosyBrown;
}
if (n - 1 > -1)
{
tabla[m - 2, n - 1].BackColor = Color.RosyBrown;
}
if (n - 2 > -1)
{
tabla[m -1, n - 2].BackColor = Color.RosyBrown;
}
if (n + 2 < 8)
{
tabla[m -1, n + 2].BackColor = Color.RosyBrown;
}
}
else if (m - 1 > -1)
{

if (n - 2 > -1)
{
tabla[m - 1, n - 2].BackColor = Color.RosyBrown;
}
if (n + 2 < 8)
{
tabla[m - 1, n + 2].BackColor = Color.RosyBrown;
}
}
tabla[m, n].BackColor = Color.RosyBrown;
m = m1;
n = n1;
break;
case 5:
Top(m, n);
break;
case 6:
if (m == 1)
{
tabla[m, n].BackColor = Color.RosyBrown;
tabla[m+1, n].BackColor = Color.RosyBrown;
tabla[m+2, n].BackColor = Color.RosyBrown;
}
else
{
tabla[m, n].BackColor = Color.RosyBrown;
tabla[m + 1, n].BackColor = Color.RosyBrown;
}
break;
case 7:
if (m == 6)
{
tabla[m, n].BackColor = Color.RosyBrown;
tabla[m - 1, n].BackColor = Color.RosyBrown;
tabla[m - 2, n].BackColor = Color.RosyBrown;
}
else
{
tabla[m, n].BackColor = Color.RosyBrown;
tabla[m - 1, n].BackColor = Color.RosyBrown;
}
break;

}
}
public void Lovac(int m, int n)
{
int m1 = m;
int n1 = n;
while (m < 8 && n < 8)
{
tabla[m, n].BackColor = Color.RosyBrown;
m++;
n++;
}
m = m1;
n = n1;
while (m < 8 && n > -1)
{
tabla[m, n].BackColor = Color.RosyBrown;
m++;
n--;
}
m = m1;
n = n1;
while (m > -1 && n < 8)
{
tabla[m, n].BackColor = Color.RosyBrown;
m--;
n++;
}
m = m1;
n = n1;
while (m > -1 && n > -1)
{
tabla[m, n].BackColor = Color.RosyBrown;
m--;
n--;
}
m = m1;
n = n1;
}
public void Top(int m, int n)
{
int n1 = n;
int m1 = m;
while (n != 8)
{
tabla[m, n].BackColor = Color.RosyBrown;
n++;
}
n = n1;
while (n != -1)
{
tabla[m, n].BackColor = Color.RosyBrown;
n--;
}
n = n1;
while (m < 8)
{
tabla[m, n].BackColor = Color.RosyBrown;
m++;
}
m = m1;
while (m > -1)
{
tabla[m, n].BackColor = Color.RosyBrown;
m--;
}
m = m1;
}
private void pictureBox65_Click(object sender, EventArgs e)
{
figura = 2;
}

private void pictureBox66_Click(object sender, EventArgs e)


{
figura = 1;
}

private void pictureBox67_Click(object sender, EventArgs e)


{
figura = 3;
}

private void pictureBox68_Click(object sender, EventArgs e)


{
figura = 4;
}

private void pictureBox69_Click(object sender, EventArgs e)


{
figura = 5;
}

private void pictureBox70_Click(object sender, EventArgs e)


{
figura = 6;
}

private void pictureBox71_Click(object sender, EventArgs e)


{
figura = 7;
}
}
}

You might also like