J2ME的游戏堆积一(入门篇---五子棋)

目前类的结构如下(部分):

                                                   

启动J2ME的主方法 需要启动的MIDlet(必须继承MIDlet类)

FiveLove.java

/*
 * Created on 2005-5-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.midlet;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import com.leqi.common.Constants;

/**
 * @author leqi
 * create  2005-5-7
 *
 * msn:
lesqi1027@hotmail.com  
 *
 * qq: 26826880
 *
 * mail:
filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Fivelove extends MIDlet {

 /**
  *
  */
 
 private Display display;
 private Chessboard chessboard;
 private MainMenu mainMenu;
 
 public Fivelove() {
  super();
  // TODO Auto-generated constructor stub
  display = Display.getDisplay(this);
  mainMenu = new MainMenu(this, Constants.MAIN_MENU_TITLE, Constants.LIST_IMPLICIT);
 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#startApp()
  */
 protected void startApp() throws MIDletStateChangeException {
  // TODO Auto-generated method stub
     display.setCurrent(mainMenu);
 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#pauseApp()
  */
 protected void pauseApp() {
  // TODO Auto-generated method stub
 }

 /* (non-Javadoc)
  * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
  */
 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }
  
 public Display getDisplay(){
  return this.display;
 }
 
 public void exitMIDlet(){
  try {
   destroyApp(true);
  } catch (MIDletStateChangeException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  notifyDestroyed();
 }
}

其中构造函数,是在启动程序的时候,只运行一次的,所以将首界面的初始化放在次过程进行

public Fivelove() {
  super();
  // TODO Auto-generated constructor stub
  display = Display.getDisplay(this);
  mainMenu = new MainMenu(this, Constants.MAIN_MENU_TITLE, Constants.LIST_IMPLICIT);
 }

避免了每一次都初始化操作

在startApp()就是每一次进入的时候,都调用的方法。会重复调用

至于另外几个方法,就都是基本的功能,如命名!

----------------------------------------------------------------------

主菜单所用到的界面

MainMenu.java

需要继承list来实现页面的表现,以及实现CommandListener来对按键进

行监听

/*
 * Created on 2006-3-14
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.midlet;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;

import com.leqi.common.Constants;

/**
 * @author leqi
 * create  2006-3-14
 *
 * msn:
lesqi1027@hotmail.com  
 *
 * qq: 26826880
 *
 * mail:
filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MainMenu extends List implements CommandListener {

    private Command exit;
    private Command menu;
    private Command help;
    private Command start;
    private Command back;
   
    private TextBox helpMesg;
    private Form formHelp;
    private StringItem content;
   
    private Image image;
    private ImageItem imageItem;
   
    private Fivelove gobang;
   
    private Chessboard chessboard;
   
   
    public MainMenu(Fivelove gobang, String str, int i){
        super(str, i);
        this.gobang = gobang;
       
        exit = new Command("Exit", Command.EXIT, 1);
  start = new Command("Start", Command.OK, 1);
  back = new Command("Back", Command.BACK, 2);

  help = new Command("Info", Command.HELP, 3);
  formHelp = new Form("Info");
  content = new StringItem("", Constants.MAIN_HELP);
  
  try {
            image = Image.createImage(Constants.MAIN_IMAGE);
            imageItem = new ImageItem(null, image,
                    ImageItem.LAYOUT_BOTTOM |
                    ImageItem.LAYOUT_CENTER , "My Image");
            formHelp.append(content);
            formHelp.append(imageItem);
            formHelp.addCommand(back);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
  
  this.append(Constants.FIVE_LOVE, null);
  this.append(Constants.FIGHT_LORD, null);
  
  this.addCommand(help);
  this.addCommand(start);
  this.addCommand(exit);
  formHelp.setCommandListener(this);
  setCommandListener(this);
    }
    /**
     * @param arg0
     * @param arg1
     */
    public MainMenu(String str, int i) {
        super(str, i);
        // TODO Auto-generated constructor stub
    }

    /**
     * @param arg0
     * @param arg1
     * @param arg2
     * @param arg3
     */
    public MainMenu(String arg0, int arg1, String[] arg2, Image[] arg3) {
        super(arg0, arg1, arg2, arg3);
        // TODO Auto-generated constructor stub
    }

    /* (non-Javadoc)
     * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
     */
    public void commandAction(Command command, Displayable displayable) {
        // TODO Auto-generated method stub
        if (command == start) {
            if(this.getSelectedIndex() == 0){
                chessboard = new Chessboard(gobang);
          Display.getDisplay(gobang).setCurrent(chessboard);
            }
  }else if (command == exit) {
   gobang.exitMIDlet();
  }else if (command == help){
   Display.getDisplay(gobang).setCurrent(formHelp);
  }else if (command == back){
      Display.getDisplay(gobang).setCurrent(this);
  }

    }

}

 

------------------------------------------------------------------------------

五子棋游戏主界面

Chessboard.java

该类继承了Cavans类,对页面才用低层的画步处理,同时实现了

CommandListener接口,对页面上的操作进行了监听

/*
 * Created on 2005-5-7
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.midlet;

//import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextBox;
import javax.microedition.midlet.MIDletStateChangeException;
//import javax.microedition.rms.RecordStore;
//import javax.microedition.rms.RecordStoreException;
//import javax.microedition.rms.RecordStoreNotFoundException;

import com.leqi.chess.Chessman;
import com.leqi.chess.JudgeOver;
import com.leqi.chess.RecordStoreChessman;
import com.leqi.common.Constants;

/**
 * @author leqi
 * create  2005-5-7
 *
 * msn:
lesqi1027@hotmail.com  
 *
 * qq: 26826880
 *
 * mail:
filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Chessboard extends Canvas implements CommandListener {

 /**
  * 
  */

 private Command exit;

 private Command start;

 private Command stop;

 private Command menu;

 private Command cancle;

 private Command back;

 private Command help;

 private TextBox helpMesg;

 private Fivelove gobang;
 
 private Alert alert;

 /*
  * 棋盘上的坐标点
  */

 private int height = getHeight();

 private int width = getWidth();

 private int A = height / 13;
 
 private int B = A * 2;
 
 private int C = A * 3;
 
 private int D = A * 4;
 
 private int E = A * 5;
 
 private int F = A * 6;
 
 private int G = A * 7;
 
 private int H = A * 8;
 
 private int I = A * 9;
 
 private int J = A * 10;
 
 private int K = A * 11;
 
 private int L = A * 12;

 
 
 private int a = width / 13;
 
 private int b = a * 2;
 
 private int c = a * 3;
 
 private int d = a * 4;
 
 private int e = a * 5;
 
 private int f = a * 6;
 
 private int g = a * 7;
 
 private int h = a * 8;
 
 private int i = a * 9;
 
 private int j = a * 10;
 
 private int k = a * 11;
 
 private int l = a * 12;

 /*
  * 棋盘上的横向,纵向,单元格的半径和直径 a 为横向的直径 A为纵向的直径
  */
 private int rw = a / 2;

 private int rh = A / 2;
 
 private int initX = g;
 private int initY = F;
 
 private boolean fire = false;//表示按钮是否被激活
 private boolean once = true;//表示是否进行过操作
 /*
  * false 代表下黑
  * true 代表下白
  */
 private boolean color = false;
 
 
// private int index_X;
// private int index_Y;
 
 public Chessboard(Fivelove gobang) {
  super();
  this.gobang = gobang;
  exit = new Command("Exit", Command.EXIT, 1);
  stop = new Command("Stop", Command.STOP, 2);
  back = new Command("Back", Command.BACK, 2);
  cancle = new Command("Cancle", Command.CANCEL, 2);
  help = new Command("Help", Command.HELP, 3);
  helpMesg = new TextBox("Online Help", Constants.CHESSMAN_HELP, 81, 0);
  helpMesg.addCommand(back);
  
  RecordStoreChessman.deleteChessman();
  
  this.addCommand(stop);
  this.addCommand(cancle);
  this.addCommand(help);
  this.addCommand(exit);
  setCommandListener(this);
  helpMesg.setCommandListener(this);
  // TODO Auto-generated constructor stub
 }

 /*
  * (non-Javadoc)
  *
  * @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
  */
 protected void paint(Graphics graphics) {
  // TODO Auto-generated method stub
  graphics.setColor(213, 197, 170);
  graphics.fillRect(0, 0, width, height);
  graphics.setColor(0, 0, 0);
  graphics.drawLine(a, A, l, A);
  graphics.drawLine(a, B, l, B);
  graphics.drawLine(a, C, l, C);
  graphics.drawLine(a, D, l, D);
  graphics.drawLine(a, E, l, E);
  graphics.drawLine(a, F, l, F);
  graphics.drawLine(a, G, l, G);
  graphics.drawLine(a, H, l, H);
  graphics.drawLine(a, I, l, I);
  graphics.drawLine(a, J, l, J);
  graphics.drawLine(a, K, l, K);
  graphics.drawLine(a, L, l, L);
       
  graphics.drawLine(a, A, a, L);
  graphics.drawLine(b, A, b, L);
  graphics.drawLine(c, A, c, L);
  graphics.drawLine(d, A, d, L);
  graphics.drawLine(e, A, e, L);
  graphics.drawLine(f, A, f, L);
  graphics.drawLine(g, A, g, L);
  graphics.drawLine(h, A, h, L);
  graphics.drawLine(i, A, i, L);
  graphics.drawLine(j, A, j, L);
  graphics.drawLine(k, A, k, L);
  graphics.drawLine(l, A, l, L);

  graphics.setColor(223, 37, 17);

  graphics.drawRect(initX - rw, initY-rh, a, A);
  

  
  Chessman[] chessmans = RecordStoreChessman.read();
  if(chessmans != null){
   for(int x = 0; x < chessmans.length; x++){
    if(chessmans[x].isColor()){
     graphics.setColor(255, 255, 255);
     graphics.fillArc(chessmans[x].getX() - rw,
       chessmans[x].getY() - rh,
       a, A, 0, 360);
    }else{
     graphics.setColor(0, 0, 0);
     graphics.fillArc(chessmans[x].getX() - rw,
       chessmans[x].getY() - rh,
       a, A, 0, 360);
    }
   }
  }else{
      RecordStoreChessman.closeChessman();
  }
  
  
  
  if(fire && once){
   boolean find = false;
   if(chessmans != null){
       //判断此位置是否已经存在下了的棋子,如果有find = true
    for(int x = 0; x < chessmans.length; x++){
     if(chessmans[x].getX() == initX && chessmans[x].getY() == initY){
      find = true;
      break;
     }
    }
   }
   if(!find){
    Chessman chessman = new Chessman();
    chessman.setX(initX);
    chessman.setY(initY);
    chessman.setHaveChess(true);
    chessman.setColor(color);
    
    RecordStoreChessman.storeChessman(chessman);
    if(color){
     graphics.setColor(255, 255, 255);
     graphics.fillArc(initX-rw, initY-rh, a, A, 0, 360);
    }else{
     graphics.setColor(0, 0, 0);
     graphics.fillArc(initX-rw, initY-rh, a, A, 0, 360);
    }
 //add   
    if(chessmans != null){
     JudgeOver judge = new JudgeOver();
                 byte ju;
                 ju = judge.judgeMent(chessmans, chessman, a, A);
     if(ju == 1){
      alert = new Alert("Game over", "黑棋获胜", null,
        AlertType.INFO);
      alert.setTimeout(Alert.FOREVER);
      alert.addCommand(exit);
      gobang.getDisplay().setCurrent(alert);
     }
     if(ju == 2){
      alert = new Alert("Game over", "白棋获胜", null,
        AlertType.INFO);
      alert.setTimeout(Alert.FOREVER);
      alert.addCommand(exit);
      gobang.getDisplay().setCurrent(alert);
     }
    }  
 //add to over   
    color = !color;
    fire = false;
    once = false;
   }
  }
 }

 /*
  * (non-Javadoc)
  *
  * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
  *      javax.microedition.lcdui.Displayable)
  */
 public void commandAction(Command command, Displayable displayable) {
  // TODO Auto-generated method stub
  if (command == exit) {
      try {
          RecordStoreChessman.deleteChessman();
                gobang.startApp();
            } catch (MIDletStateChangeException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
  }else if (command == help){
   Display.getDisplay(gobang).setCurrent(helpMesg);
  }else if (command == back){
      Display.getDisplay(gobang).setCurrent(this);
  }else if (command == stop){
      gobang.pauseApp();
  }
 }

 /*
  * (non-Javadoc)
  *
  * @see javax.microedition.lcdui.Canvas#keyPressed(int)
  */
 protected void keyPressed(int key) {
  switch (getGameAction(key)) {
  case Canvas.UP:
   initY = initY - A;
      if(initY<A){
       initY = A;
      }
      fire = false;
      once = true;
   break;
  case Canvas.DOWN:
   initY = initY + A;
      if(initY > L){
       initY = L;
      }
      fire = false;
      once = true;
   break;
  case Canvas.LEFT:
   initX = initX - a;
      if(initX < a){
       initX = a;
      }
      fire = false;
      once = true;
   break;
     case Canvas.RIGHT:
      initX = initX + a;
         if(initX >l){
          initX = l;
         }
         fire = false;
         once = true;
         break;
     case Canvas.FIRE:
      fire = true;
         break;
  }
  repaint();
 }
}

 

-------------------------------------------------

 

用来描述棋盘每一个点信息的类

Chessman.java

涉及到四个属性,坐标,所下棋子颜色,已经是否存在棋子

 /*
 * Created on 2005-5-9
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.chess;


/**
 * @author leqi
 * create  2005-5-9
 *
 * msn: lesqi1027@hotmail.com  
 *
 * qq: 26826880
 *
 * mail: filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Chessman {
 
 private int x;
 private int y;
 private boolean haveChess;
 
 /*
  * true 为白色
  * false 为黑色
  */
 private boolean color;
 
 
 /**
  * @return Returns the color.
  */
 public boolean isColor() {
  return color;
 }
 /**
  * @param color The color to set.
  */
 public void setColor(boolean color) {
  this.color = color;
 }
 /**
  * @return Returns the haveChess.
  */
 public boolean isHaveChess() {
  return haveChess;
 }
 /**
  * @param haveChess The haveChess to set.
  */
 public void setHaveChess(boolean haveChess) {
  this.haveChess = haveChess;
 }
 /**
  * @return Returns the x.
  */
 public int getX() {
  return x;
 }
 /**
  * @param x The x to set.
  */
 public void setX(int x) {
  this.x = x;
 }
 /**
  * @return Returns the y.
  */
 public int getY() {
  return y;
 }
 /**
  * @param y The y to set.
  */
 public void setY(int y) {
  this.y = y;
 }
}

 

--------------------------------------

RecordStroeChessman.java

 此类调用了RecordStore类,作为在游戏过程中的存储介质,其中

提供的查看,存储,关闭,删除。

/*
 * Created on 2005-5-9
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.chess;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

import com.leqi.common.Constants;

/**
 * @author leqi create 2005-5-9
 *
 * msn: lesqi1027@hotmail.com
 *
 * qq: 26826880
 *
 * mail: filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RecordStoreChessman {

    private static RecordStore recordstore = null;

    /**
     * @param chessman
     */
    public static void storeChessman(Chessman chessman) {
        try {
            recordstore = RecordStore.openRecordStore(Constants.CHESSMAN_STORE,
                    true);
        } catch (RecordStoreFullException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] outputRecord;

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DataOutputStream outputDataStream = new DataOutputStream(outputStream);

        try {
            outputDataStream.writeInt(chessman.getX());
            outputDataStream.writeBoolean(chessman.isHaveChess());
            outputDataStream.writeInt(chessman.getY());
            outputDataStream.writeBoolean(chessman.isColor());

            outputDataStream.flush();
            outputRecord = outputStream.toByteArray();
            recordstore.addRecord(outputRecord, 0, outputRecord.length);

            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
            recordstore.closeRecordStore();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (RecordStoreNotOpenException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreFullException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void closeChessman() {
        try {
            recordstore.closeRecordStore();
        } catch (RecordStoreNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void deleteChessman() {
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore(Constants.CHESSMAN_STORE);
            } catch (RecordStoreNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (RecordStoreException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static Chessman[] read() {
        try {
            recordstore = RecordStore.openRecordStore(Constants.CHESSMAN_STORE,
                    true);
        } catch (RecordStoreFullException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        byte[] byteInputData = new byte[10];
        ByteArrayInputStream inputStream = new ByteArrayInputStream(
                byteInputData);
        DataInputStream inputDataStream = new DataInputStream(inputStream);

        try {
            Chessman[] chessmans = null;
            int num = recordstore.getNumRecords();
            if (num < 1) {
                return chessmans;
            } else {
                chessmans = new Chessman[num];
                int tmpX;
                int tmpY;
                boolean tmpHaveChess;
                boolean tmpColor;
                for (int x = 1; x <= num; x++) {
                    Chessman chessman = new Chessman();
                    recordstore.getRecord(x, byteInputData, 0);

                    tmpX = inputDataStream.readInt();
                    tmpHaveChess = inputDataStream.readBoolean();
                    tmpY = inputDataStream.readInt();
                    tmpColor = inputDataStream.readBoolean();

                    inputStream.reset();
                    chessman.setX(tmpX);
                    chessman.setHaveChess(tmpHaveChess);
                    chessman.setY(tmpY);
                    chessman.setColor(tmpColor);
                    chessmans[x - 1] = chessman;
                }
                inputStream.close();
                inputDataStream.close();

                recordstore.closeRecordStore();
                return chessmans;
            }
        } catch (RecordStoreNotOpenException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InvalidRecordIDException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RecordStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

 

-----------------------------------

JudgeOver.java

此类,用来判断当前情况下,五子是否已经满足获胜的条件,其中需要从八个方向分别进行判断

/*
 * Created on 2005-5-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.chess;


/**
 * @author leqi
 * create  2005-5-10
 *
 * msn: lesqi1027@hotmail.com  
 *
 * qq: 26826880
 *
 * mail: filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class JudgeOver {

 /**
  * 判断上下方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeUp(Chessman[] chessmans, int x, int y, boolean color,
   int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x
     && chessmans[z].getY() == y - A) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeUp(chessmans, x, y - A, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断下方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeDown(Chessman[] chessmans, int x, int y, boolean color,
   int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x
     && chessmans[z].getY() == y + A) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeDown(chessmans, x, y + A, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断左方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeLeft(Chessman[] chessmans, int x, int y, boolean color,
   int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x - a
     && chessmans[z].getY() == y) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeLeft(chessmans, x - a, y, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断右方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeRight(Chessman[] chessmans, int x, int y, boolean color,
   int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x + a
     && chessmans[z].getY() == y) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeRight(chessmans, x + a, y, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断左上方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeUpLeft(Chessman[] chessmans, int x, int y, boolean color,
   int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x - a
     && chessmans[z].getY() == y - A) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeUpLeft(chessmans, x - a, y - A, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断右下方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeDownRight(Chessman[] chessmans, int x, int y,
   boolean color, int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x + a
     && chessmans[z].getY() == y + A) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeDownRight(chessmans, x + a, y + A, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断右上方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeUpRight(Chessman[] chessmans, int x, int y,
   boolean color, int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x + a
     && chessmans[z].getY() == y - A) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeUpRight(chessmans, x + a, y - A, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  * 判断左下方是否已经形成五子
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return
  */
 private byte judgeDownLeft(Chessman[] chessmans, int x, int y,
   boolean color, int a, int A, byte num) {
  for (int z = 0; z < chessmans.length; z++) {
   if (chessmans[z].isColor() == color && chessmans[z].getX() == x - a
     && chessmans[z].getY() == y + A) {
    num++;
    if (num == 5) {
     return 5;
    }
    num = judgeDownLeft(chessmans, x - a, y + A, color, a, A, num);
    return num;
   }
  }
  return num;
 }

 /**
  *
  * @param chessmans
  * @param chessman
  * @param a
  * @param A
  * @return 0 代表没有分出胜负 1 代表黑方胜 2 代表白方胜
  */

 public byte judgeMent(Chessman[] chessmans, Chessman chessman, int a, int A) {
  int x = chessman.getX();
  int y = chessman.getY();
  boolean color = chessman.isColor();
  byte num = 1;
  byte totleUpDown = 1;
  byte totleLeRi = 1;
  byte totleLeUpRiDo = 1;
  byte totleRiUpLeDo = 1;
  if ((totleUpDown = judgeUp(chessmans, x, y, color, a, A, totleUpDown)) == 5
    || judgeDown(chessmans, x, y, color, a, A, totleUpDown) == 5
    || (totleLeRi = judgeLeft(chessmans, x, y, color, a, A,
      totleLeRi)) == 5
    || judgeRight(chessmans, x, y, color, a, A, totleLeRi) == 5
    || (totleLeUpRiDo = judgeUpLeft(chessmans, x, y, color, a, A,
      totleLeUpRiDo)) == 5
    || judgeDownRight(chessmans, x, y, color, a, A, totleLeUpRiDo) == 5
    || (totleRiUpLeDo = judgeUpRight(chessmans, x, y, color, a, A,
      totleRiUpLeDo)) == 5
    || judgeDownLeft(chessmans, x, y, color, a, A, totleRiUpLeDo) == 5) {
   if (color) {
    return 2;
   } else {
    return 1;
   }
  } else {
   return 0;
  }
 }
}

 

-----------------------------------

GameClock.java

此类,用来做游戏的时间控制。目前还没有调用此方法,在以后会进行调用。

需要使用java.util.TimerTask类进行控制

/*
 * Created on 2006-3-16
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.leqi.common;

import java.util.TimerTask;

/**
 * @author leqi
 * create  2006-3-16
 *
 * msn: lesqi1027@hotmail.com  
 *
 * qq: 26826880
 *
 * mail: filippo.le@gmail.com
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GameClock extends TimerTask {

    private int timeLeft = 60;//默认时间
   
    private boolean pause = false;
    /**
     *
     */
    public GameClock() {
        super();
        // TODO Auto-generated constructor stub
    }
   
    public GameClock(int timeLeft){
        this.timeLeft = timeLeft;
    }

    /**
     * @return Returns the timeLeft.
     */
    public int getTimeLeft() {
        return timeLeft;
    }
    /**
     * @param timeLeft The timeLeft to set.
     */
    public void setTimeLeft(int timeLeft) {
        this.timeLeft = timeLeft;
    }
    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        // TODO Auto-generated method stub
        if(!pause){
            timeLeft--;
        }
    }
   
    public void pause(){
        pause = true;
    }
   
    public void resume(){
        pause = false;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值