11import React from "react" ;
22
3- const MOVES_CONFIG = {
3+ const MOVE_CONFIG = {
44 rock : {
55 scissors : "crushes" ,
66 lizard : "crushes" ,
@@ -23,27 +23,35 @@ const MOVES_CONFIG = {
2323 } ,
2424} as const ;
2525
26- type Move = keyof typeof MOVES_CONFIG ;
26+ type Move = keyof typeof MOVE_CONFIG ;
2727
2828function objectKeys < T extends Record < PropertyKey , unknown > > (
2929 obj : T ,
3030) : ( keyof T ) [ ] {
3131 return Object . keys ( obj ) ;
3232}
3333
34- const MOVES : readonly Move [ ] = objectKeys ( MOVES_CONFIG ) ;
34+ const MOVES : readonly Move [ ] = objectKeys ( MOVE_CONFIG ) ;
3535
3636function getRandomMove ( ) : Move {
3737 const index = Math . floor ( Math . random ( ) * MOVES . length ) ;
3838 return MOVES [ index ] ;
3939}
4040
41+ const MOVE_EMOJI = {
42+ rock : "🪨" ,
43+ paper : "📄" ,
44+ scissors : "✂️" ,
45+ lizard : "🦎" ,
46+ Spock : "🖖" ,
47+ } as const satisfies Record < Move , string > ;
48+
4149function getWinner ( moveA : Move , moveB : Move ) : "a" | "b" | "tie" {
4250 if ( moveA === moveB ) {
4351 return "tie" ;
4452 }
4553
46- return Object . hasOwn ( MOVES_CONFIG [ moveA ] , moveB ) ? "a" : "b" ;
54+ return Object . hasOwn ( MOVE_CONFIG [ moveA ] , moveB ) ? "a" : "b" ;
4755}
4856
4957type GameResults = { wins : number ; losses : number ; draws : number } ;
@@ -67,7 +75,7 @@ function App() {
6775 display : "flex" ,
6876 flexDirection : "row" ,
6977 alignItems : "center" ,
70- gap : 5 ,
78+ gap : 10 ,
7179 } as const ;
7280
7381 const handleMove = ( move : Move ) => {
@@ -93,11 +101,23 @@ function App() {
93101 < div style = { containerStyle } >
94102 < span > Make your move!</ span >
95103 < div style = { buttonRowStyle } >
96- < button onClick = { ( ) => handleMove ( "rock" ) } > 🪨</ button >
97- < button onClick = { ( ) => handleMove ( "paper" ) } > 📄</ button >
98- < button onClick = { ( ) => handleMove ( "scissors" ) } > ✂️</ button >
99- < button onClick = { ( ) => handleMove ( "lizard" ) } > 🦎</ button >
100- < button onClick = { ( ) => handleMove ( "Spock" ) } > 🖖</ button >
104+ { MOVES . map ( ( move ) => (
105+ < button
106+ key = { move }
107+ onClick = { ( ) => handleMove ( move ) }
108+ style = { {
109+ fontSize : 30 ,
110+ // line-height unit doesn't default to pixels!
111+ lineHeight : "50px" ,
112+ textAlign : "center" ,
113+ width : 50 ,
114+ height : 50 ,
115+ padding : 0 ,
116+ } }
117+ >
118+ { MOVE_EMOJI [ move ] }
119+ </ button >
120+ ) ) }
101121 </ div >
102122 </ div >
103123 ) ;
0 commit comments