|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
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++;
}}
|
|
#2
|
||||
|
||||
|
Which line is receiving the error (and what's the error)?
I don't feel like compiling all that code. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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? |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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? |
|
#7
|
||||
|
||||
|
i'm curious, how is the computer choosing?
is it randomly set somewhere? |
|
#8
|
|||
|
|||
|
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"; } } |
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
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 |
|
#11
|
||||
|
||||
|
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? |
|
#12
|
|||
|
|||
|
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. |
|
#13
|
||||
|
||||
|
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?
|
|
#14
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Please help a newbie with game design-Java |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|