Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSCI 3701: Advanced Object-Oriented Programming Assignment 2: Developing Java Classes Due Friday, October 5 Introduction This assignment is meant to introduce you to the design

CSCI 3701: Advanced Object-Oriented Programming Assignment 2: Developing Java Classes Due Friday, October 5

Introduction

This assignment is meant to introduce you to the design and development of simple Java classes for use in a visual application.

In this assignment, you are to implement two classes that represents parts of a very simple video game, for which I have provided the visual application:

A Player class, which represents information related to the user playing the game.

A Robot class, which represents information related to the game-controlled robots the player is attempting to avoid.

Game Structure and User Interface Class

This is a simple game in which the player (marked P) moves around a 2D map (currently 15x9) to attempt to reach the top row of the map, while avoiding robots that patrol it (each marked #).

The game provides buttons (N, S, E, and W) that moves the player one square in the direction north, south, east, or west respectively.

Every time the player moves, each robot may move as well, moving one square closer to the player. Whether or not a robot moves this turn is a random decision based on some probability stored in the robot (which may be different for each robot).

Each robot also has a limited amount of fuel (also possibly different for each robot). Every time the robot moves, its fuel decreases by 1. When it is out of fuel, it can no longer move (so one possible player strategy is to try to move around until the robots run out of fuel).

Whenever a robot is at the same location as the player, the player has been hit and that robot disappears from the game (having exploded). This does not end the game, but the player will keep track of the number of times they have been hit, and the game will display this at the end.

I have provided on the course web page a user interface class called RobotApp. You can use it by:

Creating a new Java application project in NetBeans called RobotGame (which will automatically create a package called robotgame).

Creating a class called RobotApp.

Copying and pasting the code into the RobotApp class.

Creating two other classes called Player and Robot.

Note that this application contains references to Player and Robot methods. I have commented those parts out for now (and have labelled which belong to which class). Once you have implemented the classes, you can uncomment the references to them in the RobotApp class.

While you may not understand everything this visual application does, I encourage you to take a look at the parts that refer to the Player and Robot methods, as they may help get a better understanding of the requirements for those classes. However, you are not to change any of the code in this class!

Requirements for the Player class

Since the Robot class depends on the Player class, you will need to implement it first.

Internal Representation

Abstractly, a Player object must keep track of the following information:

Its x and y location.

How many times it has been hit.

Constructor:

public Player(int x, int y, int width, int height) This constructor takes as its parameters:

o The initial x and y coordinates of the player.

o The width and height of the game board (you will need to know this in order to determine whether a move is legal).

Inspector Methods:

public int getX() This method returns the current X coordinate of the pod.

public int getY() This method returns the current Y coordinate of the pod.

public int getHits() This method returns the number of times the player has been hit..

Debugging Inspector

You are also required to provide a simple debugging inspector, which you may find extremely useful for testing and debugging your program:

public String toString() This method returns a String containing the current value of all member variables of the player.

Modifier Methods:

public void move(String direction) This method moves the player one square in the direction of the parameter direction. Specifically:

o The string north increment the Y location by 1.

o The string south decrement the Y location by 1.

o The string east increment the X location by 1.

o The string west decrement the X location by 1.

The only exception is if this would move the player off the board. In that case, the move method should not change the location.

An important note: Remember that arrays in Java (like C) are indexed from 0 to the size-1. This means you should make sure x stays between 0 and width-1, and y stays between 0 and height-1.

public void hit() This method should increment by 1 the number of times the player has been hit. As you will see in the next section, this method will be called by Robot objects.

Requirements for the Robot class

Internal Representation

Abstractly, a Robot object must keep track of the following information:

Its x and y location.

How much fuel it has left.

Whether or not it has exploded.

A reference to the Player object it is chasing. This reference will be used to call that Player objects methods.

Constructor:

public Robot(int x, int y, int fuel, Player player) This constructor takes as its parameters:

o The initial x and y coordinates of the robot.

o The initial fuel level of the robot.

o A reference to the Player object the robot is chasing.

Inspector Methods:

public int getX() This method returns the current X coordinate of the pod.

public int getY() This method returns the current Y coordinate of the pod.

public boolean isExploded() This method returns true if the pod has been caught yet, false otherwise.

Debugging Inspector

You are also required to provide a simple debugging inspector, which you may find extremely useful for testing and debugging your program:

public String toString() This method returns a String containing the current value of all member variables of the robot.

Modifier Methods:

public void move() This method moves the player one square in the direction of the current player location (unless it has already exploded or its fuel has reached 0).

This can be implemented with the following simple algorithm:

If the robots x is greater than the players x, decrement the robots x.

If the robots x is less than the players x, increment the robots x.

If the robots y is greater than the players y, decrement the robots y.

If the robots y is less than the players y, increment the robots y.

You can find the players location by using the Player reference to call its getX and getY methods.

After each move, the fuel level is decreased by 1.

Once the robot moves, if it is at the same location as the Player, the following happens:

The number of times the player has been hit is incremented. This can be done by calling the Player objects hit method.

The robot explodes. This means that its isExploded() method should return true from that point onwards.

Testing and Debugging

An important thing to note is that your Player and Robot classes are not meant to run independently, but as support classes for the RobotApp class I have provided. This means that you can only test your class by running RobotApp, which does not give a great deal of information about the states of the Player and Robot objects.

To isolate these errors, I strongly suggest that you use the toString debugging inspector listed in the required methods to display the current state of the objects if you are having errors or exceptions. Specifically, consider calling and printing the result of toString inside your move methods to make sure that the pod is where you expect it to be.

Documentation

You are also required to document your Player and Robot classes. This documentation is to be in the JavaDoc format described in class.

Specifically:

Give an overall description of the class itself at the beginning of the class.

Document each constructor and method in the class. Make sure that you describe each parameter and the return value in the JavaDoc format.

As always, the documentation is a significant portion of your grade!

What to turn in:

An electronic copy of your Player.java and Robot.java classes posted to the course Blackboard site. You can find those files in the src subdirectory of the RobotGame directory of NetBeansProjects.

Robotapp.java :

/* * This class represents a visual application for the "robot chase" game. */ package RobotGame;

/** * * @author jrsullins */

import javax.swing.*; import java.awt.*; import java.awt.event.*;

public class RobotApp extends JFrame implements ActionListener {

// Member variables for visual objects. private JLabel[][] board; // 2D array of labels. Displays either # for player, // * for pod, or empty space private JButton northButton, // player presses to move up southButton, // player presses to move down eastButton, // player presses to move right westButton; // player presses to move left // Current width and height of board (will make static later). private int width = 15; private int height = 15; // Player object /* Uncomment once you have created the Player class Player player; */ // Pod object stored in array for efficiency /* Uncomment once you have created the Robot class private Robot[] robots; */ int robotCount = 3; public RobotApp() { // Construct a panel to put the board on and another for the buttons JPanel boardPanel = new JPanel(new GridLayout(height, width)); JPanel buttonPanel = new JPanel(new GridLayout(1, 4)); // Use a loop to construct the array of labels, adding each to the // board panel as it is constructed. Note that we create this in // "row major" fashion by making the y-coordinate the major // coordinate. We also make sure that increasing y means going "up" // by building the rows in revers order. board = new JLabel[height][width]; for (int y = height-1; y >= 0; y--) { for (int x = 0; x < width; x++) { // Construct a label to represent the tile at (x, y) board[y][x] = new JLabel(" ", JLabel.CENTER); // Add it to the 2D array of labels representing the visible board boardPanel.add(board[y][x]); } } // Construct the buttons, register to listen for their events, // and add them to the button panel northButton = new JButton("N"); southButton = new JButton("S"); eastButton = new JButton("E"); westButton = new JButton("W"); // Listen for events on each button northButton.addActionListener(this); southButton.addActionListener(this); eastButton.addActionListener(this); westButton.addActionListener(this); // Add each to the panel of buttons buttonPanel.add(northButton); buttonPanel.add(southButton); buttonPanel.add(eastButton); buttonPanel.add(westButton); // Add everything to a main panel attached to the content pane JPanel mainPanel = new JPanel(new BorderLayout()); getContentPane().add(mainPanel); mainPanel.add(boardPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); // Size the app and make it visible setSize(300, 350); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Auxiliary method to create game setup createGame(); } // Auxiliary method used to create board. Sets player, treasure, and walls. public void createGame() { /* Uncomment once you have created the Player class player = new Player(7, 1, width, height); */ /* Uncomment once you have created the Robot class // Construct array of Robot objects robots = new Robot[robotCount]; // Construct each Robot in the array, passing it its initial location, // fuel, and a reference to the Player. robots[0] = new Robot(7, 12, 8, player); robots[1] = new Robot(3, 10, 12, player); robots[2] = new Robot(10, 10, 10, player); */ // Call method to draw board drawBoard(); } // Auxiliary method to display player and robots in labels. public void drawBoard() { // "Erase" previous board by writing " " in each label for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { board[y][x].setText(" "); } } /* Uncomment once you have created the Robot class // Get location of each robot and write # into that label (if not exploded). for (int p = 0; p < robotCount; p++) { if (!robots[p].isExploded()) { board[robots[p].getY()][robots[p].getX()].setText("#"); } } */ /* Uncomment once you have created the Player class // Write the player onto the board. board[player.getY()][player.getX()].setText("P"); */ } public void actionPerformed(ActionEvent e) { // Determine which button was pressed, and move player in that // direction (making sure they don't leave the board). /* Uncomment once you have created the Player class if (e.getSource() == southButton) { player.move("south"); } if (e.getSource() == northButton) { player.move("north"); } if (e.getSource() == eastButton) { player.move("east"); } if (e.getSource() == westButton) { player.move("west"); } */ /* Uncomment once you have created the Robot class // Move the pods and notify the pods about player location. Also invoke // method for random m direction changes. for (int p = 0; p < robotCount; p++) { robots[p].move(); } */ // Redraw the board drawBoard(); /* Uncomment once you have created the Player class if (player.getY() >= height-1) { gameOver(); } */ } public void gameOver() { /* Uncomment once you have created the Player class JOptionPane.showMessageDialog(this, "Reached exit with "+player.getHits()+ " hits"); */ northButton.setEnabled(false); southButton.setEnabled(false); eastButton.setEnabled(false); westButton.setEnabled(false); } /** * @param args the command line arguments */ public static void main(String[] args) { RobotApp a = new RobotApp(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions