Skip to content
Merged
71 changes: 68 additions & 3 deletions NumberBaseball/NumberBaseball/main.swift
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Foundation 어떠한 역할을 해주나요?
해당 프레임워크가 현재 필요할까요?

Copy link
Copy Markdown

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 지워볼게요 !


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> = []

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set 타입을 사용해주신 이유가 무엇일까요?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3개의 중복되지 않는 랜덤 숫자를 모아 배열로 반환해야 했기 때문에 사용했습니다. 내부에 중복을 허용하지 않는 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<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()
73 changes: 70 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,73 @@
## iOS 커리어 스타터 캠프
# 숫자야구 프로젝트 저장소

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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개의 랜덤 숫자를 뽑아놓고, 사용자의 입력값을 받아 숫자, 순서의 일치여부를 판단해 승리여부를 결정하는 숫자 야구 게임.

# 개발환경 및 라이브러리
[![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)
Binary file added flowchar_step2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added flowchart_step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.