Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use OpenAI to refactor, took multiple attempts #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 62 additions & 51 deletions GildedRose/GildedRose.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,71 @@
//

public class GildedRose {

public static func updateQuality(_ items: Array<Item>) -> [Item] {
var items = items
for i in 0 ..< items.count {
if items[i].name != "Aged Brie" && 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
}
}
var updatedItems = items
for i in 0..<updatedItems.count {
switch updatedItems[i].name {
case "Aged Brie":
updateQualityForAgedBrie(&updatedItems[i])
updateSellIn(&updatedItems[i])
case "Backstage passes to a TAFKAL80ETC concert":
updateQualityForBackstagePass(&updatedItems[i])
updateSellIn(&updatedItems[i])
case "Conjured Mana Cake":
updateQualityForConjured(&updatedItems[i])
updateSellIn(&updatedItems[i])
case "Sulfuras, Hand of Ragnaros":
break
default:
updateQualityForNormalItem(&updatedItems[i])
updateSellIn(&updatedItems[i])
}
}

return items
return updatedItems
}

private static func updateSellIn(_ item: inout Item) {
item.sellIn -= 1
}

private static func updateQualityForNormalItem(_ item: inout Item) {
if item.sellIn <= 0 {
item.quality -= 2
} else {
item.quality -= 1
}
item.quality = max(0, item.quality)
}

private static func updateQualityForAgedBrie(_ item: inout Item) {
if item.sellIn <= 0 {
item.quality += 2
} else {
item.quality += 1
}
item.quality = min(50, item.quality)
}

private static func updateQualityForBackstagePass(_ item: inout Item) {
if item.sellIn <= 0 {
item.quality = 0
} else if item.sellIn <= 5 {
item.quality += 3
} else if item.sellIn <= 10 {
item.quality += 2
} else {
item.quality += 1
}
item.quality = min(50, item.quality)
}

private static func updateQualityForConjured(_ item: inout Item) {
if item.sellIn < 0 {
item.quality -= 4
} else {
item.quality -= 2
}
item.quality = max(0, item.quality)
}
}