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

📄 gamemaincanvas.java

📁 有关手机游戏网络对战方面的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
						chessGame.myTurn = false;
						chessGame.lLastX = chessGame.selectX;
						chessGame.lLastY = chessGame.selectY;
						if (chessGame.checkWin(chessGame.chesses[chessGame.lLastY
								* ChessGame.DIMENSION + chessGame.lLastX],
								chessGame.lLastX, chessGame.lLastY)) {
							Alert altWin = new Alert("You Win", null, null,
									AlertType.CONFIRMATION);
							altWin.setString(" You Win!!\n Play again ?");
							altWin.setTimeout(Alert.FOREVER);
							altWin.addCommand(new Command("Yes", Command.OK, 1));
							altWin.addCommand(new Command("No", Command.CANCEL, 1));
							altWin.setCommandListener(this);
							MIDGame.display.setCurrent(altWin,this);
							return;
						}
						if (chessGame.steps == ChessGame.DIMENSION
								* ChessGame.DIMENSION) {
							// 平局
							Alert alt = new Alert("Draw game", null, null,
									AlertType.CONFIRMATION);
							alt.setString(" Draw game!!\n Play again ?");
							alt.setTimeout(Alert.FOREVER);
							alt.addCommand(new Command("Yes", Command.OK, 1));
							alt.addCommand(new Command("No", Command.CANCEL, 1));
							alt.setCommandListener(this);
							MIDGame.display.setCurrent(alt,this);
							return;
						}
						chessGame.AI();
						if (chessGame.isFirst) {
							ig.setColor(255, 255, 255);
						} else {
							ig.setColor(255, 0, 0);
						}
						chessGame.selectX = chessGame.AIx;
						chessGame.selectY = chessGame.AIy;
						ig.fillArc(
								startX + chessGame.AIx * cDiameter - cDiameter / 2,
								startY + chessGame.AIy * cDiameter - cDiameter / 2,
								cDiameter, cDiameter, 0, 360);
						repaint(
								startX + chessGame.lLastX * cDiameter - cDiameter / 2,
								startY + chessGame.lLastY * cDiameter - cDiameter / 2,
								cDiameter + 1, cDiameter + 1);
						repaint(startX + chessGame.AIx * cDiameter - cDiameter / 2,
								startY + chessGame.AIy * cDiameter - cDiameter / 2,
								cDiameter, cDiameter);
						serviceRepaints();
						int tag = 2;
						if (!chessGame.isFirst) {
							tag = 1;
						}
						if (chessGame.checkWin(tag, chessGame.AIx, chessGame.AIy)) {
							Alert alt = new Alert("You Lost", null, null,
									AlertType.CONFIRMATION);
							alt.setString(" You Lost!!\n Play again ?");
							alt.setTimeout(Alert.FOREVER);
							alt.addCommand(new Command("Yes", Command.OK, 1));
							alt.addCommand(new Command("No", Command.CANCEL, 1));
							alt.setCommandListener(this);
							chessGame.myTurn = true;
							MIDGame.display.setCurrent(alt,this);

						}
						if (chessGame.steps == ChessGame.DIMENSION
								* ChessGame.DIMENSION) {
							// 平局
							Alert alt = new Alert("Draw game", null, null,
									AlertType.CONFIRMATION);
							alt.setString(" Draw game!!\n Play again ?");
							alt.setTimeout(Alert.FOREVER);
							alt.addCommand(new Command("Yes", Command.OK, 1));
							alt.addCommand(new Command("No", Command.CANCEL, 1));
							alt.setCommandListener(this);
							chessGame.myTurn = true;
							MIDGame.display.setCurrent(alt, this);
							return;
						}
						chessGame.myTurn = true;
					}
				}
				break;

			default:
				break;
			}
		}
	}

	public void run() {
		int type = 0;
		while (true) {
			try {
				if ((type = ConnectionControler.readMessageType()) == 99) {
					byte[] chess = ConnectionControler.readChess();
					Graphics ig = screen.getGraphics();
					if (chessGame.isFirst) {
						ig.setColor(255, 255, 255);
						chessGame.chesses[chess[1] * ChessGame.DIMENSION + chess[0]] = 2;
					} else {
						ig.setColor(255, 0, 0);
						chessGame.chesses[chess[1] * ChessGame.DIMENSION + chess[0]] = 1;
					}
					chessGame.steps++;
					chessGame.selectX=chess[0];
					chessGame.selectY=chess[1];
					ig.fillArc(startX + chess[0] * cDiameter - cDiameter / 2, startY
							+ chess[1] * cDiameter - cDiameter / 2, cDiameter,
							cDiameter, 0, 360);
					
					repaint();
					serviceRepaints();
					chessGame.rLastX = chess[0];
					chessGame.rLastY = chess[1];
					if (chessGame.checkWin(chessGame.chesses[chess[1]
							* ChessGame.DIMENSION + chess[0]], chessGame.rLastX,
							chessGame.rLastY)) {
						RecordStore rs = null;
						try {
							rs = RecordStore.openRecordStore("UserInf", false);
							OptionsForm.writeWFData(rs, 3, false);
						} catch (RecordStoreFullException e) {
//							e.printStackTrace();
						} catch (RecordStoreNotFoundException e) {
//							e.printStackTrace();
						} catch (RecordStoreException e) {
//							e.printStackTrace();
						}
						try {
							rs.closeRecordStore();
						} catch (RecordStoreNotOpenException e) {
//							e.printStackTrace();
						} catch (RecordStoreException e) {
//							e.printStackTrace();
						}
						chessGame.LwinAndFail[1]++;
						chessGame.RwinAndFail[0]++;
						Alert altWin = new Alert("You Win", null, null,
								AlertType.CONFIRMATION);
						altWin.setString(" You Fail!!\n Play again ?");
						altWin.setTimeout(Alert.FOREVER);
						altWin.addCommand(new Command("Yes", Command.OK, 1));
						altWin.addCommand(new Command("No", Command.CANCEL, 1));
						altWin.setCommandListener(this);
						MIDGame.display.setCurrent(altWin,this);
					}
					chessGame.myTurn = true;
					if (chessGame.steps == ChessGame.DIMENSION * ChessGame.DIMENSION) {
						// 平局
						Alert alt = new Alert("Draw game", null, null,
								AlertType.CONFIRMATION);
						alt.setString(" Draw game!!\n Play again ?");
						alt.setTimeout(Alert.FOREVER);
						alt.addCommand(new Command("Agree", Command.OK, 1));
						alt.addCommand(new Command("Disagree", Command.CANCEL, 1));
						alt.setCommandListener(this);
						MIDGame.display.setCurrent(alt,this);
					}
				} else if (type == 98) {
					String ms = ConnectionControler.readMessage();
					StringBuffer sb = new StringBuffer(chessGame.Rname);
					sb.append(" say : ").append(ms);
					altReceiveMessage.setString(sb.toString());
					MIDGame.display.setCurrent(altReceiveMessage, this);
				} else if (type == 97) {
					Alert alt = new Alert("Object Message", null, null,
							AlertType.CONFIRMATION);
					alt.setString("Rival ask for object,Do you agree??");
					alt.setTimeout(Alert.FOREVER);
					alt.addCommand(new Command("Agree", Command.OK, 1));
					alt.addCommand(new Command("Disagree", Command.CANCEL, 1));
					alt.setCommandListener(this);
					MIDGame.display.setCurrent(alt, this);
				} else if (type == 96) {
//					对方同意悔棋
					rePaintObject();
				} else if (type == 95) {
//					对方不同意悔棋
					Alert alt = new Alert("ObjectRespond Message", null, null,
							AlertType.CONFIRMATION);
					alt.setString("Rival disagree with objecting");
					alt.setTimeout(Alert.FOREVER);
					MIDGame.display.setCurrent(alt, this);
				}
			} catch (EOFException e) {
				// TODO 结束这个线程的运行 被动退出一方的处理
//				e.printStackTrace();
				Alert exitInfo = new Alert("Infomation", null, null,
						AlertType.WARNING);
				StringBuffer sb = new StringBuffer(' ');
				// 这里用了chessGame是造成先前的NullPointerException的原因
				sb.append(chessGame.Rname).append(" was exit,the game is over...");
				exitInfo.setTimeout(Alert.FOREVER);
				exitInfo.setString(sb.toString());
				exitGame();
				MIDGame.display.setCurrent(exitInfo, MainMenu.getInstance());

				chessGame = null;
				instance = null;
				System.gc();
				return;
			} catch (InterruptedIOException e) {
				// 主动退出的用户的处理
//				e.printStackTrace();
				MIDGame.display.setCurrent(MainMenu.getInstance());
				chessGame = null;
				instance = null;
				System.gc();
				return;
			} catch (IOException e) {
//				e.printStackTrace();
				return;
			}
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c.getLabel().equalsIgnoreCase("Exit")) {
			exitGame();
			// 这里之前抛出了奇怪的java.lang.NullPointerException
			// MIDGame.display.setCurrent(MainMenu.getInstance());
			// System.gc();
		} else if (c.getLabel().equalsIgnoreCase("Score")) {
			Alert altRi = new Alert("Score", null, null, AlertType.INFO);
			altRi.setTimeout(Alert.FOREVER);
			StringBuffer sb = new StringBuffer(" Name : ");
			sb.append(chessGame.Rname).append('\n');
			sb.append(" Wins : ").append(chessGame.RwinAndFail[0]).append('\n');
			sb.append(" Fails : ").append(chessGame.RwinAndFail[1]).append('\n');
			sb.append("\n Name : ");
			sb.append(chessGame.Lname).append('\n');
			sb.append(" Wins : ").append(chessGame.LwinAndFail[0]).append('\n');
			sb.append(" Fails : ").append(chessGame.LwinAndFail[1]);
			altRi.setString(sb.toString());

			MIDGame.display.setCurrent(altRi, this);
		} else if (c.getLabel().equalsIgnoreCase("Object")) {
			if (chessGame.myTurn) {
				try {
					ConnectionControler.dos.writeByte(97);
					ConnectionControler.dos.flush();
				} catch (IOException e) {
					MIDGame.game.notifyDestroyed();
//					e.printStackTrace();
				}
				Alert alt = new Alert("Ask for object", null, null,
						AlertType.CONFIRMATION);
				alt.setString("send object message,wait for rival respond");
				alt.setTimeout(Alert.FOREVER);
				MIDGame.display.setCurrent(alt,this);
			}
		} else if (c.getLabel().equalsIgnoreCase("Yes")) {
			chessGame.iniRestart();
			// 为单机版设置
			if (instance1 != null) {
				chessGame.myTurn = true;
				chessGame.isFirst = true;
			}
			iniScreen();
			MIDGame.display.setCurrent(this);
		} else if (c.getLabel().equalsIgnoreCase("no")) {
			if (instance != null) {
				exitGame();
			} else if (instance1 != null) {
				instance1 = null;
				MIDGame.display.setCurrent(MainMenu.getInstance());
			}
			// MIDGame.display.setCurrent(MainMenu.getInstance());
			// System.gc();
		} else if (c.getLabel().equalsIgnoreCase("Message")) {
			MIDGame.display.setCurrent(tbSendMessage);
		} else if (c.getLabel().equalsIgnoreCase("Send")) {
			ConnectionControler.sendMessage(tbSendMessage.getString());
			MIDGame.display.setCurrent(this);
		} else if (c.getLabel().equalsIgnoreCase("Back")) {
			MIDGame.display.setCurrent(this);
		} else if (c.getLabel().equalsIgnoreCase("Return")) {
			instance1 = null;
			MIDGame.display.setCurrent(MainMenu.getInstance());
			System.gc();
		} else if (c.getLabel().equalsIgnoreCase("Agree")) {
			try {
				rePaintObject();
				ConnectionControler.dos.writeByte(96);
				ConnectionControler.dos.flush();
			} catch (IOException e) {
//				e.printStackTrace();
				MIDGame.game.notifyDestroyed();
			}
			MIDGame.display.setCurrent(this);
		} else if (c.getLabel().equalsIgnoreCase("Disagree")) {
			try {
				ConnectionControler.dos.writeByte(95);
				ConnectionControler.dos.flush();
			} catch (IOException e) {
//				e.printStackTrace();
			}
			MIDGame.display.setCurrent(this);
		}
	} 
	
	//这个函数使另外一个线程抛出了异常
	public final void exitGame() {
		try {
			ConnectionControler.dis.close();
			ConnectionControler.dos.close();
			ConnectionControler.conn.close();
			if (Server.instance != null) {
				Server.instance.scn.close();
				Server.instance = null;
			} else if (Client.instance != null) {
				Client.instance = null;
			}
			// chessGame = null;
		} catch (Exception e) {
//			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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