11package com .baeldung .game ;
22
3- import java .util .HashMap ;
4- import java .util .Map ;
5- import java .util .Random ;
6- import java .util .Scanner ;
3+ import java .util .*;
74
85class RockPaperScissorsGame {
96
10- private static Map <Integer , String > movesMap = new HashMap <Integer , String >() {{
11- put (0 , "rock" );
12- put (1 , "paper" );
13- put (2 , "scissors" );
14- }};
7+ enum Move {
8+ ROCK ("rock" ),
9+ PAPER ("paper" ),
10+ SCISSORS ("scissors" );
11+
12+ private String value ;
13+
14+ Move (String value ) {
15+ this .value = value ;
16+ }
17+
18+ public String getValue () {
19+ return value ;
20+ }
21+ }
1522
1623 public static void main (String [] args ) {
1724 Scanner scanner = new Scanner (System .in );
@@ -31,7 +38,7 @@ public static void main(String[] args) {
3138 break ;
3239 }
3340
34- if (! movesMap . containsValue ( playerMove )) {
41+ if (Arrays . stream ( Move . values ()). noneMatch ( x -> x . getValue (). equals ( playerMove ) )) {
3542 System .out .println ("Your move isn't valid!" );
3643 continue ;
3744 }
@@ -51,15 +58,15 @@ public static void main(String[] args) {
5158 }
5259
5360 private static boolean isPlayerWin (String playerMove , String computerMove ) {
54- return playerMove .equals ("rock" ) && computerMove .equals ("scissors" )
55- || (playerMove .equals ("scissors" ) && computerMove .equals ("paper" ))
56- || (playerMove .equals ("paper" ) && computerMove .equals ("rock" ));
61+ return playerMove .equals (Move . ROCK . value ) && computerMove .equals (Move . SCISSORS . value )
62+ || (playerMove .equals (Move . SCISSORS . value ) && computerMove .equals (Move . PAPER . value ))
63+ || (playerMove .equals (Move . PAPER . value ) && computerMove .equals (Move . ROCK . value ));
5764 }
5865
5966 private static String getComputerMove () {
6067 Random random = new Random ();
6168 int randomNumber = random .nextInt (3 );
62- String computerMove = movesMap . get ( randomNumber );
69+ String computerMove = Move . values ()[ randomNumber ]. getValue ( );
6370 System .out .println ("Computer move: " + computerMove );
6471 return computerMove ;
6572 }
0 commit comments