Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. package Chess;
  2.  
  3. import Chess.ChessPieces.ChessPiece;
  4. import Chess.ChessPieces.Pawn;
  5. import Chess.ChessPieces.Rook;
  6.  
  7. public class ChessModel implements Boardgame {
  8. private String currentMessage = "No message yet";
  9. private ChessPiece[][] board = new ChessPiece[8][8];
  10. private boolean white = true;
  11. private ChessPiece activePiece;
  12.  
  13. ChessModel() {
  14. // Creates the board.
  15.  
  16. char color = 'B';
  17. for (int y = 0; y < 8; y+=7) {
  18. board[y][0] = new Rook(y, 0, color);
  19. board[y][1] = new Pawn(y, 1, color);
  20. board[y][2] = new Pawn(y, 2, color);
  21. board[y][3] = new Pawn(y, 3, color);
  22. board[y][4] = new Pawn(y, 4, color);
  23. board[y][5] = new Pawn(y, 5, color);
  24. board[y][6] = new Pawn(y, 6, color);
  25. board[y][7] = new Rook(y, 7, color);
  26.  
  27. for (int x = 0; x < 8; x++) {
  28. int yPos = y + (color == 'B' ? 1 : -1);
  29. board[yPos][x] = new Pawn(yPos, x, color); // Skapar bönderna
  30. }
  31.  
  32. color = 'W';
  33. }
  34. }
  35.  
  36.  
  37. @Override
  38. public boolean move(int y, int x) {
  39. if (!(0 <= y && y < 8) || !(0 <= x && x < 8)) { // Checks if the chosen coord. is out of bounds.
  40. this.currentMessage = "Please choose a position within the board!";
  41. return false;
  42. }
  43.  
  44. if (board[y][x] != null) { // Om rutan har en pjäs.
  45. if (board[y][x].getWhite() == white) { // Om det är ens egna färg.
  46. activePiece = board[y][x];
  47. return true;
  48. }
  49. else if (activePiece == null){ // Om det är första draget och motståndarens färg.
  50. this.currentMessage = "Välj en pjäs av din egna färg.";
  51. return false;
  52. }
  53. else { // Om det är andra draget - attackera motståndare.
  54. if (activePiece.checkMove(y, x, board)) { // Kollar om pjäsens drag är giltigt.
  55. makeMove(y, x);
  56. return true;
  57. }
  58. }
  59. }
  60. else if (activePiece == null) { // Om rutan är tom och det är första draget.
  61. this.currentMessage = "Välj en pjäs.";
  62. return false;
  63. }
  64. else { // Om rutan är tom och det är andra draget.
  65. if (activePiece.checkMove(y, x, board)) { // Kollar om pjäsens drag är giltigt.
  66. makeMove(y, x);
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72.  
  73. private void makeMove(int y, int x) { // Utför draget.
  74. board[activePiece.y][activePiece.x] = null; // Tömmer rutan som blev "dödad".
  75. board[y][x] = activePiece; // Flyttar pjäsen i matrisen.
  76. board[y][x].x = x; // Uppdaterar pjäsens x-kord.
  77. board[y][x].y = y; // Uppdaterar pjäsens y-kord.
  78. activePiece = null; // Markerar pjäsen som ej aktiv.
  79. white = !white; // Ändrar till motståndarens tur.
  80. }
  81.  
  82. @Override
  83. public String getStatus(int i, int j) {
  84. if(board[i][j] == null) {
  85. return " ";
  86. }
  87. return board[i][j].toString();
  88. }
  89.  
  90. @Override
  91. public String getMessage() {
  92. return this.currentMessage;
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement