Created
April 2, 2023 02:33
-
-
Save briantopping/c1b0dffafa5a8c0e2d5e6ed6a0fac865 to your computer and use it in GitHub Desktop.
ChatGPT codes Uno!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define card class | |
class Card { | |
constructor(color, value) { | |
this.color = color; | |
this.value = value; | |
} | |
toString() { | |
return `${this.color}_${this.value}`; | |
} | |
} | |
// Define deck class | |
class Deck { | |
constructor() { | |
this.cards = []; | |
const colors = ['red', 'blue', 'green', 'yellow']; | |
const values = [ | |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | |
'draw_two', 'skip', 'reverse' | |
]; | |
for (const color of colors) { | |
for (const value of values) { | |
this.cards.push(new Card(color, value)); | |
if (value !== '0') { | |
this.cards.push(new Card(color, value)); | |
} | |
} | |
} | |
for (let i = 0; i < 4; i++) { | |
this.cards.push(new Card('wild', 'wild')); | |
this.cards.push(new Card('wild', 'wild_draw_four')); | |
} | |
} | |
shuffle() { | |
for (let i = this.cards.length - 1; i > 0; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]]; | |
} | |
} | |
draw() { | |
return this.cards.shift(); | |
} | |
toString() { | |
return this.cards.join(','); | |
} | |
} | |
// Define player class | |
class Player { | |
constructor(name, isAI) { | |
this.name = name; | |
this.isAI = isAI; | |
this.hand = []; | |
} | |
drawCard(card) { | |
this.hand.push(card); | |
} | |
playCard(color, value) { | |
let playableCards = this.hand.filter(card => card.color === color || card.value === value || card.color === 'wild'); | |
if (playableCards.length === 0) { | |
return null; | |
} | |
if (this.isAI) { | |
return playableCards[Math.floor(Math.random() * playableCards.length)]; | |
} else { | |
console.log(`Your hand: ${this.hand.join(', ')}`); | |
console.log(`Playable cards: ${playableCards.join(', ')}`); | |
const input = prompt('Enter the number of the card you want to play, or enter 0 to draw a card:'); | |
if (input === '0') { | |
return null; | |
} | |
const index = parseInt(input) - 1; | |
if (isNaN(index) || index < 0 || index >= this.hand.length) { | |
console.log('Invalid input.'); | |
return this.playCard(color, value); | |
} | |
const card = this.hand[index]; | |
if (card.color !== 'wild' && card.color !== color && card.value !== value) { | |
console.log('Invalid card.'); | |
return this.playCard(color, value); | |
} | |
this.hand.splice(index, 1); | |
return card; | |
} | |
} | |
toString() { | |
return this.name; | |
} | |
} | |
// Define game class | |
class Game { | |
constructor(players) { | |
this.players = players; | |
this.deck = new Deck(); | |
this.deck.shuffle(); | |
this.currentCard = this.deck.draw(); | |
this.direction = 1; | |
this.currentPlayerIndex = 0; | |
console.log(`Starting game with ${this.players.length} players: ${this.players.join(', ')}`); | |
} | |
getCurrentPlayer() { | |
return this.players[this.currentPlayerIndex]; | |
} | |
drawCard() { | |
const card = this.deck.draw(); | |
console.log(`${this.getCurrentPlayer()}: Drew ${card}`); | |
return card; | |
} | |
playTurn() { | |
const currentPlayer = this.getCurrentPlayer(); | |
console.log(`Current card: ${ this.currentCard }`); | |
const playableCard = currentPlayer.playCard(this.currentCard.color, this.currentCard.value); | |
if (!playableCard) { | |
const drawnCard = this.drawCard(); | |
if (drawnCard.color === 'wild' && currentPlayer.isAI) { | |
drawnCard.color = ['red', 'blue', 'green', 'yellow'][Math.floor(Math.random() * 4)]; | |
} | |
if (drawnCard.color === 'wild_draw_four' && currentPlayer.isAI) { | |
drawnCard.color = ['red', 'blue', 'green', 'yellow'][Math.floor(Math.random() * 4)]; | |
console.log(`${currentPlayer}: Played ${drawnCard}`); | |
this.currentCard = drawnCard; | |
this.drawCard(); | |
this.drawCard(); | |
this.drawCard(); | |
this.nextPlayer(); | |
return; | |
} | |
currentPlayer.drawCard(drawnCard); | |
this.nextPlayer(); | |
return; | |
} | |
if (playableCard.color === 'wild') { | |
if (currentPlayer.isAI) { | |
playableCard.color = ['red', 'blue', 'green', 'yellow'][Math.floor(Math.random() * 4)]; | |
} else { | |
const input = prompt('Choose a color: red, blue, green, or yellow'); | |
if (!['red', 'blue', 'green', 'yellow'].includes(input)) { | |
console.log('Invalid input.'); | |
return this.playTurn(); | |
} | |
playableCard.color = input; | |
} | |
} | |
console.log(`${currentPlayer}: Played ${playableCard}`); | |
this.currentCard = playableCard; | |
if (playableCard.value === 'reverse') { | |
this.direction *= -1; | |
} | |
if (playableCard.value === 'skip') { | |
this.nextPlayer(); | |
} | |
if (playableCard.value === 'draw_two') { | |
this.drawCard(); | |
this.drawCard(); | |
this.nextPlayer(); | |
} | |
if (playableCard.value === 'wild_draw_four') { | |
if (!currentPlayer.isAI) { | |
const input = prompt('Choose a color: red, blue, green, or yellow'); | |
if (!['red', 'blue', 'green', 'yellow'].includes(input)) { | |
console.log('Invalid input.'); | |
return this.playTurn(); | |
} | |
this.currentCard.color = input; | |
} | |
this.drawCard(); | |
this.drawCard(); | |
this.drawCard(); | |
this.drawCard(); | |
this.nextPlayer(); | |
} | |
this.nextPlayer(); | |
} | |
nextPlayer() { | |
this.currentPlayerIndex = (this.currentPlayerIndex + this.players.length + this.direction) % this.players.length; | |
} | |
play() { | |
while (true) { | |
const currentPlayer = this.getCurrentPlayer(); | |
console.log(`${currentPlayer}'s turn:`); | |
this.playTurn(); | |
if (currentPlayer.hand.length === 0) { | |
console.log(`${ currentPlayer } wins!`); | |
break; | |
} | |
} | |
} | |
} | |
const readlineSync = require('readline-sync'); | |
function prompt(question) { | |
return readlineSync.question(question); | |
} | |
// Create players | |
const players = [ | |
new Player('Alice', false), | |
new Player('Bob', true), | |
new Player('Charlie', true) | |
]; | |
// Start game | |
const game = new Game(players); | |
game.play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment