-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(Rule): print 함수 구현 Rule -> Three로 이동 및 Rule에 location 정보 추가
- Loading branch information
Showing
5 changed files
with
77 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,15 @@ | ||
import { ThreeSixNineGame, ThreeSixNineGameImpl } from "."; | ||
import { ClapCounter } from "./ClapCounter"; | ||
import { Player } from "./Player"; | ||
import { Rule } from "./Rule"; | ||
|
||
export class BusanRule implements ThreeSixNineGame { | ||
do369(number: number): string { | ||
let clapCount = 0; | ||
|
||
for (let num of String(number)) { | ||
if (Number(num) % 3 === 0) clapCount++; | ||
} | ||
|
||
return clapCount > 0 ? "clap".repeat(clapCount) : `${number}`; | ||
} | ||
|
||
async playGame(players: Player[], clapCounter?: ClapCounter) { | ||
// test를 위해서 100으로 제한해둠 | ||
// test가 영향을 안 받게 하기 위해서는 mocking을 해줘야 할 것 같음 | ||
|
||
try { | ||
for (let index = 0; index < Infinity; index++) { | ||
await this.print(index, players, clapCounter); | ||
} | ||
} catch (e: any) { | ||
console.log(e.message); | ||
} | ||
} | ||
|
||
async print(index: number, players: Player[], clapCounter?: ClapCounter) { | ||
const playerLength = players.length; | ||
export class BusanRule implements Rule { | ||
private location = "부산"; | ||
|
||
const number = index + 1; | ||
const answer = this.do369(number); | ||
getLocation = () => { | ||
return this.location; | ||
}; | ||
|
||
const player = players[index % playerLength]; | ||
const isIncorrectAnswer = this.isTurnOfIncorrectAnswer(player); | ||
|
||
if (isIncorrectAnswer) { | ||
const incorrectAnswer = this.getIncorrectAnswer(number); | ||
console.log(`부산 - ${player.name}: ${incorrectAnswer}`); | ||
throw new Error("틀렸습니다! 게임을 종료합니다."); | ||
} | ||
|
||
const clapCount = answer.match(/clap/g)?.length || 0; | ||
|
||
clapCounter?.addClapCount(clapCount); | ||
|
||
console.log(`부산 - ${player.name}: ${answer}`); | ||
} | ||
|
||
private isTurnOfIncorrectAnswer(player: Player) { | ||
const random = Math.random(); | ||
return player.incorrectAnswerRate > random; | ||
} | ||
do369(number: number): string { | ||
const clapNumberList = String(number).match(/3|6|9+/g); | ||
|
||
private getIncorrectAnswer(number: number) { | ||
return number - 1; | ||
return clapNumberList ? "clap".repeat(clapNumberList.length) : `${number}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ThreeSixNineGame } from "./TreeSixNineGame"; | ||
|
||
export interface Rule extends Pick<ThreeSixNineGame, "do369"> { | ||
getLocation: () => string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,14 @@ | ||
import { ThreeSixNineGame, ThreeSixNineGameImpl } from "./TreeSixNineGame"; | ||
import { Player } from "./Player"; | ||
import { ClapCounter } from "./ClapCounter"; | ||
import { Rule } from "./Rule"; | ||
|
||
export class SeoulRule implements ThreeSixNineGame { | ||
do369(number: number): string { | ||
for (let num of String(number)) { | ||
if (Number(num) % 3 === 0) return "clap"; | ||
} | ||
|
||
return `${number}`; | ||
} | ||
|
||
async playGame(players: Player[], clapCounter?: ClapCounter) { | ||
// test를 위해서 100으로 제한해둠 | ||
// test가 영향을 안 받게 하기 위해서는 mocking을 해줘야 할 것 같음 | ||
|
||
try { | ||
for (let index = 0; index < Infinity; index++) { | ||
await this.print(index, players, clapCounter); | ||
} | ||
} catch (e: any) { | ||
console.log(e.message); | ||
} | ||
} | ||
|
||
async print(index: number, players: Player[], clapCounter?: ClapCounter) { | ||
const playerLength = players.length; | ||
|
||
const number = index + 1; | ||
const answer = this.do369(number); | ||
export class SeoulRule implements Rule { | ||
private location = "서울"; | ||
getLocation = () => { | ||
return this.location; | ||
}; | ||
|
||
const player = players[index % playerLength]; | ||
const isIncorrectAnswer = this.isTurnOfIncorrectAnswer(player); | ||
|
||
if (isIncorrectAnswer) { | ||
const incorrectAnswer = this.getIncorrectAnswer(number); | ||
console.log(`서울 - ${player.name}: ${incorrectAnswer}`); | ||
throw new Error("틀렸습니다! 게임을 종료합니다."); | ||
} | ||
|
||
const clapCount = answer.match(/clap/g)?.length || 0; | ||
|
||
clapCounter?.addClapCount(clapCount); | ||
|
||
console.log(`서울 - ${player.name}: ${answer}`); | ||
} | ||
|
||
private isTurnOfIncorrectAnswer(player: Player) { | ||
const random = Math.random(); | ||
return player.incorrectAnswerRate > random; | ||
} | ||
do369(number: number): string { | ||
const clapNumberList = String(number).match(/3|6|9+/g); | ||
|
||
private getIncorrectAnswer(number: number) { | ||
return number - 1; | ||
return clapNumberList ? "clap" : `${number}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters