Skip to content

Instantly share code, notes, and snippets.

@wagfim
Created August 5, 2019 02:49
Show Gist options
  • Save wagfim/80aa8043b050ccfa8b1f7256f8249b32 to your computer and use it in GitHub Desktop.
Save wagfim/80aa8043b050ccfa8b1f7256f8249b32 to your computer and use it in GitHub Desktop.
Num tabuleiro de damas, determinar as posições das peças pretas que não podem se mover. Peças pretas representadas por 7, brancas por 1 e casas vazias por 0
public class Questao10_v4 {
public static void main(String[] args) {
int[][] tabuleiro = {
/* Lado Branco */
//0,1,2,3,4,5,6,7
//B,P,B,P,B,P,B,P
/*0 B*/ {0,0,0,0,0,0,0,0},
/*1 P*/ {0,0,0,0,0,0,0,0},
/*2 B*/ {0,0,0,0,0,0,0,0},
/*3 P*/ {0,0,0,0,0,0,0,0},
/*4 B*/ {0,0,0,0,0,0,0,0},
/*5 P*/ {0,0,0,0,0,0,7,0},
/*6 B*/ {0,0,0,0,0,7,0,1},
/*7 P*/ {0,0,0,0,0,0,0,0}
//P,B,P,B,P,B,P,B
/* Lado Preto */
};
int[][] todasPosicoes = localizarTodasPosicoes(tabuleiro);
exibirPosicoes(todasPosicoes);
System.out.println("Peças que não podem se mover");
boolean[][] pecasNaoPodemMover = filtrarPecasBloqueadas(tabuleiro, todasPosicoes);
exibirPosicoesBloqueadas(pecasNaoPodemMover, todasPosicoes);
}
public static int[][] localizarTodasPosicoes(int[][] tabuleiro) {
int cont = 0;
for (int i = 0; i < tabuleiro.length; i++) {
for (int j = 0; j < tabuleiro[i].length; j++) {
if(tabuleiro[i][j] == 7)
cont++;
}
}
int[][] posicoes = new int[cont][2];
int linha = 0;
for (int i = 0; i < tabuleiro.length; i++) {
for (int j = 0; j < tabuleiro[i].length; j++) {
if(tabuleiro[i][j] == 7) {
posicoes[linha][0] = i;
posicoes[linha][1] = j;
linha++;
}
}
}
return posicoes;
}
public static boolean[][] filtrarPecasBloqueadas(int[][] tabuleiro, int [][] todasPosicoes) {
boolean[][] matrizResultado = new boolean[todasPosicoes.length][2];
for (int i = 0; i < todasPosicoes.length; i++) {
int linha = todasPosicoes[i][0];
int coluna = todasPosicoes[i][1];
//x1
if (linha < 7 && coluna > 0 && coluna < 7) {
boolean sudoesteSimples = tabuleiro[linha + 1][coluna - 1] == 7;
boolean sudesteSimples = tabuleiro[linha + 1][coluna + 1] == 7;
if (coluna == 1)
sudoesteSimples = tabuleiro[linha + 1][coluna - 1] != 0;
if (coluna == 6)
sudesteSimples = tabuleiro[linha + 1][coluna + 1] != 0;
if (sudoesteSimples && sudesteSimples)
matrizResultado[i][0] = true;
if (linha < 6 && coluna > 1 && coluna < 6) {
boolean sudoesteComer1 = tabuleiro[linha + 1][coluna - 1] == 1;
boolean sudoesteComer2 = tabuleiro[linha + 2][coluna - 2] != 0;
if (sudesteSimples && sudoesteComer1 && sudoesteComer2)
matrizResultado[i][0] = true;
boolean sudesteComer1 = tabuleiro[linha + 1][coluna + 1] == 1;
boolean sudesteComer2 = tabuleiro[linha + 2][coluna + 2] != 0;
if (sudoesteSimples && sudesteComer1 && sudesteComer2)
matrizResultado[i][0] = true;
}
if (linha < 5 && coluna == 1) {
boolean sudesteComer1 = tabuleiro[linha + 1][coluna + 1] == 1;
boolean sudesteComer2 = tabuleiro[linha + 2][coluna + 2] != 0;
if (sudoesteSimples && sudesteComer1 && sudesteComer2)
matrizResultado[i][0] = true;
}
if (linha < 5 && coluna == 6) {
boolean sudoesteComer1 = tabuleiro[linha + 1][coluna - 1] == 1;
boolean sudoesteComer2 = tabuleiro[linha + 2][coluna - 2] != 0;
if (sudesteSimples && sudoesteComer1 && sudoesteComer1)
matrizResultado[i][0] = true;
}
}
if (linha < 7 && coluna == 0) {
boolean sudesteSimples = tabuleiro[linha + 1][coluna + 1] == 7;
if (sudesteSimples)
matrizResultado[i][0] = true;
if (linha < 6) {
boolean sudesteComer1 = tabuleiro[linha + 1][coluna + 1] == 1;
boolean sudesteComer2 = tabuleiro[linha + 2][coluna + 2] != 0;
if (sudesteComer1 && sudesteComer2)
matrizResultado[i][0] = true;
}
}
if (linha < 7 && coluna == 7) {
boolean sudoesteSimples = tabuleiro[linha + 1][coluna - 1] == 7;
if (sudoesteSimples)
matrizResultado[i][0] = true;
if (linha < 6) {
boolean sudoesteComer1 = tabuleiro[linha + 1][coluna - 1] == 1;
boolean sudoesteComer2 = tabuleiro[linha + 2][coluna - 2] != 0;
if (sudoesteComer1 && sudoesteComer2)
matrizResultado[i][0] = true;
}
}
//APENAS RAINHA
if (linha == 7 && coluna > 1) {
//x1
boolean noroesteSimples = tabuleiro[linha - 1][coluna - 1] == 7;
boolean nordesteSimples = tabuleiro[linha - 1][coluna + 1] == 7;
if (noroesteSimples && nordesteSimples)
matrizResultado[i][0] = true;
nordesteSimples = tabuleiro[linha - 1][coluna + 1] != 0;
//x3
boolean noroesteComer1 = tabuleiro[linha-1][coluna-1] == 1;
boolean noroesteComer2 = tabuleiro[linha-2][coluna-2] != 0;
if (nordesteSimples && noroesteComer1 && noroesteComer2)
matrizResultado[i][0] = true;
}
//nordeste simples, e nordeste comendo
if (linha == 7 && coluna == 0) {
boolean nordesteSimples = tabuleiro[linha - 1][coluna + 1] == 7;
if (nordesteSimples)
matrizResultado[i][0] = true;
}
if (linha == 7 && coluna < 6) {
boolean nordesteComer1 = tabuleiro[linha-1][coluna+1] == 1;
boolean nordesteComer2 = tabuleiro[linha-2][coluna+2] != 0;
if (coluna == 0)
if (nordesteComer1 && nordesteComer2)
matrizResultado[i][0] = true;
if (coluna > 1) {
boolean noroesteSimples = tabuleiro[linha - 1][coluna - 1] == 7;
if (noroesteSimples && nordesteComer1 && nordesteComer2)
matrizResultado[i][0] = true;
}
}
}
return matrizResultado;
}
public static void exibirPosicoes(int[][] matriz) {
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[i].length; j++) {
if(matriz[i][0] != -1)
System.out.print("("+matriz[i][j++]+" ; "+matriz[i][j]+") ");
}
}
System.out.println();
}
private static void exibirPosicoesBloqueadas(boolean[][] pecasNaoPodemMover, int[][] todasPosicoes) {
for (int i = 0; i < pecasNaoPodemMover.length; i++) {
for (int j = 0; j < pecasNaoPodemMover[i].length; j++) {
if(pecasNaoPodemMover[i][0])
System.out.print("("+todasPosicoes[i][j++]+" ; "+todasPosicoes[i][j]+") ");
}
}
System.out.println();}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment