d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With Java Blackjack Project
12Next
Add Reply New Topic New Poll
Member
Posts: 9,664
Joined: Dec 22 2007
Gold: 845.30
Dec 6 2015 09:41pm
Let me know if you could just help me out with a couple of methods if you have some time. majority of the project is working.

can pay fg if needed
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2015 09:43pm
post whatever you need help with.
Member
Posts: 9,664
Joined: Dec 22 2007
Gold: 845.30
Dec 6 2015 09:49pm
Code
public boolean equals(Card c) {
if(this.type == c.type && this.suit == c.suit)
return true;

//does this look like a correct equals method? this type meaning the type of card (2,3,king,ace) and suit being the suit of card(spade, hearts)
//i think it is, just double checking

//this method is supposed to shift the card right in an array so then whenever i add to bottom it has a spot at [0]
private void shiftRightOne() {
if (numCardsInDeck != 52){
for(int i = numCardsInDeck - 1; i > 0; i--){
cards[I]= cards[i + 1];
}
}
else{
System.out.println("Error, deck full.");
}
}
public void addToBottom(Card c) {
if (numCardsInDeck != 52){
shiftRightOne();
c = cards[0];
numCardsInDeck++;}
else{
System.out.println("Error, deck full.");}
}

this method id supposed to add the card to [0]

does anyone see anything that just looks like it wouldn't work?[/I]

This post was edited by vunel on Dec 6 2015 09:50pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2015 09:59pm
i have a hard time figuring out how much of that is a copy/paste problem.

for one thing, this isn't doing what you think it's doing. your object c isn't used for anything, so i'd say that's probably your problem.
Code
c = cards[0];


what is this upper case I? don't have a lower case variable and an upper case of the same variable.
Code
cards[I]= cards[i + 1];


there are two kinds of devs: those who log and those who debug. if you're not comfortable with debugging, i suggest you learn to log. add more print statements so you see what's happening

/edit i'd also recommend using an arraylist instead of an array. to remove a card, you can simply call the remove method and not have to write it yourself.

This post was edited by carteblanche on Dec 6 2015 10:03pm
Member
Posts: 9,664
Joined: Dec 22 2007
Gold: 845.30
Dec 6 2015 10:07pm
what is the c = cards[0] doing?

and the cards is actually lowercase, it changed whenever i copied over i think
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2015 10:09pm
Quote (vunel @ Dec 6 2015 11:07pm)
what is the c = cards[0] doing?

and the cardsis actually lowercase, it changed whenever i copied over i think


i'll briefly remove the rest of the code so you can focus on when it's used:
Code
public void addToBottom(Card c) {
c = cards[0];
}


so you start with an object in the c variable. so far so good. now the line you're asking about is actually assigning a different card into your c variable, which completely stomps whatever you started the function with. however, you're not actually using c anywhere else. so essentially it does absolutely nothing.
Member
Posts: 9,664
Joined: Dec 22 2007
Gold: 845.30
Dec 6 2015 10:14pm
Well c is added in from the main method correct? that is where it is being used. I should be assigning c to the location of cards[0]. How would I do that? and I'm not allowed to use an array list for the project.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2015 10:17pm
Quote (vunel @ Dec 6 2015 11:14pm)
Well c is added in from the main method correct? that is where it is being used. I should be assigning c to the location of cards[0]. How would I do that? and I'm not allowed to use an array list for the project.


at this moment, c is a local variable that you're not using. no matter what you assign to it, c will go out of scope once the method ends.

if you want cards[0] to contain c, then you need to do so:
cards[0] = c;

notice the difference?
Member
Posts: 9,664
Joined: Dec 22 2007
Gold: 845.30
Dec 6 2015 10:19pm
Quote (carteblanche @ Dec 7 2015 12:17am)
at this moment, c is a local variable that you're not using. no matter what you assign to it, c will go out of scope once the method ends.

if you want cards[0] to contain c, then you need to do so:
cards[0] = c;

notice the difference?



That does make sense, thank you. Sadly that didn't fix my error. Maybe it is within my draw method?
Code
public Card draw() {

if(numCardsInDeck > 0){
numCardsInDeck--;
Card r = cards[numCardsInDeck];
return r;

}
else{
System.out.println("Error.");
return null;
}

}

I saw a note on my group website that I should be removing the card from the deck too, not just decrementing numCardsInDeck.

edit: this is whenever you draw the card from the top of the list [52..51?]

This post was edited by vunel on Dec 6 2015 10:26pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 6 2015 10:33pm
Quote (vunel @ Dec 6 2015 11:19pm)
That does make sense, thank you. Sadly that didn't fix my error. Maybe it is within my draw method?
Code
public Card draw() {

if(numCardsInDeck > 0){
numCardsInDeck--;
Card r = cards[numCardsInDeck];
return r;

}
else{
System.out.println("Error.");
return null;
}

}

I saw a note on my group website that I should be removing the card from the deck too, not just decrementing numCardsInDeck.

edit: this is whenever you draw the card from the top of the list [52..51?]


you have multiple functions. my recommendation is to test each method separate from the rest. you should verify that each piece works correctly.

i'd also suggest some sort of log feature so you know exactly what's in the deck at any point. something like so:
Code
void logDeck(){
log("numberOfCards: " + numCardsInDeck);
for (var i = 0; i < numCardsInDeck; i++){
log(i + ": " + cards[i]);
}
}


to remove the card from your deck, simply set it to null.

This post was edited by carteblanche on Dec 6 2015 10:37pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll