📄 player.java
字号:
}
if (pile[count] != null) {
if (card.getValue() == 9) {
//do nothing as valid card
} else if (seven == true && pile[count].getValue() == 7) {
if (card.getValue() >= 7)
return false;
} else if (!(card.getValue() == 2 || card.getValue() == 10 || card
.getValue() >= pile[count].getValue()))
return false;
}
} else if (card.getValue() == 9 && nine == true) {
//do nothing as valid card
} else if (seven == true && pile[0].getValue() == 7) {
if (card.getValue() >= 7)
return false;
} else if (!(card.getValue() == 2 || card.getValue() == 10 || card
.getValue() >= pile[0].getValue()))
return false;
}
//checking how many card of the same value as card played are in players hand
int amountinhand = 0;
for (int n = 0; n < hand.length(); n++) {
if (hand.getCard(n) == null)
break;
if (hand.getCard(n).getValue() == card.getValue())
amountinhand++;
}
if (amountinhand <= 1)
return false;
MultiCardD dialog = new MultiCardD(sh, amountinhand);
int numbertoplay = dialog.getChoice();
if (numbertoplay <= 1)
return false;
String command = "turn:multi:" + numbertoplay + ":" + card.getNumber()
+ ":";
addcardtopile(card);
numbertoplay--;
int toberemovedcount = 0;
int toberemoved[] = new int[3];
for (int n = 0; n < 3; n++)
toberemoved[n] = -1;
for (int n = 0; n < hand.length() - 1; n++) {
if (hand.getCard(n) == null)
break;
if (numbertoplay <= 0)
break;
if (card.getValue() == hand.getCard(n).getValue()
&& card.getNumber() != hand.getCard(n).getNumber()) {
command = command.concat(hand.getCard(n).getNumber() + ":");
addcardtopile(hand.getCard(n));
//storing which card are to be removed
toberemoved[toberemovedcount] = hand.getCard(n).getNumber();
toberemovedcount++;
numbertoplay--;
}
}
//removing card from hand
for (int n = 0; n < 3; n++) {
if (toberemoved[n] == -1)
break;
for (int i = 0; i < hand.length() - 1; i++)
if (hand.getCard(i).getNumber() == toberemoved[n]) {
hand.removeCard(i);
break;
}
}
//sending command
sendCommand(command);
//checking for 4 of a kind
if (fourOfAKind(pile[3]) || pile[0].getValue() == 10) {
burnt = true;
for (int n = 0; n < 51; n++)
pile[n] = null;
sh.addMsg("You burn the pile is your turn again");
} else {
sh.setmyTurn(false);
nextTurn();
}
return true;
}
private boolean checkformultiFaceUp(Card card) {
//checking if card selection is valid
if (pile[0] != null) {
if (card.getValue() == 9 && nine == true) {
//do nothing as valid card
} else if (card.getValue() == 10 || card.getValue() == 2) {
//do nothing as valid card
} else if (nine == true && pile[0].getValue() == 9) {
int count = 0;//determining the number of nines on top of pile
for (int i = 0; i < 52; i++) {
if (pile[i] == null)
break;
if (pile[i].getValue() == 9)
count++;
else
break;
}
if (pile[count] != null) {
if (seven == true && pile[count].getValue() == 7) {
if (card.getValue() >= 7)
return false;
} else if (!(card.getValue() == 2 || card.getValue() == 10 || card
.getValue() >= pile[count].getValue()))
return false;
}
} else if (seven == true && pile[0].getValue() == 7) {
if (card.getValue() >= 7)
return false;
} else if (!(card.getValue() == 2 || card.getValue() == 10 || card
.getValue() >= pile[0].getValue()))
return false;
}
//checking how many card of the same value as card played are in players hand
int amountinhand = 0;
for (int n = 0; n < 3; n++) {
if (hand.getFaceUp(n) != null)
if (hand.getFaceUp(n).getValue() == card.getValue())
amountinhand++;
}
if (amountinhand <= 1)
return false;
MultiCardD dialog = new MultiCardD(sh, amountinhand);
int numbertoplay = dialog.getChoice();
if (numbertoplay <= 1)
return false;
String command = "turn:faceup:multi:" + numbertoplay + ":"
+ card.getNumber() + ":";
addcardtopile(card);
numbertoplay--;
int toberemovedcount = 0;
int toberemoved[] = new int[3];
for (int n = 0; n < 3; n++)
toberemoved[n] = -1;
for (int n = 0; n < 3; n++)
if (hand.getFaceUp(n) != null) {
if (numbertoplay <= 0)
break;
if (card.getValue() == hand.getFaceUp(n).getValue()
&& card.getNumber() != hand.getFaceUp(n).getNumber()) {
command = command.concat(hand.getFaceUp(n).getNumber()
+ ":");
addcardtopile(hand.getFaceUp(n));
//storing which card are to be removed
toberemoved[toberemovedcount] = hand.getFaceUp(n)
.getNumber();
toberemovedcount++;
numbertoplay--;
}
}
//removing card from hand
for (int n = 0; n < 3; n++) {
if (toberemoved[n] == -1)
break;
for (int i = 0; i < 3; i++)
if (hand.getFaceUp(i) != null)
if (hand.getFaceUp(i).getNumber() == toberemoved[n]) {
hand.removeFaceUp(i);
break;
}
}
//sending command
sendCommand(command);
//checking for 4 of a kind
if (fourOfAKind(pile[3]) || pile[0].getValue() == 10) {
burnt = true;
for (int n = 0; n < 51; n++)
pile[n] = null;
sh.addMsg("You burn the pile is your turn again");
} else {
sh.setmyTurn(false);
nextTurn();
}
return true;
}
private void addcardtopile(Card card) {
//adding card to pile
for (int i = 51; i > 0; i--)
pile[i] = pile[i - 1];
pile[0] = card;
}
private void cardAccepted(Card card, String command) {
sendCommand(command + card.getNumber() + ":");
//adding card to pile
for (int n = 51; n > 0; n--)
pile[n] = pile[n - 1];
pile[0] = card;
sh.setmyTurn(false);
nextTurn();
}
private int pilelength() {
int cardCount = 0;
for (int n = 0; n < 52; n++) {
if (pile[n] == null)
break;
cardCount++;
}
return cardCount;
}
class WaitforMsg implements Runnable {
Thread wt; //Wait Thread
WaitforMsg() {
wt = new Thread(this, "Wait");
wt.start(); // Starting thread
}
public void run() {
do {
String otherplayermsg = "Message Error";
try {
otherplayermsg = in.readLine();
} catch (IOException e) {
sh.addMsg("Read Error: " + e);
sh.addMsg("Server Disconnection");
listen = false;
}
if (otherplayermsg == null)
endConnection();
else if (otherplayermsg.equals("end:"))
endConnection();
else if (listen) {
//sh.addMsg("Msg: " + otherplayermsg);//------------------------------------msg from server
int commandlength = 0;
//decode message and perform function required
for (int n = 0; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
commandlength = n;
break;
}
}
String command = otherplayermsg.substring(0, commandlength);
if (command.equals("deal"))
deal(otherplayermsg, commandlength);
if (command.equals("otherdetails"))
otherdetails(otherplayermsg, commandlength);
if (command.equals("yourturn"))
yourturn();
if (command.equals("getcard"))
getcard(otherplayermsg, commandlength);
if (command.equals("othersturn"))
othersturn(otherplayermsg, commandlength);
if (command.equals("msg"))
sh.addMsg(otherplayermsg.substring(commandlength,
otherplayermsg.length()));
if (command.equals("out"))
out(otherplayermsg, commandlength);
if (command.equals("otherout"))
otherout(otherplayermsg, commandlength);
if (command.equals("lost"))
lost(otherplayermsg, commandlength);
if (command.equals("reset"))
reset();
}
} while (listen);
}
private void reset() {
swapdone = false;
for (int n = 0; n < 4; n++) {
//setting scoreboard if game finishes early
if (n < 3) {
if (outofgame[n] == false)
score.addScore(othernames[n], position);
} else {
if (outofgame[n] == false)
score.addScore(playersName, position);
}
outofgame[n] = false;
}
position = 1;
for (int n = 0; n < 3; n++)
if (othernames[n].equals(servername))
whosturn = n;
hand.removeAll();
for (int n = 0; n < 3; n++) {
for (int i = 0; i < 3; i++)
faceup[n][i] = null;
carddowncount[n] = 3;
cardcount[n] = 3;
deck = 16;
}
for (int n = 0; n < 52; n++)
pile[n] = null;
}
//msg telling player they are out of the game and what posititon they came
private void out(String otherplayermsg, int commandlength) {
//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 positionString = otherplayermsg.substring(commandlength + 1,
varlength);
int playerPosition = 0;
try {
playerPosition = Integer.parseInt(positionString);
} catch (NumberFormatException b) {
sh.addMsg("Otherplayer - out - variable to Int error: " + b);
}
boolean gameover = false;
if (playerPosition == 0)
sh.addMsg("Position Error");
if (playerPosition == 1)
sh
.addMsg("Well done you have won the game your the first out !!");
else if (playerPosition == 2)
sh.addMsg("You've done alright you the second out of the game");
else if (playerPosition == 3)
sh.addMsg("Just made it, congrats your not a ShitHead !");
else if (playerPosition == 4) {
sh.addMsg("You Lost ShitHead !!!");
gameover = true;
}
outofgame[3] = true;
score.addScore(playersName, position);
if (playerPosition == 4)
score.display();
else
position++;
if (whosturn == 3 && !gameover) {
nextTurn();
displayTable();
}
}
//msg stating another player is out of the game
private void otherout(String otherplayermsg, int commandlength) {
//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 name = otherplayermsg
.substring(commandlength + 1, varlength);
sh.addMsg(name + " is out of the game");
score.addScore(name, position);
position++;
for (int n = 0; n < 3; n++)
if (othernames[n].equals(name)) {
outofgame[n] = true;
if (whosturn == n) {
nextTurn();
displayTable();
}
}
}
//telling player who lost.
private void lost(String otherplayermsg, int commandlength) {
//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 name = otherplayermsg
.substring(commandlength + 1, varlength);
sh.addMsg(name + " is the ShitHead");
sh.addMsg("Game Over");
score.addScore(name, 4);
for (int n = 0; n < 3; n++)
if (othernames[n].equals(name))
outofgame[n] = true;
score.display();
}
/*--------------------------------
* Othersturn msg format
* command: - 'otherplayer'
* name: players name
* cardno:(move) - 'pickup'
* - 'burn'
* - 'faceup' - cardno2: - a card number
* - 'multi' - numberplayed - up to 3 card numbers
* - 'facedown' - cardno2: - 'pickup'
* - a card number
* - 'multi' - numberplayed - up to 4 card numbers
* - a card number
*-------------------------------*/
private void othersturn(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 name = otherplayermsg
.substring(commandlength + 1, varlength);
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);
//determining which player just had a turn
int playernumber = 0;
for (int n = 0; n < 3; n++)
if (name.equals(othernames[n])) {
playernumber = n;
break;
}
if (cardno.equals("pickup")) {//other players picks up pile
cardcount[playernumber] = cardcount[playernumber]
+ pilelength();
for (int n = 0; n < 52; n++)
pile[n] = null;
sh.addMsg(othernames[playernumber] + " picked up the pile");
} else if (cardno.equals("burn")) {//other player burns the pile
burnt = true;
burn = true;
//removing cards from pile
sh.addMsg(name + " burnt the pile.");
for (int n = 0; n < 51; n++)
pile[n] = null;
if (deck == 0 || cardcount[playernumber] > 3) {
if (cardcount[playernumber] > 0)
cardcount[playernumber]--;
} else
deck--;
} else if (cardno.equals("faceup")) {//otherplayer plays a faceup card
int varlength3 = 0;
for (int n = varlength2 + 1; n < otherplayermsg.length(); n++) {
char extract = otherplayermsg.charAt(n);
if (extract == (':')) {
varlength3 = n;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -