When creating the GUI for the game interface, use a layout of your choice with the appropriate text fields, labels and buttons to implement your design. The program should ask the player each question one at a time and allow the player to enter an answer. If the players answer matches the actual answer, the player wins the number of points for that question. If the players answer is incorrect, the player wins no points for that question and the program should show the correct answer. After the player has answered all 5 questions, the game is over and the program should display the players total score and then terminate. This is what I have so far any help
would be appreciated.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TriviaGame {
public static void main(String[] args, Question[] qArray) throws IOException {
// Constants
final int NUM_QUESTIONS = 5;
final int NUM_PLAYERS = 1;
// Variables
int playerTurn = 1; // The current player
int questionNum; // The current question number
int playerAnswer; // The player's chosen answer
int player1points = 0; // Player's points
// Create an array of Player objects for player #1 and player #2.
Player[] players = new Player[NUM_PLAYERS];
for (int i = 0; i < NUM_PLAYERS; i++) {
players[i] = new Player(i + 1);
}
// Create an array to hold Question objects.
Question[] questions = new Question[NUM_QUESTIONS];
// Initialize the array with data.
initQuestions(questions);
// Play the game.
for (int i = 0; i < NUM_QUESTIONS; i++) {
// Display the question.
TriviaGame.displayQuestion(qArray[i], playerTurn);
// Get the player's answer.
players[playerTurn - 1].chooseAnswer();
// See if the correct answer was chosen.
if (qArray[i].getCorrectAnswerNumber()
== players[playerTurn - 1].getCurrentAnswer()) {
players[playerTurn - 1].incrementPoints();
}
// See if the the player chose the wrong answer.
// do nothing
}
// Show the game results.
showGameResults(players);
/**
* The initQuestions method uses the contents of the trivia.txt file to
* populate the qArray parameter with Question objects.
*/
public static initQuestions(Question qArray[]) throws IOException {
// Open the trivia.txt file.
File file = new File("trivia.txt");
Scanner inputFile = new Scanner(file);
// Populate the qArray with data from the file.
for (int i = 0; i < qArray.length; i++) {
// Create a Question object in the array.
qArray[i] = new Question();
// Get the question text from the file.
qArray[i].setQuestion(inputFile.nextLine());
// Get the possible answers.
for (int j = 1; j <= 4; j++) {
qArray[i].setPossibleAnswer(inputFile.nextLine(), j);
}
// Get the correct answer.
qArray[i].setCorrectAnswerNumber(Integer.parseInt(inputFile.nextLine()));
}
}
public static void displayQuestion(Question q, int playerNum) {
// Display the player number
System.out.println("Question for player #" + playerNum);
System.out.println("------------------------");
// Display the question.
System.out.println(q.getQuestionText());
for (int i = 1; i <= 4; i++) {
System.out.println(i + ". " + q.getPossibleAnswer(i));
}
}
public static void showGameResults(Player[] players) {
// Display the stats.
System.out.println("Game Over!");
System.out.println("Player's points: " + players[0].getPoints());
// Declare the winner.
if (players[0].getPoints() > players[1].getPoints()) {
System.out.println("Player wins!");
}
}
public final class Question {
// Constant for the number of answers
public final int NUM_ANSWERS = 5;
// The trivia question
private String questionText;
// An array to hold possible answers.
private String possibleAnswers[] = new String[NUM_ANSWERS];
// The number (1, 2, 3, or 4) of the correct answer.
private int correctAnswer;
//Constructor
public Question() {
// Initialize all fields to "" or 0;
questionText = "";
correctAnswer = 0;
for (int i = 1; i < NUM_ANSWERS; i++) {
setPossibleAnswer("", i);
}
}
public void setQuestion(String question) {
this.questionText = question; //Sets the question
}
public void setPossibleAnswer(String text, int num) {
this.possibleAnswers[num] = text; //Sets possible Answer
}
public void setCorrectAnswerNumber(int num) {
this.correctAnswer = num; //Sets correct Answer
}
public String getQuestionText() {
return this.questionText; //Returns Question Text
}
public String getPossibleAnswer(int num) {
return this.possibleAnswers[num]; //Returns Possible Answer
}
public int getCorrectAnswerNumber() {
return this.correctAnswer; //Returns Correct Answer
}
public String getCorrectAnswer() {
return this.possibleAnswers[this.correctAnswer]; //Returns Possible Answer
}
}
public class Player {
private int playerNumber; // The player number
private int points; // Player's points
private int currentAnswer; // Current chosen answer
//Constructor
public Player(int playerNum) {
playerNumber = playerNum;
points = 0;
}
public void chooseAnswer() {
// Create a Scanner object for keyboard input.
// Get the user's chosen answer.
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter your Answer"); //Asks user for a number
this.currentAnswer = keyboard.nextInt();
}
public int getCurrentAnswer() {
return this.currentAnswer; //Returns Current Answer
}
public void incrementPoints() {
this.points++; //Increments the points
}
public int getPoints() {
return this.points; //Returns the points
}
}
public class Test {
{
MyPanel gui = new MyPanel();
gui.setVisible(true);
}
}
class MyPanel extends JFrame implements ActionListener {
private JPanel redPanel;
private JPanel whitePanel;
private JPanel bluePanel;
public MyPanel() {
super("Panel Demonstration");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel biggerPanel = new JPanel();
biggerPanel.setLayout(new GridLayout(1, 3));
add(biggerPanel, BorderLayout.CENTER);
redPanel = new JPanel();
whitePanel = new JPanel();
bluePanel = new JPanel();
redPanel.setBackground(Color.LIGHT_GRAY);
whitePanel.setBackground(Color.LIGHT_GRAY);
bluePanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(redPanel);
biggerPanel.add(whitePanel);
biggerPanel.add(bluePanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.LIGHT_GRAY);
buttonPanel.setLayout(new FlowLayout());
add(buttonPanel, BorderLayout.SOUTH);
JButton redButton = new JButton("Red");
JButton whiteButton = new JButton("White");
JButton blueButton = new JButton("Blue");
redButton.setBackground(Color.RED);
whiteButton.setBackground(Color.WHITE);
blueButton.setBackground(Color.BLUE);
redButton.addActionListener((ActionListener) this);
whiteButton.addActionListener((ActionListener) this);
blueButton.addActionListener((ActionListener) this);
buttonPanel.add(redButton);
buttonPanel.add(whiteButton);
buttonPanel.add(blueButton);
}
public void actionPerformed(ActionEvent e) {
String buttonString = e.getActionCommand(); //recovers the string from the
//button that was pressed
if (buttonString.equals("Red"))
redPanel.setBackground(Color.RED);
else if (buttonString.equals("White"))
whitePanel.setBackground(Color.WHITE);
else if (buttonString.equals("Blue"))
bluePanel.setBackground(Color.BLUE);
else
System.out.println("Unexpected error.");
}
private void add(JPanel buttonPanel, String SOUTH) {
throw new UnsupportedOperationException("Not yet implemented");
}
private void setLayout(BorderLayout borderLayout) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
}
This post has been edited by macosxnerd101: 01 December 2010 - 10:54 AM
Reason for edit:: Please use code tags

New Topic/Question
Reply




MultiQuote







|