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

📄 dealer.java

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

/**
 * Class for controlling game play ie game dealer
 * 
 * @author Sam Cavenagh
 * @version 6/11/02
 * 
 * Website: http://home.pacific.net.au/~cavenagh/SH/ Email:
 * cavenaghweb@hotmail.com
 */
class Dealer {

	// how long ai pause for before making move(int ms)
	int aipause = 1000;

	// 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;

	int swapcount = 0;

	ServerSocket listenSocket;

	Socket gameSocket[] = new Socket[3];

	PrintWriter out[] = new PrintWriter[3];

	BufferedReader in[] = new BufferedReader[3];

	SHinterface sh;

	Image cardspic;

	Image back;

	Image backSW;

	Image pointer[];

	Image burntPic;

	Graphics g;

	String playersName;

	String otherNames[] = new String[3];

	boolean waitformsg[];

	boolean outofgame[] = new boolean[4];

	boolean aiPlayer[] = new boolean[3];

	boolean listen = true;

	boolean burnt = false;

	int socketCount = 0;

	int whosturn = 2;

	int position = 1;// ie first, second,third or shithead

	Card deck[] = new Card[52];

	Hand hands[] = new Hand[4];

	Card pile[] = new Card[52];

	GameAI ai;

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

	Point centre1;

	Point pointerpoints[] = new Point[4];

	Score score;

	int gameID = 1;// Stops AI playing a move in wrong game

	Dealer(SHinterface sh, Image cardspic, Graphics g, Hand hand, Image back,
			Image backSW, boolean fastgame, boolean seven, boolean nine,
			boolean swap, Image[] pointer, Image burntPic, Score score) {
		this.sh = sh;
		this.cardspic = cardspic;
		this.g = g;
		this.back = back;
		this.backSW = backSW;
		this.fastgame = fastgame;
		this.seven = seven;
		this.nine = nine;
		this.pointer = pointer;
		this.swap = swap;
		this.burntPic = burntPic;
		this.score = score;

		score.imDealer(this);

		hands[0] = new Hand(sh, back, g);
		hands[1] = new Hand(sh, back, g);
		hands[2] = new Hand(sh, back, g);
		hands[3] = hand;

		ai = new GameAI(seven, nine);

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

		waitformsg = new boolean[3];
		for (int n = 0; n < 3; n++) {
			waitformsg[n] = false;
			aiPlayer[n] = false;
		}

		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);

		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 onePlayer(String playersName) {
		this.playersName = playersName;
		score.addName(playersName);
		for (int n = 0; n < 3; n++) {
			otherNames[n] = "AI-" + (n + 1);
			score.addName(otherNames[n]);
			aiPlayer[n] = true;
			socketCount++;
		}
		deal();
	}

	public void start() {
		if (listen && socketCount < 3) {
			for (int n = 0; n < 3; n++)
				if (gameSocket[n] == null) {
					otherNames[n] = "AI" + (n + 1);
					score.addName(otherNames[n]);
					aiPlayer[n] = true;
					socketCount++;
				}

			try {
				listenSocket.close();
			} catch (IOException e) {
				sh.addMsg("Error Closing Listen " + e);
			}
		}
	}

	public int getAIPause() {
		return aipause;
	}

	public void setAIPause(int aipause) {
		this.aipause = aipause;
	}

	private void shuffle() {
		for (int r = 0; r < 2; r++)
			// number of times pile shuffled
			for (int n = 0; n < 52; n++) {
				int newlocation = (int) Math.round(Math.random() * 51);
				Card temp = deck[newlocation];
				deck[newlocation] = deck[n];
				deck[n] = temp;
			}
	}

	public void createConnection(String playersName) {
		this.playersName = playersName;
		score.addName(playersName);
		new ListenThread();
	}

	private void sendCommand(String command, int socNumber) {
		// sh.addMsg("msg:" + command + " socket:" + socNumber);
		if (socNumber < 3)
			if (gameSocket[socNumber] != null)
				out[socNumber].println(command);
	}

	private void sendMsg(String msg) {
		for (int n = 0; n < 3; n++) {
			if (out[n] != null)
				out[n].println("msg:" + msg);
		}
	}

	private void deal() {
		// adding card to deck
		for (int n = 0; n < 52; n++) {
			Card card = new Card(n + 1, cardspic, sh, g);
			deck[n] = card;
		}
		shuffle();// shuffling card deck
		int player = whosturn;
		for (int n = 0; n < 36; n++) {
			// dealing first card in deck
			if (player != 3)// player 3 is dealer no need to send command
				sendCommand("deal:" + deck[0].getNumber() + ":", player);
			// placing cards in players hand, facedown then faceup.
			hands[player].deal(deck[0]);

			// moving cards in deck forwards
			for (int w = 0; w < 51; w++)
				deck[w] = deck[w + 1];

			// removing card from deck
			deck[51] = null;

			// changing player being dealt to
			player++;
			if (player >= 4)
				player = 0;
		}
		sendDetails();

		if (fastgame)
			for (int n = 0; n < 52; n++)
				deck[n] = null;

		if (swap) {// if performing card swap
			displayTable();
			Card inhand[] = new Card[3];
			Card ontable[] = new Card[3];
			for (int n = 0; n < 3; n++) {
				waitformsg[n] = true;
				inhand[n] = hands[3].getCard(n);
				ontable[n] = hands[3].getFaceUp(n);
			}
			SwapD swapD = new SwapD(sh, inhand, ontable);
			if (swapD.display()) {
				hands[3].swap(swapD.getInHand(), swapD.getOnTable());
				displayTable();
			}
			swapcount++;

			// if dealer is last to make the swap
			if (swapcount == 4) {
				sendDetails();
				nextTurn();
				displayTable();
			}

			// swap for ai players
			for (int n = 0; n < 3; n++)
				if (aiPlayer[n])
					processSwap(ai.swap(hands[n], nine), 4, n);

		} else {// else start game
			nextTurn();
			displayTable();
		}
	}

	/*---------------------------------------------
	 * sending detail of other players to other players
	 * table layout
	 *     1
	 *  0     2
	 3 <- dealer
	 *--------------------------------------------*/
	private void sendDetails() {
		String command = "otherdetails:" + otherNames[1] + ":"
				+ hands[1].getFaceUp(0).getNumber() + ":"
				+ hands[1].getFaceUp(1).getNumber() + ":"
				+ hands[1].getFaceUp(2).getNumber() + ":" + otherNames[2] + ":"
				+ +hands[2].getFaceUp(0).getNumber() + ":"
				+ hands[2].getFaceUp(1).getNumber() + ":"
				+ hands[2].getFaceUp(2).getNumber() + ":" + playersName + ":"
				+ hands[3].getFaceUp(0).getNumber() + ":"
				+ +hands[3].getFaceUp(1).getNumber() + ":"
				+ hands[3].getFaceUp(2).getNumber() + ":";
		sendCommand(command, 0);

		command = "otherdetails:" + otherNames[2] + ":"
				+ hands[2].getFaceUp(0).getNumber() + ":"
				+ hands[2].getFaceUp(1).getNumber() + ":"
				+ hands[2].getFaceUp(2).getNumber() + ":" + playersName + ":"
				+ +hands[3].getFaceUp(0).getNumber() + ":"
				+ hands[3].getFaceUp(1).getNumber() + ":"
				+ hands[3].getFaceUp(2).getNumber() + ":" + otherNames[0] + ":"
				+ hands[0].getFaceUp(0).getNumber() + ":"
				+ +hands[0].getFaceUp(1).getNumber() + ":"
				+ hands[0].getFaceUp(2).getNumber() + ":";
		sendCommand(command, 1);

		command = "otherdetails:" + playersName + ":"
				+ hands[3].getFaceUp(0).getNumber() + ":"
				+ hands[3].getFaceUp(1).getNumber() + ":"
				+ hands[3].getFaceUp(2).getNumber() + ":" + otherNames[0] + ":"
				+ +hands[0].getFaceUp(0).getNumber() + ":"
				+ hands[0].getFaceUp(1).getNumber() + ":"
				+ hands[0].getFaceUp(2).getNumber() + ":" + otherNames[1] + ":"
				+ hands[1].getFaceUp(0).getNumber() + ":"
				+ +hands[1].getFaceUp(1).getNumber() + ":"
				+ hands[1].getFaceUp(2).getNumber() + ":";
		sendCommand(command, 2);
	}

	public void endConnection() {
		listen = false;

		if (listenSocket != null)
			try {
				listenSocket.close();
			} catch (IOException e) {
				sh.addMsg("Error Closing Listen " + e);
			}

		for (int n = 0; n < 3; n++) {
			if (gameSocket[n] != null) {
				out[n].println("end");
				try {
					gameSocket[n].close();
				} catch (IOException e2) {
					sh.addMsg("Error Closing Msg " + e2);
				}
			}
		}

		socketCount = 0;
		sh.addMsg("Game Connections Closed");
	}

	public void destroy() {
		gameID++;
		endConnection();
	}

	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: " + decklength(), 365, 20);
		g.drawString("Pile: " + pilelength(), 365, 40);
		hands[3].showHand();

		g.drawRoundRect(5, 360, 90, 40, 15, 15);
		g.drawString("Name: " + otherNames[0], 10, 375);
		g.drawString("Cards: " + (hands[0].length() - 1), 10, 395);
		g.drawImage(pointer[1], 68, 380, sh);
		if (hands[0].getFaceUp(0) != null)
			hands[0].getFaceUp(0).drawSideWays(tableposition[0][0]);
		else if (hands[0].getFaceDown(0) != null)
			g.drawImage(backSW, (int) tableposition[0][0].getX(),
					(int) tableposition[0][0].getY(), sh);
		if (hands[0].getFaceUp(1) != null)
			hands[0].getFaceUp(1).drawSideWays(tableposition[0][1]);
		else if (hands[0].getFaceDown(1) != null)
			g.drawImage(backSW, (int) tableposition[0][1].getX(),
					(int) tableposition[0][1].getY(), sh);
		if (hands[0].getFaceUp(2) != null)
			hands[0].getFaceUp(2).drawSideWays(tableposition[0][2]);
		else if (hands[0].getFaceDown(2) != null)
			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: " + (hands[1].length() - 1), 10, 40);
		g.drawImage(pointer[2], 70, 25, sh);

		if (hands[1].getFaceUp(0) != null)
			hands[1].getFaceUp(0).drawCard(tableposition[1][0]);
		else if (hands[1].getFaceDown(0) != null)
			g.drawImage(back, (int) tableposition[1][0].getX(),
					(int) tableposition[1][0].getY(), sh);
		if (hands[1].getFaceUp(1) != null)
			hands[1].getFaceUp(1).drawCard(tableposition[1][1]);
		else if (hands[1].getFaceDown(1) != null)
			g.drawImage(back, (int) tableposition[1][1].getX(),
					(int) tableposition[1][1].getY(), sh);
		if (hands[1].getFaceUp(2) != null)
			hands[1].getFaceUp(2).drawCard(tableposition[1][2]);
		else if (hands[1].getFaceDown(2) != null)
			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);// 365
		g.drawString("Cards: " + (hands[2].length() - 1), 360, 395);// 365
		g.drawImage(pointer[1], 423, 380, sh);
		if (hands[2].getFaceUp(0) != null)
			hands[2].getFaceUp(0).drawSideWays(tableposition[2][0]);
		else if (hands[2].getFaceDown(0) != null)
			g.drawImage(backSW, (int) tableposition[2][0].getX(),
					(int) tableposition[2][0].getY(), sh);
		if (hands[2].getFaceUp(1) != null)
			hands[2].getFaceUp(1).drawSideWays(tableposition[2][1]);
		else if (hands[2].getFaceDown(1) != null)
			g.drawImage(backSW, (int) tableposition[2][1].getX(),
					(int) tableposition[2][1].getY(), sh);
		if (hands[2].getFaceUp(2) != null)
			hands[2].getFaceUp(2).drawSideWays(tableposition[2][2]);
		else if (hands[2].getFaceDown(2) != null)
			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;

⌨️ 快捷键说明

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