Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help fixing program
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
syxxone




PostPosted: Thu Jan 22, 2009 9:21 am   Post subject: Need help fixing program

Making war for my final project, but I'm having some troubles finishing it up. Anyone know whats going on here?


code:

import java.util.Random;

class WarCards2
{
    public static void main (String[] args) throws InterruptedException
    {
        Random randomGenerator = new Random ();
        int howMany = 0;
        String winner = "";
        int[] yourCards = new int [64];
        int[] yourCardsSuit = new int [64];
        int[] hisCards = new int [64];
        int[] hisCardsSuit = new int [64];
        int[] used = new int [60];
        boolean done = false;
        boolean check = true;
        boolean gameOver = false;
        int k = 0;
        int col, num;
        int player = 1;
        int[] [] theDeck = {{14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15},    //Hearts (0)
                {14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15},                //Diamonds(1)
                {14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15},                //Clubs(2)
                {14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15}};               //Spades(3)



        // The Shuffling procedure
        // First while, only ends once all the cards are dealt
        while (done == false)
        {
            // Generates 2 random numbers, to pick a card out of the deck
            // If the location in (theDeck) has already been used, it will
            // have a value of 0.  If the value of (theDeck [col] [num]) is 0
            // It will go back and generage more random numbers, then try again.
            col = randomGenerator.nextInt (4);
            num = randomGenerator.nextInt (14);

            // If locaton in theDeck = 0, it will do nothing
            // And go back to teh start
            if (theDeck [col] [num] != 0)
            {
                if (player == 1)
                {
                    yourCards [k] = theDeck [col] [num];
                    yourCardsSuit [k] = col;
                    theDeck [col] [num] = 0;
                    k++;
                    if (k >= 28)
                        player = 2;
                }
                else if (player == 2)
                {
                    hisCards [k - 28] = theDeck [col] [num];
                    hisCardsSuit [k - 28] = col;
                    theDeck [col] [num] = 0;
                    k++;
                }
            }
            // Once all the cards have been dealt, The dealing procedure is over
            // By flagging done as true.
            if (k >= 56)
            {
                done = true;
            }
        }
        // FINALLY. THE CARD SHUFFLER IS DONE.  THAT TOOK TOO MUCH TIME AND EFFORT. Anywho, on to the game.

        // War.  Each player pulls a card off of the bottom of his/her deck.  They lay it down.
        // Whoever has the highest card wins the round, and recieves both of the cards.
        // If, however, two of the same cards are laid, this begins a war.
        // The player must then lay down 3 cards, Face down.  Then, they both flip up 1 more card.
        // These final cards are compared.  The winner gets ALL of the cards on the playing table.
        // If the final cards laid in a war were equal, the players preceed to another War, untill
        // Either player wins.

        // Round (how many rounds have been played)
        int round = 1;
        String suit1 = "", suit2 = "";
        while (gameOver == false)
        {
            System.out.println ("Round " + round);

            if (yourCardsSuit [0] == 0)
                suit1 = (" of Hearts");
            else if (yourCardsSuit [0] == 1)
                suit1 = (" of Diamonds");
            else if (yourCardsSuit [0] == 2)
                suit1 = (" of Clubs");
            else if (yourCardsSuit [0] == 3)
                suit1 = (" of Spades");

            if (hisCardsSuit [0] == 0)
                suit2 = (" of Hearts");
            else if (hisCardsSuit [0] == 1)
                suit2 = (" of Diamonds");
            else if (hisCardsSuit [0] == 2)
                suit2 = (" of Clubs");
            else if (hisCardsSuit [0] == 3)
                suit2 = (" of Spades");
            // Displays the card Player 1 just drew.
            System.out.print ("Player 1: ");
            if (yourCards [0] >= 11 || yourCards [0] == 1)
            {
                if (yourCards [0] == 11)
                {
                    System.out.println ("Jack" + suit1);
                }
                if (yourCards [0] == 12)
                {
                    System.out.println ("Queen" + suit1);
                }
                if (yourCards [0] == 13)
                {
                    System.out.println ("King" + suit1);
                }
                if (yourCards [0] == 15)
                {
                    System.out.println ("Joker");
                }
                if (yourCards [0] == 14)
                {
                    System.out.println ("Ace" + suit1);
                }
            }
            else
            {
                System.out.println (yourCards [0] + suit1);
            }
            // Displays the card player 2 just drew.
            System.out.print ("Player 2: ");
            if (hisCards [0] >= 11 || hisCards [0] == 1)
            {
                if (hisCards [0] == 11)
                {
                    System.out.println ("Jack" + suit2);
                }
                if (hisCards [0] == 12)
                {
                    System.out.println ("Queen" + suit2);
                }
                if (hisCards [0] == 13)
                {
                    System.out.println ("King" + suit2);
                }
                if (hisCards [0] == 15)
                {
                    System.out.println ("Joker");
                }
                if (hisCards [0] == 14)
                {
                    System.out.println ("Ace" + suit2);
                }
            }
            else
            {
                System.out.println (hisCards [0] + suit2);
            }
            Thread.sleep (1);
            // If Player 1 wins, this happens....
            if (yourCards [0] > hisCards [0])
            {
                howMany = 0;
                System.out.println ("Winner.... Player 1");
                for (int i = 0 ; i < 56 ; i++)
                {
                    if (yourCards [i] != 0)
                    {
                        howMany += 1;
                    }
                    if (howMany == 56)
                    {
                        gameOver = true;
                        winner = "Player one!!";
                    }

                }
                yourCards [howMany + 1] = yourCards [0];
                yourCardsSuit [howMany + 1] = yourCardsSuit [0];
                for (int i = 1 ; i < 56 ; i++)
                {
                    yourCards [i - 1] = yourCards [i];
                    yourCardsSuit [i - 1] = yourCardsSuit [i];
                    hisCards [i - 1] = hisCards [i];
                    hisCardsSuit [i - 1] = hisCardsSuit [i];
                }
                howMany = 0;
            }
            // If Player 2 wins, This happens....
            else if (hisCards [0] > yourCards [0])
            {
                System.out.println ("Winner.... Player 2");
                howMany = 0;
                for (int i = 0 ; i < 56 ; i++)
                {
                    if (hisCards [i] != 0)
                    {
                        howMany += 1;
                    }
                    if (howMany == 56)
                    {
                        gameOver = true;
                        winner = "Player two!!";
                    }

                }
                hisCards [howMany + 1] = hisCards [0];
                hisCardsSuit [howMany + 1] = hisCardsSuit [0];
                for (int i = 1 ; i < 56 ; i++)
                {
                    hisCards [i - 1] = hisCards [i];
                    hisCardsSuit [i - 1] = hisCardsSuit [i];
                    yourCards [i - 1] = yourCards [i];
                    yourCardsSuit [i - 1] = yourCardsSuit [i];
                }
                howMany = 0;
            }
            // If both cards are equal, this happens.
            else if (hisCards [0] == yourCards [0])
            {
                System.out.println ("WAR!!!!!");
                Thread.sleep (500);
                System.out.println ("Each player lays 3 cards facedown, and draws");
                System.out.println ("One final card.");
                for (int i = 0 ; i < 56 ; i++)
                {
                    if (hisCards [i] != 0)
                    {
                        howMany += 1;
                    }
                }
                if (howMany < 5)
                {
                    gameOver = true;
                    System.out.println ("Due to a lack of cards, The win goes to Player 1!!");
                    System.out.println ("Congratulations!");
                    Thread.sleep (2000);
                    System.exit (0);
                }
                for (int i = 0 ; i < 56 ; i++)
                {
                    if (yourCards [i] != 0)
                    {
                        howMany += 1;
                    }
                }
                if (howMany < 5)
                {
                    gameOver = true;
                    System.out.println ("Due to a lack of cards, The win goes to Player 2!!");
                    System.out.println ("Congratulations!");
                    Thread.sleep (2000);
                    System.exit (0);
                }
                Thread.sleep (500);
                System.out.print ("Player One Draws");
                for (int i = 0 ; i < 5 ; i++)
                {
                    System.out.print (".");
                    Thread.sleep (250);
                }
                if (yourCardsSuit [3] == 0)
                    suit1 = (" of Hearts");
                else if (yourCardsSuit [3] == 1)
                    suit1 = (" of Diamonds");
                else if (yourCardsSuit [3] == 2)
                    suit1 = (" of Clubs");
                else if (yourCardsSuit [3] == 3)
                    suit1 = (" of Spades");

                if (hisCardsSuit [3] == 0)
                    suit2 = (" of Hearts");
                else if (hisCardsSuit [3] == 1)
                    suit2 = (" of Diamonds");
                else if (hisCardsSuit [3] == 2)
                    suit2 = (" of Clubs");
                else if (hisCardsSuit [3] == 3)
                    suit2 = (" of Spades");
                if (yourCards [3] >= 11 || yourCards [3] == 1)
                {
                    if (yourCards [3] == 11)
                    {
                        System.out.println ("Jack" + suit1);
                    }
                    if (yourCards [3] == 12)
                    {
                        System.out.println ("Queen" + suit1);
                    }
                    if (yourCards [3] == 13)
                    {
                        System.out.println ("King" + suit1);
                    }
                    if (yourCards [3] == 15)
                    {
                        System.out.println ("Joker");
                    }
                    if (yourCards [3] == 14)
                    {
                        System.out.println ("Ace" + suit1);
                    }
                }
                else
                {
                    System.out.println (yourCards [3] + suit1);
                }
                Thread.sleep (500);
                System.out.print ("Player Two Draws");
                for (int i = 0 ; i < 5 ; i++)
                {
                    System.out.print (".");
                    Thread.sleep (250);
                }
                if (hisCards [3] >= 11 || hisCards [3] == 1)
                {
                    if (hisCards [3] == 11)
                    {
                        System.out.println ("Jack" + suit2);
                    }
                    if (hisCards [3] == 12)
                    {
                        System.out.println ("Queen" + suit2);
                    }
                    if (hisCards [3] == 13)
                    {
                        System.out.println ("King" + suit2);
                    }
                    if (hisCards [3] == 15)
                    {
                        System.out.println ("Joker");
                    }
                    if (hisCards [3] == 14)
                    {
                        System.out.println ("Ace" + suit2);
                    }
                }
                else
                {
                    System.out.println (hisCards [3] + suit2);
                }

                if (yourCards [3] > hisCards [3])
                {
                    System.out.println ("Player 1 wins the war!");
                    howMany = 0;
                    for (int i = 0 ; i < 55 ; i++)
                    {
                        if (yourCards [i] != 0)
                        {
                            howMany += 1;
                        }

                    }
                    yourCards [howMany + 1] = yourCards [0];
                    yourCardsSuit [howMany + 1] = yourCardsSuit [0];
                    yourCards [howMany + 2] = yourCards [1];
                    yourCardsSuit [howMany + 2] = yourCardsSuit [1];
                    yourCards [howMany + 3] = yourCards [2];
                    yourCardsSuit [howMany + 3] = yourCardsSuit [2];
                    yourCards [howMany + 4] = yourCards [3];
                    yourCardsSuit [howMany + 4] = yourCardsSuit [3];
                    yourCards [howMany + 5] = hisCards [0];
                    yourCardsSuit [howMany + 5] = hisCardsSuit [0];
                    yourCards [howMany + 6] = hisCards [1];
                    yourCardsSuit [howMany + 6] = hisCardsSuit [1];
                    yourCards [howMany + 7] = hisCards [2];
                    yourCardsSuit [howMany + 7] = hisCardsSuit [2];
                    yourCards [howMany + 8] = hisCards [3];
                    yourCardsSuit [howMany + 8] = hisCardsSuit [3];
                    for (int i = 0 ; i < 55 ; i++)
                    {
                        yourCards [i] = yourCards [i + 4];
                        yourCardsSuit [i] = yourCardsSuit [i + 4];
                        hisCards [i] = hisCards [i + 4];
                        hisCardsSuit [i] = hisCardsSuit [i + 4];
                    }

                }
                else if (hisCards [3] > yourCards [3])
                {
                    System.out.println ("Player 2 wins the war!");
                    howMany = 0;
                    for (int i = 0 ; i < 55 ; i++)
                    {
                        if (yourCards [i] != 0)
                        {
                            howMany += 1;
                        }

                    }
                    hisCards [howMany + 1] = hisCards [0];
                    hisCardsSuit [howMany + 1] = hisCardsSuit [0];
                    hisCards [howMany + 2] = hisCards [1];
                    hisCardsSuit [howMany + 2] = hisCardsSuit [1];
                    hisCards [howMany + 3] = hisCards [2];
                    hisCardsSuit [howMany + 3] = hisCardsSuit [2];
                    hisCards [howMany + 4] = hisCards [3];
                    hisCardsSuit [howMany + 4] = hisCardsSuit [3];
                    hisCards [howMany + 5] = yourCards [0];
                    hisCardsSuit [howMany + 5] = yourCardsSuit [0];
                    hisCards [howMany + 6] = yourCards [1];
                    hisCardsSuit [howMany + 6] = yourCardsSuit [1];
                    hisCards [howMany + 7] = yourCards [2];
                    hisCardsSuit [howMany + 7] = yourCardsSuit [2];
                    hisCards [howMany + 8] = yourCards [3];
                    hisCardsSuit [howMany + 8] = yourCardsSuit [3];
                    for (int i = 0 ; i < 55 ; i++)
                    {
                        hisCards [i] = hisCards [i + 4];
                        hisCardsSuit [i] = hisCardsSuit [i + 4];
                        yourCards [i] = yourCards [i + 4];
                        yourCardsSuit [i] = yourCardsSuit [i + 4];
                    }
                }


            }
            round += 1;
        }
        System.out.println ("Winner is " + winner);
        // The end of WarCards2.java  (Phew.)
    }
}

Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Thu Jan 22, 2009 9:40 am   Post subject: RE:Need help fixing program

What kind of error are you experiencing? Does the program compile? Does it run? When and how does it fail? When asking for help, please be a little more specific than "anyone know what's going on here".
syxxone




PostPosted: Thu Jan 22, 2009 9:54 am   Post subject: RE:Need help fixing program

Sorry, I dont know how to let it know when the game is over. also how to give all the cards to the other player after a war. if i do a war it just keeps repeating
syxxone




PostPosted: Thu Jan 22, 2009 9:55 am   Post subject: RE:Need help fixing program

Actually, it only when there is anohe draw during a war
And for some reason there is a 0 of hearts (0, 0) which shouldnt be there.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: