Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. package tps.tp1.pack3Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class P04Xadrez {
  5.  
  6. /**
  7. * Conceba o programa P04Xadrez que, no contexto de um tabuleiro de xadrez,
  8. * posicione de forma aleatória uma torre, um bispo e um cavalo, mas de
  9. * forma a não haver ataques entre as peças. Assim para cada peça deve-se
  10. * guardar as suas coordenadas x e y e comparar se há ataque com as peças já
  11. * processadas. A comparação deve ser realizada somente com base nas
  12. * coordenadas das peças. Em caso de ataque deve gerar novas coordenadas e
  13. * voltar a testar, até não haver qualquer ataque. No final deve-se mostrar
  14. * o tabuleiro em que a torre é visualizada com um ‘T’, o bispo com um ‘B’,
  15. * o cavalo com um ‘C’, cada posição que não sofra qualquer ataque deve
  16. * conter um ‘o’, cada posição com um só ataque deve conter um ‘-‘, e cada
  17. * posição com pelo menos dois ataques deve conter um ‘+’. O programa deverá
  18. * gerar e mostrar uma nova configuração válida quando, e sempre que, se
  19. * premir a tecla de Enter. O código deverá definir a dimensão do tabuleiro
  20. * numa variável (final) e estar preparado para funcionar para qualquer
  21. * valor (>4) dessa variável. Conceba e utilize os métodos estáticos para a
  22. * deteção dos ataques e impressão do tabuleiro.
  23. *
  24. * Não é permitido a utilização de variáveis estáticas (variáveis static da
  25. * classe)
  26. */
  27. public static void main(String[] args) {
  28. // TODO ...
  29. Scanner keyboard = new Scanner(System.in);
  30. while(true) {
  31. //int dimensao = randomIntNum(6)+4;
  32. int dimensao = 6;
  33. char[][] tabuleiro = new char [dimensao][dimensao];
  34. novoTabuleiro(tabuleiro);
  35. printTabuleiro(tabuleiro);
  36. String keychar = keyboard.nextLine();
  37. String enter = "";
  38. while(keychar!=enter) {
  39. keychar = keyboard.nextLine();
  40. }
  41.  
  42.  
  43. }
  44. }
  45. public static int randomIntNum( int dimensao) {
  46. int x=(int)(Math.random()*(dimensao))+1;
  47. return x;
  48. }
  49.  
  50.  
  51. private static char[][] novoTabuleiro(char[][]tabuleiro) {
  52. int dimensao = tabuleiro.length;
  53. int xt=randomIntNum(dimensao);
  54. int yt=randomIntNum(dimensao);
  55. int xb=randomIntNum(dimensao);
  56. int yb=randomIntNum(dimensao);
  57. int xc=randomIntNum(dimensao);
  58. int yc=randomIntNum(dimensao);
  59. //System.out.println(xt);
  60. //System.out.println(yt);
  61. //System.out.println(xb);
  62. //System.out.println(yb);
  63. //System.out.println(xc);
  64. //System.out.println(yc);
  65. while(existeAtaqueEntreTorreEBispo(xt,yt,xb,yb)||existeAtaqueEntreCavaloEBispo(xc,yc,xb,yb)) {
  66. xb=randomIntNum(dimensao);
  67. yb=randomIntNum(dimensao);
  68. }
  69. while(existeAtaqueEntreCavaloETorre(xc,yc,xt,yt)||existeAtaqueEntreCavaloEBispo(xc,yc,xb,yb)) {
  70. xc=randomIntNum(dimensao);
  71. yc=randomIntNum(dimensao);
  72. }
  73. for(int y = 0 ; y<dimensao; y++) {
  74. for(int x = 0 ; x<dimensao; x++) {
  75. if(torreAtacaPeca(xt,yt,x,y) || bispoAtacaPeca(xb,yb,x,y) || cavaloAtacaPeca(xc,yc,x,y)) {
  76. if(torreAtacaPeca(xt,yt,x,y) && bispoAtacaPeca(xb,yb,x,y) ||
  77. torreAtacaPeca(xt,yt,x,y) && cavaloAtacaPeca(xc,yc,x,y) ||
  78. cavaloAtacaPeca(xc,yc,x,y) && bispoAtacaPeca(xb,yb,x,y)) {
  79. tabuleiro[y][x]='+';
  80. }
  81. else {
  82. tabuleiro[y][x]='-';
  83. }
  84.  
  85. }
  86. else {
  87. tabuleiro[y][x]='o';
  88. }
  89. }
  90. }
  91. tabuleiroAddTorre(tabuleiro,xt,yt);
  92. tabuleiroAddBispo(tabuleiro,xb,yb);
  93. tabuleiroAddCavalo(tabuleiro,xc,yc);
  94.  
  95. return tabuleiro;
  96.  
  97. }
  98.  
  99. /**
  100. * Verifica se a torre em x,yt ataca a peça em xp,yp.
  101. *
  102. * Neste caso, se xt = xp há um ataque na vertical e se yt = yp há um ataque
  103. * na horizontal
  104. *
  105. * @return true, se há ataque
  106. */
  107. private static boolean torreAtacaPeca(int xt, int yt, int xp, int yp) {
  108. // TODO ...
  109. if(xt==xp || yt==yp) {
  110. return true;
  111. }
  112. return false;
  113. }
  114.  
  115. /**
  116. * Verifica se o bispo em x,yt ataca a peça em xp,yp.
  117. */
  118. private static boolean bispoAtacaPeca(int xb, int yb, int xp, int yp) {
  119. // TODO ...
  120. int distanciax= xb-xp;
  121. int distanciay= yb-yp;
  122. if(distanciax<0) {
  123. distanciax*=-1;
  124. }
  125. if(distanciay<0) {
  126. distanciay*=-1;
  127. }
  128. if(distanciax==distanciay){
  129. return true;
  130.  
  131. }
  132. return false;
  133. }
  134.  
  135.  
  136. /**
  137. * Verifica se o cavalo em x,yt ataca a peça em xp,yp.
  138. */
  139. private static boolean cavaloAtacaPeca(int xc, int yc, int xp, int yp) {
  140. // TODO ...
  141. 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 ||
  142. xc==xp-2 && yc==yp+1 || xc==xp-2 && yc==yp-1 || xc==xp+1 && yc==yp-2 || xc==xp-1 && yc==yp-2) {
  143. return true;
  144. }
  145. return false;
  146. }
  147.  
  148. /**
  149. * Verifica se há ataque entre a torre em xt,yt e o bispo em xb,yb.
  150. *
  151. * Utiliza os métodos anteriores.
  152. */
  153. private static boolean existeAtaqueEntreTorreEBispo(int xt, int yt, int xb, int yb) {
  154. // TODO ... Utilizar os métodos anteriores
  155. if(torreAtacaPeca(xt,yt,xb,yb) || bispoAtacaPeca(xb,yb,xt,yt)) {
  156. return true;
  157. }
  158. return false;
  159. }
  160.  
  161. /**
  162. * Verifica se há ataque entre o cavalo em xc,yc e o bispo em xb,yb.
  163. *
  164. * Utiliza os métodos anteriores.
  165. */
  166. private static boolean existeAtaqueEntreCavaloEBispo(int xc, int yc, int xb, int yb) {
  167. // TODO ... Utilizar os métodos anteriores
  168. if(cavaloAtacaPeca(xc,yc,xb,yb) || bispoAtacaPeca(xb,yb,xc,yc)) {
  169. return true;
  170. }
  171. return false;
  172. }
  173.  
  174. /**
  175. * Verifica se há ataque entre o cavalo em xc,yc e a torre em xt,yt.
  176. *
  177. * Utiliza os métodos anteriores.
  178. */
  179. private static boolean existeAtaqueEntreCavaloETorre(int xc, int yc, int xt, int yt) {
  180. // TODO ... Utilizar os métodos anteriores
  181. if(cavaloAtacaPeca(xc,yc,xt,yt) || torreAtacaPeca(xt,yt,xc,yc)) {
  182. return true;
  183. }
  184. return false;
  185. }
  186.  
  187.  
  188. /**
  189. * Adiciona a torre ao tabuleiro. Ajusta os ataques às quadrículas
  190. */
  191. private static void tabuleiroAddTorre(char[][] tabuleiro, int xt, int yt) {
  192. // TODO ...
  193. tabuleiro[yt-1][xt-1]= 'T';
  194. }
  195.  
  196. /**
  197. * Adiciona o bispo ao tabuleiro. Ajusta os ataques às quadrículas
  198. */
  199. private static void tabuleiroAddBispo(char[][] tabuleiro, int xb, int yb) {
  200. // TODO ...
  201. tabuleiro[yb-1][xb-1]= 'B';
  202. }
  203.  
  204. /**
  205. * Adiciona o cavalo ao tabuleiro. Ajusta os ataques às quadrículas
  206. */
  207. private static void tabuleiroAddCavalo(char[][] tabuleiro, int xc, int yc) {
  208. // TODO ...
  209. tabuleiro[yc-1][xc-1]= 'C';
  210. }
  211.  
  212. /**
  213. * Imprime o tabuleiro
  214. */
  215. private static void printTabuleiro(char[][] tabuleiro) {
  216. // TODO ...
  217.  
  218. char barra = 124;
  219. int dimensao=tabuleiro.length;
  220.  
  221. for(int y=0; y<dimensao;y++){
  222. System.out.print(barra);
  223. for(int x=0; x<dimensao;x++) {
  224. System.out.print(' ' );
  225. System.out.print(tabuleiro[y][x]);
  226. System.out.print(' ') ;
  227. System.out.print(barra);
  228.  
  229. }
  230. System.out.println();
  231. }
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement