Skip to content

Commit

Permalink
Track Card Component
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 24, 2022
1 parent ad4615f commit aad89be
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions evens-or-odds/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Instructions from './Instructions';
import DrawCard from './DrawCard';
import Card from './Card';
import Guess from './Guess';
import GameState from './GameState';

class App extends Component {
startGame = () => {
Expand All @@ -33,6 +34,7 @@ class App extends Component {
this.props.gameStarted ? (
<div>
<h3>The game is on!</h3>
<GameState />
<br />
<Guess />
<br />
Expand Down
20 changes: 20 additions & 0 deletions evens-or-odds/src/components/GameState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';
import { connect } from 'react-redux';

const GameState = ({ correctGuesses, remaining }) => {
const guessText = correctGuesses === 1 ? 'guess' : 'guesses';

return (
<div>
<p>{remaining} cards remaining</p>
<p>{correctGuesses} correct {guessText}</p>
</div>
)
}

export default connect(
({
deck: { remaining },
gameState: { correctGuesses }
}) => ({ remaining, correctGuesses })
)(GameState);
19 changes: 17 additions & 2 deletions evens-or-odds/src/reducers/gameState.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { SET_GUESS, SET_GAME_STARTED } from '../actions/types';
import { SET_GUESS, SET_GAME_STARTED, DECK_DRAW } from '../actions/types';

const DEFAULT_GAME_STATE = { guess: '' };
const DEFAULT_GAME_STATE = { guess: '', correctGuesses: 0 };

const EVENS = ['2', '4', '6', '8', '10'];
const ODDS = ['ACE', '3', '5', '7', '9'];

const gameStateReducer = (state = DEFAULT_GAME_STATE, action) => {
switch(action.type) {
case SET_GUESS:
return { ...state, guess: action.guess };
case SET_GAME_STARTED:
return DEFAULT_GAME_STATE;
case DECK_DRAW.FETCH_SUCCESS:
const { value } = action.cards[0];
const { guess, correctGuesses } = state;

if (
(guess === 'even' && EVENS.includes(value)) ||
(guess === 'odd' && ODDS.includes(value))
) {
return { ...state, correctGuesses: correctGuesses+1 };
}

return state;
default:
return state;
}
Expand Down

0 comments on commit aad89be

Please sign in to comment.