gpt4 book ai didi

java - Java 中的 BlackJack 与庄家玩家具有相同的牌组

转载 作者:行者123 更新时间:2023-12-01 04:46:52 27 4
gpt4 key购买 nike

我正在用 Java 编写一个经典的二十一点游戏程序。

规则是一样的,我们作为玩家和庄家(CPU)在一定的规则下进行选择。

我的代码,即下面的,制作了 2 堆独立的牌组,一组用于玩家,一组用于经销商,每组都从不同的牌组中抽取,但​​我想让它们(玩家和经销商)都从相同的套牌。

对我的代码有任何建议/更正吗?

import java.util.Random;
import java.util.Scanner;


public class River
{

private int CardNumber;

private int BeginCards;

private int Decks;

private int[] PartialSumArray = {4,8,12,16,20,24,28,32,36,52};

private int[] BeginPartialSumArray = {4,8,12,16,20,24,28,32,36,52};

private int PickedCard;

private Random randomGenerator = new Random();

//Constructor without definition
public River()
{
CardNumber = 52;
BeginCards = 52;
}

//Constructor with definition
public River(int Decks)
{
CardNumber = Decks * 52;

BeginCards = CardNumber;
this.Decks = Decks;

//Initialize partial sum array for many decks of cards
for (int i=0; i<10; i++)
{
PartialSumArray[i] = PartialSumArray[i] * Decks;
BeginPartialSumArray[i] = PartialSumArray[i] * Decks;
}
System.out.println();
}

//Create random numbers
private int computeRandomSteps(int CardNumber)
{
//System.out.print("stin random , cardnumber is" + CardNumber);
int randomSteps = randomGenerator.nextInt(CardNumber-1);
return randomSteps;
}


public int nextCard()
{

int steps = computeRandomSteps(CardNumber);
int position=0;

for (int i=0; i<CardNumber; i++)
{
if (steps<= PartialSumArray[i])
{
position = i+1;
break;
}
}

CardNumber--;
return position;
}


public int start()
{
int ShuffleLimit;
PickedCard = nextCard();
System.out.println("Picked card is :" + PickedCard);

int HelpVariable = PickedCard-1;

for (int i=0; i<10; i++)
{
if (i >= HelpVariable)
{
PartialSumArray[HelpVariable] = PartialSumArray[i]-1;
HelpVariable++;
}
}


ShuffleLimit = BeginCards/4;

if (CardNumber<ShuffleLimit)
{
for (int i=0; i<9; i++)
{
BeginPartialSumArray[i] = BeginPartialSumArray[i] * Decks;
}
}
return PickedCard;
}


public int ReturnCardNumber()
{
System.out.println("return cardnumber is " + CardNumber);
return CardNumber;
}

}



class Hand
{

private int points;
private int SumPoints=0;
private boolean Ace = true;


Scanner input = new Scanner(System.in);
//Scanner input3 = new Scanner(System.in);

//int Decks = input3.nextInt();

River myRiver = new River();
//River myRiver = new River(Decks);



public int getPoints()
{
points = myRiver.start();
if (points == 1 && Ace)
{
System.out.println("It is an Ace. Do you want to count 1 or 11?");

points = input.nextInt();
Ace = false;
}
SumPoints += points;
System.out.println("Points are : " + SumPoints);
return SumPoints;
}

public int getPointsDealer()
{
points = myRiver.start();
if (points == 1 && Ace)
{
if (SumPoints + 11 > 21)
{
points = 1;
}
else
{
points = 11;
}
Ace = false;
}
SumPoints += points;
System.out.println("Points are : " + SumPoints);
return SumPoints;
}
}


class Player
{
private int points;
private double account=0;
private double bet;
private boolean WinOrLose;

Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

public double placeBet()
{
System.out.println("How much do you want to bet?");
bet = input1.nextDouble();

return bet;
}

public double profit(boolean WinOrLose)
{
if (WinOrLose)
{
account += bet;
return account;
}
else
{
account -= bet;
return account;
}
}


public int play(River other)
{

Hand myHand = new Hand();
bet = placeBet();
points = myHand.getPoints();
boolean end = true;
String Choice;

while (end)
{
System.out.println("Make a choice");
Choice = input2.nextLine();


switch(Choice)
{
case "DoubleBet":
bet = bet *2;
points = myHand.getPoints();

if (points > 21)
{
System.out.print("Burned!");
WinOrLose = false;
account = profit(WinOrLose);
end = false;
break;
}
else if (points == 21)
{
System.out.print("You won!");
WinOrLose = true;
account = profit(WinOrLose);
end = false;
break;
}
else
{
System.out.println("Your points are :" + points);
end = false;
break;
}

case "stop":
System.out.println("Your points are :" + points);
end = false;
break;

case "Hit":
points = myHand.getPoints();
if (points > 21)
{
System.out.print("Burned!");
WinOrLose = false;
account = profit(WinOrLose);
end = false;
break;
}
else if (points == 21)
{
System.out.print("You won!");
WinOrLose = true;
account = profit(WinOrLose);
end = false;
break;
}
break;

default:
System.out.println("That is not a choice.");
end = false;
break;
}

}
return points;
}
}

class BlackJack
{

public static void main(String args[])
{
int SumPointsPlayer;
int SumPointsDealer;
boolean WinOrLose = true;
double account;
int Decks;
int BeginCards;
int ThisMomentCards;

Scanner input1 = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);

System.out.println("How many decks do you want to begin with?");
Decks = input1.nextInt();


River myRiver = new River(Decks);
Player myPlayer = new Player();

//Calculate the cards we have when the game starts
BeginCards = 52 * Decks;

System.out.println("Do you want to start the game? Yes or No.");
String Repeat;

Repeat = input2.nextLine();

while (Repeat.equals("Yes"))
{
ThisMomentCards = myRiver.ReturnCardNumber();
System.out.println("Cards are : " + ThisMomentCards);

//Player's points for 1 round
SumPointsPlayer = myPlayer.play(myRiver);

//If player catches 21 he wins instantly
if(SumPointsPlayer == 21)
{
account = myPlayer.profit(WinOrLose);
System.out.println("Your account has :" + account + "dollars!");
}
//If player catches >21 he loses instantly
else if(SumPointsPlayer > 21)
{
WinOrLose = false;
account = myPlayer.profit(WinOrLose);
System.out.println("Your account has :" + account + "dollars!");
}
//Compare the hand of player and dealer and the bigger wins
else
{
//Dealer's points for 1 round
SumPointsDealer = playDealer(myRiver);

//If dealer catches >21 he loses instantly
if(SumPointsDealer>21)
{
System.out.println("Player wins!");
account = myPlayer.profit(WinOrLose);
System.out.println("Your account has :" + account + "dollars!");
}
//Hand of player bigger than the hand of the dealer , player wins
else if (SumPointsPlayer>SumPointsDealer)
{
WinOrLose = true;
account = myPlayer.profit(WinOrLose);
System.out.println("Player wins. Your account has :" + account + "dollars!");

}
//Hand of player smaller than the hand of the dealer , dealer wins
else if (SumPointsPlayer<SumPointsDealer)
{
WinOrLose = false;
account = myPlayer.profit(WinOrLose);
System.out.println("Player lost. Your account has :" + account + "dollars!");
}
//Hand of player is equal with the hand of the dealer , it is tie
else
{
System.out.println("Player and Dealer are tie!!");
}
}
System.out.println("Do you want to continue the game? Yes or No.");
Repeat = input2.nextLine();
}

}

public static int playDealer(River other)
{
boolean bountry = true;

System.out.println("Dealer plays :");

Hand myHand = new Hand();
int SumPointsDealer = myHand.getPointsDealer();

while (bountry)
{
if (SumPointsDealer<17)
{
SumPointsDealer = myHand.getPointsDealer();
}
else if (SumPointsDealer>21)
{
System.out.println("Dealer burned!");
bountry = false;
}
else
{
bountry = false;
}
}
return SumPointsDealer;
}
}

一些说明:

1)我们随机抽牌的方式是基于一种奇怪的方式,但这不是问题,程序确实从牌组中随机抽牌的方式是可以的

2)我注意到的另一个问题是,在 Hand 类中,//中的代码不起作用,因为它不允许我拥有 System.out.println()

Scanner input = new Scanner(System.in);
//Scanner input3 = new Scanner(System.in);

//int Decks = input3.nextInt();

River myRiver = new River();
//River myRiver = new River(Decks);

我想这样做,以便我能说出用户想要玩多少套牌

最佳答案

您可以更好、更轻松地编写面向对象的代码。即 Collections.shuffle(Deck) 取代了整个随机难题其中 Deck 是您创建的对象,由 LinkedList 和计数器 [deck value] 等属性组成希望有帮助。

从主牌组的链接列表中删除对象卡并将其移至玩家的牌组中。是的,您可以通过这种方式创建任意数量的套牌。

对象卡具有属性“值”和“花色”。

关于java - Java 中的 BlackJack 与庄家玩家具有相同的牌组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15690782/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com