![]() |
|
rock scissors paper - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: rock scissors paper (/Thread-rock-scissors-paper) |
RE: rock scissors paper - Deque - 11-07-2013 You can use equals to compare them. Example: Code: if(choice.equals(Choice.ROCK)) // true iff choice equals ROCKRE: rock scissors paper - blackeagle - 11-07-2013 i know i could do it like this but i found the ordinal method easier !! am i wrong ? RE: rock scissors paper - Deque - 11-07-2013 (11-07-2013, 11:01 AM)blackeagle Wrote: i know i could do it like this but i found the ordinal method easier !! am i wrong ? It's harder to read, what you are comparing against if you compare numbers. RE: rock scissors paper - blackeagle - 11-07-2013 ok then i'll change it back to .equals !! @Deque i made it .equals again and made a isValidChoice() method for the while loop !! but it doent work well !! Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rockpaperscissors;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public enum Choice {
ROCK,
PAPER,
SCISSORS;
}
public static Choice playerChoice() {
System.out.println("choose 0 for rock");
System.out.println("choose 1 for paper");
System.out.println("choose 2 for scissors");
Scanner in = new Scanner(System.in);
int numberChosen;
Choice choicePlayer[];
numberChosen = in.nextInt();
choicePlayer = Choice.values();
return choicePlayer[numberChosen];
}
public static Choice pcChoice() {
Choice[] choicePc = Choice.values();
int rand = (new Random()).nextInt((choicePc.length));
return choicePc[rand];
}
public static void chooseWinner(Choice choice1, Choice choice2) {
if (choice1.equals(choice2)) {
System.out.println("its a tie !");
}
//if player chose rock
if ((choice1.equals(Choice.ROCK)) && (choice2.equals(Choice.SCISSORS))) {
System.out.println("player win");
}
if ((choice1.equals(Choice.ROCK)) && (choice2.equals(Choice.PAPER))) {
System.out.println("pc win");
}
//if player chose scissors
if ((choice1.equals(Choice.SCISSORS)) && (choice2.equals(Choice.PAPER))) {
System.out.println("player win");
}
if ((choice1.equals(Choice.SCISSORS)) && (choice2.equals(Choice.ROCK))) {
System.out.println("pc won");
}
//if player chose paper
if ((choice1.equals(Choice.PAPER)) && (choice2.equals(Choice.ROCK))) {
System.out.println("player win");
}
if ((choice1.equals(Choice.PAPER)) && (choice2.equals(Choice.SCISSORS))) {
System.out.println("pc won");
}
}
public static boolean isValidChoice(Choice choice) {
boolean b1;
if ((choice == Choice.ROCK) || (choice == Choice.PAPER) || (choice == Choice.SCISSORS)) {
b1 = true;
} else {
b1 = false;
}
return b1;
}
public static void main(String[] args) {
String playAgain;
String choose;
Scanner in = new Scanner(System.in);
do {
// player choice
Choice choice = playerChoice();
while ((isValidChoice(choice)) == false) {
System.out.println("choose rock paper or scissors");
choose = in.next();
}
System.out.println("you're choice is " + choice);
//pc choice
Choice pcChoice = pcChoice();
System.out.println("pc choosed " + pcChoice);
//choosing the winner
chooseWinner(choice, pcChoice);
do {
System.out.println("Do you wanna play again y/n ?");
playAgain = in.nextLine();
} while (!playAgain.equals("y") && (!playAgain.equals("n")));
} while (playAgain.equalsIgnoreCase("y"));
if (playAgain.equals("n")) {
System.out.println("thx for playing");
}
}
}RE: rock scissors paper - Deque - 11-07-2013 Code: Choice choice = playerChoice();
while ((isValidChoice(choice)) == false) {
System.out.println("choose rock paper or scissors");
choose = in.next();
}Look at this. You save a value in choose, but never do anything with it. Also the check for valid choice is not neccessary at this point, because the playerChoice() method will already fail if the input is wrong. So you need to make a check for valid input there (test if it is a number between 0 and Choice.values().length()) RE: rock scissors paper - blackeagle - 11-07-2013 time for me to go to uni i'll see what i can do about it tonight !! RE: rock scissors paper - blackeagle - 11-07-2013 @Deque so i don't need this anymore Code: while ((isValidChoice(choice)) == false) {
System.out.println("choose rock paper or scissors");
choose = in.next();
}if i understood right my method playerChoice(); put the choices 0 1 2 in the array so if i use anything else the program will terminate so the while(isValidChoice())is useless here RE: rock scissors paper - Deque - 11-08-2013 (11-07-2013, 09:19 PM)blackeagle Wrote: @Deque so i don't need this anymore Correct. RE: rock scissors paper - blackeagle - 11-08-2013 i guess i made most of the suggestions u made i still have to make an enum for win loose tie but i waned to see first if everything else is perfect or still i can change something Code: package rockpaperscissors;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public enum Choice {
ROCK,
PAPER,
SCISSORS;
}
public static Choice playerChoice() {
System.out.println("choose 0 for rock");
System.out.println("choose 1 for paper");
System.out.println("choose 2 for scissors");
Scanner in = new Scanner(System.in);
int numberChosen;
Choice choicePlayer[];
numberChosen = in.nextInt();
choicePlayer = Choice.values();
return choicePlayer[numberChosen];
}
public static Choice pcChoice() {
Choice[] choicePc = Choice.values();
int rand = (new Random()).nextInt((choicePc.length));
return choicePc[rand];
}
public static void chooseWinner(Choice choice1, Choice choice2) {
if (choice1.equals(choice2)) {
System.out.println("its a tie !");
}
//if player chose rock
if ((choice1.equals(Choice.ROCK)) && (choice2.equals(Choice.SCISSORS))) {
System.out.println("player win");
}
if ((choice1.equals(Choice.ROCK)) && (choice2.equals(Choice.PAPER))) {
System.out.println("pc win");
}
//if player chose scissors
if ((choice1.equals(Choice.SCISSORS)) && (choice2.equals(Choice.PAPER))) {
System.out.println("player win");
}
if ((choice1.equals(Choice.SCISSORS)) && (choice2.equals(Choice.ROCK))) {
System.out.println("pc won");
}
//if player chose paper
if ((choice1.equals(Choice.PAPER)) && (choice2.equals(Choice.ROCK))) {
System.out.println("player win");
}
if ((choice1.equals(Choice.PAPER)) && (choice2.equals(Choice.SCISSORS))) {
System.out.println("pc won");
}
}
public static boolean wantToPlayAgain(String playAgain) {
boolean b1 = false;
if (playAgain.equalsIgnoreCase("y")) {
b1 = true;
}
return b1;
}
public static void main(String[] args) {
String playAgain;
String choose;
Scanner in = new Scanner(System.in);
do {
// player choice
Choice choice = playerChoice();
System.out.println("you're choice is " + choice);
//pc choice
Choice pcChoice = pcChoice();
System.out.println("pc choosed " + pcChoice);
//choosing the winner
chooseWinner(choice, pcChoice);
do {
System.out.println("Do you wanna play again y/n ?");
playAgain = in.nextLine();
} while (!playAgain.equalsIgnoreCase("y") && (!playAgain.equalsIgnoreCase("n")));
} while (wantToPlayAgain(playAgain) == true);
System.out.println("thx for playing");
}
}there's the final code with the win loose or tie enum Code: package rockpaperscissors;
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public enum Choice {
ROCK,
PAPER,
SCISSORS;
}
public enum Status {
WIN,
LOOSE,
TIE;
}
public static Choice playerChoice() {
System.out.println("choose 0 for rock");
System.out.println("choose 1 for paper");
System.out.println("choose 2 for scissors");
Scanner in = new Scanner(System.in);
int numberChosen;
Choice choicePlayer[];
numberChosen = in.nextInt();
choicePlayer = Choice.values();
return choicePlayer[numberChosen];
}
public static Choice pcChoice() {
Choice[] choicePc = Choice.values();
int rand = (new Random()).nextInt((choicePc.length));
return choicePc[rand];
}
public static void chooseWinner(Choice choice1, Choice choice2) {
if (choice1.equals(choice2)) {
System.out.println(Status.TIE);
}
//if player chose rock
if ((choice1.equals(Choice.ROCK)) && (choice2.equals(Choice.SCISSORS))) {
System.out.println(Status.WIN);
}
if ((choice1.equals(Choice.ROCK)) && (choice2.equals(Choice.PAPER))) {
System.out.println(Status.LOOSE);
}
//if player chose scissors
if ((choice1.equals(Choice.SCISSORS)) && (choice2.equals(Choice.PAPER))) {
System.out.println(Status.WIN);
}
if ((choice1.equals(Choice.SCISSORS)) && (choice2.equals(Choice.ROCK))) {
System.out.println(Status.LOOSE);
}
//if player chose paper
if ((choice1.equals(Choice.PAPER)) && (choice2.equals(Choice.ROCK))) {
System.out.println(Status.WIN);
}
if ((choice1.equals(Choice.PAPER)) && (choice2.equals(Choice.SCISSORS))) {
System.out.println(Status.LOOSE);
}
}
public static boolean wantToPlayAgain(String playAgain) {
boolean b1 = false;
if (playAgain.equalsIgnoreCase("y")) {
b1 = true;
}
return b1;
}
public static void main(String[] args) {
String playAgain;
String choose;
Scanner in = new Scanner(System.in);
do {
// player choice
Choice choice = playerChoice();
System.out.println("you're choice is " + choice);
//pc choice
Choice pcChoice = pcChoice();
System.out.println("pc choosed " + pcChoice);
//choosing the winner
chooseWinner(choice, pcChoice);
do {
System.out.println("Do you wanna play again y/n ?");
playAgain = in.nextLine();
} while (!playAgain.equalsIgnoreCase("y") && (!playAgain.equalsIgnoreCase("n")));
} while (wantToPlayAgain(playAgain) == true);
System.out.println("thx for playing");
}
}RE: rock scissors paper - Deque - 11-09-2013 If all you do is printing the Enum, you don't need it. My suggestion was more for the situation where you pass it around as value. Code: while (wantToPlayAgain(playAgain) == true);Just write Code: while (wantToPlayAgain(playAgain))Also: I see comments like //if player chose scissors These comments are a hint that there is something to make more clear and that is the role of choice1 and choice2. Rename them to i.e. playerChoice, pcChoice instead. Your code is alright as far as I can see. The chooseWinner can be shortened, but that doesn't mean your method of choosing the winner isn't alright as well. So feel free to leave it as is. I just want to show some possibilities. If you see the ordinal of a Choice as who beats whom you can make use of that and say: ordwinner = (ordlooser + 1) % 3 Or in code: Code: if(ordpc == (ordplayer + 1) % 3) //pc wins
else if(ordplayer == (ordpc + 1) % 3) //player wins
else //tieBut while this is much shorter, it is also much more obscure for a reader what happens there and is vulnerable to changes of the order in the enum Choice, which is why I don't see it as better per se. Prefer to write your code in a way that you understand what is going on. |