Second, please post your error messages exactly as they appear.
19 Replies - 3471 Views - Last Post: 15 April 2015 - 01:05 PM
#16
Re: Help with Rock, Paper, Scissors, Lizard, Sock project
Posted 13 April 2015 - 08:56 PM
First, please use code tags when posting code:
.
Second, please post your error messages exactly as they appear.
Second, please post your error messages exactly as they appear.
#17
Re: Help with Rock, Paper, Scissors, Lizard, Sock project
Posted 14 April 2015 - 03:58 AM
Are you sure you've never covered methods. Because, if you've done enums...
Anyway, an enum has most of what you're looking for for free.
e.g.
Result:
Pay attention to valueOf. You should be able to turn user input into your enum and also randomly pick an enum. Now, armed with two enums, you can compare apples to apples. You should also be able to print a list of choices using that enum, so none of this int Rock =0, duplication in the code.
Hope this helps.
Anyway, an enum has most of what you're looking for for free.
e.g.
enum GameChoice { Rock, Paper, Scissors, Lizard, Spock };
System.out.println("Size: " + GameChoice.values().length);
for(GameChoice choice : GameChoice.values()) {
System.out.println(choice + " : " + choice.name() + " : " + choice.ordinal());
}
System.out.println("Parse test");
GameChoice choice = GameChoice.valueOf("Paper");
System.out.println(choice + " : " + choice.name() + " : " + choice.ordinal());
Result:
Size: 5 Rock : Rock : 0 Paper : Paper : 1 Scissors : Scissors : 2 Lizard : Lizard : 3 Spock : Spock : 4 Parse test Paper : Paper : 1
Pay attention to valueOf. You should be able to turn user input into your enum and also randomly pick an enum. Now, armed with two enums, you can compare apples to apples. You should also be able to print a list of choices using that enum, so none of this int Rock =0, duplication in the code.
Hope this helps.
This post has been edited by baavgai: 14 April 2015 - 04:47 AM
#18
Re: Help with Rock, Paper, Scissors, Lizard, Sock project
Posted 14 April 2015 - 12:52 PM
we did go over methods just what specifically are you going towards with the methods?? like an example you will
#19
Re: Help with Rock, Paper, Scissors, Lizard, Sock project
Posted 15 April 2015 - 04:05 AM
Pretty much like what I offered before, only now with an enum:
This looked like fun, so I made a test program:
Results:
Note the point of methods it to isolate bits of reusable code. Also, if you have some code done, but not all, your job essentially becomes fill in the blanks rather than write the giant main.
Hope this helps.
class RPSLS {
enum GameChoice { Rock, Paper, Scissors, Lizard, Spock };
private final Scanner scan;
private final Random rnd;
private int userScore, computerScore;
public RPSLS() { /* your code here */ }
private GameChoice getUserChoice() { /* your code here */ }
private GameChoice getComputerChoice() { /* your code here */ }
private int choiceCompare(GameChoice choice1, GameChoice choice2) { /* your code here */ }
private boolean isDone() { /* your code here */ }
private void playRound() {
GameChoice user = getUserChoice();
GameChoice computer = getComputerChoice();
System.out.println("You choose: " + user);
System.out.println("The computer chose: " + computer);
int cmp = choiceCompare(user, computer);
if (cmp<0) {
System.out.println("The Computer wins! I'm sorry but you lost.");
computerScore++;
} else if (cmp>0) {
System.out.println("You Win!!!! Congrats!");
userScore++;
} else {
System.out.println("Tie!");
}
System.out.println("Here's your score: " + userScore + " Computers score: " + computerScore);
}
public void play() {
while(!isDone()) {
playRound();
}
}
public static void main(String[] args) {
new RPSLS().play();
}
}
This looked like fun, so I made a test program:
class Rpsls {
private enum GameChoice {
Rock, Paper, Scissors, Lizard, Spock;
private String getWinningVerb(GameChoice other) { /* your code here */ }
public String getWinningMessage(GameChoice other) {
String verb = getWinningVerb(other);
return (verb==null) ? null : "" + this + " " + verb + " " + other + ".";
}
public boolean beats(GameChoice other) { return getWinningVerb(other)!=null; }
}
private final Random rnd;
public Rpsls() { this.rnd = new Random(); }
private GameChoice getRandChoice() { /* your code here */ }
private int choiceCompare(GameChoice choice1, GameChoice choice2) { /* your code here */ }
public void test() {
GameChoice x = getRandChoice();
GameChoice y = getRandChoice();
System.out.println("Player 1: " + x);
System.out.println("Player 2: " + y);
int cmp = choiceCompare(x, y);
if (cmp==0) {
System.out.println("Tie.");
} else if (cmp>0) {
System.out.println("Player 1 wins: " + x.getWinningMessage(y));
} else {
System.out.println("Player 2 wins: " + y.getWinningMessage(x));
}
}
public void test(int testCount) {
for(int i=0; i<testCount; i++) {
System.out.println("Round: " + (i+1));
test();
System.out.println();
}
}
public static void main(String[] args) {
new Rpsls().test(4);
}
}
Results:
Round: 1 Player 1: Paper Player 2: Scissors Player 2 wins: Scissors cuts Paper. Round: 2 Player 1: Rock Player 2: Spock Player 2 wins: Spock vaporizes Rock. Round: 3 Player 1: Lizard Player 2: Spock Player 1 wins: Lizard poisons Spock. Round: 4 Player 1: Lizard Player 2: Rock Player 2 wins: Rock crushes Lizard.
Note the point of methods it to isolate bits of reusable code. Also, if you have some code done, but not all, your job essentially becomes fill in the blanks rather than write the giant main.
Hope this helps.
#20
Re: Help with Rock, Paper, Scissors, Lizard, Sock project
Posted 15 April 2015 - 01:05 PM
Okay thats awesome your code makes perfect sense! i'm not going to copy it but i will use it as a reference the only thing wrong is that my professor is going to take off point for not including a switch statement in it
thats my only problem and displaying the score of the game after the user quits or loses or wins

New Topic/Question
Reply






MultiQuote


|