'국비교육/예제파일'에 해당되는 글 5건
- 2018.07.16 20180716
- 2018.07.13 180713
- 2018.07.12 0712
- 2018.07.09 0627~0705
- 2018.06.29 180627~180629 예제파일
package bingoEx1;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Bingo {
static String[][] bird = new String[][]{
{"참새","두루미","황새","비둘기","까오기"},
{"오리","타조","부엉이","올빼미","뱁새"},
{"꿩","닭","구관조","잉꼬","매"},
{"거위","독수리","콘돌","봉황","공작"},
{"까치","까마귀","앵무새","꾀꼬리","고니"}};
public static void main(String args[]) {
Frame f = new Frame("GridLayoutTest");
Button[][] b = new Button[5][5];
// 버튼에 저장될 스트링
EventHandler e = new EventHandler();
f.setSize(500, 500);
f.setLayout(new GridLayout(5, 5)); // 5행 5열의 테이블을 만든다.
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
b[i][j] = new Button(bird[i][j]);
b[i][j].addActionListener(e); // 빙고의 행렬값 매개변수로 보냄
f.add(b[i][j]);
}
}
f.setVisible(true);
}
}
class EventHandler implements ActionListener{
int cX = 0;
int cY = 0;
int cS = 0;
int cRS= 0;
int countB = 0; //빙고갯수카운트
int[][] bingo = new int[5][5]; //빙고게임기록용
public void actionPerformed(ActionEvent e) {
int i =0; // bingoCount로 보낼 행 매개변수로 사용
int j =0; // bingoCount로 보낼 열 매개변수로 사용
Button b = (Button)e.getSource(); // 소스이용한 임시 버튼생성
equals:
for(i=0;i<5;i++) {
for(j=0;j<5;j++) {
if(Bingo.bird[i][j].equals(b.getLabel())) //문자열 배열값과 버튼 라벨값을 비교
break equals; // 맞는 배열 찾으면 중첩for문 빠져나감
}
}
System.out.println("버튼이름 = "+b.getLabel());
b.setBackground(Color.LIGHT_GRAY);
bingoCount(i,j); // i,j매개변수로 bingoCount실행
}
public void bingoCount(int x,int y) {
System.out.println("x="+x+", y="+y+", bingo[0][0]="+bingo[0][0]);
if(bingo[x][y]==0) { // 이미 클릭된 버튼인지 판단
bingo[x][y]=1;
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(bingo[i][j]==1) { // 버튼활성화여부
if(i==j) // 역슬래쉬카운트
cRS++;
if(i+j==4) // 슬래쉬카운트
cS++;
cX++; //행빙고카운트
}
if(bingo[j][i]==1)
cY++; //열빙고카운트 j , i 역순
if(cS==5) {countB++; cS=0;} // 각 줄 5카운트시 빙고카운트1추가 후 변수 초기화
if(cX==5) {countB++; cX=0;}
if(cY==5) {countB++; cY=0;}
if(cRS==5) {countB++; cRS=0;}
} //for j 종료
cX=0;cY=0; //행렬카운트 초기화
} // for i 종료
cS=0; cRS=0; //슬래쉬카운트 초기화
System.out.println("현재 "+countB+"빙고!!");
countB=0; //빙고카운트 초기화
} else {
System.out.println(x+","+y+"번은 이미 체크되었습니다.");
}
}// bingoCount 죵료
}