Random Game from RAWG

I enjoy video games so naturally have a backlog to get through. I like to randomly pick a game and if it suits my mood, I’ll play it or get another random game. Unfortunately rawg.io doesn’t have this feature so I’ve made one.

I’ve done two options here and both depend on exporting my games list, as a CSV, from Rawg (within settings on rawg.io). Rawg does have an API and I may utilise this in future.

Option 1: Excel

I import the spreadsheet into the Owned tab of my Video Games spreadsheet and then if I refresh the sheet (by double-clicking into an unlocked cell), I get a new random title (D1:F1). The neat thing with this solution is I can filter the status so, for example, just show a random title from games ‘Not played’.

The formula to achieve this is: =INDEX(A4:A10000,RANDBETWEEN(1,COUNTA(A4:A10000)),1)

Option 2: Python Program

The other option (just like I like playing with Python) is the following little program, which again needs the CSV export from Rawg. Here’s the program in operation, with the code beneath it.

#!/bin/python3
import random
print("RetroJimmyX's Random Game Picker to pick a title from my collection.\n")

games_array = []
map_status = {"Currently playing":0,"Completed":1,"Played":2,"Not played":3,"Total":4}
games_stats = [0,0,0,0,0]

def loadgames():
  global games_array, map_status, games_stats
  gamesfile = open("games.csv","r")
  for line in gamesfile:
    games_array.append(line.replace("\"","").split(","))
  gamesfile.close()
  for game in games_array:
    if game[1] in map_status:
      games_stats[map_status[game[1]]] += 1
  games_stats[4] = len(games_array)
  print("=====\nSTATS\n=====")
  for i in range(len(map_status)):
    print(str(list(map_status)[i])+": "+str(games_stats[i]))

def menu_text():
  print('''\n====\nMENU\n====\nPlease make a choice below (or blank for 3):\n1 for titles that are 'Not played'\n2 for titles that I am 'Currently playing'
3 for titles that I have not 'Completed'\nx to exit program\n''')

def get_title(options):
  global games_array, map_status, games_stats
  if options == "1": game_selection = [game for game in games_array if game[1]=="Not played"]
  elif options == "2": game_selection = [game for game in games_array if game[1]=="Currently playing"]
  else: game_selection = [game for game in games_array if game[1]!="Completed"]
  generate = ""
  while generate != "m":
    num = random.randint(1,len(game_selection))
    length = max(len(game_selection[num][0]),len(game_selection[num][2]))
    print("\n"+"*"*length+"\n"+game_selection[num][0]+" ("+game_selection[num][1]+")\n"+game_selection[num][2]+"*"*length)
    generate = input("Press ENTER for another game or type m to return to menu:")
  return

def menu():
  choice = "a"
  while choice not in "123" or input != "":
    menu_text()
    choice = input("Enter Choice: ")
    if choice not in ["1","2","3","x",""]:
      print("Please make sure to choose 1-3 or x to exit.")
    elif choice == "x":
      print("Goodbye.")
      exit()
    else:
      get_title(choice)

loadgames()
menu()