Skip to content

Instantly share code, notes, and snippets.

View jakesorce's full-sized avatar

Jake Sorce jakesorce

View GitHub Profile
updateScore = (key) => {
let { dice } = this.props;
let { scores } = this.state;
let entry = scores.find( d => d.name === key );
if (entry.value) {
entry.score = singles(entry.value, dice);
} else if (entry.addAll) {
entry.score = addAllDice(dice);
} else {
entry.score = staticScore(entry.name);
state = {
scores: [
{ section: 'upper', name: 'Ones', score: null, value: 1 },
{ section: 'upper', name: 'Twos', score: null, value: 2 },
{ section: 'upper', name: 'Threes', score: null, value: 3 },
{ section: 'upper', name: 'Fours', score: null, value: 4 },
{ section: 'upper', name: 'Fives', score: null, value: 5 },
{ section: 'upper', name: 'Sixes', score: null, value: 6 },
{ section: 'lower', name: 'Three Of A Kind', score: null, addAll: true },
{ section: 'lower', name: 'Four Of A Kind', score: null, addAll: true },
export const singles = (value, dice) => {
return dice.filter( d => d === value ).reduce( (total, val) => {
return total + value;
}, 0);
}
export const addAllDice = (dice) => {
return dice.reduce( (total, val) => {
return total + val;
}, 0);
import React from 'react';
const ScoreRow = ({ name, score }) => (
<li className="collection-item">
<div>
{name}
{ score !== null ?
<span className="secondary-content">{score}</span> :
<i style={styles.icon} className="secondary-content material-icons">play_circle_filled</i>
}
import React from 'react';
import ScoreRow from './ScoreRow';
const total = (score, label) => (
<li
key={label}
className="collection-item"
>
<div>
{label}
import React from 'react';
import ScoreSection from './ScoreSection';
class ScoreCard extends React.Component {
state = {
scores: [
{ section: 'upper', name: 'Ones', score: null },
{ section: 'upper', name: 'Twos', score: null },
{ section: 'upper', name: 'Threes', score: null },
{ section: 'upper', name: 'Fours', score: null },
toggleKept = (i) => {
let { keep } = this.state;
let updatedKeep;
if (keep.includes(i))
updatedKeep = keep.filter( k => k !== i )
else
updatedKeep = [...keep, i]
this.setState({ keep: updatedKeep })
rollDice = () => {
let { keep } = this.state;
let dice = this.state.dice.map( (el, i) => {
if (keep.includes(i))
return el
return Math.floor(Math.random() * 6) + 1
});
this.setState( (state) => {
import React from 'react';
import Board from './Board';
import ScoreCard from './ScoreCard';
class Game extends React.Component {
state = { roll: 0, dice: [...new Array(5)], keep: [] };
rollDice = () => {
let dice = this.state.dice.map( (el, i) => {
return Math.floor(Math.random() * 6 ) + 1
import React from 'react';
import d1 from './images/d1.png';
import d2 from './images/d2.png';
import d3 from './images/d3.png';
import d4 from './images/d4.png';
import d5 from './images/d5.png';
import d6 from './images/d6.png';
const images = { d1, d2, d3, d4, d5, d6 };