Вы находитесь на странице: 1из 9

c  

c    
   
    
      
 
 



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.

It is recommended that you submit your project to PLATE after completing each task
in order to receive feedback about which parts of your program are correct/incorrect.


 c 

 

In BlueJ (or your preferred developmen t environment), create 4 classes: Game,


Player, Item and Exit.

Define the Game class so that a game has 1 player, 3 items and 1 exit. These
should be defined as 5 private fields within Game, with appropriate types and
appropriate names.

Also define appropriate fields in each of the other 3 classes: Player, Item and Exit.
To decide on appropriate fields, think of what data needs to be stored for each kind
of object.

Finally, define the constructor of the Game class to ask the user 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.

Your constructor should exactly match the following output:

è   
        

    

      O 

  

  
    R

   


   

  G

      O

      


    Œ
   
       D

      p  

    


    [

   


     r

   


       ‰

     


    
  


Note that the text in × is text that the user of the program will type in. In a later
task, there will be two modes: easy mode and hard mode. Choosing the mode will
have no effect at this stage, and you can assume that the user will always choose
easy mode.

Submit this now to PLATE to receive mar ks for this task, or to receive feedback on
mistakes in your output.


 

Define a method in class ^  called   which is public, takes no


parameters and returns a string of the form:

      ! 
  "  #$
 

The first 3 components display the names and positions of the 3 item objects. The
last part, ³player(1) ± 0 points´ displays information about the position of the player
(in this case, 1) and the number of points (in this case, 0). The exit is not shown in
this string.

This method   print the string; only return it as the method's result.

Hint: You can construct the string using string concatenations of the form:

" "%&' (%&  )&'   %&*

You can also use the  + class described in the text book. You must ‘ ,
however, use the 
  method.

Finally, it is recommended from a design point of view that you create a


separate   method in everyone one of your classes. For example, you can
also define a   method in your Player class as well, which only produces
the part of the string that looks like this: '
  "  #$
 % . And then your
main ^    method can invoke all of these other   methods to
do build each part of the whole string.


  

Define a method in class ^  called  which is public, takes no parameters


and returns nothing. This method should ask the user to type in some command,
either 'l' to move the player left, 'r' to move th e player right, or 'p' to pick up an item at
the current position.

Your method should follow exactly the following output format:

   ,,


 

Note that the  at the end in bold text represents text that the user types in. Your
program must print the prompt and then use a scanner to read the user's input.

The command will always be a single character. Unfortunately, Java's Scanner class
does not have a  -  method and so you need to use the following code:

   .)  /   0 $ 1

This will obtain the first character of whatever the user types.

After reading the user's input, your program should execute the appropriate
command. In this task, you can ignore the 'p' command and only support the 'l' and 'r'
commands.

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.

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.


   

Each item object has a certain number of points. For each item, you also need to
record whether the item has been picked up yet or not. Make sure you have
appropriate fields in your Item class to support this before continuing.

There are two methods that you need to modify to make this feature work.

First, you must update the   method as follows. Let's suppose that the
player has just picked up the apple and orange items. In the string, these items now
become replaced by a star: ³*´ as in the following string:

22 ! 
  "  #3
 

This string indicates that the first two items are now taken, and only the cherry item
remains.

Second, you must update your  method as follows.


If the user types in 'p', your program should send a message to the player object to
pick up whatever item exists at the player's position, if any. When the player picks up
that item, the points of that item are transferred to the player's points (th ey are added
to the player's existing points), and then the item is recorded as being picked up. If
the player is not standing on top of an item, then the 'p' command does nothing.

Submit to PLATE to receive your marks and feedback for this task.


 

Define a method in class ^  called  -   which is public, takes no


parameters and returns a )   . In each item, you should have a field to record
the fact of whether that item has been collected (i.e. picked up) or not. And in this
task, you must implement the game's  -   method to check all 3 items
and return true if all items have been collected, or return false otherwise.

Submit to PLATE to receive your marks and feedback for this task.


 

Update your ^    method so that if all items have been collected /
picked up, then the exit will now appear in the string, as follows:

222
  "  #"4
 56$75

The 56$75 at the end of this string indicates that the exit has appeared at position 0.

Also update your  method so that if the player moves to where the exit is, you
record somewhere the fact that the player has exited the game. You will need a
new )   field to store this information.

Submit to PLATE to receive your marks and feed back for this task.


! 

You will now implement several loops in your program. After completing each loop,
submit it to PLATE to receive marks and feedback for that loop.

ÔLoop 1: Define a method in class ^  called


  which is public, takes no
parameters, and returns nothing. This method repeatedly invokes
the  method until the player has reached the game's exit. When the
loop stops, the program should print the message, '-    8 
9 :% . You should also print o ut the   string once at the beginning
of the play method, and once after each time the player makes a turn. You
can test this method by right-clicking on the new object in BlueJ and selecting
the
  method.

ÔLoop 2: Update your   method in class ^  so that the points


value of each item is displayed as follows:

 2222    22  !2222222 


  "  #$
 

This string indicates that the apple is worth 4 points, the orange is worth 2
points, and the cherry is worth 7 points.

ÔWhen the user is asked whether to play in (e)asy mode or (h)ard mode, it is
possible for
the user to type in an invalid option such as 'Z'. Write a loop which repeatedly
asks the
user to select the mode until the user enters a valid option. Your output
should follow
the following format exactly:

è   
        
;   

Ôè   
       )

Ô;   

Ôè   
        
;   

Ôè   
        
    

After you have done this, define a class called J , which has a correctly
defined   method. This method should create a new ^  object, and then
invoke its
  method.


"  

This task is intended for students who are aiming for an HD in this subject.

Modify your program so that if the user selects hard mode, the game asks the user
to input the names, initial positions and initial directions for two enemy objects. We
shall use the number -1 to indicate the left direction, and 1 to indicate the right
direction. These enemy objects will automatically move left or right whenever the
player has a turn. If an enemy is heading left and reaches position zero, it will turn
around and begin heading right. If an enemy is heading right and reaches position
10, it will turn around and begin heading left. If the player and an enemy occupy the
same position at the same time, the player dies, the message ³Ouch! You were
eaten.´ is displayed, and the game stops.

Your program, when played in easy mode, should still have t he same behaviour as
before. When played in hard mode, the output should match exactly the behaviour
shown below:
è   
        
    

      

      


   
   
     

    



  

  
  

   


   



    

    


  !

   


    (

   "

  "  


  

  "     <"

   (

  (  


  ! 
  (    "

   


     $

     


    
 "

 =222>

= 22222>=!22>" ( ! 


  "  <$
 

   ,,




 =222>

= 22222>=!22>" ? ( "$ 


  (  <$
 

   ,,




 =222>

= 22222>=!22>"  ( ! 


    <$
 

@: 9 


If the player manages to avoid all enemies, the output may instead look like this:

è   
       A

    

      


      
  

   


     

    



  

  
  

   


   



    

    


  !

   


   (

   "

  "  


  ?

  "    "

   (

  (  


  4

  (     <"

   


     $

     


    
 "

 =222>

= 22222>=!22>" ? ( 4 


  "  <$
 

   ,,




 =222>

= 22222> =!22>" ( B 


  (  <$
 

   ,,




 =222>

= 22222>=!22>" B ( 


    <$
 

   ,,




2

= 22222>=!22>" 4 ( ? 


    <
 

   ,,




2

= 22222> =!22>" 3 (  


  ?  <
 

   ,,




2

= 22222>=!22>" ! ( ( 


   <
 

   ,,




22=!22>" "$ ( " 


   <3
 

   ,,




22=!22>" ! ( $ 
  B <3
 

   ,,




22=!22>" 3 ( " 


  4  <3
 

   ,,




22=!22>" 4 ( ( 
  3  <3
 

   ,,




22=!22>" B (  
  !  <3
 

   ,,




222"  ( ? 
  !  <"$
 56$75

   ,,




222" ? ( 
  3  <"$
 56$75

   ,,




222"  ( B 
  4  <"$
 56$75

   ,,




222" ( ( 4 
  B  <"$
 56$75

   ,,




222" " ( 3 


   <"$
 56$75

   ,,




222" $ ( ! 
  ?  <"$
 56$75

   ,,




222" " ( "$ 


    <"$
 56$75

   ,,




222" ( ( ! 
    <"$
 56$75

   ,,




222"  ( 3 
  (  <"$
 56$75

   ,,



222" ? ( 4 
  "  <"$
 56$75

   ,,




222" ( B 
  $  <"$
 56$75

-    8 9 :

ë  
#
 

c  
     
   

 
        
     



It is good design to separate all user interface code from the rest of your p rogram -
this allows you to easily replace your user interface with a different one without
affecting the rest of your program.

Create a class called X; containing exactly all of the user interface code for your
program. That is, any code that mentions   ,   or    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 X; class. The   methods themselves do not need to be moved into
the X; class, but the methods that print these strings do.

J
   
Your solution will be marked according to the following scheme:

     


 c   &
!  "  &
 #
$   
% #   !

& '   &
( ) 
* +  !&

, )  
   
-. & 
"
 
 /012    

    
3  
V

Вам также может понравиться