inkedmn
09-20-2002, 08:10 PM
decided to hack out some java (since i haven't been using it nearly as much as i should).
this is (loose) translation of Vince's python game on ccae...
import java.util.HashMap;
public class RockPaperScissors {
String[] weapons = {"Rock", "Paper", "Scissors"};
EasyIn easy = new EasyIn();
HashMap winners = new HashMap();
String playerWeapon;
String cpuWeapon;
public static void main(String[] args) {
RockPaperScissors rps = new RockPaperScissors();
rps.playerWeapon = rps.getPlayerWeapon();
rps.cpuWeapon = rps.getCpuWeapon();
System.out.println("\n\nYou chose: " + rps.playerWeapon);
System.out.println("The computer chose: " + rps.cpuWeapon + "\n\n");
rps.getWinner(rps.cpuWeapon, rps.playerWeapon);
}
public RockPaperScissors() {
String rock = "Rock";
String paper = "Paper";
String scissors = "Scissors";
winners.put(rock, scissors);
winners.put(scissors, paper);
winners.put(paper, rock);
System.out.println("Welcome to Rock-Paper-Scissors!");
}
public String getPlayerWeapon() {
int choice = 6;
String weapon = null;
System.out.println("Please Choose your Weapon: ");
System.out.println("1. Rock \n2. Paper \n3. Scissors \n\n0. Quit");
choice = easy.readInt();
if (choice == 0) {
System.out.println("Quitting...");
System.exit(0);
} else {
try {
weapon = weapons[(choice - 1)];
} catch (IndexOutOfBoundsException e) {
errorMsg();
getPlayerWeapon();
}
}
return weapon;
}
public void errorMsg() {
System.out.println("\nInvalid Entry.");
System.out.println("Please Select Again...\n\n");
}
public String getCpuWeapon() {
int rounded = -1;
while (rounded < 0 || rounded > 2) {
double randomNum = Math.random();
rounded = Math.round(3 * (float)(randomNum));
}
return weapons[rounded];
}
public void getWinner(String cpuWeapon, String playerWeapon) {
if (cpuWeapon == playerWeapon) {
System.out.println("Tie!");
} else if (winners.get(cpuWeapon) == playerWeapon) {
System.out.println("Computer Wins!");
} else if (winners.get(playerWeapon) == cpuWeapon) {
System.out.println("You Win!");
}
}
}
any feedback would be most welcome :)
this is (loose) translation of Vince's python game on ccae...
import java.util.HashMap;
public class RockPaperScissors {
String[] weapons = {"Rock", "Paper", "Scissors"};
EasyIn easy = new EasyIn();
HashMap winners = new HashMap();
String playerWeapon;
String cpuWeapon;
public static void main(String[] args) {
RockPaperScissors rps = new RockPaperScissors();
rps.playerWeapon = rps.getPlayerWeapon();
rps.cpuWeapon = rps.getCpuWeapon();
System.out.println("\n\nYou chose: " + rps.playerWeapon);
System.out.println("The computer chose: " + rps.cpuWeapon + "\n\n");
rps.getWinner(rps.cpuWeapon, rps.playerWeapon);
}
public RockPaperScissors() {
String rock = "Rock";
String paper = "Paper";
String scissors = "Scissors";
winners.put(rock, scissors);
winners.put(scissors, paper);
winners.put(paper, rock);
System.out.println("Welcome to Rock-Paper-Scissors!");
}
public String getPlayerWeapon() {
int choice = 6;
String weapon = null;
System.out.println("Please Choose your Weapon: ");
System.out.println("1. Rock \n2. Paper \n3. Scissors \n\n0. Quit");
choice = easy.readInt();
if (choice == 0) {
System.out.println("Quitting...");
System.exit(0);
} else {
try {
weapon = weapons[(choice - 1)];
} catch (IndexOutOfBoundsException e) {
errorMsg();
getPlayerWeapon();
}
}
return weapon;
}
public void errorMsg() {
System.out.println("\nInvalid Entry.");
System.out.println("Please Select Again...\n\n");
}
public String getCpuWeapon() {
int rounded = -1;
while (rounded < 0 || rounded > 2) {
double randomNum = Math.random();
rounded = Math.round(3 * (float)(randomNum));
}
return weapons[rounded];
}
public void getWinner(String cpuWeapon, String playerWeapon) {
if (cpuWeapon == playerWeapon) {
System.out.println("Tie!");
} else if (winners.get(cpuWeapon) == playerWeapon) {
System.out.println("Computer Wins!");
} else if (winners.get(playerWeapon) == cpuWeapon) {
System.out.println("You Win!");
}
}
}
any feedback would be most welcome :)