Playing cards with ReactJS











up vote
0
down vote

favorite












I came across problem 54 on projecteuler.net and decided to give it a go using ReactJS. I have had Angular experience prior to this. I wrote a couple of fully tested components. I think that they only have one purpose each, but they're still very big. I did my best code-wise because it's also a project where I want to show off the things I learned from a programming book about clean code. Any feedback would be greatly appreciated. I'll start with the first component I created and end it with the last.



Playing card



import React from 'react';
import propTypes from 'prop-types';
import { isNumeric } from '../../Helper';
import './Playing-Card.scss';

const cardSuites = {
D: 'diamonds',
S: 'spades',
H: 'hearts',
C: 'clubs',
};

const cardCourts = {
K: 'king',
J: 'jack',
Q: 'queen',
A: 'ace',
T: '10',
};

const cardValues = {
ace: '14',
king: '13',
queen: '12',
jack: '11',
};

const PlayingCard = ({ cardSymbol }) => {
const url = convertCardSymbolToUrl(cardSymbol);
return <img alt="playing-card" className="playing-card" src={url} />;
};

PlayingCard.propTypes = { cardSymbol: propTypes.string.isRequired };

export const convertCardSymbolToUrl = cardSymbol => `/cards/${getCardCourt(cardSymbol)}_of_${getCardSuite(cardSymbol)}.svg`;

export const getCardSuite = cardSymbol => cardSuites[cardSymbol[1]];

export const getCardCourt = cardSymbol => {
const courtSymbol = cardSymbol[0];
if (isNumeric(courtSymbol)) {
return String(courtSymbol);
}
return String(cardCourts[courtSymbol]);
};

export const getCardValue = cardCourt => {
if (isNumeric(cardCourt)) {
return Number(cardCourt);
}
return Number(cardValues[cardCourt]);
};

export default PlayingCard;


Hand



import React from 'react';
import propTypes from 'prop-types';
import { generateUniqueKeysForItems } from '../../Helper';
import PlayingCard, { getCardCourt, getCardValue, getCardSuite } from '../Playing-Card/Playing-Card';

const Hand = ({ cards }) => {
let cardsArray = cards.split(' ');
cardsArray = generateUniqueKeysForItems(cardsArray);
return cardsArray.map(card => <PlayingCard key={card.id} cardSymbol={card.value} />);
};

Hand.propTypes = { cards: propTypes.string.isRequired };

export const calculateHandValue = cardsArray => {
let cardsFrequency = calculateCardsFrequency(cardsArray);
cardsFrequency = sortFrequencyDesc(cardsFrequency);
const individualCardValues = calculateIndividualCardValues(cardsFrequency);
let handValue = 9;
const cardsCombinations = [
hasRoyalFlush,
hasStraightFlush,
hasFourOfAKind,
hasFullHouse,
hasFlush,
hasStraight,
hasThreeOfAKind,
hasTwoPairs,
hasTwoOfAKind,
];

for (let i = 0; i < cardsCombinations.length; i += 1) {
const isCombinationValid = cardsCombinations[i];
if (isCombinationValid(cardsArray)) {
break;
}
handValue -= 1;
}

return [handValue, individualCardValues];
};

export const calculateIndividualCardValues = cardsFrequency =>
Array.from(cardsFrequency.keys()).map(key => getCardValue(key));


export const hasRoyalFlush = cardsArray => {
const cardCourtsRequired = ['10', 'jack', 'queen', 'king', 'ace'];
let qualifies = true;

if (!hasSameSuits(cardsArray)) {
return false;
}
cardsArray.forEach(card => {
const court = getCardCourt(card);
if (cardCourtsRequired.indexOf(court) === -1) {
qualifies = false;
}
});

return qualifies;
};

export const hasStraightFlush = cardsArray =>
hasSameSuits(cardsArray) && hasIncrementalCourts(cardsArray);

export const hasStraight = cardsArray => hasIncrementalCourts(cardsArray);
export const hasFlush = cardsArray => hasSameSuits(cardsArray);

export const hasFourOfAKind = cardsArray => hasXOfAKind(cardsArray, 4);
export const hasThreeOfAKind = cardsArray => hasXOfAKind(cardsArray, 3);
export const hasTwoOfAKind = cardsArray => hasXOfAKind(cardsArray, 2);

const hasXOfAKind = (cardsArray, x) => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(x) !== -1;
};

export const hasTwoPairs = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
let numberOfPairs = 0;
frequencyArray.forEach(frequencyNumber => {
if (frequencyNumber === 2) {
numberOfPairs += 1;
}
});
return numberOfPairs === 2;
};

export const hasFullHouse = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(3) !== -1 && frequencyArray.indexOf(2) !== -1;
};


export const hasSameSuits = cardsArray => {
const hasEqualSuits = (card1, card2) => getCardSuite(card1) === getCardSuite(card2);

for (let i = 1; i < cardsArray.length; i += 1) {
const card = cardsArray[i];
const previousCard = cardsArray[i - 1];

if (!hasEqualSuits(card, previousCard)) {
return false;
}
}
return true;
};

export const hasIncrementalCourts = cardsArray => {
const hasIncrementalCardValues = (card1, card2) =>
getCardValue(getCardCourt(card1)) + 1 === getCardValue(getCardCourt(card2));

const sortedCards = sortByCardValueAsc(cardsArray);
for (let i = 1; i < sortedCards.length; i += 1) {
const card = sortedCards[i];
const previousCard = sortedCards[i - 1];
if (!hasIncrementalCardValues(previousCard, card)) {
return false;
}
}
return true;
};

export const calculateCardsFrequency = cardsArray => {
const cardFrequency = new Map();
cardsArray.forEach(card => {
const court = String(getCardCourt(card));
if (cardFrequency.get(court) != null) {
cardFrequency.set(court, cardFrequency.get(court) + 1);
} else {
cardFrequency.set(court, 1);
}
});
return cardFrequency;
};

export const sortFrequencyDesc = cardsFrequency =>
new Map([...cardsFrequency.entries()].sort((a, b) => {
const aCardCourt = a[0];
const aFrequency = a[1];
const bCardCourt = b[0];
const bFrequency = b[1];

if (aFrequency < bFrequency) {
return 1;
}
if (aFrequency === bFrequency && getCardValue(aCardCourt) < getCardValue(bCardCourt)) {
return 1;
}
return -1;
}));

export const sortByCardValueAsc = cardsArray => {
cardsArray.sort((card1, card2) => {
const court1 = getCardCourt(card1);
const court2 = getCardCourt(card2);
if (getCardValue(court1) > getCardValue(court2)) {
return 1;
}
if (getCardValue(court1) < getCardValue(court2)) {
return -1;
}
return 0;
});
return cardsArray;
};


export default Hand;


Round



import React from 'react';
import propTypes from 'prop-types';
import { connect } from 'react-redux';
import Hand, { calculateHandValue } from '../Hand/Hand';
import './Round.scss';

