이메일

구글 : kdj777ehdwns@gmail.com
네이버 : kdj777ehdwns@naver.com
epsckr2016.blogspot.com

2016년 9월 29일 목요일

NumBaseball 0단계,1단계

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class NumBaseball extends Frame implements ActionListener{
 Label disp;
 TextArea rec;
 Panel numPanel;
 Button[] btns = new Button[ 12 ];
 int[] com = new int[ 3 ];
 int[] usr = new int[ 3 ];
 int cnt, scnt, bcnt;
 boolean gameover;
 int a=0;  

 public static void main(String[] args) {
  NumBaseball f = new NumBaseball ( "숫자야구 Ver 1.0");
  f.setSize( 500,500 );
  f.setVisible( true );
 }
 public NumBaseball( String title) {
  super( title );

  disp = new Label();
  disp.setAlignment( Label.RIGHT );
  add( "North", disp );

  numPanel = new Panel();
  numPanel.setLayout( new GridLayout( 4,3 ) );
  for( int i = 7; i > 0; i -= 3 ) {
   for( int j = 0; j < 3; j++ ) {
    btns[ i + j ] = new Button( String.valueOf( i + j ));
    numPanel.add( btns[ i + j ] );
   }
  }
  btns [ 0 ] = new Button( "←" );
  numPanel.add( btns[ 0 ]);
  btns [ 10 ] = new Button( "다시" );
  numPanel.add( btns[ 10 ]);
  btns [ 11 ] = new Button( "완료" );
  numPanel.add( btns[ 11 ]);
  add( "Center", numPanel );

  rec = new TextArea( 10,20 );
  add( "South", rec );

  for( int i = 0; i < 12; i++ ) {
   btns[ i ].addActionListener( this );
  }

  Random r = new Random();
  com[ 0 ] = Math.abs(r.nextInt() % 9 ) + 1;
  do {
   com[ 1 ] = Math.abs(r.nextInt() % 9 ) + 1;
  } while( com[ 0 ] == com[ 1 ] );
  do {
   com[ 2 ] = Math.abs(r.nextInt() % 9 ) + 1;
  } while( com[0] == com[2] || com[1]==com[2] );
      
  cnt = 0; scnt = 0; bcnt = 0;
  gameover = false;
 }

    public void actionPerformed( ActionEvent e ) {
     if ( gameover ) return;
   
     if( e.getSource() == btns [ 0 ] ) {
      // ← 버튼 눌렀을때
      if( cnt > 0 ) {
       cnt--;
       disp.setText( "" );
       for( int i = 0; i < cnt; i++ ) {
        disp.setText( disp.getText() + usr[ i ] );
       }
      }
     } else if( e.getSource() == btns[ 10 ] ) {
      // 다시 버튼을 눌렀을 때
      disp.setText( "" );
      usr[ 0 ] = usr[ 1 ] = usr[ 2 ] = 0;
      cnt = 0;
   
     } else if( e.getSource() == btns[ 11 ] ) {
      // 완료 버튼을 눌렀을 때
      if( cnt ==3 ) {
       scnt = 0; bcnt = 0;
       if( usr[ 0 ] == com[ 0 ] ) scnt++;
       if( usr[ 1 ] == com[ 1 ] ) scnt++;
       if( usr[ 2 ] == com[ 2 ] ) scnt++;
   
       if( usr[ 0 ] == com[ 1 ] ) bcnt++;
       if( usr[ 0 ] == com[ 2 ] ) bcnt++;
       if( usr[ 1 ] == com[ 0 ] ) bcnt++;
       if( usr[ 1 ] == com[ 2 ] ) bcnt++;
       if( usr[ 2 ] == com[ 0 ] ) bcnt++;
       if( usr[ 2 ] == com[ 1 ] ) bcnt++;
   
       rec.append( usr[0] + "" + usr[1] + "" +
              usr[2] + "  → " +
                   scnt + "S"+bcnt + "B\n");
       a++; 
                cnt = 0;
                usr[ 0 ] = usr[ 1 ] = usr[ 2 ] = 0;
                disp.setText( "" );
               
                if( scnt == 3 ) {
                 //3개의 숫자를 다 맞추었을 때
                 gameover = true;
                 disp.setText("Game Over!!!");
                 rec.append( a+"번만에 맞추셨습니다"+"축하합니다.\n" );
                              
                }
      }
     } else {
      // 숫자 버튼을 눌렀을 때
      if( cnt < 3 ) {
       char btnVal = ((Button)e.getSource())
                  .getLabel().charAt( 0 );
       usr[ cnt ] = Integer.valueOf(
         String.valueOf( btnVal )).intValue();
       disp.setText(disp.getText() + btnVal );
       cnt++;
      }
     }
    }
}






날짜 : 2016년 09월 29일

댓글 없음:

댓글 쓰기