Gilded rose in Nim

- I've started with a small refactor, Command to function. This was done as
   the most obvious way to do it (to me) as in the original, would not compile
   in Nim due to violating memory lifetimes. I could have made it take a
   ref seq[Item], but then I would need to instatiate a ref seq and I didn't
   want to make a change that drastic. Also due to Nim's UFCS, the ultimate
   implementation would be basically the same anyway.
This commit is contained in:
Kieran committed 2020-10-16 18:59:43 +08:00
1 parent e2c5826e71
commit 908055ba3e
3 files changed
+173

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
import items
proc updateQuality*(items: var seq[Item]) =
for i in 0 ..< items.len:
if items[i].name != "Aged Brie" and items[i].name != "Backstage passes to a TAFKAL80ETC concert":
if items[i].quality > 0:
if items[i].name != "Sulfuras, Hand of Ragnaros":
items[i].quality = items[i].quality - 1
else:
if items[i].quality < 50:
items[i].quality = items[i].quality + 1
if items[i].name == "Backstage passes to a TAFKAL80ETC concert":
if items[i].sellIn < 11:
if items[i].quality < 50:
items[i].quality = items[i].quality + 1
if items[i].sellIn < 6:
if items[i].quality < 50:
items[i].quality = items[i].quality + 1
if items[i].name != "Sulfuras, Hand of Ragnaros":
items[i].sellIn = items[i].sellIn - 1
if items[i].sellIn < 0:
if items[i].name != "Aged Brie":
if items[i].name != "Backstage passes to a TAFKAL80ETC concert":
if items[i].quality > 0:
if items[i].name != "Sulfuras, Hand of Ragnaros":
items[i].quality = items[i].quality - 1
else:
items[i].quality = items[i].quality - items[i].quality
else:
if items[i].quality < 50:
items[i].quality = items[i].quality + 1
+12
View File
@@ -0,0 +1,12 @@
import strformat
type
Item* = object
name*: string
sellIn*, quality*: int
proc initItem*(name: string, sellIn, quality: int): Item =
Item(name: name, sellIn: sellIn, quality: quality)
proc `$`*(item: Item): string =
&"{item.name}, {item.sellIn}, {item.quality}"