const Round = ({ cards, incrementScore }) => {
const playerHands = splitCards(cards);
if (playerHands[0] === '') {
return <div />;
}
const winner = determineWinner(playerHands[0], playerHands[1]);
incrementScore(`player${winner}`);
return (
<div className="round-root">
<div className={(winner === 1) ? 'winner' : ''}>
<Hand cards={playerHands[0]} />
</div>
<div className={(winner === 2) ? 'winner' : ''}>
<Hand cards={playerHands[1]} />
</div>
</div>
);
};

Round.propTypes = {
cards: propTypes.string.isRequired,
incrementScore: propTypes.func.isRequired,
};

const mapStateToProps = state => ({});

const mapDispatchToProps = dispatch => ({
incrementScore: playerName => {
dispatch({
type: 'INCREMENT_SCORE',
payload: {
name: playerName,
},
});
},
});

export const splitCards = cards => {
const playerHands = ;
playerHands.push(cards.substr(0, Math.ceil(cards.length / 2) - 1));
playerHands.push(cards.substr(Math.ceil(cards.length / 2)));
return playerHands;
};

export const determineWinner = (player1Cards, player2Cards) => {
const p1HandValue = calculateHandValue(player1Cards.split(' '));
const p2HandValue = calculateHandValue(player2Cards.split(' '));

let winner = compareFirstTuples(p1HandValue, p2HandValue);

if (winner === 0) {
winner = compareSecondTuples(p1HandValue, p2HandValue);

if (winner === 0) {
throw Error('No winner could be determined');
}
}
return winner;
};

export const compareFirstTuples = (p1HandValue, p2HandValue) => {
if (p1HandValue[0] === p2HandValue[0]) { return 0; }
return (p1HandValue[0] > p2HandValue[0]) ? 1 : 2;
};


export const compareSecondTuples = (p1HandValue, p2HandValue) => {
const p1Tuple = p1HandValue[1];
const p2Tuple = p2HandValue[1];
let winner = 0;
const minLength = Math.min(p1Tuple.length, p2Tuple.length);
for (let i = 0; i < minLength; i += 1) {
if (p1Tuple[i] > p2Tuple[i]) {
winner = 1;
break;
}
if (p1Tuple[i] < p2Tuple[i]) {
winner = 2;
break;
}
}
return winner;
};

export default connect(mapStateToProps, mapDispatchToProps)(Round);


Game-Room



import React from 'react';
import propTypes from 'prop-types';
import Button from '@material-ui/core/Button';
import './Game-Room.scss';
import { connect } from 'react-redux';
import Game from '../Game/Game';
import WinnerDetails from '../Winner-Details/Winner-Details';


const possibleSuits = ['D', 'S', 'H', 'C'];
const possibleCourts = ['2', '3', '4', '5', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];

const GameRoom = ({ setMatches, matches, players, resetScore }) => {
const { player1, player2 } = players;
let winner = null;
if (player1.score > 0 || player1.score > 0) {
winner = (player1.score > player2.score)
? player1 : player2;
}

const readTextFile = event => {
const input = event.target;
const reader = new FileReader();
reader.onload = processGamesFile;
reader.onload = () => {
resetScore();
setMatches(processGamesFile(reader.result));
};
reader.readAsText(input.files[0]);
};

const processGamesFile = gamesFile => {
const retrievedMatches = gamesFile.split('n');
retrievedMatches.pop();
return retrievedMatches;
};

return (
<div className="game-room-root">
<Button variant="contained" color="primary" className="main-button left">
<input type="file" onChange={event => { readTextFile(event); }} />
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
resetScore();
setMatches(generateMatches(100));
}}
className="main-button right"
>
Generate Games
</Button>
<WinnerDetails winner={winner} />
<Game matches={matches} />
</div>
);
};

GameRoom.propTypes = {
setMatches: propTypes.func.isRequired,
resetScore: propTypes.func.isRequired,
players: propTypes.shape({}).isRequired,
matches: propTypes.arrayOf(propTypes.string).isRequired,
};

const mapStateToProps = state => ({
matches: state.gamesReducer,
players: state.playerReducer,
});

const mapDispatchToProps = dispatch => ({
setMatches: games => {
dispatch({
type: 'SET_GAMES',
payload: games,
});
},
resetScore: () => {
dispatch({
type: 'RESET_SCORE',
});
},
});


export const generateMatches = numberOfGames => {
const games = ;
for (let i = 0; i < numberOfGames; i += 1) {
const round = generateRound();
games.push(round);
}
return games;
};

export const generateRound = () => {
let cards = '';
let numberOfCards = 0;

while (numberOfCards < 10) {
const card = createRandomCard();
if (cards.indexOf(card) === -1) {
cards += `${card} `;
numberOfCards += 1;
}
}

cards = cards.slice(0, -1);
return cards;
};

const createRandomCard = () => getRandomCourt() + getRandomSuit();

const getRandomSuit = () => possibleSuits[Math.floor(Math.random() * possibleSuits.length)];
const getRandomCourt = () => possibleCourts[Math.floor(Math.random() * possibleCourts.length)];


export default connect(mapStateToProps, mapDispatchToProps)(GameRoom);


Components Winner-detail and Game are emitted because they're small enough. Game is just a container for rounds.










share|improve this question
















bumped to the homepage by Community 15 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Please post a short problem description along with your question. Otherwise, it will be hard for us to judge your code properly with regards to correctness and suitability for the problem at hand, among other characteristics.
    – Ben Steffan
    Jul 29 at 23:34















up vote
0
down vote

favorite












I came across problem 54 on projecteuler.net and decided to give it a go using ReactJS. I have had Angular experience prior to this. I wrote a couple of fully tested components. I think that they only have one purpose each, but they're still very big. I did my best code-wise because it's also a project where I want to show off the things I learned from a programming book about clean code. Any feedback would be greatly appreciated. I'll start with the first component I created and end it with the last.



Playing card



import React from 'react';
import propTypes from 'prop-types';
import { isNumeric } from '../../Helper';
import './Playing-Card.scss';

const cardSuites = {
D: 'diamonds',
S: 'spades',
H: 'hearts',
C: 'clubs',
};

const cardCourts = {
K: 'king',
J: 'jack',
Q: 'queen',
A: 'ace',
T: '10',
};

const cardValues = {
ace: '14',
king: '13',
queen: '12',
jack: '11',
};

const PlayingCard = ({ cardSymbol }) => {
const url = convertCardSymbolToUrl(cardSymbol);
return <img alt="playing-card" className="playing-card" src={url} />;
};

PlayingCard.propTypes = { cardSymbol: propTypes.string.isRequired };

export const convertCardSymbolToUrl = cardSymbol => `/cards/${getCardCourt(cardSymbol)}_of_${getCardSuite(cardSymbol)}.svg`;

export const getCardSuite = cardSymbol => cardSuites[cardSymbol[1]];

export const getCardCourt = cardSymbol => {
const courtSymbol = cardSymbol[0];
if (isNumeric(courtSymbol)) {
return String(courtSymbol);
}
return String(cardCourts[courtSymbol]);
};

export const getCardValue = cardCourt => {
if (isNumeric(cardCourt)) {
return Number(cardCourt);
}
return Number(cardValues[cardCourt]);
};

export default PlayingCard;


Hand



import React from 'react';
import propTypes from 'prop-types';
import { generateUniqueKeysForItems } from '../../Helper';
import PlayingCard, { getCardCourt, getCardValue, getCardSuite } from '../Playing-Card/Playing-Card';

const Hand = ({ cards }) => {
let cardsArray = cards.split(' ');
cardsArray = generateUniqueKeysForItems(cardsArray);
return cardsArray.map(card => <PlayingCard key={card.id} cardSymbol={card.value} />);
};

Hand.propTypes = { cards: propTypes.string.isRequired };

export const calculateHandValue = cardsArray => {
let cardsFrequency = calculateCardsFrequency(cardsArray);
cardsFrequency = sortFrequencyDesc(cardsFrequency);
const individualCardValues = calculateIndividualCardValues(cardsFrequency);
let handValue = 9;
const cardsCombinations = [
hasRoyalFlush,
hasStraightFlush,
hasFourOfAKind,
hasFullHouse,
hasFlush,
hasStraight,
hasThreeOfAKind,
hasTwoPairs,
hasTwoOfAKind,
];

for (let i = 0; i < cardsCombinations.length; i += 1) {
const isCombinationValid = cardsCombinations[i];
if (isCombinationValid(cardsArray)) {
break;
}
handValue -= 1;
}

return [handValue, individualCardValues];
};

export const calculateIndividualCardValues = cardsFrequency =>
Array.from(cardsFrequency.keys()).map(key => getCardValue(key));


export const hasRoyalFlush = cardsArray => {
const cardCourtsRequired = ['10', 'jack', 'queen', 'king', 'ace'];
let qualifies = true;

if (!hasSameSuits(cardsArray)) {
return false;
}
cardsArray.forEach(card => {
const court = getCardCourt(card);
if (cardCourtsRequired.indexOf(court) === -1) {
qualifies = false;
}
});

return qualifies;
};

export const hasStraightFlush = cardsArray =>
hasSameSuits(cardsArray) && hasIncrementalCourts(cardsArray);

export const hasStraight = cardsArray => hasIncrementalCourts(cardsArray);
export const hasFlush = cardsArray => hasSameSuits(cardsArray);

export const hasFourOfAKind = cardsArray => hasXOfAKind(cardsArray, 4);
export const hasThreeOfAKind = cardsArray => hasXOfAKind(cardsArray, 3);
export const hasTwoOfAKind = cardsArray => hasXOfAKind(cardsArray, 2);

const hasXOfAKind = (cardsArray, x) => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(x) !== -1;
};

export const hasTwoPairs = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
let numberOfPairs = 0;
frequencyArray.forEach(frequencyNumber => {
if (frequencyNumber === 2) {
numberOfPairs += 1;
}
});
return numberOfPairs === 2;
};

export const hasFullHouse = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(3) !== -1 && frequencyArray.indexOf(2) !== -1;
};


export const hasSameSuits = cardsArray => {
const hasEqualSuits = (card1, card2) => getCardSuite(card1) === getCardSuite(card2);

for (let i = 1; i < cardsArray.length; i += 1) {
const card = cardsArray[i];
const previousCard = cardsArray[i - 1];

if (!hasEqualSuits(card, previousCard)) {
return false;
}
}
return true;
};

export const hasIncrementalCourts = cardsArray => {
const hasIncrementalCardValues = (card1, card2) =>
getCardValue(getCardCourt(card1)) + 1 === getCardValue(getCardCourt(card2));

const sortedCards = sortByCardValueAsc(cardsArray);
for (let i = 1; i < sortedCards.length; i += 1) {
const card = sortedCards[i];
const previousCard = sortedCards[i - 1];
if (!hasIncrementalCardValues(previousCard, card)) {
return false;
}
}
return true;
};

export const calculateCardsFrequency = cardsArray => {
const cardFrequency = new Map();
cardsArray.forEach(card => {
const court = String(getCardCourt(card));
if (cardFrequency.get(court) != null) {
cardFrequency.set(court, cardFrequency.get(court) + 1);
} else {
cardFrequency.set(court, 1);
}
});
return cardFrequency;
};

export const sortFrequencyDesc = cardsFrequency =>
new Map([...cardsFrequency.entries()].sort((a, b) => {
const aCardCourt = a[0];
const aFrequency = a[1];
const bCardCourt = b[0];
const bFrequency = b[1];

if (aFrequency < bFrequency) {
return 1;
}
if (aFrequency === bFrequency && getCardValue(aCardCourt) < getCardValue(bCardCourt)) {
return 1;
}
return -1;
}));

export const sortByCardValueAsc = cardsArray => {
cardsArray.sort((card1, card2) => {
const court1 = getCardCourt(card1);
const court2 = getCardCourt(card2);
if (getCardValue(court1) > getCardValue(court2)) {
return 1;
}
if (getCardValue(court1) < getCardValue(court2)) {
return -1;
}
return 0;
});
return cardsArray;
};


export default Hand;


Round



import React from 'react';
import propTypes from 'prop-types';
import { connect } from 'react-redux';
import Hand, { calculateHandValue } from '../Hand/Hand';
import './Round.scss';

const Round = ({ cards, incrementScore }) => {
const playerHands = splitCards(cards);
if (playerHands[0] === '') {
return <div />;
}
const winner = determineWinner(playerHands[0], playerHands[1]);
incrementScore(`player${winner}`);
return (
<div className="round-root">
<div className={(winner === 1) ? 'winner' : ''}>
<Hand cards={playerHands[0]} />
</div>
<div className={(winner === 2) ? 'winner' : ''}>
<Hand cards={playerHands[1]} />
</div>
</div>
);
};

Round.propTypes = {
cards: propTypes.string.isRequired,
incrementScore: propTypes.func.isRequired,
};

const mapStateToProps = state => ({});

const mapDispatchToProps = dispatch => ({
incrementScore: playerName => {
dispatch({
type: 'INCREMENT_SCORE',
payload: {
name: playerName,
},
});
},
});

export const splitCards = cards => {
const playerHands = ;
playerHands.push(cards.substr(0, Math.ceil(cards.length / 2) - 1));
playerHands.push(cards.substr(Math.ceil(cards.length / 2)));
return playerHands;
};

export const determineWinner = (player1Cards, player2Cards) => {
const p1HandValue = calculateHandValue(player1Cards.split(' '));
const p2HandValue = calculateHandValue(player2Cards.split(' '));

let winner = compareFirstTuples(p1HandValue, p2HandValue);

if (winner === 0) {
winner = compareSecondTuples(p1HandValue, p2HandValue);

if (winner === 0) {
throw Error('No winner could be determined');
}
}
return winner;
};

export const compareFirstTuples = (p1HandValue, p2HandValue) => {
if (p1HandValue[0] === p2HandValue[0]) { return 0; }
return (p1HandValue[0] > p2HandValue[0]) ? 1 : 2;
};


export const compareSecondTuples = (p1HandValue, p2HandValue) => {
const p1Tuple = p1HandValue[1];
const p2Tuple = p2HandValue[1];
let winner = 0;
const minLength = Math.min(p1Tuple.length, p2Tuple.length);
for (let i = 0; i < minLength; i += 1) {
if (p1Tuple[i] > p2Tuple[i]) {
winner = 1;
break;
}
if (p1Tuple[i] < p2Tuple[i]) {
winner = 2;
break;
}
}
return winner;
};

export default connect(mapStateToProps, mapDispatchToProps)(Round);


Game-Room



import React from 'react';
import propTypes from 'prop-types';
import Button from '@material-ui/core/Button';
import './Game-Room.scss';
import { connect } from 'react-redux';
import Game from '../Game/Game';
import WinnerDetails from '../Winner-Details/Winner-Details';


const possibleSuits = ['D', 'S', 'H', 'C'];
const possibleCourts = ['2', '3', '4', '5', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];

const GameRoom = ({ setMatches, matches, players, resetScore }) => {
const { player1, player2 } = players;
let winner = null;
if (player1.score > 0 || player1.score > 0) {
winner = (player1.score > player2.score)
? player1 : player2;
}

const readTextFile = event => {
const input = event.target;
const reader = new FileReader();
reader.onload = processGamesFile;
reader.onload = () => {
resetScore();
setMatches(processGamesFile(reader.result));
};
reader.readAsText(input.files[0]);
};

const processGamesFile = gamesFile => {
const retrievedMatches = gamesFile.split('n');
retrievedMatches.pop();
return retrievedMatches;
};

return (
<div className="game-room-root">
<Button variant="contained" color="primary" className="main-button left">
<input type="file" onChange={event => { readTextFile(event); }} />
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
resetScore();
setMatches(generateMatches(100));
}}
className="main-button right"
>
Generate Games
</Button>
<WinnerDetails winner={winner} />
<Game matches={matches} />
</div>
);
};

GameRoom.propTypes = {
setMatches: propTypes.func.isRequired,
resetScore: propTypes.func.isRequired,
players: propTypes.shape({}).isRequired,
matches: propTypes.arrayOf(propTypes.string).isRequired,
};

const mapStateToProps = state => ({
matches: state.gamesReducer,
players: state.playerReducer,
});

const mapDispatchToProps = dispatch => ({
setMatches: games => {
dispatch({
type: 'SET_GAMES',
payload: games,
});
},
resetScore: () => {
dispatch({
type: 'RESET_SCORE',
});
},
});


export const generateMatches = numberOfGames => {
const games = ;
for (let i = 0; i < numberOfGames; i += 1) {
const round = generateRound();
games.push(round);
}
return games;
};

export const generateRound = () => {
let cards = '';
let numberOfCards = 0;

while (numberOfCards < 10) {
const card = createRandomCard();
if (cards.indexOf(card) === -1) {
cards += `${card} `;
numberOfCards += 1;
}
}

cards = cards.slice(0, -1);
return cards;
};

const createRandomCard = () => getRandomCourt() + getRandomSuit();

const getRandomSuit = () => possibleSuits[Math.floor(Math.random() * possibleSuits.length)];
const getRandomCourt = () => possibleCourts[Math.floor(Math.random() * possibleCourts.length)];


export default connect(mapStateToProps, mapDispatchToProps)(GameRoom);


Components Winner-detail and Game are emitted because they're small enough. Game is just a container for rounds.










share|improve this question
















bumped to the homepage by Community 15 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Please post a short problem description along with your question. Otherwise, it will be hard for us to judge your code properly with regards to correctness and suitability for the problem at hand, among other characteristics.
    – Ben Steffan
    Jul 29 at 23:34













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I came across problem 54 on projecteuler.net and decided to give it a go using ReactJS. I have had Angular experience prior to this. I wrote a couple of fully tested components. I think that they only have one purpose each, but they're still very big. I did my best code-wise because it's also a project where I want to show off the things I learned from a programming book about clean code. Any feedback would be greatly appreciated. I'll start with the first component I created and end it with the last.



Playing card



import React from 'react';
import propTypes from 'prop-types';
import { isNumeric } from '../../Helper';
import './Playing-Card.scss';

const cardSuites = {
D: 'diamonds',
S: 'spades',
H: 'hearts',
C: 'clubs',
};

const cardCourts = {
K: 'king',
J: 'jack',
Q: 'queen',
A: 'ace',
T: '10',
};

const cardValues = {
ace: '14',
king: '13',
queen: '12',
jack: '11',
};

const PlayingCard = ({ cardSymbol }) => {
const url = convertCardSymbolToUrl(cardSymbol);
return <img alt="playing-card" className="playing-card" src={url} />;
};

PlayingCard.propTypes = { cardSymbol: propTypes.string.isRequired };

export const convertCardSymbolToUrl = cardSymbol => `/cards/${getCardCourt(cardSymbol)}_of_${getCardSuite(cardSymbol)}.svg`;

export const getCardSuite = cardSymbol => cardSuites[cardSymbol[1]];

export const getCardCourt = cardSymbol => {
const courtSymbol = cardSymbol[0];
if (isNumeric(courtSymbol)) {
return String(courtSymbol);
}
return String(cardCourts[courtSymbol]);
};

export const getCardValue = cardCourt => {
if (isNumeric(cardCourt)) {
return Number(cardCourt);
}
return Number(cardValues[cardCourt]);
};

export default PlayingCard;


Hand



import React from 'react';
import propTypes from 'prop-types';
import { generateUniqueKeysForItems } from '../../Helper';
import PlayingCard, { getCardCourt, getCardValue, getCardSuite } from '../Playing-Card/Playing-Card';

const Hand = ({ cards }) => {
let cardsArray = cards.split(' ');
cardsArray = generateUniqueKeysForItems(cardsArray);
return cardsArray.map(card => <PlayingCard key={card.id} cardSymbol={card.value} />);
};

Hand.propTypes = { cards: propTypes.string.isRequired };

export const calculateHandValue = cardsArray => {
let cardsFrequency = calculateCardsFrequency(cardsArray);
cardsFrequency = sortFrequencyDesc(cardsFrequency);
const individualCardValues = calculateIndividualCardValues(cardsFrequency);
let handValue = 9;
const cardsCombinations = [
hasRoyalFlush,
hasStraightFlush,
hasFourOfAKind,
hasFullHouse,
hasFlush,
hasStraight,
hasThreeOfAKind,
hasTwoPairs,
hasTwoOfAKind,
];

for (let i = 0; i < cardsCombinations.length; i += 1) {
const isCombinationValid = cardsCombinations[i];
if (isCombinationValid(cardsArray)) {
break;
}
handValue -= 1;
}

return [handValue, individualCardValues];
};

