Advertisement
Guest User

Jogo.java

a guest
Mar 20th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Jogo extends Diretrizes {
  4.    
  5.     protected static final int VITORIA = 3;
  6.     protected static final int DERROTA = 7;
  7.     protected static final int ULTIMA_RODADA = 6;
  8.    
  9.     private Scanner input = new Scanner( System.in );
  10.    
  11.    
  12.     public void Principal() {
  13.        
  14.         JogadasCPU CPU = new JogadasCPU();                 
  15.         super.mostraArray();
  16.        
  17.         System.out.print( "Digite um número de 0 a 2 : " );
  18.         int linha = input.nextInt();
  19.  
  20.         System.out.print( "Digite outro número de 0 a 2 : " );
  21.         int coluna = input.nextInt();
  22.        
  23.         System.out.println();
  24.            
  25.         super.setArray( linha, coluna );
  26.        
  27.         if ( this.statusJogo() == VITORIA )
  28.             this.vitoria();
  29.        
  30.         if ( super.rodada == ULTIMA_RODADA )
  31.             this.empate();
  32.        
  33.         CPU.PrincipalCPU( linha, coluna );
  34.  
  35.        
  36.  
  37.         }
  38.  
  39.     public void Rodada() {
  40.        
  41.         super.rodada++;
  42.        
  43.         if ( this.statusJogo() == DERROTA )
  44.             this.derrota();
  45.            
  46.         this.Principal();
  47.            
  48.     }
  49.    
  50.    
  51.     public int statusJogo() {
  52.         int testaVitoria = 0;
  53.        
  54.         for ( int a = 1; a < 3; a++ ) {
  55.            
  56.             for ( int b = 0; b < 3; b++ ) {
  57.                 if ( array[b][0] == a && array[b][1] == a && array[b][2] == a )
  58.                     testaVitoria = (a == 1) ? 3 : (a == 2) ? 7 : 0;
  59.                 if ( array[0][b] == a && array[1][b] == a && array[2][b] == a )
  60.                     testaVitoria = (a == 1) ? 3 : (a == 2) ? 7 : 0;
  61.                 }
  62.                
  63.             if ( array[0][0] == a && array[1][1] == a && array[2][2] == a )
  64.                 testaVitoria = (a == 1) ? 3 : (a == 2) ? 7 : 0;
  65.             if ( array[2][0] == a && array[1][1] == a && array[0][2] == a )
  66.                 testaVitoria = (a == 1) ? 3 : (a == 2) ? 7 : 0;
  67.  
  68.         }
  69.        
  70.         return testaVitoria;
  71.  
  72.     }
  73.    
  74.     public void derrota() {
  75.        
  76.         super.mostraArray();
  77.         System.out.println( "Você perdeu! :( " ); 
  78.         this.fimDeJogo();
  79.     }
  80.    
  81.     public void vitoria() {
  82.    
  83.         super.mostraArray();
  84.         System.out.println( "Parabéns! Você ganhou!" );
  85.         this.fimDeJogo();  
  86.        
  87.     }
  88.    
  89.     public void empate() {
  90.        
  91.         super.mostraArray();
  92.         System.out.println( "VIXI! deu véia." );
  93.         this.fimDeJogo();
  94.            
  95.     }
  96.    
  97.     private void reiniciar() {
  98.        
  99.         super.rodada = 1;
  100.         int contador = 0;
  101.        
  102.         for ( int x = 0; x < 3; x++ ) {
  103.             for ( int y = 0; y < 3; y++ ) {
  104.                 super.array[x][y] = 0;
  105.             }
  106.             contador++;
  107.         }
  108.        
  109.         if ( contador == 3 )
  110.             this.Rodada();
  111.     }
  112.    
  113.     public void fimDeJogo() {
  114.        
  115.         System.out.print( "\nDeseja reiniciar a partida? (s/n) : " );
  116.         char escolha = input.next().charAt(0);
  117.        
  118.         if ( escolha == 's' )
  119.             this.reiniciar();
  120.         if ( escolha == 'n' )
  121.             System.exit(0);
  122.         else {
  123.             System.out.print( "\nEntrada inválida.\n" );
  124.             this.fimDeJogo();
  125.         }
  126.    
  127.            
  128.            
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement