J-한솔넷

폭탄 퍼즐 게임 본문

프로그래밍/JAVA

폭탄 퍼즐 게임

jhansol 2012. 10. 31. 18:13

이 프로그램은 java 수업용으로 만든 것입니다.

이 프로그램은 제가 옛날에 Turbo C에서 만들었던 것으로 Java GUI Application으로 만든 것입니다.


WinBomb.java







아래에 소스코드를 표시해두었습니다. 참고하세요. ^^



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

class WinBomb {
	public static void main(String[] args) 
	{
		new BombFrame();
	}
}

class BombFrame extends Frame implements ActionListener {
	JPanel card1;
	JPanel card2;

	String Rule;
	JButton GameStart;
	JButton[][] Pan;
	JTextArea TextRule;
	JTextField txtLevelCount;
	JTextField txtRemainBomb;
	JTextField txtScore;
	JTextField txtTurn;
	CardLayout cl;

	int LevelCount;
	int RemainBomb;
	int Score;
	int Turn;
	int MaxTurn;

	public BombFrame() {
		super( "폭탄퍼즐" );
		JPanel temp, temp2;
		JLabel tlabel;
		JButton tbutton;

		card1 = new JPanel();
		card1.setLayout( new BorderLayout() );
		Rule = "      ****폭찬 퍼절 게임****\n";
		Rule += "이 게임은 매설된 폭탄을 재거하는 게임입니다.\n";
		Rule += "사용자가 해당 칸을 마우스로 클릭하면 빈칸은 \n";
		Rule += "폭탄으로 폭탄은 빈칸으로 바뀌는 방법으로 해\n";
		Rule += "당 칸을 중심으로 가로, 새로로 폭탄매설/제거\n";
		Rule += "등을 하게 됩니다. 초기 폭탄 매설을 이 방법으로\n";
		Rule += "해당 레벨만큼 반복하여 준비합니다.\n";
		TextRule = new JTextArea( Rule );
		GameStart = new JButton( "게임 시작" );
		card1.add( TextRule, "Center" );
		card1.add( GameStart, "South" );

		card2 = new JPanel();
		card2.setLayout( new BorderLayout() );

		temp = new JPanel();
		temp.setLayout( new GridLayout( 1, 3 ) );

		temp2 = new JPanel();
		temp2.setLayout( new BorderLayout() );
		temp2.add( new JLabel( "단 계" ), "West" );
		txtLevelCount = new JTextField();
		temp2.add( txtLevelCount, "Center" );
		temp.add( temp2 );

		temp2 = new JPanel();
		temp2.setLayout( new BorderLayout() );
		temp2.add( new JLabel( "폭탄 수 " ), "West" );
		txtRemainBomb = new JTextField();
		temp2.add( txtRemainBomb, "Center" );
		temp.add( temp2 );                                                           

		temp2 = new JPanel();
		temp2.setLayout( new BorderLayout() );
		temp2.add( new JLabel( "점 수 " ), "West" );
		txtScore = new JTextField();
		temp2.add( txtScore, "Center" );
		temp.add( temp2 );

		temp2 = new JPanel();
		temp2.setLayout( new BorderLayout() );
		temp2.add( new JLabel( "턴 수 " ), "West" );
		txtTurn = new JTextField();
		temp2.add( txtTurn, "Center" );
		temp.add( temp2 );
		card2.add( temp, "North" );

		temp = new JPanel();
		Pan = new JButton[10][10];
		temp.setLayout( new GridLayout( 10, 10 ) );
		for( int i = 0 ; i < 10 ; i++ ) {
			for( int j = 0 ; j < 10 ; j++ ) {
				tbutton = new JButton( " " );
				Pan[i][j] = tbutton;
				temp.add( tbutton );
				tbutton.addActionListener( this );
			}
		}
		card2.add( temp, "Center" );
		
		cl = new CardLayout();
		setLayout( cl );
		add( "1", card1 );
		add( "2", card2 );

		setSize( 500, 600 );
		show( true );

		addWindowListener( new WindowAdapter() {
			public void windowClosing( WindowEvent e ) {
				dispose();
			}
		}
		);
		GameStart.addActionListener( this );
	}

	public void actionPerformed( ActionEvent e ) {
		int row, col;

		row = col = -1;

		if( e.getActionCommand() == "게임 시작" ) {
			cl.next( this );
			readyGame();
		}
		else {
			for( int i = 0 ; i < 10 ; i++ ) {
				for( int j = 0 ; j < 10 ; j++ ) {
					if( Pan[i][j] == e.getSource() ) {
						row = i;
						col = j;
						break;
					}
				}
				if( row >= 0 ) break;
			}

			if( row >= 0 && col >= 0 ) {
				setBomb( row, col );

				if( getBombCount() == 0 ) {
					if( LevelCount == 10 ) {
						setScore();
						showState();
						if( JOptionPane.showConfirmDialog( this, "축하합니다. !!\n게임의 모든 판을 통과하였습니다.\n다시하시겠습니까" ) == JOptionPane.YES_OPTION ) {
							cl.first( this );
							return;
						}
					}
					else {
						setScore();
						LevelCount++;
						setPan();
						showState();
					}
				}
				else {
					Turn++;
					showState();
				}
			}

			if( Turn > MaxTurn && JOptionPane.showConfirmDialog( this, "안타깝네요. !!\n게임의 지정 턴을 초과했습니다.\n다시하시겠습니까" ) == JOptionPane.YES_OPTION ) {
					cl.first( this );
					return;
			}
		}
	}

	public void readyGame() {
		LevelCount = 1;
		RemainBomb = 0;
		Score = 0;
		Turn = 0;
		MaxTurn = 0;
		initPan();
		setPan();
		showState();
	}

	public void initPan() {
		for( int i = 0 ; i < 10 ; i++ ) {
			for( int j = 0 ; j < 10 ; j++ ) {
				Pan[i][j].setText( " " );
			}
		}
	}

	public void setPan() {
		int row, col;
		Random r = new Random();

		initPan();
		for( int i = 0 ; i < LevelCount ; i++ ) {
			row = Math.abs( r.nextInt() % 10 );
			col = Math.abs( r.nextInt() % 10 );
			setBomb( row, col );
		}
		getBombCount();
		Turn = 1;
		MaxTurn = LevelCount * 2;
	}

	public void setBomb( int row, int col ) {
		for( int i = 0 ; i < 10 ; i++ ) {
			if( Pan[i][col].getText() == " " ) Pan[i][col].setText( "●" );
			else Pan[i][col].setText( " " );
		}
		for( int i = 0 ; i < 10 ; i++ ) {
			if( Pan[row][i].getText() == " " ) Pan[row][i].setText( "●" );
			else Pan[row][i].setText( " " );
		}
		if( Pan[row][col].getText() == " " ) Pan[row][col].setText( "●" );
		else Pan[row][col].setText( " " );
	}

	public void showState() {
		txtLevelCount.setText( new Integer( LevelCount ).toString() );
		txtRemainBomb.setText( new Integer( RemainBomb ).toString() );
		txtScore.setText( new Integer( Score ).toString() );
		txtTurn.setText( new Integer( Turn ).toString() + "/" + new Integer( MaxTurn ).toString() );
	}

	public int getBombCount() {
		int cnt = 0;
		for( int i = 0 ; i < 10 ; i++ ) {
			for( int j = 0 ; j < 10 ; j++ ) {
				if( Pan[i][j].getText() == "●" ) cnt++;
			}
		}
		return RemainBomb = cnt;
	}

	public int setScore() {
		if( RemainBomb == 0 ) {
			Score += ( MaxTurn - Turn ) * 100 + 10;
		}
		return Score;
	}
}