export const calculateIndividualCardValues = cardsFrequency =>
Array.from(cardsFrequency.keys()).map(key => getCardValue(key));


export const hasRoyalFlush = cardsArray => {
const cardCourtsRequired = ['10', 'jack', 'queen', 'king', 'ace'];
let qualifies = true;

if (!hasSameSuits(cardsArray)) {
return false;
}
cardsArray.forEach(card => {
const court = getCardCourt(card);
if (cardCourtsRequired.indexOf(court) === -1) {
qualifies = false;
}
});

return qualifies;
};

export const hasStraightFlush = cardsArray =>
hasSameSuits(cardsArray) && hasIncrementalCourts(cardsArray);

export const hasStraight = cardsArray => hasIncrementalCourts(cardsArray);
export const hasFlush = cardsArray => hasSameSuits(cardsArray);

export const hasFourOfAKind = cardsArray => hasXOfAKind(cardsArray, 4);
export const hasThreeOfAKind = cardsArray => hasXOfAKind(cardsArray, 3);
export const hasTwoOfAKind = cardsArray => hasXOfAKind(cardsArray, 2);

const hasXOfAKind = (cardsArray, x) => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(x) !== -1;
};

export const hasTwoPairs = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
let numberOfPairs = 0;
frequencyArray.forEach(frequencyNumber => {
if (frequencyNumber === 2) {
numberOfPairs += 1;
}
});
return numberOfPairs === 2;
};

export const hasFullHouse = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(3) !== -1 && frequencyArray.indexOf(2) !== -1;
};


export const hasSameSuits = cardsArray => {
const hasEqualSuits = (card1, card2) => getCardSuite(card1) === getCardSuite(card2);

for (let i = 1; i < cardsArray.length; i += 1) {
const card = cardsArray[i];
const previousCard = cardsArray[i - 1];

if (!hasEqualSuits(card, previousCard)) {
return false;
}
}
return true;
};

export const hasIncrementalCourts = cardsArray => {
const hasIncrementalCardValues = (card1, card2) =>
getCardValue(getCardCourt(card1)) + 1 === getCardValue(getCardCourt(card2));

const sortedCards = sortByCardValueAsc(cardsArray);
for (let i = 1; i < sortedCards.length; i += 1) {
const card = sortedCards[i];
const previousCard = sortedCards[i - 1];
if (!hasIncrementalCardValues(previousCard, card)) {
return false;
}
}
return true;
};

export const calculateCardsFrequency = cardsArray => {
const cardFrequency = new Map();
cardsArray.forEach(card => {
const court = String(getCardCourt(card));
if (cardFrequency.get(court) != null) {
cardFrequency.set(court, cardFrequency.get(court) + 1);
} else {
cardFrequency.set(court, 1);
}
});
return cardFrequency;
};

export const sortFrequencyDesc = cardsFrequency =>
new Map([...cardsFrequency.entries()].sort((a, b) => {
const aCardCourt = a[0];
const aFrequency = a[1];
const bCardCourt = b[0];
const bFrequency = b[1];

if (aFrequency < bFrequency) {
return 1;
}
if (aFrequency === bFrequency && getCardValue(aCardCourt) < getCardValue(bCardCourt)) {
return 1;
}
return -1;
}));

export const sortByCardValueAsc = cardsArray => {
cardsArray.sort((card1, card2) => {
const court1 = getCardCourt(card1);
const court2 = getCardCourt(card2);
if (getCardValue(court1) > getCardValue(court2)) {
return 1;
}
if (getCardValue(court1) < getCardValue(court2)) {
return -1;
}
return 0;
});
return cardsArray;
};


export default Hand;


Round



import React from 'react';
import propTypes from 'prop-types';
import { connect } from 'react-redux';
import Hand, { calculateHandValue } from '../Hand/Hand';
import './Round.scss';

const Round = ({ cards, incrementScore }) => {
const playerHands = splitCards(cards);
if (playerHands[0] === '') {
return <div />;
}
const winner = determineWinner(playerHands[0], playerHands[1]);
incrementScore(`player${winner}`);
return (
<div className="round-root">
<div className={(winner === 1) ? 'winner' : ''}>
<Hand cards={playerHands[0]} />
</div>
<div className={(winner === 2) ? 'winner' : ''}>
<Hand cards={playerHands[1]} />
</div>
</div>
);
};

Round.propTypes = {
cards: propTypes.string.isRequired,
incrementScore: propTypes.func.isRequired,
};

const mapStateToProps = state => ({});

const mapDispatchToProps = dispatch => ({
incrementScore: playerName => {
dispatch({
type: 'INCREMENT_SCORE',
payload: {
name: playerName,
},
});
},
});

export const splitCards = cards => {
const playerHands = ;
playerHands.push(cards.substr(0, Math.ceil(cards.length / 2) - 1));
playerHands.push(cards.substr(Math.ceil(cards.length / 2)));
return playerHands;
};

export const determineWinner = (player1Cards, player2Cards) => {
const p1HandValue = calculateHandValue(player1Cards.split(' '));
const p2HandValue = calculateHandValue(player2Cards.split(' '));

let winner = compareFirstTuples(p1HandValue, p2HandValue);

if (winner === 0) {
winner = compareSecondTuples(p1HandValue, p2HandValue);

if (winner === 0) {
throw Error('No winner could be determined');
}
}
return winner;
};

export const compareFirstTuples = (p1HandValue, p2HandValue) => {
if (p1HandValue[0] === p2HandValue[0]) { return 0; }
return (p1HandValue[0] > p2HandValue[0]) ? 1 : 2;
};


export const compareSecondTuples = (p1HandValue, p2HandValue) => {
const p1Tuple = p1HandValue[1];
const p2Tuple = p2HandValue[1];
let winner = 0;
const minLength = Math.min(p1Tuple.length, p2Tuple.length);
for (let i = 0; i < minLength; i += 1) {
if (p1Tuple[i] > p2Tuple[i]) {
winner = 1;
break;
}
if (p1Tuple[i] < p2Tuple[i]) {
winner = 2;
break;
}
}
return winner;
};

export default connect(mapStateToProps, mapDispatchToProps)(Round);


Game-Room



import React from 'react';
import propTypes from 'prop-types';
import Button from '@material-ui/core/Button';
import './Game-Room.scss';
import { connect } from 'react-redux';
import Game from '../Game/Game';
import WinnerDetails from '../Winner-Details/Winner-Details';


const possibleSuits = ['D', 'S', 'H', 'C'];
const possibleCourts = ['2', '3', '4', '5', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];

