What's new
  • Welcome to PHCorner Forums. Take a moment to Sign up and gain unlimited access and extra privileges that guests are not entitled to, such as: All that and more! Registration is quick, simple and absolutely free. Join our community today!

Help Request IT EXPERT I HAVE A CHALLENGE FOR YOU


twojuantwo

Leecher
heto, Rock Paper Scissor... kahawig ng gusto mo.
Assignment ko noong last year. Baguhin mo na lang para sa requirements mo.

Code:
/**
 * 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();
    }

    
}


[/code.
 
Comment

fireclouu

Addict
, Last edited:
My iteration:
Java:
static Scanner input = new Scanner(System.in);
    private static void TEST_JAKENPON() {
        // init
        final int PLAYERS = 0xff + 1; // consider as thrown point ( for nonexistent player 0xff)
        String[] player_move = new String[PLAYERS];
        int[] playerScore = new int[PLAYERS];
    
        for (int i = 0; i < PLAYERS; i++) {
            playerScore[i] = 0;
        }
    
    
        final char[] MOVESET = {'P', 'R', 'S'};
        System.out.println("Rock, Paper, Scissor ");
        System.out.println("P - paper\nR - rock\nS - scissor");
        boolean isActive = true;
        do {
            System.out.println();
            System.out.print("Your turn: ");
            player_move[0] = input.next();
        
            System.out.print("Comp. turn: ");
            player_move[1] = "" + MOVESET[new Random().nextInt(MOVESET.length - 1)];
            System.out.println(player_move[1]);
        
            int player = DEPENDENT_JAKENPON_RULES(player_move[0], player_move[1]);
            playerScore[player]++;
        
            System.out.println("\n You: " + playerScore[0] + "\nComp: " + playerScore[1]);
        
        
        } while (isActive);
    
    }

    private static int DEPENDENT_JAKENPON_RULES(String... move) {
        if (move[0].equalsIgnoreCase(move[1])) {
            System.out.println("DRAW");
            return 0xff;
        }
    
        switch(move[0].toUpperCase()) {
            case "P":
                if (move[1].equals("S")) {
                    System.out.println("Comp WIN");
                    return 1;
                } else {
                    System.out.println("You WIN");
                    return 0;
                }
            case "R":
                if (move[1].equals("P")) {
                    System.out.println("Comp WIN");
                    return 1;
                } else {
                    System.out.println("You WIN");
                    return 0;
                }
            case "S":
                if (move[1].equals("R")) {
                    System.out.println("Comp WIN");
                    return 1;
                } else {
                    System.out.println("You WIN");
                    return 0;
                }
        }
    
        return 0xff; // my throw point
    }
'di ko na ininclude break kung may naka-5point na, ayos lang sakin 65% hehe :) use it as your reference
 
Comment

aa1012

Addict
kalokohan. Ang gagawa nyan uto-uto. :ROFLMAO::ROFLMAO::ROFLMAO: pa spooon feed masyado mga studyante dito. wala bang magtatanong ng totoong problem yung totoong project man lang sana. ang boring ng mga ganitong tanong.
 
Comment

Like us on facebook

Online statistics

Members online
1,453
Guests online
1,357
Total visitors
2,810

Forum statistics

Threads
883,740
Messages
15,627,766
Members
1,410,714
Top