Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


rock scissors paper filter_list
Author
Message
rock scissors paper #1
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");
        }
    }
}
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #2
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.
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 #3
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".
[Image: rytwG00.png]
Redcat Revolution!

Reply

RE: rock scissors paper #4
(11-03-2013, 07:56 PM)Deque Wrote: 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.

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?
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".

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 Smile
(This post was last modified: 11-03-2013, 11:15 PM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #5
@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 !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #6
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");
        }

    }
}
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #7
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)
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: rock scissors paper #8
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. Smile

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.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: rock scissors paper #9
i fixed this thx for the suggestion appreciate it Smile

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 ?
(This post was last modified: 11-04-2013, 01:21 AM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #10
(11-04-2013, 01:18 AM)blackeagle Wrote: i fixed this thx for the suggestion appreciate it Smile

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 ?

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.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply







Users browsing this thread: 2 Guest(s)