📄 dealer.java
字号:
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]
+ ":faceup: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 < 3; i++)
if (hands[whosturn].getFaceUp(i) != null)
if (hands[whosturn].getFaceUp(i).getNumber() == cardno) {
location = i;
break;
}
addcardtopile(hands[whosturn].getFaceUp(location));
command = command.concat(hands[whosturn].getFaceUp(location)
.getNumber()
+ ":");
hands[whosturn].removeFaceUp(location);
}
// sending turn to other players
for (int n = 0; n < 3; n++)
if (n != whosturn)
sendCommand(command, n);
// checking for 4 of a kind else next players turn
if (fourOfAKind(pile[3]) || pile[0].getValue() == 10) {
burnt = true;
for (int n = 0; n < 51; n++)
pile[n] = null;
sh.addMsg(otherNames[whosturn] + " burn the pile");
return true;
}
return false;
}
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 canDealerPlay() {
// testing is player has a card they can play
if (pile[0] != null) {
int top = 0;
boolean canplay = false;
if (hands[3].getCard(0) == null) {// if player only has card on
// the table
if (hands[3].isFaceUp())// if player has faceup card on the
// table
{
for (int n = 0; n < 3; n++) {
if (hands[3].getFaceUp(n) != null) {
if (nine == true && pile[0].getValue() == 9) {
top = 0;
for (int i = 0; i < 52; i++) {
if (pile[i] == null) {
canplay = true;
break;
}
if (pile[i].getValue() == 9)
top++;
else
break;
}
}
if (canplay)
break;
if (seven == true && pile[top].getValue() == 7
&& hands[3].getFaceUp(n).getValue() < 7) {
canplay = true;
break;
} else if (hands[3].getFaceUp(n).getValue() == 2
|| hands[3].getFaceUp(n).getValue() == 10) {
canplay = true;
break;
} else if (nine == true
&& hands[3].getFaceUp(n).getValue() == 9) {
canplay = true;
break;
} else if (seven != true
|| pile[top].getValue() != 7) {
if (pile[top].getValue() <= hands[3].getFaceUp(
n).getValue()) {
canplay = true;
break;
}
}
}
}
} else
// if player only has facedown cards
canplay = true;
} else {
for (int n = 0; n < hands[3].length() - 1; n++) {
if (hands[3].getCard(n) == null)
break;
if (nine == true && pile[0].getValue() == 9) {
top = 0;
for (int i = 0; i < 52; i++) {
if (pile[i] == null) {
canplay = true;
break;
}
if (pile[i].getValue() == 9)
top++;
else
break;
}
}
if (canplay)
break;
if (hands[3].getCard(n).getValue() == 2
|| hands[3].getCard(n).getValue() == 10) {
canplay = true;
break;
}
if (nine == true && hands[3].getCard(n).getValue() == 9) {
canplay = true;
break;
}
if (seven == true && pile[top].getValue() == 7
&& hands[3].getCard(n).getValue() < 7) {
canplay = true;
break;
} else if (seven != true || pile[top].getValue() != 7) {
if (pile[top].getValue() <= hands[3].getCard(n)
.getValue()) {
canplay = true;
break;
}
}
}
}
if (canplay) {
// sh.addMsg("Its Your Turn");
sh.setmyTurn(true);
} else {// cant play then must pick up the pile
sh.addMsg("The card played was a " + pile[top].getStringValue()
+ " you had to pick up the pile");
for (int n = 0; n < 52; n++) {
if (pile[n] == null)
break;
hands[3].addCard(pile[n]);
pile[n] = null;
}
// sending move to other players
for (int n = 0; n < 3; n++)
sendCommand("othersturn:" + playersName + ":pickup:", n);
nextTurn();
displayTable();
}
} else {
// sh.addMsg("Its Your Turn");
sh.setmyTurn(true);
}
}
private void processSwap(String otherplayermsg, int commandlength,
int playerNumber) {
if (!otherplayermsg.equals("error")) {
int varlength;
Card inhand[] = new Card[3];
Card ontable[] = new Card[3];
for (int r = 0; r < 2; r++)
for (int n = 0; n < 3; n++) {
varlength = commandlength;
// determining how many card where played
commandlength = 0;
for (int i = varlength + 1; i < otherplayermsg.length(); i++) {
char extract = otherplayermsg.charAt(i);
if (extract == (':')) {
commandlength = i;
break;
}
}
String cardnoString = otherplayermsg.substring(
varlength + 1, commandlength);
int cardno = 0;
try {
cardno = Integer.parseInt(cardnoString);
} catch (NumberFormatException b) {
sh
.addMsg("processSwap - error - variable to Int error: "
+ b);
}
Card card = new Card(cardno, cardspic, sh, g);
if (r == 0)
inhand[n] = card;
else
ontable[n] = card;
}
hands[playerNumber].swap(inhand, ontable);
}
waitformsg[playerNumber] = false;
swapcount++;
if (swapcount == 4) {
sendDetails();
nextTurn();
displayTable();
}
}
class WaitforMsg implements Runnable {
Thread wt; // Wait Thread
int socketNumber; // With of the 3 sockets is this socket listening to
// ?
boolean socketOK = true;
boolean haveswapped = false;
WaitforMsg(int socketNumber) {
this.socketNumber = socketNumber;
wt = new Thread(this, "Wait");
wt.start(); // Starting thread
}
public void run() {
do {
String otherplayermsg = "Message Error";
try {
otherplayermsg = in[socketNumber].readLine();
} catch (IOException e) {
sh.addMsg("Read Error: " + e);
disconnect();
}
if (socketOK) {
if (otherplayermsg == null) {
disconnect();
} else if (otherplayermsg.equals("end")) {
disconnect();
} else {
if (waitformsg[socketNumber]) {
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("turn"))
processTurn(otherplayermsg, commandlength);
if (command.equals("swap")) {
processSwap(otherplayermsg, commandlength,
socketNumber);
haveswapped = true;
}
}
}
}
} while (listen == true && socketOK == true);
sh
.addMsg("Player "
+ otherNames[socketNumber]
+ " Game Socket Has Disconnected \nReplacing player with GameAI");
sendMsg("Player "
+ otherNames[socketNumber]
+ " Game Socket Has Disconnected Replacing player with GameAI");
}
private void disconnect() {
try {
gameSocket[socketNumber].close();
} catch (IOException e) {
sh.addMsg("Error Closing Listen " + e);
}
gameSocket[socketNumber] = null;
in[socketNumber] = null;
out[socketNumber] = null;
aiPlayer[socketNumber] = true;
if (!haveswapped)
swapcount++;
if (whosturn == socketNumber)
new AIThread();
socketOK = false;
}
}
class ListenThread implements Runnable {
Thread lt; // Listen Thread
ListenThread() {
lt = new Thread(this, "Listen");
lt.start(); // Starting thread
}
public void run() {
// Opening listening Socket
listenSocket = null;
try {
listenSocket = new ServerSocket(4445);
} catch (IOException e) {
sh.addMsg("Could not listen " + e);
return;
}
boolean endlook = false;
// Waiting for connection
do {
gameSocket[socketCount] = null;
try {
gameSocket[socketCount] = listenSocket.accept();
} catch (IOException e2) {
// sh.addMsg("Error Accept " + e2);//removing error notice
// as apears when game is started early
endlook = true;
}
if (!endlook) {
try {
out[socketCount] = new PrintWriter(
gameSocket[socketCount].getOutputStream(), true);
in[socketCount] = new BufferedReader(
new InputStreamReader(gameSocket[socketCount]
.getInputStream()));
} catch (IOException e) {
sh.addMsg("Error Out / In problem." + e);
}
try {
otherNames[socketCount] = in[socketCount].readLine();
score.addName(otherNames[socketCount]);
} catch (IOException e3) {
sh.addMsg("Getting Otherplayers Name Error " + e3);
}
// checking if players name matches name of any of the other
// players
for (int n = 0; n < socketCount; n++)
if (otherNames[socketCount].equals(otherNames[n]))
otherNames[socketCount] = otherNames[socketCount]
.concat("-2");
if (otherNames[socketCount].equals(playersName))
otherNames[socketCount] = otherNames[socketCount]
.concat("-2");
out[socketCount].println(playersName);
out[socketCount].println(fastgame);
out[socketCount].println(seven);
out[socketCount].println(nine);
out[socketCount].println(swap);
new WaitforMsg(socketCount);
socketCount++;
}
} while (listen == true && socketCount < 3 && endlook != true);
sh.addMsg("No longer listening for Game Connections");
if (listenSocket != null)
try {
listenSocket.close();
} catch (IOException e) {
sh.addMsg("Error Closing Listen " + e);
}
deal();
}
}
class AIThread implements Runnable {
Thread ait; // AI Thread
int implaying;
int currentID;
AIThread() {
ait = new Thread(this, "AI");
ait.start(); // Starting thread
implaying = (int) whosturn;
currentID = (int) gameID;
}
public void run() {
try {// pause before move played
ait.sleep(aipause);
} catch (Exception e) {
sh.addMsg("AI thead sleep error " + e);
}
// Using gameAI to process move
String command = ai.basicMove(hands[whosturn], pile, pilelength());
// if gameAI generates valid move, process move
if (!command.equals("error") && implaying == whosturn
&& currentID == gameID)
processTurn(command, 4);
if (implaying == whosturn && currentID == gameID)
run();
}
}
// shows scoreboard
class ScoreThread implements Runnable {
Thread st; // Score Thread
ScoreThread() {
st = new Thread(this, "Score");
st.start(); // Starting thread
}
public void run() {
score.display();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -