java小游戏例子--一个简单的黑白棋游戏

lmc1414 贡献于2017-03-09

作者 微软用户  创建于2011-12-02 13:13:00   修改者微软用户  修改于2011-12-02 13:34:00字数12027

文档摘要:
关键词:

 这是一段用java applet小应用程序开发的小游戏--例程演示,用于大家学习交流。 程序在线演示地址: http://www.xztc.edu.cn/java/play.htm import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.PrintStream; import java.net.URL; public class BlackVSWhite extends Applet implements Runnable,ActionListener,MouseListener { private MediaTracker imageTracker; private Image redImage; private Image blueImage; private Image offScrImage; private Graphics offScrGraphics; private Font f; private int fontHeight; private int fontAscent; private Button button; private int buttonHeight; private Choice choice; private Choice moveFirst; private boolean redMoveFirst; private boolean illegalMove; private boolean blueNotMoving; private boolean animating; private int redScore; private int blueScore; private Thread aThread; private static final int EMPTY = 0; private static final int RED = 2; private static final int BLUE = 3; private static final int BLUE_TEMP = 4; private static final int RED_TEMP = 5; private static final int VERYGOOD = 9999; private static int DIM = 10; private int boardSize = 400; private int[] grid = new int[400]; private synchronized void newGame() { DIM = (new Integer((String) choice.getSelectedItem())).intValue(); int whoMoveFirst=moveFirst.getSelectedIndex(); if(whoMoveFirst==0) redMoveFirst = true; else redMoveFirst = false; int n1; int n2; for( n1 = 0; n1 < DIM; ++n1 ) { for( n2 = 0; n2 < DIM; ++n2 ) grid[n1 * DIM + n2] = EMPTY; } n2 = DIM / 2; grid[(n2 - 1) * DIM + (n2 - 1)] = RED; grid[(n2 - 1) * DIM + n2] = BLUE; grid[n2 * DIM + (n2 - 1)] = BLUE; grid[n2 * DIM + n2] = RED; offScrGraphics.setColor( Color.white ); offScrGraphics.fillRect( 0, 0, boardSize, boardSize ); redScore = blueScore = 0; illegalMove = blueNotMoving = animating = false; boardSize = -1; repaint(); if( !redMoveFirst ) { blueMove(); checkScore(); repaint(); } } public synchronized void init() { aThread = null; imageTracker = new MediaTracker( this ); offScrImage = createImage( 400, 400 ); offScrGraphics = offScrImage.getGraphics(); f = new Font( "TimesRoman", 0, 12 ); offScrGraphics.setFont( f ); fontHeight = getFontMetrics( f ).getHeight(); fontAscent = getFontMetrics( f ).getAscent(); loadImages(); if( button == null ) { button = new Button( "新游戏" ); button.addActionListener(this); setLayout( new FlowLayout( ) ); add( button ); choice = new Choice(); choice.addItem( "6" ); choice.addItem( "8" ); choice.addItem( "10" ); choice.addItem( "12" ); choice.addItem( "14" ); add( choice ); choice.select( "10" ); } if( moveFirst == null ) { moveFirst = new Choice(); moveFirst.addItem( "玩家先走" ); moveFirst.addItem( "电脑先走" ); add( moveFirst ); moveFirst.select( 0 ); redMoveFirst = true; } addMouseListener(this); newGame(); } public synchronized void loadImages() { int i; try { redImage = getImage( getDocumentBase(), "p2.gif" ); blueImage = getImage( getDocumentBase(), "p1.gif" ); offScrGraphics.drawImage( redImage, 0, 0, this ); offScrGraphics.drawImage( blueImage, 0, 0, this ); } catch( Exception e1 ) { System.out.println( e1 ); } imageTracker.addImage( redImage, 0 ); imageTracker.addImage( blueImage, 1 ); for( i = 0; i < 2; ++i ) { try { imageTracker.checkID( i, true ); imageTracker.waitForID( i ); showStatus( "Loading image P" + i + ".gif" ); } catch( Exception e2 ) { System.out.println( e2 ); } } } public void start() { aThread = new Thread( this ); aThread.start(); imageTracker.statusID( 0, true ); imageTracker.statusID( 1, true ); } public void run() { for( ;; ) { try { Thread.sleep( 50L ); continue; } catch( Exception unused1 ) { continue; } } } /*public void stop() { if( aThread != null && aThread.isAlive() ) aThread.stop(); }*/ public void destroy() { } public synchronized void blueMove() { int num1 = -1; int num2 = -1; int num3 = -1; int num4 = -1; int num5; for( num5 = 0; num5 < DIM; ++num5 ) { int num6; for( num6 = 0; num6 < DIM; ++num6 ) { if( grid[num5 * DIM + num6] == 0 ) { num2 = isGood( num5, num6, 3, 2 ); if( num1 < num2 && num2 > 0 ) { num1 = num2; num3 = num5; num4 = num6; } } } } blueNotMoving = false; if( num1 > 0 ) { grid[num3 * DIM + num4] = BLUE; change( num3, num4, 3, 2, 4 ); } else blueNotMoving = true; } public synchronized int isGood(int num1, int num2, int num3, int num4) { int num6 = 0; int num5 = 0; int num7; int num8; int num9; int num10; for( num8 = num2 + 1; num8 < DIM; ++num8 ) { if( grid[num1 * DIM + num8] == num4 ) ++num5; else { if( grid[num1 * DIM + num8] == num3 ) break; if( grid[num1 * DIM + num8] == 0 ) { num5 = 0; break; } } } if( num8 >= DIM ) num5 = 0; num6 += num5; num5 = 0; for( num8 = num2 - 1; num8 >= 0; --num8 ) { if( grid[num1 * DIM + num8] == num4 ) ++num5; else { if( grid[num1 * DIM + num8] == num3 ) break; if( grid[num1 * DIM + num8] == 0 ) { num5 = 0; break; } } } if( num8 <= -1 ) num5 = 0; num6 += num5; num5 = 0; for( num7 = num1 + 1; num7 < DIM; ++num7 ) { if( grid[num7 * DIM + num2] == num4 ) ++num5; else { if( grid[num7 * DIM + num2] == num3 ) break; if( grid[num7 * DIM + num2] == 0 ) { num5 = 0; break; } } } if( num7 >= DIM ) num5 = 0; num6 += num5; num5 = 0; num7 = num1 - 1; while( num7 >= 0 ) { if( grid[num7 * DIM + num2] == num4 ) ++num5; if( grid[num7 * DIM + num2] == num3 ) break; if( grid[num7 * DIM + num2] == 0 ) { num5 = 0; break; } else --num7; } if( num7 <= -1 ) num5 = 0; num6 += num5; num5 = 0; num9 = num2 + 1; num10 = num1 - 1; while( num9 >= 0 && num10 >= 0 ) { if( grid[num10 * DIM + num9] == num4 ) ++num5; if( grid[num10 * DIM + num9] == num3 ) break; if( grid[num10 * DIM + num9] == 0 ) { num5 = 0; break; } else { ++num9; --num10; } } if( num9 >= DIM || num10 <= -1 ) num5 = 0; num6 += num5; num5 = 0; num9 = num2 + 1; for( num10 = num1 + 1; num9 < DIM && num10 < DIM; ++num10 ) { if( grid[num10 * DIM + num9] == num4 ) ++num5; else { if( grid[num10 * DIM + num9] == num3 ) break; if( grid[num10 * DIM + num9] == 0 ) { num5 = 0; break; } } ++num9; } if( num9 >= DIM || num10 >= DIM ) num5 = 0; num6 += num5; num5 = 0; num9 = num2 - 1; for( num10 = num1 - 1; num9 >= 0 && num10 >= 0; --num10 ) { if( grid[num10 * DIM + num9] == num4 ) ++num5; else { if( grid[num10 * DIM + num9] == num3 ) break; if( grid[num10 * DIM + num9] == 0 ) { num5 = 0; break; } } --num9; } if( num9 <= -1 || num10 <= -1 ) num5 = 0; num6 += num5; num5 = 0; num9 = num2 - 1; for( num10 = num1 + 1; num9 < DIM && num10 < DIM; ++num10 ) { if( grid[num10 * DIM + num9] == num4 ) ++num5; else { if( grid[num10 * DIM + num9] == num3 ) break; if( grid[num10 * DIM + num9] == 0 ) { num5 = 0; break; } } --num9; } if( num9 <= -1 || num10 >= DIM ) num5 = 0; num6 += num5; return num6; } public synchronized void change(int num1, int num2, int num3, int num4, int num5) { int num6 = 0; int num8 = num2 + 1; int num7; int num9; int num10; int num11; int num12; int num13; int num14; int num15; int num16; int num17; int num18; int num19; while( num8 < DIM ) { num7 = num1 * DIM + num8; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else ++num8; } if( num6 != 0 ) { for( num9 = num2 + 1; num9 < DIM; ++num9 ) { num7 = num1 * DIM + num9; if( grid[num7] == num4 ) grid[num7] = num5; else if( grid[num7] == num3 ) break; } } num6 = 0; num9 = num2 - 1; while( num9 >= 0 ) { num7 = num1 * DIM + num9; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else --num9; } if( num6 != 0 ) { for( num10 = num2 - 1; num10 >= 0; --num10 ) { num7 = num1 * DIM + num10; if( grid[num7] == num4 ) grid[num7] = num5; else if( grid[num7] == num3 ) break; } }  num6 = 0; num10 = num1 + 1; while( num10 < DIM ) { num7 = num10 * DIM + num2; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else ++num10; } if( num6 != 0 ) { for( num11 = num1 + 1; num11 < DIM; ++num11 ) { num7 = num11 * DIM + num2; if( grid[num7] == num4 ) grid[num7] = num5; else if( grid[num7] == num3 ) break; } } num6 = 0; num11 = num1 - 1; while( num11 >= 0 ) { num7 = num11 * DIM + num2; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else --num11; } if( num6 != 0 ) { for( num12 = num1 - 1; num12 >= 0; --num12 ) { num7 = num12 * DIM + num2; if( grid[num7] == num4 ) grid[num7] = num5; if( grid[num7] == num3 ) break; } } num6 = 0; num12 = num2 + 1; num13 = num1 - 1; while( num12 < DIM && num13 >= 0 ) { num7 = num13 * DIM + num12; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else { ++num12; --num13; } } if( num6 != 0 ) { num14 = num2 + 1; for( num15 = num1 - 1; num14 < DIM && num15 >= 0; --num15 ) { num7 = num15 * DIM + num14; if( grid[num7] == num4 ) grid[num7] = num5; if( grid[num7] == num3 ) break; ++num14; } } num6 = 0; num14 = num2 + 1; num15 = num1 + 1; while( num14 < DIM && num15 < DIM ) { num7 = num15 * DIM + num14; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else { ++num14; ++num15; } } if( num6 != 0 ) { num16 = num2 + 1; for( num17 = num1 + 1; num16 < DIM && num17 < DIM; ++num17 ) { num7 = num17 * DIM + num16; if( grid[num7] == num4 ) grid[num7] = num5; if( grid[num7] == num3 ) break; ++num16; } } num6 = 0; num16 = num2 - 1; num17 = num1 - 1; while( num16 >= 0 && num17 >= 0 ) { num7 = num17 * DIM + num16; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else { --num16; --num17; } } if( num6 != 0 ) { num18 = num2 - 1; for( num19 = num1 - 1; num18 >= 0 && num19 >= 0; --num19 ) { num7 = num19 * DIM + num18; if( grid[num7] == num4 ) grid[num7] = num5; if( grid[num7] == num3 ) break; --num18; } } num6 = 0; num18 = num2 - 1; num19 = num1 + 1; while( num18 >= 0 && num19 < DIM ) { num7 = num19 * DIM + num18; if( grid[num7] != num4 ) { if( grid[num7] == num3 ) num6 = 1; break; } else { --num18; ++num19; } } if( num6 != 0 ) { int num20 = num2 - 1; int num21; for( num21 = num1 + 1; num20 >= 0 && num21 < DIM; ++num21 ) { num7 = num21 * DIM + num20; if( grid[num7] == num4 ) grid[num7] = num5; if( grid[num7] == num3 ) break; --num20; } } animating = true; } public void checkScore() { int num2; redScore = blueScore = 0; for( num2 = 0; num2 < DIM; ++num2 ) { int num3; for( num3 = 0; num3 < DIM; ++num3 ) { int num1 = num2 * DIM + num3; if( grid[num1] == 2 || grid[num1] == 5 ) ++redScore; else if( grid[num1] == 3 || grid[num1] == 4 ) ++blueScore; } } } public synchronized void paint(Graphics g) { if( !imageTracker.checkAll() ) { g.setColor( getForeground() ); g.drawString( "Loading images", 50, 50 ); } else { int num2; int num3; int num4; int num5; int num6; buttonHeight = button.getSize().height + button.getLocation().y * 2; getFontMetrics( f ); num2 = getSize().width - fontHeight * 3 - buttonHeight; num3 = getSize().height - fontHeight * 3 - buttonHeight; if( num2 != boardSize ) { if( num2 > num3 ) boardSize = num3 / DIM * DIM; else boardSize = num2 / DIM * DIM; } if( boardSize > 400 ) boardSize = 400; offScrGraphics.setColor(getBackground() ); offScrGraphics.fillRect( 0, 0, getSize().width, getSize().height ); offScrGraphics.setColor( Color.white ); offScrGraphics.fillRect( 0, 0, boardSize, boardSize ); num4 = boardSize / DIM; offScrGraphics.setColor( Color.red ); for( num5 = 0; num5 < DIM; ++num5 ) { offScrGraphics.drawLine( 0, num5 * num4, boardSize - 1, num5 * num4 ); for( num6 = 0; num6 < DIM; ++num6 ) { offScrGraphics.drawLine( num6 * num4, 0, num6 * num4, boardSize - 1 ); if( grid[num5 * DIM + num6] == 2 ) offScrGraphics.drawImage( redImage, num6 * num4 + 1, num5 * num4 + 1, num4 - 2, num4 - 2, this ); else if( grid[num5 * DIM + num6] == 3 ) offScrGraphics.drawImage( blueImage, num6 * num4 + 1, num5 * num4 + 1, num4 - 2, num4 - 2, this ); } } offScrGraphics.drawLine( boardSize - 1, 0, boardSize - 1, boardSize - 1 ); offScrGraphics.drawLine( 0, boardSize - 1, boardSize - 1, boardSize - 1 ); offScrGraphics.setColor( getForeground() ); if( illegalMove ) offScrGraphics.drawString( "下子非法,请重新下子!", button.getLocation().x, boardSize + fontAscent ); else if( blueNotMoving ) offScrGraphics.drawString( "Computer Pass", button.getLocation().x, boardSize + fontAscent ); offScrGraphics.drawImage( redImage, button.getLocation().x, boardSize + fontHeight * 2 - fontAscent, fontAscent, fontAscent, this ); offScrGraphics.drawString( " 玩家: " + (String.valueOf( redScore )), button.getLocation().x + fontAscent, boardSize + fontHeight * 2 ); offScrGraphics.drawImage( blueImage, button.getLocation().x, boardSize + fontHeight * 3 - fontAscent, fontAscent, fontAscent, this ); offScrGraphics.drawString( " 电脑: " + (String.valueOf( blueScore )), button.getLocation().x + fontAscent, boardSize + fontHeight * 3 ); offScrGraphics.setColor( Color.white ); num6 = 0; if( animating ) { num6 = num4 - 2; while( num6 > 0 ) { int num7; for( num7 = 0; num7 < DIM; ++num7 ) { int num8; for( num8 = 0; num8 < DIM; ++num8 ) { if( grid[num7 * DIM + num8] == 5 ) { offScrGraphics.fillRect( num8 * num4 + 1, num7 * num4 + 1, num4 - 1, num4 - 1 ); offScrGraphics.drawImage( blueImage, num8 * num4 + 1 + num4 / 2 - num6 / 2, num7 * num4 + 1 + num4 / 2 - num6 / 2, num6, num6, this ); if( num6 <= 2 ) grid[num7 * DIM + num8] = 2; } else if( grid[num7 * DIM + num8] == 4 ) { offScrGraphics.fillRect( num8 * num4 + 1, num7 * num4 + 1, num4 - 1, num4 - 1 ); offScrGraphics.drawImage( redImage, num8 * num4 + 1 + num4 / 2 - num6 / 2, num7 * num4 + 1 + num4 / 2 - num6 / 2, num6, num6, this ); if( num6 <= 2 ) grid[num7 * DIM + num8] = 3; } } } g.drawImage( offScrImage, button.getLocation().x, buttonHeight, this ); num6 -= 3; try { Thread.sleep( 50L ); continue; } catch( Exception unused9 ) { continue; } } animating = false; paint( g ); } if((redScore+blueScore)==DIM*DIM ) { if(redScore!=blueScore) offScrGraphics.drawString((redScore>blueScore)?"You Win!":"You Lose!",button.getLocation().x, boardSize + fontAscent); else offScrGraphics.drawString("No One Win!",button.getLocation().x, boardSize + fontAscent); } g.drawImage( offScrImage, button.getLocation().x, buttonHeight, this ); } } public synchronized void update(Graphics g) { paint( g ); } public void actionPerformed(ActionEvent e) { newGame(); } public void mousePressed(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public synchronized void mouseReleased(MouseEvent e) { int x=e.getX(); int y=e.getY(); if( imageTracker.checkID( 0, true ) ) { int num4 = (x - button.getLocation().x) * DIM / boardSize; int num5 = (y - buttonHeight) * DIM / boardSize; if( num5 < DIM && num4 < DIM && grid[num5 * DIM + num4] == 0 ) { illegalMove = false; if( isGood( num5, num4, 2, 3 ) <= 0 ) { illegalMove = true; paint(getGraphics() ); return; } else { grid[num5 * DIM + num4] = 2; change( num5, num4, 2, 3, 5 ); checkScore(); paint( getGraphics() ); try { Thread.sleep( 1000L ); } catch( Exception unused6 ) { } blueMove(); checkScore(); paint( getGraphics() ); } } else if( num5 < DIM && num4 < DIM && grid[num5 * DIM + num4] == 3 ) { illegalMove = true; paint( getGraphics() ); return; } } return; }  }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 10 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档