오늘의하루

Java로 숫자야구 게임 만들기 본문

JAVA

Java로 숫자야구 게임 만들기

오늘의하루_master 2022. 12. 1. 11:15

만들면서 새로 알게된 점은 int 배열을 List<Interger>로 변환할때 List<Integer> list = new ArrayList<>(Arrays.toList(int 배열))이 안된다는 것이다.

그래서 코드를 작성할때 for문을 통해 하나씩 list에 추가해주었다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main
{
   public static void main(String[] args) {
      System.out.println("Number BaseBall Game!");
      System.out.print("START(AnyKey) or QUIT(Q) = ");
      try{
          BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          /*************************************************************************************

          BufferedReader saves it in Buffer when you enter it and transmits the contents stored
          in the buffer at once when it is full or when the opening character is entered.
          
          **************************************************************************************/
          String sq = br.readLine();
          if(sq.equalsIgnoreCase("q")){
              System.out.println("Game Exit..");
              System.exit(0);
          }else{
              System.out.println("Game Start!!");
              Game game = new Game();
            //   System.out.println(Arrays.toString(game.arr)); /* TEST */
              while(true){
                  System.out.print("Please enter a 4-digit number >> ");
                  String[] A2 = br.readLine().split(" +");
                  if(A2[0].equalsIgnoreCase("q")){
                      System.out.println("End the game...");
                      break;
                  }
                  if(A2.length != 4){
                      System.out.println("********* Error! 4-digit number!");
                      continue;
                  }
                  Set<String> A2check = new HashSet<>(Arrays.asList(A2));
                  if(A2check.size() != 4){
                      System.out.println("********* Duplicate number found!");
                      System.out.println("********* Please re-enter!");
                      continue;
                  }
                  int[] A3 = new int[A2.length];
                  for(int i = 0; i < A2.length; i++){
                      A3[i] = Integer.parseInt(A2[i]);
                  }
                //   System.out.println(Arrays.toString(A3)); /* TEST */
                  List<Integer> listq = new ArrayList<>();
                  for(int num : game.arr){
                      listq.add(num);
                  }
                  int x = com(listq, game.arr , A3);
                  if(x == 1){
                      break;
                  }
              }
          }
      }catch(Exception e){
          
      }
   }
   static public int com(List<Integer> listq, int[] Q, int[] A){
       int strike = 0;
       int ball = 0;
       int out = 0;
       
       for(int i = 0; i < Q.length; i++){
           if(Q[i] == A[i]){
               strike = strike + 1;
           }else{
               if(listq.contains(A[i])){
                   ball = ball + 1;
               }else{
                   out = out + 1;
               }
           }
       }
       if(strike == 4){
           System.out.println("You won the game!!!!");
           return 1;
       }else{
           System.out.println("strike = " + strike + ", ball = " + ball + ", out = " + out);
           return -1;
       }
       
   }
}

class Game{
    int[] arr;
    
    Game(){
        this.arr = question(this.arr);
    }
    
    private int[] question(int[] arr){ // Question Making...
        Set<Integer> set = new HashSet<>();
        int i = 0;
        
        while(set.size() < 4){
            int num = (int)(Math.random() * 100) + 1;
            set.add(num);
        }
        
        arr = new int[set.size()];
        Iterator it = set.iterator();
        while(it.hasNext()){
            Object next = it.next();
            arr[i++] = (int)next;
        }
        return arr;
    }
}
Comments