⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 five1.java~43~

📁 无线应用开发中的j2me程序实例 五子棋游戏
💻 JAVA~43~
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;

public class Five1 extends MIDlet implements CommandListener
{
	private Display display;
	private final static Command exit=new Command("Exit",Command.EXIT,1);
	private myCanvas c;

	public Five1 (){
		display=Display.getDisplay(this);
		c=new myCanvas();
		c.addCommand(exit);
		c.setCommandListener(this);
	}

	public void startApp(){
		display.setCurrent(c);
		c.startGame();
	}

	public void pauseApp(){
		c.stopGame();
	}

	public void destroyApp(boolean unconditional){
		c.stopGame();
	}

	public void commandAction(Command c,Displayable s){
		if(c==exit){
			destroyApp(false);
			notifyDestroyed();
		}
	}
};
class myCanvas extends GameCanvas implements Runnable
{
	private boolean gameOver;
	private Graphics g;
	private int width,height;
	private int x,y;//人走的坐标
        private int robot_x,robot_y;//电脑走的坐标
        private int r,mapx,mapy,gap,length;
	private Sprite sprite;
	private int[][] chess;
	private boolean chessIsWhite;


	public myCanvas(){
		super(true);
		gameOver=true;
		g=getGraphics();
		x=6;
		y=6;
                robot_x=0;
                robot_y=0;
		width=this.getWidth();
		height=this.getHeight();
		r=3;
		mapx=10;
		mapy=10;
		gap=6;
		length=84;
		Image img=null;
		try{
			img=Image.createImage("/opl.PNG");
		}
		catch(java.io.IOException e){System.out.println("Can not find opl.png!");}
		sprite=new Sprite(img,9,9);
		chess=new int[15][15];
		chessIsWhite=false;


	}

	public void startGame(){
		gameOver=false;
		Thread myThread=new Thread(this);
		myThread.start();
	}

	public void stopGame(){
		gameOver=true;
	}

	public void run(){
          while (!gameOver) {
////////////////////人走/////////////////////////
            int keyState = this.getKeyStates();
            //LEFT
            if ( (keyState & LEFT_PRESSED) != 0) {
              x = x - gap;
              if (x < 6)
                x = 6;
            }
            //right
            if ( (keyState & RIGHT_PRESSED) != 0) {
              x = x + gap;
              if (x > 6 + gap * 14)
                x = 6 + gap * 14;
            }
            //up
            if ( (keyState & UP_PRESSED) != 0) {
              y = y - gap;
              if (y < 6)
                y = 6;
            }
            //down
            if ( (keyState & DOWN_PRESSED) != 0) {
              y = y + gap;
              if (y > 6 + gap * 14)
                y = 6 + gap * 14;
            }
            //fire
            if ( (keyState & FIRE_PRESSED) != 0) {
 //             System.out.println( (y - 6) / gap + "," + (x - 6) / gap);
              if (chess[ (y - 6) / gap][ (x - 6) / gap] == 0 && !chessIsWhite) {
                chess[ (y - 6) / gap][ (x - 6) / gap] = 1;
                chessIsWhite = !chessIsWhite;
                judge();
                if (judge()) {
                  System.out.println("win**************************");
                  gameOver = true;
                }
              }
 ////////////////////机器走///////////////////////
              if (!gameOver && chessIsWhite) {
                computerdo(width, height);

              }
////////////////////机器走///////////////////////
            }
////////////////////人走/////////////////////////

/////////////////////////绘制缓冲区//////////////////////////
            //底色
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, width, height);
            g.setColor(228, 228, 131);
            g.fillRect(0, 0, 10 + (gap + 1) * 14, 10 + (gap + 1) * 14);
            //小球
      //			g.setColor(255,0,0);
      //			g.fillArc(x-r,y-r,r+r,r+r,0,360);
            //棋盘
            g.setColor(0, 0, 0);
            mapx = 10;
            mapy = 10;
            gap = 6;
            length = 84;
            for (int i = 0; i < 15; i++) {
              g.drawLine(mapx, mapy, mapx + length, mapy);
              mapy = mapy + gap;
            }
            mapx = 10;
            mapy = 10;
            gap = 6;
            length = 84;
            for (int i = 0; i < 15; i++) {
              g.drawLine(mapx, mapy, mapx, mapy + length);
              mapx = mapx + gap;
            }
            //棋子

            for (int i = 0; i < 15; i++)
              for (int j = 0; j < 15; j++) {
                if (chess[i][j] == 1) {
                  g.setColor(0, 0, 0);
                  g.fillArc(10 + gap * j - r, 10 + gap * i - r, r + r, r + r, 0, 360);
                }
                if (chess[i][j] == 2) {
                  g.setColor(255, 255, 255);
                  g.fillArc(10 + gap * j - r, 10 + gap * i - r, r + r, r + r, 0, 360);
                }
              }

            //选择框
            sprite.setPosition(x, y);
            if (chess[ (y - 6) / gap][ (x - 6) / gap] == 0)
              sprite.setFrame(0);
            if (chess[ (y - 6) / gap][ (x - 6) / gap] == 1)
              sprite.setFrame(1);
            if (chess[ (y - 6) / gap][ (x - 6) / gap] == 2)
              sprite.setFrame(2);
            sprite.paint(g);
            //信息
            if (gameOver && !chessIsWhite) {
              g.setColor(255, 0, 0);
              g.drawString("The white is winner!", 10, 100,
                           Graphics.LEFT | Graphics.TOP);
            }
            if (gameOver && chessIsWhite) {
              g.setColor(255, 0, 0);
              g.drawString("The black is winner!", 10, 100,
                           Graphics.LEFT | Graphics.TOP);
            }
      /////////////////////////绘制缓冲区//////////////////////////
            flushGraphics();

            try {
              Thread.sleep(200);
            }
            catch (InterruptedException ie) {}

          }
	}

        public boolean judge(){
          int sum = 1;
          int tempx = (y - 6) / gap;
          int tempy = (x - 6) / gap;
          //横联判断
          for (int i = 1; i <= 4; i++) {
            if (tempy + i <= 14) {

              if (chess[tempx][tempy + i] == chess[tempx][tempy] &&
                  chess[tempx][tempy] != 0) {
                sum++;

              }
              else
                break;
            }
            else
              break;
          }

          for (int i = 1; i <= 4; i++) {
            if (tempy - i >= 0) {
              if (chess[tempx][tempy - i] == chess[tempx][tempy] &&
                  chess[tempx][tempy] != 0)
                sum++;
              else
                break;
            }
            else
              break;
          }
          if (sum == 5)
            return true;
//          System.out.println("1********************");
          //熟练判断
          sum = 1;
          for (int i = 1; i <= 4; i++) {
            if (tempx + i <= 14) {

              if (chess[tempx + i][tempy] == chess[tempx][tempy] &&
                  chess[tempx][tempy] != 0) {
                sum++;

              }

              else
                break;
            }
            else
              break;
          }

          for (int i = 1; i <= 4; i++) {
            if (tempx - i >= 0) {
              if (chess[tempx - i][tempy] == chess[tempx][tempy]) {
                sum++;

              }
              else
                break;
            }
            else
              break;
          }
          if (sum == 5)
            return true;
 //         System.out.println("2********************");
          //判断左高右低
          sum = 1;
          for (int i = 1; i <= 4; i++) {

            if (tempx + i <= 14 && tempy + i <= 14 &&
                chess[tempx + i][tempy + i] == chess[tempx][tempy] &&
                chess[tempx][tempy] != 0) {
              sum++;

            }
            else
              break;

          }

          for (int i = 1; i <= 4; i++) {

            if (tempx - i >= 0 && tempy - i >= 0 &&
                chess[tempx - i][tempy - i] == chess[tempx][tempy]) {
              sum++;

            }
            else
              break;

          }
          if (sum == 5)
            return true;
//          System.out.println("3********************");
          //判断右高左低
          sum = 1;
          for (int i = 1; i <= 4; i++) {
            if (tempx - i >= 0 && tempy + i <= 14 &&
                chess[tempx - i][tempy + i] == chess[tempx][tempy] &&
                chess[tempx][tempy] != 0) {
              sum++;
            }

            else
              break;
          }

          for (int i = 1; i <= 4; i++) {

            if (tempx + i <= 14 && tempy - i >= 0 &&
                chess[tempx + i][tempy - i] == chess[tempx][tempy]) {
              sum++;
            }
            else
              break;
          }
//          System.out.println("sum=" + sum);
          if (sum == 5)
            return true;
          else
            return false;
        }
        public void computerdo(int width,int height){
               int max_black,max_white,max_temp,max=0;
               for(int i = 0; i <15; i++)
               {
                   for(int j = 0; j <15; j++)
                   {

                       if(chess[i][j]==0)
                       {
                          max_white=checkMax(i,j,2);//判断白子的最大值
                          System.out.println("*****");
                      //    max_black=checkMax(i,j,1);//判断黑子的最大值
                          max_black=0;
                          max_white=0;

                          if (max_white <= max_black)
                            max_temp = max_black;
                          else
                            max_temp = max_white;
                          if(max_temp>max)
                          {
                             max=max_temp;
                             robot_x=i;
                             robot_y=j;
                          }
                       }

                   }
               }
System.out.println("robot:"+robot_y+","+robot_x);
               chess[robot_y][robot_x]=2;


               chessIsWhite=!chessIsWhite;
             }
        public int checkMax(int x, int y,int black_or_white){
          int num = 0, max_num, max_temp = 0;
          int x_temp = x, y_temp = y;
          int x_temp1 = x_temp, y_temp1 = y_temp;
System.out.println("checkMax:"+x+","+y+"-"+black_or_white+"-");
      /////////////////////////////////////水平方向///////////////////////////////////////
      //计算右边
          for (int k = 1; k < 5; k++) {
            x_temp1++;
            if (x_temp1 >15)
              break;
System.out.println(x_temp1+","+y_temp1);
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }

/*
      //计算左边
          x_temp1 = x_temp;
          for (int i = 1; i < 5; i++) {
            x_temp1--;
            if (x_temp1 < 0)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }
  */
          if (num < 5)
            max_temp = num;
System.out.println("水平方向");
System.out.println();
      ////////////////////////////////////////垂直方向///////////////////////////////////////////
      //计算上边
 /*         x_temp1 = x_temp;
          y_temp1 = y_temp;
          num = 0;
          for (int i = 1; i < 5; i++) {
            y_temp1--;
            if (y_temp1 < 0)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }

      //计算下边
          y_temp1 = y_temp;
          for (int i = 1; i < 5; i++) {
            y_temp1++;
            if (y_temp1 >15)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }
          if (num > max_temp && num < 5)
            max_temp = num;
            System.out.println("垂直方向");
      //////////////////////////////////////////315度方向/////////////////////////////////////////
      //计算左上
          x_temp1 = x_temp;
          y_temp1 = y_temp;
          num = 0;
          for (int i = 1; i < 5; i++) {
            x_temp1--;
            y_temp1--;
            if (y_temp1 < 0 || x_temp1 < 0)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }
      //计算右下
          x_temp1 = x_temp;
          y_temp1 = y_temp;
          for (int i = 1; i < 5; i++) {
            x_temp1++;
            y_temp1++;
            if (y_temp1 >15|| x_temp1 >15)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }
          if (num > max_temp && num < 5)
            max_temp = num;
          System.out.println("315度方向");
      /////////////////////////////////////////////45度方向//////////////////////////////////////////////////////
      //计算右上
          x_temp1 = x_temp;
          y_temp1 = y_temp;
          num = 0;
          for (int i = 1; i < 5; i++) {
            x_temp1++;
            y_temp1--;
            if (y_temp1 < 0 || x_temp1 >15)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }
          System.out.println("******");
      //计算左下
          x_temp1 = x_temp;
          y_temp1 = y_temp;
          for (int i = 1; i < 5; i++) {
            x_temp1--;
            y_temp1++;
            if (y_temp1 >15 || x_temp1 < 0)
              break;
            if (chess[x_temp1][y_temp1] == black_or_white)
              num++;
            else
              break;
          }
          System.out.println("45度方向");
  */        if (num > max_temp && num < 5)
            max_temp = num;

          max_num = max_temp;
          return max_num;
        }
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -