HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Generating random HearthStone cards: Preparing the training data

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomthecardspreparinggeneratinghearthstonetrainingdata

Problem

For a little toy project of mine I need to have data available in a certain format in order to be able to feed into a Recurrent Neural Network (Long short term memory), it uses HearthStone card data that is available in JSON format.

Example input:

{
    "id":"EX1_306",
    "name":"Succubus",
    "type":"Minion",
    "faction":"Neutral",
    "rarity":"Free",
    "cost":2,
    "attack":4,
    "health":3,
    "text":"Battlecry: Discard a random card.",
    "flavor":"Warlocks have it pretty good.",
    "artist":"Matt Dixon",
    "collectible":true,
    "race":"Demon",
    "playerClass":"Warlock",
    "howToGet":"Unlocked at Level 1.",
    "howToGetGold":"Unlocked at Level 40.",
    "mechanics":[
        "Battlecry"
    ]
}


Example output:

Basic | Spell | Common | Paladin | Blessing of Kings | 4 | Give a minion +4/+4. (+4 Attack/+4 Health)
Classic | Minion | Legendary | Neutral | Dragon | Onyxia | 9 | 8 | 8 | Battlecry: Summon 1/1 Whelps until your side of the battlefield is full.
Basic | Minion | Free | Neutral | Beast | River Crocolisk | 2 | 2 | 3 | 
Basic | Weapon | Free | Warrior | Fiery War Axe | 2 | 3 | 2 | 
Classic | Minion | Legendary | Neutral | Pirate | Captain Greenskin | 5 | 5 | 4 | Battlecry: Give your weapon +1/+1.
Classic | Minion | Legendary | Neutral | None | Tinkmaster Overspark | 3 | 3 | 3 | Battlecry: Transform another random minion into a 5/5 Devilsaur or a 1/1 Squirrel.
Goblins vs Gnomes | Minion | Common | Neutral | Mech | Flying Machine | 3 | 1 | 4 | Windfury


Please note that the example input describes only a single card, while the example output describes an unrelated set of cards.

I decided to implement this in Groovy, and am looking forward to receiving lots of feedback as this is the first standalone program I've written in Groovy.

```
import groovy.json.JsonSlurper

import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardOpenO

Solution

Looks pretty groovy to me. I only have some minor nitpicks.

Instead of def map = new HashMap(), you can write simpler as def map = [:]

Likewise, instead of def list = new ArrayList(), you can write def list = []

As of Groovy 1.8, the splitIntoSets method can be simplified with collectEntries:

static Map splitIntoSets(Object cardsJson) {
    cardsJson.collectEntries { it }
}

Code Snippets

static Map<String, List> splitIntoSets(Object cardsJson) {
    cardsJson.collectEntries { it }
}

Context

StackExchange Code Review Q#96812, answer score: 6

Revisions (0)

No revisions yet.