카테고리 없음

라이브 코딩테스트 3

father6019 2025. 5. 24. 00:16
728x90
반응형

시간이 얼마 남지 않았다..

3.6.9 게임 final  version 

 

우선 369게임 구현 

package com.example.coding.livefive;

public class TSNGameMain {

    public static void main(String[] args) {
        int n = 100;
        for (int i = 1; i <= n; i++) {
            System.out.println(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;
        }

    }
}

 

사용자와 오답률을 추가하면

package com.example.coding.livefive;


import java.util.concurrent.ThreadLocalRandom;

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 TSNGameMain {

    public static void main(String[] args) {

        User[] users = new User[4];
        users[0] = new User("일론머스크", 10);
        users[1] = new User("이론머스크", 10);
        users[2] = new User("삼론머스크", 10);
        users[3] = new User("사론머스크", 10);

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

    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;
        }
    }


    private static String answer(String action, int errRate) {
        int rate = ThreadLocalRandom.current().nextInt(1, 101);
        if (rate < errRate) {
            return "ERROR";
        } else {
            return action;
        }
    }
}

 

지역별 다른규칙의 추상화, 다형성 추가 

 

package com.example.coding.livefive;


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

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;
    }

}


abstract class TSNGame {
    protected final TotalClapCount totalClapCount;
    public TSNGame(TotalClapCount totalClapCount) {
        this.totalClapCount = totalClapCount;
    }
    public abstract String do369(int i);
}

class SeoulTSNGame extends TSNGame {
    public SeoulTSNGame(TotalClapCount totalClapCount) {
        super(totalClapCount);
    }

    @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) {
            totalClapCount.increment();
            return "clap";
        } else {
            return number;
        }
    }
}

class BusanTSNGame extends TSNGame {
    public BusanTSNGame(TotalClapCount totalClapCount) {
        super(totalClapCount);
    }

    @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++;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        if (clapCount > 0) {
            while (clapCount > 0){
                stringBuilder.append("clap");
                totalClapCount.increment();
                clapCount--;
            }
            return stringBuilder.toString();
        } else {
            return number;
        }
    }
}

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

interface TSNGameFactoryInterface {
    public TSNGame getTSNGame();
}

class SeoulTSNGameFactory implements TSNGameFactoryInterface {

    private final TotalClapCount totalClapCount;

    public SeoulTSNGameFactory(TotalClapCount totalClapCount){
        this.totalClapCount = totalClapCount;
    }

    @Override
    public TSNGame getTSNGame() {
        return new SeoulTSNGame(totalClapCount);
    }
}

class BusanTSNGameFactory implements TSNGameFactoryInterface {

    private final TotalClapCount totalClapCount;

    public BusanTSNGameFactory(TotalClapCount totalClapCount){
        this.totalClapCount = totalClapCount;
    }

    @Override
    public TSNGame getTSNGame() {
        return new BusanTSNGame(totalClapCount);
    }
}

class TotalClapCount {
    private final AtomicInteger atomicInteger = new AtomicInteger(0);

    public void increment() {
        atomicInteger.incrementAndGet();
    }

    public int get() {
        return atomicInteger.get();
    }

    public void reset() {
        atomicInteger.set(0);
    }

}


public class TSNGameMain {

    public static void main(String[] args) {

        User[] users = new User[4];
        users[0] = new User("일론머스크", 0);
        users[1] = new User("이론머스크", 10);
        users[2] = new User("삼론머스크", 0);
        users[3] = new User("사론머스크", 0);


        TotalClapCount totalClapCount  = new TotalClapCount();

        TSNGame seoulGame = TSNGameFactory.get(new SeoulTSNGameFactory(totalClapCount));
        TSNGame busanGame = TSNGameFactory.get(new BusanTSNGameFactory(totalClapCount));


        int n = 100;


        CompletableFuture<Void> seoulTask = CompletableFuture.runAsync(() -> {
            for (int i = 1; i <= n; i++) {
                int index = (i - 1) % 4;
                String result = seoulGame.do369(i);
                if (result.equals(answer(result, users[index].getErrRate()))) {
                    System.out.println(users[index].getName() + " : " + result);
                } else {
                    System.out.println("Seoul Game Over");
                    break;
                }
            }
        });

        CompletableFuture<Void> busanTask = CompletableFuture.runAsync(() -> {
            for (int i = 1; i <= n; i++) {
                int index = (i - 1) % 4;
                String result = busanGame.do369(i);
                if (result.equals(answer(result, users[index].getErrRate()))) {
                    System.out.println(users[index].getName() + " : " + result);
                } else {
                    System.out.println("Busan Game Over");
                    break;
                }
            }
        });

        CompletableFuture.allOf(seoulTask, busanTask).join();
        System.out.println("total clap count: " + totalClapCount.get());
    }

    private static String answer(String action, int errRate) {
        int rate = ThreadLocalRandom.current().nextInt(1, 101);
        if (rate < errRate) {
            return "ERROR";
        } else {
            return action;
        }
    }
}
728x90
반응형