Skip to content
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] Diana, Denny #204

Open
wants to merge 14 commits into
base: ic_11_diana
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions NumberBaseball/NumberBaseball.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
2E6689692B464B00009123FF /* GenerateNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E6689682B464B00009123FF /* GenerateNumber.swift */; };
2E66896B2B464B1A009123FF /* UserInputNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E66896A2B464B1A009123FF /* UserInputNumber.swift */; };
2E66896D2B464B88009123FF /* CheckStrikeAndBall.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E66896C2B464B88009123FF /* CheckStrikeAndBall.swift */; };
2E66896F2B464C1B009123FF /* GameStart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E66896E2B464C1B009123FF /* GameStart.swift */; };
2E6689732B464FC4009123FF /* ExcuteGame.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E6689722B464FC4009123FF /* ExcuteGame.swift */; };
C7A29CFC2551372B00CAC77A /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7A29CFB2551372B00CAC77A /* main.swift */; };
/* End PBXBuildFile section */

Expand All @@ -23,6 +28,12 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
2E6689682B464B00009123FF /* GenerateNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenerateNumber.swift; sourceTree = "<group>"; };
2E66896A2B464B1A009123FF /* UserInputNumber.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserInputNumber.swift; sourceTree = "<group>"; };
2E66896C2B464B88009123FF /* CheckStrikeAndBall.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckStrikeAndBall.swift; sourceTree = "<group>"; };
2E66896E2B464C1B009123FF /* GameStart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameStart.swift; sourceTree = "<group>"; };
2E6689722B464FC4009123FF /* ExcuteGame.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExcuteGame.swift; sourceTree = "<group>"; };
8BA1F3BF2B46562C00D80796 /* FlowChar_Baseball.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = FlowChar_Baseball.png; sourceTree = "<group>"; };
C7A29CF82551372B00CAC77A /* NumberBaseball */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = NumberBaseball; sourceTree = BUILT_PRODUCTS_DIR; };
C7A29CFB2551372B00CAC77A /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -58,6 +69,12 @@
isa = PBXGroup;
children = (
C7A29CFB2551372B00CAC77A /* main.swift */,
2E6689722B464FC4009123FF /* ExcuteGame.swift */,
2E66896E2B464C1B009123FF /* GameStart.swift */,
2E6689682B464B00009123FF /* GenerateNumber.swift */,
2E66896A2B464B1A009123FF /* UserInputNumber.swift */,
2E66896C2B464B88009123FF /* CheckStrikeAndBall.swift */,
8BA1F3BF2B46562C00D80796 /* FlowChar_Baseball.png */,
);
path = NumberBaseball;
sourceTree = "<group>";
Expand Down Expand Up @@ -120,6 +137,11 @@
buildActionMask = 2147483647;
files = (
C7A29CFC2551372B00CAC77A /* main.swift in Sources */,
2E6689692B464B00009123FF /* GenerateNumber.swift in Sources */,
2E66896B2B464B1A009123FF /* UserInputNumber.swift in Sources */,
2E66896D2B464B88009123FF /* CheckStrikeAndBall.swift in Sources */,
2E6689732B464FC4009123FF /* ExcuteGame.swift in Sources */,
2E66896F2B464C1B009123FF /* GameStart.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
57 changes: 57 additions & 0 deletions NumberBaseball/NumberBaseball/CheckStrikeAndBall.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// CheckWinOrLose.swift
// NumberBaseball
//
// Created by 김민제 on 1/4/24.
//

import Foundation

extension NumberBaseBall {
func compare(){
if repeatTime > 1 {
let strike = checkStrike()
let ball = checkIfBall()

print("\(strike) 스트라이크, \(ball) 볼")

if strike == 3 {
print("사용자 승리..!")
repeatTime = 9
} else {
repeatTime -= 1
print("남은 기회는 \(repeatTime)")
userInputNumber()
}
} else {
print("컴퓨터 승리...!")
repeatTime = 9
}
}

func checkStrike () -> Int {
var numberOfStrike: Int = 0

for i in 0...(userNumList.count - 1) {
if comNumList[i] == userNumList[i] {
numberOfStrike += 1
}
}
return numberOfStrike
}

func checkIfBall() -> Int {
var numberOfBall: Int = 0

for i in 0...(userNumList.count - 1) {
var changedComNumList: [String] = comNumList
changedComNumList[i] = "-1"

if changedComNumList.contains(userNumList[i]) {
numberOfBall += 1
}
}
return numberOfBall
}
}

41 changes: 41 additions & 0 deletions NumberBaseball/NumberBaseball/ExcuteGame.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// ExcuteGame.swift
// NumberBaseball
//
// Created by 김민제 on 1/4/24.
//

import Foundation

extension NumberBaseBall {

Choose a reason for hiding this comment

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

extension으로 하신 이유가 있을까요?

enum GameFunc: String {

Choose a reason for hiding this comment

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

swift에선 약어보단 풀네이밍이 선호됩니다😆

case start = "1"
case end = "2"
}

func execute() {
var flag: Bool = true

while flag {
print("1. 게임시작")
print("2. 게임종료")
print("원하는 기능을 선택해주세요: ", terminator: "")

guard let input = readLine(), !input.isEmpty else {
print("입력이 잘못 되었습니다.")
continue
}
Comment on lines +24 to +27

Choose a reason for hiding this comment

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

현재와 같이 에러처리를 하게되면 빈값 이외의 잘못된 입력에 대한 예외처리가 안될 거 같습니다! Step2의 조건인 메뉴의 잘못된 입력 처리의 예시를 참고하시어 수정해주세요😊


guard let selected = GameFunc(rawValue: input) else {
continue
}

switch selected {
case .start:
gameStart()
case .end:
flag = false
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions NumberBaseball/NumberBaseball/GameEnd.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// GameEnd.swift
// NumberBaseball
//
// Created by 김민제 on 1/4/24.
//

import Foundation

extension NumberBaseBall {

}
16 changes: 16 additions & 0 deletions NumberBaseball/NumberBaseball/GameStart.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// GameStart.swift
// NumberBaseball
//
// Created by 김민제 on 1/4/24.
//

import Foundation

extension NumberBaseBall {
func gameStart(){
generateNumber()
userInputNumber()
}
}

Comment on lines +10 to +16

Choose a reason for hiding this comment

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

1️⃣ extension으로 하신 이유가 있을까요?

2️⃣ 별도의 파일로 분리한 이유가 있을까요?
파일을 분리하는 기준을 생각해보시면 좋을 거 같아요!

코드는 결국 협업을 위한 것이기 때문에 파일분리, 객체 extension은 근거가 필요합니다!

26 changes: 26 additions & 0 deletions NumberBaseball/NumberBaseball/GenerateNumber.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// GenerateNumber.swift
// NumberBaseball
//
// Created by 김민제 on 1/4/24.
//

import Foundation

extension NumberBaseBall {
func generateNumber() {
comNumList = []

while comNumList.count < 3 {
let randomNum = Int.random(in: 1...9)

let randomNumStr = String(randomNum)

if !comNumList.contains(randomNumStr) {
comNumList.append(randomNumStr)
}
}
print("comNumList - \(comNumList)")

Choose a reason for hiding this comment

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

해당 부분은 확인을 위한 print 구문인 거 같습니다😊
삭제부탁드려요!

}
}

43 changes: 43 additions & 0 deletions NumberBaseball/NumberBaseball/UserInputNumber.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// UserInputNumber.swift
// NumberBaseball
//
// Created by 김민제 on 1/4/24.
//

import Foundation

extension NumberBaseBall {
func userInputNumber() {
userNumList = []

print("입력해주세요 : ", terminator: "")

guard let randomNum = readLine() else {
return
}

let splitUserInput = randomNum.components(separatedBy: " ")

guard splitUserInput.count == 3 else {
print("숫자 3개를 띄어쓰기로 구분하여 입력해주세요.")
userInputNumber()
return
}
Comment on lines +22 to +26

Choose a reason for hiding this comment

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

잘못된 입력 시 스텝에서 제시하는 조건에 맞추어 print구문 수정이 필요할 거 같습니다!


for i in 0...(splitUserInput.count - 1) {
let duplicatedArr: Array<String> = splitUserInput.filter({$0 == splitUserInput[i]})

guard duplicatedArr.count == 1 else {
print("중복 숫자는 허용하지 않습니다.")
userInputNumber()
return
}
}

userNumList = splitUserInput
compare()
}
}


17 changes: 14 additions & 3 deletions NumberBaseball/NumberBaseball/main.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
//
// NumberBaseball - main.swift
// Created by yagom.
// Created by yagom.
// Copyright © yagom academy. All rights reserved.
//
//

import Foundation

print("Hello, World!")
class NumberBaseBall {
var comNumList: Array<String>
var userNumList: Array<String>
var repeatTime: Int = 9

init(comNumList: Array<String>, userNumList: Array<String>) {
self.comNumList = comNumList
self.userNumList = userNumList
}
}
Comment on lines +9 to +18

Choose a reason for hiding this comment

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

해당 객체도 별도의 파일로 구분하면 좋을 거 같습니다!😊


let numberBaseball = NumberBaseBall(comNumList: [], userNumList: [])

Choose a reason for hiding this comment

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

해당 객체를 빈배열로 넣어주는 이유가 있을까요?
10,11번째줄에서 NumberBaseBall의 초기값으로 주는 것과 차이가 있을까요?

numberBaseball.execute()
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,53 @@
## iOS 커리어 스타터 캠프
# iOS 커리어 스타터 캠프 11기
#### 제목: 숫자 야구
#### 구성원
- Diana: https://github.com/Diana-yjh
- Danny: https://github.com/dannykim1215

### 숫자야구 프로젝트 저장소
</br>

- 이 저장소를 자신의 저장소로 fork하여 프로젝트를 진행합니다
## ✍️ FlowChart
![Screenshot 2024-01-05 at 9 15 55 PM](https://github.com/Diana-yjh/ios-number-baseball/assets/57698939/11f4ccbb-8f66-4259-aac5-3161db6b8869)


## 🗓️ 타임라인
### 1월 3일
기본 큰 기능 구현 (프로그램 실행 함수, 게임 시작 함수, 사용자 입력 함수 와 검증 기능)</br></br>

### 1월 4일
컴퓨터 랜덤 숫자 생성 함수, 스트라이크 체크 기능, 볼 체크 기능 구현 후 각 함수들을 파일로 분리</br>
FlowChart 이미지 추가, Readme에 추가</br>
이미 구현 한 스트라이크 체크 기능과 볼 체크 기능 을 사용하여 체크 함수 구현</br>
체크 함수 내 게임 시작 함수(execute()) 호출 제거</br></br>

### 1월 5일
사용자 입력 함수 ```filter``` 를 활용하여 로직 변경</br></br>
변경 전
```
if splitUserInput[0] != splitUserInput[1] && splitUserInput[0] != splitUserInput[2] && splitUserInput[1] != splitUserInput[2] {
~
}
```
</br>
변경 후
</br>

```
for i in 0...2 {
let deleteSameNumber = splitUserInput.filter({$0 == splitUserInput[i]})
}
```

</br>
게임 9회 종료 후에도 게임이 종료되지 않고 반복되는 오류 수정 </br>
게임이 실행되지 않고 사용자 입력이 반복되는 오류 수정</br>

## 👍 실행 결과
![Screenshot 2024-01-05 at 9 14 15 PM](https://github.com/Diana-yjh/ios-number-baseball/assets/57698939/82b9742d-b148-46c5-83b1-8215bce5917f)

## 🧐 학습 내용
짝프로그래밍 방식으로 프로젝트 진행</br>
Swift 기본 문법 및 고차함수 ```filter``` 등 을 처음 사용</br>
Class 구현 및 사용</br>
Guard let, If let 등 을 사용한 Optional 바인딩</br>
Git push, pull, remote 설정 등 학습