| | |
War card game
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 13
Reputation:
Solved Threads: 0
Here is the full code thus far:
If you were to run this, it would display one turn of war (e.g. both players put down one card).
What I need help on is this:
1.) Play a full game, not just one turn.
2.) If Abe wins the turn, then the won cards go to the bottom of pileAbe. If Bob wins the turn, then the won cards go to the bottom of pileBob. If a battle occures, certain things will happen (see #3 below)
3.) If a Battle:
a.) get 4 cards from each pile (including the first card...so a total of 5 from each pile)
b.) compare the 4th:
- if Abe wins, all 10 cards go to bottom of pileAbe.
- if Bob wins, all 10 go to bottom of pileBob.
- if they draw the same card again, then Battle again.
c.) A triple Battle halts the game (System.exit(0))
4.) Play ends (System.exit(0)) when one pile falls below an easily changed number (e.g. END_LIMIT = 8) or when a file has too few cards to engage in a Battle (e.g. a double battle).
Thanks to anyone who can help me get this working as a real game!
Java Syntax (Toggle Plain Text)
public class Cardmain { public static void main (String[] args) { War wargame = new War(); wargame.play(); } } class Deck { static final int numberOfCards = 52; Card[] cards = new Card[numberOfCards]; int index; public Deck() { // Constructs all 52 Card objects for (int i = 0;i < numberOfCards; i++) cards[i] = new Card(i); } public void shuffle() { // Shuffles the deck. Card temp; int a; for (int x = 0; x < numberOfCards; x++) { a = (int)(Math.random() * numberOfCards); temp = cards[x]; cards[x] = cards[a]; cards[a] = temp; } } public Card getNextCard() { // Gets the next card in the deck. if (index >= numberOfCards) index = 0; int temp = index; index++; return cards[temp]; } } class Card { // The values 0-12 represent clubs, 13-25 diamonds, // 26-38 hearts, and 39-51 spades. Within each suit, // the lowest value is the ace and highest is the king. int suit = 0; // 0 through 3 int rank = 0; // 0 through 12 public Card(int s, int r) { // Sets suit and rank if (s <= 12) { suit = 0; rank = r; } else if (s >= 13 && s <= 25) { suit = 1; rank = r % 13; } else if (s >= 26 && s <= 38) { suit = 2; rank = r % 26; } else if (s >= 39 && s <= 51) { suit = 3; rank = r % 39; } } public Card(int number) { this(number, number); } public String toString() { // Returns a string representation of a Card in // a format like so: The Ace of Clubs String theCard; String[] cardRank = {"Ace","Two","Three","Four","Five","Six","Seven", "Eight","Nine","Ten","Jack","Queen","King"}; String[] cardSuit = {"Clubs","Diamonds","Hearts","Spades"}; // cardSuit is not needed for War: theCard = (cardRank[rank]); return theCard; } public int compareWar(Card other) { if ((rank == 0) && (other.rank != 0)) return 1; else if ((other.rank == 0) && (rank != 0)) return -1; else if (rank > other.rank) return 1; else if (other.rank > rank) return -1; else return 0; } } public class War { public final int END_LIMIT = 8; private Deck warDeck; private Vector pileAbe = new Vector(30,22); //set capacity at 30 private Vector pileBob = new Vector(30,22); //increment by 22 if needed public War(Deck gameDeck) { warDeck = gameDeck; warDeck.shuffle(); //Shuffles deck } public void play() { for(int i = 0;i < Deck.numberOfCards; i+=2) pileAbe.addElement(warDeck.getNextCard()); for(int i = 0;i < Deck.numberOfCards; i+=2) pileBob.addElement(warDeck.getNextCard()); System.out.println("Abe = " + (Card)pileAbe.elementAt(0)); System.out.println("Bob = " + (Card)pileBob.elementAt(0)); int test = ((Card)pileAbe.firstElement()).compareWar((Card)pileBob.firstElement()); if (test < 0) System.out.println("Bob wins!"); else if (test > 0) System.out.println("Abe wins!"); else System.out.println("Battle!"); //The 2 cards match System.out.println("=========="); } }
What I need help on is this:
1.) Play a full game, not just one turn.
2.) If Abe wins the turn, then the won cards go to the bottom of pileAbe. If Bob wins the turn, then the won cards go to the bottom of pileBob. If a battle occures, certain things will happen (see #3 below)
3.) If a Battle:
a.) get 4 cards from each pile (including the first card...so a total of 5 from each pile)
b.) compare the 4th:
- if Abe wins, all 10 cards go to bottom of pileAbe.
- if Bob wins, all 10 go to bottom of pileBob.
- if they draw the same card again, then Battle again.
c.) A triple Battle halts the game (System.exit(0))
4.) Play ends (System.exit(0)) when one pile falls below an easily changed number (e.g. END_LIMIT = 8) or when a file has too few cards to engage in a Battle (e.g. a double battle).
Thanks to anyone who can help me get this working as a real game!
Well to start with number one do you know how to use loops thats how I'd do it if you don't ask.
PETA People for the Eating of Tasty Animals.
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
Do you still have that code?
PETA People for the Eating of Tasty Animals.
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
FireFox
Hijack This
Ad-Aware
Hijack this tutorial
Microsoft AntiSpyware
CompUchat
•
•
Join Date: Oct 2004
Posts: 13
Reputation:
Solved Threads: 0
The code was just adding a for loop in the following fashion. It seems if I add a for loop inside play() to test...oh say 5 times, it messes up after the first turn....it gets "stuck" on Abe for some reason (test=1). To do this, add: for(int x = 0; x < 5; x++){ on top of: System.out.println("Abe = " + (Card)pileAbe.elementAt(0)). Close the for loop after: System.out.println("==========") with a }.
Little pointless to post in the thread which is over 2 years old and author and some members are not active for same time
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- [C#] Project War(The Card Game) (C#)
- C++ card game war (C++)
- Making War card game (C)
- card game (Java)
- Card Game (Java)
Other Threads in the Java Forum
- Previous Thread: How do I make Java games for cell phones
- Next Thread: Help with menu-driven java applications please
Views: 7840 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
6 android api apple applet application arguments array arrays automation binary bluetooth bold byte c++ chat class classes client code component coordinates database datagram doctype draw eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework html ide ideas image ingres input integer internet intersect ip j2me java javaexcel javaprojects jmf jni jpanel jtextarea julia linux list loop map method methods mobile netbeans newbie nextline number object oracle pong print problem program programming project recursion recursive scanner screen sell server set size sms socket sort sql string swing test threads time transfer tree user web websites windows






