How to make Mancala Board

edited December 2017 in Questions about Code

Q1 That's the Question, I'm wondering If I should use an array, or just use a for loop to create it. Here is the Code Given.

             //Constants that control the setup of the game
            final int NUM_PITS = 6;  //Number of pits on each side of the board (not including stores)
            final int INIT_NUM_OF_STONES = 4; //Initial number of stones in each pit

            //The sizes of the rectangles to be used for the pits and stores
            //These sizes are calculated based on canvas width and height.
            float pitWidth, pitHeight, storeWidth, storeHeight;


            void setup(){
              //The canvas size should be about NUM_PITS+4 units by 3 units in shape
              size(1000,300); //This is good for NUM_PITS=6
              setPitSizes();  //Set the variables that depend on width or height

            }

            void draw(){
              drawMessage("This program doesn't do anything yet",200);
            }


            //==============Given Code==========================

            void setPitSizes(){
              // This sets the size variables that depend on the canvas width and height
              pitWidth = width/(NUM_PITS+4); //NUM_PITS pits plus a double-width store at each end
              pitHeight = height/3; //Divide into thirds: top pits, bottom pits, and a message area
              storeWidth = 2*pitWidth; //The stores are always exactly twice the size of the pits
              storeHeight = 2*pitHeight;
            }

            void drawStonesInBoxes(int numStones, float left, float top, float wide, float high, int colour, int rows, int cols){
              /*This function draws a rectangle, as specified by left, top, wide, high,
              * and with the given colour. It will then draw numStones small black circles
              * within that rectangle, using a rows x cols grid. If numStones is larger than
              * the number of stones that will fit (rows*cols), then a number will be drawn
              * in the centre of the rectangle instead.
              */

              //1. draw the rectangular area
              fill(colour);
              rect(left, top, wide, high);

              //2. If there are too many stones to fit, just draw a big number in the box
              if(numStones>rows*cols){
                textAlign(CENTER,CENTER); //textgoes right in the middle
                fill(0);
                textSize(high/2); //and about half the size of the box
                text(numStones,left+wide/2,top+high/2);
              }

              else {
                fill(0);
                //3. Precalculate the spacing for the rows and columns of stone
                float xSpacing = wide/cols;
                float nextX = xSpacing/2;
                float ySpacing = high/rows;
                float nextY = ySpacing/2;
                float stoneSize = wide/(2*cols); //Fill up about half the width with stone circles
                //Draw the correct number of stones in a grid pattern
                for(int i=0; i<numStones; i++){
                  ellipse(left+nextX,top+nextY,stoneSize,stoneSize);
                  nextX += xSpacing; //Move one stone to the right
                  if(nextX>wide){  //If outside the box
                    nextY += ySpacing;      //go to the next row
                    nextX = xSpacing/2;   //and back to the left again
                  }
                }
              }
            }

            void drawMessage(String message, int colour){
              //This draws a message in the bottom 1/3 of the canvas, against
              //a background of the given colour. The text is black, and sized
              //so that a 2-line message will fit.
              fill(colour);
              rect(0,height*2/3,width,height/3);
              float size = height/12.0;
              textAlign(CENTER,CENTER);
              textSize(size);
              fill(0);
              text(message,width/2,height*5/6);
            }
Tagged:

Answers

  • edited December 2017

    You will need to keep track of the number of stones in each pit. You could do this with lots of variables:

    int number_of_stones_in_pit_0;
    int number_of_stones_in_pit_1;
    int number_of_stones_in_pit_2;
    int number_of_stones_in_pit_3;
    int number_of_stones_in_pit_4;
    int number_of_stones_in_pit_5;
    int number_of_stones_in_pit_6;
    int number_of_stones_in_pit_7;
    int number_of_stones_in_pit_8;
    int number_of_stones_in_pit_9;
    int number_of_stones_in_pit_10;
    int number_of_stones_in_pit_11;
    int number_of_stones_in_pit_12;
    int number_of_stones_in_pit_13;
    

    But look! You're really doing the same thing over and over again. This is not DRY (Don't Repeat Yourself). You should make sure your code is DRY (Don't repeat Yourself). That is, Don;t Repeat Yourself. if you Don;t Repeat Yourself, then when you see that you are doing the same thing over and over again, realize that YES, THERE IS A BETTER WAY.

    In this case, you can use an array. it's like many variables all in one.

    int[] number_of_stones_in_pit = new int[14];
    

    Wow, it's 1/14th the size, but has the same number of variables!

    So now follow along with the directions. How many stones go in pit #0?

    number_of_stones_in_pit[0] = ?????;
    

    How about the other pits?

    number_of_stones_in_pit[1] = ?????;
    

    Is there a pattern? Could you do it with a loop?

  • Another assignment posted verbatim and with no actual attempt from the poster.

    I'm wondering If I should use an array

    Did you read the first paragraph?

  • 1) does say "Create a suitable array" ... seems pretty clear.

Sign In or Register to comment.