开发者

Just another novice Java coder asking for approval

开发者 https://www.devze.com 2023-02-11 05:57 出处:网络
So... I\'m learning some Java recently and I want You to help me with my very first program, if You like. This is my code for program, which displays random card from the deck.

So... I'm learning some Java recently and I want You to help me with my very first program, if You like. This is my code for program, which displays random card from the deck.

My question is following: is there any smarter way to make this program work?

/*
 * File: RandomCard.java
 * ----------------
 * This program display random card from the deck with it's own rank
 * (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Quenn, King) and suit (Clubs,
 * Diamonds, Hearts, Spades).
 */

import acm.program.*;
import acm.util.*;

public class RandomCard extends ConsoleProgram {
    /* Run the program */
    public void run() {
        println("This program displays a random card from the deck.");
        println("You random card is " + getRandomRank() + " "
                + getRandomSuit() + ".");       
    }

    /* Get random rank for the card. */
    private String getRandomRank() {
        int rank = rgen.nextInt(1, 13);
        switch (rank) {
            case 1: return "Ace";
            case 2: return "2";
            case 3: return "3";
            case 4: return "4";
            case 5: return "5";
            case 6: return "6";
            case 7: return "7";
            case 8: return "8";
            case 9: return "9";
            case 10: return "10";
            case 11: return "Jack";
            case 12: return "Queen";
            case 13: return "King";
            default: return null;
        }
    }

    /* Create random suit from within Clubs, Diamonds, Hearts and Spades. */
    private String getRandomSuit() {
        int suit = rgen.nextInt(0, 3);
        switch (suit) {
            case 0: return "Clubs";
   开发者_Go百科         case 1: return "Diamonds";
            case 2: return "Hearts";
            case 3: return "Spades";
            default: return null;
        }
    }

    /* Create an instance variable for the random number generator */
    private RandomGenerator rgen = new RandomGenerator();
}


A minor improvement: any time you have a switch statement like this:

 switch (rank) {
        case 1: return "Ace";
        case 2: return "2";
        case 3: return "3";
        case 4: return "4";
        case 5: return "5";
        case 6: return "6";
        case 7: return "7";
        case 8: return "8";
        case 9: return "9";
        case 10: return "10";
        case 11: return "Jack";
        case 12: return "Queen";
        case 13: return "King";
        default: return null;
    }

You can instead declare an array:

String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", 
                   "Jack", "Queen", "King" };

And then:

if (rank < 1 || rank > 13) return null;
return ranks[rank - 1]; // arrays are zero-based


A suggestion, instead of returning String for the Rank and Suite, I would have make them enum instead, that way they are typesafe.

public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }
public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

See here how it is done: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html


There are many different things you could do to this little program.

However, the first thing to ask yourself in this situation is: what do you want to learn about next? After all, the purpose of your exercise is to grow as a programmer.

Some examples that I'd pursue:
Array handling: @JUST MY correct OPINION and @Daniel both offer a way of getting rid of the switch statement and handling this as arrays.

Another might be to learn about classes and collections by creating a Card class and working with a collection to store the various cards as well as retrieve them.

You could even extend the program to play a simple game like blackjack. There are many things to learn about like moving objects (cards) between collections (player hand, deck, dealer hand). Perhaps you could simulate a shuffle by reorganizing an ordered list.

You could add persistance by saving the current "game" to an xml or json file.

The above ideas have real world implications and don't require any "graphic" or online interface.

What you currently have is pretty good quality code, so kudos. Just pick a new concept and apply it to better yourself.


Using this approach with independent lists of enums does not work good. It may be sufficient for the application you have created but if you create some kind of card game you will end up with double cards like two aces of hearts. I suggest you create a class Card which contains a rank and a suite as enums. Create a class Deck wich initializes with creating all cards for a deck. Then you may have a method Card drawNextRandom() which returns the next random card from the deck and removes it from the deck.

public class Card{
  public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
  public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };

  private Rank rank;
  private Suite suite;

  public Card(Rank rank, Suite suite){
    this.rank = rank;
    this.suite = suite;
  }

  // don't forget getters, equals and hashcode
}

public class Deck{

  private RandomGenerator rgen = new RandomGenerator();

  private List<Card> cards = new ArrayList<Card>();

  public Deck(){
    for(Rank rank : Rank.values()){
      for(Suite suite : Suite.values()){
        cards.add(new Card(rank, suite));
      }
    }
  }

  public Card nextRandomCard(){
    int nextCard = rgen.nextInt(0, cards.size());
    Card card = cards.remove(nextCard);  
    return card;
  }
}

This will be some starting point for a card game.

Cheers, Sven

0

精彩评论

暂无评论...
验证码 换一张
取 消