Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lp2.lab03;
- import java.util.Scanner;
- public class JogoDaVelhaModularizado{
- static char tabuleiro[][] = new char[3][3];
- static final char O='O';
- static final char X='X';
- static int jogada=0,j,i, maximo = 9;
- static boolean ganhou = false;
- static Scanner sc = new Scanner(System.in);
- public static void main(String args[]){
- for(i=0;i<3;i++)
- for(j=0;j<3;j++)
- tabuleiro[i][j]=' ';
- while(jogada<=maximo)
- {
- System.out.print(" JOGO DA VELHA \n");
- ExibeTabuleiro();
- System.out.println("\nINSIRA AS COORDENADAS");
- if((jogada%2)==1)System.out.print("PLAYER 2\nLINHA: ");
- else System.out.print("PLAYER 1\nLINHA: ");
- i = sc.nextInt();
- System.out.print("COLUNA: ");
- j = sc.nextInt();
- if(tabuleiro[i-1][j-1]==' ')
- {
- if((jogada%2)==1)tabuleiro[i-1][j-1]=X;
- else tabuleiro[i-1][j-1]=O;
- jogada++;
- }
- VerificaVitoria();
- if (ganhou)
- {
- ExibeTabuleiro();
- if (jogada % 2 == 0) System.out.print("\nJogador 2, VOCE VENCEU!!!");
- else System.out.print("\nJogador 1, VOCE VENCEU!!!");
- break;
- }
- if(jogada==9)
- {
- ExibeTabuleiro();
- System.out.print("\nPARTIDA EMPATADA");
- break;
- }
- }
- sc.close(); //fecha o objeto Scanner
- } //fecha o main
- private static void ExibeTabuleiro(){
- for(i=0;i<3;i++)
- {
- System.out.print(tabuleiro[i][0]+"|"+tabuleiro[i][1]+"|"+tabuleiro[i][2]);
- if(i<3-1)
- {
- System.out.print("\n------\n");
- }
- }
- } //fecha ExibeTabuleiro
- private static void VerificaVitoria(){
- for(i=0;i<3;i++)
- if (tabuleiro[i][0]== O && tabuleiro[i][1]== O && tabuleiro[i][2]== O) ganhou = true;
- for(j=0;j<3;j++)
- if (tabuleiro[0][j]== O && tabuleiro[1][j]== O && tabuleiro[2][j]== O) ganhou = true;
- if((tabuleiro[0][0]==O && tabuleiro[1][1]==O && tabuleiro[2][2]==O)||
- (tabuleiro[0][2]==O && tabuleiro[1][1]==O && tabuleiro[2][0]==O)) ganhou = true;
- for(i=0;i<3;i++)
- if (tabuleiro[i][0]== X && tabuleiro[i][1]== X && tabuleiro[j][2]== X) ganhou = true;
- for(j=0;j<3;j++)
- if (tabuleiro[0][j]== X && tabuleiro[1][j]== X && tabuleiro[2][j]== X) ganhou = true;
- if((tabuleiro[0][0]==X && tabuleiro[1][1]==X && tabuleiro[2][2]==X)||
- (tabuleiro[0][2]==X && tabuleiro[1][1]==X && tabuleiro[2][0]==X)) ganhou = true;
- }
- } // fecha a classe
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement