숫자 빙고 게임

 


 

public static void map(int n, int m, int pick[], int user[][]) {
	int w = 0;
	boolean ch = false;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			while (true) {
				int tmp = (int) (Math.random() * m) + 1;
				for (int k = 0; k < pick.length; k++) {
					if (tmp == pick[k])
						ch = true;
				}
				if (ch) {
					ch = false;
					continue;
				} else {
					pick[w++] = user[i][j] = tmp;
					break;
				}
			}
		}
	}
}

public static void line(int n, int user[][]) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			System.out.print(user[i][j] + "\t");
		}
		System.out.println();
	}
	System.out.println();

}

public static void ucheck(int user[][], int com[][], int uch) {
	for (int i = 0; i < user.length; i++) {
		for (int j = 0; j < user.length; j++) {
			if (user[i][j] == uch) {
				user[i][j] = 0;
			}
			if (com[i][j] == uch) {
				com[i][j] = 0;
			}
		}
	}
}

public static void ccheck(int m, int com[][], int user[][]) {
	int tmp = 0;
	int cnt = 0, c=0;
	int sa=0;
	

	while (cnt<1) {
		tmp = (int) (Math.random() * m + 1);
		for (int i = 0; i < com.length; i++) {
			for (int j = 0; j < com.length; j++) {
				if (com[i][j] == tmp) {
					com[i][j] = 0;
					sa=tmp;
				}
			}
		}
		
		for (int i = 0; i < user.length; i++) {
			for (int j = 0; j < user.length; j++) {
				if (user[i][j] == sa) {
					user[i][j] = 0;
				}
			}
		}
		cnt+=1;
	}
	System.out.println("컴퓨터 차례 - 고른 숫자는 : "+tmp);
}

public static void bingo(int n,int b,int user[][], int com[][]) {
	int bingo=0, cbingo=0;
	int cnt=0, cntt=0, ccnt=0, ccntt=0;
	
	for(int i=0; i<n; i++) {
		cnt=0; cntt=0; ccnt=0; ccntt=0;
		for(int j=0; j<n; j++) {
			if(user[i][n-1-j]==0) {
				cnt+=1;
				if(cnt==n) {
					bingo+=1;
					cnt=0;
				}
			}
			if(user[n-1-j][i]==0) {
				cntt+=1;
				if(cntt==n) {
					bingo+=1;
					cntt=0;
				}
			}
			
			if(com[i][n-1-j]==0) {
				ccnt+=1;
				if(ccnt==n) {
					cbingo+=1;
					ccnt=0;
				}
			}
			if(com[n-1-j][i]==0) {
				ccntt+=1;
				if(ccntt==n) {
					cbingo+=1;
					ccntt=0;
				}
			}
		}	
	}
	
	if(bingo>=b) {
		System.out.println(bingo+"줄로 유저 승!!");
		System.exit(0);
	}
	else if(cbingo>=b) {
		System.out.println(cbingo+"줄로 컴퓨터 승!!");
		System.exit(0);
	}
	
	else if(bingo>=1 | cbingo>=1) {
		System.out.println("유저 : "+  bingo +" 줄 빙고\n" + "컴퓨터 : " + cbingo + " 줄 빙고");
	}
	
}

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int uch = 0;
	
	
	System.out.print("n * n 배열만큼 뽑을 n을 입력 : ");
	int n = Integer.parseInt(br.readLine());
	System.out.print("빙고값 : 1 ~ m 숫자 m을 입력(m = n*n이상) : ");
	int m = Integer.parseInt(br.readLine());
	System.out.print("빙고 몇줄시 승리 할지 입력(n*2까지 가능) : ");
	int b = Integer.parseInt(br.readLine());

	int user[][] = new int[n][n];
	int com[][] = new int[n][n];
	int upick[] = new int[n * n];
	int cpick[] = new int[n * n];

	map(n, m, upick, user);
	map(n, m, cpick, com);
	System.out.println("[사용자 빙고 판]");
	line(n, user);
	System.out.println("[컴퓨터 빙고 판]");
	line(n, com);

	for(;;) {
	System.out.print("유저차례 - 고를 숫자 입력 :");
	uch = Integer.parseInt(br.readLine());
	ucheck(user, com, uch);
	System.out.println("[사용자 빙고 판]");
	line(n, user);
	System.out.println("[컴퓨터 빙고 판]");
	line(n, com);

	ccheck(m, com, user);
	System.out.println("[사용자 빙고 판]");
	line(n, user);
	System.out.println("[컴퓨터 빙고 판]");
	line(n, com);
	
	bingo(n,b,user,com);
	}
}

 

출력하면 다음과 같습니다.

 

+ Recent posts