diff --git a/NumberBaseball/NumberBaseball/main.swift b/NumberBaseball/NumberBaseball/main.swift index a19fd53d..d09aba94 100644 --- a/NumberBaseball/NumberBaseball/main.swift +++ b/NumberBaseball/NumberBaseball/main.swift @@ -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 = [] +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 { + var numbersSet: Set = [] + + 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) -> Array { + 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) { + print("임의의 수 : ", terminator: "") + for index in 0...2 { + print("\(numbers[index])", terminator: " ") + } +} +startGame() diff --git a/README.md b/README.md index 648238f7..388b71f0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,73 @@ -## iOS 커리어 스타터 캠프 +# 숫자야구 프로젝트 저장소 +> 프로젝트 기간 2022/08/16 ~ 2022/08/19 +> 팀원: @애종 @LJ / 리뷰어: @바드 -### 숫자야구 프로젝트 저장소 +# 목차 +- [프로젝트 소개](#프로젝트-소개) +- [순서도](#순서도) + - [Step1](##Step1) + - [Step2](##Step2) +- [코딩 컨벤션](##코딩-컨벤션) +- [STEP 0](#STEP-0) + - [고민한 점](#고민한-점) + - [배운 개념](#배운개념) +- [STEP 1](#STEP-1) + - [고민한 점](#고민한-점) + - [배운 개념](#배운개념) -- 이 저장소를 자신의 저장소로 fork하여 프로젝트를 진행합니다 + +# 프로젝트 소개 +1~9까지 수 중 3개의 랜덤 숫자를 뽑아놓고, 사용자의 입력값을 받아 숫자, 순서의 일치여부를 판단해 승리여부를 결정하는 숫자 야구 게임. + +# 개발환경 및 라이브러리 +[![swift](https://img.shields.io/badge/swift-5.6-orange)]() +[![xcode](https://img.shields.io/badge/Xcode-13.4-blue)]() + +# 순서도 + +## Step1 +![](https://i.imgur.com/eE0lPEQ.png) + +## Step2 +![](https://i.imgur.com/Cekbs0w.png) + + + +--- + +## 코딩 컨벤션 +- 변수명 정할 때 단어 축약하지 않기 +- 상수로 선언해줄 수 있는 부분은 최대한 상수로 구현 +- 전역변수는 최대한 지양 +- 타입 명시는 통일 +- 함수 내부는 함수명과 함수의 목적에 맞게 구현 + +# [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) diff --git a/flowchar_step2.png b/flowchar_step2.png new file mode 100644 index 00000000..bcc7b2f3 Binary files /dev/null and b/flowchar_step2.png differ diff --git a/flowchart_step1.png b/flowchart_step1.png new file mode 100644 index 00000000..bcc97e0b Binary files /dev/null and b/flowchart_step1.png differ