import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GameInterface extends Frame implements WindowListener { // Text Field, Button, CheckBox private TextField [] NumBox = new TextField [81]; private Button doneButton = new Button("Check"); private Button startButton = new Button("Start"); private Button submitButton= new Button("È®ÀÎ"); private CheckboxGroup levelChG = new CheckboxGroup(); private Checkbox level1Ch = new Checkbox("LV1", true, levelChG); private Checkbox level2Ch = new Checkbox("LV2", false, levelChG); private Checkbox level3Ch = new Checkbox("LV3", false, levelChG); private Dialog dialog = null; // ¹Ú½º »ö ÁöÁ¤ private static Color spaceBoxCol = (Color.orange); private static Color spaceTextCol = (Color.white); private static Color staticBoxCol = (Color.pink); private static Color staticTextCol = (Color.darkGray); private static Color inputBoxCol = (Color.magenta); private static Color inputTextCol = (Color.black); private static Color errorBoxCol = (Color.yellow); private static Color errorTextCol = (Color.black); private static Color correctBoxCol = (Color.pink); private static Color correctTextCol = (Color.red); private SudokuAlgo algo; class AcListener implements ActionListener { public void actionPerformed( ActionEvent event ) { // Á¦¾î ¹öÆ° ´©¸¦ ¶§ ¹ß»ýÇÏ´Â À̺¥Æ® if( event.getSource() == doneButton ) { // üũ ¹öÆ°À¸·Î ¿Ï·á½ÃÁ¡À̳ª Ʋ¸° °ÍÀÌ ¾ø³ª °Ë»çÇÏ´Â ºÎºÐ System.out.println( "CheckButton" ); int leftBox = checkResult(); if( leftBox > 0 ) confirmDialog( leftBox+"°³ ³²¾Ò½À´Ï´Ù." ); else confirmDialog( "¸ðµÎ ¸ÂÃ߼̽À´Ï´Ù." ); } else if( event.getSource() == submitButton ) { // Dialog È®ÀÎ ¹öÆ° dialog.setVisible( false ); } else if( event.getSource() == startButton ) { // °ÔÀÓÀ» ½ÃÀÛÇÏ´Â ºÎºÐ Checkbox curChBox = levelChG.getSelectedCheckbox(); int level = 0; System.out.println( "startButton" ); // ÇöÀç ¼±ÅõǾî ÀÖ´Â ·¹º§¿¡ µû¶ó ³­À̵µ °áÁ¤ if( curChBox == level1Ch ) level = algo.level1; else if( curChBox == level2Ch ) level = algo.level2; else level = algo.level3; startGame( level ); } else // ºóÄ­¿¡ ¼ýÀÚ ³Ö´Â À̺¥Æ® { TextField SourceNum = (TextField)event.getSource(); // System.out.println( SourceNum.getText() ); SourceNum.selectAll(); } } } public GameInterface( SudokuAlgo algo ) { int i = 0; this.algo = algo; this.addWindowListener(this); this.setTitle("Sudoku Game"); // 9x9 ¹Ú½º È­¸é ±¸¼º Panel numBoxP = new Panel( new GridLayout(algo.totalWidthCell,algo.totalHeightCell) ); numBoxP.setFont(new Font("SansSerif", Font.BOLD, 26)); for( i = 0 ; i < algo.totalCell ; i++ ) { NumBox[i] = new TextField( 1 ); NumBox[i].setColumns( 1 ); NumBox[i].setSize( 4, 4 ); NumBox[i].setBackground( staticBoxCol ); NumBox[i].setForeground( staticTextCol ); // À̺¥Æ® ¿¬°á NumBox[i].addActionListener( new AcListener() ); numBoxP.add("Center", NumBox[i] ); } // Logo String title = "½ÃÁð±Ç ³»³ö!", subTitle1 = "by", subTitle2 = "sayrin"; for( i = 0 ; i < title.length() ; i++ ) NumBox[1+i+3*9].setText( title.substring(i, i+1) ); for( i = 0 ; i < subTitle1.length() ; i++ ) NumBox[1+i+5*9].setText( subTitle1.substring(i, i+1) ); for( i = 0 ; i < subTitle2.length() ; i++ ) NumBox[3+i+6*9].setText( subTitle2.substring(i, i+1) ); // Á¦¾î ¹öÆ° ±¸¼º //Panel ctrlP = new Panel( new BorderLayout() ); Panel ctrlP = new Panel( new GridLayout(1,3) ); // ·¹º§ ¼±Åà ¹öÆ° ctrlP.add( level1Ch ); ctrlP.add( level2Ch ); ctrlP.add( level3Ch ); // ½ÃÀÛ ¹öÆ° ctrlP.add( startButton ); startButton.addActionListener( new AcListener() ); // ¿Ï·á ¹öÆ° ctrlP.add( doneButton ); doneButton.addActionListener( new AcListener() ); this.setLayout( new FlowLayout(FlowLayout.CENTER, 100, 10) ); this.add( numBoxP ); this.add( ctrlP ); } // Ä­¿¡ ¼ýÀÚ¸¦ ä¿ò public void setNumberField( int index, int number ) { String strNum = Integer.toString( number ); if( number == 0 || number > algo.blockCell ) return; NumBox[index].setText( strNum ); NumBox[index].validate(); NumBox[index].setEditable( false ); } // Á¤´ä°ú ºñ±³Çغ½ public int checkResult() { int i = 0; String num = null; int leftBox = 0; for( i = 0 ; i < algo.totalCell ; i++ ) { if( algo.getBlindNumber(i) != 0 ) continue; num = Integer.toString( algo.getOriginalNumber(i) ); System.out.println(NumBox[i].getText()); if( num.compareTo(NumBox[i].getText()) != 0 ) // error { // Ʋ·ÈÀ» °æ¿ì 'E'¸¦ Ç¥½ÃÇÑ´Ù. NumBox[i].setText( "E" ); NumBox[i].setBackground( errorBoxCol ); NumBox[i].setForeground( errorTextCol ); NumBox[i].validate(); leftBox++; } else // correct { NumBox[i].setBackground( correctBoxCol ); NumBox[i].setForeground( correctTextCol ); NumBox[i].setEditable( false ); } } return leftBox; } // ³­À̵µ¸¦ ¼±Åà ÇßÀ» °æ¿ì ¹®Á¦ ÃâÁ¦ public void startGame( int level ) { // ¸Ê »ý¼º algo.autoMakeMap( level ); // ¸Ê Ãâ·Â algo.printOriginalMap(); // ¸Ê ºóÄ­¿¡ ÇöÀç ¹®Á¦¸¦ Àû¿ë½ÃÅ´ int index = 0, data = 0; for( index = 0 ; index < algo.totalCell ; index++ ) { data = algo.getBlindNumber( index ); if( data == 0 ) { NumBox[index].setText( "" ); NumBox[index].setBackground( spaceBoxCol ); NumBox[index].setForeground( spaceTextCol ); NumBox[index].setEditable( true ); continue; } NumBox[index].setText( Integer.toString(data) ); NumBox[index].setBackground( staticBoxCol ); NumBox[index].setForeground( staticTextCol ); NumBox[index].setEditable( false ); } } // ¸Þ½ÃÁö ¹Ú½º public void confirmDialog( String msg ) { if( "".equals( msg ) == false ) { dialog = new Dialog(new Frame(),"È®ÀÎ"); // Áß¾Ó¿¡ ¶ç¿ì±â dialog.setBounds(this.getX()+(this.getSize().width/2-100), this.getY() + (this.getSize().height/2-50), 200, 100); Panel p = new Panel(); p.add( new Label(msg) ); p.add( submitButton ); submitButton.addActionListener( new AcListener() ); dialog.add( p ); dialog.pack(); dialog.setVisible( true ); } } // Á¾·á À̺¥Æ® public void windowClosing( WindowEvent e ) { System.exit(0); } public void windowActivated( WindowEvent e ) {} public void windowClosed( WindowEvent e ) {} public void windowDeactivated( WindowEvent e ) {} public void windowDeiconified( WindowEvent e ) {} public void windowIconified( WindowEvent e ) {} public void windowOpened( WindowEvent e ) {} }