Question

I am struggling with writing a text-based adventure game with 3 classes. If someone could show me an example that would...

I am struggling with writing a text-based adventure game with 3 classes. If someone could show me an example that would be great, thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package Game;

import java.awt.*;

import java.awt.event.*;

import java.util.Random;

import sun.audio.*;

import java.io.*;

import javax.swing.*;

// Defines a class PlayMusic

class PlayMusic

{

// Method to play music

public static void music()

{

// Creates an object of class AudioPlayer

AudioPlayer MGP = AudioPlayer.player;

// Creates an object of class AudioStream

AudioStream BGM;

// Creates an object of class AudioData  

AudioData MD;

ContinuousAudioDataStream loop = null;

// try block begins

try

{

// Creates an audio stream from the Music.wav file

BGM = new AudioStream(new FileInputStream("F:/Workspace/Music.wav"));

// Gets the data and converts it to Audio stream

MD = BGM.getData();

// Loops Continuously to play the music

loop = new ContinuousAudioDataStream(MD);

}// End of try block

// Catch block to handle exception

catch(IOException error)

{

System.out.print("file not found");

}// End of catch block

// Calls the method to start music

MGP.start(loop);

}// End of method

}// End of class PlayMusic

// Defines class PlayGame

class PlayGame

{

// Declares container and component objects

JFrame jf;

JPanel jp1, jp2, jp3, mainP;

JLabel mainL, numberL, resultL;

JTextField resultT;

JButton calculateB, nextB;

// Random class object declared

Random rand;

// To store current level of the game

int level;

// To store first, second random number

int first, second;

// To store the calculate result and user current score

int result, score;

// To count number of questions asked

int counter;

// To store user name

String name;

// Default constructor definition

PlayGame()

{

// Calls the method to play the music

PlayMusic.music();

// Initializes level to 1

level = 1;

// Initializes score to 0

score = 0;

// Initializes question counter to 1

counter = 1;

// Creates Random class object

rand = new Random();

// Creates a frame

jf = new JFrame("Math Game");

// Creates a panels

jp1 = new JPanel();

jp2 = new JPanel();

jp3 = new JPanel();

mainP = new JPanel();

// Creates a labels

mainL = new JLabel("Level - " + level);

numberL = new JLabel();

resultL = new JLabel();

// Creates a text files

resultT = new JTextField(5);

// Creates a buttons

calculateB = new JButton("Show Result");

nextB = new JButton("Next Question");

// Adds main level to panel 1

jp1.add(mainL);

// Adds levels to panel 2

jp2.add(numberL);

jp2.add(resultT);

jp2.add(resultL);

// Adds buttons to panel 3

jp3.add(calculateB);

jp3.add(nextB);

// Adds panels main panel

mainP.add(jp1);

mainP.add(jp2);

mainP.add(jp3);

// Sets the layout of the main panel to 3 rows 1 column

mainP.setLayout(new GridLayout(3, 1));

// Adds the main panel to frame

jf.add(mainP);

// Sets the frame visible property to true

jf.setVisible(true);

// Set the size of the frame to width 400 and height 200

jf.setSize(400, 200);

// Sets the frame to center

jf.setLocationRelativeTo(null);

// Accepts user name

name = JOptionPane.showInputDialog("Enter your name: ");

// Calls the method to generate question

generateRandomNumber(level);

// Registers action listener to nextB button using anonymous class

nextB.addActionListener(new ActionListener()

{

// Overrides the actionPerformed() method of ActionListener interface

public void actionPerformed(ActionEvent ae)

{

// Clears the contents of result label and text field

resultT.setText("");

resultL.setText("");

// Calls the method to generate question

generateRandomNumber(level);

// Increase the question counter by one

counter++;

}// End of method

});// End of anonymous class

// Registers action listener to calculateB button using anonymous class

calculateB.addActionListener(new ActionListener()

{

// Overrides the actionPerformed() method of ActionListener interface

public void actionPerformed(ActionEvent ae)

{

// Checks if question counter is 3 set the level to 2

if(counter == 4)

level = 2;

// Otherwise checks if question counter is 6 set the level to 3

else if(counter == 7)

level = 3;

// Otherwise checks if question counter is 9 set the level to 4

else if(counter == 10)

level = 4;

// Otherwise checks if question counter is 12 set the level to 1

else if(counter == 13)

{

// Reset the level to 1

level = 1;

// Reset the question counter to 0

counter = 0;

// Displays error message

JOptionPane.showMessageDialog(jf, name + " Your Score: " + score,

"Result", JOptionPane.ERROR_MESSAGE);

// Reset the score to 0

score = 0;

}// End of else if condition

// Set the level number in main label

mainL.setText("Level - " + level);

// Checks if user entered result text field is null then display error message

if(resultT.getText() == "")

// Displays error message

JOptionPane.showMessageDialog(jf, name + " Please enter the result ",

"ERROR", JOptionPane.ERROR_MESSAGE);

// Otherwise user entered the answer

else

{

// Checks if calculated result is equals to user entered answer

if(result == Integer.parseInt(resultT.getText()))

{

// Increase the score by one

score++;

// Sets the congratulation message in result label

resultL.setText("Good Correct Answer");

}// End of if condition

// Otherwise wrong answer

else

// Sets the wrong message in result label and displays the corrent answer

resultL.setText("Sorry Wrong Answer" + "\n Correct Answer: " + result);

}// End of else

}// End of method

});// End of anonymous class

}// End of constructor

// Method to generate question

void generateRandomNumber(int currentLevel)

{

// Generates random number between 1 and 9 inclusive

first = rand.nextInt(9) + 1;

second = rand.nextInt(9) + 1;

// Checks if second number is greater than the first number then swap the numbers

if(second > first)

{

// Swapping process

int temp = first;

first = second;

second = temp;

}// End of if condition

// Checks if level is 1 then addition

if(currentLevel == 1)

{

// Sets the question to number label

numberL.setText(first + " + " + second);

// Calculates the result

result = first + second;

}// End of if condition

// Otherwise checks if level is 2 then subtraction

else if(currentLevel == 2)

{

// Sets the question to number label

numberL.setText(first + " - " + second);

// Calculates the result

result = first - second;

}// End of else if condition

// Otherwise checks if level is 3 then multiplication

else if(currentLevel == 3)

{

// Sets the question to number label

numberL.setText(first + " X " + second);

// Calculates the result

result = first * second;

}// End of else if condition

// Otherwise checks if level is 4 then division

else if(currentLevel == 4)

{

// Sets the question to number label

numberL.setText(first + " / " + second);

// Calculates the result

result = first / second;

}// End of else if condition

}// End of method

}// End of class PlayGame

// Driver class MathGame

public class MathGame

{

// main method definition

public static void main(String[] args)

{

// Calls the default constructor

new PlayGame();

}// End of main method

}// End of driver class MathGame

Input Enter your name: Mohan OK Cancel Math Game Level -1 9+8 17 Good Correct Answer Show Result Next Question 의 Math Game Le

Add a comment
Know the answer?
Add Answer to:
I am struggling with writing a text-based adventure game with 3 classes. If someone could show me an example that would...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • It would be great if someone can help me with this physics problem I struggling with,...

    It would be great if someone can help me with this physics problem I struggling with, Thanks!

  • Hello, I am struggling with this C# WPF application for one of my classes. I am...

    Hello, I am struggling with this C# WPF application for one of my classes. I am using visual studio to work on it. The description for the problem is as follows: Create an app that can open a text file. Each line of the file contains an arbitrary number of words separated by spaces. Display the count for how many words are in the line and the count how many non-space characters are in the line on the form /...

  • I am writing a program to play the game "Game of Life". I am hoping someone...

    I am writing a program to play the game "Game of Life". I am hoping someone can help me edit my sad program so that it will print the first board as well as the next generation. The first board is working just fine. It is the next generation that is not working and i'm not sure what I am doing wrong. Thanks in advance. public class Life {    public char FirstBoard[][];    public Life()    { FirstBoard =...

  • I was hoping that someone could help me through a discussion/check of a pedigree I had...

    I was hoping that someone could help me through a discussion/check of a pedigree I had to draw for a class project. I am struggling because there are multiple diseases to indicate a mechanism of inheritance for, and I'm getting confused with the instance of two parents affected by two separate diseases. In addition to this, I am supposed to indicate the likelihood of the first child of the last generation inheriting any of these diseases...which I believe I could...

  • I am trying to solve this, but getting lost with the units. Could someone help me...

    I am trying to solve this, but getting lost with the units. Could someone help me with the solution so I can check my answer? Thanks! 14.) Methane, CH4, diffuses in a given apparatus at the rate of 30 mLimin . At what rate would a gas with a molar mass of 100 diffuse under the same conditions?

  • I am really struggling with how to approach this program. Conway's Game of life, whereby, 1....

    I am really struggling with how to approach this program. Conway's Game of life, whereby, 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Submit...

  • I'm struggling with two simpler calc questions, and I would appreciate it if someone could walk...

    I'm struggling with two simpler calc questions, and I would appreciate it if someone could walk me through the steps on these to show me where I'm going wrong. Thank you. (2 pts) Consider the power series Find the radius of convergence R. If it is infinite, type "infinity" or "inf Answer: Rinf What is the interval of convergence? Use interval notation for an interval. Type the number, if it is only a single point. Type NONE if there it...

  • Im stuck. can someone please show me code on how i can make text appear on...

    Im stuck. can someone please show me code on how i can make text appear on html canvas. i want to create a form where someone for example put their name and when submit button is clicked on the form it appear on the canvas. i am using html javascript. please show code for javascript, canvas, and html. please let me know if im not making sense. Thank you

  • Can someone help me with this assignment. I am struggling with the formatting PART A Create...

    Can someone help me with this assignment. I am struggling with the formatting PART A Create a web page containing data of your choice and the following styles: A background image that repeats vertically on the right hand side of the page. The image should not scroll with the page contents. All the headings should be displayed in green and Arial fonts. Create at least three headings in your web page. A paragraph style with centered double-spaced text that displays...

  • The following image is a solution. I am struggling with the mathmatical concept. Can someone please...

    The following image is a solution. I am struggling with the mathmatical concept. Can someone please explain to me how they got 54 degree!? I keep getting 43 degree. Also how did they get (41.25-j16.25)ohms? I would appreciate a step by step of the conversion operation. 21 50+j75 tan 54°

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT