Login Register






rock scissors paper filter_list
Author
Message
RE: rock scissors paper #49
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"); } }
(This post was last modified: 11-08-2013, 03:38 PM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply





Messages In This Thread
rock scissors paper - by blackeagle - 11-03-2013, 03:12 PM