1
0
mirror of https://github.com/mizanxali/uno-online synced 2024-11-17 08:14:55 +00:00

Added game over and winner functionality

This commit is contained in:
Mizanali Panjwani 2021-02-14 20:31:42 +05:30
parent 5a78088c6e
commit 6ca3a90a15
2 changed files with 48 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import './Game.css'
const Game = (props) => { const Game = (props) => {
const player1active = props.turn === 'Player 1' const player1active = props.turn === 'Player 1'
return ( return (
props.gameOver ? <div><h1>GAME OVER</h1><a href='/'>Home</a></div> : props.gameOver ? <div><h1>GAME FORFEITED</h1>{props.winner !== '' && <><h1>GAME OVER</h1><h2>{props.winner} wins!</h2></>}<a href='/'>Home</a></div> :
<div className='Game'> <div className='Game'>
<h1>Turn: {props.turn}</h1> <h1>Turn: {props.turn}</h1>
<div className='player1Deck' style={player1active ? null : {pointerEvents: 'none'}}> <div className='player1Deck' style={player1active ? null : {pointerEvents: 'none'}}>
@ -42,6 +42,7 @@ const Game = (props) => {
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
gameOver: state.gameOver, gameOver: state.gameOver,
winner: state.winner,
turn: state.turn, turn: state.turn,
player1Deck: state.player1Deck, player1Deck: state.player1Deck,
player2Deck: state.player2Deck, player2Deck: state.player2Deck,

View File

@ -1,6 +1,6 @@
import { CARD_DRAWN, CARD_PLAYED, START_GAME } from "./actions"; import { CARD_DRAWN, CARD_PLAYED, START_GAME } from "./actions";
//pack of 108 cards //pack of 108 cards (# = reverse)
const CARDS = [ const CARDS = [
'0R', '1R', '1R', '2R', '2R', '3R', '3R', '4R', '4R', '5R', '5R', '6R', '6R', '7R', '7R', '8R', '8R', '9R', '9R', 'skipR', 'skipR', '#R', '#R', 'D2R', 'D2R', '0R', '1R', '1R', '2R', '2R', '3R', '3R', '4R', '4R', '5R', '5R', '6R', '6R', '7R', '7R', '8R', '8R', '9R', '9R', 'skipR', 'skipR', '#R', '#R', 'D2R', 'D2R',
'0G', '1G', '1G', '2G', '2G', '3G', '3G', '4G', '4G', '5G', '5G', '6G', '6G', '7G', '7G', '8G', '8G', '9G', '9G', 'skipG', 'skipG', '#G', '#G', 'D2G', 'D2G', '0G', '1G', '1G', '2G', '2G', '3G', '3G', '4G', '4G', '5G', '5G', '6G', '6G', '7G', '7G', '8G', '8G', '9G', '9G', 'skipG', 'skipG', '#G', '#G', 'D2G', 'D2G',
@ -10,7 +10,6 @@ const CARDS = [
] ]
//NUMBER CODES FOR ACTION CARDS //NUMBER CODES FOR ACTION CARDS
//REVERSE - #
//SKIP - 404 //SKIP - 404
//DRAW 2 - 252 //DRAW 2 - 252
//WILD - 300 //WILD - 300
@ -18,6 +17,7 @@ const CARDS = [
const initialState = { const initialState = {
gameOver: true, gameOver: true,
winner: '',
turn: '', turn: '',
player1Deck: [], player1Deck: [],
player2Deck: [], player2Deck: [],
@ -27,6 +27,14 @@ const initialState = {
drawCardPile: [] drawCardPile: []
} }
const checkGameOver = (arr) => {
return arr.length === 1
}
const setWinner = (arr, player) => {
return arr.length === 1 ? player : ''
}
const reducer = (state = initialState, action) => { const reducer = (state = initialState, action) => {
switch(action.type) { switch(action.type) {
case START_GAME: { case START_GAME: {
@ -106,6 +114,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
turn: 'Player 2', turn: 'Player 2',
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
@ -121,6 +131,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 2'),
turn: 'Player 1', turn: 'Player 1',
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
@ -142,6 +154,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
turn: 'Player 2', turn: 'Player 2',
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
@ -157,6 +171,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 2'),
turn: 'Player 1', turn: 'Player 1',
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
@ -190,6 +206,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
currentColor: colorOfPlayedCard, currentColor: colorOfPlayedCard,
@ -204,6 +222,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 2'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
currentColor: colorOfPlayedCard, currentColor: colorOfPlayedCard,
@ -225,6 +245,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
currentColor: colorOfPlayedCard, currentColor: colorOfPlayedCard,
@ -239,6 +261,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 2'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
currentColor: colorOfPlayedCard, currentColor: colorOfPlayedCard,
@ -277,6 +301,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
player2Deck: [...state.player2Deck.slice(0, state.player2Deck.length), drawCard1, drawCard2, ...state.player2Deck.slice(state.player2Deck.length)], player2Deck: [...state.player2Deck.slice(0, state.player2Deck.length), drawCard1, drawCard2, ...state.player2Deck.slice(state.player2Deck.length)],
@ -301,6 +327,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 2'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
player1Deck: [...state.player1Deck.slice(0, state.player1Deck.length), drawCard1, drawCard2, ...state.player1Deck.slice(state.player1Deck.length)], player1Deck: [...state.player1Deck.slice(0, state.player1Deck.length), drawCard1, drawCard2, ...state.player1Deck.slice(state.player1Deck.length)],
@ -331,6 +359,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
player2Deck: [...state.player2Deck.slice(0, state.player2Deck.length), drawCard1, drawCard2, ...state.player2Deck.slice(state.player2Deck.length)], player2Deck: [...state.player2Deck.slice(0, state.player2Deck.length), drawCard1, drawCard2, ...state.player2Deck.slice(state.player2Deck.length)],
@ -355,6 +385,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 2'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
player1Deck: [...state.player1Deck.slice(0, state.player1Deck.length), drawCard1, drawCard2, ...state.player1Deck.slice(state.player1Deck.length)], player1Deck: [...state.player1Deck.slice(0, state.player1Deck.length), drawCard1, drawCard2, ...state.player1Deck.slice(state.player1Deck.length)],
@ -376,13 +408,15 @@ const reducer = (state = initialState, action) => {
//check who played the card and return new state accordingly //check who played the card and return new state accordingly
if(cardPlayedBy === 'Player 1') { if(cardPlayedBy === 'Player 1') {
//ask for new color //ask for new color
const newColor = prompt('Enter new color: R / G / B / Y') const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)')
//remove the played card from player1's deck and add it to playedCardsPile (immutably) //remove the played card from player1's deck and add it to playedCardsPile (immutably)
const removeIndex = state.player1Deck.indexOf(action.payload.cardPlayed) const removeIndex = state.player1Deck.indexOf(action.payload.cardPlayed)
//then update turn, currentColor and currentNumber //then update turn, currentColor and currentNumber
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
turn: 'Player 2', turn: 'Player 2',
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
@ -393,7 +427,7 @@ const reducer = (state = initialState, action) => {
else { else {
//ask for new color //ask for new color
const newColor = prompt('Enter new color: R / G / B / Y') const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)')
//remove the played card from player2's deck and add it to playedCardsPile (immutably) //remove the played card from player2's deck and add it to playedCardsPile (immutably)
const removeIndex = state.player2Deck.indexOf(action.payload.cardPlayed) const removeIndex = state.player2Deck.indexOf(action.payload.cardPlayed)
@ -401,6 +435,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
turn: 'Player 1', turn: 'Player 1',
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
@ -415,7 +451,7 @@ const reducer = (state = initialState, action) => {
//check who played the card and return new state accordingly //check who played the card and return new state accordingly
if(cardPlayedBy === 'Player 1') { if(cardPlayedBy === 'Player 1') {
//ask for new color //ask for new color
const newColor = prompt('Enter new color: R / G / B / Y') const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)')
//remove the played card from player1's deck and add it to playedCardsPile (immutably) //remove the played card from player1's deck and add it to playedCardsPile (immutably)
const removeIndex = state.player1Deck.indexOf(action.payload.cardPlayed) const removeIndex = state.player1Deck.indexOf(action.payload.cardPlayed)
@ -432,6 +468,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player1Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)], player1Deck: [...state.player1Deck.slice(0, removeIndex), ...state.player1Deck.slice(removeIndex + 1)],
player2Deck: [...state.player2Deck.slice(0, state.player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...state.player2Deck.slice(state.player2Deck.length)], player2Deck: [...state.player2Deck.slice(0, state.player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...state.player2Deck.slice(state.player2Deck.length)],
@ -443,7 +481,7 @@ const reducer = (state = initialState, action) => {
else { else {
//ask for new color //ask for new color
const newColor = prompt('Enter new color: R / G / B / Y') const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)')
//remove the played card from player2's deck and add it to playedCardsPile (immutably) //remove the played card from player2's deck and add it to playedCardsPile (immutably)
const removeIndex = state.player2Deck.indexOf(action.payload.cardPlayed) const removeIndex = state.player2Deck.indexOf(action.payload.cardPlayed)
@ -460,6 +498,8 @@ const reducer = (state = initialState, action) => {
//return new state //return new state
return { return {
...state, ...state,
gameOver: checkGameOver(state.player2Deck),
winner: setWinner(state.player1Deck, 'Player 1'),
playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)], playedCardsPile: [...state.playedCardsPile.slice(0, state.playedCardsPile.length), action.payload.cardPlayed, ...state.playedCardsPile.slice(state.playedCardsPile.length)],
player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)], player2Deck: [...state.player2Deck.slice(0, removeIndex), ...state.player2Deck.slice(removeIndex + 1)],
player1Deck: [...state.player1Deck.slice(0, state.player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...state.player1Deck.slice(state.player1Deck.length)], player1Deck: [...state.player1Deck.slice(0, state.player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...state.player1Deck.slice(state.player1Deck.length)],