|
 |
|
Dev Articles Community Forums
> Programming
> Java Development
|
Please help a newbie with game design-Java
Discuss Please help a newbie with game design-Java in the Java Development forum on Dev Articles. Please help a newbie with game design-Java Java Development forum discussing Java, JSP, J2ME. Get help with Java and all of its related languages, libraries and variations. If it’s Java-related, you can discuss it here.
|
|
 |
|
|
|

Dev Articles Community Forums Sponsor:
|
|

December 9th, 2003, 09:12 PM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
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++;
}}
|

December 9th, 2003, 10:37 PM
|
 |
I'm Internet Famous
|
|
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886
 
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.
|

December 9th, 2003, 10:55 PM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
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
|

December 11th, 2003, 01:05 PM
|
 |
I'm Internet Famous
|
|
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886
 
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?
|

December 11th, 2003, 03:28 PM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
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
|

December 11th, 2003, 05:35 PM
|
Frank The Tank!
|
|
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240
Time spent in forums: < 1 sec
Reputation Power: 17
|
|
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?
|

December 11th, 2003, 08:01 PM
|
 |
I'm Internet Famous
|
|
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886
 
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?
|

December 12th, 2003, 12:43 PM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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";
}
}
|

December 15th, 2003, 02:50 PM
|
Frank The Tank!
|
|
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240
Time spent in forums: < 1 sec
Reputation Power: 17
|
|
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.
|

December 15th, 2003, 04:41 PM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
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
|

December 18th, 2003, 11:02 PM
|
 |
I'm Internet Famous
|
|
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886
 
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?
|

December 19th, 2003, 10:49 AM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
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.
|

December 19th, 2003, 02:27 PM
|
 |
I'm Internet Famous
|
|
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886
 
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?
|

December 19th, 2003, 02:31 PM
|
Registered User
|
|
Join Date: Dec 2003
Location: PA
Posts: 7
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.
|
Developer Shed Advertisers and Affiliates
Thread Tools |
Search this Thread |
|
|
Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|