Guest User

Untitled

a guest
Oct 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. package edu.iastate.cs228.hw1;
  2.  
  3.  
  4. /**
  5. *
  6. * Living refers to the life form occupying a square in a world grid. It is a
  7. * superclass of Badger, Empty, Fox, Grass, and Rabbit, and has two abstract
  8. * methods awaiting implementation.
  9. *
  10. */
  11. public abstract class Living
  12. {
  13. protected World world; // the world in which the life form resides
  14. protected int row; // location of the square on which
  15. protected int column; // the life form resides
  16.  
  17. // constants to be used as indices.
  18. protected static final int BADGER = 0;
  19. protected static final int EMPTY = 1;
  20. protected static final int FOX = 2;
  21. protected static final int GRASS = 3;
  22. protected static final int RABBIT = 4;
  23.  
  24. public static final int NUM_LIFE_FORMS = 5;
  25.  
  26. // life expectancies
  27. public static final int BADGER_MAX_AGE = 4;
  28. public static final int FOX_MAX_AGE = 5;
  29. public static final int RABBIT_MAX_AGE = 3;
  30.  
  31.  
  32. /**
  33. * Censuses all life forms in the 3 X 3 neighborhood in a world.
  34. * @param population counts of all life forms
  35. */
  36. protected void census(int population[ ])
  37. {
  38. // TODO
  39. //
  40. // Count the numbers of Badgers, Empties, Foxes, Grasses, and Rabbits
  41. // in the 3 by 3 neighborhood centered at this Living object. Store the
  42. // counts in the array population[] at indices 0, 1, 2, 3, 4, respectively.
  43.  
  44. for(int i = row-1; i <= row + 1; i++ )
  45. {
  46. for(int j = column - 1; j <= column + 1; j++)
  47. {
  48. if(i>0 && j > 0 && i < world.getWidth() && j < world.getWidth())
  49. {
  50. if(world.grid[row][column].who() == State.BADGER )
  51. population[BADGER] ++;
  52. else if(world.grid[row][column].who() == State.FOX )
  53. population[FOX] ++;
  54. else if(world.grid[row][column].who() == State.GRASS )
  55. population[GRASS] ++;
  56. else if(world.grid[row][column].who() == State.RABBIT )
  57. population[RABBIT] ++;
  58. else population[EMPTY] ++;
  59. }
  60. }
  61. }
  62. }
  63.  
  64.  
  65. /**
  66. * Gets the identity of the life form on the square.
  67. * @return State
  68. */
  69. public abstract State who();
  70. // To be implemented in each class of Badger, Empty, Fox, Grass, and Rabbit.
  71. //
  72. // There are five states given in State.java. Include the prefix State in
  73. // the return value, e.g., return State.Fox instead of Fox.
  74.  
  75.  
  76. /**
  77. * Determines the life form on the square in the next cycle.
  78. * @param wNew world of the next cycle
  79. * @return Living
  80. */
  81. public abstract Living next(World wNew);
  82. // To be implemented in the classes Badger, Empty, Fox, Grass, and Rabbit.
  83. //
  84. // For each class (life form), carry out the following:
  85. //
  86. // 1. Obtains counts of life forms in the 3X3 neighborhood of the class object.
  87.  
  88. // 2. Applies the survival rules for the life form to determine the life form
  89. // (on the same square) in the next cycle. These rules are given in the
  90. // project description.
  91. //
  92. // 3. Generate this new life form at the same location in the world wNew.
  93.  
  94.  
  95. }
Add Comment
Please, Sign In to add comment