@@ -5,6 +5,7 @@ import { Text, View, StyleSheet, TouchableHighlight} from 'react-native';
5
5
6
6
export default class GameScreen extends React . Component {
7
7
constructor ( props ) {
8
+
8
9
super ( props ) ;
9
10
10
11
this . state = {
@@ -14,6 +15,8 @@ export default class GameScreen extends React.Component {
14
15
this . props . navigation . getParam ( 'data' ) . map ( player =>
15
16
this . state . scoreboard [ player . name ] = { 20 : 0 , 19 : 0 , 18 : 0 , 17 : 0 , 16 : 0 , 15 : 0 , B : 0 , score : 0 }
16
17
)
18
+
19
+ this . history = [ JSON . parse ( JSON . stringify ( this . state . scoreboard ) ) ]
17
20
}
18
21
19
22
textColor = ( clicks ) => {
@@ -47,10 +50,21 @@ export default class GameScreen extends React.Component {
47
50
}
48
51
}
49
52
53
+ undoFunction = ( ) => {
54
+ console . log ( this . history ) ;
55
+ if ( this . history . length > 1 ) {
56
+ this . setState ( prevState => {
57
+ this . history . pop ( )
58
+ return { scoreboard : JSON . parse ( JSON . stringify ( this . history [ this . history . length - 1 ] ) ) }
59
+ } )
60
+ }
61
+ }
62
+
50
63
scoreboardButton = ( name , num ) => {
51
64
if ( this . state . scoreboard [ name ] [ num ] < 3 ) {
52
65
this . setState ( prevState => {
53
66
prevState . scoreboard [ name ] [ num ] ++ ;
67
+ this . history . push ( JSON . parse ( JSON . stringify ( prevState . scoreboard ) ) ) ;
54
68
return { scoreboard : prevState . scoreboard }
55
69
} )
56
70
}
@@ -65,6 +79,7 @@ export default class GameScreen extends React.Component {
65
79
else {
66
80
prevState . scoreboard [ key ] [ "score" ] += num ;
67
81
}
82
+ this . history . push ( JSON . parse ( JSON . stringify ( prevState . scoreboard ) ) ) ;
68
83
return { scoreboard : prevState . scoreboard }
69
84
} )
70
85
}
@@ -140,12 +155,44 @@ export default class GameScreen extends React.Component {
140
155
< View style = { { flexDirection : 'row' , flex : 1 } } >
141
156
{ buttonsListArr }
142
157
</ View >
158
+ < View style = { { flexDirection : 'row' } } >
159
+ < TouchableHighlight style = { { flex :1 } } underLayColor = 'green' onPress = { ( ) => { this . undoFunction ( ) } } >
160
+ < View style = { styles . undoButton } >
161
+ < Text style = { styles . undoText } > Undo </ Text >
162
+ </ View >
163
+ </ TouchableHighlight >
164
+ < TouchableHighlight style = { { flex :1 } } underLayColor = 'green' onPress = { ( ) => { this . props . navigation . navigate ( 'Home' ) } } >
165
+ < View style = { styles . newGameButton } >
166
+ < Text style = { styles . newGameText } > New Game </ Text >
167
+ </ View >
168
+ </ TouchableHighlight >
169
+ </ View >
143
170
</ View >
144
171
) ;
145
172
}
146
173
}
147
174
148
175
const styles = StyleSheet . create ( {
176
+ undoButton : {
177
+ alignItems : 'center' ,
178
+ backgroundColor : 'indianred'
179
+ } ,
180
+ undoText : {
181
+ padding : 15 ,
182
+ fontSize : 25 ,
183
+ fontWeight : 'bold' ,
184
+ color : 'white'
185
+ } ,
186
+ newGameButton : {
187
+ alignItems : 'center' ,
188
+ backgroundColor : '#2196F3'
189
+ } ,
190
+ newGameText : {
191
+ padding : 15 ,
192
+ fontSize : 25 ,
193
+ fontWeight : 'bold' ,
194
+ color : 'white'
195
+ } ,
149
196
playerContainer : {
150
197
height : 70 ,
151
198
alignItems : 'center' ,
0 commit comments