|
作者: jtm [jtm] 论坛用户 | 登录 |
我想编一个电脑和你对下,但一时又不知怎么编!!! 希望高手给指点一下!!!! |
地主 发表时间: 04/05 20:56 |
回复: bailove [bailove] 论坛用户 | 登录 |
// JDK1.0下编译通过。不明之处E-Mail to:link@163.net import java.applet.*; import java.awt.*; import java.net.*; import java.lang.Math; import IDD_DIALOG_MSG; //============================================================================== // Main Class for applet fiveGame // //============================================================================== public class fiveGame extends Applet implements Runnable { // THREAD SUPPORT: // m_fiveGame is the Thread object for the applet //-------------------------------------------------------------------------- Thread m_fiveGame = null; char m_15x15[][] = new char[15][15]; int m_MsgCount=0; char m_Side = 'w'; char m_TempSide = 'w'; boolean m_End = false; boolean m_IsApplet = true; int m_X = 0; int m_Y = 0; int m_OX = 0; int m_OY = 0; int WF0_1=2; int WF0_2=50; int WF0_3=1800; int WF0_4=19000; int WF1_1=1; int WF1_2=20; int WF1_3=150; int WF1_4=1200; int WF2_3=1000; int WF2_4=1000; int WF5=300000; Image BlackImage; Image WhiteImage; Button m_Button = new Button("开始"); Checkbox m_Checkbox = new Checkbox("先走"); List m_List = new List(); // fiveGame Class Constructor //-------------------------------------------------------------------------- public fiveGame(){ setLayout(null); m_Button.reshape(16*20+5,30,60,30); m_Checkbox.reshape(16*20+5,70,60,20); m_List.reshape(20,30+15*19+10,15*24,65 ); add(m_Button); add(m_Checkbox); add(m_List); initGame(); // m_Button.show(); // TODO: Add constructor code here } //init game //------------------------------------------------------------------------- private void initGame(){ m_End = false; for( int i=0;i<15;i++ ){ for( int j=0;j<15;j++ ){ m_15x15[i][j] = 'n'; } } if( m_TempSide == 'w' ){ double di,dj; di = 0; dj = 0; while( di <= 5.0||di >= 10.0 ) di = 14.0*Math.random(); while( dj <= 5.0||dj >= 10.0 ) dj = 14.0*Math.random(); m_15x15[(int)di][(int)dj] = m_TempSide=='w'?'b':'w'; m_OX = (int)di; m_OY = (int)dj; } m_Side = m_TempSide; Message("游戏开始!"); } public boolean handleEvent(Event evt ){ // if( evt.target instanceof Button ){ // if( ((Button)evt.target).getLabel() == "Ok" ){ // ((Button)evt.target).setLabel("Cancel"); // }else if( ((Button)evt.target).getLabel() == "Cancel" ){ // ((Button)evt.target).setLabel("Ok"); // } // } if( evt.target.hashCode() == m_Button.hashCode() ){ // m_Button.setLabel("OK"); Message(evt.toString()); if( evt.id == Event.GOT_FOCUS ){ initGame(); repaint(); } }else if( evt.target.hashCode() == m_Checkbox.hashCode() ){ if( m_Checkbox.getLabel() == "先走" ){ m_Checkbox.setLabel("后走"); m_TempSide = 'w'; }else{ m_Checkbox.setLabel("先走"); m_TempSide = 'b'; } }else if( evt.target.hashCode() == m_List.hashCode() ){ if( evt.id == Event.LIST_SELECT ){ Dialog d = new Dialog(null,true); IDD_DIALOG_MSG dlg = new IDD_DIALOG_MSG( d ); dlg.CreateControls(); d.show(); } }else{ if( evt.target.hashCode() == hashCode() ){ if( evt.id == Event.WINDOW_DESTROY ){ System.exit(0); } } } return super.handleEvent( evt ); } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: fiveGame\r\n" + "Author: Luck\r\n" + "Created with Microsoft Visual J++ Version 1.0"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { // If you use a ResourceWizard-generated "control creator" class to // arrange controls in your applet, you may want to call its // CreateControls() method from within this method. Remove the following // call to resize() before adding the call to CreateControls(); // CreateControls() does its own resizing. //---------------------------------------------------------------------- resize(400, 400); String param; if( m_IsApplet ){ param = getParameter("white"); BlackImage=getImage( getCodeBase(),param); }else{ param = "yellow.jpg"; BlackImage=getToolkit().getImage( param); } if( m_IsApplet ){ param = getParameter("black"); WhiteImage=getImage( getCodeBase(),param); }else{ param = "blue.jpg"; WhiteImage=getToolkit().getImage( param); } m_Checkbox.setBackground( Color.lightGray ); initGame(); // TODO: Place additional initialization code here } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { // TODO: Place applet cleanup code here } // fiveGame Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { // TODO: Place applet paint code here //g.drawString("Running: " + Math.random(), 10, 20); int x,y,i,j; if( !m_IsApplet ){ g.setColor( Color.lightGray ); g.fillRect(0,0,400,430); } x=30; y=30; for( i=0;i<15;i++ ){ g.setColor( Color.darkGray ); g.drawString(""+(i+1),5,y+i*20+5); g.setColor( Color.white ); g.drawString(""+(i+1),5+1,y+i*20+5+1); drawSlidLine(g,x,y+i*20,x+14*20,y+i*20); } for( i=0;i<15;i++ ){ g.setColor(Color.white); g.drawString(""+(i+1),x+i*20-5,15); g.setColor( Color.darkGray ); g.drawString(""+(i+1),x+i*20-5+1,15+1); drawSlidLine(g,x+i*20,y,x+i*20,y+14*20); } for( i=0;i<15;i++ ){ for( j=0;j<15;j++ ){ if( i==m_OX&&j==m_OY ){ g.setColor( Color.white ); g.drawRect(x+i*20-9,y+j*20-9,17,17); g.setColor( Color.darkGray ); g.drawRect(x+i*20-8,y+j*20-8,17,17); } drawSlidPoint( g,x+i*20,y+j*20,m_15x15[i][j] ); } } } // fiveGame drawSliodLine //-------------------------------------------------------------------------- private void drawSlidLine( Graphics g,int x1,int y1,int x2,int y2 ){ g.setColor( Color.darkGray ); g.drawLine(x1,y1,x2,y2); g.setColor( Color.white ); g.drawLine(x1+1,y1+1,x2+1,y2+1); } private void drawSlidPoint( Graphics g,int x,int y,char c ){ if( c=='n' ) return; if( c=='w' ){ g.drawImage( WhiteImage,x-5,y-5,new Color(0,0,0),this ); //g.drawArc( x-5,y-5,10,10,0,360 ); }else if( c=='b' ){ g.drawImage( BlackImage,x-5,y-5,new Color(0,0,0),this ); //g.drawRect( x-5,y-5,10,10 ); } } // The start() method is called when the page containing the applet // first appears on the screen. The AppletWizard's initial implementation // of this method starts execution of the applet's thread. //-------------------------------------------------------------------------- public void start(){ // if (m_fiveGame == null) // { // m_fiveGame = new Thread(this); // m_fiveGame.start(); // } // TODO: Place additional applet start code here } // The stop() method is called when the page containing the applet is // no longer on the screen. The AppletWizard's initial implementation of // this method stops execution of the applet's thread. //-------------------------------------------------------------------------- public void stop() { // if (m_fiveGame != null) // { // m_fiveGame.stop(); // m_fiveGame = null; // } // TODO: Place additional applet stop code here } // THREAD SUPPORT // The run() method is called when the applet's thread is started. If // your applet performs any ongoing activities without waiting for user // input, the code for implementing that behavior typically goes here. For // example, for an applet that performs animation, the run() method controls // the display of images. //-------------------------------------------------------------------------- public void run() { while (true) { try { //repaint(); // TODO: Add additional thread-specific code here //Thread.sleep(50); } catch (InterruptedException e) { // TODO: Place exception-handling code here in case an // InterruptedException is thrown by Thread.sleep(), // meaning that another thread has interrupted this one stop(); } } } private boolean isGrid( int x,int y ){ if( x < 25||x> 30+15*20+5 ) return false; if( y < 25||y> 30+15*20+5 ) return false; float m,n; int mi,ni; m = ((float)x-30.0f)/20.0f; n = ((float)y-30.0f)/20.0f; mi=(int)m; ni=(int)n; m-=mi; n-=ni; if( m > 0.7 ){ m_X = mi+1; }else if( m < 0.3 ){ m_X = mi; }else return false; if( n > 0.7 ){ m_Y = ni+1; }else if( n < 0.3 ){ m_Y = ni; }else return false; return true; } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { // TODO: Place applet mouseDown code here if( m_End == true ) return true; if( isGrid( x,y ) == true ){ if( m_15x15[m_X][m_Y] == 'n' ){ m_15x15[m_X][m_Y] = m_Side; repaint(20+m_OX*20,20+m_OY*20,20,20); m_OX=m_X; m_OY=m_Y; repaint(20+m_X*20,20+m_Y*20,20,20); if( testGame( m_15x15 ) == true ){ comPlay( m_15x15,m_Side=='w'?'b':'w' ); m_15x15[m_X][m_Y] = m_Side=='w'?'b':'w'; repaint(20+m_OX*20,20+m_OY*20,20,20); m_OX=m_X; m_OY=m_Y; repaint(20+m_X*20,20+m_Y*20,20,20); testGame( m_15x15 ); } }else{ Message("此处以有棋子!"); } }else{ Message("请往棋盘交点上点!"); } return true; } // MOUSE SUPPORT: // The mouseUp() method is called if the mouse button is released // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseUp(Event evt, int x, int y) { // TODO: Place applet mouseUp code here return true; } public boolean mouseMove(Event evt,int x,int y ){ // if( isGrid( x,y ) == true ){ // repaint(20+m_OX*20,20+m_OY*20,20,20); // m_OX=m_X; // m_OY=m_Y; // repaint(20+m_X*20,20+m_Y*20,20,20); // } return true; } private int Dump( int x ,int Wf ){ if( Wf == 1 ){ switch(x){ case 1:return WF0_1; case 2:return WF0_2; case 3:return WF0_3; case 4:return WF0_4; } }else if( Wf == 0 ){ switch(x){ case 1:return WF1_1; case 2:return WF1_2; case 3:return WF1_3; case 4:return WF1_4; } }else if( Wf == 2 ){ if( x == 4 ) return WF2_4; if( x == 3 ) return WF2_3; }if( x == 5 ){ return WF5; } return -1; } private long SreachLine( char Fline[],int M,char Nf ){ char Wf; int i = 0; int j = 0; int n = 0; int k = 0; long count = 0; if( Nf == 'b' ) Wf = 'w'; else Wf = 'b'; try{ while( M - j >= 5 ){ while( i+j < M && Fline[ i+j ] != Wf ) i++; if( i > 4 ){ for( k = 0; k < i; k++ ){ n = 0; while( k < i && Fline[ j+k ] == Nf ){ n++; k++; } if( n>0 ){ //??????? if( n > 4 ) return -1; //发现5子 if( n == k || k == i ) Wf = 0; else Wf = 1; if( n == 2 && Wf == 1 ) if((k+1<i&&Fline[ j+k+1 ] == Nf)||( k>3&&Fline[ j+k-4 ]==Nf)){ n++; Wf = 2; } if( n == 3 && Wf == 0 ) if((k+1<i&&Fline[ j+k+1 ] == Nf)||( k>4&&Fline[ j+k-5 ]==Nf)){ n++; Wf = 2; } try{ count = count + Dump( n , Wf ); }catch( Exception e ){ Message("Dump @ "+e.toString() ); } } } } j = j + i + 1; i = 0; } }catch( Exception e ){ Message( "SrechLine @ "+e.toString() ); } return count; } private long SreachArea( char Area[][],char Nf ){ int i,j; long count = 0; long cbak=0; char Fline[]=new char[15]; try{ for( i = 0; i < 15; i++ ){ for( j = 0; j < 15; j++ ) Fline[j] = Area[i][j]; cbak = SreachLine( Fline,15,Nf ); if( cbak == -1 ) return -1; else count = count + (long)cbak; } for( i = 0; i < 15; i++ ){ for( j = 0; j < 15; j++ ) Fline[j] = Area[j][i]; cbak = SreachLine( Fline,15,Nf ); if( cbak == -1 ) return -1; else count = count + (long)cbak; } for( i = 0; i < 15 - 4; i++ ){ for( j = 0; j < 15 - i; j++ ) Fline[j] = Area[i+j][j]; cbak = SreachLine( Fline, 15-i, Nf ); if( cbak == -1 ) return -1; else count = count + (long)cbak; } for( i = 15-1; i > 3; i-- ){ for( j = 0; j < i+1; j++ ) Fline[j] = Area[i-j][j]; cbak = SreachLine( Fline, i+1, Nf ); if( cbak == -1 ) return -1; else count = count + (long)cbak; } for( i = 1; i < 15 - 4; i++ ){ for( j = 0; j < 15 - i; j++ ) Fline[j] = Area[i+j][15-j-1]; cbak = SreachLine( Fline, 15-i, Nf ); if( cbak == -1 ) return -1; else count = count + (long)cbak; } for( i = 15-1-1; i > 3; i-- ){ for( j = 0; j < i+1; j++ ) Fline[j] = Area[i-j][15-j-1]; cbak = SreachLine( Fline, i+1, Nf ); if( cbak == -1 ) return -1; else count = count + (long)cbak; } }catch( Exception e ){ Message( "SreachArea @ "+e.toString() ); } return count; } private void Message( String msg ){ m_List.addItem( m_MsgCount+"-"+msg ); m_MsgCount++; try{ while( m_List.countItems() > 101 ){ m_List.delItem( 0 ); } }catch( Exception e ){ } int i = m_List.countItems(); if( i > 0 ) m_List.makeVisible(i-1); } private boolean testGame( char Area[][] ){ long va; Message("testGame Start"); try{ if( (va=SreachArea( Area,m_Side )) == -1 ){ Message("你赢,单击<开始>!"); m_End = true; return false; } Message("w return value="+va); if( (va=SreachArea( Area,m_Side=='w'?'b':'w' )) == -1 ){ Message("你输,单击<开始>!"); m_End = true; return false; }else{ Message("b return value="+va); } }catch( Exception e ){ Message("testGame @ "+e.toString() ); } return true; } private boolean testWin( char Area[][],char Nf ){ int i,j,m,n,h,k; char Mf; Mf = Nf=='w'?'b':'w'; for( i=0;i<15;i++ ){ for( j=0;j<15;j++ ){ if( Area[i][j] == 'n' ){ Area[i][j] = Nf; if( testLost( Area,Mf ) ){ m_X = i; m_Y = j; Area[i][j] = 'n'; return true; } Area[i][j] = 'n'; } } } return false; } private boolean testLost( char Area[][],char Nf ){ int i,j,v; char Mf; Mf = Nf=='w'?'b':'w'; v = 0; for( i=0;i<15;i++ ){ for( j=0;j<15;j++ ){ if( Area[i][j] == 'n' ){ Area[i][j] = Mf; if( SreachArea( Area,Mf ) == -1 ){ Area[i][j] = 'n'; if( v>=1 ) return true; v++; continue; } Area[i][j] = 'n'; } } } return false; } private void comPlay( char Area[][],char Nf ){ int i,j,mi,mj; char Mf; long mvalue,nvalue,maxvalue; long mvalue0,nvalue0,value; mj = mi = 0; value = mvalue0 = nvalue0 = mvalue = nvalue = 0; maxvalue = Long.MIN_VALUE; Mf = Nf=='w'?'b':'w'; for( i=0;i<15;i++ ){ for( j=0;j<15;j++ ){ if( Area[i][j] == 'n' ){ Area[i][j] = Nf; if( SreachArea( Area,Nf ) == -1 ){ Area[i][j] = 'n'; m_X = i; m_Y = j; return; } Area[i][j] = 'n'; } } } for( i=0;i<15;i++ ){ for( j=0;j<15;j++ ){ if( Area[i][j] == 'n' ){ Area[i][j] = Mf; if( SreachArea( Area,Mf ) == -1 ){ Area[i][j] = 'n'; m_X = i; m_Y = j; return; } Area[i][j] = 'n'; } } } // if( testWin( Area,Nf ) == true ){ // return; // } mvalue0 = SreachArea( Area,Mf ); nvalue0 = SreachArea( Area,Nf ); for( i=0;i<15;i++ ){ for( j=0;j<15;j++ ){ if( Area[i][j] == 'n' ){ Area[i][j] = Nf; nvalue = SreachArea( Area,Nf ); if( nvalue == -1 ) nvalue = WF5; Area[i][j] = Mf; mvalue = SreachArea( Area,Mf ); if( mvalue == -1 ) mvalue = WF5; nvalue -= nvalue0; mvalue -= mvalue0; value = nvalue+mvalue; if( value > maxvalue ){ mi = i; mj = j; maxvalue = value; } Area[i][j] = 'n'; } } } m_X = mi; m_Y = mj; return; } // TODO: Place additional applet code here public static void main(String args[]) { Frame f = new Frame("五子棋"); fiveGame drawTest = new fiveGame(); drawTest.m_IsApplet = false; drawTest.init(); drawTest.start(); f.add("Center", drawTest); f.resize(400, 430); f.show(); } } //------------------------------------------------------------------------------ // IDD_DIALOG_MSG.java: // Implementation of "control creator" class IDD_DIALOG_MSG //------------------------------------------------------------------------------ import java.awt.*; import DialogLayout; public class IDD_DIALOG_MSG { Container m_Parent = null; boolean m_fInitialized = false; DialogLayout m_Layout; // Control definitions //-------------------------------------------------------------------------- Button IDOK; Label IDC_STATIC1; // Constructor //-------------------------------------------------------------------------- public IDD_DIALOG_MSG (Container parent) { m_Parent = parent; } // Initialization. //-------------------------------------------------------------------------- public boolean CreateControls() { // CreateControls should be called only once //---------------------------------------------------------------------- if (m_fInitialized || m_Parent == null) return false; // m_Parent must be extended from the Container class //---------------------------------------------------------------------- if (!(m_Parent instanceof Container)) return false; // Since a given font may not be supported across all platforms, it // is safe to modify only the size of the font, not the typeface. //---------------------------------------------------------------------- Font OldFnt = m_Parent.getFont(); if (OldFnt != null) { Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 10); m_Parent.setFont(NewFnt); } // All position and sizes are in dialog logical units, so we use a // DialogLayout as our layout manager. //---------------------------------------------------------------------- m_Layout = new DialogLayout(m_Parent, 271, 64); m_Parent.setLayout(m_Layout); m_Parent.addNotify(); Dimension size = m_Layout.getDialogSize(); Insets insets = m_Parent.insets(); m_Parent.resize(insets.left + size.width + insets.right, insets.top + size.height + insets.bottom); // Control creation //---------------------------------------------------------------------- IDOK = new Button ("OK"); m_Parent.add(IDOK); m_Layout.setShape(IDOK, 114, 43, 50, 14); IDC_STATIC1 = new Label ("", Label.CENTER); m_Parent.add(IDC_STATIC1); m_Layout.setShape(IDC_STATIC1, 7, 15, 257, 18); m_fInitialized = true; return true; } } |
B1层 发表时间: 04/05 21:44 |
回复: bailove [bailove] 论坛用户 | 登录 |
从上面可知道. 编程是要一个一个的脚印来的.初学看都不懂是吗?我也是 前几天我编好了"锤子.剪刀.布"的游戏 |
B2层 发表时间: 04/05 21:46 |
回复: jtm [jtm] 论坛用户 | 登录 |
这是C吧,小弟只是拿QB编的!!! 小平不同,小弟不行!!! |
B3层 发表时间: 04/05 21:52 |
回复: bailove [bailove] 论坛用户 | 登录 |
是用JDK1.0 但原理懂的一切好办 |
B4层 发表时间: 04/05 22:05 |
回复: jtm [jtm] 论坛用户 | 登录 |
我想知道你的构思!!!! |
B5层 发表时间: 04/05 22:09 |
回复: jtm [jtm] 论坛用户 | 登录 |
哪位大哥给我讲一下原理!!!!!! 谢谢!!!!! |
B6层 发表时间: 04/07 11:05 |
回复: jtm [jtm] 论坛用户 | 登录 |
哪位大虾行行好 就教教我吧!!! 告诉我它判断的原理!!!? |
B7层 发表时间: 04/08 20:14 |
回复: penfe [penfe] 论坛用户 | 登录 |
JDK1.0是什么啊,是不是编译器啊? |
B8层 发表时间: 04/09 12:59 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号