一、连接服务器端代码:
1,客户端首先进行初始化:
public boolean initNet() {
try { // 打开两个连接(一个上一个下),并使它们联结
sc = (SocketConnection) Connector.open(url);
System.out.println("cnnect to server...");
io_is = sc.openInputStream();
io_os = sc.openOutputStream();
return true;
} catch (Exception e) {
netWork = false ;
System.out.println(e.getMessage() + "初始化网络错误");
return false;
}
}
2,与服务器端进行发包收包动作:
public void sendMessage(String str) {
try {
os.write(str.getBytes());
os.write("\r\n".getBytes());
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public InputStream getMessage(){
try{
is.read();
}catch(Exception e){
System.out.println("null message");
e.printStackTrace();
}
return is;
}
public synchronized void run() {
System.out.println("run send");
while (true) {
// If no client to deal, wait until one connects
if (message == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
if (message == null) {
break;
}
try {
os.write(message.getBytes());
os.write("\r\n".getBytes());
} catch (IOException ioe) {
ioe.printStackTrace();
}
// Completed client handling, return handler to pool and
// mark for wait
message = null;
}
}
二、运行游戏的代码:
public void run() {
if (start1 && first) {
System.out.println("start1 and first");
Random rand = new Random();
int count;
for (int i = 0; i < 14; i++) {
// 拿牌
if (mah[0][0][i] == 0) {
// 随机取牌添加到最后
do {
count = abs(rand.nextInt() % 136);
} while (initmah[count] == 0);
initmah[count] = 0;
if (count < 108) {
mah[0][0][i] = count % 9 + 1;
} else {
mah[0][0][i] = (count - 108) % 7 + 1;
}
mah[0][1][i] = count / 36;
if (mah[0][0][i] == magic1 && mah[0][1][i] == magic2) {
mah[0][0][i] = -1;
mah[0][1][i] = 0;
}
if (mah[0][0][i] == 7 && mah[0][1][i] == 3) {
mah[0][0][i] = magic1;
mah[0][1][i] = magic2;
}
mahcount--;
break;
}
}
// 进行胡牌判断,还应该进行杠判断
// 如果可以胡了的话,提示可以胡了
winnum = win(0);
if (winnum > 0) {
eat1 = 6;
}
first = false;}
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9]