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

📄 board.java

📁 Game Caro 4 user begginer
💻 JAVA
📖 第 1 页 / 共 3 页
字号:


import java.util.Random;
import java.lang.String;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class Board extends Canvas implements CommandListener {

MIDlet midlet;
Display dpy;
Options options;
Random rand;
int Mode;  // mode screen 
int Level; // level for game
boolean selectUndo; //select undo
private int[] colors = {0x000000,0xff0000, 0x0000ff, 0x00ffff,
                            0xff00ff, 0xff8080, 0x80ff80, 0x8080ff};

Font font;
// data for interface
Piece[][] grid;    // grid for board
// grid width and height, size 
int rows;
int colums;
int osy,osx,sx,sy; //old and new select x y
ClipRect clipRect,cRect; // clip rect for think
int size;
int margin;
//for data for board
Move newmove;  //new move
static Move[] histmove; //hitory move
Move Gen_dat[];	// recorder 
int Gen_begin[],Gen_end[];
int hp,m_hp;            // history point
int ply;
int side,xside;
int maskwin;
String msg;
// table max array
int max[] = {12,8,6,4,4,4,4,4,4,4,4,4,4,4,4,4};
// table point to square empty
int spoint[][] ={{0,6,120,340,2200,5300,0,0,0,0},
                 {0,2,45,230,1500,5300,1,1,1,1},                       
                 {0,1,2,65,1100,5300,1,1,1,1}};
// commands
static final int CMD_ABOUT	= 0;
static final int CMD_EXIT	= 1;
static final int CMD_OPTIONS	= 2;
static final int CMD_RESET	= 3;
static final int CMD_START	= 4;
static final int CMD_HELP	= 5;
static final int CMD_ZLAST	= 6;	// must be ze last, of course
Command cmd[];

// state variables
static final int INITIALIZED = 0;
static final int HUMANTHINK = 1;
static final int COMPUTERTHINK = 2;
static final int HUMANWIN = 3;
static final int COMPUTERWIN = 4;
int gameState;
static final int MAXPOINT = 20000;
static final int EMPTY = 0;
static final int HUMAN = 1;
static final int COMPUTER = 2;
static final int NOSELECT = 0;
static final int SELECT    = 1;
//for win
static final int NOWON      = 0;
static final int BIAS_1     = 1;
static final int BIAS_2     = 1;
static final int VERTICAL   = 1;
static final int HORIZONTAL = 1;
static final int WIN_LINE4B4= 2;
static final int WIN_LINE1B4= 3;
static final int WIN_LINE4B3= 4;
static final int WIN_LINE2B3= 5;


void D(String s) { System.out.println(s); }

	class ClipRect{
		public int y1,x1,y2,x2;
		ClipRect(int ny1, int nx1, int ny2, int nx2){
			y1 = ny1; x1 = nx1; y2 = ny2; x2 = nx2;
		}
		public void setAll(int ny1, int nx1, int ny2, int nx2){
			y1 = ny1; x1 = nx1; y2 = ny2; x2 = nx2;
		}
	}
    class Piece {
	int color;	// color for side
	int size;	// size grid
	int x, y;	// current x y in grid coordinates
	int select;
	Piece(int nx, int ny, int nsize,int ncolor) {    	    
	    x = nx;
	    y = ny;
	    size = nsize;
		color = ncolor;
		select = NOSELECT;
	}
    void setSelect(int newselect) {
	    select = newselect;
	}
	int IsSelect(){
       return select;
	}
	void setColor(int newcolor) {
	    color = newcolor;
	}
	int getColor(){
       return color;
	}
    
	// assumes background is lightgray	void paint(Graphics g,int mode) {		g.setColor(0x808080);
		g.drawRect(x, y, size,size);
	  if( select == SELECT){
			g.setColor(0xffff00);
		    g.drawRect(x+1, y+1, size-2,size-2);
	  }	
	  if( mode == 0){
	    if(color == HUMAN){
		  g.setColor(0xff0000);
		  g.fillRoundRect(x+2,y+2,size-4,size-4,x+2,y+2);
	    }
	    if(color == COMPUTER){
		  g.setColor(0x0000ff);
		  g.fillRoundRect(x+2,y+2,size-4,size-4,x+2,y+2);
	    }	  }	  else{		 if (color == HUMAN) {
		    g.setColor(0xff0000);
		    g.setFont(font);
		    g.drawString("X",x + size/4, y+1,g.TOP|g.LEFT);
		} 
		if (color == COMPUTER){
			g.setColor(0x0000ff);
		    g.setFont(font);
		    g.drawString("O",x + size/4, y + 1,g.TOP|g.LEFT);
		} 	  }	}
	
    }//end class piece
	class Move{ //class addess for move
		int y,x,prior;
		Move(int ny,int nx,int nprior){
			y =ny; x=nx; prior = nprior;
		}
		public void set(int ny,int nx,int nprior){
			y =ny; x=nx; prior = nprior;
		}
		public int gety(){
			return y;
		}
		public int getx(){
			return x;
		}
		public int getprior(){
			return prior;
		}
		public void setprior(int nprior){
			prior = nprior;
		}
	}

    class BoardCommand extends Command {
	int tag;

	BoardCommand(String label, int type, int pri, int tag_) {
		super(label, type, pri);
		tag = tag_;
	}
    }

// FUNCTION 
void setState(int ns) {
	gameState = ns;	
	switch (gameState) {
	    case INITIALIZED:		
		addCommand(cmd[CMD_ABOUT]);
		removeCommand(cmd[CMD_RESET]);
		addCommand(cmd[CMD_START]);
		addCommand(cmd[CMD_HELP]);
		addCommand(cmd[CMD_EXIT]);
		addCommand(cmd[CMD_OPTIONS]);
		break;
		case COMPUTERTHINK:
		removeCommand(cmd[CMD_ABOUT]);
		removeCommand(cmd[CMD_RESET]);
		removeCommand(cmd[CMD_START]);
		removeCommand(cmd[CMD_HELP]);
		addCommand(cmd[CMD_EXIT]);
		removeCommand(cmd[CMD_OPTIONS]);
		break;
	    case HUMANTHINK:
		addCommand(cmd[CMD_ABOUT]);
		addCommand(cmd[CMD_RESET]);
		removeCommand(cmd[CMD_START]);
		addCommand(cmd[CMD_HELP]);
		addCommand(cmd[CMD_EXIT]);
		addCommand(cmd[CMD_OPTIONS]);
		break;
	    case HUMANWIN:
	    case COMPUTERWIN:
		addCommand(cmd[CMD_ABOUT]);
		removeCommand(cmd[CMD_RESET]);
		addCommand(cmd[CMD_START]);
		addCommand(cmd[CMD_HELP]);
		addCommand(cmd[CMD_EXIT]);
		addCommand(cmd[CMD_OPTIONS]);
		break;
	}
   msg = IsMSG(gameState); //get message string
}


