Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 9th, 2003, 09:12 PM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Please help a newbie with game design-Java

Hey,
I am having trouble with my design of a game called Rock, paper,scissors.
The user enters a letter to indicate their choice and the computer picks a value in the range of 1-3 and the 2 are compared.The design also has to show counts for computer wins,player wins and the ties.
I have this code and I am having errors. I'm a newbie to Java and need your assistance.
Please find my classes and the driver. I don't know where I am going wrong but my driver says that variable computerChoice may have not been initialized.
Code:
package rps;
import java.io.*;
public class computer {
String computerChoice;
int choiceType;
public computer(String ComputerChoice, int ChoiceType)
 {
   computerChoice=ComputerChoice;
 choiceType=ChoiceType;

  }
 public String knowComputerChoice()
 {
   return computerChoice;
 }
 public int knowChoiceType()
 {
return choiceType;
  }
 public String someMethod(String computerChoice) {
 if(choiceType==1)
   computerChoice="r";

 else if(choiceType==2)
   computerChoice="p";

 else
   computerChoice="s";

   return computerChoice;

 }}

package rps;

public class player {
  String playerEntry;
 
  public player(String PlayerEntry)
  {
    playerEntry=PlayerEntry;
    }
  public String knowPlayerEntry()
  {
    return playerEntry;

  
  }

    }
package rps;
public class decider {
 public static String decideWinner(String computerChoice,String playerEntry)
  {
    String winner;
if(computerChoice.equals(playerEntry)){
      winner = "Draw";
      System.out.println("Tie!");
    }
      else if(computerChoice.equals("r")&& playerEntry.equals("s")){
        winner="computer";
        System.out.println("Computer won!");
      }
          else if(computerChoice.equals("p")&&playerEntry.equals("r")){
            winner ="computer";
            System.out.println("Computer won!");
          }
          else if(computerChoice.equals("s")&&playerEntry.equals("p")){
            winner = "computer";
            System.out.println("Computer Won!");
          }
          else{winner="player";
            System.out.println("Player won!");
  }
  return winner;

  }}
import java.io.*;
import rps.*;
public class RockPaperScissorsDr {
  public static void main(String []args) throws IOException
  {
    computer Computer;
    player Player;
    decider Decider;
    String playerEntry,computerChoice,winner;
    int playerWins=0,computerWins=0,ties=0,choiceType=(int  )(Math.random()*3);
    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println("An invalid entry will close the game!");
    System.out.println("Please choose from the menu, the letter you would like to play");
    System.out.println("Enter r for rock,p for paper,s for scissors:");
    playerEntry=in.readLine().toLowerCase();
    if(!(playerEntry.equals("r")&&playerEntry.equals("s")&&playerEntry.equals("p")))
System.out.println("Invalid Entry! End of game!");
while(playerEntry.equals("r")&&playerEntry.equals("s")&&playerEntry.equals("p")){
      System.out.println("Computer's choice is: "+choiceType);
      decider.decideWinner(computerChoice,playerEntry);
          computerWins++;}
          playerWins++;
          ties++;

          }}

Reply With Quote
  #2  
Old December 9th, 2003, 10:37 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Which line is receiving the error (and what's the error)?
I don't feel like compiling all that code.

Reply With Quote
  #3  
Old December 9th, 2003, 10:55 PM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey,
The line is :decider.decideWinner(computerChoice,playerEntry);
In the driver, towards the end...It says:variable computerChoice may have not been initialized.
Thanks,
Madge

Reply With Quote
  #4  
Old December 11th, 2003, 01:05 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
I see you've got the line:
String playerEntry,computerChoice,winner;

however I can't see where the variable computerChoice is being set (within RockPaperScissorsDr)...

what's computerChoice supposed to have in it?
or am I overlooking a line?

Reply With Quote
  #5  
Old December 11th, 2003, 03:28 PM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi MadCowDzz,
It's supposed to hold the computer play(what the copmuter choses) like r for rock,s for scissors and p for paper.
So I'm not sure if I should say computerChoice="what"...to initialze it or if I should disturb my class again and try to put something else inside it but I will keep on tryin.
I like programming but Java is driving me nuts!
Thanks

Reply With Quote
  #6  
Old December 11th, 2003, 05:35 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
finesoul,

Try initializing your computerChoice variable to null when you first create it. The problem is that because you're not initializing computerChoice anywhere in your code, it's throwing your exception when it's expecting the variable in your called methods.

Btw, the only methods that set/initialize the variable are someMethod and computer; none of which are being called anywhere in your RockPaperScissorsDr class. Am I missing something?

Reply With Quote
  #7  
Old December 11th, 2003, 08:01 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
i'm curious, how is the computer choosing?
is it randomly set somewhere?

Reply With Quote
  #8  
Old December 12th, 2003, 12:43 PM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry 2 tiny winy problems left with While Loop

Hey,
I'm nearlydone.Thanks for all your help...
Now I have only 2 small problems. I need my counters to count separately for the different winnings. The other thing is for it stay open for the player to play and when they enter an invalid statement, it closes. I have done the most part, it's just a few lines I guess I am missing that I can't seem to figure out. I have pasted the appropriate class and the driver. Please help...I am nearly done! Are there things I need to change?

My driver:

import java.io.*;
import rps.*;
public class RockPaperScissorsDr {
public static void main(String []args) throws IOException
{

Player player;
Decider decider;
String playerEntry;
int playerWins=0,computerWins=0,ties=0;
System.out.println("An invalid entry will close the game!");
System.out.println("Please choose from the menu, the letter you would like to play");
BufferedReader in;
in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter r for rock,p for paper,s for scissors:");
playerEntry=in.readLine().toLowerCase();
System.out.println("Player played is "+playerEntry);
while(playerEntry!=null){
int computerChoice=(int)(Math.random()*2+1);
System.out.println("The computer played "+computerChoice);
if(!(!playerEntry.equals("r"))&&(!playerEntry.equals("s"))&&(!playerEntry.equals("p"))){
System.out.println("The winner is: " +Decider.decideWinner(computerChoice,playerEntry)) ;}
else
{ System.out.println("Player's choice is invalid.The game will close!");}
//System.out.println("To play again,please enter letter from menu: ");
//playerEntry=in.readLine().toLowerCase();
System.out.println("The computer won "+computerWins);
System.out.println("The player won "+playerWins);
System.out.println("There were "+ties+" ties. Thank you for playing");



}
}}
My class:

package rps;
public class Decider {
public static String decideWinner(int computerChoice,String playerEntry)
{
String winner;
int rock=1;
int paper=2;
int scissors=3;
if(computerChoice==1&&playerEntry.equals("r")&&computerChoice==2&&playerEntry.equals("p")&&
computerChoice==3&&playerEntry.equals("s")){
winner = "draw";


}
else if(computerChoice==1&& playerEntry.equals("r"))//rock breaks scissors
{
winner="computer";


}
else if(computerChoice==3&&playerEntry.equals("p"))//scissors cut paper
{
winner ="computer";

}
else if(computerChoice==2&&playerEntry.equals("r"))//paper covers rock
{
winner = "computer";

}
else{winner="You";
}
return winner;
}
public static String update(String winner,int computerWins,int playerWins,int tie){
if (winner.equals("computer")) {
computerWins++;
}
else if (winner.equals("you")) {
playerWins++;
}
else{tie++;}
return "draw";

}
}

Reply With Quote
  #9  
Old December 15th, 2003, 02:50 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 18
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
finesoul,

What are your counters? Which variables are they? Can you just give me a quick breakdown of what it is you're trying to do.

As for having the game stay "open" and only "close" when there's an invalid statement; what exactly do you mean by that?

Thanks.

Reply With Quote
  #10  
Old December 15th, 2003, 04:41 PM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi FrankieShakes,

My variables that need to count are computerWins, playerWins and ties.They increment each time they win like when the computer wins, computerWins has to add this count to itself and if there is a tie between the computer and the player then the tie adds 1 to its count and so on.
As for the invalid statement: The application ends when the user enters an invalid choice.
Thanks

Reply With Quote
  #11  
Old December 18th, 2003, 11:02 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
as for exiting the program...

after this block of code:

System.out.print("Enter r for rock,p for paper,s for scissors:");
playerEntry=in.readLine().toLowerCase();


Add this (off the top of my head):
if (playerEntry.equals("r") || playerEntry.equals("p") || playerEntry.equals("s")) {
System.exit(1);
} else {
// do stuff
}


Can you elaborate a bit on the counter question?

Reply With Quote
  #12  
Old December 19th, 2003, 10:49 AM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey MadCowz,
Thanks!
Each time the computer wins, it has to add the winnings of the computer, each time the player wins it counts the playersWinings and same for the ties.

Reply With Quote
  #13  
Old December 19th, 2003, 02:27 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
I can't find the specific line in your code, but where ever you're printing the results of the game, increment the appropriate counter... or are you doing this already and there's a problem?

Reply With Quote
  #14  
Old December 19th, 2003, 02:31 PM
finesoul finesoul is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: PA
Posts: 7 finesoul User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I had done it in my last class called Decider, check towards the end of my thread. I also tried it in my driver like you are saying to let it print and increment but that couldn't work either.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Please help a newbie with game design-Java


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap