Created
September 26, 2016 23:33
-
-
Save JoeFurfaro/a5fa012af6c393fd352cf396d810bb52 to your computer and use it in GitHub Desktop.
A mini expandable project of the game Rock Paper Scissors Lizard Spock
This file contains 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
/* | |
* Expandable Rock Paper Scissors Lizard Spock | |
* By Joe Furfaro | |
*/ | |
import java.util.Random; | |
import java.util.Scanner; | |
public class RPSLS | |
{ | |
public static void main(String[] args) | |
{ | |
play(); | |
} | |
static void play() | |
{ | |
Scanner input = new Scanner(System.in); | |
System.out.println("Pick a move! (Enter the move's associated number value)"); | |
for(Move m : Move.values()) | |
{ | |
System.out.println("[" + m.ordinal() + "] " + m.toString()); | |
} | |
Move userMove = null; | |
try | |
{ | |
int moveInt = Integer.parseInt(input.nextLine()); | |
for(Move m : Move.values()) | |
{ | |
if(m.ordinal() == moveInt) | |
{ | |
userMove = m; | |
} | |
} | |
} catch(Exception e) | |
{ | |
System.out.println("That is not a valid number!"); | |
} | |
input.close(); | |
int moveCount = 0; | |
for(@SuppressWarnings("unused") Move m : Move.values()) | |
{ | |
moveCount += 1; | |
} | |
int cpuMoveInt = new Random().nextInt(moveCount); | |
Move cpuMove = null; | |
for(Move m : Move.values()) | |
{ | |
if(cpuMoveInt == m.ordinal()) | |
{ | |
cpuMove = m; | |
} | |
} | |
int u = userMove.ordinal(); | |
int c = cpuMove.ordinal(); | |
int sum = u + c; | |
boolean win = true; | |
if(u == c) | |
{ | |
System.out.println("It's a tie!"); | |
return; | |
} | |
if(u > c) | |
{ | |
if(sum %2 == 0) | |
{ | |
win = true; | |
} else | |
{ | |
win = false; | |
} | |
} else if(u < c) | |
{ | |
if(sum %2 == 0) | |
{ | |
win = false; | |
} else | |
{ | |
win = true; | |
} | |
} | |
if(win) | |
{ | |
System.out.println("You win! (CPU chose " + cpuMove.toString() + ")"); | |
} else | |
{ | |
System.out.println("You lose. (CPU chose " + cpuMove.toString() + ")"); | |
} | |
} | |
static enum Move | |
{ | |
//Feel free to add and remove... Completely expandable | |
SCISSORS, | |
PAPER, | |
ROCK, | |
LIZARD, | |
SPOCK, | |
PANTS, | |
SHIRT; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment