Results 1 to 6 of 6

Thread: Need help

  1. #1
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default Need help

    I have to make the game "War" for my computer science class. We were instructed to use arrays to accomplish this. Once a card from one deck is used, how would I add it to the other persons deck/remove it from mine?

    The name of the deck array is playerDeck.
    To output a card I have been doing:
    System.out.println ("Your card is the " + playerDeck.deck[c]);
    c gets incremented by one each time through the while loop.
    Let's say that I lose, that the opponents card is higher, how would I remove card "c" from the playerDeck array and add it to the opponents deck array, called compDeck?

    I know I am new here, but I appreciate any help in this matter.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Welcome to the forum.

    Myself, I'd use ArrayLists here not arrays since it is easy to add and remove items from these flexible collections.

    Oh, it's considered bad form to cross-post a question in multiple forums. It's frustrating to many of to put in the effort answering a question only to find that we wasted our time, that the question was already answered elsewhere. Your best bet is to choose the one best forum for your question and ask it there.

  3. #3
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    I got it to work but before I explain anything, hmm what would happen you your card if you win? You said the opponent's card goes under your deck but then how about your own card?

    Edit: I forgot to ask, what happens if there is a tie?
    Last edited by Bomber_Will; 01-18-2009 at 01:44 AM.

  4. #4
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Fubarable - thanks for the advice
    Bomber_Will - once I figure out how to do it once I will be able to apply it to the other possibilities. So, is there an easy way to do it?

    Thanks for you help.

  5. #5
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Well I put the player deck in a text file and the opponent deck in another text file and then made the program make an array out of it. Then it's just a matter of saying

    Java Code:
    int c = 0; //Start of deck
    int i = 52; //End of deck
    
    while(playerDeck[c] != 0 && compDeck[c] != 0) //Checks to see if both decks have cards left
    {
        if(playerDeck[c] > compDeck[c])
        {
           *Place compDeck[c] in a temp variable
           *Place playerDeck[c] to playerDeck[i] where c is the current place in 
    the deck and i is the last free space in the array
           *Increase i by 1
           *Place the temp variable into the new playerDeck[i]
           *Print out the result
           *Increase i by 1
           *Increase c by 1
        }
    Each * can be replaced by a single line of code, there is probably an easier way but this way should work.
    Last edited by Bomber_Will; 01-18-2009 at 07:01 PM.

  6. #6
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    This is what I have so far. I have it in a loop until one person wins, and once I get everything worked out I will put the user prompts back in. There is also a card class and a deck class. playerDeckTotal and opponentDeckTotal are only in there because I could not figure out how to say how many cards were left in a given array.

    import java.util.Scanner;
    /**
    *
    * @author (your name)
    * @version (a version number or a date)
    */
    public class war
    {
    public static void main (String [] args)
    {
    Scanner input = new Scanner( System.in );
    String response;
    String ruleResponse;
    String quit;
    boolean continueTurn;
    int c;
    int playerDeckTotal = 52;
    int opponentDeckTotal = 52;

    Deck playerDeck = new Deck();
    playerDeck.shuffle();

    Deck compDeck = new Deck();
    compDeck.shuffle();

    System.out.println ("Do you know how to play?");
    System.out.print ("");
    ruleResponse = input.nextLine ();
    if (ruleResponse.charAt(0) == 'n' || ruleResponse.charAt(0) == 'N')
    {
    System.out.println ("How do you not know how to play war?");
    System.out.println ("Sigh, well I will tell you anyways, insignificant human.");
    System.out.println ("Each player starts out with a standard 52 card deck.");
    System.out.println ("Each player reveals the top card of their deck.");
    System.out.println ("The player with the highest card wins.");
    System.out.println ("Two being the lowest, and King being the highest.");
    System.out.println ("However, an Ace beats everything.");
    System.out.println ("In the event that both players play the same card, a war is started.");
    System.out.println ("In a war, both players lay 3 cards facedown, and show the forth card.");
    System.out.println ("Whomever has the highest forth card wins the war and all cards in said war.");
    System.out.println ("If a player has all of the cards, he or she wins.");
    System.out.println ("Let us begin.");
    System.out.println ("To quit, type in 'Quit' at any time");
    continueTurn = true;
    }
    else
    {
    System.out.println ("Good, I have no reason to bore myself with rules.");
    System.out.println ("Let us begin then.");
    System.out.println ("To quit, type in 'Quit'");
    continueTurn = true;
    }
    c = 1;
    int turnNum = 1;
    while (playerDeckTotal < 104 && opponentDeckTotal < 104)
    {

    if (c >= 47)
    {
    playerDeck.shuffle ();
    c =1;
    }

    System.out.println ("Turn Number " +turnNum);
    turnNum ++;
    System.out.println ("Your card is the " + playerDeck.deck[c]);
    System.out.println ("My card is the " + compDeck.deck[c]);

    if (playerDeck.deck[c].number > compDeck.deck[c].number)
    {
    System.out.println ("You win this time, comrade.");
    playerDeckTotal = playerDeckTotal + 1;
    opponentDeckTotal = opponentDeckTotal - 1;
    System.out.println ("There are " + playerDeckTotal + " cards in your deck");
    System.out.println ("I have " + opponentDeckTotal + " cards in my deck");
    c++;
    }

    else
    if (playerDeck.deck[c].number < compDeck.deck[c].number)
    {
    System.out.println ("HA! I win this one, insignificant worm.");
    playerDeckTotal = playerDeckTotal - 1;
    opponentDeckTotal = opponentDeckTotal + 1;
    System.out.println ("There are " + playerDeckTotal + " cards in your deck");
    System.out.println ("I have " + opponentDeckTotal + " cards in my deck");
    c++;
    }

    else if (playerDeck.deck[c].number == compDeck.deck[c].number)
    {
    System.out.println ("HA! Now we war!!");
    int x = 1;
    int y = 1;
    while (x < 4)
    {
    System.out.println ("The three cards you lay down are " + playerDeck.deck[c + x]);
    x++;
    }
    while (y < 4)
    {
    System.out.println ("The three cards I lay down are " + compDeck.deck[c + y]);
    y++;
    }
    c = c + 4;
    System.out.println ("Your card is " + playerDeck.deck[c]);
    System.out.println ("My card is " + compDeck.deck[c]);

    if (playerDeck.deck[c].number > compDeck.deck[c].number)
    {
    System.out.println ("You win this time, comrade.");
    playerDeckTotal = playerDeckTotal + 4;
    opponentDeckTotal = opponentDeckTotal - 4;
    System.out.println ("There are " + playerDeckTotal + " cards in your deck");
    System.out.println ("I have " + opponentDeckTotal + " cards in my deck");
    c++;
    }

    else if (playerDeck.deck[c].number < compDeck.deck[c].number)
    {
    System.out.println ("HA! I win this one, insignificant worm.");
    playerDeckTotal = playerDeckTotal - 4;
    opponentDeckTotal = opponentDeckTotal + 4;
    System.out.println ("There are " + playerDeckTotal + " cards in your deck");
    System.out.println ("I have " + opponentDeckTotal + " cards in my deck");
    c++;
    }
    }
    }
    if (playerDeckTotal == 104)
    System.out.println ("I.. I've been beaten. You win. Enjoy life.");
    else
    System.out.println ("I WIN!! I WIN!! YOU STINK!! BAHAHAHA!! GOOD DAY, SIR!!");
    }
    }

    And I am assuming that there is a much easier way of doing this, so feel free to enlighten me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •