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
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
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).
game.addPlayer(name);
game.addPlayer(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);
if(a0.equals(answer))
Answer
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;
//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";
game.setCurrentPlayer(0);
if(a0.equals(answer1))
game.correct();
else
game.incorrect();
game.setCurrentPlayer(1);
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()
if(a2.equals(answer3))
game.correct();
else
game.incorrect();
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;
//attributes
setSize(width, height);
setTitle(title);
setUndecorated(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//create and add objects
add(new GamePanel());
}
//background.jpg
//MisterX.jpg
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 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.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 {
//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);
}
}
}
}