saba abesad

saba abesad

  • NA
  • 4
  • 768

i can not add computer in ti tac toe

Jun 4 2017 10:02 AM
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 WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int s1;
public int s2;
public int sd;
public int turns = 0;
public bool onturn = true;
public Button CPU()
{
Button button = null;
button = CPUtrywinordefend("O");
if (button != null)
return button;
else
{
button = CPUtrywinordefend("X");
if (button != null)
return button;
else
return CPUmoverandom();
}
}
public Button CPUtrywinordefend(string s)
{
Button button = null;
//horizonatl
if ((button1.Text) == (button2.Text) && button1.Text == s && button3.Text == "")
return button3;
else if (button1.Text == button3.Text && button1.Text == s && button2.Text == "")
return button2;
else if (button2.Text == button3.Text && button2.Text == s && button1.Text == "")
return button1;
else if ((button4.Text) == (button6.Text) && button4.Text == s && button5.Text == "")
return button5;
else if (button4.Text == button5.Text && button4.Text == s && button3.Text == "")
return button2;
else if (button6.Text == button5.Text && button5.Text == s && button4.Text == "")
return button4;
else if (button7.Text == button9.Text && button7.Text == s && button8.Text == "")
return button8;
else if (button7.Text == button8.Text && button7.Text == s && button9.Text == "")
return button9;
else if (button9.Text == button8.Text && button9.Text == s && button7.Text == "")
return button7;
//vertikall
else if ((button1.Text) == (button4.Text) && button1.Text == s && button7.Text == "")
return button7;
else if (button1.Text == button7.Text && button1.Text == s && button4.Text == "")
return button4;
else if (button7.Text == button4.Text && button4.Text == s && button1.Text == "")
return button1;
else if (button8.Text == button5.Text && button5.Text == s && button2.Text == "")
return button2;
else if (button8.Text == button2.Text && button2.Text == s && button5.Text == "")
return button4;
else if (button2.Text == button5.Text && button2.Text == s && button8.Text == "")
return button8;
else if (button9.Text == button6.Text && button6.Text == s && button3.Text == "")
return button3;
else if (button9.Text == button3.Text && button3.Text == s && button6.Text == "")
return button6;
else if (button3.Text == button6.Text && button2.Text == s && button9.Text == "")
return button9;
//diagonal9.Text == "")
else if (button1.Text == button5.Text && button5.Text == s && button9.Text == "")
return button9;
else if (button9.Text == button1.Text && button9.Text == s && button5.Text == "")
return button5;
else if (button9.Text == button5.Text && button5.Text == s && button1.Text == "")
return button1;
else if (button7.Text == button5.Text && button5.Text == s && button3.Text == "")
return button3;
else if (button7.Text == button3.Text && button3.Text == s && button5.Text == "")
return button5;
else if (button3.Text == button5.Text && button5.Text == s && button7.Text == "")
return button7;
else return null;
}
public Button CPUmoverandom()
{
Button button = null;
foreach (Control C in Controls)
{
button = C as Button;
if (button != null)
{
if (button.Text == "")
return button;
}
}
return null;
}
private void buttonclicked(object sender, EventArgs e)
{
Button button = (Button)sender;
{
if (button.Text == "")
{
if (onturn == true)
button.Text = "X";
else
button.Text = "O";
turns++;
onturn = !onturn;
}
if (CheckWinner() == true)
{
if (onturn == false)
{
s1++;
MessageBox.Show("??? ?????");
newgame();
if (onturn == false) ;
}
else
{
s2++;
MessageBox.Show("??????????? ?????");
newgame();
}
}
if (Checkdraw() == true && CheckWinner() == false)
{
sd++;
MessageBox.Show("fre");
newgame();
}
if(onturn== false)
{
CPU().PerformClick();
}
}
}
public void newgame()
{
button1.Text = button2.Text = button3.Text = button4.Text = button5.Text = button6.Text = button7.Text = button8.Text = button9.Text = "";
turns = 0;
onturn = true;
label1.Text = s1.ToString();
label2.Text = s2.ToString();
label3.Text = sd.ToString();
}
public bool Checkdraw()
{
if ((turns == 9) && CheckWinner() == false)
return true;
else
return false;
}
public bool CheckWinner()
{
//?????????????
if ((button1.Text == button2.Text) && (button2.Text == button3.Text) && button1.Text != "")
return true;
else if ((button4.Text == button5.Text) && (button5.Text == button6.Text) && button4.Text != "")
return true;
else if ((button7.Text == button8.Text) && (button8.Text == button9.Text) && button7.Text != "")
return true;
// ???????????
if ((button1.Text == button4.Text) && (button4.Text == button7.Text) && button1.Text != "")
return true;
else if ((button2.Text == button5.Text) && (button5.Text == button8.Text) && button2.Text != "")
return true;
else if ((button3.Text == button6.Text) && (button6.Text == button9.Text) && button3.Text != "")
return true;
// ???????????
if ((button1.Text == button5.Text) && (button5.Text == button9.Text) && button1.Text != "")
return true;
else if ((button3.Text == button5.Text) && (button5.Text == button7.Text) && button3.Text != "")
return true;
else
return false;
}
private void button11_Click(object sender, EventArgs e)
{
s1 = s2 = sd = 0;
newgame();
}
private void button10_Click(object sender, EventArgs e)
{
newgame();
}
private void button12_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text= "X :" + s1;
label2.Text = "O:" + s2;
label3.Text = "fre:" + sd;
}
}
}

Attachment: WindowsFormsApp7.zip

Answers (4)