7 Replies - 2055 Views - Last Post: 25 June 2012 - 07:52 PM Rate Topic: -----

#1 madason08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-June 12

Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 03:24 PM

Im having trouble repeating the command, i know i can copy and past several times so i can play the game again but our professor doesn't want us to do it. This is my first assignment and already having trouble with the repeat code. Everything else goes through. could anyone tell me what im doing wrong.

PS. Game is suppose to be based on a cartoon hence the Pokemon, lol

package rockpaperscissors;

/**
 * 
 * @author Manuel Rodriguez
 * @date 6-25-2012
 * 
 * A simple program to play rock paper scissors but with pokemon
 */

//imports
import javax.swing.JOptionPane;
import java.util.Random;

public class rockpaperscissors {

	public static void main(String[] args) {
	   
		String input;					   //stores user input from JOP
		int uchoice;						//parse of input; users choice
		int compchoice;					 //random choice by computer

		//prompts for user input and then parses it to uchoice
		input = JOptionPane.showInputDialog("Who will you choose? Bulbasaur, Charmander, or Squirtle?\n" +
				"1 for Bulbasaur, 2 for Charmander, and 3 for Squirtle: ");
		uchoice = Integer.parseInt(input);
		
		//generates random number and sets a limit
		Random randomnum = new Random ();
		
		compchoice = randomnum.nextInt(3);
		
		
		
		//determines whether the user or computer wins
		if (uchoice == 1 && compchoice == 0)
				JOptionPane.showMessageDialog(null, "Your Bulbasaur tide with Ash's Bulbasaur!");
		
		else if (uchoice == 1 && compchoice == 1)
				JOptionPane.showMessageDialog(null, "Ash's Charmander beats Your Bulbasaur. You lose!");
		
		else if (uchoice == 1 && compchoice == 2)
				JOptionPane.showMessageDialog(null, "Your Bulbasaur beats Ash's Squirtle. You win!");
		
		else if (uchoice == 2 && compchoice == 0)
				JOptionPane.showMessageDialog(null, "Your Charmander beats Ash's Bulbasaur. You win!");
		
		else if (uchoice == 2 && compchoice == 1)
				JOptionPane.showMessageDialog(null, "Your Charmander tied with Ash's Charmander!");
		
		else if (uchoice == 2 && compchoice == 2)
				JOptionPane.showMessageDialog(null, "Ash's Squirtle beats Your Charmander. You lose!");
		
		else if (uchoice == 3 && compchoice == 0)
				JOptionPane.showMessageDialog(null, "Ash's Bulbasaur beats Your Squirtle. You lose!");
		
		else if (uchoice == 3 && compchoice == 1)
				JOptionPane.showMessageDialog(null, "Your Squirtle beats Ash's Charmander. You win!");
		
		else if (uchoice == 3 && compchoice == 2)
				JOptionPane.showMessageDialog(null, "Your Squirtle tied with Ash's Squirtle!");
	   
		
		boolean play;
		String playAgain;
		do{
		
			int i = 0;
		      while(i < 4)
		    	  
		System.out.println("Play again?");
		playAgain = in.nextLine();
		if(playAgain.equals("yes")){
		play = true;
		}
		if(playAgain.equals("no")){
		play=false;
		}
		}while(play==true);

	}
	

}


Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble in repeating a rock paper scissors game

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 04:29 PM

This is an horrible design which does not take care of any problem segmentation

Please avoid
if(condition1 == 1 && condition2 == 1) {
}
else if(condition1 == 1 && condition2 == 2) {
}
else if(condition1 == 1 && condition2 == 3) {
}


test only once condition1 !!
if(condition1 == 1) {
    ifcondition2 == 1) {
    }
    ifcondition2 == 2) {
    }
    ifcondition2 == 3) {
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 Barem0to  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-June 12

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 05:46 PM

Hello. I'm a new programmer myself. But here's what I would do.

Create a different class called game and have a method called go in it that houses everything except for the last few lines starting with System.out.println("Play Again?").

And in my main class called start or something it'll look like this:

public class start{
                 public static void main(string[] args){
                             Game g;
                              While(play == true){
                                    g.go()
                                     sysoprintln("Play Again?")
                                   ааif(playAgain.equals("yes")){play = true};
                                     ifииииииииииииииииииии(no) play = false;
                              }
                }
}


Something like that. Im on my tablet by the way so the code might not show properly...

Once again Im a beginner so i dont know if thats right...
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1379
  • Posts: 3,039
  • Joined: 05-April 11

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 06:18 PM

You can just have a while or do-while loop in your static main method if you want to repeat. Just move your "do {" up, then I believe it could work.

If you did learn about classes and methods why didn't you use it?
A couple of methods will make your code easier to read and maintain, example:
startRound(boolean player1Start)

player.takeTurn()

//Returns null if draw
Player findWinner()

boolean playAgain()

You could even have a class for the different choices the players can make with a super class weapon/item.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 06:21 PM

don't want to disapoint you but you are far from running code

             public static void main(string[] args){  
                   Game g;  
                   While(play == true){   // by the way while() should be written with a capital W 
                        g.go()            // the Game object g is null. You can't reference its go() method


Was This Post Helpful? 0
  • +
  • -

#6 Barem0to  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-June 12

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 06:33 PM

View Postpbl, on 25 June 2012 - 06:21 PM, said:

don't want to disapoint you but you are far from running code

             public static void main(string[] args){  
                   Game g;  
                   While(play == true){   // by the way while() should be written with a capital W 
                        g.go()            // the Game object g is null. You can't reference its go() method




I know the w is capital and i said to make a to make a go method before the code snippet in my post... If you made a method go in class Game why eould you not be able to reference it?

Once again I'm on a tablet and I'm not going to write out full code on a touchscreen keyboard.

Also looking back the W was written with a capital..... -_-
Was This Post Helpful? 0
  • +
  • -

#7 Barem0to  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-June 12

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 06:56 PM

Wait a minute. Pbl you confused me... The w in a while loop isnt capitalized... Made a mistake in my original post.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,023
  • Joined: 15-July 08

Re: Having trouble in repeating a rock paper scissors game

Posted 25 June 2012 - 07:52 PM

pbl intended a lowercase "w" in the while loop. He was making a point of your null object.
Game g;
...
g.go();



is illegal because g is null...You have not yet created an object for it.

You need to instantiate it!
Game g = new Game(); // Or something like this.
...
g.go();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1