Hello all,
I just wrote a simple chess board and make a mcve to demonstrate my problem.
It’s that when you have a 2d array that stores the x,y position for each of the 64 cells, you need in setup() to make the for-loop for Y BEFORE the for-loop for x. I find this a bit confusing.
(which is not stated here: Two-Dimensional Arrays / Processing.org)
Also, since the y-value 8 in chess is BELOW and 1 is on the top (of the board) that is also inverted in comparison to processing where 0 is on the top and height (max) at the bottom. I tackle this in setup() as well.
Maybe someone can check if my reproach (in setup() and in also in drawChessBoard()) is correct and shed some light on this.
Order of indices
For example, PVector [][] feldPos = new PVector [9] [9]; :
- can I say
feldPos [x][y]or is
feldPos [y][x](with swapped x and y) better / correct?
Thanks to you all!
Warm regards,
Chrisir
// mcve for Chess board
final int groesseZelle = 40; // Feldgröße (Kantenlänge einer Zelle)
// Hier werden die zwei Feldfarben benannt, also jeweils in einer Variable gespeichert:
final color weiss = color (255.0, 206.0, 158.0); // Weiss
final color schwarz = color (209.0, 139.0, 71.0); // schwarz
final color YELLOW = color(255, 255, 44);
final color GREEN = color(0, 255, 0);
final color RED = color(255, 0, 0);
float sx = 216.0, //
sy = 120;
float xadd = 3.4; //
float g = 10.0; //
// wir speichern die Position aller Felder des Schabretts
PVector [][] feldPos = new PVector [9] [9];
// ======================================================================================
void setup() { // läuft nur einmal
size (600, 700); // X / Y
int posx = 120;
int posy = 180+7*groesseZelle;
// define BOARD Schachbrett /// first y THEN x for-loop!!!!
for (int y=1; y<9; y= y+1) { // for int y rows
for (int x=1; x<9; x = x+1) { // for int x columns
feldPos[x][y] = new PVector(posx, posy);
// COLUMN x increase
posx=posx+groesseZelle;
} // for x
// New Row
posy = posy-groesseZelle; // nächste Zeile y
posx=120; // Grundstellung posx
} // for y
background(200);
} // Ende sub-routine
void draw() { // läuft 60 mal pro Sekunde
background(200); // löschen
Ueberschrift(); // Schach
drawChessBoard(); // 64 Felder
BuchstabenWaagerecht(); // a..h
ZahlenSenkrecht(); // 1..8
} // Ende sub-routine draw()
// ======================================================================================
void Ueberschrift() { // Schach <--- / -->
fill(255.0, 10.0, 0.0);
// sx = sx + xadd;
if (sx >=390) // nach rechts
xadd = xadd * -1;
g = g + 0.50;
if (sx <= 0) // nach links
xadd = xadd * -1;
textSize(32); // " SCHACH " - grösse
text(" Schach ", sx, sy);
} // Ende Übersschrift
// ======================================================================================
void drawChessBoard () {
for (int x=1; x<9; x = x+1) { // for int x columns
for (int y=1; y<9; y= y+1) { // for int y rows
setzeDieFillFeldFarbe(x, y);
noStroke(); // no frame
rect (feldPos[x][y].x, feldPos[x][y].y, groesseZelle, groesseZelle); //
textSize(12);
fill(0);
text ( str(x)+","+str(y),
feldPos[x][y].x+12, feldPos[x][y].y+12);
} // end for int y
} // End for X
} // End func
// ======================================================================================
void setzeDieFillFeldFarbe(int xLokal, int yLokal) {
// setzt die fill Farbe für das nächste Feld des Schachbretts
if (xLokal % 2 == 0) { // if x GERADE
// Anfang x GERADE
if (yLokal % 2 == 0) // if y
fill(weiss);
else
fill(schwarz);
//
} // ende if x
else { // Anfang x UNGERADE
// Anfang x UNGERADE
if (yLokal % 2 == 0) {
fill(schwarz); // hell
} else {
fill(weiss);
} // ende else y
//
} // ende else x
} // Ende sub-routine
// ======================================================================================
void BuchstabenWaagerecht() { // Buchstaben a bis h waagerecht
char buchstabe = 'a';
int posz = 80;
for ( int z = 1; z < 9; z = z + 1 ) {
posz = posz + 40;
fill(255);
textSize(30);
text(buchstabe++, posz, 530);
} // ende for Buchstaben
} // Ende sub-routine
// ======================================================================================
void ZahlenSenkrecht() {
// Zahlen 1 bis 8 senkrecht
textSize(30);
for (int b=1; b < 9; b = b+1) {
int yposs=160 + (9-b) * 40;
text( b, 80, yposs);
} // Ende for
} // Ende sub-routine

