![]() |
|
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) |
rock scissors paper - blackeagle - 11-03-2013 i know it's not hard to do and i could have done it in better ways !! but as you all know i'm still learning java i made this game and decided to share it with you just to see your thoughts and opinions and pls tell me if i made anything wrong or if i have some errors!! Code: package rockpaperscissors;
import java.util.Scanner;
public class RockPaperScissors {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// player choice
System.out.println("Player choice : ");
String choicePlayer;
choicePlayer = in.nextLine();
while ((!choicePlayer.equalsIgnoreCase("rock")) && (!choicePlayer.equalsIgnoreCase("paper")) && (!choicePlayer.equalsIgnoreCase("scissors"))) {
System.out.println("choose rock paper or scissors");
choicePlayer = in.nextLine();
}
System.out.println("you're choice is " + choicePlayer);
//pc choice
double random = Math.random();
String choicePc = null;
if (random <= 0.4) {
choicePc = "rock";
}
if ((random > 0.4) && (random <= 0.7)) {
choicePc = "paper";
}
if (random > 0.7) {
choicePc = "scissors";
}
System.out.println("pc choosed " + choicePc);
//choosing the winner
if (choicePlayer.equals(choicePc)) {
System.out.println("its a tie !");
}
//if player chose rock
if (choicePlayer.equals("rock") && (choicePc.equals("scissors"))) {
System.out.println("player won");
}
if (choicePlayer.equals("rock") && (choicePc.equals("paper"))) {
System.out.println("pc won");
}
//if player chose scissors
if (choicePlayer.equals("scissors") && (choicePc.equals("paper"))) {
System.out.println("player win");
}
if (choicePlayer.equals("scissors") && (choicePc.equals("rock"))) {
System.out.println("pc won");
}
//if player chose paper
if (choicePlayer.equals("paper") && (choicePc.equals("rock"))) {
System.out.println("player win");
}
if (choicePlayer.equals("paper") && (choicePc.equals("scissors"))) {
System.out.println("pc won");
}
}
}RE: rock scissors paper - Deque - 11-03-2013 Doesn't look like you have errors. Your next step would be to move the code pieces that belong together into methods instead of writing everything in the main method. RE: rock scissors paper - Coder-san - 11-03-2013 I don't know about Java, but do you really need a double random when all you need is 3 choices for the PC? There would've been an equal chance if you had given random just 3 options. Currently rock has the most chance of appearing. Also the game just ends on the first match. Should make it prompt for "Play again? Y/N". RE: rock scissors paper - blackeagle - 11-03-2013 (11-03-2013, 07:56 PM)Deque Wrote: Doesn't look like you have errors. ok thank you i'll try and work on this !! (11-03-2013, 08:46 PM)Coder-san Wrote: I don't know about Java, but do you really need a double random when all you need is 3 choices for the PC? well thx for your opinion !! about the random i don't know how to make the chances equals or how to make 1 option get more ... i wish if you could help since you don't know java @Deque might help !! about the play aain i'll work on it
RE: rock scissors paper - blackeagle - 11-04-2013 @Deque sry for bothering you again but i wanted to start working on making methods as u said but then i stuck !! methods for what should i do ? a method for player choice and another for pc choice ? or what can you pls explain !! and finally can you tell me why do i need to make methods if i'm using them only once !! RE: rock scissors paper - blackeagle - 11-04-2013 i did some work check it when you can and let me know if that's what you meant Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rockpaperscissors;
import java.util.Scanner;
public class RockPaperScissors {
public static String playerChoice() {
Scanner in = new Scanner(System.in);
System.out.println("Player choice : ");
String choicePlayer;
choicePlayer = in.nextLine();
return choicePlayer;
}
public static String pcChoice() {
double random = Math.random();
String choicePc = null;
if (random <= 0.4) {
choicePc = "rock";
}
if ((random > 0.4) && (random <= 0.7)) {
choicePc = "paper";
}
if (random > 0.7) {
choicePc = "scissors";
}
return choicePc;
}
public static void chooseWinner(String choice1, String choice2) {
if (choice1.equals(choice2)) {
System.out.println("its a tie !");
}
//if player chose rock
if (choice1.equals("rock") && (choice2.equals("scissors"))) {
System.out.println("player won");
}
if (choice1.equals("rock") && (choice2.equals("paper"))) {
System.out.println("pc won");
}
//if player chose scissors
if (choice1.equals("scissors") && (choice2.equals("paper"))) {
System.out.println("player win");
}
if (choice1.equals("scissors") && (choice2.equals("rock"))) {
System.out.println("pc won");
}
//if player chose paper
if (choice1.equals("paper") && (choice2.equals("rock"))) {
System.out.println("player win");
}
if (choice1.equals("paper") && (choice2.equals("scissors"))) {
System.out.println("pc won");
}
}
public static void main(String[] args) {
String playAgain;
Scanner in = new Scanner(System.in);
do {
// player choice
String choice = playerChoice();
while ((!choice.equalsIgnoreCase("rock")) && (!choice.equalsIgnoreCase("paper")) && (!choice.equalsIgnoreCase("scissors"))) {
System.out.println("choose rock paper or scissors");
choice = in.nextLine();
}
System.out.println("you're choice is " + choice);
//pc choice
String 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 - Frooxius - 11-04-2013 Just a quick suggestion: Try multiplying the random change by 3, so you don't have to compare it against fractional numbers. Like this: Code: public static String pcChoice() {
double random = Math.random()*3;
String choicePc = null;
if (random <= 1) {
choicePc = "rock";
}
if ((random > 1) && (random <= 2)) {
choicePc = "paper";
}
if (random > 3) {
choicePc = "scissors";
}
return choicePc;
}You can even make it simpler and use something called if-else-if ladder: Code: [code]public static String pcChoice() {
double random = Math.random()*3;
String choicePc = null;
if (random <= 1) {
choicePc = "rock";
}
else if (random <= 2) {
choicePc = "paper";
}
else {
choicePc = "scissors";
}
return choicePc;
}It means that the next condition is checked only if the first one is not true. That means it checks if it's smaller or equal to one and if it is, it sets result to "rock". Only if that's not true it goes to the next condition and checks if it's smaller than 2. If that's not true either, that automatically means that it's larger than 2 so it doesn't even need to check and just sets the result to "scissors". EDIT: Also, there are many other ways of doing this. What I would suggest looking into though are enumerations (think of them as "named integers" - at least in their usual form and use) for storing the choice, instead of a string - you could try the next version to be done with enums :3 Strings for this are usually quite messy, especially if you get many choices. They give you more control over the consistency of the choices - if you make a typo in some string or comparison, it might take a while to track down, but with enums the compiler will tell you right away (plus they're more efficient) RE: rock scissors paper - ArkPhaze - 11-04-2013 Maybe methods for the PC to choose their *weapon*? A method for validating the user input? A method for checking the results? (edit: Ah, a few members have already posted as I was writing out this reply.) There are better ways of doing this though: Code: if (choicePlayer.equals(choicePc)) {
System.out.println("its a tie !");
}
//if player chose rock
if (choicePlayer.equals("rock") && (choicePc.equals("scissors"))) {
System.out.println("player won");
}
if (choicePlayer.equals("rock") && (choicePc.equals("paper"))) {
System.out.println("pc won");
}
//if player chose scissors
if (choicePlayer.equals("scissors") && (choicePc.equals("paper"))) {
System.out.println("player win");
}
if (choicePlayer.equals("scissors") && (choicePc.equals("rock"))) {
System.out.println("pc won");
}
//if player chose paper
if (choicePlayer.equals("paper") && (choicePc.equals("rock"))) {
System.out.println("player win");
}
if (choicePlayer.equals("paper") && (choicePc.equals("scissors"))) {
System.out.println("pc won");
}I'm not a Java programmer, but this is all hardcoded. I know from experience that there are better ways. ![]() For example, here's some code I quickly came up with for how you could go about checking. Code: #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
// 0 - Rock
// 1 - Paper
// 2 - Scissors
int main()
{
printf("Choose:\n\t0 - Rock\n\t1 - Paper\n\t2 - Scissors\n");
srand(time(NULL));
char cpu = rand() % 3;
char user = 0;
scanf("%c", &user);
user -= '0';
printf("You chose: %d\n", user);
if (cpu)
{
bool b = cpu & 1;
printf("You %s\n", b ? "Win!" : "Lose...");
cpu = b ? (user == 0 ? 2 : user - 1) : (user +1) % 3;
}
else
{
printf("Draw.\n");
cpu = user;
}
printf("Computer chose: %d\n", cpu);
}There's no checking of the user input for a valid char: '0','1', or '2'. But this is intended to serve as a logic perspective. Code: Choose:
0 - Rock
1 - Paper
2 - Scissors
0
You chose: 0
You Win!
Computer chose: 2
Process returned 0 (0x0) execution time : 0.773 s
Press any key to continue.Also written in C, but the operations should be hardly different. RE: rock scissors paper - blackeagle - 11-04-2013 i fixed this thx for the suggestion appreciate it ![]() Spoiler:Code: /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rockpaperscissors;
import java.util.Scanner;
public class RockPaperScissors {
public static String playerChoice() {
Scanner in = new Scanner(System.in);
System.out.println("Player choice : ");
String choicePlayer;
choicePlayer = in.nextLine();
return choicePlayer;
}
public static String pcChoice() {
int random = 1 + (int) (Math.random() * 3);
String choicePc;
if (random == 1) {
choicePc = "rock";
} else if (random == 2) {
choicePc = "paper";
} else {
choicePc = "scissors";
}
return choicePc;
}
public static void chooseWinner(String choice1, String choice2) {
if (choice1.equals(choice2)) {
System.out.println("its a tie !");
}
//if player chose rock
if (choice1.equals("rock") && (choice2.equals("scissors"))) {
System.out.println("player won");
}
if (choice1.equals("rock") && (choice2.equals("paper"))) {
System.out.println("pc won");
}
//if player chose scissors
if (choice1.equals("scissors") && (choice2.equals("paper"))) {
System.out.println("player win");
}
if (choice1.equals("scissors") && (choice2.equals("rock"))) {
System.out.println("pc won");
}
//if player chose paper
if (choice1.equals("paper") && (choice2.equals("rock"))) {
System.out.println("player win");
}
if (choice1.equals("paper") && (choice2.equals("scissors"))) {
System.out.println("pc won");
}
}
public static void main(String[] args) {
String playAgain;
Scanner in = new Scanner(System.in);
do {
// player choice
String choice = playerChoice();
while ((!choice.equalsIgnoreCase("rock")) && (!choice.equalsIgnoreCase("paper")) && (!choice.equalsIgnoreCase("scissors"))) {
System.out.println("choose rock paper or scissors");
choice = in.nextLine();
}
System.out.println("you're choice is " + choice);
//pc choice
String 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");
}
}
}@ArkPhaze as i understood you want me to change scissors rock paper to 0,1,2 and use 0,1,2 while comparing ? RE: rock scissors paper - ArkPhaze - 11-04-2013 (11-04-2013, 01:18 AM)blackeagle Wrote: i fixed this thx for the suggestion appreciate it A lot more needs to happen for a comparison of strings, so using an integral type would be better. I just wanted to show you that you don't have to compare values individually like that for every outcome. You can use math to your advantage like I did. You are still comparing strings: Code: public static void chooseWinner(String choice1, String choice2) {
if (choice1.equals(choice2)) {
System.out.println("its a tie !");
}
//if player chose rock
if (choice1.equals("rock") && (choice2.equals("scissors"))) {
System.out.println("player won");
}
if (choice1.equals("rock") && (choice2.equals("paper"))) {
System.out.println("pc won");
}
//if player chose scissors
if (choice1.equals("scissors") && (choice2.equals("paper"))) {
System.out.println("player win");
}
if (choice1.equals("scissors") && (choice2.equals("rock"))) {
System.out.println("pc won");
}
//if player chose paper
if (choice1.equals("paper") && (choice2.equals("rock"))) {
System.out.println("player win");
}
if (choice1.equals("paper") && (choice2.equals("scissors"))) {
System.out.println("pc won");
}
}And these are all individual if statements, so many redundant checks happen too. The same lines of code for println are repeated where they don't have to be as well. |