Cheesboard lettering?

edited April 2017 in How To...

Hello everybody,

I just get started with processing some days ago so I hope somebody can help me with my problem:

I've written a code for a chessboard but I don't know how to letter the individual fields. Down on the left side i wanted "A1", right next to it "B1" etc. How can I do it?

I hope someone can help me and sorry for my bad english - I'm from Denmark :)

Tagged:

Answers

  • You can make a nested for loop

    Double for loop

    One is for lines other for columns

    To get A1 say

    char(i+65) + str(j)

    Or so

    Use text()

    See reference

  • edited April 2017
    • Arrays use positive integer values as their index, starting from 0.
    • In order to convert a character to an array's index, you can use this formula: int idx = ch - 'A';.
    • Let's say we've got: String field = "D3";. The we get: char ch = field.charAt(0);.
    • Now using int idx = ch - 'A';, we have: int idx = 'D' - 'A';. Result is: idx = 3. *-:)
  • See also reference on for

  • edited April 2017

    Sorry I think I'm too stupid but it doesn't work.

  • edit post, highlight code, press ctrl-o to format it nicely.

    start by printing a bit of text, any bit of text, anywhere...

    now move that text to the right place.

    then modify that to print all the x text in the correct positions.

    then write a loop that'll do that.

    do the same for y positions.

  • edited April 2017

    There is a color selector in menu tools so that you can pick nicer brown colors - or see Wikipedia for colors : https://en.wikipedia.org/wiki/Chess

    The colors used there are [rgb] :

    light field 
    255.0
    206.0
    158.0
    
    
    dark field 
    209.0
    139.0
    71.0
    

    which I found out with this simple sketch :

        PImage chessBoard; 
        color a1 ;
    
        void setup() {
          size (800, 800);
          chessBoard=loadImage("264px-Chessboard480.svg.png");
          background(0);
        } 
    
        void draw() {
          background(0); 
          image(chessBoard, 0, 0); 
          a1 = get(mouseX, mouseY);
    
          text (red(a1)
            +"\n"
            +green(a1)
            +"\n"
            +blue(a1)
            +"\nHit any key to println it (you can copy it from there).", 333, 333);
        }
    
        void keyPressed() {
          println ( 
            red(a1)
            +"\n"
            +green(a1)
            +"\n"
            +blue(a1));
    
          println ( 
            "color colorMy1 = color("
            + int(red(a1))
            + ", "
            + int(green(a1))
            + ", "
            + int(blue(a1)) 
            + ");"  );
        }
    

    The code above looks okay; after rect say fill(111); and then text(.... what I have above - replace i j with x y obviously

    Post this

    I once downloaded the images from Wikipedia and took them for the figures

  • edited April 2017

    Please edit the spelling in your post title:

    Your chessboard (share the code!) probably uses nested loops -- a loop to draw each row, and a loop inside that to draw each square of each row.

    As @Chrisir suggests, you can print letters or numbers separately:

    // print 0123456
    for(int i=0;i<8;i++){
      print(i);
    }
    
    // print ABCDEFGH
    for(int i=0;i<8;i++){
      print(char(i+65));
    }
    

    ...so combine these loops in the same way (or at the same time) as you combine the loops to draw your squares. To draw the text to the screen instead of the console, use text().

  • edited April 2017

    Have you removed your code...? Why? This can be seen as rude here by some. (My answer is not understandable now anymore and it makes me look like a moron).

    Please correct the title from

    Cheesboard to Chessboard

    Thank you!

  • Please don't remove your code like this

  • This is my code till now:

    void setup() {
      size(800, 800);
    }
    
    void draw() {
      background(255);
      for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
          if ((x+y)%2 == 0) {
            fill(255);
          } else {
            fill(0);
          }
          rect(x * 100, y * 100, 100, 100);
        }
      }
    }
    
Sign In or Register to comment.