카테고리 없음

라이브 코딩테스트 2

father6019 2025. 5. 15. 23:45
728x90
반응형

다시 만들어 보자 

369게임의 조건은 이거 였나보다

/**
 * 1) 게임횟수만큼 순회 (100회)
 * 2) 초기 시작 숫자 : 1
 * 3) 현재 숫자 do369메서드 인자로 사용
 * 4) do369 - 숫자를 문자열로 변경
 * 5) do369 - 문자열에서 3, 6, 9 라는 숫자를 갖는지 체크
 * 6) ㄴ 있으면 clap 반환, 없으면 숫자 문자형태로 반환
 * 7) 현재 숫자를 말할 사용자 선택
 * 8) (현재 숫자-1 % 4) => players 인덱스
 * 9) do369의 결과와 인덱스에 해당하는 players를 sout로 출력
 */

 

대충 작성해 보면

public class TSNGameTestTwoMain {
    public static void main(String[] args) {
        int n = 100;
        String[] users = {"일출", "이토", "영삼", "사준"};
        for (int i = 1; i <= n; i++) {
            int index = (i -1) % 4;
            System.out.println(users[index] +" : "+ do369(i));
        }
    }

    private static String do369(int i) {
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }

        if(clapCount > 0 ){
            return "clap";
        } else {
            return number;
        }
    }
}

결과

 

대충 이렇게 찍히는데 일단 369는 만든거 같고

1단계 : 주어진 요구사항에 맞게 369 게임

구현2단계 : 오답률에 따른 게임 종료 및 사용자 등 몇 가지 심화 기능과 클래스

추가3단계 : 지역별 다른 규칙의 369 게임을 위해 추상화 및 다형성

적용4단계 : 다양한 지역 동시 게임 진행을 위한 동시성 적용

 

2단계를 해보면 

 

class User {
    private String name;
    private int errRate;

    public User(String name, int errRate) {
        this.name = name;
        this.errRate = errRate;
    }
    public String getName() {
        return name;
    }
    public int getErrRate() {
        return errRate;
    }
}

public class TSNGameTestTwoMain {
    public static void main(String[] args) {
        int n = 100;
        User[] users = new User[4];
        users[0] = new User("일출", 0);
        users[1] = new User("이토", 10);
        users[2] = new User("영삼", 20);
        users[3] = new User("영삼", 30);

        for (int i = 1; i <= n; i++) {
            int index = (i - 1) % 4;

            if(do369(i).equals(Answer(do369(i), users[index].getErrRate()))) {
                System.out.println(users[index].getName() + " : " + do369(i));
            } else {
                System.out.println(users[index].getName() + " : Game Over");
                break;
            }

        }
    }

    private static String Answer(String number, int errRate){
        int rate = (int) (Math.random() * 100) + 1;
        if(rate < errRate){
            return "ERROR";
        } else {
            return number;
        }
    }

    private static String do369(int i) {
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }

        if (clapCount > 0) {
            return "clap";
        } else {
            return number;
        }
    }
}

 

3단계 지역별 다른 규칙  추상화 다형성..

 

abstract class TSNGame {
    public abstract String do369(int i);
}
interface TSNGameFactoryInterface {
    public TSNGame getTSNGame();
}

class TSNGameFactory {
    public static TSNGame getTSNGame(TSNGameFactoryInterface tsnGameFactoryInterface) {
        return tsnGameFactoryInterface.getTSNGame();
    }
}

class SeoulTSNGameFactory implements TSNGameFactoryInterface {
    @Override
    public TSNGame getTSNGame(){
        return new SeoulTSNGame();
    }
}


class BusanTSNGameFactory implements TSNGameFactoryInterface {
    @Override
    public TSNGame getTSNGame(){
        return new BusanTSNGame();
    }
}
class BusanTSNGame extends TSNGame {
    @Override
    public String do369(int i){
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }
        if (clapCount > 0) {
            return "clap 아님니까";
        } else {
            return number;
        }
    }
}
class SeoulTSNGame extends TSNGame {
    @Override
    public String do369(int i){
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }
        if (clapCount > 0) {
            return "clap";
        } else {
            return number;
        }
    }
}


class User {
    private String name;
    private int errRate;

    public User(String name, int errRate) {
        this.name = name;
        this.errRate = errRate;
    }
    public String getName() {
        return name;
    }
    public int getErrRate() {
        return errRate;
    }
}

public class TSNGameTestTwoMain {
    public static void main(String[] args) {
        int n = 100;
        User[] users = new User[4];
        users[0] = new User("일출", 0);
        users[1] = new User("이토", 10);
        users[2] = new User("영삼", 20);
        users[3] = new User("영삼", 30);

        TSNGame seoulTSNGame = TSNGameFactory.getTSNGame(new SeoulTSNGameFactory());
        TSNGame busanTSNGame = TSNGameFactory.getTSNGame(new BusanTSNGameFactory());

        for (int i = 1; i <= n; i++) {
            int index = (i - 1) % 4;
            if(seoulTSNGame.do369(i).equals(Answer(seoulTSNGame.do369(i), users[index].getErrRate()))) {
                System.out.println(users[index].getName() + " : " + seoulTSNGame.do369(i));
            } else {
                System.out.println(users[index].getName() + " : seoul Game Over");
                break;
            }
        }
        for (int i = 1; i <= n; i++) {
            int index = (i - 1) % 4;
            if(busanTSNGame.do369(i).equals(Answer(busanTSNGame.do369(i), users[index].getErrRate()))) {
                System.out.println(users[index].getName() + " : " + busanTSNGame.do369(i));
            } else {
                System.out.println(users[index].getName() + " : busan Game Over");
                break;
            }
        }
    }

    private static String Answer(String number, int errRate){
        int rate = (int) (Math.random() * 100) + 1;
        if(rate < errRate){
            return "ERROR";
        } else {
            return number;
        }
    }

}

 

4단계를 해보면


abstract class TSNGame {
    public abstract String do369(int i);
}
interface TSNGameFactoryInterface {
    public TSNGame getTSNGame();
}

class TSNGameFactory {
    public static TSNGame getTSNGame(TSNGameFactoryInterface tsnGameFactoryInterface) {
        return tsnGameFactoryInterface.getTSNGame();
    }
}

class SeoulTSNGameFactory implements TSNGameFactoryInterface {
    @Override
    public TSNGame getTSNGame(){
        return new SeoulTSNGame();
    }
}

class BusanTSNGameFactory implements TSNGameFactoryInterface {
    @Override
    public TSNGame getTSNGame(){
        return new BusanTSNGame();
    }
}
class BusanTSNGame extends TSNGame {
    @Override
    public String do369(int i){
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }
        if (clapCount > 0) {
            return "clap 아님니까";
        } else {
            return number;
        }
    }
}
class SeoulTSNGame extends TSNGame {
    @Override
    public String do369(int i){
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }
        if (clapCount > 0) {
            return "clap";
        } else {
            return number;
        }
    }
}


class User {
    private String name;
    private int errRate;

    public User(String name, int errRate) {
        this.name = name;
        this.errRate = errRate;
    }
    public String getName() {
        return name;
    }
    public int getErrRate() {
        return errRate;
    }
}

public class TSNGameTestTwoMain {
    public static void main(String[] args) {
        int n = 100;
        User[] users = new User[4];
        users[0] = new User("일출", 0);
        users[1] = new User("이토", 10);
        users[2] = new User("영삼", 20);
        users[3] = new User("영삼", 30);

        TSNGame seoulTSNGame = TSNGameFactory.getTSNGame(new SeoulTSNGameFactory());
        TSNGame busanTSNGame = TSNGameFactory.getTSNGame(new BusanTSNGameFactory());


        CompletableFuture<Void> seoulTask = CompletableFuture.runAsync(()->{
            for (int i = 1; i <= n; i++) {
                int index = (i - 1) % 4;
                if(seoulTSNGame.do369(i).equals(Answer(seoulTSNGame.do369(i), users[index].getErrRate()))) {
                    System.out.println(users[index].getName() + " : " + seoulTSNGame.do369(i));
                } else {
                    System.out.println(users[index].getName() + " : seoul Game Over");
                    break;
                }
            }
        });
        CompletableFuture<Void> busanTask = CompletableFuture.runAsync(()->{
            for (int i = 1; i <= n; i++) {
                int index = (i - 1) % 4;
                if(busanTSNGame.do369(i).equals(Answer(busanTSNGame.do369(i), users[index].getErrRate()))) {
                    System.out.println(users[index].getName() + " : " + busanTSNGame.do369(i));
                } else {
                    System.out.println(users[index].getName() + " : busan Game Over");
                    break;
                }
            }
        });

        CompletableFuture.allOf(seoulTask, busanTask).join();

    }

    private static String Answer(String number, int errRate){
        int rate = (int) (Math.random() * 100) + 1;
        if(rate < errRate){
            return "ERROR";
        } else {
            return number;
        }
    }
}

 

음.. 이건데..

clap. count 추가된 마지막 버전

package com.example.coding.livetwo;


/**
 * 1) 게임횟수만큼 순회 (100회)
 * 2) 초기 시작 숫자 : 1
 * 3) 현재 숫자 do369메서드 인자로 사용
 * 4) do369 - 숫자를 문자열로 변경
 * 5) do369 - 문자열에서 3, 6, 9 라는 숫자를 갖는지 체크
 * 6) ㄴ 있으면 clap 반환, 없으면 숫자 문자형태로 반환
 * 7) 현재 숫자를 말할 사용자 선택
 * 8) (현재 숫자-1 % 4) => players 인덱스
 * 9) do369의 결과와 인덱스에 해당하는 players를 sout로 출력
 */

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 1단계 : 주어진 요구사항에 맞게 369 게임
 * 구현2단계 : 오답률에 따른 게임 종료 및 사용자 등 몇 가지 심화 기능과 클래스
 * 추가3단계 : 지역별 다른 규칙의 369 게임을 위해 추상화 및 다형성
 * 적용4단계 : 다양한 지역 동시 게임 진행을 위한 동시성 적용
 */

class ClapCounter{
    private final AtomicInteger count = new AtomicInteger(0);
    
    public void increment(){
        count.incrementAndGet();
    }
    public int getCount(){
        return count.get();
    }
}

abstract class TSNGame {
    public abstract String do369(int i);
}
interface TSNGameFactoryInterface {
    public TSNGame getTSNGame();
}

class TSNGameFactory {
    public static TSNGame getTSNGame(TSNGameFactoryInterface tsnGameFactoryInterface) {
        return tsnGameFactoryInterface.getTSNGame();
    }
}

class SeoulTSNGameFactory implements TSNGameFactoryInterface {
    @Override
    public TSNGame getTSNGame(){
        return new SeoulTSNGame();
    }
}

class BusanTSNGameFactory implements TSNGameFactoryInterface {
    @Override
    public TSNGame getTSNGame(){
        return new BusanTSNGame();
    }
}
class BusanTSNGame extends TSNGame {
    @Override
    public String do369(int i){
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }
        if (clapCount > 0) {
            return "clap 아님니까";
        } else {
            return number;
        }
    }
}
class SeoulTSNGame extends TSNGame {
    @Override
    public String do369(int i){
        String number = String.valueOf(i);
        char[] chars = number.toCharArray();
        int clapCount = 0;
        for (char c : chars) {
            if (c == '3' || c == '6' || c == '9') {
                clapCount++;
            }
        }
        if (clapCount > 0) {
            return "clap";
        } else {
            return number;
        }
    }
}


class User {
    private String name;
    private int errRate;

    public User(String name, int errRate) {
        this.name = name;
        this.errRate = errRate;
    }
    public String getName() {
        return name;
    }
    public int getErrRate() {
        return errRate;
    }
}

public class TSNGameTestTwoMain {
    
    
    public static void main(String[] args) {
        ClapCounter clapCounter = new ClapCounter();
        int n = 100;
        User[] users = new User[4];
        users[0] = new User("일출", 0);
        users[1] = new User("이토", 10);
        users[2] = new User("영삼", 20);
        users[3] = new User("영삼", 30);

        TSNGame seoulTSNGame = TSNGameFactory.getTSNGame(new SeoulTSNGameFactory());
        TSNGame busanTSNGame = TSNGameFactory.getTSNGame(new BusanTSNGameFactory());

        CompletableFuture<Void> seoulTask = CompletableFuture.runAsync(()->{
            for (int i = 1; i <= n; i++) {
                int index = (i - 1) % 4;
                if(seoulTSNGame.do369(i).equals(Answer(seoulTSNGame.do369(i), users[index].getErrRate()))) {
                    System.out.println(users[index].getName() + " : " + seoulTSNGame.do369(i));
                    if (seoulTSNGame.do369(i).contains("clap")){
                        clapCounter.increment();
                    }
                    
                } else {
                    System.out.println(users[index].getName() + " : seoul Game Over");
                    break;
                }
            }
        });
        CompletableFuture<Void> busanTask = CompletableFuture.runAsync(()->{
            for (int i = 1; i <= n; i++) {
                int index = (i - 1) % 4;
                if(busanTSNGame.do369(i).equals(Answer(busanTSNGame.do369(i), users[index].getErrRate()))) {
                    System.out.println(users[index].getName() + " : " + busanTSNGame.do369(i));
                    if (busanTSNGame.do369(i).contains("clap")){
                        clapCounter.increment();
                    }
                } else {
                    System.out.println(users[index].getName() + " : busan Game Over");
                    break;
                }
            }
        });

        CompletableFuture.allOf(seoulTask, busanTask).join();
        System.out.println("clap count : "+clapCounter.getCount());

    }

    private static String Answer(String number, int errRate){
        int rate = (int) (Math.random() * 100) + 1;
        if(rate < errRate){
            return "ERROR";
        } else {
            return number;
        }
    }
}

 

728x90
반응형