1
0
mirror of https://github.com/mizanxali/uno-online synced 2024-11-05 02:35:26 +00:00

Added UNO button functionality and logic

This commit is contained in:
Mizanali Panjwani 2021-02-24 22:25:42 +05:30
parent 5340695785
commit eacd8e0001

View File

@ -57,6 +57,8 @@ const Game = (props) => {
const [playedCardsPile, setPlayedCardsPile] = useState([])
const [drawCardPile, setDrawCardPile] = useState([])
const [isUnoButtonPressed, setUnoButtonPressed] = useState(false)
//runs once on component mount
useEffect(() => {
//shuffle PACK_OF_CARDS array
@ -114,7 +116,8 @@ const Game = (props) => {
setDrawCardPile(drawCardPile)
})
socket.on('updateGameState', ({ gameOver, winner, turn, player1Deck, player2Deck, currentColor, currentNumber, playedCardsPile, drawCardPile }) => {
socket.on('updateGameState', ({ isUnoButtonPressed, gameOver, winner, turn, player1Deck, player2Deck, currentColor, currentNumber, playedCardsPile, drawCardPile }) => {
isUnoButtonPressed && setUnoButtonPressed(isUnoButtonPressed)
gameOver && setGameOver(gameOver)
winner && setWinner(winner)
turn && setTurn(turn)
@ -172,8 +175,38 @@ const Game = (props) => {
//remove the played card from player1's deck and add it to playedCardsPile (immutably)
//then update turn, currentColor and currentNumber
const removeIndex = player1Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1)
updatedPlayer1Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
turn: 'Player 2',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
currentColor: colorOfPlayedCard,
currentNumber: numberOfPlayedCard,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
turn: 'Player 2',
@ -183,10 +216,39 @@ const Game = (props) => {
currentNumber: numberOfPlayedCard
})
}
}
else {
//remove the played card from player2's deck and add it to playedCardsPile (immutably)
//then update turn, currentColor and currentNumber
const removeIndex = player2Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1)
updatedPlayer2Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
turn: 'Player 1',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
currentColor: colorOfPlayedCard,
currentNumber: numberOfPlayedCard,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
@ -199,6 +261,7 @@ const Game = (props) => {
})
}
}
}
//check for number match
else if(currentNumber === numberOfPlayedCard) {
console.log('numbers matched!')
@ -207,8 +270,38 @@ const Game = (props) => {
//remove the played card from player1's deck and add it to playedCardsPile (immutably)
//then update turn, currentColor and currentNumber
const removeIndex = player1Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1)
updatedPlayer1Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
turn: 'Player 2',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
currentColor: colorOfPlayedCard,
currentNumber: numberOfPlayedCard,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
turn: 'Player 2',
@ -218,10 +311,38 @@ const Game = (props) => {
currentNumber: numberOfPlayedCard
})
}
}
else {
//remove the played card from player2's deck and add it to playedCardsPile (immutably)
//then update turn, currentColor and currentNumber
const removeIndex = player2Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1)
updatedPlayer2Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
turn: 'Player 1',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
currentColor: colorOfPlayedCard,
currentNumber: numberOfPlayedCard,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
@ -234,6 +355,7 @@ const Game = (props) => {
})
}
}
}
//if no color or number match, invalid move - do not update state
else {
alert('Invalid Move!')
@ -252,8 +374,37 @@ const Game = (props) => {
//remove the played card from player1's deck and add it to playedCardsPile (immutably)
//then update currentColor and currentNumber
const removeIndex = player1Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1)
updatedPlayer1Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
currentColor: colorOfPlayedCard,
currentNumber: 404,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
@ -262,10 +413,38 @@ const Game = (props) => {
currentNumber: 404
})
}
}
else {
//remove the played card from player2's deck and add it to playedCardsPile (immutably)
//then update currentColor and currentNumber
const removeIndex = player2Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1)
updatedPlayer2Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
currentColor: colorOfPlayedCard,
currentNumber: 404,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
@ -277,6 +456,7 @@ const Game = (props) => {
})
}
}
}
//check for number match - if skip card played on skip card
else if(currentNumber === 404) {
console.log('Numbers matched!')
@ -285,8 +465,37 @@ const Game = (props) => {
//remove the played card from player1's deck and add it to playedCardsPile (immutably)
//then update currentColor and currentNumber - turn will remain same
const removeIndex = player1Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1)
updatedPlayer1Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
currentColor: colorOfPlayedCard,
currentNumber: 404,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
@ -295,10 +504,38 @@ const Game = (props) => {
currentNumber: 404
})
}
}
else {
//remove the played card from player2's deck and add it to playedCardsPile (immutably)
//then update currentColor and currentNumber - turn will remain same
const removeIndex = player2Deck.indexOf(played_card)
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1)
updatedPlayer2Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
currentColor: colorOfPlayedCard,
currentNumber: 404,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
@ -310,6 +547,7 @@ const Game = (props) => {
})
}
}
}
//if no color or number match, invalid move - do not update state
else {
alert('Invalid Move!')
@ -334,8 +572,36 @@ const Game = (props) => {
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//pull out last two elements from drawCardPile
const drawCard1X = copiedDrawCardPileArray.pop()
const drawCard2X = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1X)
updatedPlayer1Deck.push(drawCard2X)
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)],
currentColor: colorOfPlayedCard,
currentNumber: 252,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
@ -346,6 +612,7 @@ const Game = (props) => {
drawCardPile: [...copiedDrawCardPileArray]
})
}
}
else {
//remove the played card from player2's deck and add it to playedCardsPile (immutably)
//remove 2 new cards from drawCardPile and add them to player1's deck (immutably)
@ -356,9 +623,36 @@ const Game = (props) => {
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//pull out last two elements from drawCardPile
const drawCard1X = copiedDrawCardPileArray.pop()
const drawCard2X = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1X)
updatedPlayer2Deck.push(drawCard2X)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
winner: checkWinner(player2Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)],
currentColor: colorOfPlayedCard,
currentNumber: 252,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)],
player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)],
@ -368,6 +662,7 @@ const Game = (props) => {
})
}
}
}
//check for number match - if draw 2 card played on draw 2 card
else if(currentNumber === 252) {
console.log('number matched!')
@ -382,8 +677,36 @@ const Game = (props) => {
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//pull out last two elements from drawCardPile
const drawCard1X = copiedDrawCardPileArray.pop()
const drawCard2X = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1X)
updatedPlayer1Deck.push(drawCard2X)
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, ...player2Deck.slice(player2Deck.length)],
currentColor: colorOfPlayedCard,
currentNumber: 252,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
isUnoButtonPressed: false,
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
@ -394,6 +717,7 @@ const Game = (props) => {
drawCardPile: [...copiedDrawCardPileArray]
})
}
}
else {
//remove the played card from player2's deck and add it to playedCardsPile (immutably)
//remove 2 new cards from drawCardPile and add them to player1's deck (immutably)
@ -404,10 +728,36 @@ const Game = (props) => {
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//pull out last two elements from drawCardPile
const drawCard1X = copiedDrawCardPileArray.pop()
const drawCard2X = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1X)
updatedPlayer2Deck.push(drawCard2X)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
winner: checkWinner(player2Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)],
currentColor: colorOfPlayedCard,
currentNumber: 252,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)],
player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, ...player1Deck.slice(player1Deck.length)],
@ -417,6 +767,7 @@ const Game = (props) => {
})
}
}
}
//if no color or number match, invalid move - do not update state
else {
alert('Invalid Move!')
@ -432,6 +783,34 @@ const Game = (props) => {
//remove the played card from player1's deck and add it to playedCardsPile (immutably)
const removeIndex = player1Deck.indexOf(played_card)
//then update turn, currentColor and currentNumber
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1)
updatedPlayer1Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
turn: 'Player 2',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
currentColor: newColor,
currentNumber: 300,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player1Deck),
@ -443,22 +822,53 @@ const Game = (props) => {
currentNumber: 300
})
}
}
else {
//ask for new color
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)
const removeIndex = player2Deck.indexOf(played_card)
//then update turn, currentColor and currentNumber
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//make a copy of drawCardPile array
const copiedDrawCardPileArray = [...drawCardPile]
//pull out last two elements from it
const drawCard1 = copiedDrawCardPileArray.pop()
const drawCard2 = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1)
updatedPlayer2Deck.push(drawCard2)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
turn: 'Player 2',
turn: 'Player 1',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
currentColor: newColor,
currentNumber: 300,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
turn: 'Player 1',
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)],
currentColor: newColor,
currentNumber: 300
})
}
}
break;
}
//if card played was a draw four wild card
@ -478,6 +888,32 @@ const Game = (props) => {
const drawCard3 = copiedDrawCardPileArray.pop()
const drawCard4 = copiedDrawCardPileArray.pop()
//then update currentColor and currentNumber - turn will remain same
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player1Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//pull out last two elements from drawCardPile
const drawCard1X = copiedDrawCardPileArray.pop()
const drawCard2X = copiedDrawCardPileArray.pop()
const updatedPlayer1Deck = [...player1Deck.slice(0, removeIndex), ...player1Deck.slice(removeIndex + 1)]
updatedPlayer1Deck.push(drawCard1X)
updatedPlayer1Deck.push(drawCard2X)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player1Deck),
winner: checkWinner(player1Deck, 'Player 1'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player1Deck: [...updatedPlayer1Deck],
player2Deck: [...player2Deck.slice(0, player2Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player2Deck.slice(player2Deck.length)],
currentColor: newColor,
currentNumber: 600,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player1Deck),
@ -490,6 +926,7 @@ const Game = (props) => {
drawCardPile: [...copiedDrawCardPileArray]
})
}
}
else {
//ask for new color
const newColor = prompt('Enter first letter of new color in uppercase (R/G/B/Y)')
@ -515,6 +952,44 @@ const Game = (props) => {
currentNumber: 600,
drawCardPile: [...copiedDrawCardPileArray]
})
//if two cards remaining check if player pressed UNO button
//if not pressed add 2 cards as penalty
if(player2Deck.length===2 && !isUnoButtonPressed) {
alert('Oops! You forgot to press UNO')
//pull out last two elements from drawCardPile
const drawCard1X = copiedDrawCardPileArray.pop()
const drawCard2X = copiedDrawCardPileArray.pop()
const updatedPlayer2Deck = [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)]
updatedPlayer2Deck.push(drawCard1X)
updatedPlayer2Deck.push(drawCard2X)
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...updatedPlayer2Deck],
player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)],
currentColor: newColor,
currentNumber: 600,
drawCardPile: [...copiedDrawCardPileArray]
})
}
else {
//send new state to server
socket.emit('updateGameState', {
gameOver: checkGameOver(player2Deck),
winner: checkWinner(player2Deck, 'Player 2'),
playedCardsPile: [...playedCardsPile.slice(0, playedCardsPile.length), played_card, ...playedCardsPile.slice(playedCardsPile.length)],
player2Deck: [...player2Deck.slice(0, removeIndex), ...player2Deck.slice(removeIndex + 1)],
player1Deck: [...player1Deck.slice(0, player1Deck.length), drawCard1, drawCard2, drawCard3, drawCard4, ...player1Deck.slice(player1Deck.length)],
currentColor: newColor,
currentNumber: 600,
drawCardPile: [...copiedDrawCardPileArray]
})
}
}
}
break;
@ -753,6 +1228,7 @@ const Game = (props) => {
src={require(`../assets/cards-front/${playedCardsPile[playedCardsPile.length-1]}.png`).default}
/> }
<button disabled={turn !== 'Player 1'} onClick={onCardDrawnHandler}>DRAW CARD</button>
<button onClick={() => setUnoButtonPressed(!isUnoButtonPressed)}>UNO</button>
</div>
<br />
<div className='player2Deck' style={{pointerEvents: 'none'}}>
@ -786,6 +1262,7 @@ const Game = (props) => {
src={require(`../assets/cards-front/${playedCardsPile[playedCardsPile.length-1]}.png`).default}
/> }
<button disabled={turn !== 'Player 2'} onClick={onCardDrawnHandler}>DRAW CARD</button>
<button onClick={() => setUnoButtonPressed(!isUnoButtonPressed)}>UNO</button>
</div>
<br />
<div className='player2Deck' style={turn === 'Player 1' ? {pointerEvents: 'none'} : null}>