game.java
来自「jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,」· Java 代码 · 共 702 行 · 第 1/2 页
JAVA
702 行
}
/**
Helper method that parses a protocol result to a response ID and data,
and invokes the response listener
@param result the result to handle
*/
private void handleProtocolResult(String result)
{
// Debug.println("handleProtocolResult: result = "+result);
//DEBUG - if there is an internal server error print a message and return
if(-1 != result.indexOf("html"))
{
Debug.println("************************** Got Internal Server Error");
return;
}
int delim = result.indexOf(ProtocolConstants.FIELD_DELIMITER);
String ridString = null;
String data = null;
if(delim != -1)
{
ridString = result.substring(0,delim);
data = result.substring(delim+1);
}
else
{
ridString = result;
data = "NO_DATA";
}
// Debug.println("\nridString: "+ridString);
try
{
int rid = Integer.parseInt(ridString.trim());
responseListener.onResponse(rid, data.trim());
}
catch(NumberFormatException e)
{
// Debug.println("Non integer response ID returned: "+ ridString);
}
}
/**
Helper method to handle exceptions when communicating with server
*/
private void handleException(Exception e)
{
e.printStackTrace();
String msg = e.getMessage();
System.out.println(msg);
responseListener.onResponse(ProtocolConstants.R_ERROR, msg);
}
/**
Sets the appropriate response listener object
@param ResopnseListener the response listener object
*/
public void setResponseListener(ResponseListener l)
{
responseListener = l;
}
/**
Gets the index of the local player
@return the index of the local player
*/
public int getThisPlayer()
{
return thisPlayer;
}
/**
Adds a player to the list of players in this game
@param Player the player to add
*/
public void addPlayer(Player player)
{
players.addElement(player);
}
/**
Returns a player with the specified id. The id happens to match the index
in the vector of players
@return the player with the specified id
*/
public Player getPlayer(int id)
{
return (Player)players.elementAt(id);
}
/**
Returns the players in this game
@return the Vector of players
*/
public Vector getPlayers()
{
return players;
}
/**
Sets whose turn it is
@param int the index of the player whose turn it is
*/
public void setTurn(int playerIndex)
{
turn = playerIndex;
}
/**
Returns the index of the player whose turn it currently is
@return the index of the player whose turn it currently is
*/
public int getTurn()
{
return turn;
}
/**
Indicates if the game has started
@return true if the game has started, false otherwise
*/
public boolean isStarted()
{
return started;
}
/**
Indicates if all bets are in or not
@return true if all bets are in, false otherwise
*/
public boolean areBetsIn()
{
return betsIn;
}
/**
Sets the game to be over
public void setGameOver()
{
gameOver = true;
}
*/
/**
Sets the player with the indicated id to be bust
*/
public void bustPlayer(int id)
{
bust[id] = true;
}
/**
Indicates if the player with id is bust
*/
public boolean isBust(int id)
{
return bust[id];
}
/**
Raises a flag to show that the player with the specified ID got blackjack
@param id the id of the player to set
*/
public void setBlackjack(int id)
{
blackjack[id] = true;
}
/**
Checks if a player has blackjack
@param id the id of the player to check
*/
public boolean hasBlackjack(int id)
{
return blackjack[id];
}
/**
Sets which player is using this instance of the game
@param int the index of the player that is using this instance of the game
*/
public void setThisPlayer(int playerIndex)
{
thisPlayer = playerIndex;
}
/**
Sets the started flag to indicate that the game has started
*/
public void setStarted(boolean set)
{
started = set;
}
public void startNewGame()
{
// doReset();
turn = 1;
gameReset = false;
// gameOver = false;
started = true;
betsIn = false;
ResponseListener responseListener = null;
statusTask.cancel();
for(Enumeration e = players.elements(); e.hasMoreElements();)
{
Player p = (Player)e.nextElement();
p.reset();
}
for(int i = 0; i < bust.length; i++)
{
bust[i] = false;
}
for(int j = 0; j < blackjack.length; j++)
{
blackjack[j] = false;
}
}//end method
/**
Helper method that resets the Game object for a new game
*/
private void doReset()
{
statusTimer = new Timer();
players = new Vector(4);
turn = ProtocolConstants.NO_ID;
gameReset = true;
started = false;
betsIn = false;
// gameOver = false;
// thisPlayer = ProtocolConstants.NO_ID;
ResponseListener responseListener = null;
for(int i = 0; i < bust.length; i++)
{
bust[i] = false;
}
for(int j = 0; j < blackjack.length; j++)
{
blackjack[j] = false;
}
}
/**
Inner class encapsulating the timer task that checks if the game should start
*/
private class StartStatusTask extends TimerTask
{
private int busy = 0;
public void run()
{
if(started || gameReset)
{
cancel();
}
else
{
try
{
if(busy != 0)
{
System.out.println("Timeouts for START status timer:"+busy);
return;
}
// increment busy counter
busy++;
// System.out.println("Sending START status request.");
String result = Protocol.startStatus(thisPlayer);
handleProtocolResult(result);
//get the response id and the data
}
catch(IOException e)
{
handleException(e);
}//catch
// decrement busy counter
busy--;
}//else
}//end method
}//end inner class
/**
Inner class encapsulating the timer task that checks the game status
This task is started when the user places his bet and should continue
throughout the rest of the game
*/
private class StatusTask extends TimerTask
{
private int busy = 0;
public void run()
{
// if(gameOver || gameReset)
if(gameReset)
{
Debug.println("StatusTask: Cancelling");
// Debug.println("gameOver = "+gameOver);
Debug.println("gameReset = "+gameReset);
cancel();
}
else
{
try
{
if(busy != 0)
{
System.out.println("Timeouts for STATUS timer:"+busy);
return;
}
// increment busy counter
busy++;
// System.out.println("Sending status request");
String result = Protocol.status(thisPlayer);
handleProtocolResult(result);
}
catch(IOException e)
{
handleException(e);
}//catch
// decrement busy counter
busy--;
}
}//end method
}//end inner class
}//end class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?