Advertisement
Guest User

...Game

a guest
Sep 25th, 2011
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.55 KB | None | 0 0
  1. Programming Fundamentals (31267) & Object Oriented Programming (48023) - Assignment 1
  2. Due date: Monday, 3rd October 2011, 11:00 am.
  3. Value: 20%
  4. Topics: Data flow, OO programming basics, Control flow.
  5. Objectives: This assignment supports objectives 1-5.
  6. Introduction
  7. In Assignment 1 you will write a simple game in which the player must move left and right to collect stones and place them on a treasure chest in the right combination to unlock a treasure.
  8. Read all of the following sections carefully before starting the assignment.
  9. Program description
  10. In this game, there are 4 stones represented by 4 symbols (#, @, %, $) which have numeric values (1, 2, 3, 4) respectively.
  11. The current state of the game is displayed as a string:
  12.  
  13. Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)
  14. The numbers in parentheses indicate positions on the ground. Here, the player named “Jack” is at position 0, the # stone is at position 1, the @ stone is at position 2, the % stone is at position 3, the $ stone is at position 4 and the treasure chest is at position 5. The number /6\ for the treasure chest indicates the combination required to unlock the treasure.
  15. The player must move left and right, pick up stones one at a time, and place them on the treasure chest in the right order.
  16. If any 4 stones A,B,C,D are placed on the treasure chest in that order, this will trigger the treasure chest to calculate (A+B)*C (and ignoring D). If the numeric value calculated by this formula matches the required combination (which is “6” in the example above), then the treasure will unlock and the player wins.
  17. The player also has a maximum number of moves allowed to unlock the treasure. If the maximum available moves are used up without unlocking the treasure, then the player loses the game.
  18.  
  19. Sample output #1
  20. In the following sample output, the player gets the combination correct and wins the treasure. Note that anything shown in bold font indicates what the user has been asked to type in (using a Scanner). The player can choose from the following actions: move left (l), move right (r), pick up a stone (p) and drop a stone (d).
  21. Enter combination for the treasure chest (5-10): 6
  22. Enter maximum allowed moves: 100
  23. Enter player name: Jack
  24. Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)
  25. Move (l/r/p/d): r
  26. Jack(1) #(1) @(2) %(3) $(4) Treasure/6\(5)
  27. Move (l/r/p/d): r
  28. Jack(2) #(1) @(2) %(3) $(4) Treasure/6\(5)
  29. Move (l/r/p/d): p
  30. Jack@(2) #(1) (2) %(3) $(4) Treasure/6\(5)
  31. Move (l/r/p/d): r
  32. Jack@(3) #(1) (2) %(3) $(4) Treasure/6\(5)
  33. Move (l/r/p/d): r
  34. Jack@(4) #(1) (2) %(3) $(4) Treasure/6\(5)
  35. Move (l/r/p/d): r
  36. Jack@(5) #(1) (2) %(3) $(4) Treasure/6\(5)
  37. Move (l/r/p/d): d
  38. Jack(5) #(1) (2) %(3) $(4) Treasure/6\(5)
  39. Move (l/r/p/d): l
  40. Jack(4) #(1) (2) %(3) $(4) Treasure/6\(5)
  41. Move (l/r/p/d): p
  42. Jack$(4) #(1) (2) %(3) (4) Treasure/6\(5)
  43. Move (l/r/p/d): r
  44. Jack$(5) #(1) (2) %(3) (4) Treasure/6\(5)
  45. Move (l/r/p/d): d
  46. Jack(5) #(1) (2) %(3) (4) Treasure/6\(5)
  47. Move (l/r/p/d): l
  48. Jack(4) #(1) (2) %(3) (4) Treasure/6\(5)
  49. Move (l/r/p/d): l
  50. Jack(3) #(1) (2) %(3) (4) Treasure/6\(5) (Continued...)
  51. Move (l/r/p/d): l
  52. Jack(2) #(1) (2) %(3) (4) Treasure/6\(5)
  53. Move (l/r/p/d): l
  54. Jack(1) #(1) (2) %(3) (4) Treasure/6\(5)
  55. Move (l/r/p/d): p
  56. Jack#(1) (1) (2) %(3) (4) Treasure/6\(5)
  57. Move (l/r/p/d): r
  58. Jack#(2) (1) (2) %(3) (4) Treasure/6\(5)
  59. Move (l/r/p/d): r
  60. Jack#(3) (1) (2) %(3) (4) Treasure/6\(5)
  61. Move (l/r/p/d): r
  62. Jack#(4) (1) (2) %(3) (4) Treasure/6\(5)
  63. Move (l/r/p/d): r
  64. Jack#(5) (1) (2) %(3) (4) Treasure/6\(5)
  65. Move (l/r/p/d): d
  66. Jack(5) (1) (2) %(3) (4) Treasure/6\(5)
  67. Move (l/r/p/d): l
  68. Jack(4) (1) (2) %(3) (4) Treasure/6\(5)
  69. Move (l/r/p/d): l
  70. Jack(3) (1) (2) %(3) (4) Treasure/6\(5)
  71. Move (l/r/p/d): p
  72. Jack%(3) (1) (2) (3) (4) Treasure/6\(5)
  73. Move (l/r/p/d): r
  74. Jack%(4) (1) (2) (3) (4) Treasure/6\(5)
  75. Move (l/r/p/d): r
  76. Jack%(5) (1) (2) (3) (4) Treasure/6\(5)
  77. Move (l/r/p/d): d
  78. Jack(5) (1) (2) (3) (4) Treasure\6/(5)
  79. You unlocked the treasure!
  80.  
  81. Notice that when the player picks up a stone such as #, the # symbol disappears from its original position in the display string, and reappears next to the player's name.
  82. Sample output #2
  83. In the second sample output, the player fails to unlock the treasure within the required number of moves, and loses the game.
  84.  
  85. Enter combination for the treasure chest (5-10): 8
  86. Enter maximum allowed moves: 3
  87. Enter player name: Jill
  88. Jill(0) #(1) @(2) %(3) $(4) Treasure/8\(5)
  89. Move (l/r/p/d): r
  90. Jill(1) #(1) @(2) %(3) $(4) Treasure/8\(5)
  91. Move (l/r/p/d): p
  92. Jill#(1) (1) @(2) %(3) $(4) Treasure/8\(5)
  93. Move (l/r/p/d): r
  94. Jill#(2) (1) @(2) %(3) $(4) Treasure/8\(5)
  95. You lose.
  96.  
  97. Do not start the assignment yet! Keep reading!
  98. Solution requirements
  99. To receive any marks, your solution must meet the following minimum requirements:
  100. 1. The tasks described below must be implemented in order. That is, task 1 must be done first, followed by task 2, and so on. You will not be able to receive marks for later tasks unless you have completed the earlier tasks.
  101. 2. Your solution must use only the features of Java that are taught in this subject before the due date. For example, it must not use arrays (or equivalent), inheritance, exceptions, varargs, interfaces, or generics. This restriction is necessary so that we are able to assess your expertise in using fundamental programming techniques. Assignment 2 will allow you more freedom to use more advanced features of Java.
  102. 3. Your program's output must exactly match the output given by PLATE (see “Assignment Submission and Return”).
  103. 4. Your solution must not circumvent or trick PLATE's scoring system.
  104. Technical points
  105. 1. Your program should create ONLY ONE Scanner object, otherwise it may behave incorrectly when you upload your solution to PLATE.
  106. 2. Sometimes you will want to read a single character from the keyboard. To do this, you must first read the entire line as a String using Scanner.nextLine() and then use the String.charAt() method to get the first character from the string. The first character of a string str is obtained via the expression str.charAt(0) .
  107. 3. There is a well-known problem with using the Scanner.nextInt() method in combination with Scanner.nextLine(). To make sure you don't encounter any problems, you must clear the line buffer with a call to nextLine() after each call to nextInt(). Below is the correct way to read an integer from the keyboard:
  108.  
  109. System.out.print("Please enter a number: ");
  110. int number = keyboard.nextInt();
  111. keyboard.nextLine();
  112. System.out.println("The number is " + number);
  113.  
  114. Tasks
  115. This section describes each piece of functionality you must implement, and the order in which they must be implemented. Note that the functionality constitutes only half of your overall mark while design and coding style makes up the rest. Please read Section “Marking Scheme” for more details.
  116. You should submit your project to PLATE after completing each task in order to receive feedback about which parts of your program are correct/incorrect.
  117. Task 1: Initialisation
  118. The simplest solution requires at least 4 classes: Game, Player, Stone and TreasureChest.
  119. Before you write any code, think about what fields and methods (including get/set methods) are needed for each class and write them down in the space below:
  120. class Game
  121. - fields: e.g. player, treasureChest, ...
  122. - methods: e.g. turn(), ...
  123. class Player
  124. - fields:
  125. - methods:
  126. class Stone
  127. - fields:
  128. - methods:
  129. class TreasureChest
  130. - fields:
  131. - methods:
  132. (The above list is not worth marks, but it will help guide you to write code).
  133. You may add additional classes to your project if you can justify doing so. However, the simplest solution needs only 4 classes.
  134. Now you are ready to write your code. At each stage, click the compile button and fix any errors before continuing.
  135. 1. Create a BlueJ project.
  136. 2. Create your 4 classes and remove BlueJ's template code.
  137. 3. Declare all of the fields in each class.
  138. 4. Declare all of your methods. But leave the code empty for now, except for get/set methods. Write all of the code for all of your get/set methods now.
  139. Finally, declare a constructor for the Game class. This constructor should ask the user to enter a “combination” for the treasure (this is simply a number between 5-10), for the initial positions and information about all of the player, item and exit objects, and to then create these new objects and store them in the 5 fields of the game.
  140. Your constructor should be responsible for running only the initial “setup” part of the game:
  141. Enter combination for the treasure chest (5-10): 6
  142. Enter maximum allowed moves: 100
  143. Enter player name: Jack
  144. Submit this now to PLATE to receive marks for this task, or to receive feedback on mistakes in your output.
  145. Task 2: toString
  146. Define a method in class Game called toString() which is public, takes no parameters and returns a string of the form:
  147. Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)
  148. The format of this string was explained earlier.
  149. This method should not print the string, only return it as the method's result.
  150. Hint: You can construct the string using string concatenations of the form:
  151. return "string1” + “string2” + variable + “another string” + …
  152. You can also use the StringBuffer class described in the text book. You must not, however, use the sprintf method (since it uses the untaught varargs feature).
  153. Tip: it is recommended from a design point of view that you create a separate toString() method in every one of your classes. For example, you can also define a toString() method in your Player class as well, which only produces the part of the string that looks like this: “Jack(0)”. And then your main Game.toString() method can invoke all of the other toString() methods to build each part of the whole string.
  154. Task 3: Player's turn
  155. Define a method in class Game called turn() which is public, takes no parameters and returns nothing. This method allows the player to have his/her turn by asking the user to type in some command, either 'l' to move the player left, 'r' to move the player right, 'p' to pick up an item at the current position or 'd' to drop an item at the current position.
  156. Your method should follow exactly the following output format:
  157. Move (l/r/p/d): r
  158. Your program must print the prompt ”Move (l/r/p/d): ” and then use a scanner to read the user's input. (Read the Technical Points section to learn how to read a single character)
  159. After reading the user's input, your program should execute the appropriate command. In this task, you can ignore the 'p' and 'd' commands and just support the 'l' and 'r' commands.
  160. If the user types 'l' or 'r', your program should send a message to the player object to move one position to the left or right respectively.
  161. You do not need to try this entire task at once. Complete a little bit at a time, and submit to PLATE to see your progress and receive PLATE's feedback.
  162. Task 4: Picking up items
  163. When a player picks up a stone, it is removed from the ground, and held by the player. There should be three elements to your solution:
  164. 1. You will need a field in your player object to store the stone that the player is currently holding. You can set this to null whenever the player is not holding a stone.
  165. 2. Update the toString() method to correctly display whenever the player is holding a stone (according to the sample output above).
  166. 3. Update the turn() method to recognise the 'p' command to pick up a stone at the current position. This command should do nothing if the player is already holding a stone.
  167. Submit to PLATE to receive your marks and feedback for this task.
  168.  
  169. Task 5: Dropping items
  170. Extend your turn() method to recognise the 'd' command. If the player moves to the position of the treasure chest and inputs the 'd' command, the currently held stone (if any) should be placed into the treasure chest.
  171. The treasure chest is capable of storing up to 4 stones (i.e. 4 fields). If all of the fields are empty, the stone should be placed into the first field. If the first field already contains a stone, then the stone should be placed into the second field. If that is also already filled, try the 3rd field. And if that too is filled, then finally try to store it into the 4th field.
  172. When a stone is placed onto the treasure chest, it becomes invisible to the user.
  173. Implement this feature according to the sample output at the top of this document, and submit to PLATE to receive your mark and feedback for this task.
  174. Task 6: Game over
  175. Define a method in class Game called isOver() which is public, takes no parameters and returns a boolean. This method should return true in either of the following two situations (and in all other situations should return false):
  176. 1. All 4 stones have been placed on the treasure chest in the correct order (as explained in the “Program Description” section).
  177. 2. The player has used up all of the maximum allowed turns/moves and has not yet placed all 4 stones correctly.
  178. Submit to PLATE to receive your marks and feedback for this task.
  179. Task 7: Loops
  180. You will now implement some loops in your program. After completing each loop, submit it to PLATE to receive marks and feedback for that loop.
  181.  
  182. • Loop 1: Define a method in class Game called play() which is public, takes no parameters, and returns nothing. This method repeatedly invokes the turn() method until the game is over (see the previous task). When the loop stops, the program should print either “You unlocked the treasure!” or “You lose.” depending on whether the player won or lost. You should also print out the toString() string before/after each turn. You can test this method by right-clicking on the new object in BlueJ and selecting the play() method.
  183. • Loop 2: When the user is asked to specify the treasure chest combination, this number must be within the range 5-10. Write a loop which repeatedly asks the user to enter a number until the user enters a valid number within the range 5-10. Your output should follow the following format exactly:
  184. Enter combination for the treasure chest (5-10): 4
  185. Invalid combination.
  186. Enter combination for the treasure chest (5-10): 2
  187. Invalid combination.
  188. Enter combination for the treasure chest (5-10): 12
  189. Invalid combination.
  190. Enter combination for the treasure chest (5-10): 5
  191. Enter maximum allowed moves:
  192.  
  193. After you have done this, define a class called Main, which has a correctly defined main() method. This method should create a new Game object, and then invoke its play() method.
  194. Task 8: Picking up and dropping stones (advanced)
  195. Modify your program so that it is possible for the player to pick up stones that have already been placed on the treasure chest, and to place stones back onto the ground. This behaviour should be supported via the 'd' and 'p' commands.
  196. Because a treasure chest can store multiple stones at the same position, the player should always pick up the most recently placed stone first. For example, if 4 stones have already been placed on the treasure chest, then the 'p' command should pick up the 4th stone. The 'p' command has no effect if the treasure chest has no stones.
  197. The 'd' command can be used to drop a stone back on the ground at the current position, if the player is standing at a position between 1-4 and there is currently no stone already sitting at that position.
  198. Note: Unless you use code-reuse principles from the Week 7 lecture to implement this task, you may lose a significant amount of marks for “code reuse” (see the Marking Scheme section below).
  199. Submit to PLATE to receive your marks and feedback for this task.
  200. Bonus task - 3 marks
  201. If you have completed all of the above tasks, you may receive up to 3 bonus marks for completing this task (refer to the section "Marking Scheme" for details of how the bonus marks are awarded).
  202. It is good design to separate all user interface code from the rest of your program - this allows you to easily replace your user interface with a different one without affecting the rest of your program.
  203. Create a class called UI containing exactly all of the user interface code for your program. That is, any code that mentions Scanner, System.in or System.out should be moved into this class. To be eligible to receive the bonus marks, any given change to the user interface should be possible by making changes only to your UI class. The toString() methods themselves do not need to be moved into the UI class, but the methods that print these strings do.
  204. Marking scheme
  205. Your solution will be marked according to the following scheme:
  206. Correctness (100)
  207. Initialisation
  208. toString
  209. Player's turn
  210. Picking up items
  211. Dropping items
  212. Game over
  213. Loops
  214. Picking up/dropping stones 5 marks
  215. 15 marks
  216. 10 marks
  217. 15 marks
  218. 15 marks
  219. 15 marks
  220. 15 marks
  221. 10 marks
  222.  
  223. Coding style/Design (50)
  224. Style
  225. Object-oriented design
  226. Code reuse 10 marks
  227. 10 marks
  228. 30 marks
  229.  
  230. Bonus task (3)
  231.  
  232. Your coding style/design marks will be awarded as follows:
  233. • Style will be marked based on the use of correct indentation.
  234. • Object-oriented design will be marked based on whether you have appropriately moved code into your separate classes. Penalties will apply for placing code into the Game class that is more relevant to one of the other classes.
  235. • Code reuse will be marked based on how well you have used refactoring techniques to eliminate repeated code (see week 7). If you structure your program well, there should be very little to no repeated code.
  236. Your overall mark will be calculated using the formula:
  237. • your mark = correctness * (50 + design) / 100 + bonus
  238. If your mark is calculated to be greater than 100 due to the bonus, it will be reduced to 100.
  239.  
  240. Individual assessment
  241. This assignment must be done by yourself and not with anyone else. Group work is not allowed and will be considered as academic misconduct.
  242. Academic misconduct
  243. Working with another person on this assignment is not allowed and is considered as academic misconduct. Do not show other people your code, or look at other people's code, or discuss assignment code with other people; it is an offence to have a copy of someone else's assignment (before the submission date). Do not post your assignment on the web. Posting your assignment on the web and getting help through blogs, forums or other websites is considered to be an academic misconduct.
  244.  
  245. To detect plagiarism, the subject uses an online system called PLATE available at http://plate.it.uts.edu.au
  246.  
  247. Students may find it useful to consult The UTS Coursework Assessment Policy & Procedure Manual, at http://www.gsu.uts.edu.au/policies/assessment-coursework.html
  248. Expected work load
  249. It is expected that this assignment will take about 15 to 20 hours of work. A well-designed solution is expected to use approximately 150-200 lines of code, while a badly-designed solution may reach up to 300 lines of code. Some people may complete the task in 5 hours, and some may need 30 hours or more; there is a huge variation in students' experience and abilities.
  250. Frequently asked questions
  251. A FAQ (Frequently Asked Questions) will be maintained at UTSOnline/Assignments/Assignment 1/FAQ. FAQs and their answers will be posted there as they arrive. You should read the FAQ at least once before you hand in your solution, but to be safe check it every couple of days. Anything posted on the FAQ will be considered as part of the assignment specification. The FAQ will be frozen (no new entries) two days before the due date.
  252. Discussion board
  253. A discussion board has been set up on UTSOnline to discuss the assignment. It is important to read the rules below before using the discussion board. Posts that do not follow the rules will be ignored.
  254. You may discuss any task of the assignment; however you must not share actual code from the assignment on the board. The FAQ should always be checked before asking questions on the discussion board.
  255. Discussion board rules:
  256. • You must not share assignment code on the discussion board.
  257. • You must always set a relevant and descriptive subject line for your message.
  258. • Do not click the "reply" button to reply to another message if you are not intending to respond to that message. New questions should always be posted in reply to the root message of the thread.
  259. • Additional rules may be posted on the discussion board itself.
  260. Assignment submission and return
  261. Your assignment must be submitted as a JAR file through the PLATE system, online, at http://plate.it.uts.edu.au/. You may submit and resubmit as many times as you wish, before the due date, and PLATE will update your mark.
  262. Instructions for submitting to PLATE are displayed online at the PLATE website.
  263. WARNING! PLATE may become overloaded on or near the due date when many students load and test their solution at the last minute. This will not be considered as a valid reason for an extension. To be safe, you should aim to submit your solution well before the due date.
  264. Your marks for Assignment 1 will be available through PLATE within 3 weeks after the due date.
  265. Late submission
  266. The assignment is due at 11:00 am on Monday, 3rd October 2011. Each 24 hour period after this time is considered to be an extra day late. For example, a submission at 11:01 am on Monday, 3rd October is considered 1 day late, while a submission at 11:01 am on Tuesday, 4th October 2011 is considered 2 days late.
  267. Your assignment will lose 20% of its original mark per day late, unless permission has been given by the subject co-ordinator before the due date.
  268. If your performance in an assessment item or items has been affected by extenuating or special circumstances beyond your control you may apply for Special Consideration. Information on how to apply can be found at http://www.sau.uts.edu.au/assessment/consideration.html
  269. Model solution
  270. A model solution can be copied from UTSOnline a couple of weeks after the due date at Assignments/Assignment 1/Solution.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement