I'm in a little bit of a pickle on my Tic Tac Toe program because I can't see how to simplify the code without breaking it. Do I need to make my code modular?
public partial class dglassAssign4 : Form
{
string[] asImArr;
const int ciArrSize = 9;
int[] aiOrdArr;
int turn = 1;
bool click0 = false, click1 = false, click2 = false, click3 = false, click4 = false, click5 = false, click6 = false, click7 = false, click8 = false;
bool x0, o0, x1, o1, x2, o2, x3, o3, x4, o4, x5, o5, x6, o6, x7, o7, x8, o8,over;
public dglassAssign4()
{
asImArr = new string[] {"WO.jpg", "WX.jpg", "BO.jpg", "RX.jpg" };
aiOrdArr = new int[ciArrSize];
InitializeComponent();
}
private void btnGame_Click(object sender, EventArgs e)
{
gameOver();
}
private void dglassAssign4_Load(object sender, EventArgs e)
{
}
private void pb0_Click(object sender, EventArgs e)
{
if (click0==false)
{
if(turn%2!=0)
{
pb0.Image = Image.FromFile("WX.jpg");
x0 = true;
}
else
{
pb0.Image = Image.FromFile("WO.jpg");
o0 = true; ;
}
turn++;
click0 = true;
endGame(turn);
}
}
private void pb1_Click(object sender, EventArgs e)
{
if (click1 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb1.Image = Image.FromFile("WX.jpg");
x1 = true;
}
else
{
pb1.Image = Image.FromFile("WO.jpg");
o1 = true;
}
turn++;
click1 = true;
endGame(turn);
}
}
private void pb2_Click(object sender, EventArgs e)
{
if (click2 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb2.Image = Image.FromFile("WX.jpg");
x2 = true;
}
else
{
pb2.Image = Image.FromFile("WO.jpg");
o2 = true;
}
turn++;
click2 = true;
endGame(turn);
}
}
private void pb3_Click(object sender, EventArgs e)
{
if (click3 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb3.Image = Image.FromFile("WX.jpg");
x3 = true;
}
else
{
pb3.Image = Image.FromFile("WO.jpg");
o3 = true;
}
turn++;
click3 = true;
endGame(turn);
}
}
private void pb4_Click(object sender, EventArgs e)
{
if (click4 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb4.Image = Image.FromFile("WX.jpg");
x4 = true;
}
else
{
pb4.Image = Image.FromFile("WO.jpg");
o4 = true;
}
turn++;
click4 = true;
endGame(turn);
}
}
private void pb5_Click(object sender, EventArgs e)
{
if (click5 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb5.Image = Image.FromFile("WX.jpg");
x5 = true;
}
else
{
pb5.Image = Image.FromFile("WO.jpg");
o5 = true;
}
turn++;
click5 = true;
endGame(turn);
}
}
private void pb6_Click(object sender, EventArgs e)
{
if (click6 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb6.Image = Image.FromFile("WX.jpg");
x6 = true;
}
else
{
pb6.Image = Image.FromFile("WO.jpg");
o6 = true;
}
turn++;
click6 = true;
endGame(turn);
}
}
private void pb7_Click(object sender, EventArgs e)
{
if (click7 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb7.Image = Image.FromFile("WX.jpg");
x7 = true;
}
else
{
pb7.Image = Image.FromFile("WO.jpg");
o7 = true;
}
turn++;
click7 = true;
endGame(turn);
}
}
private void pb8_Click_1(object sender, EventArgs e)
{
if (click8 == false)
{
txtOut.Text = "";
if (turn % 2 != 0)
{
pb8.Image = Image.FromFile("WX.jpg");
x8 = true;
}
else
{
pb8.Image = Image.FromFile("WO.jpg");
o8 = true;
}
turn++;
click8 = true;
endGame(turn);
}
}
private void endGame(int turn)
{
if(turn >= 5)
{
if (x0 == true && x1 == true && x2 == true || x3 == true && x4 == true && x5 == true || x6 == true && x7 == true && x8 == true || x0==true && x3==true && x6 ==true || x1==true && x4==true && x7==true ||x2==true && x5==true && x8==true|| x0==true && x4==true && x8==true ||
x2 == true && x4 == true && x6 == true || o0 == true && o1 == true && o2 == true || o3 == true && o4 == true && o5 == true || o6 == true && o7 == true && o8 == true || o0 == true && o3 == true && o6 == true || o1 == true && o4 == true && o7 == true || o2 == true && o5 == true && o8 == true || o0 == true && o4 == true && o8 == true ||
o2 == true && o4 == true && o6 == true)
{
txtOut.Text = ("Winner!!!");
over = true;
gameOver();
}
}
}
private void gameOver()
{
pb0.Image = null;
pb1.Image = null;
pb2.Image = null;
pb3.Image = null;
pb4.Image = null;
pb5.Image = null;
pb6.Image = null;
pb7.Image = null;
pb8.Image = null;
click0 = false;
click1 = false;
click2 = false;
click3 = false;
click4 = false;
click5 = false;
click6 = false;
click7 = false;
click8 = false;
x0 = false;
x1 = false;
x2 = false;
x3 = false;
x4 = false;
x5 = false;
x6 = false;
x7 = false;
x8 = false;
o0 = false;
o1 = false;
o2 = false;
o3 = false;
o4 = false;
o5 = false;
o6 = false;
o7 = false;
o8 = false;
turn = 1;
}
}
}