Advertisement
LightProgrammer000

Jogo da Velha

Apr 8th, 2020
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. /*
  2.  * Jogo da Velha
  3.    
  4.     ---------------------------------------------------------
  5.         1   2   3           1   2   3           1   2   3    
  6.     1   X               1       X           1           X
  7.     2   X               2       X           2           X
  8.     3   X               3       X           3           X
  9.     ---------------------------------------------------------
  10.  
  11.     ---------------------------------------------------------
  12.         1   2   3           1   2   3           1   2   3    
  13.     1   X   X   X       1                   1          
  14.     2                   2   X   X   X       2          
  15.     3                   3                   3   X   X   X
  16.     ---------------------------------------------------------
  17.  
  18.     ---------------------------------------------------------
  19.         1   2   3           1   2   3          
  20.     1   X               1           X            
  21.     2       X           2       X                  
  22.     3           X       3   X                      
  23.     ---------------------------------------------------------
  24.  */
  25. package Tarefas_Jogos;
  26.  
  27. // Bibliotecas
  28. import java.util.Scanner;
  29.  
  30. public class Jogo_da_Velha
  31. {
  32.     public static void main(String[] args)
  33.     {
  34.         try
  35.         {
  36.             jogar();            
  37.         }
  38.        
  39.         catch (Exception e)
  40.         {
  41.             //System.err.println(e);
  42.         }  
  43.     }
  44.  
  45.     // Metodo: Programa principal
  46.     private static void jogar()
  47.     {
  48.         // Variavel de controle
  49.         int contagem_jogadas = 0;
  50.         char vencedor = 'A';
  51.         char jogador = 'X';
  52.  
  53.         // Variaveis: Matriz Tabuleiro
  54.         int linha, coluna;
  55.         char tabuleiro [][] = new char[3][3];
  56.  
  57.         // Instanciacao
  58.         Scanner ent = new Scanner(System.in);
  59.  
  60.         // Chamada de metodo: Inicializando vetor com '*'
  61.         iniciar_tabuleiro(tabuleiro);
  62.         exibir_tabuleiro(tabuleiro);
  63.  
  64.         // Estrutura de repeticao: Jogadas do jogador
  65.         while (vencedor != 'X' && vencedor != 'O')
  66.         {
  67.             System.out.printf("\n# Vez do jogador \"%c\"\n\n", jogador);
  68.  
  69.             System.out.print("# Linha: ");
  70.             linha = ent.nextInt();
  71.  
  72.             System.out.print("# Coluna: ");
  73.             coluna = ent.nextInt();
  74.  
  75.             // Estrutura de decisao: 'Linha e Coluna' entre '1 e 3'
  76.             if ( (linha >= 1 && linha <= 3) && (coluna >= 1 && coluna <= 3) )
  77.             {
  78.                 // Linha: 0, 1, 2 <<==>> Coluna: 0, 1, 2
  79.                 tabuleiro[linha - 1][coluna - 1] = jogador;
  80.                
  81.                 // Estruturas de decisao:
  82.                 if (contagem_jogadas == 9)
  83.                 {
  84.                     System.out.println("\n Empatou");
  85.                     vencedor = 'E';
  86.                 }
  87.  
  88.                 else
  89.                 {
  90.                     // Estrutura de decisao: Troca de jogador
  91.                     if (jogador == 'X')
  92.                     {
  93.                         jogador = 'O';
  94.                     }
  95.  
  96.                     else
  97.                     {
  98.                         jogador = 'X';
  99.                     }    
  100.                    
  101.                     // Chamda de metodo: Mostrar tabuleiro
  102.                     exibir_tabuleiro(tabuleiro);
  103.                 }
  104.  
  105.                 // Analise de resultado
  106.                 vencedor = analise_resultado(tabuleiro);
  107.  
  108.                 // Incremento: Contagem de jogadas
  109.                 contagem_jogadas++;
  110.             }  
  111.  
  112.             else
  113.             {
  114.                 System.out.println("\n# Posicao nao existe");
  115.             }
  116.         }
  117.        
  118.         System.out.println("\n -------------------------");
  119.         System.out.println(" # Vencedor: " + vencedor);
  120.         System.out.println(" -------------------------");
  121.     }
  122.  
  123.     // Metodo: Exibir tabuleiro
  124.     private static void exibir_tabuleiro(char tabuleiro[][])
  125.     {
  126.         System.out.println("\n ========== Tabuleiro ========== \n");
  127.         for (int i = 0; i < tabuleiro.length; i++)
  128.         {
  129.             for (int j = 0; j < tabuleiro[i].length; j++)
  130.             {
  131.                 System.out.print("\t" + tabuleiro[i][j]);
  132.             }
  133.  
  134.             System.out.println("\n");
  135.         }
  136.  
  137.         System.out.println(" ===============================");
  138.     }
  139.  
  140.     // Metodo: Iniciando tabuleiro
  141.     private static void iniciar_tabuleiro(char tabuleiro[][])
  142.     {
  143.         for (int i = 0; i < tabuleiro.length; i++)
  144.         {
  145.             for (int j = 0; j < tabuleiro[i].length; j++)
  146.             {
  147.                 tabuleiro[i][j] = '*';
  148.             }
  149.         }
  150.     }
  151.  
  152.     // Metodo: Analisando o vencesor
  153.     private static char analise_resultado(char tabuleiro[][])
  154.     {
  155.         // Estrutura de repeticao
  156.         for (int i = 0; i < tabuleiro.length; i++)
  157.         {
  158.             // Estrutura de decisao: Linha
  159.             if ( (tabuleiro[i][0] == tabuleiro[i][1]) && (tabuleiro[i][1] == tabuleiro[i][2]) )
  160.             {
  161.                 return tabuleiro[i][0];
  162.             }
  163.            
  164.             // Estrutura de decisao: Coluna
  165.             else if ( (tabuleiro[0][i] == tabuleiro[1][i]) && (tabuleiro[1][i] == tabuleiro[2][i]) )
  166.             {
  167.                 return tabuleiro[0][i];
  168.             }
  169.         }
  170.        
  171.         // Estrutura de decisao: Diagonal principal
  172.         if ( (tabuleiro[0][0] == tabuleiro[1][1]) && (tabuleiro[1][1] == tabuleiro[2][2]) )
  173.         {
  174.             return tabuleiro[0][0];
  175.         }
  176.        
  177.         // Estrutura de decisao: Diagonal secundaria
  178.         else if ( (tabuleiro[0][2] == tabuleiro[1][1]) && (tabuleiro[1][1] == tabuleiro[2][0]) )
  179.         {
  180.             return tabuleiro[0][2];
  181.         }
  182.  
  183.         return 0;
  184.  
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement