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

Student Showdown - Q and A battle game, part 1: The Champions

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

Problem

Just a self challenge/fun thing.

I have a scene where the player picks a character, of course it works but I'm repeating myself an awful lot. I would appreciate any formatting suggestions and making it more streamlined in general.

```
Champion ranger = new Champion.Generator(
"Legolas' Lover", // name
new Image("/Resources/Ranger.gif"), // icon
new Image("/Resources/Ranger.jpg")) // portrait
.attack(4)
.armor(2)
.health(8)
.description("Has less health than most, but decent armor, and strong attacks!")
.generate();

Champion dragon = new Champion.Generator(
"Scion of Daenerys", // name
new Image("/Resources/Dragon.gif"), // icon
new Image("/Resources/Dragon.jpg")) // portrait
.attack(4)
.armor(2)
.health(8)
.description("Is a motherloving dragon")
.generate();

Champion angel = new Champion.Generator(
"Full Metal Bitch", // name
new Image("/Resources/Angel.gif"), // icon
new Image("/Resources/Angel.jpg")) // portrait
.attack(4)
.armor(2)
.health(8)
.description("Really likes rock music, tons of defense")
.generate();

Champion spirit = new Champion.Generator(
"Forlorn Phantasm", // name
new Image("/Resources/Spirit.gif"), // icon
new Image("/Resources/Spirit.jpg")) // portrait
.attack(4)
.armor(2)
.health(8)
.description("So much health, find yourself spirited away~")
.generate();

Champion knight = new Champion.Generator(
"Dark Lancelot", // name
new Image("/Resources/Knight.gif"), // icon
new Image("/Resources/Knight.jpg")) // portrait
.attack(4)
.armor(2)
.health(8)
.description("He's f

Solution

A List, a list! My kingdom for a list!

List champions = new ArrayList<>();
champions.add(new Champion.Generator(
        "Legolas' Lover", // name
        new Image("/Resources/Ranger.gif"), // icon
        new Image("/Resources/Ranger.jpg")) // portrait
        .attack(4)
        .armor(2)
        .health(8)
        .description("Has less health than most, but decent armor, and strong attacks!")
    .generate());
...


  • Add all champions to a list



  • Loop through the list of champions and for each champion:



  • Create the button



  • Create the VBox



  • Add the VBox to the main layout



Additional observations

Currently all your champions are hard-coded into the code. I can highly recommend loading the champion data from a file.

Additionally, your 'icon' and 'portrait' are very similar, perhaps it is enough to specify "Ranger" instead of "/Resources/Ranger.gif" and "/Resources/Ranger.jpg" ?

In your code it seems to be "obvious" that a .gif is icon and .jpg is portrait. I would recommend naming them ranger_icon.gif and ranger_portrait.jpg instead though.

Code Snippets

List<Champion> champions = new ArrayList<>();
champions.add(new Champion.Generator(
        "Legolas' Lover", // name
        new Image("/Resources/Ranger.gif"), // icon
        new Image("/Resources/Ranger.jpg")) // portrait
        .attack(4)
        .armor(2)
        .health(8)
        .description("Has less health than most, but decent armor, and strong attacks!")
    .generate());
...

Context

StackExchange Code Review Q#87964, answer score: 10

Revisions (0)

No revisions yet.