MrThoe

Chess in processing Day 1

Jun 1st, 2022
1,247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. Cell[][] cell = new Cell[8][8];
  2. int w;
  3. Piece[] piece = new Piece[32];
  4.  
  5. void setup(){
  6.    size(400,400);
  7.    w = width/8;
  8.    textSize(w);
  9.    for(int i = 0; i < 8; i++){
  10.      for(int j = 0; j < 8; j++){
  11.        cell[i][j] = new Cell(i,j,w);
  12.      }
  13.    }
  14.    makePieces();
  15.      
  16. }
  17.  
  18. void draw(){
  19.    backGround();
  20.    showPieces();
  21.    piece[0].show();
  22. }
  23.  
  24. void makePieces(){
  25.   for(int i = 0; i < 16; i++){
  26.     if(i < 8){ //PAWNS
  27.       piece[i] = new Piece(i, 1, 'P', true);  //Black pieces
  28.       piece[i+16] = new Piece(i, 6, 'P', false);  //White Pieces
  29.     } else if(i == 8 || i == 15){  //ROOKS
  30.       piece[i] = new Piece(i-8, 0, 'R', true);
  31.       piece[i+16] = new Piece(i-8, 7, 'R', false);
  32.     } else if(i == 9 || i == 14){  //'N'IGHTS
  33.       piece[i] = new Piece(i-8, 0, 'N', true);
  34.       piece[i+16] = new Piece(i-8, 7, 'N', false);
  35.     } else if(i == 10 || i == 13){  //Bishops
  36.       piece[i] = new Piece(i-8, 0, 'B', true);
  37.       piece[i+16] = new Piece(i-8, 7, 'B', false);
  38.     } else if(i == 11){  //Queens
  39.       piece[i] = new Piece(i-8, 0, 'Q', true);
  40.       piece[i+16] = new Piece(i-8, 7, 'Q', false);
  41.     } else {  //Kings
  42.       piece[i] = new Piece(i-8,0,'K',true);
  43.       piece[i+16] = new Piece(i-8,7,'K',false);
  44.     }
  45.   }  
  46. }
  47.  
  48. void showPieces(){
  49.   for(int i = 0; i < piece.length; i++){
  50.     piece[i].show();  
  51.   }
  52. }
  53.  
  54. void backGround(){
  55.    for(int i = 0; i < 8; i++){
  56.      for(int j = 0; j < 8; j++){
  57.        cell[i][j].show();
  58.      }
  59.    }
  60. }
  61.  
  62. public class Cell{
  63.   int row;
  64.   int column;
  65.   int w;
  66.   boolean hasPiece;
  67.   boolean isSelected;
  68.  
  69.   public Cell(int i, int j, int w){
  70.     this.row = i;
  71.     this.column = j;
  72.     this.hasPiece = false;
  73.     this.isSelected = false;
  74.     this.w = w;
  75.   }
  76.  
  77.   public void show(){
  78.     if((this.row + this.column)%2 == 0){
  79.       fill(0, 0, 255);
  80.     } else {
  81.       fill(255,0,0);
  82.     }
  83.     rect(this.row*this.w, this.column*this.w, this.w, this.w);
  84.   }
  85. }
  86.  
  87. public class Piece{
  88.   int x;
  89.   int y;
  90.   char ch;
  91.   boolean onBoard;
  92.   boolean isBlack;
  93.  
  94.   public Piece(int x, int y, char ch, boolean isBlack){
  95.      this.x = x;
  96.      this.y = y;
  97.      this.ch = ch;
  98.      this.isBlack = isBlack;
  99.   }
  100.  
  101.   public void show(){
  102.     if(this.isBlack){
  103.       fill(0);
  104.     } else {
  105.       fill(255);
  106.     }
  107.     text(this.ch, (this.x+.15)*w, (this.y+.85)*w);
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment