📄 mjconsoleclienteventhandler.java~3~
字号:
package com.newpalm.game.mj.client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import com.newpalm.game.mj.share.MJCard;
import com.newpalm.game.mj.share.MJCardCollection;
import com.newpalm.game.mj.share.MJEventObject;
import com.newpalm.game.mj.share.MJEventRoundResult;
import com.newpalm.game.mj.share.PlayerCardModule;
/**
* @author liyamin
*/
public class MJConsoleClientEventHandler extends MJClientEventHandler {
// private static ConsoleCommand cmdQuit =
// new ConsoleCommand("q", "退出(q)", null, true);
// private static ConsoleCommand cmdLook =
// new ConsoleCommand("l", "查看(l)", null, false);
// private static ConsoleCommand cmdDrop =
// new ConsoleCommand("d", "出牌(d)", null, true);
// private static ConsoleCommand cmdCancel =
// new ConsoleCommand("c", "不要(c)", null, true);
// private static ConsoleCommand cmdEat =
// new ConsoleCommand("e", "吃(e)", null, true);
// private static ConsoleCommand cmdPeng =
// new ConsoleCommand("p", "碰(p)", null, true);
// private static ConsoleCommand cmdGang =
// new ConsoleCommand("g", "杠(g)", null, true);
// private static ConsoleCommand cmdAnGang =
// new ConsoleCommand("a", "暗杠(a)", null, true);
// private static ConsoleCommand cmdHu =
// new ConsoleCommand("h", "胡(h)", null, true);
private static ConsoleCommand cmdQuit =
new ConsoleCommand("q", "Quit", null, true);
private static ConsoleCommand cmdLook =
new ConsoleCommand("l", "Look", null, false);
private static ConsoleCommand cmdDrop =
new ConsoleCommand("d", "Drop", null, true);
private static ConsoleCommand cmdCancel =
new ConsoleCommand("c", "Cancel", null, true);
private static ConsoleCommand cmdEat =
new ConsoleCommand("e", "Eat", null, true);
private static ConsoleCommand cmdPeng =
new ConsoleCommand("p", "Peng", null, true);
private static ConsoleCommand cmdGang =
new ConsoleCommand("g", "Gang", null, true);
private static ConsoleCommand cmdAnGang =
new ConsoleCommand("a", "An gang", null, true);
private static ConsoleCommand cmdHu =
new ConsoleCommand("h", "Hu", null, true);
/**
* @param client
*/
public MJConsoleClientEventHandler(MJConsoleClient client) {
super(client);
}
/* (non-Javadoc)
* @see com.inxun.game.mj.client.MJClientEventHandler#onNewRound()
*/
public void onNewRound(MJEventObject event) {
System.out.println(ConsolePrinter.wrapText(event.getData().toString()));
try {
Thread.sleep(1500);
} catch (Exception e) {}
doCmdLook(null);
}
public void onHu(MJEventObject event) {
if (!(event instanceof MJEventRoundResult))
return;
MJEventRoundResult rt = (MJEventRoundResult) event;
String str = ConsolePrinter.wrapText(rt.getDescription());
System.out.println(str);
}
/* (non-Javadoc)
* @see com.inxun.game.mj.client.MJClientEventHandler#onTurnToPlay()
*/
public void onTurnToPlay(MJEventObject event) {
PlayerCardModule myCards = theMod.getMyCardModule(theClient);
MJCardCollection handCards = myCards.getHandCards();
Vector vt = new Vector();
if (handCards.isHuable()) {
vt.add(cmdHu);
}
MJCard[] cards = handCards.getAnGangableCards();
if (cards!= null) {
vt.add(cmdAnGang);
}
vt.add(cmdDrop);
vt.add(cmdLook);
vt.add(cmdQuit);
ConsoleCommand[] cmds = new ConsoleCommand[vt.size()];
vt.toArray(cmds);
showCommandMenu("Drop a card", cmds, true);
}
/* (non-Javadoc)
* @see com.inxun.game.mj.client.MJClientEventHandler#onWaitPass()
*/
public void onWaitPass(MJEventObject event) {
int lastPlayerNo = theMod.getLastPlayerNo();
if (myNo == lastPlayerNo) {
theClient.sendPassSignal();
return;
}
MJCard card = theMod.getLastDroppedCard();
PlayerCardModule myCards = theMod.getMyCardModule(theClient);
MJCardCollection handCards = myCards.getHandCards();
boolean eatable;
if ((myNo + 4 - lastPlayerNo) % 4 != 1) {
eatable = false;
} else {
eatable = handCards.isEatable(card);
}
boolean pengable = handCards.isPengable(card);
boolean gangable = handCards.isGangable(card);
boolean huable = handCards.isHuable(card);
if (eatable || pengable || gangable || huable) {
Vector vt = new Vector();
if (huable)
vt.add(cmdHu);
if (gangable)
vt.add(cmdGang);
if (pengable)
vt.add(cmdPeng);
if (eatable)
vt.add(cmdEat);
vt.add(cmdLook);
vt.add(cmdCancel);
ConsoleCommand[] cmds = new ConsoleCommand[vt.size()];
vt.toArray(cmds);
showCommandMenu("Chance", cmds, true);
} else {
theClient.sendPassSignal();
}
}
// console methods
private void showCommandMenu(
String prompt,
ConsoleCommand[] cmds,
boolean showCmds) {
String[] cmd;
while (true) {
cmd = readCommand(prompt, cmds, showCmds);
if (doCommond(cmd)) {
for (int i = 0; i < cmds.length; i++) {
if (cmds[i].isEnough()
&& cmds[i].getCommand().equals(cmd[0]))
return;
}
}
}
}
private int readNumber(String prompt, int from, int to) {
int min = Math.min(from, to);
int max = Math.max(from, to);
while (true) {
System.out.print(prompt + "(" + min + "-" + max + "):");
String[] cmd = readCommand();
if (cmd == null || cmd.length == 0)
continue;
int n;
try {
n = Integer.parseInt(cmd[0]);
} catch (NumberFormatException e) {
n = min - 1;
}
if (n >= min && n <= max)
return n;
}
}
private String[] readCommand(
String prompt,
ConsoleCommand[] cmds,
boolean showCmds) {
if (cmds == null || cmds.length == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -