重现Bagh Chan游戏(虎与山羊)

huangapple go评论103阅读模式
英文:

Recreating the Bagh Chan game (Tigers and goats)

问题

Below is the translated code:

  1. public class 游戏展示器 implements MouseListener
  2. {
  3. // 实例变量
  4. private int 块大小; // 块大小 - 所有其他尺寸都从块大小派生
  5. private int 棋盘大小; // 棋盘大小
  6. private SimpleCanvas 画布; // 用于绘制的 SimpleCanvas 对象
  7. private GameRules 规则; // GameRules 对象
  8. private Board 棋盘; // Board 对象
  9. private AIplayer 电脑玩家; // AIplayer 对象
  10. // 在块大小步长上的棋盘上的2D坐标
  11. public static final int[][] 位置 = {{1,1},{1,4},{1,7},{4,7},{7,7},{7,4},{7,1},{4,1},
  12. {2,2},{2,4},{2,6},{4,6},{6,6},{6,4},{6,2},{4,2},
  13. {3,3},{3,4},{3,5},{4,5},{5,5},{5,4},{5,3},{4,3}};
  14. // 山羊移动的源和目标地点
  15. private int[] 移动 = {-1,-1}; //-1 表示没有选择
  16. /**
  17. * 构造函数,初始化实例变量并添加鼠标监听器。
  18. * 绘制棋盘。
  19. */
  20. public 游戏展示器(int 块大小)
  21. {
  22. this.块大小 = 块大小;
  23. 棋盘大小 = 块大小 * 8;
  24. 画布 = new SimpleCanvas("老虎与山羊", 棋盘大小, 棋盘大小, Color.BLUE);
  25. 画布.addMouseListener(this);
  26. 规则 = new GameRules();
  27. 棋盘 = new Board();
  28. 电脑玩家 = new AIplayer();
  29. 绘制棋盘();
  30. }
  31. /**
  32. * 使用默认块大小的构造函数
  33. */
  34. public 游戏展示器( )
  35. {
  36. this(80);
  37. }
  38. /**
  39. * 绘制棋盘线和棋子以及山羊数量。
  40. */
  41. private void 绘制棋盘()
  42. {
  43. 画布.drawRectangle(0,0,棋盘大小,棋盘大小,Color.BLUE); // 清空画布
  44. //绘制山羊和老虎。 (绘制阴影不是必须的)
  45. //显示山羊数量
  46. // 其余部分不变
  47. }
  48. // 其他方法和内容保持不变
  49. }
  1. public class 游戏规则
  2. {
  3. // 其余内容保持不变
  4. }
  1. public class 棋盘
  2. {
  3. // 其余内容保持不变
  4. }
  1. public class 电脑玩家
  2. {
  3. // 其余内容保持不变
  4. }

请注意,以上翻译的代码只是直接的翻译,并没有修改变量名和注释等,因此可能会看起来比较生硬。如果需要更自然的中文表达,可以适当调整变量名和注释。另外,由于代码中有部分未完成的部分(TODO 部分),可能需要根据任务要求进行补充和修改。

英文:

Basically for a homework assignment I've been asked to recreate a small indian game known as Bagn Chan, and I have no idea where to start. Ive been given some skeleton code, which is all attached.

** NOT ASKING FOR FULL SOLUTION, JUST GUIDANCE, although more than happy if someone wants to do a full solution **

The IDE used is BlueJ, but yeh I just have no idea what to do so any help would be massively appreciated.

