Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void jogoDaVelha2Players(char matriz[][]) {
- System.out.print("JOGO DA VELHA 2 PLAYERS\n");
- int jogador = 1;
- int linha;
- int coluna;
- int contador = 0;
- while(!acabou(matriz)) {
- if(contador == 9) {
- System.out.println("\nVelha!");
- break;
- }
- System.out.print("\nDigite a linha (jogador " + jogador + "): ");
- linha = in.nextInt();
- System.out.print("Digite a coluna (jogador " + jogador + "): ");
- coluna = in.nextInt();
- if(matriz[linha][coluna] == 'x' || matriz[linha][coluna] == 'o') {
- System.out.println("Esta posição já possui valor! Por favor, digite outra.");
- continue;
- }
- if(!(linha > 2 || coluna > 2 || linha < 0 || coluna < 0)) {
- if(jogador == 1) {
- matriz[linha][coluna] = 'x';
- jogador = 2;
- } else {
- matriz[linha][coluna] = 'o';
- jogador = 1;
- }
- listaMatrizJogoDaVelha(matriz);
- contador++;
- } else {
- System.out.print("Erro! Por favor, digite valores de 0 a 2.");
- }
- }
- }
- public static boolean acabou(char matriz[][]) {
- ArrayList<Character> linha = new ArrayList<>();
- ArrayList<Character> coluna = new ArrayList<>();
- ArrayList<Character> diagonalPrincipal = new ArrayList<>();
- ArrayList<Character> diagonalSecundaria = new ArrayList<>();
- for(int i=0; i<matriz.length; i++) {
- linha.clear();
- coluna.clear();
- for(int j=0; j<matriz.length; j++) {
- linha.add(matriz[i][j]);
- coluna.add(matriz[j][i]);
- if(i == j) {
- diagonalPrincipal.add(matriz[i][j]);
- }
- if(i + j == 2) {
- diagonalSecundaria.add(matriz[i][j]);
- }
- }
- if(verificaVitoria(linha) || verificaVitoria(diagonalPrincipal) || verificaVitoria(diagonalSecundaria) || verificaVitoria(coluna)) {
- return true;
- }
- }
- return false;
- }
- public static boolean verificaVitoria(ArrayList<Character> sequencia) {
- int ocorrenciasX = Collections.frequency(sequencia, 'x');
- int ocorrenciasO = Collections.frequency(sequencia, 'o');
- if(ocorrenciasX == 3) {
- System.out.println("Jogador 1 venceu!");
- return true;
- } else if(ocorrenciasO == 3) {
- System.out.println("Jogador 2 venceu!");
- return true;
- } else {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement