Any improvements anyone can see that i can't?

//Jeopardy, by Isaiah Johnson.
PFont digits;//font
Cell[] grid; //makes each individual cells
int cols; //how many colloms
int rows; // how many rows
final int stateGameBoard=0;
final int stateQuestion=1;
final int winner=2;
final int double_jeopardy=3;
int state= stateGameBoard; // makes the dislplay
final int team1=0;
final int team2=1;
int team=team1;// the 2 teams
int numA=0;// number of questions answered
int p;// the cell that was clicked
int x=0; // where on the x axis you are 
int y=0;// where on the y axis you are
int attempt=1; // what atempt you are on
int possScore=0;// the possible score you are able to resive
int t1Score=0;// team 1 score
int t2Score=0;// team 2 score
int ans=2;// what the app needs to change the attempt
int arrow=1;// my arrow/ text color change
int convert(int row, int col) {//converter
  return (col*rows)+row;
}
int norm=(height+width)/8;// size of words normaly
int max=0;//max of double jeopardy
int number=0;// what you wager in double jeopardy

void setup() {
  fullScreen(P2D);
  loadData();
  orientation(LANDSCAPE);
  textAlign(CENTER);
} 

void draw () {
  numA=0;
  for (int i = 0; i < cols*rows; i++) { 
    grid[i].check();
  }
  if (numA==(cols*rows)) {
    state=winner;
  }
  background(128);
  fill(6, 12, 233);  
  rect(45, 45, width-90, height-90);
  switch(state) {

  case stateGameBoard:
    background(128, 128, 128); 
    fill(0);
    gameboard();
    textSize(norm);
    switch(team) {
    case team1:
      fill(255, 213, 0);  
      text("team 1= "+t1Score, (0*((width-100)/5))+(width/8), 25);
      fill(100); 
      text("team 2= "+t2Score, (4*((width-100)/5))+(width/8), 25);
      break;
    case team2:
      fill(100); 
      text("team 1= "+t1Score, (0*((width-100)/5))+(width/8), 25);
      fill(255, 213, 0);
      text("team 2= "+t2Score, (4*((width-100)/5))+(width/8), 25);
      break;
    }
    break;

  case double_jeopardy:
    textSize(norm);
    switch(team) {
    case team1:
      fill(255, 213, 0);  
      text("team 1= "+t1Score, (0*((width-100)/5))+(width/8), 25);
      max=t1Score;
      fill(100); 
      text("team 2= "+t2Score, (4*((width-100)/5))+(width/8), 25);
      break;
    case team2:
      fill(100); 
      text("team 1= "+t1Score, (0*((width-100)/5))+(width/8), 25);
      fill(255, 213, 0);
      text("team 2= "+t2Score, (4*((width-100)/5))+(width/8), 25);
      max=t2Score;
      break;
    }
    if(max==0){
      max=1000;
    }
    fill(255, 213, 0);
    textAlign(CENTER);
    textSize(50);
    text("You have receved Double Jeopardy!", width/2, (height/2)-50);
    text("How much are you willing to wager?", width/2, (height/2)+50);
    if(number>=max){
      number=max;
    }
    text(number, width/2, (height/2)+100);
    break;

  case stateQuestion: 
    background(128, 128, 128); 
    fill(0);
    Question();
    textSize(norm);
    switch(team) {
    case team1:
      fill(255, 213, 0);  
      text("team 1= "+t1Score, (0*((width-100)/5))+(width/8), 25);
      fill(100, 100, 100); 
      text("team 2= "+t2Score, (4*((width-100)/5))+(width/8), 25);
      break;
    case team2:
      fill(100, 100, 100); 
      text("team 1= "+t1Score, (0*((width-100)/5))+(width/8), 25);
      fill(255, 213, 0);
      text("team 2= "+t2Score, (4*((width-100)/5))+(width/8), 25);
      break;
    }
    break;

  case winner:
    fill(255, 213, 0);
    textSize(25);
    textAlign(CENTER);
    text(win()+" Wins!!!!!!!!!", width/2, height/2);
    break;
  }
}

//-----------------------------------------------------------------------------------------------------------

void loadData() {
  String[] Cat;
  String[] Questions;

  Cat=loadStrings("categories.txt");
  cols = Cat.length;  

  Questions=loadStrings("questions.txt");
  int QuestionsNumberOfCol = split(Questions[0], '^').length;

  // validate: categories against questions
  if (cols != QuestionsNumberOfCol) {
    println ("Number of Categories does not match the of columns of the questions");
    exit();
    return;
  } 

  // preparation: 
  // distribute questions on grid of questions 
  // QuestionsGrid is a 2D grid, but the first index is Y, then X
  String[][]QuestionsGrid = new String [ Questions.length ] [ QuestionsNumberOfCol ]  ;
  // loop over lines 
  for (int i = 0; i < Questions.length; i++) { 
    QuestionsGrid[i] = split(Questions[i], '^');
    println ("Line "+i+": ");
    printArray(QuestionsGrid[i]);
    // validate: questions must be the same numbers in every row
    if ( QuestionsGrid[i].length != QuestionsNumberOfCol ) {
      println ("\n\nNumber of columns of the questions is not always the same: Line: "+i+". Program terminates. #########");
      exit();
      return;
    }
  }
  rows =  Questions.length+1; 
  grid = new Cell[cols*rows];
  int k=0;
  int dub=int(random(cols*rows));
  for (int i = 0; i < cols; i++) { // X
    for (int j = 0; j < rows; j++) {  // Y
      if (j==0) {
        if (dub==k) {
          dub++;
        }
        // we enter a category into the grid
        grid[k] = new Cell((i*((width-100)/cols))+55, (j*((height-100)/rows))+55, (width/cols)-30, (height/rows)-30, k, Cat[i], "", true, false);
      } else {
        // we enter a Question into the grid (swappng x and y for QuestionsGrid since QuestionsGrid is [y][x])
        if (k==dub) {
          grid[k] = new Cell((i*((width-100)/cols))+55, (j*((height-100)/rows))+55, (width/cols)-30, (height/rows)-30, k, " "+(j*100)+" ", QuestionsGrid[j-1] [i], false, true );
        } else {
          grid[k] = new Cell((i*((width-100)/cols))+55, (j*((height-100)/rows))+55, (width/cols)-30, (height/rows)-30, k, " "+(j*100)+" ", QuestionsGrid[j-1] [i], false, false );
        }
      }
      k++;
    }
  }
}

String win() {
  if (t1Score>t2Score) {
    return "Team 1";
  } else {
    return "Team 2";
  }
}

//-------------------------------------------------------------------------------------------------------------------------------------------------------------

void mouseReleased() {

  // depending on state 
  switch (state) { 

  case stateGameBoard:
    //moves tiles when mouse is pressed (released)
    int row = (mouseY-55)/((height-100)/rows);
    int col = (mouseX-50)/((width-100)/cols);
    p=convert(row, col);
    Cell currentCell=grid[p];

    if (!currentCell.hasBeenClicked && !currentCell.isCategory&&!currentCell.isDouble) {
      x=col;
      y=row;
      state=stateQuestion;
    } else {
      if (!currentCell.hasBeenClicked && !currentCell.isCategory && currentCell.isDouble) {
        x=col;
        y=row;
        state=double_jeopardy;
      }
    }

    break;
  } //switch 
  //
} //func 

void keyReleased() {
  // depending on state 
  switch (state) { 
  case stateQuestion:
    if (key=='y'||key=='Y') {
      ans+=1;
    } else if (key=='n'||key=='N') {
      ans-=1;
    }
    break;

  case double_jeopardy:
    if (key==CODED) {
      if (keyCode== UP) {
        if (keyCode==SHIFT) {
          number=max;
        }else{
          number+=100;
        }
      }
      if(keyCode==DOWN){
        if(keyCode==SHIFT){
          number=max;
        }else{
        number-=100;
        }
      }
    }
    if(keyCode==ENTER){
      state=stateQuestion;
    }

  case stateGameBoard:
    if (key=='r'||key=='R') {
      int rando=int(random(cols*rows));
      t1Score=0;
      t2Score=0;
      team=team1;
      for (int i = 0; i < cols*rows; i++) { 
        if (grid[i].isCategory) {
          rando+=1;
        }
        grid[i].hasBeenClicked=false;
        if (i==rando&&!grid[i].isCategory) {
          grid[i].isDouble=true;
          println(i);
        } else {
          grid[i].isDouble=false;
        }
      }
      state=stateGameBoard;
    }  // if...else if...
    break;
  } //switch 
  //
} //func 

//----------------------------------------------------------------------------------------------------------------------------

void gameboard() {
  rect(45, 45, width-90, height-90);
  for (int i = 0; i < cols*rows; i++) { 
    grid[i].display();
  }
}

void Question() {
  fill(6, 12, 233);
  rect(45, 45, width-90, height-90);
  fill(255, 213, 0);
  if (grid[convert(y, x)].isDouble) {
    possScore=number;
  }else{
    possScore=y*100;
  }
  textSize(grid[convert(y, x)].question.length()*5);
  text(grid[convert(y, x)].question, width/2, height/2);
  score();
}

void score() {
  switch(team) {
  case team1:

    if (attempt==1) {
      if (ans==3) {
        t1Score+=possScore;
        possScore=0;
        attempt=1;
        team=team2;
        state=stateGameBoard;
        grid[p].hasBeenClicked=true;
        ans=2;
        x=0;
        y=0;
      }
      if (ans==1) {
        attempt=2;
        team=team2;
        state=stateQuestion;
      }
    }
    if (attempt==2) {
      if (ans==2) {
        t1Score+=possScore;
        possScore=0;
        attempt=1;
        state=stateGameBoard;
        grid[p].hasBeenClicked=true;
        ans=2;
        x=0;
        y=0;
      }
      if (ans==0) {
        possScore=0;
        attempt=1;
        ans=2;
        state=stateGameBoard;
        x=0;
        y=0;
      }
    }
    break;

  case team2:
    if (attempt==1) {
      if (ans==3) {
        t2Score+=possScore;
        possScore=0;
        attempt=1;
        team=team1;
        state=stateGameBoard;
        grid[p].hasBeenClicked=true;
        ans=2;
        x=0;
        y=0;
      }
      if (ans==1) {
        attempt=2;
        team=team1;
        state=stateQuestion;
      }
    }
    if (attempt==2) {
      if (ans==2) {
        t2Score+=possScore;
        possScore=0;
        attempt=1;
        ans=2;
        state=stateGameBoard;
        grid[p].hasBeenClicked=true;
        x=0;
        y=0;
      }
      if (ans==0) {
        possScore=0;
        attempt=1;
        state=stateGameBoard;
        ans=2;
        x=0;
        y=0;
      }
    }
    break;
  }
}

//-------------------------------------------------------------------------------------------------------------------------------

class Cell { 
  float x, y; // x,y location
  float w, h; // width and height 
  int tag;
  String text; // text for cell is closed
  String question="";// flags
  boolean isCategory=false; 
  boolean isDouble=false;
  boolean hasBeenClicked=false; // Cell Constructor
  Cell(float tempX, float tempY, float tempW, float tempH, int tempTag, String num, String tempQuestion, boolean tempisCategory, boolean tempisDouble) { 
    x = tempX; 
    y = tempY; 
    w = tempW; 
    h = tempH;
    tag= tempTag;
    text=num;
    question =  tempQuestion;
    isCategory=tempisCategory;
    isDouble=tempisDouble;
  }

  void check() {
    if (hasBeenClicked||isCategory) {
      numA+=1;
    }
  }

  void display() { 
    fill(6, 12, 233);
    stroke(0);
    rect(x, y, w, h);
    fill(255, 213, 0);
    textAlign(CENTER);
    textSize((width/(cols*rows))/2);
    if (!hasBeenClicked) {
      text(text, x+w/2, y+h/2);
      if (isDouble) {
        text("Double", x+w/2, (y+20)+h/2);
      }
    }
    //}
  }
}

Answers

  • There is a new forum

    Please ask there

  • Your draw() is too long

    Try to make 3-4 main functions so draw has only 4-8 lines

    You only have one class - try more

  • Answer ✓

    Global variables:

    You want to have empty lines where a new section of variables starts (eg. have one Block of state related constants and variables)

    There is a function convert in the variables section- doesn’t belong here

    width and height shouldn’t work in this place - better use in setup (see line 27)

    Convention: constants are written IN_CAPITALS

  • what is the new form? thanks for the advice. how would you suggest to use more classes?

Sign In or Register to comment.