Last active
September 8, 2023 10:35
-
-
Save godrm/f835c2f682a70be68dfe50af535c882e to your computer and use it in GitHub Desktop.
사다라게임 recfatoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct HugeLadderGame { | |
struct LadderPlayer { | |
var name = "" | |
} | |
struct InputView { | |
static func readHeight() -> Int { | |
print("사다리 높이를 입력해주세요.") | |
let height = readLine() ?? "" | |
return Int(height) ?? 0 | |
} | |
static func readPlayerNames() -> [String] { | |
print("참여할 사람 이름을 입력하세요") | |
let players = readLine() ?? "" | |
return players.split(separator: ",").map{String($0)} | |
} | |
} | |
var height = 0 | |
var players = [LadderPlayer]() | |
var ladders = [[Int]]() | |
mutating func run() { | |
self.height = InputView.readHeight() | |
let names = InputView.readPlayerNames() | |
self.players = names.map({LadderPlayer(name:$0)}) | |
generateLadders() | |
printLadders() | |
} | |
mutating func generateLadders() { | |
for row in 0..<height { | |
ladders.append([Int]()) | |
for _ in 0..<players.count { | |
let hasStep = Int(arc4random_uniform(2)) | |
ladders[row].append(hasStep) | |
} | |
} | |
} | |
func printLadders() { | |
for row in ladders { | |
print("|", terminator:"") | |
for step in row { | |
if step==1 { | |
print("---", "|", separator:"", terminator:"") | |
} | |
else { | |
print(" ", "|", separator:"", terminator:"") | |
} | |
} | |
print() | |
} | |
} | |
} | |
var game = HugeLadderGame() | |
game.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum InputError : Error { | |
case emptyValue | |
case outOfRange | |
case notNumber | |
} | |
protocol RangeChecker { | |
func isAvailable(_ value : String) -> Bool | |
} | |
struct InputView { | |
static func ask(prompt: String) throws -> String { | |
print(prompt) | |
guard let result = readLine() else { | |
throw InputError.emptyValue | |
} | |
return result | |
} | |
static func readHeight(rangeChecker: RangeChecker) throws -> Int { | |
let height = try ask(prompt: "") | |
if rangeChecker.isAvailable(height) { | |
return Int(height) ?? 0 | |
} | |
throw InputError.outOfRange | |
} | |
static func readHeight() throws -> Int { | |
let height = try ask(prompt: "") | |
if let height = Int(height) { | |
return height | |
} | |
throw InputError.notNumber | |
} | |
static func readPlayers() throws -> String { | |
return try ask(prompt: "") | |
} | |
static func readPlayerNames() throws -> [String] { | |
let players = try ask(prompt: "") | |
// return players.split(separator: ",").compactMap{ String($0) } | |
return players.components(separatedBy: ",") | |
} | |
} | |
struct StringUtiltity { | |
static func commaSeparated(value: String) -> [String] { | |
return value.components(separatedBy: ",") | |
} | |
} | |
struct HugeLadderGame : OutputViewPrintable { | |
struct LadderPlayer { | |
var name = "" | |
} | |
private var height = 0 | |
private var players = [LadderPlayer]() | |
private var ladders = [[Int]]() | |
init(height: Int, names: [String]) { | |
self.height = height | |
self.players = names.map({LadderPlayer(name:$0)}) | |
} | |
mutating func run() { | |
generateLadders() | |
} | |
mutating func generateLadders() { | |
for row in 0..<height { | |
ladders.append([Int]()) | |
for _ in 0..<players.count { | |
let hasStep = Int(arc4random_uniform(2)) | |
ladders[row].append(hasStep) | |
} | |
} | |
} | |
func printLadder( handler: ([Int])->() ) { | |
for row in self.ladders { | |
handler(row) | |
} | |
} | |
} | |
protocol OutputViewPrintable { | |
func printLadder(handler: ([Int])->()) | |
} | |
struct OutputView { | |
static func printLadders(game: OutputViewPrintable) { //ladders : [[Int]]) { | |
game.printLadder(handler: | |
{ row in | |
print("|", terminator:"") | |
printLine(row) | |
print() | |
}) | |
} | |
static func printLine(_ row: [Int]) { | |
for step in row { | |
if step==1 { | |
print("---", "|", separator:"", terminator:"") | |
} | |
else { | |
print(" ", "|", separator:"", terminator:"") | |
} | |
} | |
} | |
} | |
func inputAll() -> (Int, [String]) { | |
//Input | |
defer { | |
} | |
do { | |
let height = try InputView.readHeight() | |
let players = try InputView.readPlayers() | |
let names = StringUtiltity.commaSeparated(value: players) | |
return (height: height, names: names) | |
} | |
catch { | |
} | |
return (height: 0, names: []) | |
} | |
func main() { | |
let height : Int | |
let names : [String] | |
do { | |
height = try InputView.readHeight() | |
let players = try InputView.readPlayers() | |
names = StringUtiltity.commaSeparated(value: players) | |
} | |
catch { | |
} | |
let parameter : (height: Int, names:[String]) = inputAll() | |
//Validation | |
//Process | |
var game = HugeLadderGame(height: parameter.height, names: parameter.names) | |
game.run() | |
//Formatting | |
//Output | |
OutputView.printLadders(game: game) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// HughLadderGame | |
// | |
// Created by JK on 2020/02/12. | |
// Copyright © 2020 codesquad. All rights reserved. | |
// | |
import Foundation | |
struct LadderPlayer { | |
private var name = "" | |
init(name: String) { | |
self.name = name | |
} | |
} | |
struct InputView { | |
static func read(with description:String) -> String { | |
print(description) | |
let line = readLine() ?? "" | |
return line | |
} | |
static func readNumber(with description:String) -> Int { | |
return Int(read(with: description)) ?? 0 | |
} | |
static func readHeight() -> Int { | |
return readNumber(with: "사다리 높이를 입력해주세요.") | |
} | |
static func readPlayerNames() -> String { | |
let players = read(with: "참여할 사람 이름을 입력하세요") | |
return players | |
} | |
} | |
struct Tokenizer { | |
static func players(players: String, delimeter: Character = ",") -> [String] { | |
return players.split(separator: delimeter).map{String($0)} | |
} | |
} | |
struct Step { | |
var hasFoothold : Bool = false | |
} | |
struct StepRow { | |
private var steps = [Step]() | |
init(players: [LadderPlayer]) { | |
for _ in 0..<players.count { | |
let hasStep = Int(arc4random_uniform(2)) | |
steps.append( Step(hasFoothold: hasStep == 1) ) | |
} | |
} | |
} | |
struct Ladders { | |
private var ladders = [StepRow]() | |
init(height: Int, players: [LadderPlayer]) { | |
for _ in 0..<height { | |
ladders.append( StepRow(players: players) ) | |
} | |
} | |
} | |
class AbstractGame { | |
var playTime : Int = 1 | |
func run() { | |
} | |
} | |
protocol Gameplayable { | |
var times : Int { get set } | |
func play() | |
} | |
class HugeLadderGame : AbstractGame { | |
private var height = 0 | |
private var players = [LadderPlayer]() | |
private var ladders : Ladders? | |
init(height: Int, names: [String]) { | |
self.height = height | |
self.players = names.map({LadderPlayer(name:$0)}) | |
} | |
func generateLadders() { | |
ladders = Ladders(height: height, players: players) | |
} | |
} | |
protocol OutputExplorerable { | |
func explore(handler: ([Int]) -> () ) | |
func explore(row: [Int], handler: (Int) -> () ) | |
} | |
extension HugeLadderGame: OutputExplorerable{ | |
func explore(handler: ([Int]) -> () ) { | |
// for row in ladders { | |
// handler(row) | |
// } | |
} | |
func explore(row: [Int], handler: (Int) -> () ) { | |
for step in row { | |
handler(step) | |
} | |
} | |
} | |
struct HugeLadderMock: OutputExplorerable{ | |
func explore(handler: ([Int]) -> () ) { | |
for row in 1...3 { | |
handler([row]) | |
} | |
} | |
func explore(row: [Int], handler: (Int) -> () ) { | |
for step in row { | |
handler(step) | |
} | |
} | |
} | |
struct OutputView { | |
static func printLadders(game: OutputExplorerable) { | |
game.explore(handler: { (row) in | |
print("|", terminator:"") | |
game.explore(row: row) { (step) in | |
if step==1 { | |
print("---", "|", separator:"", terminator:"") | |
} | |
else { | |
print(" ", "|", separator:"", terminator:"") | |
} | |
} | |
print() | |
}) | |
} | |
} | |
//main | |
let height = InputView.readHeight() | |
let players = InputView.readPlayerNames() | |
let names = Tokenizer.players(players: players) | |
var game = HugeLadderGame(height: height, names: names) | |
game.generateLadders() | |
OutputView.printLadders(game: HugeLadderMock()) | |
func testGame() { | |
let height = 100 | |
let names = [String]() | |
var game = HugeLadderGame(height: height, names: names) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// GreatLadderGame | |
// | |
// Created by JK on 2020/09/16. | |
// Copyright © 2020 codesquad. All rights reserved. | |
// | |
import Foundation | |
struct InputView { | |
static func read(prompt: String) -> String { | |
print(prompt) | |
return readLine() ?? "" | |
} | |
static func readHeight() -> Int { | |
let height = read(prompt: "사다리 높이를 입력해주세요.") | |
return Int(height) ?? 0 | |
} | |
static func readPlayerNames() -> String { | |
let players = read(prompt: "참여할 사람 이름을 입력하세요") | |
return players | |
} | |
} | |
struct StringExtension { | |
static func spliter(players: String) -> [String] { | |
return players.split(separator: ",").map{String($0)} | |
} | |
} | |
struct HugeLadderGame : OutputViewAdaptor { | |
func numberOfHeight() -> Int { | |
return height | |
} | |
func namesOfPlayers() -> [String] { | |
return players.map { $0.name } | |
} | |
func hasStep(row: Int, column: Int) -> Bool { | |
return ladders[row][column] == 1 | |
} | |
struct LadderPlayer { | |
var name = "" | |
} | |
private var height = 0 | |
private var players = [LadderPlayer]() | |
private var ladders = [[Int]]() | |
init(height: Int, names: [String]){ | |
self.height = height | |
self.players = names.map({LadderPlayer(name:$0)}) | |
} | |
mutating func generateLadders() -> [[Int]] { | |
for row in 0..<height { | |
ladders.append([Int]()) | |
for _ in 0..<players.count { | |
let hasStep = Int(arc4random_uniform(2)) | |
ladders[row].append(hasStep) | |
} | |
} | |
return ladders | |
} | |
// func makeResult() -> String { | |
// for row in ladders { | |
// print("|", terminator:"") | |
// for step in row { | |
// if step==1 { | |
// print("---", "|", separator:"", terminator:"") | |
// } | |
// else { | |
// print(" ", "|", separator:"", terminator:"") | |
// } | |
// } | |
// print() | |
// } | |
} | |
protocol OutputViewAdaptor { | |
func numberOfHeight() -> Int | |
func namesOfPlayers() -> [String] | |
func hasStep(row: Int, column: Int) -> Bool | |
} | |
struct OutputView { | |
private var adaptor : OutputViewAdaptor! | |
init(adaptor : OutputViewAdaptor) { | |
self.adaptor = adaptor | |
} | |
func printLadders() { | |
let names = adaptor.namesOfPlayers() | |
for rowIndex in 0..<adaptor.numberOfHeight() { | |
print("|", terminator:"") | |
for columnIndex in 0..<names.count { | |
let step = adaptor.hasStep(row: rowIndex, column: columnIndex) | |
if step { | |
print("---", "|", separator:"", terminator:"") | |
} | |
else { | |
print(" ", "|", separator:"", terminator:"") | |
} | |
} | |
print() | |
} | |
} | |
} | |
//MARK:- ViewController | |
let height = InputView.readHeight() | |
let name = InputView.readPlayerNames() | |
let names = StringExtension.spliter(players: name) | |
var game = HugeLadderGame(height: height, names: names) | |
let ladders = game.generateLadders() | |
let outputView = OutputView.init(adaptor: game) | |
outputView.printLadders() | |
struct MockGame : OutputViewAdaptor { | |
func numberOfHeight() -> Int { | |
return 1 | |
} | |
func namesOfPlayers() -> [String] { | |
return ["JK"] | |
} | |
func hasStep(row: Int, column: Int) -> Bool { | |
return false | |
} | |
} | |
let mock = MockGame() | |
let outputViewTest = OutputView.init(adaptor: mock) | |
outputViewTest.printLadders() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct InputView { | |
private static func readSomething(_ announcement : String) -> String { | |
print(announcement) | |
let some = readLine() ?? "" | |
return some | |
} | |
static func readHeight() -> Int { | |
let height = readSomething("사다리 높이를 입력해주세요.") | |
return Int(height) ?? 0 | |
} | |
static func readPlayerNames() -> String { | |
let players = readSomething("참여할 사람 이름을 입력하세요") | |
return players | |
} | |
} | |
struct LadderStep : CustomStringConvertible { | |
static func randomStep() -> Int { | |
return Int(arc4random_uniform(2)) | |
} | |
var description: String { | |
return step.rawValue | |
} | |
enum Ladder : String { | |
case on = "---" | |
case off = " " | |
case bar = "|" | |
} | |
private var step : Ladder = .off | |
init(_ hasStep : Int) { | |
if hasStep == 1 { | |
step = .on | |
} | |
else { | |
step = .off | |
} | |
} | |
} | |
struct OutputView { | |
fileprivate static func prinntLadder(_ row: [LadderStep]) { | |
row.forEach { | |
print( $0, LadderStep.Ladder.bar, separator:"", terminator:"") | |
} | |
} | |
static func printLadders(ladders : [[LadderStep]]) { | |
for row in ladders { | |
print(LadderStep.Ladder.bar, terminator:"") | |
prinntLadder(row) | |
print() | |
} | |
} | |
static func printPlayers(_ players : OutputViewConvertible) { | |
let playerNameList : [String] = players.printableList | |
for item in playerNameList { | |
print(item) | |
} | |
} | |
static func printPlayers(handlerable : OutputViewHandlerable) { | |
handlerable.printBy { (name) in | |
print(name) | |
} | |
} | |
} | |
struct LadderPlayer { | |
var name = "" | |
} | |
protocol OutputViewHandlerable { | |
func printBy(handler: (String) -> ()) | |
} | |
protocol OutputViewConvertible { | |
var printableList : [String] { get } | |
} | |
struct LadderPlayers : OutputViewConvertible { | |
var printableList: [String] { | |
return players.map{ $0.name } | |
} | |
private(set) var players = [LadderPlayer]() | |
init(names : String) { | |
let nameList = names.split(separator: ",") | |
players = nameList.map{ LadderPlayer(name: String($0)) } | |
} | |
var count : Int { | |
return players.count | |
} | |
} | |
extension LadderPlayers : OutputViewHandlerable { | |
func printBy(handler: (String) -> ()) { | |
players.forEach{ handler($0.name) } | |
} | |
} | |
struct HugeLadderGame { | |
var height = 0 | |
var players : LadderPlayers! | |
var ladders = [[LadderStep]]() | |
mutating func run() { | |
self.height = InputView.readHeight() | |
let names = InputView.readPlayerNames() | |
self.players = LadderPlayers.init(names: names) | |
generateLadders() | |
OutputView.printLadders(ladders: ladders) | |
OutputView.printPlayers(players) | |
} | |
mutating fileprivate func generateLadder(_ row: Int) { | |
ladders.append([LadderStep]()) | |
for _ in 0..<players.count { | |
ladders[row].append(LadderStep(LadderStep.randomStep())) | |
} | |
} | |
mutating func generateLadders() { | |
for row in 0..<height { | |
generateLadder(row) | |
} | |
} | |
} | |
var game = HugeLadderGame() | |
game.run() | |
struct PlayersMock : OutputViewConvertible { | |
var printableList: [String] { | |
return ["JK", "Crong"] | |
} | |
} | |
OutputView.printPlayers(PlayersMock()) | |
let offStep = LadderStep(0) //on.off. | |
let onStep = LadderStep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct InputView { | |
static func foo(param: Int) -> Int { | |
return param // parameter 매개변수 | |
} | |
static private func readData(question: String) -> String { | |
print(question) | |
return readLine() ?? "" | |
} | |
static func readHeight() -> Int { | |
let height = readData(question: "사다리 높이를 입력해주세요.") | |
return Int(height) ?? 0 | |
} | |
static func readPlayerNames() -> [String] { | |
let players = readData(question: "참여할 사람 이름을 입력하세요") | |
return players.split(separator: ",").map{String($0)} | |
} | |
} | |
InputView.foo(param: 100) //argument 인자.값 | |
struct LadderPlayer { | |
var name = "" | |
} | |
struct HugeLadderGame { | |
private var height = 0 | |
private var players = [LadderPlayer]() | |
private var ladders = [[Int]]() | |
//------- | |
mutating func config(height : Int, names: [String]) { | |
self.height = height | |
self.players = names.map({LadderPlayer(name:$0)}) | |
generateLadders() | |
} | |
mutating func generateLadders() { | |
for row in 0..<height { | |
ladders.append([Int]()) | |
for _ in 0..<players.count { | |
let hasStep = Int(arc4random_uniform(2)) | |
ladders[row].append(hasStep) | |
} | |
} | |
} | |
} | |
extension HugeLadderGame : LadderPrintable { | |
func hasStep(row: Int, col: Int) -> Bool { | |
return (ladders[row][col] == 1) | |
} | |
func matrix() -> (row: Int, col: Int) { | |
return (height, players.count-1) | |
} | |
} | |
protocol LadderPrintable { | |
func matrix() -> (row : Int, col : Int) | |
func hasStep(row: Int, col: Int) -> Bool | |
} | |
struct OutputView { | |
fileprivate static func printStep(_ game: LadderPrintable, _ row: Int, _ col: Int) { | |
let step = game.hasStep(row: row, col: col) | |
if step { | |
print("---", "|", separator:"", terminator:"") | |
} | |
else { | |
print(" ", "|", separator:"", terminator:"") | |
} | |
} | |
fileprivate static func printColumn(_ matrix: (row: Int, col: Int), _ game: LadderPrintable, _ row: Int) { | |
print("|", terminator:"") | |
for col in 0..<matrix.col { | |
printStep(game, row, col) | |
} | |
print() | |
} | |
static func printLadders(from game: LadderPrintable) { | |
let matrix = game.matrix() | |
for row in 0..<matrix.row { | |
printColumn(matrix, game, row) | |
} | |
} | |
} | |
struct MockGame : LadderPrintable { | |
func matrix() -> (row: Int, col: Int) { | |
return (2,2) | |
} | |
func hasStep(row: Int, col: Int) -> Bool { | |
return true | |
} | |
} | |
func main() { | |
// | |
let height = InputView.readHeight() | |
let names = InputView.readPlayerNames() | |
// | |
var game = HugeLadderGame() | |
game.config(height: height, names: names) | |
// | |
OutputView.printLadders(from: MockGame()) | |
} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// LadderGame | |
// | |
// Created by JK on 03/01/2018. | |
// Copyright © 2018 JK. All rights reserved. | |
// | |
import Foundation | |
protocol LadderRunnable { | |
func ladderHeight() -> Int | |
func playerNames() -> [String] | |
} | |
struct InputView { | |
static func readHeight() -> Int { | |
print("사다리 높이를 입력해주세요.") | |
let height = readLine() ?? "" | |
return Int(height) ?? 0 | |
} | |
static func readPlayerNames() -> [String] { | |
print("참여할 사람 이름을 입력하세요") | |
let players = readLine() ?? "" | |
return players.split(separator: ",").map{String($0)} | |
} | |
} | |
struct GameInfo : LadderRunnable { | |
private var height : Int | |
private var names : [String] | |
init(height: Int, names: [String]) { | |
self.height = height | |
self.names = names | |
} | |
func ladderHeight() -> Int { | |
return height | |
} | |
func playerNames() -> [String] { | |
return names | |
} | |
} | |
struct LadderPlayer { | |
var name = "" | |
} | |
struct SmallLadderGame { | |
static func run(runner : LadderRunnable) { | |
let ladders = LadderGenerater.generateLadders(runner: runner) | |
let ladderInfo = LadderInfo(steps: ladders) | |
OutputView().printLadders(printable: ladderInfo) | |
} | |
} | |
struct LadderGenerater { | |
static func generateLadders(runner : LadderRunnable) -> [[Int]] { | |
var ladders = [[Int]]() | |
let names = runner.playerNames() | |
let players = names.map({LadderPlayer(name:$0)}) | |
for row in 0..<runner.ladderHeight() { | |
ladders.append([Int]()) | |
for _ in 0..<players.count { | |
let hasStep = Int(arc4random_uniform(4)) | |
ladders[row].append(hasStep) | |
} | |
} | |
return ladders | |
} | |
} | |
protocol LadderPrintable { | |
func ladders() -> [[Int]] | |
} | |
struct LadderInfo : LadderPrintable { | |
private var ladderSteps: [[Int]] | |
func ladders() -> [[Int]] { | |
return ladderSteps | |
} | |
init(steps: [[Int]]) { | |
self.ladderSteps = steps | |
} | |
} | |
struct OutputView { | |
func printLadders(printable: LadderPrintable) { | |
for row in printable.ladders() { | |
print("|", terminator:"") | |
for step in row { | |
switch step { | |
case 1: | |
print("---", "|", separator:"", terminator:"") | |
case 2: | |
print("--\\", "|", separator:"", terminator:"") | |
case 3: | |
print("/--", "|", separator:"", terminator:"") | |
default: | |
print(" ", "|", separator:"", terminator:"") | |
} | |
} | |
print() | |
} | |
} | |
} | |
//main | |
let gameInfo = GameInfo(height: InputView.readHeight(), | |
names: InputView.readPlayerNames()) | |
struct GameInfoMock : LadderRunnable { | |
func ladderHeight() -> Int { | |
return 0 | |
} | |
func playerNames() -> [String] { | |
return ["aaaaaaaaaaaaaaaaaa"] | |
} | |
} | |
SmallLadderGame.run(runner:GameInfoMock()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