课程设计要做一个游戏,由于对C++不是很熟悉,老师也准许使用java 或者其他的语言,在.net我学过事件,一种委托回调,但是在java 我不是很了解,应该原理都相同吧!
游戏大致是这样的,现在这在写成功判断!于是我自己加了一个事件\
package com.zhaojun.base; import org.w3c.dom.events.Event; import java.util.*; /** * 游戏类 * * @author Coder-Jun */ public class Game { /* 地图枚举 */ public enum MAP_USER { Red, Black, Null, } /* 记录错误日志 */ private StringBuilder errorStringBuilder; /* 记录日志 */ private StringBuilder arrivedBuilderLog; /* 游戏地图 */ private MAP_USER[][] map; public int getCount() { return this._count; } public int getAllCount() { return this._all_count; } private int _xcount; private int _ycount; /* 记录胜利人 */ private MAP_USER winUser = MAP_USER.Null; private Dictionary<MAP_USER, String> _userMsg; private int _count;// 走了多少 private int _all_count;// 一共可以走多少 private Collection winlistener; /** * 添加成功事件 * * @param listener */ public void addWinListener(WinEvent listener) { if (winlistener == null) { winlistener = new HashSet(); } this.winlistener.add(listener); } private void notifyDoEvent() { Iterator iter = winlistener.iterator(); while (iter.hasNext()) { WinEvent listener = (WinEvent) iter.next(); listener.DoEvent(); } } /** * 成功接口 */ public interface WinEvent extends EventListener { public void DoEvent(); } /** * 初始化地图大小 * * @param xCount * @param yCount */ private Game(int xCount, int yCount) { /* 初始化地图 */ errorStringBuilder = new StringBuilder(); arrivedBuilderLog = new StringBuilder(); arrivedBuilderLog.append("1.初始化地图成功!\n"); _userMsg = new Hashtable<MAP_USER, String>(); this.initMap(xCount, yCount); this.initUser(); } public static void Start(int x, int y) { //初始化地图 Game game = new Game(x, y); Scanner scanner = new Scanner(System.in); game.addWinListener(new WinEvent() { @Override public void DoEvent() { System.out.println("我成功拉!"); } }); //存用户 byte userint = 1; /*下棋*/ for (int i = 0; i < game.getAllCount(); ++i) { //下棋 Start MAP_USER user; int var11; int var12; while (true) { /*用户 判断Start*/ user = MAP_USER.Null; switch (userint) { case 1: user = MAP_USER.Red; System.out.println("红方先走X,Y:"); break; case 2: user = MAP_USER.Black; System.out.println("黑方先走X,Y:"); } /*End*/ String string = scanner.next(); /*解析路径*/ try { String[] e = string.split(","); if (e.length >= 2) { var11 = Integer.parseInt(e[0]); var12 = Integer.parseInt(e[1]); } else { System.err.println("您下棋有误,重新修正!"); continue; } } catch (Exception var10) { System.err.println("您下棋有误,重新修正!"); continue; } //下棋失败 if (!game.inputGo(var11, var12, user)) { System.err.println(game.getError()); continue; } else { game.showMap(); if (userint == 1) { userint = 2; break; } if (userint == 2) { userint = 1; break; } } } //下棋 Start } } /** * @return */ private MAP_USER checkWin() { return MAP_USER.Black; } /** * 加载地图 * * @param xCount * @param yCount */ private void initMap(int xCount, int yCount) { // 记录能下的总步 this.map = new MAP_USER[xCount][yCount]; for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { map[i][j] = MAP_USER.Null; } } this._xcount = xCount; this._ycount = yCount; this._all_count = xCount * yCount; } /** * 初始化用户 */ private void initUser() { System.out.println("请输入黑方名字:"); Scanner canScanner = new Scanner(System.in); String heiNameString = canScanner.next(); System.out.println("请输入红方名字:"); String hongNameString = canScanner.next(); _userMsg.put(MAP_USER.Black, heiNameString); _userMsg.put(MAP_USER.Red, hongNameString); } /** * 地图 */ private void showMap() { for (int i = 0; i < map.length; i++) { /* 输出头 */ if (i == 0) { System.out.print("Map\t"); for (int j = 0; j < map[i].length; j++) { System.out.print(" [" + j + "]\t"); } System.out.println(""); } System.out.print("[" + i + "]\t"); /* 地图输出 */ for (int j = 0; j < map[i].length; j++) { System.out.print("{" + map[i][j] + "}\t"); } System.out.println(""); } } /** * 下棋 * * @param x * @param y * @param user * @return */ private boolean inputGo(int x, int y, MAP_USER user) { boolean tag = false; /*越界检查*/ if (x > this.map[0].length - 1) { errorStringBuilder.append("下棋越界X犯规!"); return false; } if (y > this.map.length - 1) { errorStringBuilder.append("下棋越界Y犯规!"); return false; } if (map[y][x] == MAP_USER.Null) { map[y][x] = user; tag = true; } else { /* 添加错误信息 */ errorStringBuilder.append("X:" + x + "Y:" + y + "已经有人在这个地方下了!!"); return false; } /* 用户下了棋 是否成功! */ if (this.checkWin() == MAP_USER.Null) { } else { } this._count++; return tag; } /** * 获取错误信息 * * @return 错误信息 */ private String getError() { String resString = errorStringBuilder.toString(); this.errorStringBuilder.delete(0, errorStringBuilder.length() - 1); return resString; } }
1.首先要使用事件,自己就必须写一个接口 继承EventListener 然后里面写一个需要实现的接口方法
/**
* 成功接口
*/
public interface WinEvent extends EventListener {
public void DoEvent();
}
private Collection winlistener;
/**
* 添加成功事件
*
* @param listener
*/
public void addWinListener(WinEvent listener) {
if (winlistener == null) {
winlistener = new HashSet();
}
this.winlistener.add(listener);
}
private void notifyDoEvent() {
Iterator iter = winlistener.iterator();
while (iter.hasNext()) {
WinEvent listener = (WinEvent) iter.next();
listener.DoEvent();
}
}
2.实现基本的添加事件和通知就可以了;;;接下来就是使用了
哈哈,成功了!
学习来源: http://www.cnblogs.com/atyou/archive/2013/01/07/2850321.html
原文:http://www.cnblogs.com/xiaojunzi/p/5041073.html