const GameRoom = ({ setMatches, matches, players, resetScore }) => {
const { player1, player2 } = players;
let winner = null;
if (player1.score > 0 || player1.score > 0) {
winner = (player1.score > player2.score)
? player1 : player2;
}

const readTextFile = event => {
const input = event.target;
const reader = new FileReader();
reader.onload = processGamesFile;
reader.onload = () => {
resetScore();
setMatches(processGamesFile(reader.result));
};
reader.readAsText(input.files[0]);
};

const processGamesFile = gamesFile => {
const retrievedMatches = gamesFile.split('n');
retrievedMatches.pop();
return retrievedMatches;
};

return (
<div className="game-room-root">
<Button variant="contained" color="primary" className="main-button left">
<input type="file" onChange={event => { readTextFile(event); }} />
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
resetScore();
setMatches(generateMatches(100));
}}
className="main-button right"
>
Generate Games
</Button>
<WinnerDetails winner={winner} />
<Game matches={matches} />
</div>
);
};

GameRoom.propTypes = {
setMatches: propTypes.func.isRequired,
resetScore: propTypes.func.isRequired,
players: propTypes.shape({}).isRequired,
matches: propTypes.arrayOf(propTypes.string).isRequired,
};

const mapStateToProps = state => ({
matches: state.gamesReducer,
players: state.playerReducer,
});

const mapDispatchToProps = dispatch => ({
setMatches: games => {
dispatch({
type: 'SET_GAMES',
payload: games,
});
},
resetScore: () => {
dispatch({
type: 'RESET_SCORE',
});
},
});


export const generateMatches = numberOfGames => {
const games = ;
for (let i = 0; i < numberOfGames; i += 1) {
const round = generateRound();
games.push(round);
}
return games;
};

export const generateRound = () => {
let cards = '';
let numberOfCards = 0;

while (numberOfCards < 10) {
const card = createRandomCard();
if (cards.indexOf(card) === -1) {
cards += `${card} `;
numberOfCards += 1;
}
}

cards = cards.slice(0, -1);
return cards;
};

const createRandomCard = () => getRandomCourt() + getRandomSuit();

const getRandomSuit = () => possibleSuits[Math.floor(Math.random() * possibleSuits.length)];
const getRandomCourt = () => possibleCourts[Math.floor(Math.random() * possibleCourts.length)];


export default connect(mapStateToProps, mapDispatchToProps)(GameRoom);


Components Winner-detail and Game are emitted because they're small enough. Game is just a container for rounds.










share|improve this question















I came across problem 54 on projecteuler.net and decided to give it a go using ReactJS. I have had Angular experience prior to this. I wrote a couple of fully tested components. I think that they only have one purpose each, but they're still very big. I did my best code-wise because it's also a project where I want to show off the things I learned from a programming book about clean code. Any feedback would be greatly appreciated. I'll start with the first component I created and end it with the last.



Playing card



import React from 'react';
import propTypes from 'prop-types';
import { isNumeric } from '../../Helper';
import './Playing-Card.scss';

const cardSuites = {
D: 'diamonds',
S: 'spades',
H: 'hearts',
C: 'clubs',
};

const cardCourts = {
K: 'king',
J: 'jack',
Q: 'queen',
A: 'ace',
T: '10',
};

const cardValues = {
ace: '14',
king: '13',
queen: '12',
jack: '11',
};

const PlayingCard = ({ cardSymbol }) => {
const url = convertCardSymbolToUrl(cardSymbol);
return <img alt="playing-card" className="playing-card" src={url} />;
};

PlayingCard.propTypes = { cardSymbol: propTypes.string.isRequired };

export const convertCardSymbolToUrl = cardSymbol => `/cards/${getCardCourt(cardSymbol)}_of_${getCardSuite(cardSymbol)}.svg`;

export const getCardSuite = cardSymbol => cardSuites[cardSymbol[1]];

export const getCardCourt = cardSymbol => {
const courtSymbol = cardSymbol[0];
if (isNumeric(courtSymbol)) {
return String(courtSymbol);
}
return String(cardCourts[courtSymbol]);
};

export const getCardValue = cardCourt => {
if (isNumeric(cardCourt)) {
return Number(cardCourt);
}
return Number(cardValues[cardCourt]);
};

export default PlayingCard;


Hand



import React from 'react';
import propTypes from 'prop-types';
import { generateUniqueKeysForItems } from '../../Helper';
import PlayingCard, { getCardCourt, getCardValue, getCardSuite } from '../Playing-Card/Playing-Card';

const Hand = ({ cards }) => {
let cardsArray = cards.split(' ');
cardsArray = generateUniqueKeysForItems(cardsArray);
return cardsArray.map(card => <PlayingCard key={card.id} cardSymbol={card.value} />);
};

Hand.propTypes = { cards: propTypes.string.isRequired };

export const calculateHandValue = cardsArray => {
let cardsFrequency = calculateCardsFrequency(cardsArray);
cardsFrequency = sortFrequencyDesc(cardsFrequency);
const individualCardValues = calculateIndividualCardValues(cardsFrequency);
let handValue = 9;
const cardsCombinations = [
hasRoyalFlush,
hasStraightFlush,
hasFourOfAKind,
hasFullHouse,
hasFlush,
hasStraight,
hasThreeOfAKind,
hasTwoPairs,
hasTwoOfAKind,
];

for (let i = 0; i < cardsCombinations.length; i += 1) {
const isCombinationValid = cardsCombinations[i];
if (isCombinationValid(cardsArray)) {
break;
}
handValue -= 1;
}

return [handValue, individualCardValues];
};

export const calculateIndividualCardValues = cardsFrequency =>
Array.from(cardsFrequency.keys()).map(key => getCardValue(key));


export const hasRoyalFlush = cardsArray => {
const cardCourtsRequired = ['10', 'jack', 'queen', 'king', 'ace'];
let qualifies = true;

if (!hasSameSuits(cardsArray)) {
return false;
}
cardsArray.forEach(card => {
const court = getCardCourt(card);
if (cardCourtsRequired.indexOf(court) === -1) {
qualifies = false;
}
});

return qualifies;
};

export const hasStraightFlush = cardsArray =>
hasSameSuits(cardsArray) && hasIncrementalCourts(cardsArray);

export const hasStraight = cardsArray => hasIncrementalCourts(cardsArray);
export const hasFlush = cardsArray => hasSameSuits(cardsArray);

export const hasFourOfAKind = cardsArray => hasXOfAKind(cardsArray, 4);
export const hasThreeOfAKind = cardsArray => hasXOfAKind(cardsArray, 3);
export const hasTwoOfAKind = cardsArray => hasXOfAKind(cardsArray, 2);

const hasXOfAKind = (cardsArray, x) => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(x) !== -1;
};

export const hasTwoPairs = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
let numberOfPairs = 0;
frequencyArray.forEach(frequencyNumber => {
if (frequencyNumber === 2) {
numberOfPairs += 1;
}
});
return numberOfPairs === 2;
};

