10 Replies - 387 Views - Last Post: 03 November 2019 - 06:00 PM Rate Topic: -----

#1 jimhalpert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 19

War Game Help

Posted 03 November 2019 - 03:19 PM

For my War Game I am working on my PlayedPile class. However, I'm having trouble trying to pass my data from DrawPile to PlayedPile. Here is the code I have written so far:

The Rules of the Game are the following:
1. Shuffle a deck of cards (52), excluding the Joker.
2. Split the deck in half, so that each player has 26 cards.
3. Flip the top of each card, if one of the players has a higher card they win the round and add the two cards played at the bottom of their deck. If the two cards are identical, then war is played.
4. War is played by placing 5 cards and flipping the fifth card. If one of the players has a higher card number, they automatically win the round and place the 10 cards at the bottom of their deck.
5. During war, if the players both have the same card again, they will place 5 cards and flip the fifth one, comparing the two. (The winner of that round will get 20 cards).
6. If one of the players do not have enough cards in war the other player automatically wins the game.
7. If the number of rounds continues over 600 the game ends with a tie.

import java.util.ArrayList;

/**
* The DrawPile receives half of the deck and removes the top card each round unless war is played.
*/
public class DrawPile{
    private ArrayList<Card> pile1;
    private ArrayList<Card> pile2;
    
    /**
    * The DrawPile initializes pile1 and pile2.
    * pre: none
    * post: DrawPile is declared.
    */
    public DrawPile(ArrayList<Card> halfPile1, ArrayList<Card> halfPile2){
        pile1 = new ArrayList<Card>(halfPile1);
        pile2 = new ArrayList<Card>(halfPile2);

        PlayedPile play = new PlayedPile(pile1.get(0), pile2.get(0));  // Would this be how you would send the top card 
                                                                          of the deck to PlayedPile? (I'm kind of stuck on 
                                                                          that part) 
    }

    /**
    * Removes the card that was Drawn. (top card)
    * pre: none
    * post: An element is removed.
    */
    public void remove(){
        pile1.remove(0);
        pile2.remove(0);
    }

    /**
    * Played Pile is added to the bottom of the deck after each round.     // This whole method I am confused about since 
    * pre: none                                                                 it is linked to the PlayedPile
    * post: Card(s) is added to each pile.
    */
    public void add(Object c, Object c2){
        pile1.add(c);
        pile2.add(c2);
    }
}


PlayedPile Starts

/*
* The PlayedPile class 
*/
public class PlayedPile{

    public PlayedPile(Object c1, Object c2){ // I am very confused on the PlayedPile part. I do not understand how you  
                                               would pass the top card to PlayedPile and then send back the cards to add 
                                               them onto the winner's pile. (player 1 or player 2)
        
    }

    public void addToDeck(){                 // This method should be used to add the values back into the DrawPile class.
        
    }
    
}
:code:

This post has been edited by modi123_1: 03 November 2019 - 03:23 PM
Reason for edit:: In the future please use the [code] tag button in the editor


Is This A Good Question/Topic? 0
  • +

Replies To: War Game Help

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: War Game Help

Posted 03 November 2019 - 04:01 PM

Quote

having trouble trying to pass my data from DrawPile to PlayedPile.

Please explain. If there are error messages copy the full text and paste it here.
Was This Post Helpful? 0
  • +
  • -

#3 jimhalpert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 19

Re: War Game Help

Posted 03 November 2019 - 04:11 PM

View PostNormR, on 03 November 2019 - 04:01 PM, said:

Quote

having trouble trying to pass my data from DrawPile to PlayedPile.

Please explain. If there are error messages copy the full text and paste it here.

Quote

There aren't really any errors because I still need to write my main code, however, just getting the basics of the game is the part I am having trouble with. Here is the rest of my code:


/**
* The Card class creates a card object to represent one card from the deck. 
*/
public class Card{
    private char suit;
    private int cardNum; //2 to 14, instead of King, Queen, and Jack

    /**
    * Constructor
    * pre: none
    * post: The Card object is created with an int and char parameter.
    */
    public Card(int cardNum, char suit){
        this.suit = suit;
        this.cardNum = cardNum;
    }

    /**
    * Returns the value of the card number.
    * pre: none
    * post: Return's cardNum
    */

    public int getValue(){
        return(cardNum);
    }

    /**
    * Returns the card (number and suit)
    * pre: none
    * post: Returns a String
    */
    public String toString() {

        String cardString;

        cardString = cardNum + " of ";

        if(cardNum > 10){
            switch(cardNum){
                case(11): cardString = "Jack of ";
                break;
                case(12): cardString = "Queen of ";
                break;
                case(13): cardString = "King of ";
                break;
                default: cardString = "Ace of ";
            }
        }

         if(suit == 'H'){  
             cardString += "Hearts";
        }else if(suit == 'D'){
             cardString += "Diamonds";
        }else if(suit == 'S'){
             cardString += "Spades"; 
        }else if(suit == 'C'){
             cardString += "Clovers";
        }

        return cardString;

    }

    /**
    * Determines if object c is smaller, the same, or larger than this Card object.
    * pre: none
    * post: -1 has been returned if c is larger than this Card, 0 has been returned if they are the same size, and 1 has been returned if c is smaller then this Card.
    */
    public int compareTo(Object c){
        Card testCard = (Card)c;

        if(cardNum < testCard.getValue()){
            return(-1);
        }else if(cardNum == testCard.getValue()){
            return(0);
        }else{
            return(1);
        }

    }
    
}


import java.util.ArrayList;
import java.lang.Math;
import java.util.Random;

/**
* The Deck card initializes and shuffles the deck.
*/
public class Deck{
    private ArrayList<Card> deck;

    /**
    * Constructor
    * pre: none
    * post: The Deck object is created.
    */
    public Deck(){
        char s = 'a'; 
        deck = new ArrayList<Card>();

        for(int m = 0; m < 4; m++){
            for (int i = 2; i < 15; i++) {
              if(m == 0){
                  s = 'H';
              }else if(m == 1){
                  s = 'D';
              }else if(m == 2){
                  s = 'S';
              }else if(m == 3){
                  s = 'C';
              }
    
             deck.add(new Card(i, s)); 
           }
       }
    }

    /**
    * Shuffles the deck of cards
    * pre: none
    * post: The deck is shuffled. 
    */    
    public void Shuffle(){
        Card temp;

        for(int z = 0;  z < 3000; z++){
             int x = (int)(Math.random()*52);
             int y = (int)(Math.random()*52);

             temp = deck.get(x);

             deck.set(x, deck.get(y));

             deck.set(y, temp);   
        }
   }

   /**
   * Creates two ArrayList to store half of the deck.
   * pre: none
   * post: The deck is split in half and then stored in two ArrayLists.
   */
    public void getHalfPile(){ 

      ArrayList<Card> halfPile1 = new ArrayList<Card>();
      ArrayList<Card> halfPile2 = new ArrayList<Card>();

      for(int num = 0; num < 27; num++){
          halfPile1.add(deck.get(num));
      }

      for(int num1 = 27; num1 <= 52; num1++){
          halfPile2.add(deck.get(num1));
      }

      DrawPile draw = new DrawPile(halfPile1, halfPile2);

  }

}


import java.util.ArrayList;

/**
* The DrawPile recieves half of the deck and removes the top card each round, unless war is played.
*/
public class DrawPile{
    private ArrayList<Card> pile1;
    private ArrayList<Card> pile2;
    
    /**
    * The DrawPile initializes pile1 and pile2.
    * pre: none
    * post: DrawPile is declared.
    */
    public DrawPile(ArrayList<Card> halfPile1, ArrayList<Card> halfPile2){
        pile1 = new ArrayList<Card>(halfPile1);
        pile2 = new ArrayList<Card>(halfPile2);

        PlayedPile play = new PlayedPile(pile1.get(0), pile2.get(0));
    }

    /**
    * Removes the card that was Drawn. (top card)
    * pre: none
    * post: An element is removed.
    */
    public void remove(){
        pile1.remove(0);
        pile2.remove(0);
    }

    /**
    * Played Pile is added to the bottom of the deck after each round.
    * pre: none
    * post: An ArrayList is added to each pile.
    */
    public void add(Object c, Object c2){
        pile1.add(c);
        pile2.add(c2);
    }
}


/*
* The PlayedPile class 
*/
public class PlayedPile{

    public PlayedPile(Object c1, Object c2){
        
        
    }

    public void addToDeck(){
        
    }
    
}



Quote

I don't understand how I would pass one card from each of the two half piles into PlayedPile and then add them back into DrawPile so that the cards are added back onto the bottom of the card decks.

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: War Game Help

Posted 03 November 2019 - 04:14 PM

I would assume a card would be passed by removing it from one pile and adding it to the other pile.
Do the "pile"s have methods for removing cards from the top and adding them to the bottom?
Was This Post Helpful? 0
  • +
  • -

#5 jimhalpert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 19

Re: War Game Help

Posted 03 November 2019 - 04:20 PM

View PostNormR, on 03 November 2019 - 04:14 PM, said:

I would assume a card would be passed by removing it from one pile and adding it to the other pile.
Do the "pile"s have methods for removing cards from the top and adding them to the bottom?

Quote

Well, in the main I would have to declare who the winner is for each round (by comparing which top card has the greatest value). Then I would add both of the top cards to the bottom of the winner's deck. However, I don't believe I have methods for each pile. But I do have a method for removing the top card from both decks and then I was planning to pass those two cards to PlayedPile. After the round, PlayedPile would send those cards back to the winning deck (that's the part I am stuck on).

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: War Game Help

Posted 03 November 2019 - 04:24 PM

Is this the logic you are working on:
card1 = remove card from player1
card2 = remove card from player2
determine winner
add card1 and card2 to winner

This post has been edited by NormR: 03 November 2019 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#7 jimhalpert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 19

Re: War Game Help

Posted 03 November 2019 - 04:29 PM

View PostNormR, on 03 November 2019 - 04:24 PM, said:

Is this the logic you are working on:
card1 = remove card from player1
card2 = remove card from player2
determine winner
add card1 and card2 to winner


Not really, sorry I'll try to explain better.

So there are two players. They split a deck of cards. Player1 has 26 cards, Player2 has 26 cards. They take their top card and face it up. If Player1 has a higher card they take both their own card and Player2's card and adds the two cards to the bottom of their deck (vice versa). If both Players have the same card, war is played. They have to place 5 cards faced down and flip the 5th one up. The 5th one is the one that decides the winner. The winner/player would get 10 cards added to the deck. The game ends when one of the players has 1 card left.
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: War Game Help

Posted 03 November 2019 - 04:58 PM

ok, my logic steps left out when the cards were equal.
Otherwise was it correct?

Add code in small steps, don't try to do it all at once.
Was This Post Helpful? 0
  • +
  • -

#9 jimhalpert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 19

Re: War Game Help

Posted 03 November 2019 - 05:05 PM

View PostNormR, on 03 November 2019 - 04:58 PM, said:

ok, my logic steps left out when the cards were equal.
Otherwise was it correct?

Add code in small steps, don't try to do it all at once.


Yeah it was right. Sorry I didn't see your edit before I wrote the reply.
And I'll keep that in mind. Thanks for the tip.
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: War Game Help

Posted 03 November 2019 - 05:46 PM

I don't understand the class structure.
There are cards
There is a deck of cards
There are two players who each have a pile of cards.
Some where there needs to be the game logic.

What other classes are needed?

This post has been edited by NormR: 03 November 2019 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#11 jimhalpert   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-November 19

Re: War Game Help

Posted 03 November 2019 - 06:00 PM

View PostNormR, on 03 November 2019 - 05:46 PM, said:

I don't understand the class structure.
There are cards
There is a deck of cards
There are two players who each have a pile of cards.
Some where there needs to be the game logic.

What other classes are needed?


Yes, I do have Game class that brings everything together. Then the Main just creates an object for Game and calls it. I apologize for not adding that in.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1