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

📄 chessgame.java

📁 有关手机游戏网络对战方面的源码
💻 JAVA
字号:
package unicoco;

import java.io.IOException;

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;

public class ChessGame {
	public final static byte DIMENSION = 22;

	byte[] chesses = new byte[DIMENSION * DIMENSION];

	boolean isFirst;

	// 本地上一个棋的位置
	byte lLastX;

	byte lLastY;
//远程上一个棋的位置
	byte rLastX;

	byte rLastY;

	// 选择框的位置
	byte selectX;

	byte selectY;

	// 当前的轮流
	boolean myTurn;

	// 自己的名字和战绩
	short[] LwinAndFail = new short[2];

	String Lname;

	// 对方的名字和战绩,不用初始化
	short[] RwinAndFail;

	String Rname;

	short steps = 0;

	public ChessGame(boolean isServer) {
		lLastX = lLastY = -1;
		selectX = selectY = DIMENSION / 2;
		isFirst = isServer;
		myTurn = isFirst;
		RecordStore rs = null;
		try {
			rs = RecordStore.openRecordStore("UserInf", false);
		} catch (RecordStoreFullException e) {
//			e.printStackTrace();
			return;
		} catch (RecordStoreNotFoundException e) {
//			e.printStackTrace();
			return;
		} catch (RecordStoreException e) {
//			e.printStackTrace();
			return;
		}
		Lname = OptionsForm.readUserName(rs);
		OptionsForm.readWFData(rs, LwinAndFail);
		try {
			rs.closeRecordStore();
		} catch (RecordStoreNotOpenException e) {
//			e.printStackTrace();
			return;
		} catch (RecordStoreException e) {
//			e.printStackTrace();
			return;
		}
		//  需要连接
		try {
			ConnectionControler.sendUserInfo(Lname, LwinAndFail);
		} catch (IOException e) {
//			e.prntStackTrace();
			return;
		}
		Rname = ConnectionControler.readUserName();
		RwinAndFail = ConnectionControler.readUserWF();
	}

	public ChessGame(boolean isServer, boolean single) {
		lLastX = lLastY = -1;
		selectX = selectY = DIMENSION / 2;
		isFirst = isServer;
		myTurn = isFirst;
	}

	public final boolean checkWin(int tag,byte x,byte y) {
		// 方向:上
		for (int i = 1; i <= 4 && y - i >= 0; i++) {
			if (chesses[(y - i) * DIMENSION + x] != tag) {
				for (int j = 1; j <= 5 - i && y + j < DIMENSION; j++) {
					if (chesses[(y + j) * DIMENSION + x] != tag) {
						break;
					}
					if (j == 5 - i) {
						return true;
					}
				}
				break;
			}
			if (i == 4) {
				return true;
			}
		}
		// 左
		for (int i = 1; i <= 4 && x - i >= 0; i++) {
			if (chesses[y * DIMENSION + x - i] != tag) {
				for (int j = 1; j <= 5 - i && x + j < DIMENSION; j++) {
					if (chesses[y * DIMENSION + x + j] != tag) {
						break;
					}
					if (j == 5 - i) {
						return true;
					}
				}
				break;
			}
			if (i == 4) {
				return true;
			}
		}
		// 左上
		for (int i = 1; i <= 4 && x - i >= 0 && y - i >= 0; i++) {
			if (chesses[(y - i) * DIMENSION + x - i] != tag) {
				for (int j = 1; j <= 5 - i && x + j < DIMENSION
						&& y + j < DIMENSION; j++) {
					if (chesses[(y + j) * x + y + j] != tag) {
						break;
					}
					if (j == 5 - i) {
						return true;
					}
				}
				break;
			}
			if (i == 4) {
				return true;
			}
		}
		// 右上
		for (int i = 1; i <= 4 && y - i >= 0 && x + i < DIMENSION; i++) {
			if (chesses[(y - i) * DIMENSION + x + i] != tag) {
				for (int j = 1; j <= 5 - i && y + j < DIMENSION
						&& x - j >= 0; j++) {
					if (chesses[(y + j) * DIMENSION + x - j] != tag) {
						break;
					}
					if (j == 5 - i) {
						return true;
					}
				}
				break;
			}
			if (i == 4) {
				return true;
			}
		}
		return false;
	}

	public final void iniRestart() {
		isFirst = !isFirst;
		lLastX = -1;
		lLastY = -1;
		myTurn = isFirst;
		selectY = selectX = DIMENSION / 2;
		steps = 0;
		for (int i = 0; i < chesses.length; i++) {
			chesses[i] = 0;
		}
	}

	// AI 下棋的坐标
	byte AIx;

	byte AIy;

	public final void AI() {
		int maxWhite = 0, maxRed = 0, maxTemp = 0, max = 0;
		// i是纵坐标,j是横坐标
		for (int i = 0; i < DIMENSION; i++) {
			for (int j = 0; j < DIMENSION; j++) {
				if (chesses[i * DIMENSION + j] == 0) {
					maxWhite = checkMax(j, i, 2);
					if (maxWhite >= 4) {
						AIx = (byte) j;
						AIy = (byte) i;
						if (isFirst) {
							chesses[AIy * DIMENSION + AIx] = 2;
						} else {
							chesses[AIy * DIMENSION + AIx] = 1;
						}
						return;
					}
					maxRed = checkMax(j, i, 1);
					maxTemp = Math.max(maxRed, maxWhite);
					if (maxTemp > max) {
						max = maxTemp;
						AIx = (byte) j;
						AIy = (byte) i;
					}
				}
			}
		}
		// 当isFirst为true,AI的值为2
		if (isFirst) {
			chesses[AIy * DIMENSION + AIx] = 2;
		} else {
			chesses[AIy * DIMENSION + AIx] = 1;
		}
	}

	public final int checkMax(int x, int y, int color) {
		// 在一条直线上相邻同色棋的数量
		int num = 0;
		// 在一条直线上相邻同色的棋子最大的数量
		int maxTemp = 0;
		// right
		for (int i = 1; i < 5; i++) {
			if (x + i >= DIMENSION) {
				break;
			}
			if (chesses[y * DIMENSION + x + i] == color) {
				num++;
			} else {
				break;
			}
		}
		// left
		for (int i = 1; i < 5; i++) {
			if (x - i < 0) {
				break;
			}
			if (chesses[y * DIMENSION + x - i] == color) {
				num++;
			} else {
				break;
			}
		}
		if (num < 4) {
			maxTemp = num;
			num = 0;
		} else {
			return num;
		}
		// up
		for (int i = 1; i < 5; i++) {
			if (y - i < 0) {
				break;
			}
			if (chesses[(y - i) * DIMENSION + x] == color) {
				num++;
			} else {
				break;
			}
		}
		// down
		for (int i = 1; i < 5; i++) {
			if (y + i >= DIMENSION) {
				break;
			}
			if (chesses[(y + i) * DIMENSION + x] == color) {
				num++;
			} else {
				break;
			}
		}
		if (num < 4 && num > maxTemp) {
			maxTemp = num;
			num = 0;
		}
		if (num >= 4) {
			return num;
		}
		// top_left
		for (int i = 1; i < 5; i++) {
			if (y - i < 0 || x - i < 0) {
				break;
			}
			if (chesses[(y - i) * DIMENSION + x - i] == color) {
				num++;
			} else {
				break;
			}
		}
		// bottom_right
		for (int i = 1; i < 5; i++) {
			if (y + i >= DIMENSION || x + i >= DIMENSION) {
				break;
			}
			if (chesses[(y + i) * DIMENSION + x + i] == color) {
				num++;
			} else {
				break;
			}
		}
		if (num < 4 && num > maxTemp) {
			maxTemp = num;
			num = 0;
		}
		if (num >= 4) {
			return num;
		}
		// top_right
		for (int i = 1; i < 5; i++) {
			if (y - i < 0 || x + i >= DIMENSION) {
				break;
			}
			if (chesses[(y - i) * DIMENSION + x + i] == color) {
				num++;
			} else {
				break;
			}
		}
		// bottom_left
		for (int i = 1; i < 5; i++) {
			if (y + i >= DIMENSION || x - i < 0) {
				break;
			}
			if (chesses[(y + i) * DIMENSION + x - i] == color) {
				num++;
			} else {
				break;
			}
		}
		if (num < 4 && num > maxTemp) {
			maxTemp = num;
			num = 0;
		}
		if (num >= 4) {
			return num;
		}
		return maxTemp;
	}
}

⌨️ 快捷键说明

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