Forums Register Login

War Game

+Pie Number of slices to send: Send
I am trying to write a program to play War. We have been charged to do the following;

Create a virtual "War" card game. The way it needs to work is the player and the computer each get 5 cards. The player and the computer each lay a card down - whoever has the higher card wins that hand and takes both the cards. This continues until someone has all of the cards. That is the winner.

We need to

Create a class named Card. This class consists of the following:
1. A String instance variable named suit, which holds the suit of the card (Hearts, Diamonds, Clubs, Spades)
2. A String instance variable named number, which holds the number of the card (2,3,4,...9,10,Jack,Queen,King,Ace)
3. A constructor that contains the following;
1. A String array that holds all of the suits
2. A String array that holds all of the numbers
3. Generates a random number that randomly represents the suit and assigns that suit to the suit instance variable using the suits array (think about this - the random number is an int and the array is full of Strings)
4. Generates a random number that randomly represents the number and assigns that number to the number instance variable using the suits array

4. A method named getCard that takes the Sstring number and converts it to an integer so it can be used to compare values when the game begins. Your task is to figure out the logic for this method.

Create a class named CardTestDrive. This class is responsible for playing the game.
1. The player and the computer start off with 5 cards each.
2. Every time there is a "War", instntiate a player card object and a computer card object
3. Using the objects' instance variables and method, compare the two cards number values. If it is a tie, disregard that particular war.
4. Based upon who wins, update the number of cards each player has.

My can't figure out how to make the Card class convert the string numbers over to integers and return the values so that I can use them in the CardTestDrive.

What am I doing wrong. This time I tried attaching the Java files rather that posting the code inside my question.





+Pie Number of slices to send: Send
to start, your Card class is wrong - you have a collection there, so that class should be a 'deck'.
a 'card' is only one of them.

you'll probably also need a 'hand' class.
+Pie Number of slices to send: Send
 

Chris Hembel wrote:



What is 'z' for? Instead, you should be setting 'getCard' equal to Integer.parseInt (number) like such:

+Pie Number of slices to send: Send
First to Michael

I know that it appears that the Card class is wrong and if I was truly writing a full fledged War Game I would probably need many more classes to really get it to work. However, our instructor is just trying to get us to make this game very simple. We are in beginning programming and we just need to get it to work with a total of 10 cards. A true War Game would include 52 cards.

My real problem is getting the getCard method to convert the random string that is selected for each of the cards. I realize that I am going to have to do something with the places that are holding words instead of numbers but I just want it to return a number on those times when there is a number selected.

Joel

I did have the method with the getCard = Integer.parseInt (number); line in the code. However it still does not return the correct integer.

I really appreciate any other suggestions you could give me,
+Pie Number of slices to send: Send
 

Chris Hembel wrote:
I did have the method with the getCard = Integer.parseInt (number); line in the code. However it still does not return the correct integer.



Alright, then that means the problem lies elsewhere. What you had originally for that line was definitely what you want. If that's not working, though, then you need to debug the program to find out what's not going as planned. I'd start with adding System.out.println(number); as the first line in your getCard() method. That way you'll be able to see whether or not 'number' is what you expect it to be.

EDIT: Also I found a little mistake in your syntax. You use computer.getCard in an if statement in your main method. However, when calling a method, you use parenthesis. It should be computer.getCard(). Before, you were calling the variable 'getCard'.
+Pie Number of slices to send: Send
> My real problem is getting the getCard method to convert the random string that is selected for each of the cards.

remove the random stuff from the Card constructor.

in getCard() get a random number 0 to 51

the 'suit' is randomNumber/13 (which will be 0 to 3 = index number of StringSuit[])
the rank is randomNumber % 13 (which will be 0 to 12 = index number of StringNumber[])
+Pie Number of slices to send: Send
I think I have this solved. At least well enough that I can pass the homework in.

Thanks for all of the suggestions. They did help.

Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6718 times.
Similar Threads
check my code please?
Blackjack program not good with random cards
Error
Logging problem and grammatical problems
Help With 5 card stud program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:00:41.