quarta-feira, 5 de julho de 2017

Jogo da velha em C# com inteligência artificial

Hoje estarei postando um tutorial que irá ensinar como programar um jogo da velha em C# utilizando o Windows forms do Visual Studio, que terá uma inteligência artificial.
Se você acompanha o blog, você deve se lembrar que eu já havia postado um tutorial de como criar um jogo da velha para dois jogadores, certo? Caso não tenha visto clique aqui para conferir!
No tutorial anterior, o jogo da velha é jogado por duas pessoas reais, portanto no tutorial de hoje irei mostrar uma forma de programar um jogo da velha onde o outro jogador é uma inteligência artificial, onde basicamente o ''computador irá pensar'' para jogar.

Para começar o tutorial, abra seu Visual Studio ou Visual C# Express e vá em ''File >> New Project >> Windows forms application >> em 'Name' digite "JogodaVelhaIA" e dê ok.
Como você fez no tutorial anterior, crie o local do jogo através do formulário arrastando do toolbox 9 botões e os organizando em uma grade 3x3, também arraste algumas labels para registrar as pontuações de ambos jogadores e mais um botão para limpar a tela:



Após tudo isso, pressione F7 em seu teclado e abra o editor de scripts.
Dentro da classe do formulário vamos criar nossas variáveis:

public partial class Form1 : Form
    {
}

Criaremos as variáveis boelanas para determinar a vez do jogador e a variável que define se os botões estão habilitados.
As variáveis inteiras serão: x_vitoria, o_vitoria, empates, rodadas, e vencedor:



public partial class Form1 : Form { bool vez, button_disable; int x_vitoria = 0, o_vitoria = 0 , empates = 0, rodadas = 0, vencedor = 0; public Form1() { InitializeComponent(); }

Agora pense em como você pensaria enquanto joga este jogo: Você utiliza três pensamentos: O ofensivo, onde você marcará nas lacunas os locais onde você tem possibilidades de vitória, o defensivo, onde você tenta impedir que o outro jogador vença o jogo, e a aleatoriedade, principalmente quando você começa e normalmente o local onde você vai marcar é escolhido ao acaso, bem como no final do jogo onde não há o que fazer e de qualquer forma o jogo dará empate, você escolhe qualquer lacuna para preencher, aleatoriamente, certo? Pois então, vamos utilizar essas três estratégias para a inteligência artificial também, para que ela fique o mais perto da realidade de um jogador humano possível.

Cada uma dessas três estratégias no C# serão interpretadas a partir de métodos, ok? Portanto criaremos os três seguintes métodos: Ofensivo, Defensivo e Randomico.
Também criarmos um método que verificará se algum jogador venceu a partida:


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace JogoDaVelhaIA { public partial class Form1 : Form { bool vez, button_disable; int x_vitoria = 0, o_vitoria = 0, empates = 0, rodadas = 0, vencedor = 0; public Form1() { InitializeComponent(); } private void Ofensivo() { } private void Defensivo() { } private void Randomico() { } private void checagemVencedor() { } } }

Porém, antes de programarmos de fato a inteligência artificial, vamos programar os botões que o jogador real poderá acessar ao clicá-los?
Pois bem, volte para a aba do design do formulário e dê um duplo clique no primeiro botão, abrindo o editor de script com o método deste botão. Dentro deste método estabeleça uma condição que irá verificar três validações:

1º Se é a vez do jogador real, onde a variável 'vez' deve ser 'false';
2º Se o botão está habilitado, ou seja, a variável 'button_disable' deve estar false;
3º Se esta lacuna ainda não está preenchida, ou seja, se button1.Text está vazio ("")

Caso todas essas validações forem satisfeitas, coloque para que button1.Text seja "X", a variável ''rodada'' aumente em 1, a variável 'vez' retorne true (para que o próximo jogador possa jogar) e chame o método de checagem de vitória:


using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace JogoDaVelhaIA { public partial class Form1 : Form { bool vez, button_disable; int x_vitoria = 0, o_vitoria = 0, empates = 0, rodadas = 0, vencedor = 0; public Form1() { InitializeComponent(); } private void Ofensivo() { } private void Defensivo() { } private void Randomico() { } private void checagemVencedor() { } private void button1_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button1.Text == "") { button1.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } } } 

Note que ao debugar o código (F5) e ao clicar no primeiro botão, você terá algo assim:



Para enfim programar os outros 8 botões, basta copiar essa condição e colar dentro dos métodos dos outros botões, lembrando de alterar onde está escrito ''button1'' pela numeração do botão correspondente.
Depois de programar todos os botões seu código deve estar assim:



using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace JogoDaVelhaIA { public partial class Form1 : Form { bool vez, button_disable; int x_vitoria = 0, o_vitoria = 0, empates = 0, rodadas = 0, vencedor = 0; public Form1() { InitializeComponent(); } private void Ofensivo() { } private void Defensivo() { } private void Randomico() { } private void checagemVencedor() { } private void button1_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button1.Text == "") { button1.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button2_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button2.Text == "") { button2.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button3_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button3.Text == "") { button3.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button4_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button4.Text == "") { button4.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button5_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button5.Text == "") { button5.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button6_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button6.Text == "") { button6.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button7_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button7.Text == "") { button7.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button8_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button8.Text == "") { button8.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button9_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button9.Text == "") { button9.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } } } 

Agora você pode clicar em qualquer botão que o "X" irá aparecer de acordo com o botão clicado, certo? Porém, a próxima vez agora deveria ser do jogador 2, que no caso seria a IA (inteligência artificial) mas como ainda não a programamos, o jogo não sucede.

Vá para o método de checagem de vitória e crie uma condição que verifique se está na vez da IA (vez == true) e se ainda o jogo não acabou, ou seja, a variável ''vencedor'' está igual a zero.
Se essas condições forem satisfeitas significa que está na vez do jogador 2 jogar, ou seja, vamos chamar o método da sua estratégia.
Mas qual estratégia vamos chamar primeiro?
O mais correto seria chamar o método "Ofensivo'' onde primeiramente a IA vai verificar se ela tem possibilidades de ganhar a partida, caso nenhuma das condições desse método sejam satisfeitas, ou seja, a IA não tem possibilidades de vencer no momento, ela vai querer verificar se o outro jogador tem possibilidade de ganhar, caso sim, ela irá impedi-lo, caso não, a única coisa que lhe resta é marcar num local aleatório, ou seja, a ordem de checagem para chamarmos os métodos será: 1) Ofensivo, 2) Defensivo, 3) Randômico.
Então dentro dessa condição chame o método "Ofensivo":

 private void checagemVencedor()
        {
            if (vez == true && vencedor == 0)
            {
                Ofensivo();
            }
        }

Vamos agora programar a jogada da IA caso ela almeje vencer, ou seja, vá ao método 'Ofensivo'.
Para verificar se ela pode vencer, ela vai verificar CADA possibilidade de vitória eminente que ela pode possuir.
Preste atenção neste esboço:


Note que a IA poderá verificar que ela possui chances de vencer marcando no button3, caso ela já tenha marcado no botão 1 e 2 OU no botão 6 e 9?
Pois bem, a nossa primeira condição vai literalmente validar esses dois casos (botão 1 e 2 ou botão 6 e 9, levando em consideração que o próprio botão 3 (onde ela deverá marcar) deve estar vazio.
Seu método ficará assim:


private void Ofensivo() { if (button1.Text == "O" && button2.Text == "O" && button3.Text == "" || button6.Text == "O" && button6.Text == "O" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } }

Outra forma da IA vencer seria com esses tipos de jogadas:


Ou seja, tendo marcado os botões 1 e 3 OU os botões 5 e 8, ela deverá marcar no botão 2:




private void Ofensivo() { if (button1.Text == "O" && button2.Text == "O" && button3.Text == "" || button6.Text == "O" && button6.Text == "O" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button3.Text == "O" && button2.Text == "" || button5.Text == "O" && button8.Text == "O" && button2.Text == "") { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } }

Também pode vencer se fizer estas jogadas:


Ou seja, marcar nos botões 2 e 3 OU 4 e 7, deverá marcar no botão 1:


private void Ofensivo() { if (button1.Text == "O" && button2.Text == "O" && button3.Text == "" || button6.Text == "O" && button6.Text == "O" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button3.Text == "O" && button2.Text == "" || button5.Text == "O" && button8.Text == "O" && button2.Text == "") { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "O" && button3.Text == "O" && button1.Text == "" || button4.Text == "O" && button7.Text == "O" && button1.Text == "") { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } }

Por fim, nós teremos algumas outras possibilidades de vitórias nas outras horizontais e nas diagonais, basta ir verificando como fizemos mais acima.
Porém, gostaríamos que esse código realmente verificasse as 16 possibilidades, portanto precisamos de um contador num loop que rode o esse método 17 vezes (o último loop irá determinar que nenhuma das condições foi satisfeita e irá chamar o método defensivo).
Após colocar as condições num loop com um contador até o 17 e criar todas as verificações, seu método deverá estar assim:




private void Ofensivo() { for (int i = 1; i <= 17; i++) { if (i == 17 && vez == true) { Defensivo(); } if (button1.Text == "O" && button2.Text == "O" && button3.Text == "" || button6.Text == "O" && button6.Text == "O" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button3.Text == "O" && button2.Text == "" || button5.Text == "O" && button8.Text == "O" && button2.Text == "") { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "O" && button3.Text == "O" && button1.Text == "" || button4.Text == "O" && button7.Text == "O" && button1.Text == "") { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "O" && button5.Text == "O" && button6.Text == "" || button3.Text == "O" && button9.Text == "O" && button6.Text == "") { button6.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "O" && button6.Text == "O" && button4.Text == "" || button1.Text == "O" && button7.Text == "O" && button4.Text == "") { button4.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "O" && button6.Text == "O" && button5.Text == "" || button2.Text == "O" && button8.Text == "O" && button5.Text == "") { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "O" && button8.Text == "O" && button9.Text == "" || button3.Text == "O" && button6.Text == "O" && button9.Text == "") { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button8.Text == "O" && button9.Text == "O" && button7.Text == "" || button1.Text == "O" && button4.Text == "O" && button7.Text == "") { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "O" && button9.Text == "O" && button8.Text == "" || button2.Text == "O" && button5.Text == "O" && button8.Text == "") { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "O" && button4.Text == "O" && button8.Text == "") { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "O" && button8.Text == "O" && button2.Text == "") { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button5.Text == "O" && button9.Text == "") { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button3.Text == "O" && button5.Text == "O" && button7.Text == "") { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "O" && button9.Text == "O" && button1.Text == "") { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "O" && button5.Text == "O" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button9.Text == "O" && button5.Text == "" || button3.Text == "O" && button7.Text == "O" && button5.Text == "") { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } } }

Para programar o método defensivo é mais simples, basta copiar todo o método ofensivo, e só alterar a variável do loop do contador e nas checagens substituir onde está "O" por "X". A condição que verifica se nenhuma das validações foram cumpridas só deve ter alterada a variável do contador e chamar o método randômico.
Seu código vai ficar assim: private void Defensivo() { for (int r = 1; r <= 17; r++) { if (r == 17 && vez == true) { Randomico(); } if (button1.Text == "X" && button2.Text == "X" && button3.Text == "" || button6.Text == "X" && button6.Text == "X" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "X" && button3.Text == "X" && button2.Text == "" || button5.Text == "X" && button8.Text == "X" && button2.Text == "") { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "X" && button3.Text == "X" && button1.Text == "" || button4.Text == "X" && button7.Text == "X" && button1.Text == "") { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "X" && button5.Text == "X" && button6.Text == "" || button3.Text == "X" && button9.Text == "X" && button6.Text == "") { button6.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "X" && button6.Text == "X" && button4.Text == "" || button1.Text == "X" && button7.Text == "X" && button4.Text == "") { button4.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "X" && button6.Text == "X" && button5.Text == "" || button2.Text == "X" && button8.Text == "X" && button5.Text == "") { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "X" && button8.Text == "X" && button9.Text == "" || button3.Text == "X" && button6.Text == "X" && button9.Text == "") { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button8.Text == "X" && button9.Text == "X" && button7.Text == "" || button1.Text == "X" && button4.Text == "X" && button7.Text == "") { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "X" && button9.Text == "X" && button8.Text == "" || button2.Text == "X" && button5.Text == "X" && button8.Text == "") { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "X" && button4.Text == "X" && button8.Text == "") { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "X" && button8.Text == "X" && button2.Text == "") { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "X" && button5.Text == "X" && button9.Text == "") { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button3.Text == "X" && button5.Text == "X" && button7.Text == "") { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "X" && button9.Text == "X" && button1.Text == "") { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "X" && button5.Text == "X" && button3.Text == "") { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "X" && button9.Text == "X" && button5.Text == "" || button3.Text == "X" && button7.Text == "X" && button5.Text == "") { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } } }

Agora vamos programar o método randômico, que irá definir uma marcação aleatória para a IA:
Primeiro deve-se verificar se está na vez do jogador 2:

private void Randomico()
        {
            if (vez == true)
            {
            }
        }

Vamos também criar mais um loop para que seja verificado quais das 9 lacunas do jogo estão vazias:


private void Randomico() { bool looping = false; if (vez == true) { looping = true; } while (looping != false) { } }

Agora iremos utilizar o método Random da seguinte forma:

Random rdn = new Random();
                int randomizar = rdn.Next(1, 9);

Vamos utilizar a estrutura condição switch case para validar cada uma das randomizações:

private void Randomico() { bool looping = false; if (vez == true) { looping = true; } while (looping != false) { Random rdn = new Random(); int randomizar = rdn.Next(1, 9); switch (randomizar) { case 1: if (button1.Text == "" && vez == true) { button1.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 2: if (button2.Text == "" && vez == true) { button2.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 3: if (button3.Text == "" && vez == true) { button3.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 4: if (button4.Text == "" && vez == true) { button4.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 5: if (button5.Text == "" && vez == true) { button5.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 6: if (button6.Text == "" && vez == true) { button6.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 7: if (button7.Text == "" && vez == true) { button7.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 8: if (button8.Text == "" && vez == true) { button8.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 9: if (button9.Text == "" && vez == true) { button9.Text = "O"; rodadas++; looping = false; vez = false; checagemVencedor(); } break; } } }

Neste momento debugue o código e veja que a IA utiliza as três estratégias propostas de forma eficiente:



Veja as três jogadas no vídeo acima, a IA se defende, ataca e também joga aleatoriamente no início.

Note que agora estamos quase no final deste tutorial, pois precisamos programar as checagens de vitórias ou empates dos jogadores.
Dê uma olhada que no tutorial anterior para ver como esta checagem ocorre, não irei entrar nesse mérito por aqui, pois o foco do tutorial é ensinar a programar a IA, os outros métodos poderão ser encontrados no tutorial já linkado nesta postagem.
A checagem de vitórias e empate deve estar assim:

private void Havencedor() { } private void checagemVencedor() { if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X") { vencedor = 1; Havencedor(); } if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") { vencedor = 1; Havencedor(); } if (button7.Text == "X" && button8.Text == "X" && button9.Text == "X") { vencedor = 1; Havencedor(); } if (button1.Text == "X" && button4.Text == "X" && button7.Text == "X") { vencedor = 1; Havencedor(); } if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") { vencedor = 1; Havencedor(); } if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X") { vencedor = 1; Havencedor(); } if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X") { vencedor = 1; Havencedor(); } if (button3.Text == "X" && button5.Text == "X" && button7.Text == "X") { vencedor = 1; Havencedor(); } // Player 2 if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O") { vencedor = 2; Havencedor(); } if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") { vencedor = 2; Havencedor(); } if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O") { vencedor = 2; Havencedor(); } if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O") { vencedor = 2; Havencedor(); } if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") { vencedor = 2; Havencedor(); } if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O") { vencedor = 2; Havencedor(); } if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O") { vencedor = 2; Havencedor(); } if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O") { vencedor = 2; Havencedor(); } if (rodadas == 9 && vencedor == 0) { MessageBox.Show("Empate"); empate++; label4.Text = Convert.ToString(empate); button_disable = true; } if (vez == true && vencedor == 0 && rodadas !=9) { Ofensivo(); } }

Veja que no código, eu irei um método chamado "Havencedor'', e ele é chamado quando algum jogador ganha a partida, correto?
O que vamos  escrever dentro desse código é simples: Uma condição que verifica quem foi o vencedor, se a variável 'vencedor' for 1 foi o jogador real, se for 2 foi a IA, e dependendo de quem venceu, uma mensagem em popup irá anunciar o vencedor, a variável de vitórias do ganhador aumentará e os botões ficarão desabilitados:

private void Havencedor() { if (vencedor == 1) { x_vitoria++; label6.Text = Convert.ToString(x_vitoria); MessageBox.Show("Jogador 1 venceu!"); button_disable = true; } else if (vencedor == 2) { o_vitoria++; label5.Text = Convert.ToString(o_vitoria); MessageBox.Show("Jogador 2 venceu!"); button_disable = true; } }

O último passo para nosso programa ficar pronto é programar o botão "Limpar", onde ao clicar aqui, o jogo irá reiniciar para uma nova partida:


private void button10_Click(object sender, EventArgs e) { button1.Text = ""; button2.Text = ""; button3.Text = ""; button4.Text = ""; button5.Text = ""; button6.Text = ""; button7.Text = ""; button8.Text = ""; button9.Text = ""; rodadas = 0; button_disable = false; if (vencedor == 1 || vencedor == 0) { vez = false; vencedor = 0; } else { vez = true; vencedor = 0; checagemVencedor(); } }

Debugue o código e perceba que ele funciona corretamente, por exemplo:


Por fim, se você fez tudo conforme o tutorial, seu código deve estar assim:



using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace JogodaVelhaIA { public partial class Form1 : Form { bool vez, button_disable; int x_vitoria, o_vitoria, empate = 0, rodadas = 0, vencedor = 0; public Form1() { InitializeComponent(); } private void Ofensivo() { if (vez == true) { for (int i = 1; i <= 17; i++) { if (i == 17 && vez == true) { Defensivo(); } if (button1.Text == "O" && button2.Text == "O" && button3.Text == "" && vez == true || button6.Text == "O" && button6.Text == "O" && button3.Text == "" && vez == true) { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button3.Text == "O" && button2.Text == "" && vez == true || button5.Text == "O" && button8.Text == "O" && button2.Text == "" && vez == true) { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "O" && button3.Text == "O" && button1.Text == "" && vez == true || button4.Text == "O" && button7.Text == "O" && button1.Text == "" && vez == true) { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "O" && button5.Text == "O" && button6.Text == "" && vez == true || button3.Text == "O" && button9.Text == "O" && button6.Text == "" && vez == true) { button6.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "O" && button6.Text == "O" && button4.Text == "" && vez == true || button1.Text == "O" && button7.Text == "O" && button4.Text == "" && vez == true) { button4.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "O" && button6.Text == "O" && button5.Text == "" && vez == true || button2.Text == "O" && button8.Text == "O" && button5.Text == "" && vez == true) { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "O" && button8.Text == "O" && button9.Text == "" && vez == true || button3.Text == "O" && button6.Text == "O" && button9.Text == "" && vez == true) { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button8.Text == "O" && button9.Text == "O" && button7.Text == "" && vez == true || button1.Text == "O" && button4.Text == "O" && button7.Text == "" && vez == true) { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "O" && button9.Text == "O" && button8.Text == "" && vez == true || button2.Text == "O" && button5.Text == "O" && button8.Text == "" && vez == true) { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "O" && button4.Text == "O" && button8.Text == "" && vez == true) { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "O" && button8.Text == "O" && button2.Text == "" && vez == true) { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button5.Text == "O" && button9.Text == "" && vez == true) { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button3.Text == "O" && button5.Text == "O" && button7.Text == "" && vez == true) { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "O" && button9.Text == "O" && button1.Text == "" && vez == true) { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "O" && button5.Text == "O" && button3.Text == "" && vez == true) { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "O" && button9.Text == "O" && button5.Text == "" && vez == true || button3.Text == "O" && button7.Text == "O" && button5.Text == "" && vez == true) { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } } } } private void Defensivo() { if (vez == true) { for (int r = 1; r <= 17; r++) { if (r == 17 && vez == true) { Randomico(); } if (button1.Text == "X" && button2.Text == "X" && button3.Text == "" && vez == true || button6.Text == "X" && button6.Text == "X" && button3.Text == "" && vez == true) { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "X" && button3.Text == "X" && button2.Text == "" && vez == true || button5.Text == "X" && button8.Text == "X" && button2.Text == "" && vez == true) { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "X" && button3.Text == "X" && button1.Text == "" && vez == true || button4.Text == "X" && button7.Text == "X" && button1.Text == "" && vez == true) { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "X" && button5.Text == "X" && button6.Text == "" && vez == true || button3.Text == "X" && button9.Text == "X" && button6.Text == "" && vez == true) { button6.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "X" && button6.Text == "X" && button4.Text == "" && vez == true || button1.Text == "X" && button7.Text == "X" && button4.Text == "" && vez == true) { button4.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "X" && button6.Text == "X" && button5.Text == "" && vez == true || button2.Text == "X" && button8.Text == "X" && button5.Text == "" && vez == true) { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "X" && button8.Text == "X" && button9.Text == "" && vez == true || button3.Text == "X" && button6.Text == "X" && button9.Text == "" && vez == true) { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button8.Text == "X" && button9.Text == "X" && button7.Text == "" && vez == true || button1.Text == "X" && button4.Text == "X" && button7.Text == "" && vez == true) { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "X" && button9.Text == "X" && button8.Text == "" && vez == true || button2.Text == "X" && button5.Text == "X" && button8.Text == "" && vez == true) { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button2.Text == "X" && button4.Text == "X" && button8.Text == "" && vez == true) { button8.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button4.Text == "X" && button8.Text == "X" && button2.Text == "" && vez == true) { button2.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "X" && button5.Text == "X" && button9.Text == "" && vez == true) { button9.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button3.Text == "X" && button5.Text == "X" && button7.Text == "" && vez == true) { button7.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button5.Text == "X" && button9.Text == "X" && button1.Text == "" && vez == true) { button1.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button7.Text == "X" && button5.Text == "X" && button3.Text == "" && vez == true) { button3.Text = "O"; rodadas++; vez = false; checagemVencedor(); } if (button1.Text == "X" && button9.Text == "X" && button5.Text == "" && vez == true || button3.Text == "X" && button7.Text == "X" && button5.Text == "" && vez == true) { button5.Text = "O"; rodadas++; vez = false; checagemVencedor(); } } } } private void Randomico() { bool looping = false; if (vez == true) { looping = true; } while (looping != false) { Random rdn = new Random(); int randomizar = rdn.Next(1, 9); switch (randomizar) { case 1: if (button1.Text == "" && vez == true) { button1.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 2: if (button2.Text == "" && vez == true) { button2.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 3: if (button3.Text == "" && vez == true) { button3.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 4: if (button4.Text == "" && vez == true) { button4.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 5: if (button5.Text == "" && vez == true) { button5.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 6: if (button6.Text == "" && vez == true) { button6.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 7: if (button7.Text == "" && vez == true) { button7.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 8: if (button8.Text == "" && vez == true) { button8.Text = "O"; rodadas++; vez = false; looping = false; checagemVencedor(); } break; case 9: if (button9.Text == "" && vez == true) { button9.Text = "O"; rodadas++; looping = false; vez = false; checagemVencedor(); } break; } } } private void Havencedor() { if (vencedor == 1) { x_vitoria++; label6.Text = Convert.ToString(x_vitoria); MessageBox.Show("Jogador 1 venceu!"); button_disable = true; } else if (vencedor == 2) { o_vitoria++; label5.Text = Convert.ToString(o_vitoria); MessageBox.Show("Jogador 2 venceu!"); button_disable = true; } } private void checagemVencedor() { if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X") { vencedor = 1; Havencedor(); } if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X") { vencedor = 1; Havencedor(); } if (button7.Text == "X" && button8.Text == "X" && button9.Text == "X") { vencedor = 1; Havencedor(); } if (button1.Text == "X" && button4.Text == "X" && button7.Text == "X") { vencedor = 1; Havencedor(); } if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X") { vencedor = 1; Havencedor(); } if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X") { vencedor = 1; Havencedor(); } if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X") { vencedor = 1; Havencedor(); } if (button3.Text == "X" && button5.Text == "X" && button7.Text == "X") { vencedor = 1; Havencedor(); } // Player 2 if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O") { vencedor = 2; Havencedor(); } if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O") { vencedor = 2; Havencedor(); } if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O") { vencedor = 2; Havencedor(); } if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O") { vencedor = 2; Havencedor(); } if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O") { vencedor = 2; Havencedor(); } if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O") { vencedor = 2; Havencedor(); } if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O") { vencedor = 2; Havencedor(); } if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O") { vencedor = 2; Havencedor(); } if (rodadas == 9 && vencedor == 0) { MessageBox.Show("Empate"); empate++; label4.Text = Convert.ToString(empate); button_disable = true; } if (vez == true && vencedor == 0 && rodadas !=9) { Ofensivo(); } } private void button1_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button1.Text == "") { button1.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button2_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button2.Text == "") { button2.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button3_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button3.Text == "") { button3.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button4_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button4.Text == "") { button4.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button5_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button5.Text == "") { button5.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button6_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button6.Text == "") { button6.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button7_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button7.Text == "") { button7.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button8_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button8.Text == "") { button8.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button9_Click(object sender, EventArgs e) { if (vez == false && button_disable == false && button9.Text == "") { button9.Text = "X"; rodadas++; vez = true; checagemVencedor(); } } private void button10_Click(object sender, EventArgs e) { button1.Text = ""; button2.Text = ""; button3.Text = ""; button4.Text = ""; button5.Text = ""; button6.Text = ""; button7.Text = ""; button8.Text = ""; button9.Text = ""; rodadas = 0; button_disable = false; if (vencedor == 1 || vencedor == 0) { vez = false; vencedor = 0; } else { vez = true; vencedor = 0; checagemVencedor(); } } } } 

E nosso tutorial chegou ao fim! Espero que tenham gostado da explicação, e até mais. :)