My hw is to create a strategy for this rock paper scissors game.
this is my code for the game itself:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*The RPs applt plays Rock paper
*scissor, lizard, Spock game and
*keeps track of how many times
*you won lost or tied
*/
public class LizardSpockApplet extends JApplet implements ActionListener {
private final char moves[] = {'R', 'P', 'S', 'L', 'V'};
public JRadioButton rock, paper, scissors, lizard, spock;
private JTextField display, winsDisplay;
private int totalWins, totalLost, totalTies;
Font tahomaBold = new Font("Tahoma", Font.BOLD, 16);
Font tahoma = new Font("Tahoma", Font.PLAIN, 14);
Strategy computerStrategy;
public void init() {
rock = new JRadioButton(" Rock ", true);
paper = new JRadioButton(" Paper ");
scissors = new JRadioButton(" Scissors ");
lizard = new JRadioButton(" Lizard ");
spock = new JRadioButton(" Spock ");
ButtonGroup rpsButtons = new ButtonGroup();
rpsButtons.add(rock);
rpsButtons.add(paper);
rpsButtons.add(scissors);
rpsButtons.add(lizard);
rpsButtons.add(spock);
JButton go = new JButton(" Go ");
go.setFont(tahoma);
go.addActionListener(this);
display = new JTextField(25);
display.setEditable(false);
display.setBackground(Color.yellow);
display.setFont(tahoma);
winsDisplay = new JTextField(25);
winsDisplay.setEditable(false);
winsDisplay.setBackground(new Color(190,140,255));
winsDisplay.setFont(tahomaBold);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(rock);
c.add(paper);
c.add(scissors);
c.add(lizard);
c.add(spock);
Component spacer = Box.createRigidArea(new Dimension(60,5));
c.add(spacer);
c.add(go);
c.add(spacer);
c.add(display);
c.add(winsDisplay);
computerStrategy = new UltimateStrategy(65);
}
public void actionPerformed(ActionEvent e) {
char playerMove, computerMove;
if (rock.isSelected())
playerMove = 'R';
else if (paper.isSelected())
playerMove = 'P';
else if (lizard.isSelected())
playerMove = 'L';
else if (spock.isSelected())
playerMove = 'V';
else // if (scissors.isSelected())
playerMove = 'S';
//replacted with Strategy.
//int k = (int)(Math.random() * 5);
//computerMove = moves[k];
computerMove = computerStrategy.nextMove();
int result = nextPlay(playerMove , computerMove);
String msg = " You said " + makeWord(playerMove) + ", I said " +
makeWord(computerMove);
if (result < 0) {
msg += " -- you win.";
totalWins++;
}
else if (result == 0) {
msg += " -- tie.";
totalTies++;
}
else {// if (result > 0)
msg += " -- You lose.";
totalLost++;
}
display.setText(msg);
winsDisplay.setText("Games won: " + totalWins + ", lost: " + totalLost +
", tied: " + totalTies);
}
String makeWord(char move) {
String word;
switch (move) {
case 'R': word = "ROCK"; break;
case 'P': word = "PAPER"; break;
case 'S': word = "SCISSORS"; break;
case 'L': word = "LIZARD"; break;
case 'V': word = "SPOCK"; break;
default : word = "Error";
}
return word;
}
/**
* returns -1 if the player wins,
* 0 if it's a tie, and 1 if the computer wins
* Examples:
* nextPlay('R', 'R') returns 0
* nextPlay('R', 'S') returns 1
* nextPlay('R', 'P') returns -1
*/
int nextPlay(char computerMove, char playerMove) {
if(computerMove == playerMove)
return 0;
if(computerMove == 'R') {
if(playerMove=='S' || playerMove=='L')
return 1;
else
return -1;
}
else if(computerMove == 'P') {
if(playerMove=='R' || playerMove=='V')
return 1;
else
return -1;
}
else if(computerMove == 'S') {
if(playerMove=='P' || playerMove=='L')
return 1;
else
return -1;
}
else if(computerMove == 'L') {
if(playerMove=='V' || playerMove=='P')
return 1;
else
return -1;
}
else {//computerMove =='V'
if(playerMove=='R' || playerMove=='S')
return 1;
else
return -1;
}
} //end of nextPlay method.
}
And this is my code for my Strategy:
/**
*
*This strategy will always allow the user to win at least 20% of the time
*While dictating the percent, p, that the computer
*will win. Whatever probability left will be random pick
*for the computer.
*/
import java.util.Random;
public class UltimateStrategy extends Strategy{
private int losePercent;
private int winPercent;
Random randNumber;
Random choice = new Random();
int c = choice.nextInt(101);
char playerMove;
/**
*Default constructor: the Computer will almost always win
*NOTE: winPercent cannot be greater than 80%
*/
public UltimateStrategy(int p){
winPercent = p;
losePercent = 20;
randNumber = new Random();
}
/**
*Choose the next move:
*Have a p percent chance of always
*picking the right option to win
*while having a 20 percent chance to
*let the User win. The rest is up to chance
*
*/
public char nextMove(){
if (randNumber.nextInt(101) < losePercent){
if (playerMove == 'R'){
if (c < 50.0)
return 'S';
else
return 'L';
}
else if (playerMove == 'P'){
if (c<50)
return 'R';
else
return 'V';
}
else if (playerMove == 'S'){
if (c < 50)
return 'P';
else
return 'L';
}
else if (playerMove=='L'){
if (c < 50)
return 'P';
else
return 'V';
}
else if (playerMove=='V'){
if (c <50)
return 'R';
else
return 'S';
}
}
/*
*
*Sets the conditions for when the computer wins.
*
*/
else if (randNumber.nextInt(101) > losePercent &&
randNumber.nextInt(101)<= (winPercent + losePercent +1)){
if (playerMove == 'R'){
if (c < 50.0)
return 'P';
else
return 'V';
}
else if (playerMove == 'P'){
if (c < 50.0)
return 'S';
else
return 'L';
}
else if (playerMove == 'S'){
if (c < 50.0)
return 'R';
else
return 'V';
}
else if (playerMove == 'L'){
if (c < 50.0)
return 'S';
else
return 'R';
}
else if (playerMove == 'V'){
if (c < 50.0)
return 'P';
else
return 'L';
}
}
return RandomStrategy();
}
/**
*randomly returns the char R, P, S, L, or V.
*They have equal probability
*
*/
public char RandomStrategy(){
if ((int)Math.random()*10<2)
return 'R';
else if ((int)Math.random()*10<4)
return 'P';
else if ((int)Math.random()*10<6)
return 'S';
else if ((int)Math.random()*10<8)
return 'L';
else
return 'V';
}
}
Everything complies fine...but when I run it, the applet isn't running the way it should be. For example, if I pick Rock, the computer will constantly pick rock and so forth.