# Arranging objects in random rows and columns

**URL:** <https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163>\
**Category:** Coding Questions\
**Created:** [March 1, 2021, 9:57am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163 "2021-03-01T09:57:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 1, 2021, 9:57am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/1 "2021-03-01T09:57:42Z")

</div>

private void InitialSetup()  
{  
AddPiece(whiteRook, white, 0, 0);  
AddPiece(whiteKnight, white,1,2);  
AddPiece(whiteBishop, white,2,0);  
AddPiece(whiteQueen, white, 3,4);  
AddPiece(whiteKing, white, 4, 6);  
AddPiece(whiteBishop, white,5,0);  
AddPiece(whiteKnight, white,6,3);  
AddPiece(whiteRook, white, 7, 6);  
}

public void AddPiece(GameObject prefab, Player player, int col, int row)  
{

GameObject pieceObject=board.AddPiece(  
prefab, col, row);

```
player.pieces.Add(pieceObject);
pieces[col, row] = pieceObject;

```

}

8 columns 6 rows I have 48 game objects, I want them to be arranged in random rows and columns every time the game starts, but how can I do this with the addPiece method?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 1, 2021, 10:31am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/2 "2021-03-01T10:31:56Z")

</div>

in theory, add all objects to an array A and shuffle it. (or shuffle an integer array and use it as and index)

Then you can just make a nested for-loop and add consecutively from the list A

```auto
int k=0; 
for (int rowNumber = 0; rowNumber < 6...{
   for (int colNumber = 0; colNumber < 8... {
       pieces[colNumber, rowNumber] = A[k];
       k++;
   }
}

```

For a similar shuffle example see [Can someone help me with this game? - #4 by Chrisir](https://discourse.processing.org/t/can-someone-help-me-with-this-game/28147/4)

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 1, 2021, 11:07am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/3 "2021-03-01T11:07:35Z")

</div>

Thanks for your answer but it won’t accept them without adding addPiece method I guess I need to edit the method randomly to do this action.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 1, 2021, 11:24am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/4 "2021-03-01T11:24:49Z")

</div>

you can call addPiece method from within the nested for-loop

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 1, 2021, 11:28am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/5 "2021-03-01T11:28:30Z")

</div>

can you write a small example

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 1, 2021, 11:30am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/6 "2021-03-01T11:30:24Z")

</div>

```auto
int k=0; 
for (int rowNumber = 0; rowNumber < 6...{
   for (int colNumber = 0; colNumber < 8... {
       AddPiece(A[k], white, colNumber, rowNumber ) ;
       k++;
   }
}

```

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 1, 2021, 11:31am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/7 "2021-03-01T11:31:26Z")

</div>

for (int i = 0; i \< 8; i++)  
{  
AddPiece(whitePawn, white, i, 1);  
}

Just add a few add Piece methods one under the other and make them arbitrarily arranged on the 6th line.

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 1, 2021, 11:34am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/8 "2021-03-01T11:34:12Z")

</div>

what’s the “k” here?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 1, 2021, 11:35am UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/9 "2021-03-01T11:35:32Z")

</div>

assuming that A contains all figures in a random order, k would give an index for it to read from the array.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [March 1, 2021, 5:36pm UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/10 "2021-03-01T17:36:29Z")

</div>

Here is my take on saving a chessboard. Feel free to use it as a reference:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c3be46432c7f8d2c226d46722fe9efc546d87850.png)  
Here are the pieces if you want to use it:  
 ![sprites](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/af98f7a213d1060ab4e604eb98f9ad0e4483e30e.png)

> **Code**
>
> ```auto
> int w = 8, h =8, scl = 100, sScl = 173, v = 0, sel = -1;
> boolean creative = true;
> PImage source;
> ArrayList<PImage> sprites = new ArrayList<PImage>();
> int grid[][] = new int[w][h];
> String board = "11,12,0,0,0,0,6,5,10,12,0,0,0,0,6,4,9,12,0,0,0,0,6,3,8,12,0,0,0,0,6,2,7,12,0,0,0,0,6,1,9,12,0,0,0,0,6,3,10,12,0,0,0,0,6,4,11,12,0,0,0,0,6,5";
> 
> void setup() {
> loadBoard(board);
> loadImages();
> imageMode(3);
> size(800, 800);
> }
> 
> void draw() {
> noStroke();
> displayGrid();
> }
> void displayGrid() {
> background(#50c878);
> fill(255);
> for (int j = 0; j < h; j++) for (int i = j % 2; i < w; i+=2) rect(i*scl, j*scl, scl, scl);
> if(!creative) fill(0, 0, 255);
> else noFill();
> if (!creative && sel != -1) rect((sel % w)*scl, floor(sel/w)*scl,scl,scl);
> for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) if (grid[i][j] != 0) image(sprites.get(grid[i][j]-1), (i+0.5)*scl, (j+0.5)*scl);
> if (creative) image(sprites.get(v), mouseX, mouseY);
> }
> 
> String saveBoard() {
> String output = "";
> for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) output += grid[i][j]+((( (i == w-1 && j == h-1)? "" : ",")));
> return output;
> }
> void loadBoard(String input) {
> int info[] = int(split(input, ","));
> for (int i = 0; i < info.length; i++) {
> int y = i % w, x = floor(i/w);
> grid[x][y] = info[i];
> }
> }
> void mousePressed() {
> int mx = constrain(floor(map(mouseX, 0, w*scl, 0, w)), 0, w-1), my = constrain(floor(map(mouseY, 0, h*scl, 0, h)), 0, h-1);
> if (creative) grid[mx][my] = ((((mouseButton == LEFT)? v+1 : 0)));
> else {
> if(grid[mx][my] == 0) sel = sel;
> else sel = mx+my*w;
> }
> }
> void mouseWheel(MouseEvent e) {
> v -= e.getCount();
> if (v > sprites.size()-1) v = 0;
> if (v < 0) v = sprites.size()-1;
> }
> 
> void loadImages() {
> source = loadImage("sprites.png");
> for (int i = 0; i < 12; i++) {
> sprites.add(shrink(source.get(i*sScl, 0, sScl, sScl), 0.5));
> }
> for (int i = 0; i < sprites.size(); i++) {
> image(sprites.get(i), i*sScl, 0);
> }
> }
> PImage shrink(PImage input, float value) {
> input.resize(floor(input.width*value), floor(input.height*value));
> return(input);
> }
> void keyPressed() {
> if (key == ' ') println(saveBoard()+"\n");
> else if (key == 'c') {
> creative = !creative;
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 2, 2021, 12:07pm UTC](https://discourse.processing.org/t/arranging-objects-in-random-rows-and-columns/28163/11 "2021-03-02T12:07:54Z")

</div>

here is an example of shuffle

```auto

import java.util.*; 

final int rows = 10; 
final int columns = 8;
final float unit = 90.0;

ArrayList<Cube> cubes = new ArrayList();

void setup() { 
  size(1600, 860, P3D);

  int count; 

  count = rows * columns; // wideCount * highCount * depthCount; 
  // cubes = new Cube[count];

  // setting up the grid 
  int index = 0; 
  for (int z = 0; z < rows; z++) {
    for (int x = 0; x < columns; x++) { 
      cubes.add( new Cube(x*unit+200, height/2.0+280, z*unit-800, true));
    }
    // }
  }
  //
}//func 

void draw() { 
  background(0); 
  lights();

  //for (int i = 0; i < cubes.size(); i++) { 
  // cubes.get(i).display();
  //}

  int index = 0;  
  for (int z = 0; z < rows; z++) {
    for (int x = 0; x < columns; x++) { 
      cubes.get(index).displayAt( x*unit+200, height/2.0+280, z*unit-800);
      index++;
    }
  }

  if (frameCount == 77 ||
    keyPressed) {
    Collections.shuffle(cubes);
  }//if
}//func 

// ==============================================================================================================

class Cube { 

  float posX; 
  float posY; 
  float posZ; 

  final color RED = color(255, 0, 0); 
  final color GREEN = color(0, 255, 0); 

  color col; // color (3*cubes.size(), 0, 0); // color(random(255), random(255), random(255)); // color(255, 0, 0); // 
  boolean exist = false; 

  float sizeCube = 90;

  int id; 

  // Contructor 
  Cube(float xOffsetTemp, float yOffsetTemp, float zOffsetTemp, 
    boolean existTemp) { 
    posX = xOffsetTemp; 
    posY = yOffsetTemp;
    posZ = zOffsetTemp;
    exist = existTemp;

    id=cubes.size();

    col = lerpColor(RED, GREEN, map( cubes.size(), 0, rows*columns, 0, 1 ));
  }// Contructor 

  // Custom method for drawing the object 
  void display() { 
    if (exist) {
      stroke(255); 
      fill(col);
      pushMatrix();
      translate(posX, posY, posZ);
      box(sizeCube);
      fill(255); 
      text(id, 0, -sizeCube/2-6); 
      popMatrix();
    }//if
  }//method

  // Custom method for drawing the object 
  void displayAt(float posX_, float posY_, float posZ_) { 
    if (exist) {
      stroke(255); 
      fill(col);
      pushMatrix();
      translate(posX_, posY_, posZ_);
      box(sizeCube);
      fill(255); 
      text(id, 0, -sizeCube/2-6); 
      popMatrix();
    }//if
  }//method
  //
} //class
//

```
