I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!!
Programming Project #4 – Programmer Jones and the Temple of Gloom
Part 1
The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you will write, the hero is the intrepid Programmer Jones. His job is to enter the Temple of Gloom and retrieve the Golden Keyboard. Jones can take any path he wishes to find the Keyboard. There is just one slight problem, he must follow the EXACT same path out of the Temple or his core will be dumped. If you have never had your core dumped, trust me, you don't want it to happen.
The game starts in a green room within the temple. A 3-digit code must be entered to use the door to an adjacent room. That code must be re-entered on the return through the same door. The goal of the game is to move from room to room, each room has a unique color and the player will provide the code, until you reach the gold room. Once there you must pick up the keyboard and back track through the exact same rooms, entering the same codes, or you die.
To implement this project you are going to build a Room class and a Stack class. You must build your own custom Stack class that pushes Room objects containing colors/codes onto a stack when entering a room and pops them off when returning to a room in the Temple.
A few points about the game:
The Room class must have as a minimum a String member variable for the color and an int member variable for the 3-digit numeric code. I suggest using the simple get/set method technique for creating this class.
The Stack class must push and pop Room objects. The minimum data members are top and an array of room objects. The methods must be push(), pop(), peek() and empty().
The 'Dump' button on the screen is for testing and debugging purposes. When it is pressed 'dump' the stack to the console (not on the GUI screen). Do this by creating a second Stack object, popping everything off (and printing color/code) the original stack, and pushing the Room objects on the second Stack. When you are done, pop the Room objects from the second Stack and push them back onto the original. Thereby restoring the original Stack. Do not take short cuts with the 'Dump' command, do not just look at the objects in the original Stack without popping them.
The key point to remember is entering a room requires a push of
the current room color (not the new room color) and the player
supplied code. When the player returns to a room the color and code
must match the Room object popped from the stack.
The colors:
From the green room, there are doors to the brown, pink and blue
rooms.
From the pink room, there are doors to the green, brown and blue
rooms.
From the brown room, there are doors to the pink, green and red
rooms.
From the blue room there are doors to the green, pink and yellow
rooms
From the red room, there are doors to the brown and yellow
rooms.
From the yellow room there are doors to the red, blue and gold
rooms.
From the gold room, there is a door to the yellow room.
Part 2
Extreme ADT Modify your program to work 3 different Stack classes (Array, Link List, Java)
Research
First, look up the documentation for the Java Stack class using the online documentation. Google "Java Stack Class" and it is usually the top result.
You will find documentation on the various methods available, also check out how to copy a Stack object and how to tell when the Stack is empty.
This is how to do it:
Add an interface named MyStack to your project with these methods:
void push ( Room obj ) Room pop ( ) Room peek ( ) boolean empty ( )
If all your stack classes StackArray, StackList and StackJava implement this interface your main program can build and use a Stack object for ANY of them without changing the code.
Add a button or ask the user which Stack to use at the start of each game.
Make sure the StackJava classe uses generics to push/pop/peek
Room objects.
Implementation:
Below is a starting a program for this project. It contains everything you need to set up a window, enter subroutine names, and push/pop buttons. It contains NO code to create/modify/print a stack. That is your job. An obvious starting point is to create a project, copy and paste this code, and make sure you can compile and run this code AS IS. Then add your portion of the code.
/*************************************************************** Project Number 4 - Comp Sci 182 - Data Structures Start Code - Build your program starting with this code Snakes! I Hate Snakes! - Indiana Jones Copyright 2003,2005 Christopher C. Ferguson This code may only be used with the permission of Christopher C. Ferguson ***************************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Project4 extends JFrame implements ActionListener { private static int xpos=0,ypos=0;// place window at this position private static int xsize=700,ysize=500;// set window to this size // Private state variables. private JPanel northPanel,centerPanel; private JButton pushButton,popButton,dumpButton,exitButton; private JTextField colorField; private JTextField codeField; private JTextArea outputArea; ////////////MAIN//////////////////////// public static void main(String[] args) { Project4 tpo = new Project4(); } ////////////CONSTRUCTOR///////////////////// public Project4 () { addScreenComponents(); // put the stuff on the screen // Exit when the window is closed. i.e. when top right X box pressed addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(xsize,ysize); setLocation(xpos,ypos); setVisible(true); } public void addScreenComponents() { northPanel = new JPanel(); northPanel.add(new JLabel("Enter A Color: ")); colorField = new JTextField("",15); northPanel.add(colorField); northPanel.add(new JLabel("And A Code: ")); codeField = new JTextField("",5); northPanel.add(codeField); pushButton = new JButton("Push"); northPanel.add(pushButton); pushButton.addActionListener(this); popButton = new JButton("Pop"); northPanel.add(popButton); popButton.addActionListener(this); dumpButton = new JButton("Dump"); northPanel.add(dumpButton); dumpButton.addActionListener(this); exitButton = new JButton("Exit"); northPanel.add(exitButton); exitButton.addActionListener(this); getContentPane().add("North",northPanel); centerPanel = new JPanel(); outputArea = new JTextArea("Who Dares Enter.... The Temple of Gloom!",20,60); centerPanel.add(outputArea); getContentPane().add(centerPanel,"Center"); } ////////////BUTTON CLICKS /////////////////////////// public void actionPerformed(ActionEvent e) { if (e.getSource()== exitButton) { dispose(); System.exit(0); } if (e.getSource()== popButton) { String newcolor = colorField.getText(); outputArea.setText("Pop returning to " + newcolor); // add code to pop color off the stack, check that the color/code matches and change to that color room } if (e.getSource()== pushButton) { String newcolor = colorField.getText(); outputArea.setText("Push entering " + newcolor); // add code to push color/code ON the stack and change to that color room } if (e.getSource()== dumpButton) { System.out.println("Stack Contents Dump: "); // add code to print contents of Stack to the CONSOLE } } } // End Of Project4
Below is a JavaFX start code for this project. To use it make sure you create a JavaFX project/application and names the project 'Project4':
/*************************************************************** Project Number 4 - Comp Sci 182 - Data Structures Start Code - Build your program starting with this code Snakes! I Hate Snakes! - Indiana Jones Copyright 2015 Christopher C. Ferguson and Aaron Black This code may only be used with the permission of Christopher C. Ferguson ***************************************************************/ package project4; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.stage.Stage; public class Project4 extends Application { private static int xpos = 0, ypos = 0; // place window at this position private static int xsize = 700, ysize = 500; // set window to this size private Button pushButton, popButton, dumpButton; private TextField colorField, codeField; private TextArea outputArea; @Override public void start(Stage primaryStage) { // this pane contains the buttons and text fields that the user will interract with HBox topPane = new HBox(); topPane.setPadding(new Insets(10, 5, 5, 5)); // sets padding around the topPane topPane.setSpacing(10.0); // sets spacing between items in pixels topPane.setAlignment(Pos.CENTER_LEFT); // sets the alignment of items added to topPane // these are all of the items that are added to the HBox for display Label colorLabel = new Label(); // default empty label constructor colorLabel.setText("Enter a Color: "); // setting the label text after creation topPane.getChildren().add(colorLabel); // adding the label to the HBox colorField = new TextField(); // default empty text field constructor colorField.setPrefWidth(150.0); // sets size of colorField in pixels topPane.getChildren().add(colorField); // adding the textfield to the HBox Label codeLabel = new Label("Enter a Code: "); // label with values constructor topPane.getChildren().add(codeLabel); // adding codeField = new TextField(""); // textfield with starting value constructor (it's empty in this case) codeField.setPrefWidth(50.0); topPane.getChildren().add(codeField); pushButton = new Button(); // default empty button constructor pushButton.setText("Push"); // setting the button text after creation pushButton.setOnAction(new ButtonHandler()); // assigning a class to handle events from this button topPane.getChildren().add(pushButton); popButton = new Button("Pop"); // button with text constructor popButton.setOnAction(new ButtonHandler()); topPane.getChildren().add(popButton); dumpButton = new Button("Dump"); dumpButton.setOnAction(new ButtonHandler()); topPane.getChildren().add(dumpButton); Button exitButton = new Button("Exit"); exitButton.setOnAction(new ExitHandler()); // note that this is a different class to handle the button events topPane.getChildren().add(exitButton); // this pane contains the output displayed based on user actions Pane centerPane = new Pane(); // these are all of the items added to the pane for display outputArea = new TextArea(""); outputArea.setEditable(false); // outputArea.setPrefSize(xsize, ysize); outputArea.setText("Who dares enter... The Temple of Gloom!"); centerPane.getChildren().add(outputArea); // this pane contains the organization for the other panes BorderPane mainPane = new BorderPane(); mainPane.setTop(topPane); mainPane.setCenter(centerPane); // this scene makes up the main window of the project Scene scene = new Scene(mainPane, xsize, ysize); primaryStage.setTitle("Project 3"); primaryStage.setScene(scene); // adds the above scene to the window primaryStage.setX(xpos); // sets the window to open at this x position primaryStage.setY(ypos); // sets the window to open at this y position primaryStage.show(); } // ButtonHandler is one implementation of event handling. // Multiple buttons are registered to it, and the class itself determines the // necessary action based on who called it. // This approach benefits from global buttons, because the handler has to tell where the event came from. class ButtonHandler implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent e) { if (e.getSource() == popButton) { String newcolor = colorField.getText(); outputArea.setText("Pop returning to " + newcolor); // add code to pop color off the stack, check that the color/code matches and change to that color room } if (e.getSource() == pushButton) { String newcolor = colorField.getText(); outputArea.setText("Push entering " + newcolor); // add code to push color/code ON the stack and change to that color room } if (e.getSource() == dumpButton) { System.out.println("Stack Contents Dump: "); // add code to print contents of Stack to the CONSOLE } } } // ExitHandler is an alternate implementation of event handling // Each button is registered to a different handler, which performs the desired actions // This approach does not require global buttons. class ExitHandler implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { launch(args); } }
Project4.java
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.awt.Image;
import java.awt.MediaTracker;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Project4 extends Application {
private Room currentRoom;
// greenRoom = new Room("green"),pinkRoom = new Room("pink"),
// brownRoom = new Room("brown"),blueRoom = new
Room("blue"),redRoom = new Room("red"),
// yellowRoom = new Room("yellow"), goldRoom = new
Room("gold");
private Scene scene;
private Stack stack;
private boolean hasKey = false, gameWon = false;
private static int xpos = 0, ypos = 0; // place window at this
position
private static int xsize = 700, ysize = 500; // set window to this
size
private Button pushButton, popButton, dumpButton, arrayButton,
linkButton, javaButton, exitButton,
getKeyButton;
private TextField colorField, codeField;
// private TextArea outputArea;
private VBox vbox;
private Label txt, colorLabel;
private HBox topPane = new HBox();
private boolean usingArray = false, usingList = false, usingStack =
false;
@Override
public void start(Stage primaryStage) {
stack = new Stack();
// this pane contains the buttons and text fields that the user
will interract with
topPane.setPadding(new Insets(10, 5, 5, 5)); // sets padding around
the topPane
topPane.setSpacing(10.0); // sets spacing between items in
pixels
topPane.setAlignment(Pos.CENTER_LEFT); // sets the alignment of
items added to topPane
//Ask which stack they want to use
arrayButton = new Button("Array");
arrayButton.setOnAction(new ButtonHandler());
topPane.getChildren().add(arrayButton);
linkButton = new Button("Link");
linkButton.setOnAction(new ButtonHandler());
topPane.getChildren().add(linkButton);
javaButton = new Button("Java Stack");
javaButton.setOnAction(new ButtonHandler());
topPane.getChildren().add(javaButton);
exitButton = new Button("Exit");
exitButton.setOnAction(new ExitHandler()); // note that this is a
different class to handle the button events
topPane.getChildren().add(exitButton);
vbox = new VBox();
txt = new Label("Welcome to Programmer Jones! Select a data
structure.");
vbox.getChildren().add(txt);
// this pane contains the organization for the other panes
BorderPane mainPane = new BorderPane();
mainPane.setTop(topPane);
mainPane.setCenter(vbox);
// this scene makes up the main window of the project
scene = new Scene(mainPane, xsize, ysize);
primaryStage.setTitle("Project 4");
primaryStage.setScene(scene); // adds the above scene to the
window
primaryStage.setX(xpos); // sets the window to open at this x
position
primaryStage.setY(ypos); // sets the window to open at this y
position
primaryStage.show();
}
// ButtonHandler is one implementation of event handling.
// Multiple buttons are registered to it, and the class itself
determines the
// necessary action based on who called it.
// This approach benefits from global buttons, because the handler
has to tell where the event came from.
class ButtonHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e) {
if(e.getSource() == getKeyButton) {
hasKey = true;
txt.setText("You got the Golden Keyboard!!!");
topPane.getChildren().remove(getKeyButton);
}
if(e.getSource() == arrayButton) {
topPane.getChildren().remove(arrayButton);
topPane.getChildren().remove(linkButton);
topPane.getChildren().remove(javaButton);
topPane.getChildren().remove(exitButton);
colorLabel = new Label(); // default empty label constructor
colorLabel.setText("Enter a Color: "); // setting the label text
after creation
topPane.getChildren().add(colorLabel); // adding the label to the
HBox
colorField = new TextField(); // default empty text field
constructor
colorField.setPrefWidth(150.0); // sets size of colorField in
pixels
topPane.getChildren().add(colorField); // adding the textfield to
the HBox
Label codeLabel = new Label("Enter a Code: "); // label with values
constructor
topPane.getChildren().add(codeLabel); // adding
codeField = new TextField(""); // textfield with starting value
constructor (it's empty in this case)
codeField.setPrefWidth(50.0);
topPane.getChildren().add(codeField);
Room greenRoomStart = new Room("green");
currentRoom = greenRoomStart;
scene.getStylesheets().add("green.css");
txt.setText("Who dares enter... The Temple of Gloom! You are in the
Green room.\n" +
"Before you stands three doors of Brown Blue and Pink.");
pushButton = new Button(); // default empty button
constructor
pushButton.setText("Push"); // setting the button text after
creation
pushButton.setOnAction(new ButtonHandler()); // assigning a class
to handle events from this button
topPane.getChildren().add(pushButton);
popButton = new Button("Pop"); // button with text
constructor
popButton.setOnAction(new ButtonHandler());
topPane.getChildren().add(popButton);
dumpButton = new Button("Dump");
dumpButton.setOnAction(new ButtonHandler());
topPane.getChildren().add(dumpButton);
topPane.getChildren().add(exitButton);
}
if(e.getSource() == linkButton) {
txt.setText("This one does not work yet. ");
}
if(e.getSource() == javaButton) {
txt.setText("This one doesn't work sorry");
}
if (e.getSource() == popButton) {
String newcolor = colorField.getText();
int code;
//CATCH ANY NUMBERS THAT AREN'T THREE DIGITS, OR AREN'T A
NUMBER
try {
code = Integer.parseInt(codeField.getText());
if((code < 100 || code > 999)) {
txt.setText("Enter a 3 digit number!");
return;
}
}
catch(Exception e1) {
txt.setText("Enter a 3-digit number!");
return;
}
txt.setText("Pop returning to " + newcolor);
if(stack.empty() != true) {
if(stack.peek().getColor().toLowerCase().equals(newcolor.toLowerCase())
&& stack.peek().getKey() == code) {
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
currentRoom = stack.pop();
switch(currentRoom.getColor().toLowerCase())
{
case "green": scene.getStylesheets().add("green.css");
//THIS IS THE WIN CONDITION. Remove all buttons off the
screen
if(stack.empty() && hasKey) {
topPane.getChildren().remove(popButton);
topPane.getChildren().remove(pushButton);
topPane.getChildren().remove(colorField);
topPane.getChildren().remove(codeField);
txt.setText("YOU WON!!!! Restart to play again :)");
}
break;
case "pink": scene.getStylesheets().add("pink.css");
break;
case "brown": scene.getStylesheets().add("brown.css");
break;
case "blue": scene.getStylesheets().add("blue.css");
break;
case "red": scene.getStylesheets().add("red.css");
break;
case "yellow": scene.getStylesheets().add("yellow.css");
break;
case "gold": scene.getStylesheets().add("gold.css");
break;
}
}else {
//DEATH.
txt.setText("You died.");
topPane.getChildren().remove(pushButton);
topPane.getChildren().remove(popButton);
}
}else {
//DEATH.
txt.setText("You died.");
topPane.getChildren().remove(pushButton);
topPane.getChildren().remove(popButton);
}
}
if (e.getSource() == pushButton) {
String newcolor = colorField.getText().toLowerCase();
int code;
try {
code = Integer.parseInt(codeField.getText());
if((code < 100 || code > 999)) {
txt.setText("Enter a 3 digit number!");
return;
}
}
catch(Exception e1) {
txt.setText("Enter a 3-digit number!");
return;
}
if(!stack.empty())
if(code == stack.peek().getKey()) {
txt.setText("You used that code for the last room!");
return;
}
txt.setText("Push entering " + newcolor);
//THE INPUTED COLOR AND CODE IS ASSIGNED TO THE NEW ROOM AND PUSHED
TO THE STACK
switch(newcolor)
{
case "green":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("blue") ||
currentRoom.getColor().equals("brown") ||
currentRoom.getColor().equals("pink"))) {
txt.setText("That door isn't here.");
return;
}
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
txt.setText("Before you stands Brown Blue and Pink doors.");
currentRoom.setKey(code);
stack.push(currentRoom);
Room greenRoom = new Room("green");
currentRoom = greenRoom;
break;
case "pink":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("brown") ||
currentRoom.getColor().equals("red") ||
currentRoom.getColor().equals("blue"))) {
txt.setText("That door isn't here.");
return;
}
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
txt.setText("Before you stands Brown Blue and Green doors.");
currentRoom.setKey(code);
stack.push(currentRoom);
Room pinkRoom = new Room("pink");
currentRoom = pinkRoom;
break;
case "brown":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("green") ||
currentRoom.getColor().equals("red") ||
currentRoom.getColor().equals("pink"))) {
txt.setText("That door isn't here.");
return;
}
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
txt.setText("Before you stands Green Red and Pink doors.");
currentRoom.setKey(code);
stack.push(currentRoom);
Room brownRoom = new Room("brown");
currentRoom = brownRoom;
break;
case "blue":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("green") ||
currentRoom.getColor().equals("yellow") ||
currentRoom.getColor().equals("pink"))) {
txt.setText("That door isn't here.");
return;
}
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
txt.setText("Before you stands Green Yellow and Pink
doors.");
currentRoom.setKey(code);
stack.push(currentRoom);
Room blueRoom = new Room("blue");
currentRoom = blueRoom;
break;
case "red":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("brown") ||
currentRoom.getColor().equals("yellow"))) {
txt.setText("That door isn't here.");
return;
}
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
txt.setText("Before you stands Brown and Yellow doors.");
currentRoom.setKey(code);
stack.push(currentRoom);
Room redRoom = new Room("red");
currentRoom = redRoom;
break;
case "yellow":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("blue") ||
currentRoom.getColor().equals("red") ||
currentRoom.getColor().equals("gold"))) {
txt.setText("That door isn't here.");
return;
}
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
txt.setText("Before you stands Red Blue and Gold doors.");
currentRoom.setKey(code);
stack.push(currentRoom);
Room yellowRoom = new Room("yellow");
currentRoom = yellowRoom;
break;
case "gold":
//Logic for being able to enter the right rooms
if(!(currentRoom.getColor().equals("yellow"))) {
txt.setText("That door isn't here.");
return;
}
txt.setText("Before you stands the Yellow door.");
getKeyButton = new Button("Keyboard!!!!");
getKeyButton.setOnAction(new ButtonHandler());
topPane.getChildren().add(getKeyButton);
scene.getStylesheets().remove(currentRoom.getColor() +
".css");
currentRoom.setKey(code);
stack.push(currentRoom);
Room goldRoom = new Room("gold");
currentRoom = goldRoom;
break;
default: txt.setText("That's not a room! You are in the " +
currentRoom.getColor()
+ " room.");
}
//CHANGE THE ROOM COLOR
switch(currentRoom.getColor().toLowerCase())
{
case "green": scene.getStylesheets().add("green.css");
break;
case "pink": scene.getStylesheets().add("pink.css");
break;
case "brown": scene.getStylesheets().add("brown.css");
break;
case "blue": scene.getStylesheets().add("blue.css");
break;
case "red": scene.getStylesheets().add("red.css");
break;
case "yellow": scene.getStylesheets().add("yellow.css");
break;
case "gold": scene.getStylesheets().add("gold.css");
break;
}
}
if (e.getSource() == dumpButton) {
Stack temp = new Stack();
System.out.println("Stack Contents Dump: ");
// add code to print contents of Stack to the CONSOLE
if(stack.empty())
System.out.println("The Stack is empty.");
else
for(int i = stack.getTop(); i > -1; i--) {
System.out.println(i + ": " + stack.peek().getColor() + " " +
stack.peek().getKey());
temp.push(stack.pop());
}
for(int i = temp.getTop(); i > -1; i--)
stack.push(temp.pop());
if(hasKey)
System.out.println("You have the keyboard.");
else
System.out.println("You don't have the keyboard");
}
}
}
// ExitHandler is an alternate implementation of event
handling
// Each button is registered to a different handler, which performs
the desired actions
// This approach does not require global buttons.
class ExitHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
Please do up vote.
Thanks.
Get Answers For Free
Most questions answered within 1 hours.