Repository navigation
숫자게임 [STEP1] 애종, LJ #138
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
숫자게임 [STEP1] 애종, LJ #138
Changes from all commits
40c78ed
aba4642
2079a86
94e92a7
52c0c40
c6c5583
0a4605f
9e01ddc
b06c91a
671a155
a551a71
9db91f3
8f37f76
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 |
|---|---|---|
| @@ -1,10 +1,75 @@ | ||
| // | ||
| // NumberBaseball - main.swift | ||
| // Created by yagom. | ||
| // Created by 애종,LJ. | ||
| // Copyright © yagom academy. All rights reserved. | ||
| // | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| print("Hello, World!") | ||
| var computerNumberArray: Array<Int> = [] | ||
| var tryCount: Int = 9 | ||
|
|
||
|
|
||
| func startGame() { | ||
| computerNumberArray = createRandomNumbers() | ||
| while tryCount > 0 { | ||
| let randomNumberArray = createRandomNumbers() | ||
|
|
||
| printRandomNumberArray(randomNumberArray) | ||
| let strikeAndBall = checkStrikeAndBall(with: randomNumberArray) | ||
|
|
||
| print("\n\(strikeAndBall[0]) 스트라이크, \(strikeAndBall[1]) 볼") | ||
|
|
||
| tryCount -= 1 | ||
| judgeVictory(by: strikeAndBall[0]) | ||
| printRemainTryCount(strikeCount: strikeAndBall[0]) | ||
| } | ||
| } | ||
|
|
||
| func createRandomNumbers() -> Array<Int> { | ||
| var numbersSet: Set<Int> = [] | ||
|
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.
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. 3개의 |
||
|
|
||
| while numbersSet.count < 3 { | ||
| numbersSet.insert(Int.random(in: 1...9)) | ||
| } | ||
| return Array(numbersSet) | ||
| } | ||
|
|
||
| func judgeVictory(by strikeCount: Int){ | ||
| if strikeCount == 3 { | ||
| print("사용자 승리...!") | ||
| } else if tryCount == 0 { | ||
| print("컴퓨터 승리...!") | ||
| } | ||
| } | ||
|
|
||
| func printRemainTryCount(strikeCount: Int) { | ||
| if strikeCount != 3 && tryCount > 0 { //남은기회 출력조건 | ||
| print("남은 기회 : \(tryCount) \n") | ||
| } | ||
| } | ||
|
|
||
|
|
||
| func checkStrikeAndBall(with numbers: Array<Int>) -> Array<Int> { | ||
| var strikeCount = 0 | ||
| var ballCount = 0 | ||
|
|
||
| for (index,number) in numbers.enumerated() { | ||
| guard computerNumberArray.contains(number) else { continue } | ||
| if computerNumberArray[index] == number { | ||
| strikeCount += 1 | ||
| } else { | ||
| ballCount += 1 | ||
| } | ||
| } | ||
| return [strikeCount, ballCount] | ||
| } | ||
|
|
||
| func printRandomNumberArray(_ numbers: Array<Int>) { | ||
| print("임의의 수 : ", terminator: "") | ||
| for index in 0...2 { | ||
| print("\(numbers[index])", terminator: " ") | ||
| } | ||
| } | ||
|
|
||
| startGame() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,73 @@ | ||
| ## iOS 커리어 스타터 캠프 | ||
| # 숫자야구 프로젝트 저장소 | ||
|
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. LGTM 👍 |
||
| > 프로젝트 기간 2022/08/16 ~ 2022/08/19 | ||
| > 팀원: @애종 @LJ / 리뷰어: @바드 | ||
|
|
||
| ### 숫자야구 프로젝트 저장소 | ||
| # 목차 | ||
| - [프로젝트 소개](#프로젝트-소개) | ||
| - [순서도](#순서도) | ||
| - [Step1](##Step1) | ||
| - [Step2](##Step2) | ||
| - [코딩 컨벤션](##코딩-컨벤션) | ||
| - [STEP 0](#STEP-0) | ||
| - [고민한 점](#고민한-점) | ||
| - [배운 개념](#배운개념) | ||
| - [STEP 1](#STEP-1) | ||
| - [고민한 점](#고민한-점) | ||
| - [배운 개념](#배운개념) | ||
|
|
||
| - 이 저장소를 자신의 저장소로 fork하여 프로젝트를 진행합니다 | ||
|
|
||
|
|
||
| # 프로젝트 소개 | ||
| 1~9까지 수 중 3개의 랜덤 숫자를 뽑아놓고, 사용자의 입력값을 받아 숫자, 순서의 일치여부를 판단해 승리여부를 결정하는 숫자 야구 게임. | ||
|
|
||
| # 개발환경 및 라이브러리 | ||
| []() | ||
| []() | ||
|
|
||
| # 순서도 | ||
|
|
||
| ## Step1 | ||
|  | ||
|
|
||
| ## Step2 | ||
|  | ||
|
|
||
|
|
||
|
|
||
| --- | ||
|
|
||
| ## 코딩 컨벤션 | ||
| - 변수명 정할 때 단어 축약하지 않기 | ||
| - 상수로 선언해줄 수 있는 부분은 최대한 상수로 구현 | ||
| - 전역변수는 최대한 지양 | ||
| - 타입 명시는 통일 | ||
| - 함수 내부는 함수명과 함수의 목적에 맞게 구현 | ||
|
|
||
| # [STEP 0] | ||
|
|
||
|
|
||
| ## 배운개념 | ||
| - git commit 명령어, commit양식, git 사용방법 | ||
|
|
||
|
|
||
| # [STEP 1] | ||
|
|
||
| ## 고민한 점 | ||
| - 볼과 스트라이크를 비교하는 과정을 어떻게 설계할까 고민했습니다. 3가지의 경우의 수로 나누어 어떤것부터 제외해 나갈까 고민한 후 최종적으로 ‘순서에 상관없이 숫자를 포함하는 경우’ 를 먼저 guard로 묶은 후 순서의 일치 여부에 따라 볼과 스트라이크를 구분했습니다. | ||
| - 실행문과 함수의 배치 -> 변수/상수 선언, 실행문 먼저, 함수는 실행되는 순으로 | ||
| - guard else{} statement 에 return을 입력했을 때 함수가 조기 종료되었습니다. 저희가 의도한 것은 반복문 내부의 else{} 에 진입하면 `해당 반복을 중단하고 다음 반복으로 진행`시키는 것이었기 때문에, 공식문서에 따라 continue로 변경했습니다. | ||
| - 무작위 숫자 3개 저장타입을 `Set`와 `Array`중 고민하였습니다. -> 둘 다 사용하였습니다. 첫번째 검증조건인 중복제거에서는 `Set`를, 위치비교에서는 `Array`를 사용하였습니다. | ||
|
|
||
| ## 배운개념 | ||
|
|
||
| - `LGTM`(Looks Good To Me😉) | ||
| - 마크다운 작성법 | ||
|
|
||
|
|
||
|
|
||
| ## 🔗 참고 링크 | ||
| - [Swift ReferenceManual - Guard Statement](https://docs.swift.org/swift-book/ReferenceManual/Statements.html#grammar_guard-statement) | ||
| - [Swift ReferenceManual - Return Statement](https://docs.swift.org/swift-book/ReferenceManual/Statements.html#grammar_return-statement) | ||
| - [Swift API Design Guidelines](https://www.swift.org/documentation/api-design-guidelines/) | ||
| - [Developer Apple Documentation - enumerated()](https://developer.apple.com/documentation/swift/array/enumerated()) | ||
| - [Flow Chart 공식문서](http://www.tcpschool.com/codingmath/flowchart) | ||
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.
Foundation 어떠한 역할을 해주나요?
해당 프레임워크가 현재 필요할까요?
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.
현재는 swift standard library 정도에서 충분한 내용이므로 필요없다고 보여집니다!!
Foundation이 필요한지 아닌지 궁금할 땐 import Foundation 지워볼게요 !