📄 eightgame.java
字号:
/*
* EightGame.java
*
* Created on February 7, 2006, 1:53 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.ray.ninegrid;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;
/**
*
* @author Ray#
*/
public class EightGame {
public static class Info{
String userid;
int step;
boolean win;
public Info(String id,int s){
userid=id;
step=s;
win=false;
}
public String toString(){
return userid;
}
void setWin(){
win=true;
}
String toNetString(){
return userid+"?"+step+"?"+win;
}
}
protected String userid;
protected int score;
protected int steps;
private long startTime;
private long pauseTime;
private volatile long pauseStart;
protected int[] curStatus;
protected EightBFS eight;
protected EightDTBFS eightdt;
protected EightAStar eightas;
protected Vector<Info> others;
protected boolean isserver;
protected Set<Connection> connections;
protected final int maxConnections=3;
protected ThreadGroup threadGroup;
protected Listener listener;
protected static final int PORT=2006;
protected volatile boolean win;
protected transient boolean startgame;
protected Client clientConnection;
/** Creates a new instance of EightGame */
private EightGame(boolean n,int[] cur,String userid) {
setIsserver(n);
setCurStatus(cur);
startTime=System.currentTimeMillis();
pauseStart=startTime;
eight=EightBFS.getInstance(cur);
eightdt=EightDTBFS.getInstance(cur);
eightas=EightAStar.getInstance(cur);
others=new Vector<Info>();
others.add(new Info(userid,0));
setUserid(userid);
win=false;
connections=new HashSet<Connection>();
startgame=false;
}
public static EightGame createInstance(int[] input,String userid){
if(input.length!=9)return null;
boolean[] judge=new boolean[9];
int[] init=new int[10];
for(int i=0;i<9;i++){
if(judge[input[i]]){
return null;
}judge[input[i]]=true;
init[i]=input[i];
if(input[i]==0)init[9]=i+1;
}
if(!AbstractEightAnalyse.order(init)){
return null;
}
return new EightGame(true,init,userid);
}
public static EightGame createInstance(String userid){
Random rand=new Random();
int[] init;
int loc;
do{
init=new int[10];
for(int i=1;i<9;i++){
loc=rand.nextInt(9);
while(init[loc]!=0)loc=(loc+1)%9;
init[loc]=i;
}
for(int i=0;i<9;i++){
if(init[i]==0){
init[9]=i+1;
break;
}
}
}while(!EightBFS.order(init));
return new EightGame(true,init,userid);
}
public boolean isServer() {
return isserver;
}
public synchronized void setIsserver(boolean net) {
this.isserver = net;
}
public static EightGame connectTo(String ip,String userid){
try{
Socket client=new Socket(ip,PORT);
Scanner jin=new Scanner(client.getInputStream());
PrintWriter out=new PrintWriter(client.getOutputStream());
out.println(userid);
out.flush();
String message;
while((message=jin.next())!=null){
message=message.toLowerCase();
if(message.startsWith("400")){
int init[]=new int[10];
for(int i=0;i<10;i++){
message=jin.next();
init[i]=Integer.parseInt(message);
}
EightGame ins=new EightGame(false,init,userid);
String[] players=jin.nextLine().split(" ");
for(int i=0;i<players.length;i++){
if(players[i].length()>0 && !players[i].equals(userid))
ins.others.add(new Info(players[i],0));
}
ins.clientConnection=ins.new Client(client);
ins.clientConnection.start();
return ins;
}else{
return null;
}
}
}catch(Exception e){
EightUtil.log(e);
}
return null;
}
public void asServer(){
if(listener==null){
try{
listener= new Listener(threadGroup,PORT);
listener.start();
}catch(IOException e){
}
}
}
public void startGame(){
if(listener!=null)
listener.stopListener();
startgame=true;
if(isServer()){
for(Connection i:connections){
i.sendStartGame();
}
}
}
public void updateStatus(){
if(isServer()){
for(Connection i:connections){
i.sendUserInfos();
}
}
}
public String getWinner(){
for(Info i:others){
if(i.win){
return i.userid;
}
}return null;
}
/**
* @return 0 Game over, player has reach the end status; 1 move one step; 2 do nothing.
*/
public synchronized int move(int index){
if(!startgame)return 0;
if(moveOperation(index)){
steps++;
others.get(0).step++;
if(!isServer()){
clientConnection.updateInfo(null,0,false);
}
if(eight.win(curStatus)){
win=true;
others.get(0).win=true;
return 0;
}
return 1;
}
return 2;
}
private boolean moveOperation(int index){
int temp=index-curStatus[9]+1;
switch(temp){
case 1:
curStatus=EightBFS.move(curStatus,'r');
return true;
case -1:
curStatus=EightBFS.move(curStatus,'l');
return true;
case 3:
curStatus=EightBFS.move(curStatus,'d');
return true;
case -3:
curStatus=EightBFS.move(curStatus,'u');
return true;
}
return false;
}
public int[] getCurStatus() {
return curStatus;
}
public synchronized void setCurStatus(int[] curStatus) {
this.curStatus = curStatus;
}
public String getUserid() {
return userid;
}
public synchronized void setUserid(String userid) {
this.userid = userid;
}
public int getScore() {
return score;
}
public int getSteps() {
return steps;
}
public int getConnects(){
if(startgame)return -1;
return others.size();
}
public synchronized void pause(){
pauseStart=System.currentTimeMillis();
}
public synchronized void goon(){
long cur=System.currentTimeMillis();
pauseTime+=cur-pauseStart;
pauseStart=0;
}
public long getTime() {
long cur=System.currentTimeMillis();
if(pauseStart>0){
return (pauseStart-startTime-pauseTime)/1000;
}else{
return (cur-startTime-pauseTime)/1000;
}
}
public synchronized Vector<Info> getOthers() {
return others;
}
public boolean isPause(){
return pauseStart>0;
}
protected synchronized void addConnection(Socket s){
if(connections.size()>=maxConnections){
try{
PrintWriter out=new PrintWriter(s.getOutputStream());
out.print("501");
out.flush();
s.close();
}catch(IOException e){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -