eng
competition

Text Practice Mode

Tic Tac Toe C#

created Sep 10th 2014, 17:04 by miguelmcn


0


Rating

198 words
3 completed
00:00
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 JogoDaVelha
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         
        string[,] matriz = new string[3,3];
        string sinal = "O";
        int contVelha = 0;
 
        private void button2_Click(object sender, EventArgs e)
        {
            contVelha++;
 
            Button btn = ((Button)sender);
 
            if (sinal == "X")
            {
                sinal = "O";
            }
            else
            {
                sinal = "X";
            }
 
            btn.Text = sinal;
 
            int linha, coluna;
            linha =  Convert.ToInt32(btn.Tag.ToString().Substring(0,1));
            coluna = Convert.ToInt32(btn.Tag.ToString().Substring(1, 1));
            matriz[linha, coluna] = btn.Text;
 
            bool venceu = false;
 
            for (int i = 0; i < 3; i++)
            {
                if (matriz[i, 0] == sinal && matriz[i, 1] == sinal && matriz[i, 2] == sinal)
                {
                    venceu = true;
                    break;
                }
                if (matriz[0, i] == sinal && matriz[1, i] == sinal && matriz[2, i] == sinal)
                {
                    venceu = true;
                    break;
                }
            }
 
            if (matriz[0, 0] == sinal && matriz[1, 1] == sinal && matriz[2, 2] == sinal)
            {
                venceu = true;
            }
            if (matriz[0, 2] == sinal && matriz[1, 1] == sinal && matriz[2, 0] == sinal)
            {
                venceu = true;
            }
 
            if (venceu)
            {
                MessageBox.Show("Fim de jogo");
            }
 
            if (contVelha == 9)
            {
                MessageBox.Show("Velha");
            }
        }
 
        private void btnReiniciar_Click(object sender, EventArgs e)
        {
            for (int l = 0; l < 3; l++)
            {
                for (int c = 0; c < 3; c++)
                {
                    matriz[l, c] = "";
                }
            }
            contVelha = 0;
 
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            btn0.Text = "";
        }
    }
}
 

saving score / loading statistics ...