Created
January 15, 2015 13:46
-
-
Save Donkie/1a7c69ad16c33d61d16f to your computer and use it in GitHub Desktop.
RPS Game in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.Random; | |
public class Main{ | |
public static void main(String[] args) | |
{ | |
Scanner input = new Scanner(System.in); | |
System.out.println("Welcome to the endless RPS game!"); | |
System.out.println("Do you have a friend to play against? Y/N"); | |
boolean againstBot = false; | |
while(true) | |
{ | |
String s = input.nextLine(); | |
s = s.toLowerCase().trim(); | |
if(s.equals("n")) | |
againstBot = true; | |
else | |
continue; | |
break; | |
} | |
while(true) // Play RPS forever! | |
{ | |
//New round | |
System.out.println("********"); | |
System.out.println("NEW GAME"); | |
System.out.println("********"); | |
Type p1 = null; | |
Type p2 = null; | |
System.out.println("Player 1, type your move"); | |
while(true) // Loop until we have a good output | |
{ | |
String s = input.nextLine(); | |
p1 = Type.parseType(s); | |
if(p1 != null) | |
break; | |
System.out.println("Please try again."); | |
} | |
if(againstBot) | |
{ | |
p2 = Type.randomType(); | |
} | |
else | |
{ | |
System.out.println("Player 2, type your move"); | |
while(true) | |
{ | |
String s = input.nextLine(); | |
p2 = Type.parseType(s); | |
if(p2 != null) | |
break; | |
System.out.println("Please try again."); | |
} | |
} | |
if(p1 == p2) | |
{ | |
System.out.println("It's a tie!"); | |
} | |
else if(p1.beats(p2)) | |
{ | |
System.out.println(p1 + " " + p1.verb(p2) + " " + p2); | |
System.out.println("Player 1 wins!"); | |
} | |
else if(p2.beats(p1)) | |
{ | |
System.out.println(p2 + " " + p2.verb(p1) + " " + p1); | |
System.out.println("Player 2 wins!"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Type{ | |
ROCK{ | |
@Override | |
public boolean beats(Type other){ | |
return other == SCISSOR || other == LIZARD; | |
} | |
@Override | |
public String verb(Type other){ | |
if(other == SCISSOR) | |
return "crushes"; | |
if(other == LIZARD) | |
return "crushes"; | |
return null; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Rock"; | |
} | |
}, PAPER{ | |
@Override | |
public boolean beats(Type other){ | |
return other == ROCK || other == SPOCK; | |
} | |
@Override | |
public String verb(Type other){ | |
if(other == ROCK) | |
return "covers"; | |
if(other == SPOCK) | |
return "disproves"; | |
return null; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Paper"; | |
} | |
}, SCISSOR{ | |
@Override | |
public boolean beats(Type other){ | |
return other == PAPER || other == LIZARD; | |
} | |
@Override | |
public String verb(Type other){ | |
if(other == PAPER) | |
return "cuts"; | |
if(other == LIZARD) | |
return "decapitates"; | |
return null; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Scissor"; | |
} | |
}, LIZARD{ | |
@Override | |
public boolean beats(Type other){ | |
return other == PAPER || other == SPOCK; | |
} | |
@Override | |
public String verb(Type other){ | |
if(other == PAPER) | |
return "eats"; | |
if(other == SPOCK) | |
return "poisons"; | |
return null; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Lizard"; | |
} | |
}, SPOCK{ | |
@Override | |
public boolean beats(Type other){ | |
return other == SCISSOR || other == ROCK; | |
} | |
@Override | |
public String verb(Type other){ | |
if(other == SCISSOR) | |
return "smashes"; | |
if(other == ROCK) | |
return "vaporizes"; | |
return null; | |
} | |
@Override | |
public String toString() | |
{ | |
return "Spock"; | |
} | |
}; | |
public static Type parseType(String value){ | |
value = value.toLowerCase().trim(); | |
if(value.equals("rock")) | |
return ROCK; | |
else if(value.equals("paper")) | |
return PAPER; | |
else if(value.equals("scissor")) | |
return SCISSOR; | |
else if(value.equals("lizard")) | |
return LIZARD; | |
else if(value.equals("spock")) | |
return SPOCK; | |
else | |
return null; | |
} | |
public static Type randomType() | |
{ | |
return Type.values()[(int)(Math.random()*Type.values().length)]; | |
} | |
public abstract boolean beats(Type other); //Used to determine who wins? | |
public abstract String verb(Type other); //Incase we won, what action did we do? | |
} | |
/* | |
Scissors cuts paper | |
Paper covers rock | |
Rock crushes lizard | |
Lizard poisons Spock | |
Spock smashes scissors | |
Scissors decapitates lizard | |
Lizard eats paper | |
Paper disproves Spock | |
Spock vaporizes rock | |
Rock crushes scissors | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment