twojuantwo
Enthusiast
heto, Rock Paper Scissor... kahawig ng gusto mo.
Assignment ko noong last year. Baguhin mo na lang para sa requirements 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.