Login Register






rock scissors paper filter_list
Author
Message
RE: rock scissors paper #51
@Deque
what should i do about the enum choice all i need i n my code is to print it !
what do you mean by this : "My suggestion was more for the situation where you pass it around as value."
i'll rename choice1 and choice2 to playerChoice nd pc Choice . i agree it's better!!
as for my choose winner method i prefer it as it is !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #52
(11-09-2013, 10:18 AM)blackeagle Wrote: @Deque
what should i do about the enum choice all i need i n my code is to print it !
what do you mean by this : "My suggestion was more for the situation where you pass it around as value."
i'll rename choice1 and choice2 to playerChoice nd pc Choice . i agree it's better!!
as for my choose winner method i prefer it as it is !!

I mean you don't need an enum for the game result if don't do anything with it.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: rock scissors paper #53
ok so this is the final code for this game
Code:
package rockpaperscissors; import java.util.Random; import java.util.Scanner; public enum Choice { ROCK, PAPER, SCISSORS; } public class RockPaperScissors { 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 playerChoice, Choice pcChoice) { if (playerChoice.equals(pcChoice)) { System.out.println("it's a tie"); } //if player chose rock if ((playerChoice.equals(Choice.ROCK)) && (pcChoice.equals(Choice.SCISSORS))) { System.out.println("player win"); } if ((playerChoice.equals(Choice.ROCK)) && (pcChoice.equals(Choice.PAPER))) { System.out.println("pc win"); } //if player chose scissors if ((playerChoice.equals(Choice.SCISSORS)) && (pcChoice.equals(Choice.PAPER))) { System.out.println("player win"); } if ((playerChoice.equals(Choice.SCISSORS)) && (pcChoice.equals(Choice.ROCK))) { System.out.println("pc win"); } //if player chose paper if ((playerChoice.equals(Choice.PAPER)) && (pcChoice.equals(Choice.ROCK))) { System.out.println("player win"); } if ((playerChoice.equals(Choice.PAPER)) && (pcChoice.equals(Choice.SCISSORS))) { System.out.println("pc win"); } } 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)); System.out.println("thx for playing"); } }

i just wanna really thank every1 who helped me on this thread so thank you @Deque @ArkPhaze @Frooxius @Coder-san
this thread can now be closed or it might stay open if some1 need to discuss something about it !!
(This post was last modified: 11-09-2013, 02:53 PM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply