Java bingo.

Discussion in 'Web Design & Coding' started by RD2DG, Nov 30, 2005.

  1. RD2DG

    RD2DG I smell a bump! VIP Member

    Joined:
    Oct 11, 2003
    Messages:
    549
    Likes Received:
    11
    Location:
    Madison
    So im taking this "compter science" class and we were required to write a Java constructor that made a 2d array of a bingo bored.

    my solution:
    Code:
    public class BingoCard
    {
        private int[][] myCard ;
        /**
         * Constructor for BingoCard objects
         * Initialize the 2-dim array myCard
         * Each column must have 5 different, randomly selected numbers
         * Col 1 numbers must be from 1 to 15 inclusive
         * Col 2 numbers must be from 16 to 30 inclusive
         * Col 3 numbers must be from 31 to 45 inclusive, ignore the free spot for now
         * Col 4 numbers must be from 46 to 60 inclusive
         * Col 5 numbers must be from 61 to 75 inclusive
         */
       public BingoCard()
        {
            myCard = new int[5][5] ;
            for(int r = 0; r < myCard.length; r++)
            {
                for(int c = 0; c < myCard.length; c++)
                {
                    myCard[r][c] = (int)(r*15+1+Math.random()*15) ;
                    if(c > 0)
                    {
                        for(int prevsc = 0; prevsc < c; prevsc++)
                        {
                            while(myCard[r][prevsc] == myCard[r][c])
                            {
                                myCard[r][c] = (int)(r*15+1+Math.random()*15) ;
                            }
                        }
                    }
                    
                }
            }
        }
    }
    
    I am surprized that it works myself because it is so sloppy:roll.
    Also for some reason I think it was made "backwards", im not sure if it is or not.
    Is there other ways that this can be done?
     
    Last edited: Nov 30, 2005
    RD2DG, Nov 30, 2005
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.