public Board(MIDlet midlet_) {

	int i,j;

	// "global" variables

	midlet = midlet_;
	dpy = Display.getDisplay(midlet);

	font = Font.getFont(Font.FACE_SYSTEM,
	    Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

	// REMIND update font
	Mode = 1;
	selectUndo = false;
	size = font.charWidth('M') + 6;
	//init interface , if grid < 20 then set mode
	if(getWidth() <= 128 || getHeight() <= 128){ 
		Mode = 0; size = 8; // screen is <= 128 pixel
	}	
    colums = (getWidth()/size) -1;
	rows = (getHeight()/size) -2;  
	margin = size/2 -2;		
	grid = new Piece[rows][colums]; // create the grid arrays	
	for (i = 0; i < rows; i++) {
		for (j = 0; j < colums; j++) {
		  int left = j * size +  margin;
		  int top  = i * size +  margin ;		
		  grid[i][j] = new Piece(left,top,size,EMPTY);		  
		}
	}
    
	// set up commands
	cmd = new Command[CMD_ZLAST];

	cmd[CMD_ABOUT] =
	    new BoardCommand("About", Command.HELP, 5, CMD_ABOUT);

	cmd[CMD_EXIT] =
	    new BoardCommand("Exit", Command.EXIT, 6, CMD_EXIT);

	cmd[CMD_OPTIONS] =
	    new BoardCommand("Options", Command.SCREEN, 3, CMD_OPTIONS);
	
	cmd[CMD_RESET] = 
	    new BoardCommand("Reset", Command.SCREEN, 1, CMD_RESET);

	cmd[CMD_START] =
	    new BoardCommand("New Game...", Command.SCREEN, 1, CMD_START);

	cmd[CMD_HELP] =
	    new BoardCommand("Help", Command.SCREEN, 4, CMD_HELP);

	// set up the listener
	setCommandListener(this);
    //set upp new Random
	rand = new Random();
	// set up options screen
	options = new Options(dpy, this);	
	// set up and initial state
    initboard(HUMAN);
	setState(INITIALIZED);	
}

public void commandAction(Command c, Displayable d) {
	switch (((BoardCommand) c).tag) {
	    case CMD_ABOUT:
		About.showAbout(Display.getDisplay(midlet));
		break;
	    case CMD_EXIT:
		midlet.notifyDestroyed();
		break;
	    case CMD_OPTIONS:
		dpy.setCurrent(options);
		Level = options.level+1;
		break;
	    case CMD_RESET:		
		resetGrid();
		setState(INITIALIZED);
		repaint();
		if(gameState == COMPUTERTHINK){ ComGo(); return;}
		break;
	    case CMD_START:
	    Level = options.level+1;		
		resetGrid();
		setState(side);	
		repaint();
		if(gameState == COMPUTERTHINK){ ComGo(); return;}
		break;
	    case CMD_HELP:
		Uses.showHelp(Display.getDisplay(midlet));
		break;
	}
}


public void showNotify() {
	// System.out.println("Board: showNotify");
}

public void hideNotify() {
	// System.out.println("Board: hideNotify");
}

public void paint(Graphics g) {
	g.setColor(0xc0c0c0);
	g.fillRect(0, 0, getWidth(), getHeight());
	
	for (int j = 0; j < rows; j++) {
		for (int k = 0; k < colums; k++) {
			grid[j][k].paint(g,Mode);
		}
	}
  
	g.setColor(0x808080);
	g.drawRect(0,  getHeight() - (size+size/2), getWidth(), getHeight()-1);
	g.setColor(colors[gameState]);
	g.drawString(msg,size,  getHeight() - (size+size/2),Graphics.TOP|Graphics.LEFT);
}

public void update(Graphics g){
	grid[osy][osx].paint(g,Mode);
	grid[sy][sx].paint(g,Mode);
	g.setColor(colors[gameState]);
	g.drawString(msg,size,  getHeight() - (size+size/2),Graphics.TOP|Graphics.LEFT);
}
//human go with key pressed
public void keyPressed(int keyCode) {
	if (gameState != HUMANTHINK)  return;
	int nsx = sx, nsy = sy;

	// There is no game action.. Use keypad constants instead
    switch (keyCode) {
	case Canvas.KEY_STAR:	// user undo move
		if(selectUndo == false){ 
			selectUndo = true;	
		}
		else{
		    selectUndo = false;	
		    UnDoMove();			
		 }
		return;
	case Canvas.KEY_NUM1: //left + up
               if(nsx-1 >= 0) nsx -= 1;
			   if(nsy -1 >= 0) nsy -= 1;
               break;
    case Canvas.KEY_NUM2: // up
               if(nsy -1 >= 0) nsy -= 1;
               break;
	case Canvas.KEY_NUM3:  // right + up
               if(nsy -1 >= 0) nsy -= 1;
			   if(nsx+1 < colums) nsx += 1;
               break;
	case Canvas.KEY_NUM7: //left + down
			  if(nsx-1 >= 0) nsx -= 1;		 
              if(nsy+1 < rows) nsy +=1;
              break;	   
    case Canvas.KEY_NUM8: //down
              if(nsy+1 < rows) nsy +=1;
              break;
	case Canvas.KEY_NUM9: //right + down
		      if(nsy+1 < rows) nsy +=1;
              if(nsx+1 < colums) nsx += 1;
              break;
    case Canvas.KEY_NUM4: // left
              if(nsx-1 >= 0) nsx -= 1;		
              break;
    case Canvas.KEY_NUM6: // right
              if(nsx+1 < colums) nsx += 1;
              break;
	case Canvas.KEY_NUM5:
			  if(grid[nsy][nsx].IsSelect() == 1){
				 if(grid[nsy][nsx].getColor() == EMPTY){
				   humanGo(nsy,nsx);
				   return;
				 }
			  }
		      break;         

⌨️ 快捷键说明

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