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.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

check declaration of

char playerMove;

Inside UltimateStrategy
and all the consequences of its use

here's my Strategy code:
/**
 *
 *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';
		
	}

}


And my rock Paper Scissors Spock code:

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.
}

Does anybody know how to fix this code? My primary roadblock is that I don't know how to make "playerMove" from my Strategy the same as the "playerMove" in my applet...

Does anybody know how to fix this code? My primary roadblock is that I don't know how to make "playerMove" from my Strategy the same as the "playerMove" in my applet...

Can you elaborate what you mean by "make the same"? Do you mean that they must be the same value? Is there only one instance of each class? You can create get and set functions. Each instance needs to know about the other instance tio use the get and set functions. You may need to create a class data member of type Strategy in your LizardSpockApplet class and a class data member of type LizardSpockApplet in your Strategy class in order for one instance of one class to "get" and "set" the other instance of the other class.

my goal was to have the computer picks its move depending on what the player chooses. I'm not exactly sure how to get my Strategy to record what the player chooses and plug that character in to get what the computer will choose.

For example:
if my probability just happen to fall into a predetermined winPercent

public UltimateStrategy(int p){
		winPercent = p;
		losePercent = 20;
		randNumber = new Random();
	
	}

And the player chooses scissors...how would I get my strategy to record this and plug that in as playerMove to get the computer to play rock or spock?

my goal was to have the computer picks its move depending on what the player chooses. I'm not exactly sure how to get my Strategy to record what the player chooses and plug that character in to get what the computer will choose.

For example:
if my probability just happen to fall into a predetermined winPercent

public UltimateStrategy(int p){
		winPercent = p;
		losePercent = 20;
		randNumber = new Random();
	
	}

And the player chooses scissors...how would I get my strategy to record this and plug that in as playerMove to get the computer to play rock or spock?

How about passing the parameter on this function call?

computerMove = computerStrategy.nextMove();

Make it:

computerMove = computerStategy.nextMove (playerMove);

Change the nextMove function so it takes a character.

I used an abstract class called Strategy.
this is the code for it:

/**
 *
 *  Defines a strategy for the computer player.
 *  The Strategy specifies how the computer will choose Rock, Paper, Scisors, or Spock
 *
 */
public abstract class Strategy {
  final char moves[] = {'R', 'P', 'S', 'L', 'V'};  
  //this variable will be visible to all subclasses of Strategy
  
  /**
   *  Decides what play the computer should make next.
   *  @returns either R,P, S, L, or V.
   */
  public abstract char nextMove();
  
  
  }

I don't think I can make it
computerMove = computerStategy.nextMove (playerMove);

because it doesn't override the abstract method nextMove

Shouldn't failing to override a method of an abstract class give you an error? In any case its a logic error not to if you need that method. So implement it and then say any problems you're having.

This article has been dead for over six months. Start a new discussion instead.