What's new

Closed Java Programming Expert I need You

Status
Not open for further replies.

M H I N Y E

Forum Guru
Elite
Joined
May 11, 2018
Posts
3,631
Solutions
5
Reaction
5,768
Points
1,610
1. Write a program that simulates a paper-rock-scissor game. Each users will type in either P,R, or S if the user inputs a wrong letter it must in form the user to input the correct one. the program announce the winner a winner is the player who first obtain the score of 5.

Na stock Po ako kng paano ko ipalabas Yung winner kapag naka 5correct answer na Yung player




import java.util.Scanner; import java.io.*;

public class Part2 {

public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);

String userInput1 = " ";
char player1Input;

String userInput2 = " ";
char player2Input;

for(int i = 0; i < 5; i++) //Loop for game
{

System.out.println("Player 1: Please Enter e.g R for Rock:");
System.out.println("R.Rock");
System.out.println("P.Paper");
System.out.println("S.Scissors");

userInput1 = scan.next();
player1Input = userInput1.charAt(0);


System.out.println("Player 2: Please Enter e.g S for Scissors");
System.out.println("R.Rock");
System.out.println("P.Paper");
System.out.println("S.Scissors");



userInput2 = scan.next();
player2Input = userInput2.charAt(0);

}





switch(player1Input)
{
case 'R': System.out.println ("Player 1:Rock");

switch(player2Input)
{
case'R' : System.out.println("Player 2:Rock");
System.out.println("The Game Result is Draw");
break;
case'P' :System.out.println("Player 2:paper");
System.out.println("The Game Result is: Player 2 Wins");
break;
case'S' :System.out.println("Player 2:Scissors");
System.out.println("The Game Result is: Player 1 Wins");
break;
default: System.out.println("Invalid Option");
break;
}
break;

case 'P' : System.out.println ("Player 1:paper");
switch(player2Input)
{
case'R' : System.out.println("Player 2:Rock");
System.out.println("The game result is: Player 1 Wins");
break;
case'P' :System.out.println("Player 2:paper");
System.out.println("The game result is: Draw");
break;
case'S' :System.out.println("Player 2:Scissors");
System.out.println("The Game Result is: Player 2 Wins2");
break;
default: System.out.println("Invalid Option");
break;
}
break;

case 'S' : System.out.println ("Player 1:Scissors");
switch(player2Input)
{
case'R' : System.out.println("Player 2:Rock");
System.out.println("The Game Result is: Player 2 Wins");
break;
case'P' :System.out.println("Player 2:paper");
System.out.println("The game result is: Player 1 Wins");
break;
case'S' :System.out.println("Player 2:Scissors");
System.out.println ("The Game Result is: Draw");
break;
default: System.out.println("Invalid Option");
break;
}
break;

}
}
}
 
Code:
public void main()
{
    1. Get input of player 1 - R, P, S
    2. Get input of player 2 or random - R, P, S

    if(input1 == input2)
    {
        //Draw
    }
    else
    {
        if(input1 is S and input2 is R
        or input1 is R and input2 is P
        or input1 is P and input2 is S)
        {
            //Lose
            //Add point to player 2
        }
        else
        {
            //win
            //Add point to player 1
        }
    }
}
If-else lang naman yan ah.
 
Code:
import java.util.Scanner; import java.io.*;



public class Part2 {



public static void main (String[]args)

{



Scanner scan = new Scanner(System.in);



String userInput1 = " ";

char player1Input = 0;



String userInput2 = " ";

char player2Input = 0;



int p1s=0;

int p2s=0;

for(int i = 1; i <= 5; i++) //Loop for game

{



System.out.println("Player 1: Please Enter e.g R for Rock:");

System.out.println("R.Rock");

System.out.println("P.Paper");

System.out.println("S.Scissors");



userInput1 = scan.next();

player1Input = userInput1.charAt(0);





System.out.println("Player 2: Please Enter e.g S for Scissors");

System.out.println("R.Rock");

System.out.println("P.Paper");

System.out.println("S.Scissors");







userInput2 = scan.next();

player2Input = userInput2.charAt(0);















switch(player1Input)

{

case 'R': System.out.println ("Player 1:Rock");



switch(player2Input)

{

case'R' : System.out.println("Player 2:Rock");

System.out.println("Round "+i+" Draw  Player 1:"+p1s+" Player 2:"+p2s);

break;

case'P' :System.out.println("Player 2:paper");

p2s=p2s+1;

System.out.println("Round "+i+" Player 2 wins  Player 1:"+p1s+" Player 2:"+p2s);

break;

case'S' :System.out.println("Player 2:Scissors");

p1s=p1s+1;

System.out.println("Round "+i+" Player 1 wins  Player 1:"+p1s+" Player 2:"+p2s);

break;

default: System.out.println("Invalid Option");

break;

}

break;



case 'P' : System.out.println ("Player 1:paper");

switch(player2Input)

{

case'R' : System.out.println("Player 2:Rock");

p1s=p1s+1;

System.out.println("Round "+i+" Player 1 wins  Player 1:"+p1s+" Player 2:"+p2s);

break;

case'P' :System.out.println("Player 2:paper");

System.out.println("Round "+i+" Draw  Player 1:"+p1s+" Player 2:"+p2s);

break;

case'S' :System.out.println("Player 2:Scissors");

p2s=p2s+1;

System.out.println("Round "+i+" Player 2 wins  Player 1:"+p1s+" Player 2:"+p2s);

break;

default: System.out.println("Invalid Option");

break;

}

break;



case 'S' : System.out.println ("Player 1:Scissors");

switch(player2Input)

{

case'R' : System.out.println("Player 2:Rock");

p2s=p2s+1;

System.out.println("Round "+i+" Player 2 wins  Player 1:"+p1s+" Player 2:"+p2s);

break;

case'P' :System.out.println("Player 2:paper");

p1s=p1s+1;

System.out.println("Round "+i+" Player 1 wins  Player 1:"+p1s+" Player 2:"+p2s);

break;

case'S' :System.out.println("Player 2:Scissors");

System.out.println("Round "+i+" Draw  Player 1:"+p1s+" Player 2:"+p2s);

break;

default: System.out.println("Invalid Option");

break;

}

break;



}

}

    if (p2s>p1s){

        System.out.print("Player 2 wins: Score "+ p2s);

    }

    else if (p1s>p2s){

        System.out.print("Player 1 wins: Score "+ p1s);     

    }

    else{

            System.out.print("DRAW");

    }

}

}
 
There are two main problems in your code:

1. It's hard to read them because you just copy-pasted them without using proper code tags. There's a reason why they exist in this forum (You don't need programming skills with this one)

2. Your implementation is complex compare to what the problem is asking. It's a simple problem. Complex solutions can make things difficult to understand and buggy.

Simplify the solution first so you can troubleshoot the code easier, and use code tags so we can read it better.
 
Here's a Java REPL for your reference: You do not have permission to view the full content of this post. Log in or register now.

So the idea is to split the problem into smaller tasks. You split it to make the code readable, easier to debug, and avoid unnecessary duplication. Think OOP (it's Java).

I created the following class/enums

- Gesture (an enumeration of all valid inputs)
- Player (a class that represents a player)
- A GameEngine (a class that represents the rules of a Rock Paper Scissors game)
- A GameManager (a class that puts everything together)

The goal is to put behaviors that are only relevant to the class.

REPL Notes:
Ideally those classes are on separate files. But for the purpose of demonstration, I placed them on the same file.

Sample output

OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)  javac -classpath .:/run_dir/junit-4.12.jar -d . Main.java  java -classpath .:/run_dir/junit-4.12.jar Main Round 1 fight! John, enter your move: P James, enter your move: R John's Paper vs James's Rock John has won this round! Round 2 fight! John, enter your move: S James, enter your move: R John's Scissors vs James's Rock James has won this round! Round 3 fight! John, enter your move: KP Invalid input! Please re-enter move: LO Invalid input! Please re-enter move: R James, enter your move: P John's Rock vs James's Paper James has won this round! Round 4 fight! John, enter your move: S James, enter your move: S John's Scissors vs James's Scissors No one won! Round 5 fight! John, enter your move: R James, enter your move: R John's Rock vs James's Rock No one won! Round 6 fight! John, enter your move: P James, enter your move: R John's Paper vs James's Rock John has won this round! Round 7 fight! John, enter your move: P James, enter your move: R John's Paper vs James's Rock John has won this round! Round 8 fight! John, enter your move: P James, enter your move: R John's Paper vs James's Rock John has won this round! Round 9 fight! John, enter your move: P James, enter your move: R John's Paper vs James's Rock John has won this round! John has won the game!
 
Java:
/**
 * Author: TwoJuanTwo
 *
 */

import java.util.Random;
import java.util.Scanner;

public class App {
    public static int MAX_PLAYERS = 2;
    private int[] playerHands;
    public static int MAX_CHOICES = 3;
    private int[] scores;
    static Scanner input;
    static String[] result;
    static Boolean playing = false;

    public static void main(String[] args) {
        
        App game = new App();
        game.displayWelcome();
        while(playing) {
            game.getHands();
            game.getWinner();
            game.displayResult();
            
            System.out.println("Play again? Y or N");
                String yOrN = (input.nextLine()).trim().toLowerCase();
                if(yOrN.equals("n")) {
                    playing = false;
                }
        }

    }
    
    //Constructor
    public App() {
        input = new Scanner(System.in);
        scores = new int[2];
        playerHands = new int[MAX_PLAYERS];
        playing = true;   
    }

    public void getHands() {
        for(int i=0; i<MAX_PLAYERS; i++) {
            playerHands[i] = randomHand(i);
        }
        System.out.println();
    }
    
    private int randomHand(int playerNumber) {
        int randomNum = 0;
        System.out.print("PLAYER #" + (playerNumber+1) + ", Hit enter key.");
        input.nextLine();
        Random random = new Random();
        randomNum = random.nextInt(MAX_CHOICES)  + 1;
        return randomNum;
    }
    
    
    public void displayResult() {
        for(int i = 0; i<playerHands.length; i++) {
            System.out.print("PLAYER " + (i+1) + ": " + getEquivalent(playerHands[i]) +  result[i] + "\tSCORE: "+ scores[i] + "\n");
        }
        System.out.println();
    }
    
    private String getEquivalent(int i) {
        String equivalent = "";
        switch(i) {
            case 1: equivalent = "Rock"; break;
            case 2: equivalent = "Paper"; break;
            case 3: equivalent = "Scissor"; break;
        }
        return equivalent;
    }
    
    private void getWinner() {
        String choices = "" + getEquivalent(playerHands[0]) + getEquivalent(playerHands[1]);
        choices = choices.toLowerCase();
        result = determineWinner(choices);
    }
    
    private String[] determineWinner(String hands) {
        String[] win = new String[2];
        switch(hands) {
        case "rockrock":
            win[0] = "";
            win[1] = ""; break;
        case "paperpaper":
            win[0] = "";
            win[1] = ""; break;
        case "scissorscissor":
            win[0] = "";
            win[1] = ""; break;
        
        case "rockpaper":
            win[0] = " ";
            win[1] = " *win";
            scores[1] += scores[1]+1;
            break;
        case "rockscissor":
            win[0] = " *win";
            win[1] = " ";
            scores[0] = scores[0] + 1;
            break;
        
        case "paperrock":
            win[0] = " *win";
            win[1] = " ";
            scores[0] = scores[0] + 1;
            break;
        case "paperscissor":
            win[0] = " ";
            win[1] = " *win";
            scores[1] = scores[1]+1;
            break;
        
        case "scissorrock":
            win[0] = "";
            win[1] = " *win";
            scores[1] = scores[1]+1;
            break;
        case "scissorpaper":
            win[0] = " *win";
            win[1] = " ";
            scores[0] = scores[0] + 1;
            break;
        }
        return win;
    }
    
    public void displayWelcome() {
        System.out.println("**********************");
        System.out.println("Rock Paper Scissor 1.0");
        System.out.println("**********************");
        System.out.println();
    }
}
 
Status
Not open for further replies.

Similar threads

Back
Top