export const hasFullHouse = cardsArray => {
const cardsFrequency = calculateCardsFrequency(cardsArray);
const frequencyArray = Array.from(cardsFrequency.values());
return frequencyArray.indexOf(3) !== -1 && frequencyArray.indexOf(2) !== -1;
};


export const hasSameSuits = cardsArray => {
const hasEqualSuits = (card1, card2) => getCardSuite(card1) === getCardSuite(card2);

for (let i = 1; i < cardsArray.length; i += 1) {
const card = cardsArray[i];
const previousCard = cardsArray[i - 1];

if (!hasEqualSuits(card, previousCard)) {
return false;
}
}
return true;
};

export const hasIncrementalCourts = cardsArray => {
const hasIncrementalCardValues = (card1, card2) =>
getCardValue(getCardCourt(card1)) + 1 === getCardValue(getCardCourt(card2));

const sortedCards = sortByCardValueAsc(cardsArray);
for (let i = 1; i < sortedCards.length; i += 1) {
const card = sortedCards[i];
const previousCard = sortedCards[i - 1];
if (!hasIncrementalCardValues(previousCard, card)) {
return false;
}
}
return true;
};

export const calculateCardsFrequency = cardsArray => {
const cardFrequency = new Map();
cardsArray.forEach(card => {
const court = String(getCardCourt(card));
if (cardFrequency.get(court) != null) {
cardFrequency.set(court, cardFrequency.get(court) + 1);
} else {
cardFrequency.set(court, 1);
}
});
return cardFrequency;
};

export const sortFrequencyDesc = cardsFrequency =>
new Map([...cardsFrequency.entries()].sort((a, b) => {
const aCardCourt = a[0];
const aFrequency = a[1];
const bCardCourt = b[0];
const bFrequency = b[1];

if (aFrequency < bFrequency) {
return 1;
}
if (aFrequency === bFrequency && getCardValue(aCardCourt) < getCardValue(bCardCourt)) {
return 1;
}
return -1;
}));

export const sortByCardValueAsc = cardsArray => {
cardsArray.sort((card1, card2) => {
const court1 = getCardCourt(card1);
const court2 = getCardCourt(card2);
if (getCardValue(court1) > getCardValue(court2)) {
return 1;
}
if (getCardValue(court1) < getCardValue(court2)) {
return -1;
}
return 0;
});
return cardsArray;
};


export default Hand;


Round



import React from 'react';
import propTypes from 'prop-types';
import { connect } from 'react-redux';
import Hand, { calculateHandValue } from '../Hand/Hand';
import './Round.scss';

const Round = ({ cards, incrementScore }) => {
const playerHands = splitCards(cards);
if (playerHands[0] === '') {
return <div />;
}
const winner = determineWinner(playerHands[0], playerHands[1]);
incrementScore(`player${winner}`);
return (
<div className="round-root">
<div className={(winner === 1) ? 'winner' : ''}>
<Hand cards={playerHands[0]} />
</div>
<div className={(winner === 2) ? 'winner' : ''}>
<Hand cards={playerHands[1]} />
</div>
</div>
);
};

Round.propTypes = {
cards: propTypes.string.isRequired,
incrementScore: propTypes.func.isRequired,
};

const mapStateToProps = state => ({});

const mapDispatchToProps = dispatch => ({
incrementScore: playerName => {
dispatch({
type: 'INCREMENT_SCORE',
payload: {
name: playerName,
},
});
},
});

export const splitCards = cards => {
const playerHands = ;
playerHands.push(cards.substr(0, Math.ceil(cards.length / 2) - 1));
playerHands.push(cards.substr(Math.ceil(cards.length / 2)));
return playerHands;
};

export const determineWinner = (player1Cards, player2Cards) => {
const p1HandValue = calculateHandValue(player1Cards.split(' '));
const p2HandValue = calculateHandValue(player2Cards.split(' '));

let winner = compareFirstTuples(p1HandValue, p2HandValue);

if (winner === 0) {
winner = compareSecondTuples(p1HandValue, p2HandValue);

if (winner === 0) {
throw Error('No winner could be determined');
}
}
return winner;
};

export const compareFirstTuples = (p1HandValue, p2HandValue) => {
if (p1HandValue[0] === p2HandValue[0]) { return 0; }
return (p1HandValue[0] > p2HandValue[0]) ? 1 : 2;
};


export const compareSecondTuples = (p1HandValue, p2HandValue) => {
const p1Tuple = p1HandValue[1];
const p2Tuple = p2HandValue[1];
let winner = 0;
const minLength = Math.min(p1Tuple.length, p2Tuple.length);
for (let i = 0; i < minLength; i += 1) {
if (p1Tuple[i] > p2Tuple[i]) {
winner = 1;
break;
}
if (p1Tuple[i] < p2Tuple[i]) {
winner = 2;
break;
}
}
return winner;
};

export default connect(mapStateToProps, mapDispatchToProps)(Round);


Game-Room



import React from 'react';
import propTypes from 'prop-types';
import Button from '@material-ui/core/Button';
import './Game-Room.scss';
import { connect } from 'react-redux';
import Game from '../Game/Game';
import WinnerDetails from '../Winner-Details/Winner-Details';


const possibleSuits = ['D', 'S', 'H', 'C'];
const possibleCourts = ['2', '3', '4', '5', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];

const GameRoom = ({ setMatches, matches, players, resetScore }) => {
const { player1, player2 } = players;
let winner = null;
if (player1.score > 0 || player1.score > 0) {
winner = (player1.score > player2.score)
? player1 : player2;
}

const readTextFile = event => {
const input = event.target;
const reader = new FileReader();
reader.onload = processGamesFile;
reader.onload = () => {
resetScore();
setMatches(processGamesFile(reader.result));
};
reader.readAsText(input.files[0]);
};

const processGamesFile = gamesFile => {
const retrievedMatches = gamesFile.split('n');
retrievedMatches.pop();
return retrievedMatches;
};

return (
<div className="game-room-root">
<Button variant="contained" color="primary" className="main-button left">
<input type="file" onChange={event => { readTextFile(event); }} />
</Button>
<Button
variant="contained"
color="primary"
onClick={() => {
resetScore();
setMatches(generateMatches(100));
}}
className="main-button right"
>
Generate Games
</Button>
<WinnerDetails winner={winner} />
<Game matches={matches} />
</div>
);
};

GameRoom.propTypes = {
setMatches: propTypes.func.isRequired,
resetScore: propTypes.func.isRequired,
players: propTypes.shape({}).isRequired,
matches: propTypes.arrayOf(propTypes.string).isRequired,
};

const mapStateToProps = state => ({
matches: state.gamesReducer,
players: state.playerReducer,
});

const mapDispatchToProps = dispatch => ({
setMatches: games => {
dispatch({
type: 'SET_GAMES',
payload: games,
});
},
resetScore: () => {
dispatch({
type: 'RESET_SCORE',
});
},
});


