Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tps.tp1.pack3Arrays;
- import java.util.Scanner;
- public class P04Xadrez {
- /**
- * Conceba o programa P04Xadrez que, no contexto de um tabuleiro de xadrez,
- * posicione de forma aleatória uma torre, um bispo e um cavalo, mas de
- * forma a não haver ataques entre as peças. Assim para cada peça deve-se
- * guardar as suas coordenadas x e y e comparar se há ataque com as peças já
- * processadas. A comparação deve ser realizada somente com base nas
- * coordenadas das peças. Em caso de ataque deve gerar novas coordenadas e
- * voltar a testar, até não haver qualquer ataque. No final deve-se mostrar
- * o tabuleiro em que a torre é visualizada com um ‘T’, o bispo com um ‘B’,
- * o cavalo com um ‘C’, cada posição que não sofra qualquer ataque deve
- * conter um ‘o’, cada posição com um só ataque deve conter um ‘-‘, e cada
- * posição com pelo menos dois ataques deve conter um ‘+’. O programa deverá
- * gerar e mostrar uma nova configuração válida quando, e sempre que, se
- * premir a tecla de Enter. O código deverá definir a dimensão do tabuleiro
- * numa variável (final) e estar preparado para funcionar para qualquer
- * valor (>4) dessa variável. Conceba e utilize os métodos estáticos para a
- * deteção dos ataques e impressão do tabuleiro.
- *
- * Não é permitido a utilização de variáveis estáticas (variáveis static da
- * classe)
- */
- public static void main(String[] args) {
- // TODO ...
- Scanner keyboard = new Scanner(System.in);
- while(true) {
- //int dimensao = randomIntNum(6)+4;
- int dimensao = 6;
- char[][] tabuleiro = new char [dimensao][dimensao];
- novoTabuleiro(tabuleiro);
- printTabuleiro(tabuleiro);
- String keychar = keyboard.nextLine();
- String enter = "";
- while(keychar!=enter) {
- keychar = keyboard.nextLine();
- }
- }
- }
- public static int randomIntNum( int dimensao) {
- int x=(int)(Math.random()*(dimensao))+1;
- return x;
- }
- private static char[][] novoTabuleiro(char[][]tabuleiro) {
- int dimensao = tabuleiro.length;
- int xt=randomIntNum(dimensao);
- int yt=randomIntNum(dimensao);
- int xb=randomIntNum(dimensao);
- int yb=randomIntNum(dimensao);
- int xc=randomIntNum(dimensao);
- int yc=randomIntNum(dimensao);
- //System.out.println(xt);
- //System.out.println(yt);
- //System.out.println(xb);
- //System.out.println(yb);
- //System.out.println(xc);
- //System.out.println(yc);
- while(existeAtaqueEntreTorreEBispo(xt,yt,xb,yb)||existeAtaqueEntreCavaloEBispo(xc,yc,xb,yb)) {
- xb=randomIntNum(dimensao);
- yb=randomIntNum(dimensao);
- }
- while(existeAtaqueEntreCavaloETorre(xc,yc,xt,yt)||existeAtaqueEntreCavaloEBispo(xc,yc,xb,yb)) {
- xc=randomIntNum(dimensao);
- yc=randomIntNum(dimensao);
- }
- for(int y = 0 ; y<dimensao; y++) {
- for(int x = 0 ; x<dimensao; x++) {
- if(torreAtacaPeca(xt,yt,x,y) || bispoAtacaPeca(xb,yb,x,y) || cavaloAtacaPeca(xc,yc,x,y)) {
- if(torreAtacaPeca(xt,yt,x,y) && bispoAtacaPeca(xb,yb,x,y) ||
- torreAtacaPeca(xt,yt,x,y) && cavaloAtacaPeca(xc,yc,x,y) ||
- cavaloAtacaPeca(xc,yc,x,y) && bispoAtacaPeca(xb,yb,x,y)) {
- tabuleiro[y][x]='+';
- }
- else {
- tabuleiro[y][x]='-';
- }
- }
- else {
- tabuleiro[y][x]='o';
- }
- }
- }
- tabuleiroAddTorre(tabuleiro,xt,yt);
- tabuleiroAddBispo(tabuleiro,xb,yb);
- tabuleiroAddCavalo(tabuleiro,xc,yc);
- return tabuleiro;
- }
- /**
- * Verifica se a torre em x,yt ataca a peça em xp,yp.
- *
- * Neste caso, se xt = xp há um ataque na vertical e se yt = yp há um ataque
- * na horizontal
- *
- * @return true, se há ataque
- */
- private static boolean torreAtacaPeca(int xt, int yt, int xp, int yp) {
- // TODO ...
- if(xt==xp || yt==yp) {
- return true;
- }
- return false;
- }
- /**
- * Verifica se o bispo em x,yt ataca a peça em xp,yp.
- */
- private static boolean bispoAtacaPeca(int xb, int yb, int xp, int yp) {
- // TODO ...
- int distanciax= xb-xp;
- int distanciay= yb-yp;
- if(distanciax<0) {
- distanciax*=-1;
- }
- if(distanciay<0) {
- distanciay*=-1;
- }
- if(distanciax==distanciay){
- return true;
- }
- return false;
- }
- /**
- * Verifica se o cavalo em x,yt ataca a peça em xp,yp.
- */
- private static boolean cavaloAtacaPeca(int xc, int yc, int xp, int yp) {
- // TODO ...
- if(xc==xp+2 && yc==yp+1 || xc==xp+2 && yc==yp-1 || xc==xp+1 && yc==yp+2 || xc==xp-1 && yc==yp+2 ||
- xc==xp-2 && yc==yp+1 || xc==xp-2 && yc==yp-1 || xc==xp+1 && yc==yp-2 || xc==xp-1 && yc==yp-2) {
- return true;
- }
- return false;
- }
- /**
- * Verifica se há ataque entre a torre em xt,yt e o bispo em xb,yb.
- *
- * Utiliza os métodos anteriores.
- */
- private static boolean existeAtaqueEntreTorreEBispo(int xt, int yt, int xb, int yb) {
- // TODO ... Utilizar os métodos anteriores
- if(torreAtacaPeca(xt,yt,xb,yb) || bispoAtacaPeca(xb,yb,xt,yt)) {
- return true;
- }
- return false;
- }
- /**
- * Verifica se há ataque entre o cavalo em xc,yc e o bispo em xb,yb.
- *
- * Utiliza os métodos anteriores.
- */
- private static boolean existeAtaqueEntreCavaloEBispo(int xc, int yc, int xb, int yb) {
- // TODO ... Utilizar os métodos anteriores
- if(cavaloAtacaPeca(xc,yc,xb,yb) || bispoAtacaPeca(xb,yb,xc,yc)) {
- return true;
- }
- return false;
- }
- /**
- * Verifica se há ataque entre o cavalo em xc,yc e a torre em xt,yt.
- *
- * Utiliza os métodos anteriores.
- */
- private static boolean existeAtaqueEntreCavaloETorre(int xc, int yc, int xt, int yt) {
- // TODO ... Utilizar os métodos anteriores
- if(cavaloAtacaPeca(xc,yc,xt,yt) || torreAtacaPeca(xt,yt,xc,yc)) {
- return true;
- }
- return false;
- }
- /**
- * Adiciona a torre ao tabuleiro. Ajusta os ataques às quadrículas
- */
- private static void tabuleiroAddTorre(char[][] tabuleiro, int xt, int yt) {
- // TODO ...
- tabuleiro[yt-1][xt-1]= 'T';
- }
- /**
- * Adiciona o bispo ao tabuleiro. Ajusta os ataques às quadrículas
- */
- private static void tabuleiroAddBispo(char[][] tabuleiro, int xb, int yb) {
- // TODO ...
- tabuleiro[yb-1][xb-1]= 'B';
- }
- /**
- * Adiciona o cavalo ao tabuleiro. Ajusta os ataques às quadrículas
- */
- private static void tabuleiroAddCavalo(char[][] tabuleiro, int xc, int yc) {
- // TODO ...
- tabuleiro[yc-1][xc-1]= 'C';
- }
- /**
- * Imprime o tabuleiro
- */
- private static void printTabuleiro(char[][] tabuleiro) {
- // TODO ...
- char barra = 124;
- int dimensao=tabuleiro.length;
- for(int y=0; y<dimensao;y++){
- System.out.print(barra);
- for(int x=0; x<dimensao;x++) {
- System.out.print(' ' );
- System.out.print(tabuleiro[y][x]);
- System.out.print(' ') ;
- System.out.print(barra);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement