unknown pimage bug

edited March 2018 in Questions about Code

I made a code for this chess game that allows the program to detect certain coloured pieces. but for some reason, it doesn't work for white. it works for black, but the strange thing is, if I switch the sides around, it becomes black that doesn't work. somebody please help me.

        PImage board;
        PImage[][] locations = new PImage [8][8];
        PImage wpawn, bpawn, wknight, bknight, wbishop, bbishop, wrook, brook, wqueen, bqueen, wking, bking;
        boolean white = true, turn = white, black = false, click = false;
        int clickx, clicky, clickx1, clicky1;
        player john = new player();
        player ai = new player();
        void setup() {
          size(800, 800);
          board = loadImage("board.png");
          board.resize(width, height);
          imageMode(CENTER);
          ai.spawn(0, true);//0 is black, 1 is white
          john.spawn(1, false);
        }
        void draw() {//------------------------------------------------------------------
          image(board, width/2, height/2);
          john.move();
          ai.move();
          print(white(1, 1));
        }
        void mousePressed() {
          if (click) {
            clickx1 = round(mouseX / (width/8)-0.5);
            clicky1 = round(mouseY / (height/8)-0.5);
            if (locations[clicky][clickx] == bqueen) {
              if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
                locations[clicky][clickx] = null;
                locations[clicky1][clickx1] = null;
                locations[clicky1][clickx1] = bqueen;
              }
            } else if (locations[clicky][clickx] == bbishop) {
              if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
                locations[clicky][clickx] = null;
                locations[clicky1][clickx1] = null;
                locations[clicky1][clickx1] = bbishop;
              }
            } else if (locations[clicky][clickx] == bknight) {
              if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
                locations[clicky][clickx] = null;
                locations[clicky1][clickx1] = null;
                locations[clicky1][clickx1] = bknight;
              }
            } else if (locations[clicky][clickx] == brook) {
              if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
                locations[clicky][clickx] = null;
                locations[clicky1][clickx1] = null;
                locations[clicky1][clickx1] = brook;
              }
            } else if (locations[clicky][clickx] == bpawn) {
              if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
                locations[clicky][clickx] = null;
                locations[clicky1][clickx1] = null;
                locations[clicky1][clickx1] = bpawn;
              }
            } else if (locations[clicky][clickx] == bking) {
                if (validmove(clickx, clickx1, clicky, clicky1, locations)) {
                locations[clicky][clickx] = null;
                locations[clicky1][clickx1] = null;
                locations[clicky1][clickx1] = bking;
              }
            }
            click = false;
          } else {
            clickx = round(mouseX / (width/8)-0.5);
            clicky = round(mouseY / (height/8)-0.5);
            click = true;
          }
        }

        boolean black(int x, int y) {
          if (locations[y][x] == bpawn) {
            return true;
          } else if (locations[y][x] == bbishop) {
            return true;
          } else if (locations[y][x] == bknight) {
            return true;
          } else if (locations[y][x] == brook) {
            return true;
          } else if (locations[y][x] == bqueen) {
            return true;
          } else if (locations[y][x] == bking) {
            return true;
          }else return false;
        }
        boolean white(int x, int y) {
          if (locations[y][x] == wpawn) {
            return true;
          } else if (locations[y][x] == wbishop) {
            return true;
          } else if (locations[y][x] == wknight) {
            return true;
          } else if (locations[y][x] == wrook) {
            return true;
          } else if (locations[y][x] == wqueen) {
            return true;
          } else if (locations[y][x] == wking) {
            return true;
          }else return false;
        }

            boolean validmove(int x, int x1, int y, int y1, PImage[][] locations) {
              if (locations[y][x] == wpawn) {
                if (y!=0) {
                  if (x-x1==1&&y-y1 == 1&&(black(x-1,y-1)||black(x+1,y-1))) {
                    return true;
                  } else if (x-x1==-1&&y-y1 == 1&&(black(x-1,y-1)||black(x+1,y-1))) {
                    return true;
                  }

                  if (locations[y-1][x] == null) {//no block
                    if (x==x1) {
                      if (y == 1||y == 6) {//on rank 2 or 7
                        if (y-y1 <=2) {// can move up to 2 squares
                          return true;
                        } else return false;//moving over 2 squares
                      } else {
                        if (y-y1 == 1) {
                          return true;
                        } else return false;
                      }//else
                    } else return false;
                  } else return false;
                } else return false;
              } else if (locations[y][x] == wbishop) {
               // if (x=x+1);
               // return false;
             // } else {
                return false;
              }else return false;
            }
            void img() {
              wpawn = loadImage("whitepawn.png");
              bpawn = loadImage("blackpawn.png");
              bqueen = loadImage("blackqueen.png");
              wknight = loadImage("whiteknight.png");
              wbishop = loadImage("whitebishop.png");
              bknight = loadImage("blackknight.png");
              wrook = loadImage("whiterook.png");
              brook = loadImage("blackrook.png");
              wking = loadImage("whiteking.png");
              bking = loadImage("blackking.png");
              bbishop = loadImage("blackbishop.png");
              wqueen = loadImage("whitequeen.png");
              bpawn.resize(100, 100);
              wknight.resize(100, 100);
              bknight.resize(100, 100);
              wbishop.resize(100, 100);
              bbishop.resize(100, 100);
              wrook.resize(100, 100);
              brook.resize(100, 100);
              wking.resize(100, 100);
              bking.resize(100, 100);
              wqueen.resize(100, 100);
              bqueen.resize(100, 100);
              wpawn.resize(100, 100);
            }
            class player {
              boolean ai;
              int colour, pawns, knights, bishops, rooks, queens;

              void spawn(int team, boolean computer) {
                colour = team;
                ai = computer;
                img();
                if (colour==0) {
                  locations[0][0] = wrook;
                  locations[0][1] = wknight;
                  locations[0][2] = wbishop;
                  locations[0][3] = wqueen;
                  locations[0][4] = wking;
                  locations[0][5] = wbishop;
                  locations[0][6] = wknight;
                  locations[0][7] = wrook;
                  locations[1][0] = wpawn;
                  locations[1][1] = wpawn;
                  locations[1][2] = wpawn; 
                  locations[1][3] = wpawn;
                  locations[1][4] = wpawn;
                  locations[1][5] = wpawn;
                  locations[1][6] = wpawn;
                  locations[1][7] = wpawn;
                } else {
                  locations[7][0] = brook;
                  locations[7][1] = bknight;
                  locations[7][2] = bbishop;
                  locations[7][3] = bqueen;
                  locations[7][4] = bking;
                  locations[7][5] = bbishop;
                  locations[7][6] = bknight;
                  locations[7][7] = brook;
                  locations[6][0] = bpawn;
                  locations[6][1] = bpawn;
                  locations[6][2] = bpawn;
                  locations[6][3] = bpawn;
                  locations[6][4] = bpawn;
                  locations[6][5] = bpawn;
                  locations[6][6] = bpawn;
                  locations[6][7] = bpawn;
                }
              }
              void display() {
                board();
              }
              void board() {
                for (int i = 0; i<8; i++)
                for (int j = 0; j<8; j++) { 
                  if (locations[j][i] != null) image(locations[j][i], i*width/8+50, j*height/8+50);//piece
                }
              }
              void move() {
                display();
                if (ai) {
                }
              }
            }

Answers

  • edited March 2018

    @Deiplz -- I'm not sure I understand your question, but I'll take a stab at it.

    1. You have 12 PImages -- 6 white, 6 black.
    2. You have a mousePressed function that checks for only 6 PImages -- specifically, the black ones. Your checks look like this:

      if (locations[clicky][clickx] == bqueen) {
      
    3. "but for some reason, it doesn't work for white" -- because you aren't testing for white pieces when you click the mouse?

    4. "if I switch the sides around, it becomes black that doesn't work" -- I don't know what you are switching -- the graphics assets, or something else -- but if you are only click-testing for 6 piece types, you are only going to be able to recognize 6 piece types.

  • you load all 12 images for both players. that's too many.

    and we don't have the images so we can't run this ourselves.

        locations[clicky1][clickx1] = null;
        locations[clicky1][clickx1] = bqueen;
    

    set it to null and then immediately set it to something else? why?

  • For jeremydouglass's question, the switch means that If I switch the locations of black with white, like for example right now [0][0] is occupied by a white rook, what I mean is that it switches with the black, and then [0][0] would be occupied by a black rook and the white rook would be wherever. Does that answer your question?

  • and also, the detect is in draw, the bit of code that says: print(white(1, 1)); the clicks are only for moving the pieces, I have tried swaping the colours, but it's still the same problem. it is either a problem with the boolean, or the PImage, but I think it is the PImage.

  • koogs, that is the take function. I make the spot null to destroy any piece that was there, and then replaces it with bqueen. but maybe I don't need the null function. and for the image problem, I have put the zip file with the data here: https://drive.google.com/open?id=1ehgxRtZ1DuyKbEE4_2xXg2miDbmD91z5

  • I make the spot null to destroy any piece that was there, and then replaces it with bqueen. but maybe I don't need the null function.

    1. setting an object reference to null doesn't destroy what was there -- the reference no longer points to that object -- but garbage collection should deal with dereferenced things eventually -- so:
    2. set the reference to a new object with no unnecessary intermediate null -- and:
    3. null isn't a function -- it is a special value of its own type
  • okay, but what about the original problem?

  • edited April 2018

    Not sure. a bunch of things about the code seem odd -- for one example:

    • you define both a global boolean white and a function white - same for black.

    Also:

    • unused variables: you class has an unused int called "pawns" (and more).
    • style: class Player isn't TitleCase.
    • et cetera

    Maybe a bit of cleanup before taking another shot at debugging?

    Your "player" class always puts the pieces of each color on the same sides of the board -- so if you are expecting a swap to swap sides, it doesn't do that. You can test for white(1,1) or black(6,6), but you can't test for black(1,1) or white(6,6). Again, I don't really know exactly what doing when you are switching, as I believe (?) it isn't shown in code.

  • I have quite a few things I don't understand, but I think you don't get what I mean. It will take quite a long time for you to get it, but One thing I can tell you is that what you suggested isn't the problem. I have updated my code quite a bit, here is it now:

    void img() {
      wpawn = loadImage("whitepawn.png");
      bpawn = loadImage("blackpawn.png");
      bqueen = loadImage("blackqueen.png");
      wknight = loadImage("whiteknight.png");
      wbishop = loadImage("whitebishop.png");
      bknight = loadImage("blackknight.png");
      wrook = loadImage("whiterook.png");
      brook = loadImage("blackrook.png");
      wking = loadImage("whiteking.png");
      bking = loadImage("blackking.png");
      bbishop = loadImage("blackbishop.png");
      wqueen = loadImage("whitequeen.png");
      bpawn.resize(100, 100);
      wknight.resize(100, 100);
      bknight.resize(100, 100);
      wbishop.resize(100, 100);
      bbishop.resize(100, 100);
      wrook.resize(100, 100);
      brook.resize(100, 100);
      wking.resize(100, 100);
      bking.resize(100, 100);
      wqueen.resize(100, 100);
      bqueen.resize(100, 100);
      wpawn.resize(100, 100);
      locations[0][0] = wrook;
      locations[0][1] = wknight;
      locations[0][2] = wbishop;
      locations[0][3] = wqueen;
      locations[0][4] = wking;
      locations[0][5] = wbishop;
      locations[0][6] = wknight;
      locations[0][7] = wrook;
      locations[1][0] = wpawn;
      locations[1][1] = wpawn;
      locations[1][2] = wpawn; 
      locations[1][3] = wpawn;
      locations[1][4] = wpawn;
      locations[1][5] = wpawn;
      locations[1][6] = wpawn;
      locations[1][7] = wpawn;
      locations[7][0] = brook;
      locations[7][1] = bknight;
      locations[7][2] = bbishop;
      locations[7][3] = bqueen;
      locations[7][4] = bking;
      locations[7][5] = bbishop;
      locations[7][6] = bknight;
      locations[7][7] = brook;
      locations[6][0] = bpawn;
      locations[6][1] = bpawn;
      locations[6][2] = bpawn;
      locations[6][3] = bpawn;
      locations[6][4] = bpawn;
      locations[6][5] = bpawn;
      locations[6][6] = bpawn;
      locations[6][7] = bpawn;
    }
    PImage[][] locations = new PImage [8][8];
    PImage wpawn, bpawn, wknight, bknight, wbishop, bbishop, wrook, brook, wqueen, bqueen, wking, bking;
    boolean white = true, turn = white, black = false, click = false;
    int clickx, clicky, clickx1, clicky1;
    player john = new player(0, true);//0 is black, 1 is white
    player ai = new player(1, false);
    void setup() {
      size(800, 800);
      //imageMode(CENTER);
      img();
    }
    void draw() {//------------------------------------------------------------------
      john.move();
      ai.move();
      print(notwhite(0, 0));
      showboard();
      noStroke();
    }
    void mousePressed() {
      if (click) {
        clickx1 = round(mouseX / (width/8)-0.5);
        clicky1 = round(mouseY / (height/8)-0.5);
        if (validmove(clickx, clickx1, clicky, clicky1)) {
          if (locations[clicky][clickx] == bqueen) {
            locations[clicky][clickx] = null;
            locations[clicky1][clickx1] = bqueen;
          } else if (locations[clicky][clickx] == bbishop) {
            locations[clicky][clickx] = null;
            locations[clicky1][clickx1] = bbishop;
          } else if (locations[clicky][clickx] == bknight) {     
            locations[clicky][clickx] = null;
            locations[clicky1][clickx1] = bknight;
          } else if (locations[clicky][clickx] == brook) {
            locations[clicky][clickx] = null;
            locations[clicky1][clickx1] = brook;
          } else if (locations[clicky][clickx] == bpawn) {
            locations[clicky][clickx] = null;
            locations[clicky1][clickx1] = bpawn;
          } else if (locations[clicky][clickx] == bking) {
            locations[clicky][clickx] = null;
            locations[clicky1][clickx1] = bking;
          }
        }
        click = false;
      } else {
        clickx = round(mouseX / (width/8)-0.5);
        clicky = round(mouseY / (height/8)-0.5);
        click = true;
      }
    }
    void showboard() {
      for (int i = 0; i<8; i++)
        for (int j = 0; j<8; j++) { 
          if ((i+j)%2 == 0) fill(180, 180, 255);
          else fill(200, 115, 115);
          rect(i*width/8, j*height/8, width/8, height/8);//chessboard
          if (locations[j][i] != null) image(locations[j][i], i*width/8, j*height/8);//piece
          if (click) {
            if (validmove(clickx, i, clicky, j)) {
              fill(50, 175, 50, 150);//highlight posibble moves in red
              ellipse(i*width/8+50, j*height/8+50, 20, 20);
            }
            if (j == clicky && i == clickx && locations[j][i] != null) {
              fill(0, 0, 255, 100);//highlight piece in blue
              rect(i*width/8, j*height/8, width/8, height/8);
            }
          }
        }
    }
    boolean validmove(int x, int x1, int y, int y1) {
      if (locations[y][x] == bpawn) {
        if(y!=0){
        if (x-x1==1&&y-y1 == 1&&locations[y-1][x-1]!=null) return true;
        else if (x-x1==-1&&y-y1 == 1&&locations[y-1][x+1]!=null)return true;
        if (locations[y-1][x] == null) {//no block
          if (x==x1) {//if not change files
            if (y == 1||y == 6&&locations[y-2][x]==null) return((y-y1 ==1||y-y1==2));
            else return (y-y1 == 1);
          } else return false;//x=x1
        } else return false;//no block
        }else{ locations[y][x]=bqueen;
        return false;
        }
      } else if (locations[y][x] == bbishop) {
        return (abs(x-x1)==abs(y-y1));
      } else if (locations[y][x] == brook) {
        return (y==y1||x==x1);
      } else if (locations[y][x]==bqueen) {
        return (abs(x-x1)==abs(y-y1)||y==y1||x==x1);
      } else if (locations[y][x]==bknight) {
        return((abs(y-y1)==2&&abs(x-x1)==1)||(abs(y-y1)==1&&abs(x-x1)==2));
      } else if (locations[y][x]==bking) {
        return true;
      } else {
        return false;
      }
    }
    class player {
      boolean ai;
      int colour, pawns, knights, bishops, rooks, queens;
      player(int team, boolean computer) {
        colour = team;
        ai = computer;
      }
      void move() {
        for (int i = 0; i<8; i++)
          for (int j = 0; j<8; j++)
            if (locations[j][i] != null) image(locations[j][i], i*width/8+50, j*height/8+50);//piece
      }
    }
    boolean black (int y, int x) {
      return (locations[y][x] == bqueen || locations[y][x] == bknight || locations[y][x] == bpawn || locations[y][x] == brook || locations[y][x] == bbishop || locations[y][x] == bking);
    }
    boolean white (int y, int x) {
      return (locations[y][x] == wqueen || locations[y][x] == wknight || locations[y][x] == wpawn || locations[y][x] == wrook || locations[y][x] == wbishop || locations[y][x] == wking);
    }
    boolean notblack (int y, int x) {
      return (white(y, x) || locations[y][x] ==null);
    }
    boolean notwhite (int y, int x) {
      return (black(y, x) || locations[y][x] ==null);
    }
    
  • Answer ✓

    sorry, but for some reason it works now, thank you

  • ;)

Sign In or Register to comment.