Skip to content

Commit

Permalink
지역명 출력 method 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
silano08 committed Apr 27, 2024
1 parent 9c06c83 commit 59f9bdf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/JavaPractice/ThreeSixNineGame/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ void set369Rule(ClapRule rule) {
String do369(int number) {
return rule.do369(number);
}

String getLocalName(){
return rule.getLocalName();
}
}
5 changes: 5 additions & 0 deletions src/JavaPractice/ThreeSixNineGame/Rule/Busan369.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public String do369(int number) {
}
return result;
}

@Override
public String getLocalName() {
return "부산";
}
}
2 changes: 2 additions & 0 deletions src/JavaPractice/ThreeSixNineGame/Rule/ClapRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

public interface ClapRule {
String do369(int number);

String getLocalName();
}

5 changes: 5 additions & 0 deletions src/JavaPractice/ThreeSixNineGame/Rule/Seoul369.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public String do369(int number) {
if(numStr.contains("3") || numStr.contains("6") || numStr.contains("9")) return "clap";
return numStr;
}

@Override
public String getLocalName() {
return "서울";
}
}
51 changes: 38 additions & 13 deletions src/JavaPractice/ThreeSixNineGame/ThreeSixNineGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ThreeSixNineGame(RandomProvider randomProvider, Context clapRule) {
* ..중략..
* 상철: 100
*/
public void playGame(Player[] players) {
public void playGame(Player[] players, ClapCounter clapCounter) {
int i = 1;
while (true) {
// 오답률에 의한 랜덤확률 생성
Expand All @@ -37,31 +37,56 @@ public void playGame(Player[] players) {
if (rate < players[(i - 1) % 4].getIncorrectRate()) {
// 오답을 말하는 경우
if (result.equals(Integer.toString(i))) {
System.out.println(players[(i - 1) % 4].getName() + "님이 숫자를 얘기해야하는데 박수를 쳤습니다.");
System.out.println(clapRule.getLocalName() + " - " + players[(i - 1) % 4].getName() + "님이 숫자를 얘기해야하는데 박수를 쳤습니다.");
break;
} else {
System.out.println(players[(i - 1) % 4].getName() + "님이 박수를 쳐야하는데 숫자를 얘기했습니다.");
System.out.println(clapRule.getLocalName() + " - " +players[(i - 1) % 4].getName() + "님이 박수를 쳐야하는데 숫자를 얘기했습니다.");
break;
}
} else {
System.out.println(players[(i - 1) % 4].getName() + ": " + result);
clapCounter.countClap(result);
System.out.println(clapRule.getLocalName() + " - " +players[(i - 1) % 4].getName() + ": " + result);
}
i += 1;
}
}

public void main(String[] args) {
public static void main(String[] args) {
Player[] players = new Player[4];
players[0] = new Player("상철", 0.1);
players[1] = new Player("영철", 0.1);
players[2] = new Player("영숙", 0.1);
players[3] = new Player("동칠", 0.01);
players[0] = new Player("상철", 0.01);
players[1] = new Player("영철", 0.01);
players[2] = new Player("영숙", 0.0021);
players[3] = new Player("동칠", 0.001);

Context context = new Context();
ClapCounter clapCounter = new ClapCounter();

context.set369Rule(new Busan369());
// 부산의 룰로 play
Context context1 = new Context();
context1.set369Rule(new Busan369());
ThreeSixNineGame game1 = new ThreeSixNineGame(new RandomProvider(), context1);

ThreeSixNineGame game = new ThreeSixNineGame(new RandomProvider(), context);
game.playGame(players);
// 서울의 룰로 play
Context context2 = new Context();
context2.set369Rule(new Seoul369());
ThreeSixNineGame game2 = new ThreeSixNineGame(new RandomProvider(), context2);

// 각 게임을 별도의 스레드에서 실행
Thread thread1 = new Thread(() -> game1.playGame(players,clapCounter), "BusanGameThread");
Thread thread2 = new Thread(() -> game2.playGame(players,clapCounter), "SeoulGameThread");

// 스레드 시작
thread1.start();
thread2.start();

// 모든 스레드가 종료될 때까지 대기
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println("Game interrupted: " + e.getMessage());
}

clapCounter.printClapCount();
}

}
10 changes: 5 additions & 5 deletions test/JavaPractice/ThreeSixNineGame/ThreeSixNineGameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import JavaPractice.ThreeSixNineGame.Rule.ClapRule;
import JavaPractice.ThreeSixNineGame.Rule.Seoul369;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

class ThreeSixNineGameTest {

Expand Down Expand Up @@ -35,8 +34,9 @@ void testPlayGameCompletes100Rounds() {
// ThreeSixNineGame 인스턴스 생성
ThreeSixNineGame game = new ThreeSixNineGame(mockRandomProvider, mockContext);

ClapCounter clapCounter = new ClapCounter();
// 테스트 실행
game.playGame(players);
game.playGame(players,clapCounter);

// do369 메서드 호출 검증
verify(mockContext, times(100)).do369(anyInt());
Expand All @@ -45,7 +45,7 @@ void testPlayGameCompletes100Rounds() {
}

@Test
void ruleChangeTest(){
void ruleChangeTest() {

Context context = new Context();

Expand Down

0 comments on commit 59f9bdf

Please sign in to comment.