본문 바로가기
→ My Meta+IT/JAVA Source

Java Bingo 소스 코드 분석 풀이

by DigitalJobs 2021. 12. 14.

Java Bingo 소스 코드 분석 풀이


This is an example for developers starting java. Hope this helps you.


import java.util.*;

public class Bingo {
 public static int player[][] = new int[5][5];
 public static int com[][] = new int[5][5];
 
 public static void main (String[] args) throws Exception{
  
  System.out.println("====================================================");
  System.out.println("==========    B I N G O       G A M E    ===========");
  System.out.println("====================================================");
  System.out.println("================== 3줄을 완성하세요 ==================");
  System.out.println("====================================================\n");
  SetGame sg = new SetGame();
  GameStart gt = new GameStart();
  
  System.out.println("==========플레이어 빙고판==========");
  sg.makeboard(player);
  sg.makeboard(com);
  SetGame.print(player);
  //System.out.println("==========컴퓨터 빙고판=========="); sg.print(com);
  
  while(true){
   gt.P_choice();
   gt.C_choice();
   gt.turnCount++;
  }
  
 }
}

class SetGame{
 int ran;
 boolean check;
 int temp[][] = new int[5][5];
 int num[] = new int [25];
 
 public int[][] makeboard(int temp[][]){
  //player 빙고판 세팅
  for(int i = 0; i < 25; ){
   ran = (int)(Math.random() * 26);
   check = true;
   
   for(int j = 0; j < 25; j++){
    if(num[j] == ran)
     check = false;
   }
   
   if(check != false)
    num[i++] = ran;
  }
  
  for(int k = 0, i = 0; i < 5; i++){
   for(int j = 0; j < 5; j++, k++){
    temp[i][j] = num[k];
   }
  }
  return temp;
 }
 
 public static void print(int temp[][]){
  for(int i = 0; i < 5; i++){
   for(int j = 0; j < 5; j++){
    if(temp[i][j] == '0')
     System.out.print("▦\t");
    else
     System.out.print(temp[i][j] + "\t");
   }
   System.out.println();
  }
  System.out.println("==================================");
 }
 
}


class GameStart extends Bingo{
 Scanner in = new Scanner(System.in);
 int num, input, n=0;
 int row, col;
 static int turnCount = 0;
 boolean check;
 public boolean c_win;
 public boolean p_win;
 
 public void P_choice(){
  System.out.print("숫자를 입력하세요 : ");
  input = in.nextInt();
  marking(input);
    
 }
 
 public void C_choice(){
  while(true){
   row = (int)(Math.random() * 5);
   col = (int)(Math.random() * 5);
   if(com[row][col] == '0')
    continue;
   else{
    num = com[row][col];
    break;
   }
  }
  System.out.print("\n컴퓨터 선택 : " + num);
  marking(num);
 }
 
 public void marking(int k){
  System.out.println();
  //player, com  마킹
  for(int i = 0; i < 5; i++){
   for(int j =0; j < 5; j++){
    if(player[i][j] == k)
     player[i][j] = '0';
    
    if(com[i][j] == k)
     com[i][j] = '0';
   }
  }
 
  SetGame.print(player);
  
  if(turnCount >= 7){
   c_checking();
   p_checking();
   whoWin();
  }
  
 }
 
 public void c_checking(){
  boolean G_check = true, S_check = true;
  boolean L_check = true, R_check = true;
  int c_count = 0;
  int i, j;
  
  // ↙ ↘체크
  for(i = 0; i < 5; i++){
   for(j = 0; j < 5; j++){
    if((i + j) == 4){
     if(com[i][j] != '0'){
      R_check = false;
    } }
    if(i == j){
     if(com[i][j] != '0'){
      L_check = false;
  } } } }
  
  if(R_check == true)
   c_count++;
    
  if(L_check == true)
   c_count++;
  
  
  // 가로, 세로 체크
  for(i = 0; i < 5; i++){
   G_check = true;
   S_check = true;
   
   for(j = 0; j < 5; j++){
    if(com[i][j] != '0')
     G_check = false;
   
    if(com[j][i] != '0')
     S_check = false;
   }
   
   if(G_check == true)
    c_count++;
   
   if(S_check == true)
    c_count++;
  }
  
  if(c_count == 3)
   c_win = true;
     
 }
 
 public void p_checking(){
  boolean G_check = true, S_check = true;
  boolean L_check = true, R_check = true;
  int p_count = 0;
  int i, j;
  
  // ↙ ↘체크
  for(i = 0; i < 5; i++){
   for(j = 0; j < 5; j++){
    if((i + j) == 4){
     if(player[i][j] != '0'){
      R_check = false;
    } }
    if(i == j){
     if(player[i][j] != '0'){
      L_check = false;
  } } } }
  
  if(R_check == true)
   p_count++;
    
  if(L_check == true)
   p_count++;
  
  
  // 가로, 세로 체크
  for(i = 0; i < 5; i++){
   G_check = true;
   S_check = true;
   
   for(j = 0; j < 5; j++){
    if(player[i][j] != '0')
     G_check = false;
   
    if(player[j][i] != '0')
     S_check = false;
   }
   
   if(G_check == true)
    p_count++;
   
   if(S_check == true)
    p_count++;
  }
  
  if(p_count == 3)
   p_win = true;
 }
 
 public void whoWin(){
  if(p_win == true && c_win == true){
   System.out.println("비겼습니다.");
   System.out.println("\n==========컴 퓨 터 빙고판==========");
   SetGame.print(com);
   System.exit(0);
  }
  
  else if(p_win == true){
   System.out.println("Player가 이겼습니다.");
   System.out.println("\n==========컴 퓨 터 빙고판==========");
   SetGame.print(com);
   System.exit(0);
  }
  
  else if(c_win == true){
   System.out.println("Com이 이겼습니다.");
   System.out.println("\n==========컴 퓨 터 빙고판==========");
   SetGame.print(com);
   System.exit(0);
  }
 }
 
}

댓글