#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0

    Fixed


    I found the issue






    here my main code:

    Code:
    import java.util.*;
    
    public class WarGame {
        private static int point,point1,reply,i,player;
        public static void main(String[] args)
        {
            mainMenu();
        }    
        
        public static void mainMenu()
        {
         Scanner kb = new Scanner(System.in);
            System.out.println("Welcome to War.");
            System.out.println("Would you like to play War? 1 = Yes or 2 = No");
            System.out.println("Or if you would like to veiw the help menu, press 3");
            System.out.println("");
            int reply;
            reply = kb.nextInt();
            if (reply==2)
              System.exit(0);
            if (reply==1)
              Game();
            if (reply==3)
              Help();
        }
        
        
        public static int getReply()
        {
            return reply;
        }
        
        public static int getPoint()
        {
            return point;
        }
       
        public static int getPoint1()
        {
            return point1;
        }
        
        public static void Help()
        {
            System.out.println("");
            System.out.println("Cards rank from hi to low: A K Q J T 9 8 7 6 5 4 3 2. Suits are ignored in this game.");
            System.out.println("Each player has 26. The object of the game is to win all the cards.");
            System.out.println("Whoever turns the higher card wins that match.");
            System.out.println("To play a new game press 1.");
            System.out.println("To play next hand press 4.");
            System.out.println("To stop playing in the middle of the game press 0");
            System.out.println("");
            mainMenu();
        }
            
        public static void Game()
        {
           
               
          Scanner kb = new Scanner(System.in);
          Card[][] hands = new Card[2][1];
          Deck myDeck = new Deck();
                
           for(int i = 0; i < 26;) 
           {
                  System.out.printf("\n Round %s of The War \n", i);
                  System.out.println("Enter your option: 4 or 0");
                  reply = kb.nextInt();
                  if(reply==4)
                  {
                    for (int player = 0; player < hands.length; player++)
                        hands[player][0] = myDeck.dealCard();
    
                    for (int player = 0; player < hands.length; player++) 
                    {
                       System.out.printf("Player %d: ", player);
                       printHand(hands[player]);
                    }
                       
                    int player1 = hands[0][0].getValue(); 
                    int player2 = hands[1][0].getValue();
                       
                    if (player1 > player2)
                    {
                      System.out.println("Player One Wins The War");
                      point++;
                    }
                    if (player2 > player1)
                    {
                      System.out.println("Player Two Wins The War");
                      point1++;
                    }
                    if (player1 == player2)
                     System.out.println("The War Is A Tie");
                     
                    i++; 
                  }
                  else if (reply==0)
                  {
                   Score();
                   mainMenu();
                  }
            }
            
           System.out.println("");
           System.out.println("Would you like to play again? 1 = Yes or 2 = No");
           reply = kb.nextInt();
           
           if (reply==2)
           {
            fScore();   
           }
           
           if (reply==1)
             Game();
                    
        }
        
        public static void Score()
        {
          System.out.println("");
          System.out.println("Player 1 wins: " + getPoint() + "\r");
          System.out.println("Player 2 wins: " + getPoint1() + "\r");
          System.out.println("");
        }
        
        public static void fScore()
        {
             
          System.out.println("Player 1 wins: " + getPoint() + "\r");
          System.out.println("Player 2 wins: " + getPoint1() + "\r");
          if(getPoint()<getPoint1())
                System.out.println("Player one wins!");
            else if(getPoint()>getPoint1())
                System.out.println("Player two wins!");
            else 
                System.out.println("The game was a Draw.");
            System.exit(0); 
          
        }
        
        public static void printHand(Card[] hand) 
        {
    
            for (int card = 0; card < hand.length; card++)
                System.out.printf("%s", hand[card].toString());
    
            System.out.println();
            
        } 
        
    }
    Heres my card class code:

    Code:
    public class Card extends WarGame {
    
        private int cardNum;
        final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
        final static String[] ranks = {"2", "3","4","5","6","7","8", "9","10", "Jack", "Queen", "King", "Ace"};
    
        Card (int theCard) 
        { 
            setCardNum (theCard);
        }
    
        public void setCardNum (int theCard) {
            cardNum = (theCard >= 0 && theCard <= 51)? theCard: 0;
        }
    
        public int getCardNum() {
            return cardNum;
        }
    
        public String toString() {
            return ranks[cardNum%13] + " of " + suits[cardNum/13];
        }
    
        public String getSuit() {
            return suits[cardNum/13];
        }
    
        public String getRank() {
            return ranks[cardNum%13];
        }
    
        public int getValue() {
            return cardNum%13;
        } 
    }
    And here is my deck code:

    Code:
    public class Deck {
    
        private Card[] deck = new Card[52];
        private int topCard;
    
        Deck() 
        {
    
            topCard = 0;
    
            for (int i = 0; i < deck.length; i++)
                deck[i] = new Card(i);
            Shuffle();
        }
    
        public void Shuffle() 
        {
    
            topCard = 0;
    
            for (int i = 0; i < 1000; i++) 
            {
                int j = (int)(Math.random()*52);
                int k = (int)(Math.random()*52);
                Card tmpCard = deck[j];
                deck[j] = deck[k];
                deck[k] = tmpCard;
            } 
        }
    
        public Card dealCard() 
        {
            Card theCard;
            if (topCard < deck.length) 
            {
                theCard = deck[topCard];
                topCard++;
            }
            else
                theCard = null;
    
            return theCard;
        }
    }
    Here's my output:

    PHP Code:
    WarGame.main({ });
    Welcome to War.
    Would you like to play WarYes or No
    Or if you would like to veiw the help menupress 3

    3

    Cards rank from hi to low
    A K Q J T 9 8 7 6 5 4 3 2. Suits are ignored in this game.
    Each player has 26. The object of the game is to win all the cards.
    Whoever turns the higher card wins that match.
    To play a new game press 1.
    To play next hand press 4.
    To stop playing in the middle of the game press 0

    Welcome to War
    .
    Would you like to play WarYes or No
    Or if you would like to veiw the help menupress 3

    1

     Round 0 of The War 
    Enter your option
    or 0
    4
    Player 0
    Jack of Clubs
    Player 1
    9 of Hearts
    Player One Wins The War

     Round 1 of The War 
    Enter your option
    or 0
    4
    Player 0
    Ace of Spades
    Player 1
    Queen of Diamonds
    Player One Wins The War

     Round 2 of The War 
    Enter your option
    or 0
    4
    Player 0
    King of Hearts
    Player 1
    2 of Hearts
    Player One Wins The War

     Round 3 of The War 
    Enter your option
    or 0
    0

    Player 1 wins
    3
    Player 2 wins
    0

    Welcome to War
    .
    Would you like to play WarYes or No
    Or if you would like to veiw the help menupress 3

    1

     Round 0 of The War 
    Enter your option
    or 0
    4
    Player 0
    10 of Hearts
    Player 1
    Jack of Hearts
    Player Two Wins The War

     Round 1 of The War 
    Enter your option
    or 0
    4
    Player 0
    4 of Spades
    Player 1
    5 of Spades
    Player Two Wins The War

     Round 2 of The War 
    Enter your option
    or 0
    4
    Player 0
    6 of Diamonds
    Player 1
    8 of Hearts
    Player Two Wins The War

     Round 3 of The War 
    Enter your option
    or 0
    4
    Player 0
    King of Spades
    Player 1
    Ace of Diamonds
    Player Two Wins The War

     Round 4 of The War 
    Enter your option
    or 0
    4
    Player 0
    6 of Spades
    Player 1
    10 of Clubs
    Player Two Wins The War

     Round 5 of The War 
    Enter your option
    or 0
    4
    Player 0
    5 of Hearts
    Player 1
    Jack of Diamonds
    Player Two Wins The War

     Round 6 of The War 
    Enter your option
    or 0
    4
    Player 0
    Queen of Hearts
    Player 1
    5 of Clubs
    Player One Wins The War

     Round 7 of The War 
    Enter your option
    or 0
    4
    Player 0
    Queen of Spades
    Player 1
    10 of Spades
    Player One Wins The War

     Round 8 of The War 
    Enter your option
    or 0
    4
    Player 0
    King of Hearts
    Player 1
    2 of Spades
    Player One Wins The War

     Round 9 of The War 
    Enter your option
    or 0
    4
    Player 0
    4 of Diamonds
    Player 1
    King of Clubs
    Player Two Wins The War

     Round 10 of The War 
    Enter your option
    or 0
    4
    Player 0
    Ace of Spades
    Player 1
    5 of Diamonds
    Player One Wins The War

     Round 11 of The War 
    Enter your option
    or 0
    4
    Player 0
    Jack of Spades
    Player 1
    6 of Clubs
    Player One Wins The War

     Round 12 of The War 
    Enter your option
    or 0
    4
    Player 0
    8 of Clubs
    Player 1
    8 of Diamonds
    The War Is A Tie

     Round 13 of The War 
    Enter your option
    or 0
    4
    Player 0
    7 of Hearts
    Player 1
    2 of Hearts
    Player One Wins The War

     Round 14 of The War 
    Enter your option
    or 0
    4
    Player 0
    9 of Hearts
    Player 1
    Ace of Hearts
    Player Two Wins The War

     Round 15 of The War 
    Enter your option
    or 0
    4
    Player 0
    6 of Hearts
    Player 1
    10 of Diamonds
    Player Two Wins The War

     Round 16 of The War 
    Enter your option
    or 0
    4
    Player 0
    3 of Clubs
    Player 1
    3 of Spades
    The War Is A Tie

     Round 17 of The War 
    Enter your option
    or 0
    4
    Player 0
    8 of Spades
    Player 1
    7 of Diamonds
    Player One Wins The War

     Round 18 of The War 
    Enter your option
    or 0
    4
    Player 0
    9 of Clubs
    Player 1
    Ace of Clubs
    Player Two Wins The War

     Round 19 of The War 
    Enter your option
    or 0
    4
    Player 0
    Queen of Clubs
    Player 1
    4 of Hearts
    Player One Wins The War

     Round 20 of The War 
    Enter your option
    or 0
    4
    Player 0
    3 of Hearts
    Player 1
    2 of Clubs
    Player One Wins The War

     Round 21 of The War 
    Enter your option
    or 0
    4
    Player 0
    2 of Diamonds
    Player 1
    3 of Diamonds
    Player Two Wins The War

     Round 22 of The War 
    Enter your option
    or 0
    4
    Player 0
    9 of Spades
    Player 1
    9 of Diamonds
    The War Is A Tie

     Round 23 of The War 
    Enter your option
    or 0
    4
    Player 0
    7 of Spades
    Player 1
    7 of Clubs
    The War Is A Tie

     Round 24 of The War 
    Enter your option
    or 0
    4
    Player 0
    Jack of Clubs
    Player 1
    4 of Clubs
    Player One Wins The War

     Round 25 of The War 
    Enter your option
    or 0
    4
    Player 0
    King of Diamonds
    Player 1
    Queen of Diamonds
    Player One Wins The War

    Would you like to play again
    Yes or No
    2
    Player 1 wins
    14
        VM terminated

    If someone could point out to me what my problem is I'd really appreciate it
    Last edited by blyons626; May 9th, 2013 at 01:47 PM. Reason: Fixed
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Where does the code call the shuffle() method?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Wow must've totally missed leaving that out, fixed.

IMN logo majestic logo threadwatch logo seochat tools logo