230614 TIL #111 사다리 게임 구현

김춘복·2023년 6월 13일
0

TIL : Today I Learned

목록 보기
111/494

230614 Today I Learned

저번 면접에서 칠판에 손코딩으로 사다리 타기 게임을 구현해보라는 질문을 받았다. 그때 내가 한 답변이 맞는지 확인해보기 위해 직접 코드를 짜봤다.


사다리 게임 구현

우선 필드로 몇명이 참가하는지, 입력 배열과 결과 배열, 사다리 가로줄(floor) 정보가 필요하다.

public class A_LadderGame {

  private int numOfEntry;
  private String[] inputArray = new String[numOfEntry];
  private String[] outputArray= new String[numOfEntry];
  private int[] floorInfo;

생성자는 게임 인원수를 넣어 생성하도록 했다.

public A_LadderGame(int numOfEntry) {
    if (numOfEntry < 2) throw new IllegalArgumentException("참가자수는 2이상이어야 합니다.");
    this.numOfEntry = numOfEntry;
  }

입력값 배열과 결과값 배열은 setter로 등록한다. 참가자 수와 크기에 차이가 있으면 예외처리

floor는 수동과 random 자동으로 구분했다.
수동은 사다리 범위에 맞게 직접 입력한다.
배열의 index가 위에서부터 0으로 시작하는 층수(floor)고,
안의 값이 floor의 위치인데 좌측에서부터 0으로 시작한다.
자동은 Math.random()으로 구현.

  public void setFloorInfo(int[] floorInfo) {
    for (int i : floorInfo) {
      if (i > numOfEntry-2 || i<0) throw new IllegalArgumentException("사다리 범위에서 벗어났습니다.");
    }
    this.floorInfo = floorInfo;
  }

  public void setRandomFloor(int maxFloor){
    int[] randomFloor = new int[maxFloor];
    for (int i = 0; i < randomFloor.length; i++) {
      randomFloor[i] = (int) (Math.random() * (numOfEntry-2));
    }
    this.floorInfo = randomFloor;
  }

게임은 tmp값을 둬서 floor 배열의 값들을 기준으로 서로 위치를 교환하는 식으로 구현했다. 반환은 이차원 배열로 input과 output 값을 매칭해서 반환.

  public String[][] doGame(){
    String tmp;
    String[] tmpArray = inputArray;

    for (int i = 0; i < floorInfo.length; i++) {
      int floor = floorInfo[i];
      tmp = tmpArray[floor];
      tmpArray[floor] = tmpArray[floor+1];
      tmpArray[floor+1] = tmp;
    }

    String[][] result = new String[numOfEntry][2];
    for (int i = 0; i < result.length; i++) {
      result[i][0] = tmpArray[i];
      result[i][1] = outputArray[i];
    }
    return result;
  }

구현 코드

import java.util.Arrays;

public class A_LadderGame {

  private int numOfEntry;
  private String[] inputArray = new String[numOfEntry];
  private String[] outputArray= new String[numOfEntry];
  private int[] floorInfo;

  public A_LadderGame(int numOfEntry) {
    if (numOfEntry < 2) throw new IllegalArgumentException("참가자수는 2이상이어야 합니다.");
    this.numOfEntry = numOfEntry;
  }

  public void setInputArray(String[] inputArray) {
    if (inputArray.length != numOfEntry) throw new IllegalArgumentException("참가자 수만큼 넣어주세요");
    this.inputArray = inputArray;
  }

  public void setOutputArray(String[] outputArray) {
    if (outputArray.length != numOfEntry) throw new IllegalArgumentException("참가자 수만큼 넣어주세요");
    this.outputArray = outputArray;
  }

  public void setFloorInfo(int[] floorInfo) {
    for (int i : floorInfo) {
      if (i > numOfEntry-2 || i<0) throw new IllegalArgumentException("사다리 범위에서 벗어났습니다.");
    }
    this.floorInfo = floorInfo;
  }

  public void setRandomFloor(int maxFloor){
    int[] randomFloor = new int[maxFloor];
    for (int i = 0; i < randomFloor.length; i++) {
      randomFloor[i] = (int) (Math.random() * (numOfEntry-2));
    }
    this.floorInfo = randomFloor;
  }

  public String[][] doGame(){
    String tmp;
    String[] tmpArray = inputArray;

    for (int i = 0; i < floorInfo.length; i++) {
      int floor = floorInfo[i];
      tmp = tmpArray[floor];
      tmpArray[floor] = tmpArray[floor+1];
      tmpArray[floor+1] = tmp;
    }

    String[][] result = new String[numOfEntry][2];
    for (int i = 0; i < result.length; i++) {
      result[i][0] = tmpArray[i];
      result[i][1] = outputArray[i];
    }
    return result;
  }

  public static void main(String[] args) {
    A_LadderGame ladderGame = new A_LadderGame(4);
    String[] inputArray = {"A", "B", "C", "D"};
    String[] outputArray = {"서울", "대전", "대구", "부산"};
    int[] floorInfo = {0, 2, 1, 1, 2, 1, 1};
    ladderGame.setInputArray(inputArray);
    ladderGame.setOutputArray(outputArray);
    ladderGame.setFloorInfo(floorInfo);

    String[][] result1 = ladderGame.doGame();

    System.out.println("-----------------------");
    System.out.println("첫 게임 결과는");
    for (String[] strings : result1) {
      System.out.println(Arrays.toString(strings));
    }
    System.out.println("-----------------------");

    ladderGame.setRandomFloor(10);
    String[][] result2 = ladderGame.doGame();

    System.out.println("-----------------------");
    System.out.println("두번째 랜덤게임 결과는");
    for (String[] strings : result2) {
      System.out.println(Arrays.toString(strings));
    }
    System.out.println("-----------------------");
  }

}

profile
꾸준히 성장하기 위해 매일 log를 남깁니다!

0개의 댓글