📄 dealer.java
字号:
int top = pile[0].getValue();
if (pile[1].getValue() == top && pile[2].getValue() == top
&& card.getValue() == top)
return true;
return false;
}
public boolean isValidCard(Card card, String command) {
int top = 0;
boolean multi = false;
if (hands[3].getCard(0) != null)
multi = checkformulti(card);
else if (hands[3].isFaceUp())
multi = checkformultiFaceUp(card);
if (multi)
return true;
if (card.getValue() == 2) {// 2 can be played at anytime
cardAccepted(card, command);
return true;
} else if (card.getValue() == 10 || fourOfAKind(card)) {// pile is burn
// and its
// players turn
// again
burnt = true;
for (int n = 0; n < 51; n++)
pile[n] = null;
if (hands[3].isFaceDown() == true || hands[3].length() > 1) {
for (int n = 0; n < 3; n++)
if (hands[3].length() > 1)
sendCommand(command + "burn:", n);
else
sendCommand(command + card.getNumber() + ":", n);
sh.addMsg("You have burnt the pile, its your turn again");
} else {
for (int n = 0; n < 3; n++)
if (hands[3].length() > 1)
sendCommand(command + "burn:", n);
else
sendCommand(command + card.getNumber() + ":", n);
sh.setmyTurn(false);
nextTurn();
}
return true;
} else if (pile[0] == null) {
cardAccepted(card, command);
return true;
} else if (nine == true && card.getValue() == 9) {
cardAccepted(card, command);
return true;
}
if (nine == true && pile[0].getValue() == 9) {
for (int i = 0; i < 52; i++) {
if (pile[i] == null) {
cardAccepted(card, command);
return true;
}
if (pile[i].getValue() == 9)
top++;
else
break;
}
}
if (seven == true && pile[top].getValue() == 7) {
if (card.getValue() >= 7) {
sh.addMsg("You Must Play Less Than a Seven");
return false;
} else {
cardAccepted(card, command);
return true;
}
}
if (pile[top].getValue() <= card.getValue()) {
cardAccepted(card, command);
return true;
}
if (hands[3].isFaceUp() == true || hands[3].length() > 1) {// top
sh.addMsg("You can't play a " + card.getStringValue()
+ " please select another card");
}
return false;
}
private void cardAccepted(Card card, String command) {
// sending move to other players
for (int n = 0; n < 3; n++)
sendCommand(command + card.getNumber() + ":", n);
// adding card to pile
for (int n = 51; n > 0; n--)
pile[n] = pile[n - 1];
pile[0] = card;
sh.setmyTurn(false);
nextTurn();
}
public void processTurn(String otherplayermsg, int commandlength) {
boolean burn = false;
// decode variable that came with message
int varlength = 0;
for (int n = commandlength + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength = n;
break;
}
}
String variable = otherplayermsg
.substring(commandlength + 1, varlength);
if (variable.equals("pickup")) {// if other player picks up deck
for (int n = 0; n < 52; n++) {
if (pile[n] == null)
break;
hands[whosturn].addCard(pile[n]);
pile[n] = null;
}
// sending move to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand("othersturn:" + otherNames[whosturn]
+ ":pickup:", n);
sh.addMsg(otherNames[whosturn] + " picked up the pile");
} else if (variable.equals("faceup")) {// if player plays one of there
// face up cards
int varlength2 = 0;
for (int n = varlength + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength2 = n;
break;
}
}
String cardno = otherplayermsg.substring(varlength + 1, varlength2);
if (cardno.equals("multi")) {
burn = faceupMulti(otherplayermsg, varlength2);
} else {
int cardNo = 0;
try {
cardNo = Integer.parseInt(cardno);
} catch (NumberFormatException b) {
sh.addMsg("processTurn - variable to Int error: " + b);
}
// removing card from players hand and adding to pile
for (int n = 0; n < 3; n++) {
if (hands[whosturn].getFaceUp(n) != null)
if (hands[whosturn].getFaceUp(n).getNumber() == cardNo) {
if (hands[whosturn].getFaceUp(n).getValue() == 10
|| fourOfAKind(hands[whosturn].getFaceUp(n)) == true) {// if
// card
// is
// 10
// burning
// the
// deck
burnt = true;
for (int i = 0; i < 52; i++)
pile[i] = null;
burn = true;
sh.addMsg(otherNames[whosturn]
+ " burnt the pile");
} else {// else adding card to pile
for (int i = 51; i > 0; i--)
pile[i] = pile[i - 1];
pile[0] = hands[whosturn].getFaceUp(n);
}
hands[whosturn].removeFaceUp(n);
break;
}
}
// sending move to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand("othersturn:" + otherNames[whosturn]
+ ":faceup:" + cardno + ":", n);
}
} else if (variable.equals("facedown")) {// if player plays a
// facedown card
int varlength2 = 0;
for (int n = varlength + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength2 = n;
break;
}
}
String cardno = otherplayermsg.substring(varlength + 1, varlength2);
if (cardno.equals("pickup")) {// card that was played facedown
// made player pick up the deck
int varlength3 = 0;// getting card that player played
for (int n = varlength2 + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength3 = n;
break;
}
}
String cardno2 = otherplayermsg.substring(varlength2 + 1,
varlength3);
int cardNo = 0;
try {
cardNo = Integer.parseInt(cardno2);
} catch (NumberFormatException b) {
sh
.addMsg("processTurn facedown pickup- variable to Int error: "
+ b);
}
for (int n = 0; n < 3; n++)
if (hands[whosturn].getFaceDown(n) != null)
if (hands[whosturn].getFaceDown(n).getNumber() == cardNo) {
hands[whosturn].addCard(hands[whosturn]
.getFaceDown(n));
for (int i = 0; i < 52; i++) {
if (pile[i] == null)
break;
hands[whosturn].addCard(pile[i]);
pile[i] = null;
}
for (int i = 0; i < 3; i++)
if (i != whosturn)
sendCommand("othersturn:"
+ otherNames[whosturn]
+ ":facedown:pickup:" + cardNo
+ ":", i);
sh.addMsg(otherNames[whosturn] + " played a "
+ Card.getCardStringValue(cardNo)
+ " and had to pick up the deck");
hands[whosturn].removeFaceDown(n);
break;
}
} else {// facedown card didnt make player pick up the deck
int cardNo = 0;
try {
cardNo = Integer.parseInt(cardno);
} catch (NumberFormatException b) {
sh.addMsg("processTurn - variable to Int error: " + b);
}
// removing card from players hand and adding to pile
for (int n = 0; n < 3; n++) {
if (hands[whosturn].getFaceDown(n) != null)
if (hands[whosturn].getFaceDown(n).getNumber() == cardNo) {
if (hands[whosturn].getFaceDown(n).getValue() == 10
|| fourOfAKind(hands[whosturn]
.getFaceDown(n)) == true) {// if
// card
// is 10
// burning
// the
// deck
burnt = true;
for (int i = 0; i < 52; i++)
pile[i] = null;
if (hands[whosturn].isFaceDown() == true
|| hands[whosturn].length() > 1)
burn = true;
sh.addMsg(otherNames[whosturn]
+ " burnt the pile");
} else {// else adding card to pile
for (int i = 51; i > 0; i--)
pile[i] = pile[i - 1];
pile[0] = hands[whosturn].getFaceDown(n);
}
hands[whosturn].removeFaceDown(n);
break;
}
}
// sending move to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand("othersturn:" + otherNames[whosturn]
+ ":facedown:" + cardno + ":", n);
// Check for out of game
if (hands[whosturn].getFaceDown(0) == null
&& hands[whosturn].getFaceDown(1) == null
&& hands[whosturn].getFaceDown(2) == null
&& hands[whosturn].getCard(0) == null)
outofgame(whosturn);
}
} else if (variable.equals("multi")) {// if player plays a muliple
// cards
// determining how many card where played
int varlength2 = 0;
for (int n = varlength + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength2 = n;
break;
}
}
String numPlayedString = otherplayermsg.substring(varlength + 1,
varlength2);
// converting string to int for processing
int numPlayed = 0;
try {
numPlayed = Integer.parseInt(numPlayedString);
} catch (NumberFormatException b) {
sh.addMsg("processTurn - multi - variable to Int error: " + b);
}
String command = "othersturn:" + otherNames[whosturn] + ":multi:"
+ numPlayed + ":";
for (int n = 0; n < numPlayed; n++) {
varlength = varlength2;
// determining how many card where played
varlength2 = 0;
for (int i = varlength + 1; i < otherplayermsg.length(); i++) {
char extract = otherplayermsg.charAt(i);
if (extract == (':')) {
varlength2 = i;
break;
}
}
String cardnoString = otherplayermsg.substring(varlength + 1,
varlength2);
// converting string to int for processing
int cardno = 0;
try {
cardno = Integer.parseInt(cardnoString);
} catch (NumberFormatException b) {
sh.addMsg("processTurn - multi - variable to Int error: "
+ b);
}
int location = 0;
for (int i = 0; i < hands[whosturn].length() - 1; i++)
if (hands[whosturn].getCard(i).getNumber() == cardno) {
location = i;
break;
}
addcardtopile(hands[whosturn].getCard(location));
command = command.concat(hands[whosturn].getCard(location)
.getNumber()
+ ":");
hands[whosturn].removeCard(location);
}
// if players hand is less than 3 card ands card still in deck give
// another card to player
while (deck[0] != null && hands[whosturn].length() <= 3) {
hands[whosturn].addCard(deck[0]);
sendCommand("getcard:" + deck[0].getNumber() + ":", whosturn);
// removing card from deck
deck[51] = null;
for (int w = 0; w < 51; w++)
deck[w] = deck[w + 1];
}
// sending turn to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand(command, n);
// Check for out of game
if (hands[whosturn].getFaceDown(0) == null
&& hands[whosturn].getFaceDown(1) == null
&& hands[whosturn].getFaceDown(2) == null
&& hands[whosturn].getCard(0) == null)
outofgame(whosturn);
// checking for 4 of a kind else next players turn
if (fourOfAKind(pile[3]) || pile[0].getValue() == 10) {
if (!outofgame[whosturn])
burnt = true;
for (int n = 0; n < 51; n++)
pile[n] = null;
sh.addMsg(otherNames[whosturn] + " burn the pile");
burn = true;
}
} else {// regular game play
// converting string to int for processing
int cardno = 0;
try {
cardno = Integer.parseInt(variable);
} catch (NumberFormatException b) {
sh.addMsg("processTurn - variable to Int error: " + b);
}
// removing card from players hand and adding to pile
for (int n = 0; n < hands[whosturn].length() - 1; n++) {
if (hands[whosturn].getCard(n) == null)
break;
if (hands[whosturn].getCard(n).getNumber() == cardno) {
if (hands[whosturn].getCard(n).getValue() == 10
|| fourOfAKind(hands[whosturn].getCard(n)) == true) {// if
// card
// is
// 10
// burning
// the
// deck
burnt = true;
for (int i = 0; i < 52; i++)
pile[i] = null;
burn = true;
sh.addMsg(otherNames[whosturn] + " burnt the pile");
} else {// else adding card to pile
for (int i = 51; i > 0; i--)
pile[i] = pile[i - 1];
pile[0] = hands[whosturn].getCard(n);
}
hands[whosturn].removeCard(n);
break;
}
}
// if players hand is less than 3 card ands card still in deck give
// another card to player
if (deck[0] != null && hands[whosturn].length() <= 3) {
hands[whosturn].addCard(deck[0]);
sendCommand("getcard:" + deck[0].getNumber() + ":", whosturn);
// removing card from deck
deck[51] = null;
for (int w = 0; w < 51; w++)
deck[w] = deck[w + 1];
}
if (burn) {
// sending move to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand("othersturn:" + otherNames[whosturn]
+ ":burn:", n);
} else {
// sending move to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand("othersturn:" + otherNames[whosturn] + ":"
+ cardno + ":", n);
}
// Check for out of game
if (hands[whosturn].getFaceDown(0) == null
&& hands[whosturn].getFaceDown(1) == null
&& hands[whosturn].getFaceDown(2) == null
&& hands[whosturn].getCard(0) == null)
outofgame(whosturn);
}
if (!burn && position != 4) {
if (whosturn != 3)
waitformsg[whosturn] = false;
nextTurn();
}
displayTable();
}
// returns true if pile burnt, false if not burnt
private boolean faceupMulti(String otherplayermsg, int varlength) {
// determining how many card where played
int varlength2 = 0;
for (int n = varlength + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength2 = n;
break;
}
}
String numPlayedString = otherplayermsg.substring(varlength + 1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -