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. :)
Related
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();
}
}
I'm building an application and i am trying to find a way to get data from one class to another class.
in my main class called Driver, i have a function which throws two dices and then i get a sum of them. This function is created in a class called Dices.
I have a new class called Players, where i need to get the data from the dice throw from my main class.
The code is the following
public class Player {
public String spillernavn;
public int pos;
public String SpillerNavn() {
return spillernavn;
}
public int move(int steps){
move=??;
pos=steps+pos;
return ??;
}
public int SpillerPos() {
return pos;
}
}
The Methode public int move(int steps){} is where i need to use the data from the dice throw and then make the addition with the pos, which stands for position.
The main function is the following
public static void main(String[] args) {
//Intializing dices
Dice die1 = new Dice();
Dice die2 = new Dice();
//Summ of dice
int sum = die1.throwDice() + die2.throwDice();
File name: Dice.java
import java.util.Random;
public class Dice {
private Random random;
public Dice() {
random = new Random();
}
public int throwDice() {
int num = 0;
while(num == 0) // Dice doesn't have a zero, so keep looping unless dice has a zero.
num = random.nextInt(7); // returns a random number between 0 - 6
return num;
}
}
File name: Players.java
public class Players {
private int sum;
public Players(int sum) {
this.sum = sum; // You got the data from `Driver class` here.
// You got the sum of two dice. Now do whatever you want to.
}
}
public class Driver {
public static void main(String[] args) {
Dice dye1 = new Dice();
Dice dye2 = new Dice();
int sum = dye1.throwDice() + dye2.throwDice();
Players player = new Player(sum); // Passing the data to Class Players
}
}
To send data, you need to pass the argument in the ()
You could do something like this.
public class Player {
public String spillernavn;
public int pos;
public String SpillerNavn() {
return spillernavn;
}
public int move(int steps, int move){
move=move;
pos=steps+pos;
return ??;
}
public int SpillerPos() {
return pos;
}
}
Then in main:
public static void main(String[] args) {
//Intializing dices
Dice die1 = new Dice();
Dice die2 = new Dice();
//Summ of dice
int sum = die1.throwDice() + die2.throwDice();
player.move(steps, move);
For future reference. You should probably create an instance of your dice in your player class. It's simple passing data between classes. Getters and setters.
Basically I made I class to extend the grid to 11 by 11, the constructor is being called however its not changing the grid. Any input or information on why would be greatly appreciated. I've posted my code below:
import info.gridworld.actor.*;
import info.gridworld.grid.BoundedGrid;
public class ChangeGrid extends ActorWorld{
private static final int SIZEROW = 11;
private static final int SIZECOL = 11;
public ChangeGrid()
{
super(new BoundedGrid<Actor>(SIZEROW, SIZECOL));
System.out.println("test");
}
}
import info.gridworld.actor.Bug;
public class XBug extends Bug {
/**
* A <code>BoxBug</code> traces out a square "box" of a given size. <br />
* The implementation of this class is testable on the AP CS A and AB exams.
*/
private int steps;
private int sideLength;
private int x = 0;
private static ChangeGrid object = new ChangeGrid();
/**
* Constructs a box bug that traces a square of a given side length
* #param length the side length
*/
public XBug(int length)
{
steps = 0;
sideLength = length;
}
/**
* Moves to the next location of the square.
*/
public void act()
{
if (steps < sideLength && canMove())
{
if(x==0)
{
turn();
}
move();
steps++;
x++;
}
else
{
turn();
steps = 0;
}
}
}
Your constructor is not being called when you create the world. You are calling it inside of an Actors class, then you don't even call the .show() method. You just need to change your runner class to call ChangeWorld instead of ActorWorld.
Here's an example for your code.
public class Runner
{
public static void main(String[]args)
{
ChangeWorld world = new ChangeWorld();
XBug bug = new XBug();
world.add(bug);
world.show();
}
}
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).
I am developing a blackberry app in jdp plugin for eclipse.I want to store some values froma na array in the flash memory of blackberry device,& also check whether dat value already exits in the memory or not.I am giving the code which i tried to do with persistent object,bt somehw i am nt able to get want i want,plz modify the code where reqd
package com.firstBooks.series7.db;
import java.util.Random;
import com.firstBooks.series7.AppMain;
import com.firstBooks.series7.db.parser.XMLParser;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
public class DBMain {
public static String answer = "";
public static String selectedAnswer = "";
public static Question curQuestion;
public static int currQuesNumber = 1;
public static int correctAnswerCount = 0;
public static int totalNumofQuestions = 50 ;
static int quesNum[] = new int[20];
static int quesNumNew[];
static int quesCount = -1;
static int randomPosition;
static PersistentObject store;
static {
store = PersistentStore.getPersistentObject( 0xf9f8c7a20bc35c51L);
}
static{
initialize();
}
private static void initialize(){
Random rgen = new Random(); // Random number generator
//--- Initialize the array
for (int i=0; i<quesNum.length; i++) {
quesNum[i] = i;
}
//--- Shuffle by exchanging each element randomly
for (int i=0; i< quesNum.length; i++) {
randomPosition = rgen.nextInt(quesNum.length);
int temp = quesNum[i];
quesNum[i] = quesNum[randomPosition];
quesNum[randomPosition] = temp;
synchronized(store) {
if(quesNum[randomPosition]!=quesNum[i]){
System.out.println("...........i can do it............ ");
store.setContents(quesNum);
store.commit();
}
}
}
}
/*Changed the code to get a unique random number
* #author: Venu
*/
public static int getQuestionNumber() {
quesCount++;
if(quesCount < quesNum.length){
synchronized(store) {
int [] quesNumNew = (int[])store.getContents();
return quesNumNew[quesCount];
}
}
else{
initialize();
quesCount = -1;
return getQuestionNumber();
}
}
}
What is the problem you are encountering? Did you try to wrap the array in an object that implements Persistable interface? It is like the Serializable interface in j2se.
also see:
http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/util/Persistable.html
"A class must explicitly implement this interface for the system to persistently store instances of the class."