Skip to content

Instantly share code, notes, and snippets.

@JoeFurfaro
Created September 26, 2016 23:33
Show Gist options
  • Select an option

  • Save JoeFurfaro/a5fa012af6c393fd352cf396d810bb52 to your computer and use it in GitHub Desktop.

Select an option

Save JoeFurfaro/a5fa012af6c393fd352cf396d810bb52 to your computer and use it in GitHub Desktop.

Revisions

  1. JoeFurfaro created this gist Sep 26, 2016.
    111 changes: 111 additions & 0 deletions RPSLS.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    /*
    * 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;
    }
    }