Advertisement
umar_ahmad1

Game.java

Feb 29th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package chess;
  2. import java.io.Console;
  3.  
  4. public class Game {
  5. private static boolean gameEnd=false;
  6.  
  7. public Game(){
  8. Board b = new Board();
  9. b.initialisePieces();
  10. b.printBoard();
  11. int c = 0;
  12. while (!gameEnd){
  13. //write the game logic
  14.  
  15. String start, end;
  16. int i0 = 0;
  17. int j0 = 0;
  18. int i1 = 0;
  19. int j1 = 0;
  20.  
  21. if (c == 0){
  22. System.out.println("--- Whites move ---");
  23. Console console = System.console();
  24. do{
  25. start = console.readLine("> Enter origin: ");
  26. }
  27. while (!CheckInput.checkCoordinateValidity(start));
  28.  
  29. i0 = Character.getNumericValue(start.charAt(0))-1;
  30. j0 = (start.charAt(1))-97;
  31.  
  32. if(!b.hasPiece(i0, j0)){
  33. System.out.println("No piece on " + start);
  34. continue;
  35. }
  36.  
  37. else if (b.getPiece(i0, j0).getColour()==PieceColour.BLACK) {
  38. System.out.println("Not your piece");
  39. continue;
  40. }
  41.  
  42. do{
  43. end = console.readLine("> Enter destination: ");
  44. }
  45. while (!CheckInput.checkCoordinateValidity(end));
  46.  
  47. i1 = Character.getNumericValue(end.charAt(0))-1;
  48. j1 = (end.charAt(1))-97;
  49.  
  50. if (b.hasPiece(i1,j1)){
  51. if (b.getPiece(i1, j1).getColour()==PieceColour.WHITE) {
  52. System.out.println("Cannot move here");
  53. continue;
  54. }
  55. else {
  56. Piece p = b.getPiece(i0, j0);
  57. System.out.println(p);
  58. if (p.isLegitMove(i0, j0, i1, j1)){
  59. b.removePiece(i1, j1);
  60. b.movePiece(i0, j0, i1, j1, p);
  61. b.printBoard();
  62. }
  63. else {
  64. System.out.println("Invalid move");
  65. continue;
  66. }
  67. }
  68. }
  69.  
  70.  
  71. else {
  72.  
  73. Piece p = b.getPiece(i0, j0);
  74. if (p.isLegitMove(i0, j0, i1, j1)){
  75. b.movePiece(i0, j0, i1, j1, p);
  76. b.printBoard();
  77. }
  78. else{
  79. System.out.println("Invalid move");
  80. continue;
  81. }
  82. }
  83. c = c + 1;
  84.  
  85. }
  86.  
  87. if (c == 1){
  88. System.out.println("--- Blacks move ---");
  89. Console console = System.console();
  90. do{
  91. start = console.readLine("> Enter origin: ");
  92. }
  93. while (!CheckInput.checkCoordinateValidity(start));
  94.  
  95. i0 = Character.getNumericValue(start.charAt(0))-1;
  96. j0 = (start.charAt(1))-97;
  97.  
  98. if(!b.hasPiece(i0, j0)){
  99. System.out.println("No piece on " + start);
  100. continue;
  101. }
  102.  
  103. else if (b.getPiece(i0, j0).getColour()==PieceColour.WHITE) {
  104. System.out.println("Not your piece");
  105. continue;
  106. }
  107.  
  108. do{
  109. end = console.readLine("> Enter destination: ");
  110. }
  111. while (!CheckInput.checkCoordinateValidity(end));
  112.  
  113. i1 = Character.getNumericValue(end.charAt(0))-1;
  114. j1 = (end.charAt(1))-97;
  115.  
  116. if (b.hasPiece(i1,j1)){
  117. if (b.getPiece(i1, j1).getColour()==PieceColour.BLACK) {
  118. System.out.println("Cannot move here");
  119. continue;
  120. }
  121. else {
  122. Piece p = b.getPiece(i0, j0);
  123. if (p.isLegitMove(i0, j0, i1, j1)){
  124. System.out.println("Captured");
  125. b.removePiece(i1, j1);
  126. b.movePiece(i0, j0, i1, j1, p);
  127. b.printBoard();
  128. }
  129. else{
  130. System.out.println("Invalid move");
  131. continue;
  132. }
  133. }
  134. }
  135.  
  136.  
  137. else {
  138. Piece p = b.getPiece(i0, j0);
  139. if (p.isLegitMove(i0, j0, i1, j1)){
  140. b.movePiece(i0, j0, i1, j1, p);
  141. b.printBoard();
  142. }
  143. else{
  144. System.out.println("Invalid move");
  145. continue;
  146. }
  147. }
  148. c = c - 1;
  149. }
  150.  
  151.  
  152. }
  153. }
  154. public static void main (String args[]){
  155. Game g = new Game();
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement