I am trying to create a card game of war. However, for this game, there will be an additional card pile called "trump." If either player 1 or 2 has a card that is references in the trump pile then it is an automatic win regardless of the rank. At the moment, I am stuck with the logic.
In a class called CardPile here is the constructor and the methods.
public CardPile(Card[ ] initialCards)
{
pile = new ArrayList<Card>();
for (int i=0; i<initialCards.length; i++)
pile.add(initialCards[i]);
}
public void add(Card aCard)
{
pile.add(aCard);
}
public Card get(int index)
{
return pile.get(index);
}
In my class called TrumpWar
protected CardPile tCard;
protected CardPile cp;
protected CardPile p1;
protected CardPile p2;
public TrumpWar( )
{
cp = new CardPile (new Card[52]);
cp.shuffle();
tCard = new CardPile ();
for (int i=1; i<7; i++) //<---Stuck.
{
tCard.add(tCard.get(i)); //<---error
}
cp.shuffle();
p1 = new CardPile(new Card [26]);
p2 = new CardPile(new Card [26]);
}
When I run the game I am getting a NullPointerException, and I am pretty sure that is because I am not passing anything into the trump pile. When I try to put in an int for the trump ArrayList I would get an error int cannot be converted to Card [].
How can I get the top six cards from the deck of 52 without removing them just storing them as references, and adding them to the trump pile?
Moreover, am I declaring the player1, player2, and the cardpile correctly?
I greatly appreciate the help, thank you.
You should replace with:
for (int i=0; i<6; i++)
{
tCard.add(cp.get(i));
}
You were trying to get cards from the empty tCard.
Note that this code, would still not work until you call cp = new CardPile(array) where array actually contains cards that are not null. Otherwise, tCard.add(cp.get(0)) would not add the reference to the first card, but just null
Card Class:
public class Card {
Integer i = new Integer(0);
Card(Integer is) {
this.i = is;
}
}
CardPile class:
public class CardPile {
ArrayList<Card> pile = null;
public CardPile(Integer no)
{
pile = new ArrayList<Card>();
for (int i=1; i<=no; i++) {
pile.add(new Card(i));
}
}
public void add(Card aCard)
{
pile.add(aCard);
}
public Card get(int index)
{
return pile.get(index);
}
}
TrumpWar class:
public class TrumpWar {
protected CardPile tCard;
protected CardPile cp;
protected CardPile p1;
protected CardPile p2;
public TrumpWar( )
{
cp = new CardPile (52); // only passing the no of cards to be created.
//cp.shuffle();
tCard = new CardPile(52); // only passing the no of cards to be created.
for (int i=1; i<7; i++)
{
tCard.add(tCard.get(i));
}
// cp.shuffle();
p1 = new CardPile(26);
p2 = new CardPile(26);
}
public static void main(String a[]){
new TrumpWar();
}
}
Related
I'm creating a blackjack game in Java. I need to have multiple players and need a hand class to store the cards that have been pulled from the deck. I have a hand class that functions, but even when I create two separate hand instances, dealing a card to either hand adds them to both hands.
This is my hand class code:
public class Hand2 {
private List<Cards> hand;
private Cards cards;
private int handValue;
public Hand2(List<Cards> hand) {
this.hand = hand;
}
private Cards addCard(Deck deck) {
hand.add(deck.dealCard());
return cards;
}
public int getHandValue() {
for (Cards cards : hand ) {
handValue += cards.getValue();
}
return handValue;
}
public String toString() {
return "Hand: " + hand;
}
And below I am testing it:
public static void main(String[] args) { //Testing
List<Cards> cards = new ArrayList<Cards>();
Deck deck = new Deck();
deck.shuffle();
Hand2 hand = new Hand2(cards);
Hand2 hand2 = new Hand2(cards);
hand.addCard(deck);
hand2.addCard(deck);
hand2.addCard(deck);
System.out.println(hand2);
System.out.println(hand.getHandValue());
System.out.println(hand2.getHandValue());
}
Terminal:
Hand: [Three of Diamonds, Four of Clubs, Jack of Hearts]
17
17
But I get the same hand value for either hand.
As some have already noted, the same list of cards is shared between all hands. Another problem I see in you code is that you're using fields (cards, handValue) when you should use local variables.
Try this:
public class Hand2 {
private final List<Cards> hand = new ArrayList<>();
public Hand2() {
}
private Cards addCard(Deck deck) {
Cards cards = deck.dealCard();
hand.add(cards);
return cards;
}
public int getHandValue() {
int handValue = 0;
for (Cards cards : hand ) {
handValue += cards.getValue();
}
return handValue;
}
#Override
public String toString() {
return "Hand: " + hand;
}
I have been trying to make a game for a friend, but I'm having a problem with getting my line player[i].setName(getName(pn)); in class Players to work. I want to be able to set the names of the players, or change them, in the list. but I keep getting errors at this line. This happened after i changed the public variables in class Player from static.
"Exception in thread "main" java.lang.NullPointerException
at worldhomicide.drinkinggame.PlayerInfo.Players.setPlayers(Players.java:16)
at worldhomicide.drinkinggame.main.Game.main(Game.java:25)"
Any help would be greatly appreciated! I posted all needed code below.
Game Class
public class Game{
public static void main(String[] args) {
MessageHandler.gameRules(); // Display Game Information
Players.getAmount();Players.setPlayers(); // Get player data
System.out.println("What player would you like to look up?");
int choice = Integer.parseInt(EventHandler.keyboard.next()); choice -= 1;
System.out.println(Players.player[choice].name);
}
}
Players Class
public class Players extends EventHandler {
public static int playerAmount;
public static Player[] player;
public static void setPlayers(){
player = new Player[playerAmount];
for(int i = 0; i < player.length; i++){
int pn = i+1;
player[i].setName(getName(pn));
}
}
public static void getAmount(){
MessageHandler.playerAmount();
playerAmount = Integer.parseInt(keyboard.next());
}
}
Class EventHandler
public class EventHandler {
public static Scanner keyboard = new Scanner(System.in);
public static String getName(int playerNumber){
System.out.println("What is player " + playerNumber + "'s name?");
String name = keyboard.next();
return name;
}
}
Player Class
public class Player {
public String name;
public int score;
public void setName(String name){
this.name = name;
}
}
Note that in your setPlayers() method, inside the loop, you didn't create Player object before accessing the player[i].setName() method.
for(int i = 0; i < player.length; i++){
int pn = i+1;
player[i] = new Player(); //you need to create Player object
player[i].setName(getName(pn));
}
I'm super new to programming so excuse me if my code is hard to understand. I was assigned to program a game to deal a card to the user and being it is the top card. I already have my card class made. I just need to know if I'm doing this correctly. It says build successful, but doesn't show anything. Please help!
package deck;
import java.util.*;
/**
*
* #author useradmin
*/
// Write a description of class Deck here.
public class Deck {
private Card[] theCards;
private int deal;
public Deck() {
theCards = new Card[52];
deal = 52;
this.fill();
//fill();
}
public int deal() {
return deal;
}
public Card getCard() {
Card a = null;
a = theCards[deal-1];
deal--;
return a;
}
public String toString()
{
String deckString = "New deck shuffled\n";
for(int i = 1; i <= 1; i++)
{
deckString += theCards[i].toString() + "\n";
}
return deckString;
}
public void shuffleCards() {
Random random = new Random();
Card temp;
int topCard;
for(int i = 0; i<30; i++){
topCard = random.nextInt(deal);
}
}
private void fill() {
int i, j;
int index = 0;
for(i = 0; i <4; i++) {
for(j = 1; j < 14; j++){
theCards[index] = new Card(i, j);
index++;
}
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
{
}
}
The public static void main(String[] args) method is the entry point for your program. When you run your program, this is the first method that gets called. Yours is empty, so nothing will happen.
Solution
Make a new class and call it Application.
Cut the public static void main(String[] args){} method from your Deck class and paste it into your new Application class.
Inside the main() you will need to put some code! I suggest creating a Deck object and then printing the contents of the deck using your toString() method, just so that you can see that everything is working.
Your new class should look like this:
public class Application {
//Main method (Entry point for program)
public static void main(String[] args) {
Deck myDeck = new Deck(); //Create Deck
System.out.println(myDeck.toString()); //Print contents of Deck
}
}
Make sure that you have removed the main() method from your Deck class .
Hope that helps you out. :)
During a test case, if I am trying to call upon methods (placeOnTop() for example), which should test which deck the card is from. I am not sure how this constructor is labeling/ or if at all creating different types of decks...
public class StandardDeck implements Deck {
List<Card> cards = new ArrayList<>();
public StandardDeck() {
for (Suit suit : Suit.values()) {
for (int rank = 1; rank <= 13; rank++) {
Card e = new StandardCard(suit, rank, this);
cards.add(e);
}
}
}
public void placeOnTop(Card c) {
cards.add(0, c);
}
public Card takeTop() {
return cards.remove(0);
}
}
You have no 'labeled' field but every time you call StandardDeck() will return a new StandardDeck object which holds its very own cards object, which is of type List.
I am trying to test my player class properly, I have almost done it but I am having issues with my p1.setPlayerHand method. This is the following code I have used for my player class:
Player Class:
package model;
public class Player
{
private String PlayerName;
private Hand PlayerHand;
private boolean Dealer;
public Player(String name)
{
PlayerName = name;
PlayerHand = new Hand();
Dealer = false;
}
public void setName (String name)
{
this.PlayerName = name;
}
public String getName()
{
return PlayerName;
}
public void setDealer (Boolean dealer)
{
this.Dealer = dealer;
}
public boolean getDealer()
{
return Dealer;
}
public void setPlayerHand (Hand hand)
{
this.PlayerHand = hand;
}
public void getHand()
{
PlayerHand.displayCardsinHand();
}
public static void main (String [] args)
{
Player p1 = new Player("player1");
Hand h = new Hand();
//System.out.println(p1);
p1.setName("BARRY");
System.out.println(p1.getName());
p1.setDealer(false);
System.out.println(p1.getDealer());
//this is the error that is preventing my program to run
p1.setPlayerHand(h.addCard(new Card(Suit.CLUBS, CardRank.ACE)));
p1.getHand();
}
}
The following error I receive (after testing the Player Class) is this:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setPlayerHand(Hand) in the type Player is not applicable for the arguments (void)
at model.Player.main(Player.java:57)
This is the Hand Class underneath (that is linked to the Player Class):
Hand Class:
package model;
import java.util.Vector;
import java.util.Random;
public class Hand
{
private Vector<Card> hand;
public Hand()
{
hand = new Vector<Card>();
}
public void addCard(Card c)
{
hand.add(c);
}
public void displayCardsinHand()
{
for (int card = 0; card < hand.size(); card++)
{
System.out.println(hand.elementAt(card));
}
}
public int getCardsinHand()
{
return hand.size();
}
public Card getCard(int position)
{
if(position >= 0 && position < hand.size())
return (Card)hand.elementAt(position);
else
return null;
}
public int getScore()
{
int value = 0;
boolean ace = false;
for (int i = 0; i < hand.size(); i++)
{
Card c;
c = getCard(i);
value = value + c.getRankValue();
if(c.getRankValue() == 1)
{
ace = true;
}
}
if(ace == true && value + 10 <= 21)
{
value = value + 10;
}
return value;
}
public static void main (String [] args)
{
Hand h = new Hand();
System.out.println(h);
h.displayCardsinHand();
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.HEARTS, CardRank.ACE));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.SPADES, CardRank.JACK));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.DIAMONDS, CardRank.QUEEN));
System.out.println(h.getCardsinHand());
h.addCard(new Card(Suit.CLUBS, CardRank.KING));
System.out.println(h.getCardsinHand());
System.out.println(h.getCardsinHand());
h.displayCardsinHand();
h.getCard(1);
System.out.println(h.getScore());
}
}
I have tried modifying the p1.setPlayerHand testing numerous times. I appreciate any advice and tips on how to solve this issue, thank you.
If my code is too long for this post then I will gladly accept any advice on what I should do to cut it short (for future reference).
If anyone here required to see any other classes that I wrote (that may help them help me solve this error) then please notify me on here, thank you.
The method addCard doesn't return anything (void). So you can't pass the result of this method to setPlayerHand(Hand). That's what you're doing.
The code should compile and run if you change
p1.setPlayerHand(h.addCard(new Card(Suit.CLUBS, CardRank.ACE)));
to
h.addCard(new Card(Suit.CLUBS, CardRank.ACE));
p1.setPlayerHand(h);
This is because the setPlayerHand method needs to be passed an object of type Hand, but the addCard method doesn't return anything (it's declared as void).