]> gitweb.factorcode.org Git - factor.git/blob - extra/gilded-rose/gilded-rose.factor
gilded-rose: new vocab
[factor.git] / extra / gilded-rose / gilded-rose.factor
1 ! Copyright (C) 2024 Alexander Ilin.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors command-line formatting io kernel make math
4 math.parser namespaces sequences ;
5 IN: gilded-rose
6
7 TUPLE: GildedRose
8     Items ;
9
10 TUPLE: Item
11     Name SellIn Quality ;
12
13 : Item.ToString ( item -- str )
14     [ Name>> ] [ SellIn>> ] [ Quality>> ] tri "%s %d %d" sprintf ;
15
16 :: GildedRose.UpdateQuality ( this -- )
17     this Items>> length [| i |
18         i this Items>> nth Name>> "Aged Brie" = not
19         i this Items>> nth Name>> "Backstage passes to a TAFKAL80ETC concert" = not
20         and [
21             i this Items>> nth Quality>> 0 > [
22                 i this Items>> nth Name>> "Sulfuras, Hand of Ragnaros" = not [
23                     i this Items>> nth Quality>> 1 -
24                     i this Items>> nth Quality<<
25                 ] when
26             ] when
27         ] [
28             i this Items>> nth Quality>> 50 < [
29                 i this Items>> nth Quality>> 1 +
30                 i this Items>> nth Quality<<
31                 i this Items>> nth Name>> "Backstage passes to a TAFKAL80ETC concert" = [
32                     i this Items>> nth SellIn>> 11 < [
33                         i this Items>> nth Quality>> 50 < [
34                             i this Items>> nth Quality>> 1 +
35                             i this Items>> nth Quality<<
36                         ] when
37                     ] when
38                     i this Items>> nth SellIn>> 6 < [
39                         i this Items>> nth Quality>> 50 < [
40                             i this Items>> nth Quality>> 1 +
41                             i this Items>> nth Quality<<
42                         ] when
43                     ] when
44                 ] when
45             ] when
46         ] if
47
48         i this Items>> nth Name>> "Sulfuras, Hand of Ragnaros" = not [
49             i this Items>> nth SellIn>> 1 -
50             i this Items>> nth SellIn<<
51         ] when
52
53         i this Items>> nth SellIn>> 0 < [
54             i this Items>> nth Name>> "Aged Brie" = not [
55                 i this Items>> nth Name>> "Backstage passes to a TAFKAL80ETC concert" = [
56                     i this Items>> nth Quality>> 0 > [
57                         i this Items>> nth Name>> "Sulfuras, Hand of Ragnaros" = not [
58                             i this Items>> nth Quality>> 1 -
59                             i this Items>> nth Quality<<
60                         ] when
61                     ] when
62                 ] [
63                     i this Items>> nth Quality>> i this Items>> nth Quality>> -
64                     i this Items>> nth Quality<<
65                 ] if
66             ] [
67                 i this Items>> nth Quality>> 50 < [
68                     i this Items>> nth Quality>> 1 +
69                     i this Items>> nth Quality<<
70                 ] when
71             ] if
72         ] when
73     ] each-integer ;
74
75 : main ( -- )
76     "OMGHAI!" print
77     [
78         "+5 Dexterity Vest" 10 20 Item boa ,
79         "Aged Brie" 2 0 Item boa ,
80         "Elixir of the Mongoose" 5 7 Item boa ,
81         "Sulfuras, Hand of Ragnaros" 0 80 Item boa ,
82         "Sulfuras, Hand of Ragnaros" -1 80 Item boa ,
83         "Backstage passes to a TAFKAL80ETC concert" 15 20 Item boa ,
84         "Backstage passes to a TAFKAL80ETC concert" 10 49 Item boa ,
85         "Backstage passes to a TAFKAL80ETC concert" 5 49 Item boa ,
86         ! This conjured item does not work properly yet.
87         "Conjured Mana Cake" 3 6 Item boa ,
88     ] { } make GildedRose boa
89
90     command-line get [ 2 ] [ first dec> ] if-empty [
91         "-------- day %d --------\n" printf
92         "name, sellIn, quality" print
93         dup Items>> [ Item.ToString print ] each
94         nl
95         GildedRose.UpdateQuality
96     ] with each-integer ;
97
98 MAIN: main