How to apply certain colours to specific parts of a 2d array?

edited December 2016 in Questions about Code

So i am making a game of connect four and am trying to use the arrow keys to navigate and drop the tokens in the game. I dont want to use a mousepressed function and am at a loss of how to apply the players input when he presses on the down arrow. the code below is what i have so far:

int redScore= 0;
int blueScore= 0;
int rows = 6;
int cols =7;
Cell[][] ellipseGrid = new Cell[cols][rows];
int[][] gridChecker = new int[cols][rows];
int colCount = 4;
boolean left = false;
color red = color(#FF0505);
color blue = color(#053DFF);

void setup()
{
  size(1600,800);
  int a;
  int b;
 for ( a = 0; a<cols; a++) {
    for (b = 0; b<rows; b++) {
      ellipseGrid[a][b] = new Cell(700+a*80,200+b*80,45,45,255);

    }}}




void draw()
{
   background(255); 


background(0);
 scoreboard();
  fill(#F9FA03);
  rect(660,160,560,480);
  for (int a = 0; a < cols; a++) {
    for (int b = 0; b < rows; b++) {
      fill(255);
  ellipseGrid[a][b].display();

  indicator();


  textSize(40);  
text("HI  =  "+ colCount,500,600);


    }}}

void colUpdate()
{
   for (int a = 0; a<cols; a++) {
        for (int b = 0; b<rows; b++) {
  if(colCount =1)
  {




  }
        }}}







void indicator()
{
if (colCount == 1)
{
  triangle(700,150,690,110,710,110);
  }
 if (colCount == 2)
{
  triangle(780,150,770,110,790,110);
  }
  if (colCount == 3)
{
  triangle(860,150,850,110,870,110);
  }
  if (colCount == 4)
{
  triangle(940,150,930,110,950,110);
  }
  if (colCount == 5)
{
  triangle(1020,150,1010,110,1030,110);
  }
  if (colCount == 6)
{
  triangle(1100,150,1090,110,1110,110);
  }
  if (colCount == 7)
{
  triangle(1180,150,1170,110,1190,110);
  }


}

void keyPressed()
{
   if((keyCode == LEFT) && (colCount > 1))
   colCount --;

   if((keyCode == RIGHT) && (colCount <7))
   colCount++;

   if((keyCode == DOWN) && (colCount <7 && colCount >1))
{
  for (int a = 0; a < cols; a++) {
    for (int b = 0; b < rows; b++) {
   ellipseGrid[a][b+1].display();


    }}}


  }


void scoreboard()
{
  fill(#F50008);
 rect(0,0,1600,400); 

  fill(#0005F5);
  rect(0,400,1600,400);


  fill(255);
  textSize(53);
   text("Player 1 score:"+ redScore ,50,200); 

   text("Your Turn" ,50, 300);

  text("Player 2 score :"+ blueScore, 50, 600);

  textSize(15);
  text("press z to reset score",1400,100);



//playeroneBoard();  
//playertwoBoard();  



}
void intro()
{
   fill(#1203FF);
 textSize(50);
  text("Welcome to the", width/2 -350,height/2 -100);

 fill(#1203FF);
  textSize(50);
  text("game of Connect Four",width/2 -300,height/2 );

ConnectFourBoard();
 }

 void ConnectFourBoard(){      // This is the image that is used in the intro screen of the connect four board


    fill(#F6FF03);
 rect(width/2-100,height/2+100,220,200);              // rectangle for board

       for(int i=1;i<rows;i++) {
    for(int j=1;j<cols;j++) {
      fill(255);
      ellipse(688+(i*(210/cols)), 490+(j*(250/rows)), 30, 30);
    }}}


class Cell {
  // A cell object knows about its location in the grid 
  // as well as its size with the variables x,y,w,h
  float x,y;   // x,y location
  float w,h;   // width and height
 color currentcolor;

  // Cell Constructor
  Cell(float circleX, float circleY, float circleW, float circleH,color icolor) {
    x = circleX;
    y = circleY;
    w = circleW;
    h = circleH;
   currentcolor = icolor;
  } 

  // Oscillation means increase angle


  void display() {
    stroke(255);

  fill(currentcolor);
    ellipse(x,y,w,h); 
  }
}

Answers

  • edited December 2016

    @mzadery --

    Please edit and format your code so it is readable / testable by forum members.

    edit > highlight code > CTRL+o > save

    https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

  • thanks, ive never used these forums. Appreciate the help

  • In Processing, try cleaning up the code with command+T.

    First, this code doesn't run due to errors. Have you tried clicking on them in the errors box and trying to fix them? For example:

    if(colCount =1)
    

    This gives you an out of bounds exception. Why are you trying to access [b+1]?

    ellipseGrid[a][b+1].display();
    

    This is a long, complicated piece of code. Did the code once work before your latest modifications? Or was the sketch assembled without ever working?

  • First, get some simpler version to work. Then go to a more complex one. Break up any problem into multiple parts, it makes your work easier.

  • edited December 2016 Answer ✓

    Hello,

    You have a class Cell. Good.

    The basic idea is to let the cell know whether it is filled or not.

    When a chip is placed above it (in its column) the first empty cell gets filled with this chip.

    I also made the intro work using state.

    I ordered your functions differently.

    Best, Chrisir ;-)

    // the state of the program dictates whether 
    // we show the intro or the game 
    
    // we name the values state can have: 
    final int intro =0;
    final int game  =1; 
    int state = intro;
    
    // -----
    
    int redScore= 0;
    int blueScore= 0;
    
    int rows = 6;
    int cols =7;
    
    Cell[][] ellipseGrid = new Cell[cols][rows];
    
    // This is not in use and is part of the class Cell now!!!!!
    // int[][] gridChecker = new int[cols][rows];
    
    // signifies your arrow for input pos
    int colCount = 4;
    
    // ??? : 
    //boolean left = false; // and right?
    
    color red = color (255, 0, 0); //  color(#FF0505);
    color blue = color(#053DFF);
    
    // ----------------------------------------------------------
    // primary functions 
    
    void setup()
    {
      size(1600, 800);
    
      for (int a = 0; a<cols; a++) {
        for (int b = 0; b<rows; b++) {
          ellipseGrid[a][b] = new Cell(700+a*80, 200+b*80, 
            45, 45, 
            color(255));
        }
      }
    }
    
    void draw()
    {
      switch(state) {
    
      case intro:
        intro();
        break;
    
      case game:
        game(); 
        break;
    
      default:
        println ("Error  58: unknown state !!!!!!!!!!!!!!!!!!!!!!!!");
        exit();
        break;
      }//switch
    } // func 
    
    // ----------------------------------------------------------
    // secondary functions: all called directly from draw()  
    
    void intro()
    {
      //  background(0);
    
      fill(#1203FF);
      textSize(50);
      text("Welcome to the", width/2 -350, height/2 -100);
    
      fill(#1203FF);
      textSize(50);
      text("game of Connect Four", width/2 -300, height/2 );
    
      ConnectFourBoard();
    }
    
    void game() 
    {
      background(0);
      scoreboard();
      fill(#F9FA03);
      rect(660, 160, 560, 480);
      for (int a = 0; a < cols; a++) {
        for (int b = 0; b < rows; b++) {
          fill(255);
          ellipseGrid[a][b].display();
        }
      }
    
      indicator();
    
      textSize(40);  
      text("HI  =  "+ colCount, 500, 600);
    }
    
    // ----------------------------------------------------------
    // Input functions 
    
    void keyPressed()
    {
      switch(state) {
    
      case intro:
        state=game;
        break;
    
      case game:
        keyPressedForStateGame(); 
        break;
    
      default:
        println ("Error  112: unknown state !!!!!!!!!!!!!!!!!!!!!!!!");
        exit();
        break;
      } //switch
    } //func 
    
    void keyPressedForStateGame() {
      if ((keyCode == LEFT) && (colCount > 1))
        colCount --;
    
      else if ((keyCode == RIGHT) && (colCount <7))
        colCount++;
    
      else if ((keyCode == DOWN) && (colCount <= 7 && colCount >= 1))
        dropAChip();
    }// func 
    
    // ------------------------------------------------------------
    // Minor functions 
    
    void dropAChip() {
      // we move UP in the column colCount-1 
      // and when we find an empty slot, we place a chip.
      // to do: different player colors
      for (int b = rows-1; b >= 0; b--) {
        // if slot is empty: 
        if (ellipseGrid[colCount-1][b].isEmpty()) {
          // place a chip: 
          ellipseGrid[colCount-1][b].setChip();
          break; // leave for-loop
        }//if
      }//for
    }//func 
    
    void colUpdate22222222()
      // ??????????????????????????????
    {
      for (int a = 0; a<cols; a++) {
        for (int b = 0; b<rows; b++) {
          if (colCount == 1)
          {
            //
          }
        }
      }
    }
    
    void indicator()
    {
      // Arrow
    
      // white
      fill(255); 
    
      if (colCount == 1)
      {
        triangle(700, 150, 690, 110, 710, 110);
      }
      if (colCount == 2)
      {
        triangle(780, 150, 770, 110, 790, 110);
      }
      if (colCount == 3)
      {
        triangle(860, 150, 850, 110, 870, 110);
      }
      if (colCount == 4)
      {
        triangle(940, 150, 930, 110, 950, 110);
      }
      if (colCount == 5)
      {
        triangle(1020, 150, 1010, 110, 1030, 110);
      }
      if (colCount == 6)
      {
        triangle(1100, 150, 1090, 110, 1110, 110);
      }
      if (colCount == 7)
      {
        triangle(1180, 150, 1170, 110, 1190, 110);
      }
    }
    
    void scoreboard()
    {
      fill(#F50008);
      rect(0, 0, 1600, 400); 
    
      fill(#0005F5);
      rect(0, 400, 1600, 400);
    
      fill(255);
      textSize(53);
      text("Player 1 score:"+ redScore, 50, 200); 
    
      text("Your Turn", 50, 300);
    
      text("Player 2 score :"+ blueScore, 50, 600);
    
      textSize(15);
      text("press z to reset score"
        +"\ncursor L/R to move arrow"
        +"\ncursor DOWN to drop chip", 
        1400, 100);
    
      //playeroneBoard();  
      //playertwoBoard();
    }
    
    void ConnectFourBoard() {    
      // This is the image that is used in the intro screen of the connect four board
    
      fill(#F6FF03);
      rect(width/2-100, height/2+100, 220, 200); // rectangle for board
    
      for (int i=1; i<rows; i++) {
        for (int j=1; j<cols; j++) {
          fill(255);
          ellipse(688+(i*(210/cols)), 490+(j*(250/rows)), 30, 30);
        }
      }
    }
    
    // ===========================================
    
    class Cell {
    
      // A cell object knows about its location in the grid 
      // as well as its size with the variables x,y,w,h
    
      float x, y;   // x,y location
      float w, h;   // width and height
      color currentcolor;
    
      // state of the field: 
      final int EMPTY  = 0;
      final int RED    = 1;
      final int YELLOW = 2;
      int filled = EMPTY; // current filling 
    
      // Cell Constructor
      Cell(float circleX, float circleY, 
        float circleW, float circleH, 
        color icolor) {
        x = circleX;
        y = circleY;
        w = circleW;
        h = circleH;
        currentcolor = icolor;
      } 
    
      void display() {
        stroke(255);
    
        // decide color from field state
        switch (filled) {
    
        case EMPTY:
          fill(currentcolor);
          break; 
    
        case RED:
          fill(red); 
          break; 
    
        case YELLOW:
          fill(blue);
          break;
        }//switch 
    
        ellipse(x, y, w, h);
      }
    
      boolean isEmpty() {
        // knows if the field is empty 
        // returns true or false: 
        return filled==EMPTY;
      }
    
      void setChip() {
        // fill the field
        // to do: fill it with the color of the player 
        // that makes the current move (RED or YELLOW)
        // if (filled==EMPTY)  
        filled = RED;
      }
    }//class
    //
    
  • thanks chrissir, thats exactly what i was struggling to do

  • edited December 2016

    sure! You are welcome!

  • your check win doesn't work...
    
    
    and you need to call it as well from draw(); 
    
    
    
    
        boolean checkWin()
        {
    
          //horizontal
    
          for (int a=0; a < rows; a++) {
            for (int b=0; b < cols-3; b++) {
              if (ellipseGrid[b][a].filled>0) {
                int tCheck = (ellipseGrid[b][a].filled)
                  + (ellipseGrid[b+1][a].filled)
                  + (ellipseGrid[b+2][a].filled) 
                  + (ellipseGrid[b+3][a].filled);
    
                boolean theyAreTheSame=false;
                if ( (ellipseGrid[b][a].filled) == (ellipseGrid[b+1][a].filled) &&  
                  (ellipseGrid[b+1][a].filled) == (ellipseGrid[b+2][a].filled) &&  
                  (ellipseGrid[b+2][a].filled) == (ellipseGrid[b+3][a].filled) )
                  theyAreTheSame=true; 
    
                if (tCheck == 8 || tCheck == 4)
                {
                  if (theyAreTheSame) {
                    winner = tCheck;
                    println ("YES");
                    return true;
                  }
                }
              }//if
            }//for
          }// for 
          return false;
        }
    
  • hey another question if thats possible for anyone. For determinning the win between two players. For horizontal, vertical and diagonal i have an idea of how i want to do it.

      boolean checkWin()
      {
    
    
        //horizontal
        for (int a=0; a < rows; a++) {
          for (int b=0; b < cols-3; b++) {
            int tCheck = (ellipseGrid[b][a]) + (ellipseGrid[b+1][a]) + (ellipseGrid[b+2][a]) + (ellipseGrid[b+3][a]);
            if (tCheck == 8 || tCheck == 4)
            {
              winner = tCheck;
              return true;
            }
          }
        }
    

    This however does now work with errors and was curious how you would go about it. The program as a whole is posted below without the boolean for checking the win of each player

    // the state of the program dictates whether 
    // we show the intro or the game 
    
    // we name the values state can have: 
    final int intro =0;
    final int game  =1; 
    int state = intro;
    
    // -----
    
    int redScore= 0;
    int blueScore= 0;
    
    int rows = 6;    //
    int cols =7;     //
    
    int turn= 1;  // whose turn is it
    int winner = 1;
    
    Cell[][] ellipseGrid = new Cell[cols][rows];
    
    // This is not in use and is part of the class Cell now!!!!!
    // int[][] gridChecker = new int[cols][rows];
    
    // signifies your arrow for input pos
    int colCount = 4;
    
    
    
    color red = color (255, 0, 0); //  color(#FF0505);
    color blue = color(#1203FF);
    
    // ----------------------------------------------------------
    // primary functions 
    
    void setup()
    {
      size(1600, 800);
    
      for (int a = 0; a<cols; a++) {
        for (int b = 0; b<rows; b++) {
          ellipseGrid[a][b] = new Cell(700+a*80, 200+b*80, 
            45, 45, 
            color(255));
        }
      }
    }
    
    void draw()
    {
      switch(state) {
    
      case intro:
        intro();
        break;
    
      case game:
        game(); 
        break;
    
      default:
        println ("Error  58: unknown state !!!!!!!!!!!!!!!!!!!!!!!!");
        exit();
        break;
      }//switch
    } // func 
    
    // ----------------------------------------------------------
    // secondary functions: all called directly from draw()  
    
    void intro()
    {
        background(0);
    
      fill(#1203FF);
      textSize(50);
      text("Welcome to the", width/2 -350, height/2-300);
    
      fill(#1203FF);
      textSize(50);
      text("game of Connect Four", width/2 -300, height/2-200);
     text("By Matthew Zadery", width/2 -300, height/2+240);
      ConnectFourBoard();
    }
    
    void game() 
    {
      background(0);
      scoreboard();
      fill(#F9FA03);
      rect(660, 160, 560, 480);
      for (int a = 0; a < cols; a++) {
        for (int b = 0; b < rows; b++) {
          fill(255);
          ellipseGrid[a][b].display();
        }
      }
    
      indicator();
     playerTurn();
    
    }
    
    // ----------------------------------------------------------
    // Input functions 
    
    void keyPressed()
    {
      switch(state) {
    
      case intro:
        state=game;
        break;
    
      case game:
        keyPressedForStateGame(); 
        break;
    
      default:
        println ("Error  112: unknown state !!!!!!!!!!!!!!!!!!!!!!!!");
        exit();
        break;
      } //switch
    } //func 
    
    void keyPressedForStateGame() {
      if ((keyCode == LEFT) && (colCount > 1))
        colCount --;
    
      else if ((keyCode == RIGHT) && (colCount <7))
        colCount++;
    
      else if ((keyCode == DOWN) && (colCount <= 7 && colCount >= 1))
    {
        dropAChip();
        turn++;
    }   
    }// func 
    
    // ------------------------------------------------------------
    // Minor functions 
    
    void dropAChip() {
      // we move UP in the column colCount-1 
      // and when we find an empty slot, we place a chip.
      // to do: different player colors
      for (int b = rows-1; b >= 0; b--) {
        // if slot is empty: 
        if (ellipseGrid[colCount-1][b].isEmpty()) {
          // place a chip: 
          ellipseGrid[colCount-1][b].setChip();
          break; // leave for-loop
        }//if
      }//for
    }//func 
    
    
    void indicator()
    {
      // Arrow
    
      // white
      fill(255); 
    
      if (colCount == 1)
      {
        triangle(700, 150, 690, 110, 710, 110);
      }
      if (colCount == 2)
      {
        triangle(780, 150, 770, 110, 790, 110);
      }
      if (colCount == 3)
      {
        triangle(860, 150, 850, 110, 870, 110);
      }
      if (colCount == 4)
      {
        triangle(940, 150, 930, 110, 950, 110);
      }
      if (colCount == 5)
      {
        triangle(1020, 150, 1010, 110, 1030, 110);
      }
      if (colCount == 6)
      {
        triangle(1100, 150, 1090, 110, 1110, 110);
      }
      if (colCount == 7)
      {
        triangle(1180, 150, 1170, 110, 1190, 110);
      }
    }
    
    void scoreboard()
    {
      fill(#F50008);
      rect(0, 0, 1600, 400); 
    
      fill(#0005F5);
      rect(0, 400, 1600, 400);
    
      fill(255);
      textSize(53);
      text("Player 1 score:"+ redScore, 50, 200); 
    
     if(turn ==1)
      text("Your Turn", 50, 300);
    
     if(turn ==2)
     text("Your Turn", 50,500);
    
      text("Player 2 score :"+ blueScore, 50, 600);
    
      textSize(15);
      text("press z to reset score"
        +"\ncursor L/R to move arrow"
        +"\ncursor DOWN to drop chip", 
        1400, 100);
    
      //playeroneBoard();  
      //playertwoBoard();
    }
    
    void ConnectFourBoard() {    
      // This is the image that is used in the intro screen of the connect four board
    
      fill(#F6FF03);
      rect(665, 263, 300, 300); // rectangle for board
    
      for (int i=0; i<7; i++) {
        for (int j=0; j<6; j++) {
          fill(255);
          ellipse(688+(i*(300/cols)), 283+(j*(300/rows)), 30, 30);
        }
      }
    }
    
    // ===========================================
    
    class Cell {
    
      // A cell object knows about its location in the grid 
      // as well as its size with the variables x,y,w,h
    
      float x, y;   // x,y location
      float w, h;   // width and height
      color currentcolor;
    
      // state of the field: 
      final int EMPTY  = 0;
      final int RED    = 1;
      final int YELLOW = 2;
      int filled = EMPTY; // current filling 
    
      // Cell Constructor
      Cell(float circleX, float circleY, 
        float circleW, float circleH, 
        color icolor) {
        x = circleX;
        y = circleY;
        w = circleW;
        h = circleH;
        currentcolor = icolor;
      } 
    
      void display() {
        stroke(255);
    
        // decide color from field state
        switch (filled) {
    
        case EMPTY:
          fill(currentcolor);
          break; 
    
        case RED:
          fill(red); 
          break; 
    
        case YELLOW:
          fill(#2013ED);
          break;
        }//switch 
    
        ellipse(x, y, w, h);
      }
    
      boolean isEmpty() {
        // knows if the field is empty 
        // returns true or false: 
        return filled==EMPTY;
      }
    
      void setChip() {
        // fill the field
        // to do: fill it with the color of the player 
        // that makes the current move (RED or YELLOW)
        // if (filled==EMPTY)  
        if (turn ==1)
        filled = RED;
        else if(turn ==2)
        filled = YELLOW;
      }
    }//class
    //
    
    void playerTurn()
    {
      if(turn > 2)
      turn = 1;
     }
    
  • I already answered this with an version of your function

    just repeat for

    • vertical,

    • for diagonal / and

    • for diagonal \

  • edited December 2016
       l
    
Sign In or Register to comment.