📄 gamescreen.java
字号:
beginIndex = endIndex+1;
endIndex = data.indexOf(ProtocolConstants.FIELD_DELIMITER, beginIndex);
}
repaint();
}
break;
//Server is notifying each player's cards
case ProtocolConstants.R_CARDS:
{
// data = "0,9,H#0,K,S#1,K,D#1,A,S#2,8,C#2,1,H"; //simulates blackjack
//Expecting data = ID,card,suit#ID,card,suit#ID....
//Parse the data and create all the cards and add them to the players
int beginIndex = 0;
int endIndex = data.indexOf(ProtocolConstants.FIELD_DELIMITER, beginIndex);
int id = ProtocolConstants.NO_ID;
char card = 'a';
char suit = 's';
int endData = data.length();
while(beginIndex < endData)
{
//if we're on the last card we adjust endIndex to the end of the string
if(endIndex == -1)
{
endIndex = endData;
}
String cardString = data.substring(beginIndex, endIndex);
//Card String should be id,num,suit
//Get the id
id = Character.digit(cardString.charAt(0), 10);
//Get the card
card = cardString.charAt(2);
suit = cardString.charAt(4);
Player player = Game.instance.getPlayer(id);
player.addCard(new Card(card, suit));
//do all then...
beginIndex = endIndex+1;
endIndex = data.indexOf(ProtocolConstants.FIELD_DELIMITER, beginIndex);
}
//When we receive cards we check to see if we are the first player
//If we are we add the Card and Hold commands
int thisPlayer = Game.instance.getThisPlayer();
if(thisPlayer == 1)
{
info = Constants.STR_YOUR_TURN;
addCommand(cmdCard);
addCommand(cmdHold);
}
repaint();
}
break;
case ProtocolConstants.R_BLACKJACK:
{
//Expecting data = id
// Debug.println("R_BLACKJACK: "+data);
int id = Character.digit(data.charAt(0), 10);
Game.instance.setBlackjack(id);
}
break;
case ProtocolConstants.R_BUST:
{
// Debug.println("R_BUST: "+data);
}
//break; fall through
case ProtocolConstants.R_CARD:
{
//expecting data = ID#card,suit#Total
// Debug.println("R_CARD "+data);
//Get the id
int id = Character.digit(data.charAt(0), 10);
//Get the card
char card = data.charAt(2); //card
char suit = data.charAt(4); //suit
Player player = Game.instance.getPlayer(id);
player.addCard(new Card(card, suit));
//do a repaint to update the screen
info = "";
repaint();
}
break;
case ProtocolConstants.R_TURN:
{
//expecting data = ID
//where ID is the next player ID
// Debug.println("R_TURN: "+data);
//When turn changes update the game object and do a repaint
int turnID = Integer.parseInt(data);
Game.instance.setTurn(turnID);
int thisPlayer = Game.instance.getThisPlayer();
if(turnID == thisPlayer)
{
addCommand(cmdCard);
addCommand(cmdHold);
info = Constants.STR_YOUR_TURN;
}
else
{
removeCommand(cmdCard);
removeCommand(cmdHold);
info = "";
}
repaint();
}
break;
case ProtocolConstants.R_END:
{
//expecting data = ID,outcome#ID,outcome#...
///where outcome: 0-lost, 1-win, 2-draw
// Debug.println("R_END: "+data);
int beginIndex = 0;
int endIndex = data.indexOf(ProtocolConstants.FIELD_DELIMITER, beginIndex);
int id = ProtocolConstants.NO_ID;
int endStatus = 0;
int endData = data.length();
// Debug.println("beginIndex, endData "+ beginIndex +" "+endData);
while(beginIndex < endData)
{
//if we're on the last outcome we adjust endIndex to the end of the string
if(endIndex == -1)
{
endIndex = endData;
}
String endStatusString = data.substring(beginIndex, endIndex);
//CendStatusString should be id,status
//Get the id
id = Character.digit(endStatusString.charAt(0), 10);
//Get the status
endStatus = Character.digit(endStatusString.charAt(2), 10);
Player player = Game.instance.getPlayer(id);
player.setEndStatus(endStatus);
//do all then...
beginIndex = endIndex+1;
endIndex = data.indexOf(ProtocolConstants.FIELD_DELIMITER, beginIndex);
}
repaint();
//If we are the initiating player we get the option to start a new game
int thisPlayer = Game.instance.getThisPlayer();
if(thisPlayer ==1)
{
addCommand(cmdNewGame);
}
}
break;
case ProtocolConstants.R_NO_CHANGE:
//do nothing
break;
case ProtocolConstants.R_NEW_GAME:
//expecting data: NO_DATA
//If we're receiving this response it means the game controller
//has pressed the New game command. This has reset the server.
//All we have to do is reset the players and go directly to the
//betting screen
Game.instance.startNewGame();
new PlaceBet().setActive();
break;
case ProtocolConstants.R_ERROR:
{
Debug.println("GameScreen: Error"+data);
//TBD alert screen
}
break;
default:
System.out.println("Unhandled response: id = "+code+", Data = "+data);
break;
}//end switc
}//end method
public void paint(Graphics g)
{
try
{
//clear the screen first
g.setColor(colorWhite);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(colorBlack);
drawTitle(g);
drawCurrentPlayer(g);
drawOtherPlayers(g);
}
catch(Exception e)
{
Debug.println("Exception in Paint:"+e);
}
}
/**
Draws the title and the underline
*/
private void drawTitle(Graphics g)
{
Font titleFont = Font.getDefaultFont();
int titleWidth = titleFont.stringWidth(Constants.STR_BLACKJACK);
TITLE_X = (SCREEN_WIDTH - titleWidth)/2;
TITLE_Y = 0;
//Do drawString twice for "Bold" effect
g.drawString(Constants.STR_BLACKJACK, TITLE_X, TITLE_Y, g.TOP | g.LEFT);
g.drawString(Constants.STR_BLACKJACK, TITLE_X+1, TITLE_Y, g.TOP | g.LEFT);
LINE_X = 5;
LINE_Y = TITLE_Y + titleFont.getHeight()-2;
//Draw double underline for "Bold" effect
g.drawLine(LINE_X, LINE_Y, SCREEN_WIDTH-LINE_X, LINE_Y );
g.drawLine(LINE_X, LINE_Y+1, SCREEN_WIDTH-LINE_X, LINE_Y+1 );
}
/**
Draws the section of the screen showing the current player
*/
private void drawCurrentPlayer(Graphics g)
{
//get the current player in turn
int currentIndex = Game.instance.getTurn();
int thisPlayer = Game.instance.getThisPlayer();
Font defaultFont = Font.getDefaultFont();
int fontHeight = defaultFont.getHeight();
Player currentPlayer = Game.instance.getPlayer(currentIndex);
int currentBet = currentPlayer.getBet();
String currentInfo = null;
if(currentIndex == 0)
{
//Current player is dealer
currentInfo = Constants.STR_DEALER;
}
else
{
currentInfo = Constants.STR_BET+": $"+currentBet;
}
g.drawString(currentInfo, LINE_X, LINE_Y+3, g.TOP | g.LEFT);
//Draw the info message
INFO_X = LINE_X+defaultFont.stringWidth(currentInfo)+3;
INFO_Y = LINE_Y+3;
//erase the old message
g.setColor(colorWhite);
g.fillRect(INFO_X, INFO_Y, defaultFont.stringWidth(Constants.STR_DEALING),fontHeight );
g.setColor(colorBlack);
//write the new one
//Do drawString twice for "Bold" effect
g.drawString(info, INFO_X, INFO_Y, g.TOP | g.LEFT);
g.drawString(info, INFO_X+1, INFO_Y, g.TOP | g.LEFT);
//Draw the image representing the player
Image currentPlayerImage = currentPlayer.getImage();
PLAYER_IMAGE_X = 0;//LINE_X;
PLAYER_IMAGE_Y = LINE_Y+fontHeight+2;
PLAYER_IMAGE_WIDTH = currentPlayerImage.getWidth();
PLAYER_IMAGE_HEIGHT = currentPlayerImage.getHeight();
g.drawImage(currentPlayerImage, PLAYER_IMAGE_X, PLAYER_IMAGE_Y, g.TOP | g.LEFT);
//Get the player's cards
Vector cards = currentPlayer.getCards();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -