-
Notifications
You must be signed in to change notification settings - Fork 21
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
가위바위보게임 [STEP 1] caron #11
base: rft_2_caron
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// RPS.swift | ||
// RockPaperScissors | ||
// | ||
// Created by qussk on 3/21/24. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
//GameView에 있는 fileprivate enum의 Hand를 공유해도 되는 건지.? 일단 따로 쓰긴했어요.. | ||
|
||
fileprivate enum Hand { | ||
static let paper: String = "🖐️" | ||
static let rock: String = "✊" | ||
static let scissor: String = "✌️" | ||
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 족보를 나타내는 타입에서 승, 무, 패에 대한 프로퍼티를 가지고 있는 이유가 궁금합니다! |
||
|
||
static let hands: [String] = [paper, rock, scissor] | ||
} | ||
|
||
struct Genealogy { | ||
let win = "승" | ||
let lose = "패" | ||
let draw = "무" | ||
|
||
//가위바위보 족보 | ||
func determineWinner(hand: [String]) -> String { | ||
switch hand { | ||
case ["🖐️", "✊"] : return win | ||
case ["🖐️", "✌️"] : return lose | ||
case ["✊", "✌️"] : return win | ||
case ["✊", "🖐️"] : return lose | ||
case ["✌️", "🖐️"] : return win | ||
case ["✌️", "✊"] : return lose | ||
Comment on lines
+29
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 패를 이모지로 나타내신 이유가 있을까요? |
||
default: | ||
return draw | ||
} | ||
} | ||
|
||
} | ||
|
||
class RPS { | ||
let genealogy: Genealogy | ||
var count:[Int] = [0,0] | ||
|
||
init(genealogy: Genealogy) { | ||
self.genealogy = genealogy | ||
} | ||
|
||
|
||
func rpsModify(of rps: [String]) throws -> Bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
guard rpsHandsCheck(of: rps), | ||
!rpsEmptyCheck(of: rps) | ||
else { | ||
throw NSError() as Error | ||
} | ||
|
||
var countting = self.count | ||
|
||
while !besttwooutOfthree(of: 3, in: countting)[0] && !besttwooutOfthree(of: 3, in: countting)[1] { | ||
print(self.count) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어떤 용도로 사용하는 print문일까요? 불필요하다면 제거하는 것이 어떨까요? |
||
countting = fightToMatch(of: rps, count: self.count) | ||
|
||
} | ||
|
||
return fightToResult(of: besttwooutOfthree(of: 3, in: countting)) | ||
} | ||
|
||
//손인지 확인 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 꼭 필요한 주석일까요? 메서드명을 명확하게 작성해서 의도를 잘 나타내보면 좋을 것 같습니다! |
||
private func rpsHandsCheck(of rps: [String]) -> Bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API 디자인 가이드에 따라 메서드명을 동사형으로 시작해보는 것은 어떨까요? |
||
return !rps.filter { Hand.hands.contains($0) }.isEmpty | ||
} | ||
|
||
//비어있는지 확인 | ||
private func rpsEmptyCheck(of rps: [String]) -> Bool { | ||
return !rps.filter { $0 == "" }.isEmpty | ||
} | ||
|
||
//승패 확인 | ||
func fightToMatch(of rps: [String], count: [Int]) -> [Int] { | ||
return fightToCounting(of: genealogy.determineWinner(hand: rps), counts: count) | ||
} | ||
|
||
//승패 카운팅 | ||
func fightToCounting(of matching: String, counts: [Int]) -> [Int] { | ||
var counts:[Int] = counts | ||
|
||
if matching == "승" { | ||
counts[0] += 1 | ||
} else if matching == "패" { | ||
counts[1] += 1 | ||
} | ||
|
||
self.count = counts | ||
return counts | ||
} | ||
|
||
//삼세판 | ||
func besttwooutOfthree(of target: Int, in counting: [Int]) -> [Bool] { | ||
return counting.map { $0 == target } //[true, false] | ||
} | ||
|
||
|
||
//삼세판후 대결 결과 | ||
func fightToResult(of countingResult: [Bool]) -> Bool { | ||
//index0 = 나 | ||
//index1 = 컴퓨터 | ||
return countingResult[0] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hand
타입을 추가로 정의하신 이유가 있을까요?한번 정의한
Hand
을 공유해서 사용하는 것이 어떨까요? 공유해도 좋을 것 같습니다!