hi , i need fast help for java programming Modify the JRockPaperScissors program
ID: 3555471 • Letter: H
Question
hi , i need fast help for java programming
Modify the JRockPaperScissors program into a MVC structure.
Please help
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JRockPaperScissors extends JFrame implements ActionListener
{
JLabel greeting = new JLabel("Rock, Paper, Scissors");
JLabel promptLabel = new JLabel("Choose one button");
Font headlineFont = new Font("Helvetica", Font.BOLD, 30);
Font mediumFont = new Font("Helvetica", Font.BOLD, 18);
JButton rock = new JButton("Rock");
JButton paper = new JButton("Paper");
JButton scissors = new JButton("Scissors");
JLabel resultsLabel = new JLabel("");
JLabel result1 = new JLabel();
JLabel result2 = new JLabel();
JLabel result3 = new JLabel();
Container con = getContentPane();
int playerCount = 0;
int computerCount = 0;
int tieCount = 0;
final int LOW = 1;
final int HIGH = 3;
public JRockPaperScissors()
{
greeting.setFont(headlineFont);
con.add(greeting);
promptLabel.setFont(mediumFont);
con.add(promptLabel);
con.add(rock);
con.add(paper);
con.add(scissors);
resultsLabel.setFont(mediumFont);
con.add(resultsLabel);
con.add(result1);
con.add(result2);
con.add(result3);
con.setLayout(new FlowLayout());
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int computer;
String msg;
String userPick;
String computerPick;
computer = LOW + (int)(Math.random() * HIGH);
if(computer == 1)
computerPick = "rock";
else
if(computer == 2)
computerPick = "paper";
else
computerPick = "scissors";
if(e.getSource() == rock)
{
userPick = "rock";
if(computer == 1)
{
msg = "tie";
++tieCount;
}
else
if(computer == 2)
{
msg = "computer";
++computerCount;
}
else
{
msg = "you";
++playerCount;
}
}
else
if(e.getSource() == paper)
{
userPick = "paper";
if(computer == 2)
{
msg = "tie";
++tieCount;
}
else
if(computer == 3)
{
msg = "computer";
++computerCount;
}
else
{
msg = "you";
++playerCount;
}
}
else
{
userPick = "scissors";
if(computer == 3)
{
msg = "tie";
++tieCount;
}
else
if(computer == 1)
{
msg = "computer";
++computerCount;
}
else
{
msg = "you";
++playerCount;
}
}
con.remove(resultsLabel);
con.remove(result1);
con.remove(result2);
con.remove(result3);
resultsLabel.setText("------Results------");
result1.setText("You picked " + userPick + " ---- Computer picked " + computerPick);
result2.setText("Winner: " + msg);
result3.setText("You: " + playerCount + " Computer: " + computerCount +
" Ties: " + tieCount);
con.add(resultsLabel);
con.add(result1);
con.add(result2);
con.add(result3);
validate();
resultsLabel.setLocation(40, 70);
result1.setLocation(30,100);
result2.setLocation(30,120);
result3.setLocation(30,140);
}
public static void main(String [] args)
{
JRockPaperScissors jr = new JRockPaperScissors();
jr.setVisible(true);
jr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jr.setSize(550,300);
}
}
Explanation / Answer
Controller.java
import javax.swing.JFrame;
public class Controller {
public static void main(String[] args) {
View view = new View();
view.setVisible(true);
view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.setSize(550, 300);
}
}
Model.java
public class Model {
final int LOW = 1;
final int HIGH = 3;
private String msg;
private String userPick;
private int playerCount = 0;
private int computerCount = 0;
private int tieCount = 0;
private int computer = LOW + (int) (Math.random() * HIGH);
private String computerPick;
public String getComputerPick() {
return computerPick;
}
public void setComputerPick() {
String computerPick = "";
if (computer == 1)
computerPick = "rock";
else if (computer == 2)
computerPick = "paper";
else
computerPick = "scissors";
}
public void rock() {
userPick = "rock";
if (computer == 1) {
msg = "tie";
++tieCount;
} else if (computer == 2) {
msg = "computer";
++computerCount;
} else {
msg = "you";
++playerCount;
}
}
public void paper() {
userPick = "paper";
if (computer == 2) {
msg = "tie";
++tieCount;
} else if (computer == 3) {
msg = "computer";
++computerCount;
} else {
msg = "you";
++playerCount;
}
}
public void scissors() {
userPick = "scissors";
if (computer == 3) {
msg = "tie";
++tieCount;
} else if (computer == 1) {
msg = "computer";
++computerCount;
} else {
msg = "you";
++playerCount;
}
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getUserPick() {
return userPick;
}
public void setUserPick(String userPick) {
this.userPick = userPick;
}
public int getPlayerCount() {
return playerCount;
}
public void setPlayerCount(int playerCount) {
this.playerCount = playerCount;
}
public int getComputerCount() {
return computerCount;
}
public void setComputerCount(int computerCount) {
this.computerCount = computerCount;
}
public int getTieCount() {
return tieCount;
}
public void setTieCount(int tieCount) {
this.tieCount = tieCount;
}
public int getComputer() {
return computer;
}
public void setComputer(int computer) {
this.computer = computer;
}
}
View.java
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class View extends JFrame implements ActionListener {
JLabel greeting = new JLabel("Rock, Paper, Scissors");
JLabel promptLabel = new JLabel("Choose one button");
Font headlineFont = new Font("Helvetica", Font.BOLD, 30);
Font mediumFont = new Font("Helvetica", Font.BOLD, 18);
JButton rock = new JButton("Rock");
JButton paper = new JButton("Paper");
JButton scissors = new JButton("Scissors");
JLabel resultsLabel = new JLabel("");
JLabel result1 = new JLabel();
JLabel result2 = new JLabel();
JLabel result3 = new JLabel();
Container con = getContentPane();
public View() {
greeting.setFont(headlineFont);
con.add(greeting);
promptLabel.setFont(mediumFont);
con.add(promptLabel);
con.add(rock);
con.add(paper);
con.add(scissors);
resultsLabel.setFont(mediumFont);
con.add(resultsLabel);
con.add(result1);
con.add(result2);
con.add(result3);
con.setLayout(new FlowLayout());
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Model model = new Model();
model.setComputerPick();
if (e.getSource() == rock) {
model.rock();
} else if (e.getSource() == paper) {
model.paper();
} else {
model.scissors();
}
con.remove(resultsLabel);
con.remove(result1);
con.remove(result2);
con.remove(result3);
resultsLabel.setText("------Results------");
result1.setText("You picked " + model.getUserPick() + " ---- Computer picked "
+ model.getComputerPick());
result2.setText("Winner: " + model.getMsg());
result3.setText("You: " + model.getPlayerCount() + " Computer: " + model.getComputerCount()
+ " Ties: " + model.getTieCount());
con.add(resultsLabel);
con.add(result1);
con.add(result2);
con.add(result3);
validate();
resultsLabel.setLocation(40, 70);
result1.setLocation(30, 100);
result2.setLocation(30, 120);
result3.setLocation(30, 140);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.