CodeForLatif
using System.Diagnostics.Eventing.Reader;
namespace ye
{
public partial class Form1 : Form
{
public enum Player
{
X, O
}
Player currentPlayer;
Random random = new Random();
byte playerWinCount = 0;
byte cpuWinCount = 0;
List<Button> buttons;
public Form1()
{
InitializeComponent();
RestartGame();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void CPUmove(object sender, EventArgs e)
{
if (buttons.Count > 0)
{
int index = random.Next(buttons.Count);
buttons[index].Enabled = false;
currentPlayer = Player.O;
buttons[index].Text = currentPlayer.ToString();
buttons[index].BackColor = Color.Red;
buttons.RemoveAt(index);
CheckGame();
foreach (Button x in buttons)
{
x.Enabled = true;
}
}
GameTimer.Stop();
}
private void PlayerMove(object sender, EventArgs e)
{
var button = (Button)sender;
currentPlayer = Player.X;
button.Text = currentPlayer.ToString();
button.Enabled = false;
button.BackColor = Color.Cyan;
buttons.Remove(button);
CheckGame();
foreach (Button x in buttons)
{
x.Enabled = false;
}
GameTimer.Start();
}
private void RestartGame(object sender, EventArgs e)
{
RestartGame();
}
private void CheckGame()
{
if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X" || button4.Text == "X" && button5.Text == "X" && button6.Text == "X"
|| button9.Text == "X" && button8.Text == "X" && button7.Text == "X"
|| button1.Text == "X" && button5.Text == "X" && button9.Text == "X"
|| button3.Text == "X" && button5.Text == "X" && button7.Text == "X"
|| button1.Text == "X" && button4.Text == "X" && button7.Text == "X"
|| button2.Text == "X" && button5.Text == "X" && button8.Text == "X"
|| button3.Text == "X" && button6.Text == "X" && button9.Text == "X")
{
GameTimer.Stop();
MessageBox.Show("Player Wins", "Azzuz Says");
playerWinCount++;
label1.Text = "Player Wins : " + playerWinCount.ToString();
RestartGame();
}
else if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O"
|| button4.Text == "O" && button5.Text == "O" && button6.Text == "O"
|| button9.Text == "O" && button8.Text == "O" && button7.Text == "O"
|| button1.Text == "O" && button5.Text == "O" && button9.Text == "O"
|| button3.Text == "O" && button5.Text == "O" && button7.Text == "O"
|| button1.Text == "O" && button4.Text == "O" && button7.Text == "O"
|| button2.Text == "O" && button5.Text == "O" && button8.Text == "O"
|| button3.Text == "O" && button6.Text == "O" && button9.Text == "O")
{
GameTimer.Stop();
MessageBox.Show("CPU Wins", "Azzuz Says");
cpuWinCount++;
label2.Text = "CPU Wins : " + cpuWinCount.ToString();
RestartGame();
}
else if(buttons.Count == 0)
{
GameTimer.Stop();
MessageBox.Show("Draw!", "Azzuz Says");
RestartGame();
}
}
private void RestartGame()
{
buttons = new List<Button> { button1, button2, button3, button4, button5, button6, button7, button8, button9 };
foreach (Button x in buttons)
{
x.Enabled = true;
x.Text = "?";
x.BackColor = DefaultBackColor;
}
}
}
}
Public Last updated: 2023-12-06 04:57:43 PM