- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想用 Java 制作 BlackJack 游戏。我使用数组来表示卡片。我在获取牌值并使用每个玩家必须计算的牌值来计算每个玩家的手牌时遇到问题。我有四个类:Card、Player、Dealer 和 BlackJackGame(驱动程序)。我将发布 Card 及其相关的 Value 方法、Player 和 BlackJackGame,因为庄家与 Player 非常相似。
//Card.Java
public class Card
{
private String suit, rank;
private int value;
public Card(String suit, String rank)
{
this.suit = suit;
this.rank = rank;
}
public String getRank()
{
return rank;
}
public int Value()
{
if(rank.equals("2"))
{
value=2;
}
else if(rank.equals("3"))
{
value=3;
}
else if(rank.equals("4"))
{
value=4;
}
else if(rank.equals("5"))
{
value=5;
}
else if(rank.equals("6"))
{
value=6;
}
else if(rank.equals("7"))
{
value=7;
}
else if(rank.equals("8"))
{
value=8;
}
else if(rank.equals("9"))
{
value=9;
}
else if(rank.equals("10"))
{
value=10;
}
else if(rank.equals("A"))
{
value=11;
}
else if(rank.equals("Q"))
{
value=10;
}
else if(rank.equals("J"))
{
value=10;
}
else if(rank.equals("K"))
{
value=10;
}
return value;
}
public String toString()
{
return(rank + " of " + suit);
}
}
//Player.java
//Player.java
public class Player
{
private int cValue; // [Card Value] I use this in a method to equal what deck[int] produces and try to use in the getValue method to no avail
private int cCount; //Card count used to count how many 'cards' added
Card[] deck= new Card[52]; // 52 card objects
private int sum; A temporary int I add the cValues into and assign the value to cValue and return it
public Player()
{
cCount=0;
}
public void addCard(Card a) //Everytime addCard is executed so I know how many cards are drawn at this point in the program everyone( 3 players and a dealer) has two cards
{
deck[cCount] = a;
cCount++;
}
public int getcCount() //Get the card count from the void method
{
return cCount;
}
public Card getCard(int a) //Return the deck integer each player has
{
return deck[a];
}
public int getCardValue(int a) // This works and it produces the value of the card I give the int of the method too however if I use more than two of these in succession, I get a null pointer exception, can't figure it out.
{
cValue = deck[a].Value();
return cValue;
}
public void getValue(int a) //The method I can't get to work, trying to calculate the hand of the player(
{
for(int i =0; i<deck.length; i++)
{
sum += cValue;
}
}
public int getValue() // I want to make this the method where the values are summed and return but for some reason no matter what I do I get 0 returned, tried everything.. I really need help with this method.
{
cValue = sum;
return cValue;
}
}
//BlackJackGame.java
public class BlackJackGame
{
public static void main(String [] args)
{
Card[] deck = new Card[52];
Player[] player = new Player[3];
int loopcount=0;
String[] suit = {"Hearts", "Clubs", "Spades", "Diamonds"};
String[] rank = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for(int i=0; i<13; i++)
{
for(int x=0; x<4;x++)
{
deck[loopcount] = new Card(suit[x], rank[i]);
loopcount++;
}
}
System.out.println("Shuffling...");
for(int i=0; i< deck.length; i++) //Shuffle
{
int count= (int)(Math.random()* deck.length);
deck[i] = deck[count];
}
Player player1 = new Player();
Player player2 = new Player();
Player player3 = new Player();
System.out.println("Welcome to our BlackJackGame!");
System.out.println("Welcome Dealer!");
Dealer dealer = new Dealer();
System.out.println("Let's deal the cards!");
player1.addCard(deck[0]);
player2.addCard(deck[1]);
player3.addCard(deck[2]);
// System.out.println("Player 1 has: " +deck[0]);
// System.out.println("Player 2 has: " +deck[1]);
// System.out.println("Player 3 has : " +deck[2]);
System.out.println("And now the Dealer gets his card...");
dealer.addCard(deck[3]);
// System.out.println("The Dealer has: " +deck[3]);
System.out.println("Now we get our second cards!");
System.out.println("Okay Dealer, deal out the cards!");
player1.addCard(deck[4]);
player2.addCard(deck[5]);
player3.addCard(deck[6]);
dealer.addCard(deck[7]);
System.out.println("These are the cards player1 has: " +deck[0]+ " "+deck[4]);
System.out.println("These are the cards player2 has: " +deck[1]+ " "+deck[5]);
System.out.println("These are the cards player3 has: " +deck[2]+ " "+deck[6]);
int p1 = player1.getCardValue(0);
int p2 = player2.getCardValue(1);
int p3 = player3.getCardValue(2); // This points to null, why?!
System.out.println(p1);
System.out.println(p2);
输出
Shuffling...
*Some print lines of stuff I wrote*
These are the cards this player has: ... ...
Exception in thread "main" java.lang.NullPointerException
at Player.getCardValue(Player:java:39)
at BlackJackGame.main(BlackJackGame.java:85)
最佳答案
您只向player3的deck[]
添加了两张牌,因此当您调用public int getCardValue(2)
时,它会转到player3牌组的第三个索引,这是空的。
Player player3 = new Player();
...
player3.addCard(deck[2]);
// deck[0]
...
player3.addCard(deck[6]);
// deck[1]
...
int p3 = player3.getCardValue(2);
// get value at deck[2]
// this goes to ->
public int getCardValue(int a)
{
// try to access deck[2], but deck only has
// 2 valid entries deck[0] and deck[1]
cValue = deck[a].Value();
return cValue;
}
其他事情...
不是每个玩家都有一个名为 deck
的数组,可以将其重命名为 hand
并使用 ArrayList
而不是 array
,ArrayList
允许您动态添加元素。
我会在您的 Card.Value()
方法中使用 switch
语句。就像这样:
public int Value()
{
switch(rank) {
case "A":
return 11;
case "K":
case "Q":
case "J":
return 10;
default:
return Integer.parseInt(rank);
}
你的洗牌算法几乎是正确的。
for(int i=0; i< deck.length; i++) //Shuffle
{
// this line chooses a random card in the deck
int count= (int)(Math.random()* deck.length);
// this line sets the card at index 'i' to the randomly chosen card
deck[i] = deck[count];
// however your creating multiple instance of one card in the deck
// instead of switching the cards around, this will lead to your deck
// having more than one of the same card.
}
它应该看起来像这样:
for(int i=0; i< deck.length; i++) //Shuffle
{
// create a temporary card to hold the value of the card to switch
Card tmp = deck[i];
// now choose a random card in the deck
int count= (int)(Math.random()* deck.length);
// now set the card at index 'i' to the randomly chosen card
deck[i] = deck[count];
// and set the randomly chosen card to deck[i]
deck[count] = tmp;
}
我可以用 getValue 方法来计算牌局吗?使用 cCount
中的数组,您可以有一种计算手牌总数的方法:
public int calcHandTotal() {
int total = 0;
for(int i = 0; i < cCount; i++) {
total += deck[i].Value();
}
return total;
}
关于Java问题计算BlackJack程序中玩家手牌的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26941961/
我正在尝试使用 flot 绘制 SQL 数据库中的数据图表,这是使用 php 收集的,然后使用 json 编码的。 目前看起来像: [{"month":"February","data":482},
我有一个来自 php 行的 json 结果,类似于 ["value"]["value"] 我尝试使用内爆函数,但得到的结果是“value”“value” |id_kategori|created_at
脚本 1 将记录 two 但浏览器仍会将 select 元素呈现为 One。该表单还将提交值 one。 脚本 2 将记录、呈现和提交 两个。我希望它们是同义词并做同样的事情。请解释它们为何不同,以及我
我的python字典结构是这样的: ips[host][ip] 每行 ips[host][ip] 看起来像这样: [host, ip, network, mask, broadcast, mac, g
在 C# 中 我正在关注的一本书对设置和获取属性提出了这样的建议: double pri_test; public double Test { get { return pri_test; }
您可能熟悉 enum 位掩码方案,例如: enum Flags { FLAG1 = 0x1, FLAG2 = 0x2, FLAG3 = 0x4, FLAG4 = 0x8
在一些地方我看到了(String)value。在一些地方value.toString() 这两者有什么区别,在什么情况下我需要使用哪一个。 new Long(value) 和 (Long)value
有没有什么时候 var result = !value ? null : value[0]; 不会等同于 var result = value ? value[0] : null; 最佳答案 在此处将
我正在使用扫描仪检测设备。目前,我的条形码的值为 2345345 A1。因此,当我扫描到记事本或文本编辑器时,输出将类似于 2345345 A1,这是正确的条形码值。 问题是: 当我第一次将条形码扫描
我正在读取 C# 中的资源文件并将其转换为 JSON 字符串格式。现在我想将该 JSON 字符串的值转换为键。 例子, [ { "key": "CreateAccount", "text":
我有以下问题: 我有一个数据框,最多可能有 600 万行左右。此数据框中的一列包含某些 ID。 ID NaN NaN D1 D1 D1 NaN D1 D1 NaN NaN NaN NaN D2 NaN
import java.util.*; import java.lang.*; class Main { public static void main (String[] args) thr
我目前正在开发我的应用程序,使其设计基于 Holo 主题。在全局范围内我想做的是工作,但我对文件夹 values、values-v11 和 values-v14. 所以我知道: values 的目标是
我遇到了一个非常奇怪的问题。 我的公司为我们的各种 Assets 使用集中式用户注册网络服务。我们一般通过HttpURLConnection使用请求方法GET向Web服务发送请求,通过qs设置参数。这
查询: UPDATE nominees SET votes = ( SELECT votes FROM nominees WHERE ID =1 ) +1 错误: You can't specify
如果我运行一段代码: obj = {}; obj['number'] = 1; obj['expressionS'] = 'Sin(0.5 * c1)'; obj['c
我正在为我的应用创建一个带有 Twitter 帐户的登录页面。当我构建我的项目时会发生上述错误。 values/strings.xml @dimen/abc_text_size_medium
我在搜索引擎中使用以下 View : CREATE VIEW msr_joined_view AS SELECT table1.id AS msr_id, table1.msr_number, tab
为什么验证会返回此错误。如何解决? ul#navigation li#navigation-3 a.current Value Error : background-position Too
我有一个数据名如下 import pandas as pd d = { 'Name' : ['James', 'John', 'Peter', 'Thomas', 'Jacob', 'Andr
我是一名优秀的程序员,十分优秀!