Arranging objects in random rows and columns

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?

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

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

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir

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.

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

can you write a small example

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

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.

what’s the “k” here?

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

1 Like

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


Here are the pieces if you want to use it:

Code
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;
  }
}
1 Like

here is an example of shuffle


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
//