Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* * */ package frogger; /** * This class represents a visual application for the frogger game. * @author jrsullins */ import javax.swing.*; import java.awt.*;

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

/* * */

package frogger; /** * This class represents a visual application for the "frogger" game. * @author jrsullins */

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

public class FroggerApp extends JFrame implements ActionListener {

// Member variables for visual objects. private JLabel[][] board; // 2D array of labels. Displays either # for player, // symbol for character, 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. private int width = 9; private int height = 9;

// Current location of player private int playerX = 5; private int playerY = 0;

// Array of character objects (will actually store subclass objects) private Character[] characters; private static final int CHARACTERS = 15;

public FroggerApp() {

// 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

// 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(200, 200); 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() {

// Construct array of character objects characters = new Character[CHARACTERS];

// Construct each character in the array, passing it its initial location // and other parameters as necessary. Polymorphism is used to store // subclass objects inot the supeclass array.

// Three patrollers on rows 2 and 6 characters[0] = new Patroller(1, 2, "left"); characters[1] = new Patroller(4, 2, "left"); characters[2] = new Patroller(7, 2, "left"); characters[3] = new Patroller(1, 6, "right"); characters[4] = new Patroller(4, 6, "right"); characters[5] = new Patroller(7, 6, "right");

// Chasers in either corner at bottom of screen characters[6] = new Chaser(0, 0); characters[7] = new Chaser(8, 0);

// Hiders in rows 3 and 5 characters[8] = new Hider(2, 3); characters[9] = new Hider(6, 3); characters[10] = new Hider(2, 5); characters[11] = new Hider(6, 5);

// One jumper each in rows 1, 4, and 7 characters[12] = new Jumper(2, 1); characters[13] = new Jumper(7, 4); characters[14] = new Jumper(3, 7);

// Call method to draw board drawBoard();

}

// Auxiliary method to display player and opponents in labels. public void drawBoard() {

// "Erase" previous board by writing " " in each label for (int y = 0; y

// Write the player onto the board. board[playerY][playerX].setText("#");

// Get location of each opponent and write its marker into that label. for (int i = 0; i

public void actionPerformed(ActionEvent e) {

// Determine which button was pressed, and move player in that // direction (making sure they don't leave the board). if (e.getSource() == southButton && playerY > 0) { playerY--; } if (e.getSource() == northButton && playerY

if (e.getSource() == eastButton && playerX 0) { playerX--; }

// Cause the opponents to take their particular action. for (int i = 0; i

// If one has captured the player, print a message and stop the game if (characters[i].getX() == playerX && characters[i].getY() == playerY) { JOptionPane.showMessageDialog(this, "You have been caught!"); gameFinished(); } }

// If the player has reached the top row, print a message and stop the game if (playerY == 8) { JOptionPane.showMessageDialog(this, "You have escaped!"); gameFinished(); }

// Redraw the board drawBoard();

}

// Auxiliary method to disable all buttons at end of game public void gameFinished() { northButton.setEnabled(false); southButton.setEnabled(false); eastButton.setEnabled(false); westButton.setEnabled(false); }

/** * @param args the command line arguments */ public static void main(String[] args) { FroggerApp a = new FroggerApp(); } }

-------------------------------------------under this line is my code but it's not working properly and I need help plz------------------------------------------

----------------------------------------Charachter

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package frogger;

/** * * @author */ public abstract class Character { private int x; private int y; public Character(int a,int b){ x=a; y=b; } protected int getX(){ return x; } protected int getY(){ return y; } protected void setX(int xCord) { x = xCord; } public abstract String getMarker(); public abstract boolean isVisible(); public abstract void act(int playerX, int playerY); }

----------------------------------------------------------Third

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package frogger;

/** * * @author */ public abstract class Third extends Character{ protected int turn; public Third(int a,int b){ super(a,b); turn=0; } protected abstract boolean timeToAct(); }

----------------------------------------------------------------Chaser

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package frogger;

/** * * @author */ public class Chaser extends Third{ public Chaser(int a,int b){ super(a,b); } public String getMarker(){ return "C"; } public boolean isVisible(){ return true; } protected boolean timeToAct(){ turn++; return turn % 3 == 0; } public void act(int x, int y) { // if our time, then act accordingly if (timeToAct()) { if (getX() > x) { x--; } else { x++; }

if (getY() > y) { y--; } else { y++; } } } }

--------------------------------------------------------------Hider

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package frogger;

/** * * @author */ public class Hider extends Character{ /** * s represents the state of Hider true for visible and false for invisible. */ private boolean s; public Hider(int a,int b){ super(a,b); s=true; } public String getMarker(){ return "H"; } public boolean isVisible(){ return s; } public void act(int x, int y) { s=!s; // switch visibility } }

-----------------------------------------------Patroller

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package frogger;

/** * * @author */ public class Patroller extends Character{ private String direction=""; public Patroller(int a, int b, String dir){ super(a,b); direction=dir; } public String getMarker(){ return "P"; } public boolean isVisible(){ return true; } @Override public void act(int x, int y) { switch (direction) { case "left": // if dir left if (getX() = 8) { setX(0); } else { x++; } break;

} } }

----------------------------------------------------Jumper

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package frogger;

import java.util.Random;

/** * * @author */ public class Jumper extends Third{ private int r; public Jumper(int a,int b){ super(a,b); Random rand = new Random(); r = rand.nextInt(8) + 1; } public String getMarker(){ return "J"; } public boolean isVisible(){ return true; } protected boolean timeToAct(){ turn++; return turn % 3 == 0; }

public void act(int x, int y) { // if our time, then act accordingly if (timeToAct()) { setX(r); // lets jump to our random number on x axis } } }

The FroggerApp Application I have provided a visual application called FroggerApp. java for you to use, which displays the game and handles the player It contains an array of character objects (using polymorphism), which it manipulates using loops. While you should not have to modify it, I encourage you to take a look at it to understand how the characters are constructed and the methods are called. You are to add a hierarchy of classes representing the creatures, based on the specifications below. Required Methods for All Character Types Since polymorphism will be used, there are a number of methods that must exist in each class in the hierarchy. Note that these methods can either be inherited from a superclass or overridden and implemented in the class itself. public int get This method returns the current column of the character public int getr This method returns the current row of the character public string getMarker This method returns the string displayed for each character type, used to draw the board (as in the example above) public boo isvisible This method returns true if the character is currently visible, false otherwise (important for Hiders). public void act (int playerx, int playery) This method is called every turn, and causes the character to take whatever action is specific to that type (moving, jumping, hiding, etc.) Note that this method takes the current location of the player as its parameters (the Chaser will need to use this) Also note that the application calls act for each character every turn. This means that it will be up to each character to know whether it is time for it to actually take its action (by keeping track of how many turns since it last acted)

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

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions

Question

Identify cultural barriers to communication.

Answered: 1 week ago