Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Racko maddness

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Racko maddness

    So my teacher gave us code that plays out the old Racko game. All we need to do is think of some kind of strategy of playing the game. So what I have done is just make the player takes card from the discarded deck and if it is in 1-6, then it goes into the first slot, 7-12 into the 2nd, 13-18 into the 3rd, and so on until 54-60 into the 10th slot. However, it isn't the most beneficial to the player. Does anyone think of a better code. This is my code:

    import java.util.Random;

    public class PlayerRyan implements Player
    {
    Random random = new Random();

    public void beginGame(Rack rack)
    { }

    public int acceptCard(Rack rack, Card card)
    {
    int k = 0;
    return k;
    }

    public int placeCard(Rack rack, Card card)
    {
    int x = card.value();
    if(x <= 6)
    {
    return 1;
    }

    if(x >= 7 && x <= 12)
    {
    return 2;
    }

    if(x >= 13 && x <= 18)
    {
    return 3;
    }

    if(x >= 19 && x <= 24)
    {
    return 4;
    }

    if(x >= 25 && x <= 30)
    {
    return 5;
    }

    if(x >= 31 && x <= 36)
    {
    return 6;
    }

    if(x >= 37 && x <= 41)
    {
    return 7;
    }

    if(x >= 42 && x <= 47)
    {
    return 8;
    }

    if(x >= 48 && x <= 53)
    {
    return 9;
    }

    if(x >= 54 && x <= 60)
    {
    return 10;
    }
    return 0;
    }
    }

    I have other code that plays the rest of the game so I only need a better strategy for my player. my player inherits from a couple of other programs.

    Player:
    public interface Player
    {
    // beginGame is invoked at the start of each game
    // rack is the initial randomly dealt rack
    // you may initialize any variables here
    public void beginGame(Rack rack);

    // acceptCard is invoked at the start of each turn
    // rack is the current state of the rack
    // card is the last discard from the previous player's turn
    // you may return a slot number, or 0 to be dealt another card
    public int acceptCard(Rack rack, Card card);

    // placeCard is invoked if zero is returned by acceptCard
    // rack is the current state of the rack
    // card is the newly drawn card
    // you may return a slot number, or 0 to discard the new card
    public int placeCard(Rack rack, Card card);
    }

    Rack:
    public class Rack extends Deck
    {
    public static final int LEN = 10;

    public Rack()
    { }

    public Rack(Rack that)
    {
    for (int i = 0; i < LEN; i++)
    {
    add(new Card(that.get(i)));
    }
    }

    public String toString()
    {
    String s = "";
    for (int i = 0; i < LEN; i++)
    {
    if (i > 0) s += ",";
    if (s.length() > 60)
    {
    s += "+";
    break;
    }
    s += get(i).value();
    }
    return "[" + s + "]";
    }

    public boolean isSorted()
    {
    boolean b = true;
    int i = 1;
    while (i < LEN)
    {
    if (get(i).compareTo(get(i-1)) < 0)
    {
    b = false;
    break;
    }
    i++;
    }
    return b;
    }

    public boolean isBetween(int i)
    {
    boolean b = false;
    if (i >= 1 && i <= LEN - 2)
    {
    int v1 = get(i-1).value();
    int v2 = get(i).value();
    int v3 = get(i+1).value();
    b = (v1 < v2 && v2 < v3);
    }
    return b;
    }

    public void show()
    {
    String line = " -------------------------------------------------------------";
    System.out.println(line);
    for (int i = LEN-1; i >=0 ; i--)
    {
    int v = get(i).value();
    String format = "%2d %" + v + "d\n%s\n";
    System.out.printf(format, i+1, v, line);
    }
    }

    public int gap(int i)
    {
    return gap(i, i+1);
    }

    public int gap(int i, int j)
    {
    int g = 0;
    if (i >= 0 && j < LEN)
    {
    g = get(j).value() - get(i).value();
    }
    return g;
    }
    }

    Card:
    public class Card implements Comparable<Card>
    {
    public static final int MIN = 1;
    public static final int MAX = 60;
    private static final int bad = 0;
    private int val= bad;

    public Card()
    {
    val = bad;
    }

    public Card(int num)
    {
    if (num >= MIN && num <= MAX) val = num;
    }
    public Card(Card that)
    {
    this.val = that.val;
    }

    public String toString()
    {
    return "[" + val + "]";
    }

    public boolean equals(Card that)
    {
    return this.val == that.val;
    }

    public int compareTo(Card that)
    {
    if (this.val < that.val) return -1;
    if (this.val > that.val) return 1;

    return 0;
    }

    public int value()
    {
    return val;
    }
    }

    Deck:
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Collections;

    public class Deck extends LinkedList<Card>
    {
    public Deck()
    { }

    public Deck(boolean fill)
    {
    if (fill)
    {
    fill();
    }
    }

    public void fill()
    {
    clear();
    int min = Card.MIN;
    int max = Card.MAX;
    for (int k = min; k <= max; k++)
    {
    add(new Card(k));
    }
    }

    public void shuffle()
    {
    Collections.shuffle(this);
    }

    public void deal(Rack rack)
    {
    if (Rack.LEN < size())
    {
    rack.clear();

    for (int i = 0; i < Rack.LEN; i++)
    {
    rack.add(pop());
    }
    }
    }

    public void deal(List<Rack> racks)
    {
    if (racks.size() * Rack.LEN < size())
    {
    for (Rack rack : racks)
    {
    rack.clear();
    }
    for (int i = 0; i < Rack.LEN; i++)
    {
    for (Rack rack : racks)
    {
    rack.add(pop());
    }
    }
    }
    }

    // public void print()
    // {
    // for (Card card : this)
    // {
    // System.out.println(card);
    // }
    // }

    public void print()
    {
    for (int i = 0; i < size(); i++)
    {
    System.out.printf("%5d) %3s\n", i, get(i));
    }
    }

    public Deck complement()
    {
    Deck deck = new Deck(true);
    Deck comp = new Deck(false);
    for (Card card : deck)
    {
    if (!contains(card))
    {
    comp.add(card);
    }
    }
    return comp;
    }

    public double average()
    {
    double a = 0;
    double t = 0;
    int n = 0;
    for (Card card : this)
    {
    t += card.value();
    n += 1;
    }
    if (n > 0)
    {
    a = t/n;
    }
    return a;
    }

    // public static Deck complement(Rack rack)
    // {
    // Deck deck = new Deck(true);
    // Deck comp = new Deck(false);
    // for (Card card : deck)
    // {
    // if (!rack.contains(card))
    // {
    // comp.add(card);
    // }
    // }
    // return comp;
    // }
    }

    Program:
    import java.util.Collections;
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Random;
    import java.util.Scanner;

    class Program
    {
    public static void main(String[] args)
    {
    test2();
    }

    public static void test1()
    {
    Deck deck = new Deck(true);
    Collections.shuffle(deck);
    deck.print();

    Rack rack1 = new Rack();
    Rack rack2 = new Rack();
    List<Rack> racks = new LinkedList<Rack>();
    racks.add(rack1);
    racks.add(rack2);
    deck.deal(racks);
    rack1.print();
    rack2.show();
    }

    public static void test2()
    {
    Deck deck = new Deck(true);
    Collections.shuffle(deck);
    deck.print();

    Rack rack = new Rack();
    deck.deal(rack);

    Scanner scanner = new Scanner(System.in);

    int c = 0;

    while (deck.size() > 0)
    {
    rack.show();

    if (rack.isSorted())
    {
    System.out.printf("You Win!!!\n");
    System.out.printf("It only took %d moves.\n", c);
    break;
    }

    c++;

    Card card = deck.pop();
    System.out.printf("%3d) %s Slot or Discard: ", c, card);

    String s = scanner.next();

    int k = 0;
    try
    {
    k = Integer.parseInt(s);
    if (k < 1 || k > 10) k = 0;
    }
    catch (Exception ex)
    { }

    if (k > 0)
    {
    rack.set(k-1, card);
    }
    }
    }

    public static boolean delay(int milliseconds)
    {
    boolean success = true;
    try
    {
    Thread.sleep(milliseconds);
    }
    catch (InterruptedException ex)
    {
    Thread.currentThread().interrupt();
    success = false;
    }
    return success;
    }
    }

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Racko maddness

    Welcome to the forum!

    Please read this topic to learn how to post code correctly along with other useful hints/tips for newcomers. Then fix your post so the code is readable.

Tags for this Thread