java实践1——poker游戏

这篇博客记录了作者在大一下学期进行的一项实践项目——使用Java编程实现扑克牌游戏的详细过程,旨在保存并分享代码与经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
大一下的实践之一,保存一下代码。

package First;

import java.text.*;
import javax.swing.JOptionPane;

public class AY2016PokerProject
{
	static String userMessage = "";
	static final int handSize = 5;
	static double totalWinnings = 0.0;

	public static void main(String[] args)
	{
		int[] cards = new int[handSize];
		int[] suits = new int[handSize];
		int[] values = new int[handSize];
		int winType;
		String selection = "", playAgain = "";
		do
		{
			while (!(selection.equals("1")) && !(selection.equals("0")))

			{
				selection = JOptionPane.showInputDialog(null,
						"1. Play Poker\n" + "0. Quit\n" + "Please enter your selection");

				if (selection == null)
					selection = "0";
				if (!(selection.equals("1")) && !(selection.equals("0")))

					JOptionPane.showMessageDialog(null, "Invalid selection - please try again\n");
			}
			if (selection.equals("1"))
			{
				userMessage = "";

				generateUniqueHand(cards);
				determineSuitsAndValuesOfCards(cards, suits, values);
				orderValuesAndSuits(suits, values);
				displayCardsToEndUser(suits, values);
				winType = evaluateHandOfCards(suits, values);
				displayTypeOfWinIfAny(winType);
				displayAmountWonIfAnything(winType);
				JOptionPane.showMessageDialog(null, userMessage);
				do
				{
					playAgain = JOptionPane.showInputDialog(null, "Would you like to play another game? (y/n)");

					if (playAgain == null)
						playAgain = "N";
					else
						playAgain = playAgain.toUpperCase();
				} while (!(playAgain.equals("Y")) && !(playAgain.equals("N")));

			}
			if (selection.equals("0"))
				playAgain = "N";
		} while (playAgain.equals("Y"));
	}

	public static void generateUniqueHand(int[] cards)

	{
		int deckSize = 52;
		int uniqueNumbersRequired = handSize, aRandomNumber;
		int index = 0, duplicateIndex;
		while (index < uniqueNumbersRequired)
		{
			aRandomNumber = (int) (Math.random() * deckSize);
			cards[index] = aRandomNumber;
			duplicateIndex = 0;
			while (cards[duplicateIndex] != aRandomNumber)
				duplicateIndex++;
			if (index == duplicateIndex)
				index++;
		}
	}

	public static void determineSuitsAndValuesOfCards(int[] cards, int[] suits, int[] values)
	{
		for (int i = 0; i < handSize; i++)
		{
			suits[i] = cards[i] / 13;
			values[i] = cards[i] % 13;
		}
	}

	public static void orderValuesAndSuits(int[] suits, int[] values)

	{
		int pass, comparison, temp;
		boolean sorted = false;
		for (pass = 1; pass <= (handSize - 1) && !sorted; pass++)
		{
			sorted = true;
			for (comparison = 1; comparison <= (handSize - pass); comparison++)
			{
				if (values[comparison - 1] < values[comparison])
				{
					temp = values[comparison - 1];
					values[comparison - 1] = values[comparison];
					values[comparison] = temp;
					temp = suits[comparison - 1];
					suits[comparison - 1] = suits[comparison];
					suits[comparison] = temp;
					sorted = false;
				}
			}
		}
	}

	public static void displayCardsToEndUser(int[] suits, int[] values)
	{

		for (int i = 0; i < handSize; i++)
		{
			switch (values[i]) {
			case 0:
				userMessage += "Two of ";
				break;
			case 1:
				userMessage += "Three of ";
				break;
			case 2:
				userMessage += "Four of ";
				break;
			case 3:
				userMessage += "Five of ";
				break;
			case 4:
				userMessage += "Six of ";
				break;
			case 5:
				userMessage += "Seven of ";
				break;
			case 6:
				userMessage += "Eight of ";
				break;
			case 7:
				userMessage += "Nine of ";
				break;
			case 8:
				userMessage += "Ten of ";
				break;
			case 9:
				userMessage += "Jack of ";
				break;
			case 10:
				userMessage += "Queen of ";
				break;
			case 11:
				userMessage += "King of ";
				break;
			case 12:
				userMessage += "Ace of ";
				break;
			}

			switch (suits[i]) {
			case 0:
				userMessage += "Clubs\n";
				break;
			case 1:
				userMessage += "Diamonds\n";
				break;
			case 2:
				userMessage += "Hearts\n";
				break;
			case 3:
				userMessage += "Spades\n";
				break;
			}
		}
	}

	public static int evaluateHandOfCards(int[] suits, int[] values)
	{
		int winType = 0;
		if (cardsOfSameSuit(suits))
		{
			if (cardsInConsecutiveDescendingSequence(values))
			{
				if (values[0] == 12)
					winType = 9;
				else
					winType = 8;
			} else
				winType = 7;
		} else
		{
			if (cardsInConsecutiveDescendingSequence(values))
				winType = 5;
			else
				winType = checkOtherPossibleCombinations(values);
		}
		return winType;
	}

	public static boolean cardsOfSameSuit(int suits[])
	{
		boolean sameSuit = true;
		for (int i = 0; (i < suits.length - 1) && sameSuit; i++)
			if (suits[i] != suits[i + 1])
				sameSuit = false;
		return sameSuit;
	}

	public static boolean cardsInConsecutiveDescendingSequence(int values[])
	{
		boolean consecutiveCards = true;
		for (int i = 0; i < values.length - 1 && consecutiveCards; i++)
			if (values[i] != values[i + 1] + 1)
				consecutiveCards = false;
		return consecutiveCards;
	}

	public static int checkOtherPossibleCombinations(int[] values)
	{
		boolean continueCardComparison;
		int sameKind = 0;
		for (int i = 0; (i < values.length - 1); i++)
		{
			continueCardComparison = true;
			for (int j = i + 1; j < values.length && continueCardComparison; j++)
			{
				if (values[i] == values[j])
					sameKind++;
				else
					continueCardComparison = false;
			}
		}
		return sameKind;
	}

	public static void displayTypeOfWinIfAny(int winType)
	{

		switch (winType) {
		case 0:
			userMessage += "\nNot a winning hand\n";
			break;
		case 1:
			userMessage += "\nOne pair\n";
			break;
		case 2:
			userMessage += "\nTwo pair\n";
			break;
		case 3:
			userMessage += "\nThree of a kind\n";
			break;
		case 4:
			userMessage += "\nFull house\n";
			break;
		case 5:
			userMessage += "\nStraight\n";
			break;
		case 6:
			userMessage += "\nFour of a kind\n";
			break;
		case 7:
			userMessage += "\nFlush\n";
			break;
		case 8:
			userMessage += "\nStraight flush\n";
			break;
		case 9:
			userMessage += "\nRoyal flush\n";
			break;
		}
	}

	public static void displayAmountWonIfAnything(int winType)
	{

		double[] money = { 0.0, 0.1, 0.2, 0.3, 0.6, 0.4, 0.7, 0.5, 0.8, 0.9 };
		NumberFormat aFormatter = NumberFormat.getCurrencyInstance();
		userMessage += "\nFor this hand you win: " + aFormatter.format(money[winType]);
		totalWinnings += money[winType];
		userMessage += "\nTotal winnings to date are: " + aFormatter.format(totalWinnings);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值