War Game

Hi,
I was wondering if anyone could help me to make the java game War. I have these two classes, the Deck class and Card class:
import java.util.*;
public class Deck {
// Instance Variables
private Card[] theDeck = new Card[52];
// Set up random number generator (an instance variable because we use it in multiple methods)
private Random generator = new Random();
/** Creates a new instance of Deck */
public Deck() {
// Initialize the deck by filling it with 52 cards in order
for (int currCard = 0; currCard < 13; currCard++)
// Spades first
theDeck[currCard] = new Card(currCard, 0);
// Then Hearts
theDeck[currCard+13] = new Card(currCard, 1);
// Then Clubs
theDeck[currCard+26] = new Card(currCard, 2);
// And finally Diamonds
theDeck[currCard+39] = new Card(currCard, 3);
public Card getTopCard()
// The 0th card is the one on the "top" of the deck
return theDeck[0];
public Card getRandomCard()
// Calculate a random number between 0 and 51
int randomCard = generator.nextInt(51);
// Return the card at that array index of the deck
return theDeck[randomCard];
public void shuffle()
final int NUM_EXCHANGES = 500;
// My shuffle method (admittedly not very interesting) works by exchanging
// pairs of cards at random locations in the deck. The more exchanges you
// make (by changing the constant in the for-loop) the more "shuffled" your deck is.
// We need to declare a temporary Card variable to hold one Card as it is
// being exchanged.
Card tempCard;
for (int currExchange = 0; currExchange < NUM_EXCHANGES; currExchange++)
// Get two random indices in the deck
int firstIndex = generator.nextInt(51);
int secondIndex = generator.nextInt(51);
// Exchange the cards at those locations
tempCard = new Card(theDeck[firstIndex].getRank(), theDeck[firstIndex].getSuit());
theDeck[firstIndex] = new Card(theDeck[secondIndex].getRank(), theDeck[secondIndex].getSuit());
theDeck[secondIndex] = new Card(tempCard.getRank(), tempCard.getSuit());
public String toString()
String toReturn = "";
// Print the card at each index of the deck using the toString method in Card
for (int currCard = 0; currCard < 52; currCard++)
toReturn += theDeck[currCard].toString() + "\n";
return toReturn;
public class Card {
// Instance Variables
private int rank;
private int suit;
/** Creates a new instance of Card */
public Card(int pRank, int pSuit) {
rank = pRank;
suit = pSuit;
// Accessors
public int getRank()
return rank;
public int getSuit()
return suit;
// Mutators
public void setRank(int pRank)
rank = pRank;
public void setSuit(int pSuit)
suit = pSuit;
// toString method
public String toString()
// String values of suit and rank
String sSuit = "", sRank = "";
// Set String value of suit
if (suit == 0)
sSuit = "Spades";
else if (suit == 1)
sSuit = "Hearts";
else if (suit == 2)
sSuit = "Clubs";
else if (suit == 3)
sSuit = "Diamonds";
// Set String value of rank
if (rank == 0)
sRank = "Ace";
else if (rank == 10)
sRank = "Jack";
else if (rank == 11)
sRank = "Queen";
else if (rank == 12)
sRank = "King";
else
sRank = "" + (rank + 1); // if the rank is numerical, turn it into a String by
// concatenating it with the empty string. Remember that we
// have to add one to compensate for starting at 0
// Return String value of suit and rank
return sRank + " of " + sSuit;
}

import java.util.*;
public class Deck {
// Instance Variables
private Card[] theDeck = new Card[52];
// Set up random number generator (an instance variable because we use it in multiple methods)
private Random generator = new Random();
/** Creates a new instance of Deck */
public Deck() {
// Initialize the deck by filling it with 52 cards in order
for (int currCard = 0; currCard < 13; currCard++)
// Spades first
theDeck[currCard] = new Card(currCard, 0);
// Then Hearts
theDeck[currCard+13] = new Card(currCard, 1);
// Then Clubs
theDeck[currCard+26] = new Card(currCard, 2);
// And finally Diamonds
theDeck[currCard+39] = new Card(currCard, 3);
public Card getTopCard()
// The 0th card is the one on the "top" of the deck
return theDeck[0];
public Card getRandomCard()
// Calculate a random number between 0 and 51
int randomCard = generator.nextInt(51);
// Return the card at that array index of the deck
return theDeck[randomCard];
public void shuffle()
final int NUM_EXCHANGES = 500;
// My shuffle method (admittedly not very interesting) works by exchanging
// pairs of cards at random locations in the deck. The more exchanges you
// make (by changing the constant in the for-loop) the more "shuffled" your deck is.
// We need to declare a temporary Card variable to hold one Card as it is
// being exchanged.
Card tempCard;
for (int currExchange = 0; currExchange < NUM_EXCHANGES; currExchange++)
// Get two random indices in the deck
int firstIndex = generator.nextInt(51);
int secondIndex = generator.nextInt(51);
// Exchange the cards at those locations
tempCard = new Card(theDeck[firstIndex].getRank(), theDeck[firstIndex].getSuit());
theDeck[firstIndex] = new Card(theDeck[secondIndex].getRank(), theDeck[secondIndex].getSuit());
theDeck[secondIndex] = new Card(tempCard.getRank(), tempCard.getSuit());
public String toString()
String toReturn = "";
// Print the card at each index of the deck using the toString method in Card
for (int currCard = 0; currCard < 52; currCard++)
toReturn += theDeck[currCard].toString() + "\n";
return toReturn;
public class Card {
// Instance Variables
private int rank;
private int suit;
/** Creates a new instance of Card */
public Card(int pRank, int pSuit) {
rank = pRank;
suit = pSuit;
// Accessors
public int getRank()
return rank;
public int getSuit()
return suit;
// Mutators
public void setRank(int pRank)
rank = pRank;
public void setSuit(int pSuit)
suit = pSuit;
// toString method
public String toString()
// String values of suit and rank
String sSuit = "", sRank = "";
// Set String value of suit
if (suit == 0)
sSuit = "Spades";
else if (suit == 1)
sSuit = "Hearts";
else if (suit == 2)
sSuit = "Clubs";
else if (suit == 3)
sSuit = "Diamonds";
// Set String value of rank
if (rank == 0)
sRank = "Ace";
else if (rank == 10)
sRank = "Jack";
else if (rank == 11)
sRank = "Queen";
else if (rank == 12)
sRank = "King";
else
sRank = "" + (rank + 1); // if the rank is numerical, turn it into a String by
// concatenating it with the empty string. Remember that we
// have to add one to compensate for starting at 0
// Return String value of suit and rank
return sRank + " of " + sSuit;
}You put your code between the tags & replace {} with []

Similar Messages

  • I can no longer get Mafia War game to open I get a white page only ..all other games still open on Firefox..please help

    I use Mozilla Firefox on my computer..I play Mafia War games and several more..now when I try to open MW I get a white page, all other games still open and work fine...I do not know how to fix this, can you help..thanks

    Hi there,
    You're running an old version of Safari. Before troubleshooting, try updating it to the latest version: 6.0. You can do this by clicking the Apple logo in the top left, then clicking Software update.
    You can also update to the latest version of OS X, 10.8 Mountain Lion, from the Mac App Store for $19.99, which will automatically install Safari 6 as well, but this isn't essential, only reccomended.
    Thanks, let me know if the update helps,
    Nathan

  • Looking for old Mac game -- War game like EMPIRE DELUXE

    In 1984 or so, I played a Mac war game that was very much like Empire Deluxe for the PC. It had units that you would move on a grid-like map. You captured cities and used them to make more units. One of the units was a nuclear bomber. You would click where you wanted to detonate the bomb on the map and the program would ding twice and blot out a space the size of a dime. Later in the game, the size increased to the size of a quarter. The game was played on a Mac +. Anyone know this game?
    Darren

    You need abandonware:
    * http://mac.the-underdogs.info/
    lots of old mac games. Copyright may or may not apply, although they have been around since the 90s without cease and desist. Check their LINKS page.
    And if you understand French, then:
    * http://www.abandonware-france.org/
    has limited mac stuff (currently offline, btw, will be back) while:
    * http://www.freeoldies.com/
    may be of use.

  • Ending Java War game with while loop

    How would i go about ending a java war game witha while loop. I want the program to run until all of the values of one of the arrays of one of the players split decks has all null values.

    GameObject[] theArrayInQuestion;
    // populate array somehow or other
    void methodWithLoop() {
       while(arrayNotNull(theArrayInQuestion)) {
       // do stuff
    boolean arrayNotNull(Object[] array) {
      int len = array.length;
      for(int i = 0; i < len; i++) {
        if ( array[i] != null ) {
          return true;
      return false
    }

  • LEGO STAR WARS game  & powerbook G4 1.67Ghz- installation problem

    I purchased the LEGO Star Wars game to use on my macs. It installs and plays on the G5 Powermac ok but won't install properly on my G4 Powerbook.
    I have been in touch with Aspyr the game developer who say they have had no reports of a similar issue. My G4 powerbook is well beyond the spec required to run the game according to Aspyr themselves.
    I was just wondering if there is any way of analysing what it is that is stopping the game from installing properly? I have tried setting up a 'clean' user account and installing the game in that accound in case it is some of the software I have on the powerbook which may be conflicting with the installation. Is there a way of trying to work out what the problem is so that I can get this game running?
    I have tried using the force as well which seemed appropriate given the nature of the game but the force is not strong in this one!

    Mobile GPUs have frequently not had the same power as their desktop counterparts. And based on:
    http://store.apple.com/us/product/TE185LL/A
    It runs best on a G5, which says to me it is optimized for 64 bit processing. I would ask Aspyr to either rewrite the specs to say whether or not Mobility chips have been tested, and/or say if they think it should run on G4s.
    Also, check to see if you have any background tasks, third party peripherals, or wireless connections which may be slowing down your machine. Check your Apple menu -> System Preferences -> Accounts -> select your active account and check your Login items. If there are any there, delete them (don't just uncheck them) and see if it makes a difference.

  • Help with war game

    I am learning java and was looking at this program I don't understand what is wrong with it, I am getting an error that says something about the compareTo cannot be found and variable cannot be found.
    class Card implements Comparable
    Card(int r, int s)
    rank = r;
    suit = s;
    int getRank()
    return rank;
    int getSuit()
    return suit;
    public int compareTo(Object ob)
    Card other = (Card)ob;
    int thisRank = this.getRank();
    int otherRank = other.getRank();
    if (thisRank == 1) thisRank = 14; // make aces high
    if (otherRank == 1) otherRank = 14;
    return thisRank - otherRank;
    public boolean equals(Object ob)
    if (ob instanceof Card)
    Card other = (Card)ob;
    return value==other.value && suit==other.suit;
    else return false;
    public String toString()
    String val;
    String [] suitList =
    { "", "Clubs", "Diamonds", "Hearts", "Spades" };
    if (rank == 1) val = "Ace";
    else if (rank == 11) val = "Jack";
    else if (rank == 12) val = "Queen";
    else if (rank == 13) val = "King";
    else val = String.valueOf(rank); // int to String
    String s = val + " of " + suitList[suit];
    for (int k=s.length()+1; k<=17; k++) s = s + " ";
    return s;
    private int rank;
    private int suit;
    class CardDeck
    CardDeck()
    deck = new Card [52];
    fill();
    void shuffle()
    for (int next = 0; next < numCards-1; next++)
    int r = myRandom(next, numCards-1);
    Card temp = deck[next];
    deck[next] = deck[r];
    deck[r] = temp;
    Card deal()
    if (numCards == 0) return null;
    numCards--;
    return deck[numCards];
    int getSize()
    return numCards;
    private void fill()
    int index = 0;
    for (int r = 1; r <= 13; r++)
    for (int s = 1; s <= 4; s++)
    deck[index] = new Card(r, s);
    index++;
    numCards = 52;
    private static int myRandom(int low, int high)
    return (int)((high+1-low)*Math.random()+low);
    private Card [] deck;
    private int numCards;
    class Pile
    Pile()
    pile = new Card[52];
    front = 0; end = 0;
    int getSize()
    return end - front;
    void clear()
    front = 0; end = 0;
    void addCard(Card c)
    pile[end] = c;
    end++;
    void addCards(Pile p)
    while (p.getSize() > 0)
    addCard(p.nextCard());
    Card nextCard()
    if (front == end)
    return null; // should not happen
    Card c = pile[front];
    front++;
    return c;
    private Card [] pile;
    private int front, end; // front &#8804; end
    class Player
    Player(String n)
    name = n;
    playPile = new Pile();
    wonPile = new Pile();
    Card playCard()
    if (playPile.getSize() == 0)
    useWonPile();
    if (playPile.getSize() > 0)
    return playPile.nextCard();
    return null;
    String getName()
    return name;
    void collectCard(Card c)
    wonPile.addCard?;
    void collectCards(Pile p)
    wonPile.addCards(p);
    void useWonPile()
    playPile.clear(); // reset front and end to 0
    playPile.addCards(wonPile);
    wonPile.clear(); // reset front and end to 0
    int numCards()
    return playPile.getSize() + wonPile.getSize();
    private Pile playPile, wonPile;
    private String name;
    class Game
    void play()
    CardDeck cd = new CardDeck();
    cd.shuffle();
    p1 = new Player("Ernie");
    p2 = new Player("Burt");
    while (cd.getSize() >= 2)
    p1.collectCard(cd.deal());
    p2.collectCard(cd.deal());
    p1.useWonPile();
    p2.useWonPile();
    Pile down = new Pile(); // Pile for cards in a war
    loop: for (int t=1; t<=100; t++)
    if (!enoughCards(1)) break loop;
    Card c1 = p1.playCard();
    Card c2 = p2.playCard();
    System.out.println("\nTurn " + t + ": ");
    System.out.print(p1.getName() + ": " + c1 + " ");
    System.out.print(p2.getName() + ": " + c2 + " ");
    if (c1.compareTo(c2) > 0)
    p1.collectCard(c1); p1.collectCard(c2);
    else if (c1.compareTo(c2) < 0)
    p2.collectCard(c1); p2.collectCard(c2);
    else // War
    down.clear();
    down.addCard(c1); down.addCard(c2);
    boolean done = false;
    do
    { int num = c1.getRank();
    if (!enoughCards(num)) break loop;
    System.out.print("\nWar! Players put down ");
    System.out.println(num + " card(s).");
    for (int m=1; m<=num; m++)
    c1 = p1.playCard(); c2 = p2.playCard();
    down.addCard(c1);
    down.addCard(c2);
    System.out.print(p1.getName()+": "+ c1 + " ");
    System.out.print(p2.getName()+": " + c2 + " ");
    if (c1.compareTo(c2) > 0)
    { p1.collectCards(down);
    done = true;
    else if (c1.compareTo(c2) < 0)
    { p2.collectCards(down);
    done = true;
    while (!done);
    } // end of for t=1 to 100
    System.out.println(p1.numCards() + " to "
    + p2.numCards());
    boolean enoughCards(int n)
    if (p1.numCards() < n || p2.numCards() < n)
    return false;
    return true;
    Player getWinner()
    if (p1.numCards() > p2.numCards())
    return p1;
    else if (p2.numCards() > p1.numCards())
    return p2;
    else
    return null;
    private Player p1, p2;
    public class War
    public static void main(String [] args)
    Game g = new Game();
    g.play();
    Player winner = g.getWinner();
    if (winner == null) System.out.println("Tie game.");
    else System.out.println("\nWinner = "
    + winner.getName());
    Message was edited by:
    thunderbbolt

    return value==other.value && suit==other.suit;I don't see a variable named value defined anywhere either. Do you? I do see rank and suit variables though.

  • Star wars game wont download

    I can't buy the game star wars the force unleashed ultimate sith edition because my macbook says there isn't enough space on my macbook. The game is 11.18 GB and my macbook have 13 GB space free!
    Help!!!

    You need at least 9 GB of available space for normal operation, so the download would put you under that. You'll have to delete some files or move them to another volume.

  • HT5622 Stock Wars game download, then request for AppleID Password to continue?

    After downloading an ipad game app to play on the stock market (with virtual money) the app asked me to register using my Apple ID Password before letting me use it.
    How can users know if this app is safe to use, or if the developer will have access to users' Apple ID? From a security point of view, an app could ask for a new username and password, but it should not try to trick people into giving them the Apple Password...
    The game is called Stock Wars.
    I looked again at this and notice that it's maybe Apple asking for this information. When the game is opened, a window opens called Game Center which has my Apple Username and requests my password. Maybe this is an Apple request and not the game? How can one be sure?

    It is most likely game center. Look on the app download page. Do you see a game center icon? Game Center is apple's social gaming app. It's how you will compete against others, track your progress, etc.
    If you go into game center on your iPad (there is an app on there, it's a white icon with what looks like 3 colored bubbles on it) you can sign in that way. If you can still do it, you can sign out and still make a game center ID that's now your apple ID.
    It's not allowing me to post a link. but  go to the top of this page, click on support and then iPad and you'll see the option to read the manual
    Chapter 20 of the manual is all about Game Center.

  • HT1491 i cant buy any in-app purchase for global war game... help please?

    as stated.. i cant buy anything from (Global war ) inapp purchase keeps failing...

    Do you have Restrictions on? Tap Settings > General > Restrictions and look for In-App Purchases.
    Try downloading something else from the App Store, to make sure there isn't a connection issue.
    If it isn't restrictions or a connection issue, contact the iTunes Store people using Express Lane.

  • Is there any war games like IGI, Call Of Duty, max Payne???? let me know if its availabe in CD ?? thanks!!, is there any war games like IGI, Call Of Duty, max Payne???? let me know if its availabe in CD ?? thanks!!

    plz help me in this,who know about this..

    plz help me in this,who know about this..

  • Everything works in mafia wars game except fighting will not load right or let me fight

    when i bring up fight list and hit attack it goes to a blank black page. will not let me attack or move around in fight list. All other places in mw are working ....

    when i bring up fight list and hit attack it goes to a blank black page. will not let me attack or move around in fight list. All other places in mw are working ....

  • How do I save changes made to games? Example...Mafia Wars on FaceBook...Thank's

    Trying to change stats on Mafia Wars game for FaceBook. It allows the change and then it goes away when I try to play. How do I make them permanent? Thank's
    == This happened ==
    A few times a week
    == today

    I afraid you cannot save these changes with the F12 developer tool. You could copy these changes to an external editor.
    Note  The Changes tab will clear if you close the F12 developer tools, but the changes you made with
    DOM Explorer persist until you refresh the webpage.
    More information about F12 developer tools, please refer to here:
    Using the F12 developer tools
    DOM explore
    https://msdn.microsoft.com/en-us/library/ie/dn255008(v=vs.85).aspx#editing_an_element
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • IM on global war the game and all it does is loading how long does it tak to load something. Cuz I can't press buy any honor point for the game. Why  can't I buy it off a game. I have the credit .I checked.

    IN my global war game I cant buy anything for my army and I know I have 15 dollars credit. I have check. How do I buy when all it does is load. help

    Have you tried the suggestions here:
    iOS: Troubleshooting applications purchased from the App Store

  • HT203167 Someone has been using my credit card to download games. this has happened before when I  had a different credit card on my itunes account. the apple store on that previous time would not give me the ip address where the downloads ended up.

    Someone I entrusted my computer to for repairs decided to keep it. all my personal information was on the computer. the peron has been using my credit card information o make ourchases from the apple store. If I do not have a credit card on the account on my iphone, I am unable to updatw it or my ipad. After persuading the apple store people to delete the last credit card info, I had to put aother credit card on the iphone to be able to update the software and thevapps. now that card is being used by the person o download war games on the new card. I cannot find any of his downloaded games on any of my devices. I am close to giving up my iphone and ipad. the last time i called the apple store, theynwould not give me the IP address of the omputer the games were downloaded to. they only gave me the name of my ipad. anyone could name thir ipad according to the name on mynipad. I have found the Apple store less than friendly about security matters like this. What can I do to maintain my acount and not continue to be ripped off by the thieving interloper? if i change my user name nd password, i will forever lose legitimate purchases. Please advise me.
    Killerappleuser

    We are fellow users here on these user-to-user forums, you're not talking to iTunes Support nor Apple.
    Is the credit card registered to exactly the same name and address (including format and spacing etc) that you have on your iTunes account, it was issued by a bank in your country and you are currently in that country ? If it is then you could check with the card issuer to see if it's them that are declining it, and if not then try contacting iTunes support and see if they know why it's being declined : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, then Account Management
    Each time that you add or change your card details then a temporary store holding charge may be applied to check that the card details are correct and valid (though that is usually $1 or the approximate local equivalent) : http://support.apple.com/kb/HT3702
    Did your daughter owe any amounts to iTunes ? Does anything show on your purchase history : http://support.apple.com/kb/HT2727 ? If they are proper charges which you don't think that you should have then you can contact iTunes Support via this page : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, then Purchases, Billing & Redemption

  • Call of duty world at war

    soo i got a macbookpro 4 with the 8600mt 512 with 2 gigs of ram i was just wondering if any 1 has played the new call of duty world at war game and gets any good framerate i know i don't i get like 30 fps any tips or ideas?
    Message was edited by: freak45

    I havent played World at War, but I do play alot of Call of Duty 4 online and in game, I havent experienced any issues and would say the graphics on my Nvidia 9600 256mb are sweet.

Maybe you are looking for

  • How do I change my apple id on my old macbook?

    I long ago changed my apple i.d., but my old MacBook Pro is still requesting the password for the old one.  The new one is integrated into everything else I use.

  • Connect touch to 2 macs?

    can i do this? am going to get a touch soon, and.. would like to sync to my ical & address on my powerbook, but itunes on my desktop mac. will this work? (or can i only work with one mac) thanx, am new to this..!

  • HT1752 F*** THAT MY G4 IS OBSOLETE, CAN'T APPLE MOVE MY OLD OPERATING SYETEM TO NEW IMAC??

    MY OLD, STILL WORKING MODEL G4 POWER MAC, HAS APPS & DOC'S THAT ARE NOT SUPPORTED ON MY NEW IMAC. CAN'T APPLE PARTITION MY HARD DRIVE (ON IMAC) AND INSTALL THE OLD OPERATIONG SYSTEM. SO THAT I CAN AT LEAST READ MY OLD DOC'S.

  • Print list migration

    Hi Experts, Please let me know how how SCMS_AO_copy function module is useful to migrate the printList document from one storage to other. Especially for http connection. i got struck at  HTTP_GET_FILES while doing migrationfrom one content repositor

  • How to update mountain lion

    i dont know how i update mountain lion. i have osx 10.7.5. i dont know how i get osx 10.8.3?