1
- import { useState , useReducer } from "react" ;
1
+ import { useState } from "react" ;
2
2
import GameCard from "./components/GameCard" ;
3
3
import "./App.css" ;
4
4
import { Navbar , Nav , NavDropdown , Row , Col , Container } from "react-bootstrap" ;
@@ -66,24 +66,39 @@ const defaultPokedex = [
66
66
} ,
67
67
] ;
68
68
69
- // BUGS : When high score is recorded images stop changing.
69
+ // FIXME : When high score is recorded images stop changing.
70
70
function App ( ) {
71
- const [ , forceUpdate ] = useReducer ( x => x + 1 , 0 ) ;
72
71
const [ score , setScore ] = useState ( 0 ) ;
73
- const [ highScore , setHS ] = useState ( 0 ) ;
72
+ const [ highScore , setHighScore ] = useState ( 0 ) ;
74
73
const [ pokedex , setPokedex ] = useState ( defaultPokedex ) ;
74
+ const [ selectedTokens , setSelected ] = useState ( [ ] ) ;
75
75
const [ pokemonToDisplay , setPokemonToDisply ] = useState ( defaultPokedex ) ;
76
76
77
- function isHighScore ( newScore ) {
78
- if ( newScore > highScore ) setHS ( newScore ) ;
77
+ function gameOver ( newScore ) {
78
+ // Check if score is higher than old HS
79
+ // if (newScore > highScore) { setHS(newScore) } //Set HS to new score
80
+ if ( newScore > highScore ) {
81
+ setHighScore ( newScore ) ;
82
+ }
83
+ // reset score
84
+ // TODO: Reset clicked values.
79
85
setScore ( 0 ) ;
80
86
}
81
87
88
+ function resetGame ( ) {
89
+ const resetDex = pokedex . map ( ( pokemon ) => {
90
+ return { ...pokemon , clicked : false } ;
91
+ } ) ;
92
+ console . log ( resetDex ) ;
93
+ setSelected ( '' ) ;
94
+ console . log ( "Reset Pokedex" )
95
+ }
96
+
82
97
function shuffle ( array ) {
83
98
let currentIndex = array . length ;
84
99
85
100
// While there remain elements to shuffle
86
- while ( currentIndex != 0 ) {
101
+ while ( currentIndex != 0 ) {
87
102
// Pick a remaining element...
88
103
let randomIndex = Math . floor ( Math . random ( ) * currentIndex ) ;
89
104
currentIndex -- ;
@@ -92,60 +107,59 @@ function App() {
92
107
[ array [ currentIndex ] , array [ randomIndex ] ] = [
93
108
array [ randomIndex ] , array [ currentIndex ] ] ;
94
109
}
95
- setPokemonToDisply ( array ) ;
96
- forceUpdate ( ) ;
110
+ return array ;
97
111
}
98
112
99
- const handleClick = ( pokemonID ) => {
100
- let newState = ( pokedex . map ( pokemon => {
101
- if ( pokemon . id === pokemonID ) {
102
- pokemon . clicked ? ( isHighScore ( score ) ) : ( setScore ( score + 1 ) ) ;
103
- return { ...pokedex , clicked : true } ;
104
- } else {
105
- return pokemon ;
106
- }
107
- } ) ) ;
108
- setPokedex ( newState ) ;
109
- shuffle ( newState ) ;
113
+ const handleClick = ( pokemonName ) => {
114
+ if ( selectedTokens . indexOf ( pokemonName ) >= 0 ) {
115
+ gameOver ( score ) ;
116
+ resetGame ( ) ;
117
+ } else {
118
+ console . log ( `Added ${ pokemonName } ` ) ;
119
+ setSelected ( [ ...selectedTokens , pokemonName ] )
120
+ setScore ( score + 1 ) ;
121
+ }
122
+
123
+ // Set new order
124
+ setPokemonToDisply ( shuffle ( pokemonToDisplay ) ) ;
110
125
} ;
111
126
112
127
return (
113
128
< >
114
- < Navbar expand = "lg" className = "bg-body-tertiary" pb = { 4 } >
115
- < Container >
116
- < Navbar . Brand href = "#home" > Pokemon Memorization</ Navbar . Brand >
117
- < Navbar . Toggle aria-controls = "basic-navbar-nav" />
118
- < Navbar . Collapse id = "basic-navbar-nav" >
119
- </ Navbar . Collapse >
129
+ < Navbar expand = "lg" className = "bg-body-tertiary" pb = { 4 } >
130
+ < Container >
131
+ < Navbar . Brand href = "#home" > Pokemon Memorization</ Navbar . Brand >
132
+ < Navbar . Toggle aria-controls = "basic-navbar-nav" />
133
+ < Navbar . Collapse id = "basic-navbar-nav" >
134
+ </ Navbar . Collapse >
120
135
< Navbar . Collapse className = "justify-content-end" >
121
136
< Col >
122
- < Row >
123
- < Navbar . Text >
124
- Score: { score }
125
- </ Navbar . Text >
126
- </ Row >
127
- < Row >
128
- < Navbar . Text >
129
- High Score: { highScore }
130
- </ Navbar . Text >
131
- </ Row >
137
+ < Row >
138
+ < Navbar . Text >
139
+ Score: { score }
140
+ </ Navbar . Text >
141
+ </ Row >
142
+ < Row >
143
+ < Navbar . Text >
144
+ High Score: { highScore }
145
+ </ Navbar . Text >
146
+ </ Row >
132
147
</ Col >
133
148
</ Navbar . Collapse >
134
- </ Container >
149
+ </ Container >
135
150
136
- </ Navbar >
137
- < Container >
151
+ </ Navbar >
152
+ < Container >
138
153
< Row className = "justify-content-md-center pt-5" >
139
- { pokemonToDisplay . map ( ( pokemon , index ) => (
140
- < GameCard
141
- key = { index }
142
- name = { pokemon . name }
143
- handleClick = { handleClick }
144
- id = { pokemon . id }
145
- />
146
- ) ) }
147
- </ Row >
148
- </ Container >
154
+ { pokemonToDisplay . map ( ( pokemon , index ) => (
155
+ < GameCard
156
+ key = { index }
157
+ pokemon = { pokemon }
158
+ handleClick = { handleClick }
159
+ />
160
+ ) ) }
161
+ </ Row >
162
+ </ Container >
149
163
</ >
150
164
) ;
151
165
}
0 commit comments