⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 player.java

📁 一个非常不错的java纸牌游戏源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.net.*;
import java.io.*;
import java.awt.*;

/**
 * Player Class
 *
 * @author Sam Cavenagh
 * @version 13/11/02
 * 
 * Website: http://home.pacific.net.au/~cavenagh/SH/
 * Email: cavenaghweb@hotmail.com
 */
class Player {

	//used for testing makes deck empty at start of game if true
	boolean fastgame = false;

	//you must play less than a seven if true
	boolean seven = false;

	//if nine is invisible
	boolean nine = false;

	//if can swap cards at start of game
	boolean swap = false;

	boolean swapdone = false;

	boolean burnt = false;

	Socket msgSocket;

	PrintWriter out;

	BufferedReader in;

	SHinterface sh;

	Image cardspic;

	Image back;

	Image backSW;

	Image burntPic;

	Image pointer[];

	Hand hand;

	Graphics g;

	String playersName;

	//Details of other players
	String othernames[] = new String[3];

	Card faceup[][] = new Card[3][3];

	int carddowncount[] = new int[3];

	int cardcount[] = new int[3];

	Point tableposition[][] = new Point[3][3];

	Point centre1;//point where pile cards are displayed

	int deck; // count of card remaining in deck;

	Card pile[] = new Card[52]; // cards in play pile;

	boolean listen = true;//is thread listening for msg

	//for whos turn indicator
	String servername;

	int whosturn = 0;

	boolean outofgame[] = new boolean[4];

	Point pointerpoints[] = new Point[4];

	Score score; // scoreboard

	int position = 1;

	Player(SHinterface sh, Image cardspic, Graphics g, Hand hand, Image back,
			Image backSW, Image burntPic, Image[] pointer, Score score) {
		this.sh = sh;
		this.cardspic = cardspic;
		this.g = g;
		this.hand = hand;
		this.back = back;
		this.backSW = backSW;
		this.burntPic = burntPic;
		this.pointer = pointer;
		this.score = score;

		tableposition[0][0] = new Point(0, 103);
		tableposition[0][1] = new Point(0, 188);
		tableposition[0][2] = new Point(0, 276);

		tableposition[1][0] = new Point(103, 0);
		tableposition[1][1] = new Point(188, 0);
		tableposition[1][2] = new Point(276, 0);

		tableposition[2][0] = new Point(354, 103);
		tableposition[2][1] = new Point(354, 188);
		tableposition[2][2] = new Point(354, 276);

		centre1 = new Point(188, 175);

		outofgame[0] = false;
		outofgame[1] = false;
		outofgame[2] = false;
		outofgame[3] = false;

		pointerpoints[0] = new Point(115, 220);
		pointerpoints[1] = new Point(220, 110);
		pointerpoints[2] = new Point(330, 220);
		pointerpoints[3] = new Point(220, 330);
	}

	public void createConnection(String servername, String playersName) {
		this.playersName = playersName;
		score.addName(playersName);
		sh.addMsg("Looking for " + servername + " Dealer");
		try {
			msgSocket = new Socket(servername, 4445);
			out = new PrintWriter(msgSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(msgSocket
					.getInputStream()));
		} catch (UnknownHostException e) {
			sh.addMsg("Server: " + servername + " Could not be Found");
			sh.closeConnection();
		} catch (IOException e2) {
			sh.addMsg("Server not Listening for Connections");
		}

		if (msgSocket != null) {
			out.println(playersName);
			String name = "unknown#$#";
			String fastgameS = "false";
			String sevenS = "false";
			String nineS = "false";
			String swapS = "false";
			try {
				name = in.readLine();
				fastgameS = in.readLine();
				sevenS = in.readLine();
				nineS = in.readLine();
				swapS = in.readLine();
			} catch (IOException e3) {
				sh.addMsg("Getting Otherplayers Name Error " + e3);
			}

			if (!name.equals("unknown#$#")) {
				sh.addMsg("Game Connection Established with " + name);
				this.servername = name;
				//score.addName(servername);//added later ??
				if (fastgameS.equals("true")) {
					fastgame = true;
					sh.addMsg("Fast Game Selected");
				}

				if (sevenS.equals("true")) {
					seven = true;
					sh.addMsg("Must Play Under Seven Selected");
				}

				if (nineS.equals("true")) {
					nine = true;
					sh.addMsg("Nine is Invisible Selected");
				}

				if (swapS.equals("true")) {
					swap = true;
					sh.addMsg("Card Swap at start of game Selected");
				}
				listen = true;
				new WaitforMsg();
			}
		}
	}

	public void sendCommand(String command) {
		out.println(command);
	}

	private boolean fourOfAKind(Card card) {
		if (pile[0] == null || pile[1] == null || pile[2] == null
				|| card == null)
			return false;
		int top = pile[0].getValue();
		if (pile[1].getValue() == top && pile[2].getValue() == top
				&& card.getValue() == top)
			return true;
		return false;
	}

	public void endConnection() {
		listen = false;

		if (msgSocket != null) {
			out.println("end:");
			try {
				msgSocket.close();
			} catch (IOException e) {
			}
		}

		sh.addMsg("Game Connection Closed");
	}

	private void nextTurn() {
		whosturn++;
		if (whosturn >= 4)
			whosturn = 0;
		while (outofgame[whosturn]) {
			whosturn++;
			if (whosturn >= 4)
				whosturn = 0;
		}
	}

	public void displayTable() {
		g.setColor(Color.black);
		g.fillRect(0, 0, 450, 550);
		g.setColor(Color.white);
		g.drawLine(0, 450, 450, 450);
		g.setColor(Color.red);
		g.drawRoundRect(355, 5, 90, 40, 15, 15);
		g.setColor(Color.white);
		g.drawString("Deck: " + deck, 365, 20);
		g.drawString("Pile: " + pilelength(), 365, 40);
		hand.showHand();

		g.drawRoundRect(5, 360, 90, 40, 15, 15);
		g.drawString("Name: " + othernames[0], 10, 375);
		g.drawString("Cards: " + cardcount[0], 10, 395);
		g.drawImage(pointer[1], 68, 380, sh);
		if (faceup[0][0] != null)
			faceup[0][0].drawSideWays(tableposition[0][0]);
		else if (carddowncount[0] >= 3)
			g.drawImage(backSW, (int) tableposition[0][0].getX(),
					(int) tableposition[0][0].getY(), sh);
		if (faceup[0][1] != null)
			faceup[0][1].drawSideWays(tableposition[0][1]);
		else if (carddowncount[0] >= 2)
			g.drawImage(backSW, (int) tableposition[0][1].getX(),
					(int) tableposition[0][1].getY(), sh);
		if (faceup[0][2] != null)
			faceup[0][2].drawSideWays(tableposition[0][2]);
		else if (carddowncount[0] >= 1)
			g.drawImage(backSW, (int) tableposition[0][2].getX(),
					(int) tableposition[0][2].getY(), sh);

		g.drawRoundRect(5, 5, 90, 40, 15, 15);
		g.drawString("Name: " + othernames[1], 10, 20);
		g.drawString("Cards: " + cardcount[1], 10, 40);
		g.drawImage(pointer[2], 70, 25, sh);
		if (faceup[1][0] != null)
			faceup[1][0].drawCard(tableposition[1][0]);
		else if (carddowncount[1] >= 3)
			g.drawImage(back, (int) tableposition[1][0].getX(),
					(int) tableposition[1][0].getY(), sh);
		if (faceup[1][1] != null)
			faceup[1][1].drawCard(tableposition[1][1]);
		else if (carddowncount[1] >= 2)
			g.drawImage(back, (int) tableposition[1][1].getX(),
					(int) tableposition[1][1].getY(), sh);
		if (faceup[1][2] != null)
			faceup[1][2].drawCard(tableposition[1][2]);
		else if (carddowncount[1] >= 1)
			g.drawImage(back, (int) tableposition[1][2].getX(),
					(int) tableposition[1][2].getY(), sh);

		g.drawRoundRect(355, 360, 90, 40, 15, 15);
		g.drawString("Name: " + othernames[2], 360, 375);
		g.drawString("Cards: " + cardcount[2], 360, 395);
		g.drawImage(pointer[1], 423, 380, sh);
		if (faceup[2][0] != null)
			faceup[2][0].drawSideWays(tableposition[2][0]);
		else if (carddowncount[2] >= 3)
			g.drawImage(backSW, (int) tableposition[2][0].getX(),
					(int) tableposition[2][0].getY(), sh);
		if (faceup[2][1] != null)
			faceup[2][1].drawSideWays(tableposition[2][1]);
		else if (carddowncount[2] >= 2)
			g.drawImage(backSW, (int) tableposition[2][1].getX(),
					(int) tableposition[2][1].getY(), sh);
		if (faceup[2][2] != null)
			faceup[2][2].drawSideWays(tableposition[2][2]);
		else if (carddowncount[2] >= 1)
			g.drawImage(backSW, (int) tableposition[2][2].getX(),
					(int) tableposition[2][2].getY(), sh);

		//drawing pile
		if (pile[0] != null) {
			//determining how many cards of the same value are ontop of each other
			int top = 0;
			if (nine == true && pile[0].getValue() == 9) {
				top = 1;
				if (pile[1] != null)
					if (pile[1].getValue() == 9) {
						top = 2;
						if (pile[2] != null)
							if (pile[2].getValue() == 9)
								top = 3;
					}
			}
			int samecount = 1;
			for (int n = top + 1; n < top + 4; n++) {
				if (pile[n] == null)
					break;
				if (pile[n].getValue() == pile[top].getValue())
					samecount++;
				else
					break;
			}
			if (samecount == 1) {//one of a kind
				if (pile[top] != null)
					pile[top].drawCard(centre1);
			} else if (samecount == 2) {//2 of a kind
				pile[top + 1].drawCard2((int) centre1.getX(), (int) centre1
						.getY() - 10);
				pile[top].drawCard2((int) centre1.getX(),
						(int) centre1.getY() + 10);
			} else if (samecount >= 3) {//3 of a kind
				pile[top + 2].drawCard2((int) centre1.getX(), (int) centre1
						.getY() - 20);
				pile[top + 1].drawCard2((int) centre1.getX(), (int) centre1
						.getY());
				pile[top].drawCard2((int) centre1.getX(),
						(int) centre1.getY() + 20);
			}
			if (nine == true && pile[0].getValue() == 9)
				if (top == 1)//one nine
					pile[0].drawSideWays2((int) centre1.getX() - 15,
							(int) centre1.getY() + 40);
			if (top == 2) {//2 nines
				pile[1].drawSideWays2((int) centre1.getX() - 15, (int) centre1
						.getY() + 40);
				pile[0].drawSideWays2((int) centre1.getX() - 15, (int) centre1
						.getY() + 50);
			}
			if (top == 3) {//3 nines
				pile[2].drawSideWays2((int) centre1.getX() - 15, (int) centre1
						.getY() + 40);
				pile[1].drawSideWays2((int) centre1.getX() - 15, (int) centre1
						.getY() + 50);
				pile[0].drawSideWays2((int) centre1.getX() - 15, (int) centre1
						.getY() + 60);
			}
		} else if (burnt) {
			g.drawImage(burntPic, 130, 190, sh);
			burnt = false;
		}
		g.drawImage(pointer[whosturn], (int) pointerpoints[whosturn].getX(),
				(int) pointerpoints[whosturn].getY(), sh);
		sh.repaint();
	}

	public void cardSelection(int cardno) {
		Card card;
		String command;
		if (hand.getCard(0) == null) {//if player only has cards on table
			if (hand.isFaceUp()) { // if cards still faceup on table
				card = hand.getFaceUp(cardno);
				command = "turn:faceup:";
				if (isValidCard(card, command))
					for (int i = 0; i < 3; i++)
						if (hand.getFaceUp(i) != null)
							if (hand.getFaceUp(i).getNumber() == card
									.getNumber()) {
								hand.removeFaceUp(i);
								break;
							}
			} else {//if only cards down
				card = hand.getFaceDown(cardno);
				command = "turn:facedown:";
				if (isValidCard(card, command))
					hand.removeFaceDown(cardno);
				else {//player must pick up the pile if not valide
					hand.addCard(hand.getFaceDown(cardno));
					sendCommand("turn:facedown:pickup:"
							+ hand.getFaceDown(cardno).getNumber() + ":");
					sh.addMsg("The card you played was a "
							+ hand.getFaceDown(cardno).getStringValue()
							+ " you had to pick up the pile");
					for (int n = 0; n < 52; n++) {
						if (pile[n] == null)
							break;
						hand.addCard(pile[n]);
						pile[n] = null;
					}
					hand.removeFaceDown(cardno);
					sh.setmyTurn(false);
					nextTurn();
				}
			}
		} else {// if player still has cards in hand
			card = hand.getCard(cardno);
			command = "turn:";
			if (isValidCard(card, command))
				for (int n = 0; n < hand.length() - 1; n++)
					if (card.getNumber() == hand.getCard(n).getNumber()) {
						hand.removeCard(n);
						break;
					}
		}
		if (deck <= 0 || hand.length() > 3)
			displayTable();
	}

	private boolean isValidCard(Card card, String command) {
		int top = 0;
		boolean multi = false;
		if (hand.getCard(0) != null)
			multi = checkformulti(card);
		else if (hand.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) == true) {//pile is burn and its players turn again
			burnt = true;
			for (int n = 0; n < 51; n++)
				pile[n] = null;
			sendCommand(command + card.getNumber() + ":");
			sh.addMsg("You have burnt the pile, its your turn again");
			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 (hand.isFaceUp() == true || hand.length() > 1)
			sh.addMsg("You can't play a " + card.getStringValue()
					+ " please select another card");
		return false;
	}

	private boolean checkformulti(Card card) {
		//checking if card selection is valid
		if (pile[0] != null) {
			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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -