Bom dia,
Este é um programa que fiz seguindo um vídeo do youtube. Vários comandos utilizados como catch, try, foreach, operadores lógicos.
namespace tictac1
{
public partial class Form1 : Form
{
bool turn = true; // true = X turn; false = Y turn
int turn_count = 0;
public Form1()
{
InitializeComponent();
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void abrirToolStripMenuItem_Click(object sender, EventArgs e)
{
turn = true;
turn_count = 0;
try
{
foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = true;
b.Text = "";
}
}
catch { }
}
private void sobreToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("By Alayon", "Tic Tac About");
}
private void sairToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button_click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (turn)
b.Text = "X";
else
b.Text = "O";
turn = !turn;
b.Enabled = false;
turn_count++;
checkFormWinner();
}
private void checkFormWinner()
{
bool there_is_a_winner = false;
//horizontal checks
if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
there_is_a_winner = true;
else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
there_is_a_winner = true;
//vertical checks
if ((A1.Text == B1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A2.Text == B2.Text) && (B2.Text == C2.Text) && (!A2.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
there_is_a_winner = true;
//DIAGONAL checks
if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
there_is_a_winner = true;
if (there_is_a_winner)
{
disableButtons();
string winner = "";
if (turn)
winner = "O";
else
winner = "X";
MessageBox.Show(winner + " Wins!", "Yay!");
}// end if
else
{
if(turn_count == 9 )
MessageBox.Show("Draw!", "Bummer!");
}
}//end checkFormWinner
private void disableButtons()
{
try
{
foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = false;
}// end foreach
}//end try
catch { }
}
}
Nenhum comentário:
Postar um comentário