umar_ahmad

game1.java

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