This is the class GameViewer

  1. public class GameViewer implements MouseListener
  2. {
  3. // instance variables
  4. private int bkSize; // block size - all other measurements to be derived from bkSize
  5. private int brdSize; // board size
  6. private SimpleCanvas sc; // an object of SimpleCanvas to draw
  7. private GameRules rules; // an object of GameRules
  8. private Board bd; // an object of Board
  9. private AIplayer ai; //an object of AIplayer
  10. // 2D coordinates of valid locations on the board in steps of block size
  11. public static final int[][] locs = {{1,1},{1,4},{1,7},{4,7},{7,7},{7,4},{7,1},{4,1},
  12. {2,2},{2,4},{2,6},{4,6},{6,6},{6,4},{6,2},{4,2},
  13. {3,3},{3,4},{3,5},{4,5},{5,5},{5,4},{5,3},{4,3}};
  14. // source and destination for the goat moves
  15. private int[] mov = {-1,-1}; //-1 means no selection
  16. /**
  17. * Constructor for objects of class GameViewer
  18. * Initializes instance variables and adds mouse listener.
  19. * Draws the board.
  20. */
  21. public GameViewer(int bkSize)
  22. {
  23. this.bkSize = bkSize;
  24. brdSize = bkSize*8;
  25. sc = new SimpleCanvas("Tigers and Goats", brdSize, brdSize, Colour.BLUE);
  26. sc.addMouseListener(this);
  27. rules = new GameRules();
  28. bd = new Board();
  29. ai = new AIplayer();
  30. drawBoard();
  31. }
  32. /**
  33. * Constructor with default block size
  34. */
  35. public GameViewer( )
  36. {
  37. this(80);
  38. }
  39. /**
  40. * Draws the boad lines and the pieces as per their locations.
  41. * Drawing of lines is provided, students to implement drawing
  42. * of pieces and number of goats.
  43. */
  44. private void drawBoard()
  45. {
  46. sc.drawRectangle(0,0,brdSize,brdSize,Color.BLUE); //wipe the canvas
  47. //draw shadows of Goats and Tigers - not compulsory //////////////////////
  48. // Draw the lines
  49. for(int i=1; i<9; i++)
  50. {
  51. //diagonal and middle line
  52. sc.drawLine(locs[i-1][0]*bkSize, locs[i-1][1]*bkSize,
  53. locs[i+15][0]*bkSize, locs[i+15][1]*bkSize, Color.red);
  54. if(i==4 || i==8) continue; //no more to draw at i=4,8
  55. // vertical line
  56. sc.drawLine(i*bkSize, i*bkSize,
  57. i*bkSize, brdSize-i*bkSize,Color.white);
  58. // horizontal line
  59. sc.drawLine(i*bkSize, i*bkSize,
  60. brdSize-i*bkSize, i*bkSize, Color.white);
  61. }
  62. // TODO 10
  63. // Draw the goats and tigers. (Drawing the shadows is not compulsory)
  64. // Display the number of goats
  65. }
  66. /**
  67. * If vacant, place a goat at the user clicked location on board.
  68. * Update goat count in rules and draw the updated board
  69. */
  70. public void placeGoat(int loc)
  71. {
  72. //TODO 2
  73. }
  74. /**
  75. * Calls the placeTiger method of AIplayer to place a tiger on the board.
  76. * Increments tiger count in rules.
  77. * Draws the updated board.
  78. */
  79. public void placeTiger()
  80. {
  81. //TODO 13
  82. }
  83. /**
  84. * Toggles goat selection - changes the colour of selected goat.
  85. * Resets selection and changes the colour back when the same goat is clicked again.
  86. * Selects destination (if vacant) to move and calls moveGoat to make the move.
  87. */
  88. public void selectGoatMove(int loc)
  89. {
  90. //TODO 16
  91. }
  92. /**
  93. * Make the user selected goat move only if legal otherwise set the destination to -1 (invalid).
  94. * If did make a goat move, then update board, draw the updated board, reset mov to -1,-1.
  95. * and call tigersMove() since after every goat move, there is a tiger move.
  96. */
  97. public void moveGoat()
  98. {
  99. //TODO 18
  100. }
  101. /**
  102. * Call AIplayer to make its move. Update and draw the board after the move.
  103. * If Tigers cannot move, display "Goats Win!".
  104. * If goats are less than 6, display "Tigers Win!".
  105. * No need to terminate the game.
  106. */
  107. public void tigersMove()
  108. {
  109. //TODO 20
  110. }
  111. /**
  112. * Respond to a mouse click on the board.
  113. * Get a valid location nearest to the click (from GameRules).
  114. * If nearest location is still far, do nothing.
  115. * Otherwise, call placeGoat to place a goat at the location.
  116. * Call this.placeTiger when it is the tigers turn to place.
  117. * When the game changes to move stage, call selectGoatMove to move
  118. * the user selected goat to the user selected destination.
  119. */
  120. public void mousePressed(MouseEvent e)
  121. {
  122. SimpleCanvas sc = new SimpleCanvas();
  123. sc.addMouseListener(e);
  124. }
  125. public void mouseClicked(MouseEvent e) {int x=e.getX();int y=e.getY();}
  126. public void mouseReleased(MouseEvent e) {}
  127. public void mouseEntered(MouseEvent e) {}
  128. public void mouseExited(MouseEvent e) {}
  129. }

This is class GameRules

  1. {
  2. // Instance variables to maintain whose move it is
  3. private boolean moveStage;
  4. private boolean goatsTurn;
  5. private int numGoats; //the number of goats on the board
  6. private int numTigers; //the number of tigers on the board
  7. private final int MAXGOATS = 12;
  8. /**
  9. * Constructor for objects of class GameRules
  10. */
  11. public GameRules()
  12. {
  13. moveStage = false;
  14. goatsTurn = true;
  15. numGoats = 0;
  16. numTigers = 0;
  17. }
  18. /**
  19. * returns moveStage
  20. */
  21. public boolean isMoveStage()
  22. {
  23. return moveStage;
  24. }
  25. /**
  26. * returns true iff it is goats turn
  27. */
  28. public boolean isGoatsTurn()
  29. {
  30. return goatsTurn;
  31. }
  32. /**
  33. * Adds (+1 or -1) to goat numbers.
  34. * Changes the goatsTurn and moveStage as per rules.
  35. */
  36. public void addGoat(int n)
  37. {
  38. //TODO 12
  39. numGoats = numGoats + n;
  40. if (numGoats == 12)
  41. {
  42. moveStage = true;
  43. goatsTurn = false;
  44. }
  45. }
  46. /**
  47. * returns number of goats
  48. */
  49. public int getNumGoats()
  50. {
  51. return numGoats;
  52. }
  53. /**
  54. * increments tigers and gives turn back to goats
  55. */
  56. public void incrTigers()
  57. {
  58. //TODO 16
  59. numTigers = numTigers + 1;
  60. goatsTurn = true;
  61. if (numTigers > 3){
  62. numTigers = 3;
  63. throw new IllegalArgumentException("Maximum of 3 Tigers allowed");
  64. }
  65. if (numTigers == 3){
  66. moveStage = true;
  67. }
  68. }
  69. /**
  70. * Returns the nearest valid location (0-23) on the board to the x,y mouse click.
  71. * Locations are described in project description on LMS.
  72. * You will need bkSize & GameViewer.locs to compute the distance to a location.
  73. * If the click is close enough to a valid location on the board, return
  74. * that location, otherwise return -1 (when the click is not close to any location).
  75. * Choose a threshold for proximity of click based on bkSize.
  76. */
  77. public int nearestLoc(int x, int y, int bkSize)
  78. {
  79. int loc = -1;
  80. int buffer = (2*bkSize/10);
  81. int oneHigh = (bkSize+buffer);
  82. int oneLow = (bkSize-buffer);
  83. int twoHigh = (2*bkSize+buffer);
  84. int twoLow = (2*bkSize-buffer);
  85. int threeHigh = (3*bkSize+buffer);
  86. int threeLow = (3*bkSize-buffer);
  87. int fourHigh = (4*bkSize+buffer);
  88. int fourLow = (4*bkSize-buffer);
  89. int fiveHigh = (5*bkSize+buffer);
  90. int fiveLow = (5*bkSize-buffer);
  91. int sixHigh = (6*bkSize+buffer);
  92. int sixLow = (6*bkSize-buffer);
  93. int sevenHigh = (7*bkSize+buffer);
  94. int sevenLow = (7*bkSize-buffer);
  95. if (oneHigh >= x && x >= oneLow) {
  96. if (oneHigh >= y && y >= oneLow){
  97. x = 1;
  98. y = 1;
  99. loc = 0;
  100. }
  101. if (fourHigh >= y && y >= fourLow){
  102. x = 1;
  103. y = 4;
  104. loc = 1;
  105. }
  106. if (sevenHigh >= y && y >= sevenLow){
  107. x = 1;
  108. y = 7;
  109. loc = 2;
  110. }
  111. }
  112. if (twoHigh >= x && x >= twoLow) {
  113. if (twoHigh >= y && y >= twoLow){
  114. x = 2;
  115. y = 2;
  116. loc = 8;
  117. }
  118. if (fourHigh >= y && y >= fourLow){
  119. x = 2;
  120. y = 4;
  121. loc = 9;
  122. }
  123. if (sixHigh >= y && y >= sixLow){
  124. x = 2;
  125. y = 6;
  126. loc = 10;
  127. }
  128. }
  129. if (threeHigh >= x && x >= threeLow) {
  130. if (threeHigh >= y && y >= threeLow){
  131. x = 3;
  132. y = 3;
  133. loc= 16;
  134. }
  135. if (fourHigh >= y && y >= fourLow){
  136. x = 3;
  137. y = 4;
  138. loc = 17;
  139. }
  140. if (fiveHigh >= y && y >= fiveLow){
  141. x = 3;
  142. y = 5;
  143. loc = 18;
  144. }
  145. }
  146. if (sevenHigh >= x && x >= sevenLow) {
  147. if (oneHigh >= y && y >= oneLow){
  148. x = 7;
  149. y = 1;
  150. loc = 6;
  151. }
  152. if (fourHigh >= y && y >= fourLow){
  153. x = 7;
  154. y = 4;
  155. loc = 5;
  156. }
  157. if (sevenHigh >= y && y >= sevenLow){
  158. x = 7;
  159. y = 7;
  160. loc = 4;
  161. }
  162. }
  163. if (sixHigh >= x && x >= sixLow) {
  164. if (twoHigh >= y && y >= twoLow){
  165. x = 6;
  166. y = 2;
  167. loc = 14;
  168. }
  169. if (fourHigh >= y && y >= fourLow){
  170. x = 6;
  171. y = 4;
  172. loc = 13;
  173. }
  174. if (sixHigh >= y && y >= sixLow){
  175. x = 6;
  176. y = 6;
  177. loc = 12;
  178. }
  179. }
  180. if (fiveHigh >= x && x >= fiveLow) {
  181. if (threeHigh >= y && y >= threeLow){
  182. x = 5;
  183. y = 3;
  184. loc = 22;
  185. }
  186. if (fourHigh >= y && y >= fourLow){
  187. x = 5;
  188. y = 4;
  189. loc = 21;
  190. }
  191. if (fiveHigh >= y && y >= fiveLow){
  192. x = 5;
  193. y = 5;
  194. loc = 20;
  195. }
  196. }
  197. if (fourHigh >= x && x >= fourLow){
  198. if (twoHigh >= y && y >= twoLow){
  199. x = 4;
  200. y = 2;
  201. loc = 15;
  202. }
  203. if (fiveHigh >= y && y >= fiveLow){
  204. x = 4;
  205. y = 5;
  206. loc = 19;
  207. }
  208. if (sixHigh >= y && y >= sixLow){
  209. x = 4;
  210. y = 6;
  211. loc = 11;
  212. }
  213. if (sevenHigh >= y && y >= sevenLow){
  214. x = 4;
  215. y = 7;
  216. loc = 3;
  217. }
  218. if (oneHigh >= y && y >= oneLow){
  219. x = 4;
  220. y = 1;
  221. loc = 7;
  222. }
  223. if (threeHigh >= y && y >= threeLow){
  224. x = 4;
  225. y = 3;
  226. loc = 23;
  227. }
  228. }
  229. return loc;
  230. }
  231. /**
  232. * Returns true iff a move from location a to b is legal, otherwise returns false.
  233. * For example: a,b = 1,2 -> true; 1,3 -> false; 7,0 -> true. Refer to the
  234. * project description for details.
  235. * Throws an exception for illegal arguments.
  236. */
  237. public boolean isLegalMove(int a, int b)
  238. {
  239. //TODO 19
  240. if (a == 7 & b == 8 | a == 8 & b == 7 |
  241. a == 15 & b == 16 | a == 16 & b == 15 |
  242. a<0 | b<0 | a>23 | b>23 ){
  243. throw new IllegalArgumentException("Move is illegal");
  244. }
  245. if (a == 0 & b == 7 | a == 7 & b == 0 |
  246. a == 8 & b == 15 | a == 15 & b == 8 |
  247. a == 23 & b == 16 | a == 16 & b == 23){
  248. return true;
  249. }
  250. if (a - b == 8 | a - b == 1 | b - a == 8 | b - a == 1){
  251. return true;
  252. }
  253. else throw new IllegalArgumentException("Move is illegal");
  254. }
  255. }

This is simplecanvas

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.List;
  5. import java.util.*;
  6. public class SimpleCanvas
  7. {
  8. private JFrame frame;
  9. private CanvasPane canvas;
  10. private Graphics2D graphic;
  11. private Image canvasImage;
  12. private boolean autoRepaint;
  13. /**
  14. * Creates and displays a SimpleCanvas of the specified size and background
  15. */
  16. public SimpleCanvas(String title, int width, int height, Color bgColour) {
  17. frame = new JFrame();
  18. canvas = new CanvasPane();
  19. frame.setContentPane(canvas);
  20. frame.setTitle(title);
  21. canvas.setPreferredSize(new Dimension(width,height));
  22. frame.pack();
  23. Dimension size = canvas.getSize();
  24. canvasImage = canvas.createImage(size.width,size.height);
  25. graphic = (Graphics2D) canvasImage.getGraphics();
  26. graphic.setColor(bgColour);
  27. graphic.fillRect(0,0,size.width,size.height);
  28. graphic.setColor(Color.black);
  29. frame.setVisible(true);
  30. this.autoRepaint = true;
  31. }
  32. /**
  33. * Creates and displays a SimpleCanvas of size 400x400 with the
  34. * default title "SimpleCanvas" and with white background.
  35. */
  36. public SimpleCanvas() {
  37. this("SimpleCanvas", 400, 400, Color.white);
  38. }
  39. /**
  40. * Draws a line on this SimpleCanvas from x1,y1 to x2,y2 with colour c.
  41. */
  42. public void drawLine(int x1, int y1, int x2, int y2, Color c) {
  43. setForegroundColour(c);
  44. graphic.drawLine(x1, y1, x2, y2);
  45. if (autoRepaint) canvas.repaint();
  46. }
  47. /**
  48. * Draws a point on this SimpleCanvas at x,y with colour c.
  49. */
  50. public void drawPoint(int x, int y, Color c) {
  51. drawLine(x, y, x, y, c);
  52. }
  53. /**
  54. * Draws a rectangle on this SimpleCanvas with sides parallel to the axes
  55. * between x1,y1 and x2,y2 with colour c.
  56. */
  57. public void drawRectangle(int x1, int y1, int x2, int y2, Color c) {
  58. setForegroundColour(c);
  59. graphic.fillRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
  60. if (autoRepaint) canvas.repaint();
  61. }
  62. /**
  63. * Draws a disc on this SimpleCanvas centred at x,y with radius r with colour c.
  64. */
  65. public void drawDisc(int x, int y, int r, Color c) {
  66. for (int i = x - r; i <= x + r; i++)
  67. for (int j = y - r; j <= y + r; j++)
  68. if (Math.pow(i-x, 2) + Math.pow(j-y, 2) <= Math.pow(r, 2))
  69. drawPoint(i, j, c);
  70. if (autoRepaint) canvas.repaint();
  71. }
  72. /**
  73. * Draws a circle on this SimpleCanvas centred at x,y with radius r with colour c.
  74. */
  75. public void drawCircle(int x, int y, int r, Color c) {
  76. for (int i = x - r; i <= x + r; i++)
  77. for (int j = y - r; j <= y + r; j++)
  78. if (Math.pow(i-x, 2) + Math.pow(j-y, 2) <= Math.pow(r, 2) &&
  79. Math.pow(i-x, 2) + Math.pow(j-y, 2) >= Math.pow(r - 5, 2))
  80. drawPoint(i, j, c);
  81. if (autoRepaint) canvas.repaint();
  82. }
  83. /**
  84. * Writes the String text on this SimpleCanvas at x,y with colour c.
  85. */
  86. public void drawString(String text, int x, int y, Color c) {
  87. setForegroundColour(c);
  88. graphic.drawString(text, x, y);
  89. if (autoRepaint) canvas.repaint();
  90. }
  91. /**
  92. * Writes the number n on this SimpleCanvas at x,y with colour c.
  93. */
  94. public void drawString(int n, int x, int y, Color c) {
  95. drawString(n + "", x, y, c);
  96. }
  97. /**
  98. * Changes the colour for subsequent drawing on this SimpleCanvas.
  99. */
  100. public void setForegroundColour(Color newColour) {
  101. graphic.setColor(newColour);
  102. }
  103. /**
  104. * Gets the colour currently used for drawing on this SimpleCanvas.
  105. */
  106. public Color getForegroundColour() {
  107. return graphic.getColor();
  108. }
  109. /**
  110. * Changes the font for subsequent Strings on this SimpleCanvas.
  111. */
  112. public void setFont(Font newFont) {
  113. graphic.setFont(newFont);
  114. }
  115. /**
  116. * Gets the font currently used for Strings on this SimpleCanvas.
  117. */
  118. public Font getFont() {
  119. return graphic.getFont();
  120. }
  121. /**
  122. * Sets the repaint mode to either manual or automatic.
  123. */
  124. public void setAutoRepaint(boolean autoRepaint) {
  125. this.autoRepaint = autoRepaint;
  126. }
  127. /**
  128. * If this SimpleCanvas does not automatically repaint after each drawing command,
  129. * this method can be used to cause a manual repaint.
  130. */
  131. public void repaint() {
  132. canvas.repaint();
  133. }
  134. /**
  135. * Causes execution to pause for the specified amount of time.
  136. * This is usually used to produce animations in an easy manner,
  137. * by repeatedly drawing, pausing, and then redrawing an object.
  138. */
  139. public void wait(int millis) {
  140. try {
  141. Thread.sleep(millis);
  142. } catch (InterruptedException ie) {
  143. System.out.println("Interruption in SimpleCanvas: " + ie);
  144. }
  145. }
  146. /**
  147. * Sets up this SimpleCanvas to respond to mouse input.
  148. */
  149. public void addMouseListener(MouseListener ml) {
  150. canvas.addMouseListener(ml);
  151. }
  152. /**
  153. * Sets up this SimpleCanvas to respond to mouse motion input.
  154. */
  155. public void addMouseMotionListener(MouseMotionListener mml) {
  156. canvas.addMouseMotionListener(mml);
  157. }
  158. class CanvasPane extends JPanel {
  159. public void paint(Graphics g) {
  160. g.drawImage(canvasImage,0,0,null);
  161. }
  162. }
  163. }

This is board COMPLETED

  1. public class Board
  2. {
  3. // An enumated type for the three possibilities
  4. private enum Piece {GOAT, TIGER, VACANT};
  5. // 1-D Array representation of the board. Top left is 0,
  6. // then goes anti-clockwise spiraling inward until 23
  7. Piece[] board;
  8. /**
  9. * Constructor for objects of class Board.
  10. * Initializes the board VACANT.
  11. */
  12. public Board()
  13. {
  14. board = new Piece[24];
  15. for (int i=0;i<24;i++)
  16. board[i] = Piece.VACANT;
  17. }
  18. /**
  19. * Checks if the location a is VACANT on the board
  20. */
  21. public boolean isVacant(int a)
  22. {
  23. //TODO 4
  24. return board[a] == Piece.VACANT;
  25. }
  26. /**
  27. * Sets the location a on the board to VACANT
  28. */
  29. public void setVacant(int a)
  30. {
  31. //TODO 5
  32. board[a] = Piece.VACANT;
  33. }
  34. /**
  35. * Checks if the location a on the board is a GOAT
  36. */
  37. public boolean isGoat(int a)
  38. {
  39. //TODO 6
  40. return board[a] == Piece.GOAT;
  41. }
  42. /**
  43. * Sets the location a on the board to GOAT
  44. */
  45. public void setGoat(int a)
  46. {
  47. //TODO 7
  48. board[a] = Piece.GOAT;
  49. }
  50. /**
  51. * Sets the location a on the board to TIGER
  52. */
  53. public void setTiger(int a)
  54. {
  55. //TODO 8
  56. board[a] = Piece.TIGER;
  57. }
  58. /**
  59. * Moves a piece by swaping the contents of a and b
  60. */
  61. public void swap(int a, int b)
  62. {
  63. //TODO 9
  64. Piece temp = board[a];
  65. board[a] = board[b];
  66. board[b] = temp;
  67. }
  68. }

This is AIPlayer

  1. import java.util.Random;
  2. import java.util.*;
  3. import java.io.*;
  4. import java.lang.*;
  5. public class AIplayer
  6. {
  7. private Random rn; // for random tiger or location selection
  8. private GameRules rul; // an instance of GameRules to check for legal moves
  9. private int[][] tigerLocs = {{-1},{-2},{-3}}; // location of tigers for convenience
  10. private int ntigers; // number of tigers placed
  11. private Board bd;
  12. private GameViewer gv;
  13. // A 2D Array that maintains legal goat eating moves that tigers can perform, e.g. {0,8,16} means
  14. // a tiger can jump from location 0 to 16 (or 16 to 0) to eat the goat at location 8
  15. private final int[][] legalEats = {{0,1,2},{0,7,6},{0,8,16},{1,9,17},{2,10,18},{2,3,4},{3,11,19},
  16. {4,5,6},{4,12,20},{5,13,21},{6,14,22},{7,15,23},{8,9,10},{8,15,14},
  17. {10,11,12},{12,13,14},{16,17,18},{16,23,22},{18,19,20},{20,21,22}};
  18. /**
  19. * Constructor for objects of class AIplayer.
  20. * Initializes instance variables.
  21. */
  22. public AIplayer()
  23. {
  24. // TODO 14
  25. ntigers = 0;
  26. }
  27. /**
  28. * Place tiger in a random VACANT location on the Board
  29. * Update the board, the tiger count and tigerLocs.
  30. */
  31. public void placeTiger(Board bd)
  32. {
  33. // TODO 15
  34. Random rn = new Random();
  35. int loc = rn.nextInt(23);
  36. if (bd.isVacant(loc) == false){
  37. placeTiger(bd);
  38. }
  39. if (bd.isVacant(loc) == true){
  40. bd.setTiger(loc);
  41. tigerLocs[ntigers][0] = loc;
  42. ntigers = ntigers + 1;
  43. System.out.println("A Tiger has appeared at location "+loc);
  44. }
  45. }
  46. /**
  47. * If possible to eat a goat, must eat and return 1
  48. * If cannot eat any goat, make a move and return 0
  49. * If cannot make any move (all Tigers blocked), return -1
  50. */
  51. public int makeAmove(Board bd)
  52. {
  53. if (eatGoat(bd)) return 1; // did eat a goat
  54. else if (simpleMove(bd)) return 0; // made a simple move
  55. else return -1; // could not move
  56. }
  57. /**
  58. * Randomly choose a tiger, move it to a legal destination and return true
  59. * if none of the tigers can move, return false.
  60. * Update the board and the tiger location (tigerLocs)
  61. */
  62. public boolean simpleMove(Board bd)
  63. {
  64. //TODO 21
  65. boolean result = false;
  66. Random rn = new Random();
  67. int tig = rn.nextInt(2);
  68. int loc = tigerLocs[tig][0];
  69. for (int z=23;z>=0; z--)
  70. {
  71. if (bd.isVacant(z)==true){
  72. System.out.println(loc+","+z);
  73. boolean isLegal = rul.isLegalMove(loc,z);
  74. if (isLegal == true)
  75. {
  76. bd.swap(loc,z);
  77. gv.drawBoard(0);
  78. result = true;
  79. return true;
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * If possible, eat a goat and return true otherwise return false.
  87. * Update the board and the tiger location (tigerLocs)
  88. */
  89. public boolean eatGoat(Board bd)
  90. {
  91. boolean eat = false;
  92. System.out.println(gv.locs[tigerLocs[1][0]][0]+","+gv.locs[tigerLocs[1][0]][1]);
  93. //for (int i = 0; i>=2; i++)
  94. //{
  95. // int loc = tigerLocs[i];
  96. // for (int b = 0; b>=23; b++)
  97. // {
  98. // if (loc == legalEats[b][0])
  99. // {
  100. // for (int c = 0; c >=23; c++)
  101. // {
  102. // if ((bd.isVacant(c) == true) && (legalEats[b][2] == c))
  103. // {
  104. // int goat = legalEats[b][1];
  105. /// if (bd.isGoat(goat))
  106. // {
  107. // bd.setVacant(b);
  108. // bd.setTiger(c);
  109. // gv.drawBoard(0);
  110. // eat = true;
  111. // }
  112. // }
  113. // }
  114. // }
  115. // }
  116. //}
  117. for (int i=0; i>=2; i++)
  118. {
  119. int loc = tigerLocs[i][0];
  120. System.out.println(loc);
  121. for (int j=0; j>=19; j++)
  122. {
  123. if ((loc==legalEats[j][0]) || (loc==legalEats[j][2]))
  124. {
  125. if (bd.isGoat(legalEats[j][1])==true)
  126. {
  127. bd.swap(legalEats[j][0],legalEats[j][2]);
  128. bd.setVacant(legalEats[j][1]);
  129. rul.addGoat(-1);
  130. eat = true;
  131. return true;
  132. }
  133. }
  134. }
  135. }
  136. return true;
  137. }
  138. }

答案1

得分: 1

我会从 // TODO 1 开始,然后是 // TODO 2,依此类推。你的课程协调员之所以放置它们在那里是有原因的。这项任务是基于你整个学期的实验和讲座内容。如果你不确定从哪里开始,可以从这些内容开始练习。任务应该是成对完成的,所以确保与你的伙伴交流,互相讨论想法。

对于更一般的建议,我会尝试比你现在开始的时间要早很多地开始任务。此外,要注意,你的课程协调员可以察觉出你是否让别人替你完成了任务,或者你是否只是从互联网上找到了答案。

附注:CITS1001 是唯一会使用 BlueJ 的单元。

英文:

I would start with the // TODO 1, then the // TODO 2, etc. Your unit coordinator put them there for a reason. The assignment builds off the lab-work and lectures you've had throughout the semester. Start with those for some practice if you are unsure of where to start. The assignment should be in pairs so make sure you speak with your partner and bounce ideas off each other.

For more general advice, I'd try start assignments a lot sooner than you seem to have. On top of that, be adviced that your unit coordinators can tell if you have gotten someone else to do your work for you and/or if you've just gotten solutions off the internet.

Sidenote: CITS1001 is the only unit where you'll be using BlueJ.

huangapple
  • 本文由 发表于 2020年10月17日 16:46:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64400614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定