WAR (card game)

[b][red]This message was edited by flex at 2002-10-25 9:17:33[/red][/b][hr]
hi guys

i am a college student is very new to java.

I had been very sick in the hospital and missed 2 weeks of class and i am way behind in my java class.

As a midterm project i am required to write a Java program for the Card game WAR and i would greatly appreciate it if you guys could give me some help

*The program is supposed to accept a user name

*the program, then uses a random number genterator to generate a random card for the user and one for the computer

*whos evers card has a higher value wins...and the program then returns the value (Computer wins) or ("users name" wins).

if they are equal to eachother another card is randomly generated untill a winner is declared

I understand all of you are very busy but i would greatly appreciate if you could help me out

thanks for any help in advance



Comments

  • : I had been very sick in the hospital and missed 2 weeks of class and i am way behind in my java class.

    way behind = 2 weeks? :-)

    I'll give you something to go on with here. I'll take every question by itself so you can easily ask more on the topics that you'll still find confusing.


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • : *The program is supposed to accept a user name

    If you're supposed/allowed to use GUI (and let's say you are) then you might read the user name (and/or any other data, for that matter) by:
    [blue]
    import javax.swing.JOptionPane;
    ...
    String userName = JOptionPane.showMessageDialog (null, "Enter your name");
    [/blue]



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • : *the program, then uses a random number genterator to generate a random card for the user and one for the computer

    You'll have to import the Random class by
    [blue]java.util.Random;[/blue],
    create a object of it's type (preferably as instance variable) by
    [blue]Random rnd = new Random ();[/blue],
    and get some randomized values by
    [blue]int cardValue = rnd.nextInt (13) + 2[/blue].



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • : *whos evers card has a higher value wins...and the program then returns the value (Computer wins) or ("users name" wins).

    The program will not return anything. However, what you're most likely thinking of is that a method returns something.
    [blue]
    if (userCard > compCard)
    return "computer wins";
    else
    return userName + "wins";
    [/blue]
    There's a shorter way to express that but you don't want to hear it now.

    Another thing i can think of is that you'd like an outprint to the screen in which case you should change [red]return "computer wins";[/red] for [blue]JOptionPane.showMessageDialog ("computer wins");[/blue].

    Output to the console would be [blue]System.out.println ("computer wins");[/blue].


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • : *if they are equal to eachother another card is randomly generated untill a winner is declared

    In that case you'll need to keep randomizing until you'll get two values not equal to eachother.
    [blue]
    int userCard, compCard;
    do
    {
    userCard = rnd.nextInt (13) + 2;
    compCard = rnd.nextInt (13) + 2;
    }
    while (userCard == compCard)
    [/blue]

    Also, the algorithm is not very good since you actually can get five (or more) kings in a row (there are only four in every deck) but let's don't bother to much :-)!


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • [b][red]This message was edited by flex at 2002-10-26 13:6:30[/red][/b][hr]
    : : *if they are equal to eachother another card is randomly generated untill a winner is declared
    :
    : In that case you'll need to keep randomizing until you'll get two values not equal to eachother.
    : [blue]
    : int userCard, compCard;
    : do
    : {
    : userCard = rnd.nextInt (13) + 2;
    : compCard = rnd.nextInt (13) + 2;
    : }
    : while (userCard == compCard)
    : [/blue]
    :
    : Also, the algorithm is not very good since you actually can get five (or more) kings in a row (there are only four in every deck) but let's don't bother to much :-)!
    :
    :
    : Kind Regards
    : Konrad
    : ----------------------------
    : (+46/0) 708-70 73 92
    : chamster@home.se
    : http://konrads.webbsida.com
    :
    :

    chamster

    THANK YOU FOR ALL YOUR HELP!!! it definately has given me alot to work with.

    However i need it to generate and display an an actual card. for example

    JOE Smith------------------Computer
    10 of hearts-----------------4 of spades

    i think she told us to use an array to do this and this was what i was having most trouble with. I'm not going to worry about writing the program so that two exact cards can not be generated in the same game(minor details...lol)

    another thing i didn't understand was

    usercard = rnd.nextInt (13)+ 2;....i don't know where those numbers come from

    I will post or email what i wrote up later tonight so you can maybe see where my errors lie.

    again, thanks for your help
    it is much appreciated

    :-)





  • : ...i need it to generate and display an an actual card. ...told us to use an array

    It sounds like you're supposed to generate a deck of cards and then assign one half to the computer and the other half to the user.
    You can use somthing like this:
    [code]
    class Card
    {
    public int color;
    public int value;

    public Card (int _value, int _color)
    {
    this.value = _value;
    this.color = _color;
    }
    }[/code]
    This can (and probably will) be written more Java-way but it's OK for now. What you can do now is to create an array of type Card. Two arrays actually, one for each player.
    [code]
    Card[] user = new Card[52 / 2];
    Card[] comp = new Card[52 / 2];[/code]
    Now you need to randomize all the cards that are in a deck and assign one after one to user/comp using a simple for-loop.

    This part (dealing the cards) can be a little bit tricky and result in a lot of unreadable code that you teacher will hate you for :-). Just ask if you don't see any "nice" way to solve it.



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • : another thing i didn't understand was
    : [blue]usercard = rnd.nextInt (13) + 2;[/blue]
    : ...i don't know where those numbers come from

    Well, the smalest value of a card is 2, the highest 14 (Ace). What you want to do is to randomize an integer value between 0 and 12 (it always starts with zero) and adding 2 to the result.

    Now, 13 means that you get values up to 13 but not including 13 itself. So nextInt (13) means - give me something that is at least zero but smaller than 13 (note - "smaller", not "smaller or equal"!).

    Did it help?


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • In case you wonder where i got "52 / 2" from
    : [code]
    : Card[] user = new Card[52 / 2];
    : Card[] comp = new Card[52 / 2];[/code]

    It's 52 cards in the deck. Every player gets exactly half of the amount which is the number of places you need to reserve for the objects of type Card. If you use the small deck it should be "24 / 2".



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories