Answered You can buy a ready-made answer or pick a professional tutor to order an original one.
Question: Basic java please help QUESTION 1: Add code to ask user about the number of players (from 1 to 3) - use game.askForInt. Store the input in a variable numPlayers. QUESTION 2: Modify the code
Question: Basic java please help QUESTION 1: Add code to ask user about the number of players (from 1 to 3) - use game.askForInt. Store the input in a variable numPlayers. QUESTION 2: Modify the code below to ask for players' names and add them to the game in a for loop (up to numPlayers iterations). String name = game.askForText("What is player 0
This problem has been solved!
Basic java please help
QUESTION 1: Add code to ask user about the number of players (from 1 to 3) - use game.askForInt. Store the input in a variable numPlayers.
QUESTION 2: Modify the code below to ask for players' names and add them to the game in a for loop (up to numPlayers iterations).
String name = game.askForText("What is player 0 name?");
game.addPlayer(name);
name = game.askForText("What is player 1 name?");
game.addPlayer(name);
name = game.askForText("What is player 2 name?");
game.addPlayer(name);
QUESTION 3: Modify this part to ask questions and get answers in a for loop (up to numPlayers iterations)
game.setCurrentPlayer(0);
String answer = game.askForText(q0);
if(a0.equals(answer))
game.correct(); //display "Correct", increment score, change frame color to green
else
game.incorrect(); //display "incorrect", change frame color of player to red
game.setCurrentPlayer(1);
answer = game.askForText(q1);
if(a1.equals(answer))
game.correct();
else
game.incorrect();
game.setCurrentPlayer(2);
answer = game.askForText(q2);
if(a2.equals(answer))
game.correct();
else
game.incorrect();
- @
- ANSWER
import java.awt.Color;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;
import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;
@SuppressWarnings("serial")public class BDialog extends JDialog {
private JButton btnAnswer = new JButton("Answer"); private JButton btnExit = new JButton("Exit"); private JTextField txtAnswer = new JTextField(10); private JLabel lblMsg = new JLabel("This is a message"); private String result; public BDialog(JFrame frame) { super(frame, true); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setUndecorated(true); setLayout(new GridLayout(2, 1)); getContentPane().setBackground(Color.black);
//set font and color Font font = new Font("Arial", Font.BOLD, 20); lblMsg.setFont(font); lblMsg.setForeground(Color.CYAN); txtAnswer.setFont(font); btnAnswer.setFont(font); btnExit.setFont(font);
JPanel tmpPanel = new JPanel(); //tmpPanel.setBackground(Color.black); tmpPanel.setOpaque(false); tmpPanel.add(lblMsg); add(tmpPanel); tmpPanel = new JPanel(); //tmpPanel.setBackground(Color.black); tmpPanel.setOpaque(false); tmpPanel.add(txtAnswer); tmpPanel.add(btnAnswer); tmpPanel.add(btnExit); add(tmpPanel);
//pack(); MyHandler handler = new MyHandler(); btnAnswer.addActionListener(handler); txtAnswer.addActionListener(handler); btnExit.addActionListener(handler); } public String showInputDialog(String msg){ lblMsg.setText(msg); setVisible(true); return result; } public void showMessageDialog(String msg){ lblMsg.setText(msg); setVisible(true); } private class MyHandler implements ActionListener{ public void actionPerformed(ActionEvent e) { if(e.getSource() == btnExit){ if(JOptionPane.showConfirmDialog(null, "Really want to exit?","Confirmation", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) System.exit(0); }else{ result = txtAnswer.getText(); txtAnswer.setText(""); setVisible(false); } } }}
import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics2D;import java.util.ArrayList;
public class Game {public static final int WIDTH = 800, HEIGHT = 500, MAX_PLAYERS = 3, PLAYER_Y = 50;protected ArrayList<Player> players = new ArrayList<Player>(MAX_PLAYERS);private GameFrame frame = new GameFrame(this, "COSC 111 Jeopardy", WIDTH, HEIGHT);private BDialog dialog = new BDialog(frame);private Player currentPlayer;private Color responseColor = Color.white;
public Game(){ frame.setLocationRelativeTo(null); dialog.setLocationRelativeTo(frame); dialog.setSize(WIDTH-10, 100); dialog.setLocation(frame.getX()+5, frame.getY() + HEIGHT - 105); frame.setVisible(true);}public void print(String msg){ frame.repaint(); dialog.showMessageDialog(msg);}public String askForText(String msg){ frame.repaint(); String response = dialog.showInputDialog(msg); return (response==null || response.length()==0)? "" : response.trim();}public int askForInt(String msg, int min, int max){ msg += "(" + min + " to " + max + ")"; boolean found = false, msgModified = false; int num = 0; while(!found){ try{ num = Integer.parseInt(askForText(msg)); if(num<min || num>max) throw new Exception(); found = true; }catch(Exception e){ if(!msgModified) msg = "Invlid input. " + msg; msgModified = true; } } return num;}
public void addPlayer(String name){ Player player = new Player(name); int playerWidth = player.getImg().getWidth(); int distanceBetweenPlayers = (WIDTH - MAX_PLAYERS * playerWidth) / (MAX_PLAYERS + 1); int x = (players.size()+1) * distanceBetweenPlayers + players.size() * playerWidth; player.setX(x); player.setY(PLAYER_Y); players.add(player); frame.repaint();}public void clearPlayers(){ players.clear();}public void paint(Graphics2D g2){ //draw players for (int i = 0; i < players.size(); i++) players.get(i).paint(g2); //draw frame around selected player if(currentPlayer != null){ int px = currentPlayer.getX()-4; int py = currentPlayer.getY()-4; int pw = currentPlayer.getImg().getWidth()+8; int ph = currentPlayer.getImg().getHeight()+8; g2.setStroke(new BasicStroke(8)); g2.setColor(responseColor); g2.drawRect(px, py, pw, ph); } //who is turn it is if(currentPlayer != null){ g2.setColor(Color.yellow); String name = currentPlayer.getName() + "'s turn"; Font font = new Font("Arial", Font.BOLD, 28); g2.setFont(font); FontMetrics metrics = g2.getFontMetrics(font); int txtWidth = metrics.stringWidth(name); g2.drawString(name, (WIDTH-txtWidth)/2, 35); }}public void setCurrentPlayer(int pl) { currentPlayer = players.get(pl); responseColor = new Color(255, 160, 255);}public void correct() { responseColor = Color.green; frame.repaint(); if(currentPlayer != null) currentPlayer.incrementScore(); print("Correct (press Enter to continue).");}public void incorrect() { responseColor = Color.red; frame.repaint(); print("Sorry, that is incorrect answer (press Enter to continue).");}}
import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;
import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;
@SuppressWarnings("serial")public class GameFrame extends JFrame{private Game game;public GameFrame(Game game, String title, int width, int height) {this.game = game;//attributessetSize(width, height);setTitle(title);setUndecorated(true);setDefaultCloseOperation(EXIT_ON_CLOSE);//create and add objectsadd(new GamePanel());}
private class GamePanel extends JPanel{BufferedImage myImage;public GamePanel() {//setBackground(Color.BLACK);try {myImage = ImageIO.read(new File("background.jpg"));} catch (IOException e) {e.printStackTrace();}}public void paint(Graphics g) {super.paint(g);g.drawImage(myImage, 0, 0, this);game.paint((Graphics2D)g);}
}
}
//background.jpg//MisterX.jpg
public class Main { //This is your main class (executable class)static Game game; //This is a global variable (can be accessed everywhere in this class)public static void main(String[] args) { game = new Game(); //This statement resets the game
//REQ1: Modify the value of the two variables q0 and a0 to a real question and answer. String q0 = "Where is the biggest city in Canada by population?"; String a0 = "Toronto";
//REQ2: Create 4 more String variables, q1, a1, q2, and a2, for two more questions and answers. String q1 = "What is the official language in Argentina? "; String a1 = "Spanish";
String q2 = "What is the postal code of UBCO"; String a2 = "VIV 1V7";
//REQ3: Declare a variable name.
// Ask the user about player0's name (using game.askForText(message)). Store the user input in the variable name. Add player to the game (using game.addPlayer(playerName)). // Repeat the same thing for the other two players.String name1 = game.askForText("What is the first player's name ");game.addPlayer(name1);String name2 = game.askForText("What is the second player's name ");game.addPlayer(name2);String name3 = game.askForText("What is the third player's name ");game.addPlayer(name3);
game.setCurrentPlayer(0);
//REQ4: Ask player0 question q0 (using game.askForText(message)). read the answer into a String variable. // If player's input is equal to a0, then call game.correct(), otherwise call game.incorrect()
String answer1 = game.askForText(q0);
if(a0.equals(answer1)) game.correct();else game.incorrect();
game.setCurrentPlayer(1);
//REQ5: Ask player1 question q1 (using game.askForText(message)). read the answer into a String variable. // If player's input is equal to a1, then call game.correct(), otherwise call game.incorrect()
String answer2 = game.askForText(q1);
if(a1.equals(answer2)) game.correct();else game.incorrect();
game.setCurrentPlayer(2);
//REQ6: Ask player2 question q2 (using game.askForText(message)). Read the answer into a String variable. // If player's input is equal to a2, then call game.correct(), otherwise call game.incorrect()
String answer3 = game.askForText(q2);
if(a2.equals(answer3))game.correct();elsegame.incorrect();
System.exit(1); //This statement terminates the program}}
import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;
import javax.imageio.ImageIO;
public class Player {private String name;private int score, x, y;//private static Image img = Toolkit.getDefaultToolkit().getImage("MisterX.jpg");private BufferedImage img;public Player(String name){this.name = name;try {img = ImageIO.read(new File("MisterX.jpg"));} catch (IOException e) {}}
public void paint(Graphics2D g2){g2.drawImage(img, x, y, null);g2.setColor(new Color(255, 210, 0));Font font = new Font("Arial", Font.PLAIN, 18);FontMetrics metrics = g2.getFontMetrics(font);int nameWidth = metrics.stringWidth(name);g2.setFont(font);g2.drawString(name, x + (img.getWidth(null)-nameWidth)/2, y + img.getHeight(null)-5);
g2.setFont(new Font("Arial", Font.BOLD, 22));g2.setColor(new Color(150, 255, 255));g2.drawString("$" + score, x+10, y + img.getHeight(null)+55);}
public String getName() {return name;}public int getScore() {return score;}public void incrementScore(){score+=100;}public int getX() {return x;}public int getY() {return y;}public BufferedImage getImg() {return img;}public void setX(int x) {this.x = x;}public void setY(int y) {this.y = y;}public void setImg(BufferedImage img) {this.img = img;}}