export const generateMatches = numberOfGames => {
const games = ;
for (let i = 0; i < numberOfGames; i += 1) {
const round = generateRound();
games.push(round);
}
return games;
};

export const generateRound = () => {
let cards = '';
let numberOfCards = 0;

while (numberOfCards < 10) {
const card = createRandomCard();
if (cards.indexOf(card) === -1) {
cards += `${card} `;
numberOfCards += 1;
}
}

cards = cards.slice(0, -1);
return cards;
};

const createRandomCard = () => getRandomCourt() + getRandomSuit();

const getRandomSuit = () => possibleSuits[Math.floor(Math.random() * possibleSuits.length)];
const getRandomCourt = () => possibleCourts[Math.floor(Math.random() * possibleCourts.length)];


export default connect(mapStateToProps, mapDispatchToProps)(GameRoom);


Components Winner-detail and Game are emitted because they're small enough. Game is just a container for rounds.







javascript algorithm playing-cards react.js redux






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 30 at 2:56









Jamal

30.2k11115226




30.2k11115226










asked Jul 29 at 21:54









P_Andre

101




101





bumped to the homepage by Community 15 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 15 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • Please post a short problem description along with your question. Otherwise, it will be hard for us to judge your code properly with regards to correctness and suitability for the problem at hand, among other characteristics.
    – Ben Steffan
    Jul 29 at 23:34


















  • Please post a short problem description along with your question. Otherwise, it will be hard for us to judge your code properly with regards to correctness and suitability for the problem at hand, among other characteristics.
    – Ben Steffan
    Jul 29 at 23:34
















Please post a short problem description along with your question. Otherwise, it will be hard for us to judge your code properly with regards to correctness and suitability for the problem at hand, among other characteristics.
– Ben Steffan
Jul 29 at 23:34




Please post a short problem description along with your question. Otherwise, it will be hard for us to judge your code properly with regards to correctness and suitability for the problem at hand, among other characteristics.
– Ben Steffan
Jul 29 at 23:34










1 Answer
1






active

oldest

votes

















up vote
0
down vote













I agree that specific questions we can answer would be helpful. That being said, a few comments:




  • Some would disagree, but I'd separate the View/display code from the game code (model). You can build out all the mechanics of cards (GameRoom, Hand, PlayingCard, etc.) without any React code.
    There is a separation between these and it will keep things simpler. That way you can script tests that ensure all your game code is correct. Then, you can put them into React components and there will be very little logic needed within the React components.


  • It seems a little odd that determineWinner throws an exception. I would probably just have it return null.


  • It can also be simplified because 0 is falsey:



return compareFirstTuples(p1HandValue, p2HandValue) ||
compareSecondTuples(p1HandValue, p2HandValue) ||
null




  • Some of the renaming of things throughout the code is a little hard to follow. I think individualCardValues becomes p1HandValue[1] and then p1Tuple. I'm not sure the abstraction to tuples really helps the code, since the code is very specific about what it's manipulating. They could just stay as "card values".






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200555%2fplaying-cards-with-reactjs%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I agree that specific questions we can answer would be helpful. That being said, a few comments:




    • Some would disagree, but I'd separate the View/display code from the game code (model). You can build out all the mechanics of cards (GameRoom, Hand, PlayingCard, etc.) without any React code.
      There is a separation between these and it will keep things simpler. That way you can script tests that ensure all your game code is correct. Then, you can put them into React components and there will be very little logic needed within the React components.


    • It seems a little odd that determineWinner throws an exception. I would probably just have it return null.


    • It can also be simplified because 0 is falsey:



    return compareFirstTuples(p1HandValue, p2HandValue) ||
    compareSecondTuples(p1HandValue, p2HandValue) ||
    null




    • Some of the renaming of things throughout the code is a little hard to follow. I think individualCardValues becomes p1HandValue[1] and then p1Tuple. I'm not sure the abstraction to tuples really helps the code, since the code is very specific about what it's manipulating. They could just stay as "card values".






    share|improve this answer

























      up vote
      0
      down vote













      I agree that specific questions we can answer would be helpful. That being said, a few comments:




      • Some would disagree, but I'd separate the View/display code from the game code (model). You can build out all the mechanics of cards (GameRoom, Hand, PlayingCard, etc.) without any React code.
        There is a separation between these and it will keep things simpler. That way you can script tests that ensure all your game code is correct. Then, you can put them into React components and there will be very little logic needed within the React components.


      • It seems a little odd that determineWinner throws an exception. I would probably just have it return null.


      • It can also be simplified because 0 is falsey:



      return compareFirstTuples(p1HandValue, p2HandValue) ||
      compareSecondTuples(p1HandValue, p2HandValue) ||
      null




      • Some of the renaming of things throughout the code is a little hard to follow. I think individualCardValues becomes p1HandValue[1] and then p1Tuple. I'm not sure the abstraction to tuples really helps the code, since the code is very specific about what it's manipulating. They could just stay as "card values".






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I agree that specific questions we can answer would be helpful. That being said, a few comments:




        • Some would disagree, but I'd separate the View/display code from the game code (model). You can build out all the mechanics of cards (GameRoom, Hand, PlayingCard, etc.) without any React code.
          There is a separation between these and it will keep things simpler. That way you can script tests that ensure all your game code is correct. Then, you can put them into React components and there will be very little logic needed within the React components.


        • It seems a little odd that determineWinner throws an exception. I would probably just have it return null.


        • It can also be simplified because 0 is falsey:



        return compareFirstTuples(p1HandValue, p2HandValue) ||
        compareSecondTuples(p1HandValue, p2HandValue) ||
        null




        • Some of the renaming of things throughout the code is a little hard to follow. I think individualCardValues becomes p1HandValue[1] and then p1Tuple. I'm not sure the abstraction to tuples really helps the code, since the code is very specific about what it's manipulating. They could just stay as "card values".






        share|improve this answer












        I agree that specific questions we can answer would be helpful. That being said, a few comments:




        • Some would disagree, but I'd separate the View/display code from the game code (model). You can build out all the mechanics of cards (GameRoom, Hand, PlayingCard, etc.) without any React code.
          There is a separation between these and it will keep things simpler. That way you can script tests that ensure all your game code is correct. Then, you can put them into React components and there will be very little logic needed within the React components.


        • It seems a little odd that determineWinner throws an exception. I would probably just have it return null.


        • It can also be simplified because 0 is falsey:



        return compareFirstTuples(p1HandValue, p2HandValue) ||
        compareSecondTuples(p1HandValue, p2HandValue) ||
        null




        • Some of the renaming of things throughout the code is a little hard to follow. I think individualCardValues becomes p1HandValue[1] and then p1Tuple. I'm not sure the abstraction to tuples really helps the code, since the code is very specific about what it's manipulating. They could just stay as "card values".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 13 at 4:54









        ndp

        1,08676




        1,08676






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200555%2fplaying-cards-with-reactjs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Quarter-circle Tiles

            build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

            Mont Emei