Hi again! I’m back with another R project. This one quite literally made me pull my hair and teeth out, but I persevered and delivered! Today, let’s play a round of UNO!

While it’s not the most advanced or complete simulation of the card game, I experimented with a lot of functions and different sorts of commands. Some of which include playing around with R’s environment, loading CSV files (yes, I made my own UNO CSV file), and targeting rows or columns! For this UNO game, you are able to shuffle your deck, deal from the deck, deal a starting card for the game, and interact with the console to determine your next moves!

Just as a note before I begin my code breakdown, I tried–I really, really did–for an hour to make it so that a single code could run the entire game, but no matter what, nothing worked!! I’m either not skilled enough or R just hates me. I like the latter answer.

Also, P.P.S, I’m crediting Garrett Grolemund for creating his hands-on R programming course. That’s where I’m learning all of my foundational coding skills, and my project draws some code from his work. That’s all! Cue the suffering…I mean, game! :D

1) I started by uploading my CSV file which contained all of the data for the UNO cards (see my repository: https://github.com/Jeluu-R/JeluuCodes/blob/main/Uno%20Card%20Spreadsheet%20-%20Sheet1.csv), and I also included some basic objects.

unodeck <- read.csv("Uno Card Spreadsheet - Sheet1.csv")
head(unodeck)
Unodeck <- unodeck

Why did I make a second copy of unodeck? I think having an extra raw copy never hurt.

2) Got straight into designing my shuffle function!

shuffle <- function() {
  sample <- sample(1:nrow(Unodeck), size = 112)
  Unodeck[sample, ]
}
  
Unodeck2 <- shuffle()

I’m using the sample command to randomize all the cards from the 1st row, to the nth (or 112nd, since that’s the total) row. I’m then choosing 112 cards from 112 cards. The [] allows me to pick all the sampled rows and display them. The extra stuff near the end assigns the newly shuffled deck to Unodeck2.

3) Designing the dealing function, dealing 7 cards like in UNO!

dealing <- function () {
  Yourdeck <- Unodeck2[1:7, ]
  assign("Unodeck2", Unodeck2[-(1:7), ], envir = globalenv())
  Yourdeck
}

dealing()

I asked R to display the first 7 rows from the shuffled deck. Next, I experimented with R’s global environment, to replace the original Unodeck2 (which had all the cards) with a new Unodeck2 that was missing the first 7 cards. In a card game, when you deal 7 cards, those 7 cards don’t go back in the deck! It’s a similar logic, here. At the end, the function still displays the 7 cards dealt to the player.

4) When you play a card game, the card at the top of the deck needs to be placed to start the game! Here is the code:

startcard <- function () {
  Start <- Unodeck2[1, ]
  assign("Unodeck2", Unodeck2[-1, ], envir = globalenv())
  Start
}

start <- function() {
  print("This is the beginning...")
  startcard()
}

start()

The startcard function draws one card from the deck and rewrites the original object so that the chosen card is eliminated from the deck (same idea as above). The start function acts as a narrator and tells you this is the beginning…ominously :), while also performing what startcard() does. Why did I break the code up? There’s a reason you’ll soon see!

5) Once you have your hand, and you have a starting card, R wants to know if you can play a card!

Answer <- function () {
  answer <- readline(prompt = "Can you play any cards? (YES or NO): ")
  if (answer == "YES") {
      print("Play!")
   } else if (answer == "NO") {
      print("Draw a card")
      drawn <- startcard()
      print(drawn)
   } 
}

Answer()

This part of the code drove me INSANE. My console started repeating the readline prompt over and over, and it wouldn’t stop! Even when I started on a fresh R script! Sorry, you probably don’t care. I digress. So, this code will prompt a message in the console that you can answer, asking if you can play or not. The answer is case sensitive (and I’m too lazy to make it so it isn’t), but a YES would get you more encouragement to play, and a NO would tell R that you need another card. This is where the startcard() function comes back to haunt you. NOTE: in order to get the prompt to reappear, you need to type the function again. I know, it’s a big inconvenience, but I also tried really hard.

6) FINAL CODE! Now you input a bunch of code to start the game. This is also my proof of efficiency!

shuffle()
   Value          Colour
13              2 Yellow
83           Skip  Green
51              2 Yellow
25              4   Blue
32              1  Green
53              4 Yellow
29              8   Blue
34              3  Green
49              9    Red
24              3   Blue
90        Reverse   Blue
78           Skip    Red

This obviously isn’t the full return from the console, because that would be unnecessarily long

dealing()
   Value       Colour
97   Plus two   Blue
54          5 Yellow
95   Plus two Yellow
15          4 Yellow
101 Plus four  Black
49          9    Red
100  Plus two  Green

There’s your 7 cards!

start()
[1] "This is the beginning..."
   Value Colour
72     5  Green

The player: Wow, how mysterious. I like this console!

Answer()

Case 1 (console message):

Can you play any cards? (YES or NO): YES
[1] "Play!"

Case 2 (console messsage):

Can you play any cards? (YES or NO): NO
[1] "Draw a card"
    Value          Colour
108 Colour change  Black

ALRIGHT! That’s the end! I haven’t figured out how to upload my raw code to GitHub yet. And to be honest, I’m also a little lazy, tired, and still healing from R being horrible to me today. Next time, I’ll be a bit more proactive. Ciao! Jessica L