Anyone have any Improvements?


#1
//Jeopardy, by Isaiah Johnson.
PFont digits;//font
Cell[] grid; //makes each individual cells
int cols; //how many colloms
int rows; // how many rows
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
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

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);
  view();
}

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

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 view() {
  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 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);
      }
    }
    //}
  }
}


#2

Can you explain what this code does? Do you have a specific question?

One thing that jumps out to me is that you should use standard naming conventions. Variables and function names should start with a lower-case letter. Classes should start with an upper-case letter.


#3

I name as I go, been working on it. the code is a game written in java to play Jeopardy.


#4

cant really test it without your other required files like
categories.txt
questions.txt
etc


#5

While we’re on the subject, I’d also encourage you to use more descriptive variable names. Names like p and number don’t really tell you much. Don’t be afraid to use longer variable names.

But other than that, if you want feedback on the design, I’d recommend putting together a design doc that explains your design. Use diagrams or flowcharts to help explain it. I don’t see any classes, which would probably help you encapsulate your code a little better.

But in the end, I wouldn’t worry too much about it. Does your code work? Do you understand it? If so, cool. Move on to the next thing.


#6

Categories.txt has:
Category 1
Category 2
Category 3
Category 4
Category 5


#7

questions.txt is a little harder:
question 1^question 6^question 11^question 16^question 21
question 2^question 7^question 12^question 17^question 22
question 3^question 8^question 13^question 18^question 23
question 4^question 9^question 14^question 19^question 24
question 5^question 10^question 15^question 20^question 25


#8

i am trying to make it work better/ faster


#9

Can you try to be more specific? What are you trying to improve? Which part is slow? How are you measuring performance?

One of the most challenging things about programming is that you need to be specific, not just when writing code but also when talking about it. It’s hard for us to help if you don’t point out the specific parts you’re having trouble with.

I’d recommend reading through this guide: Guidelines—Tips on Asking Questions

Can you try to break your problem down into smaller steps? Can you isolate the problematic behavior in a smaller test program?


#10

there realy isn’t a problem. i just put it out for some constructive criticism.


#11

Hi Godemperor949,

It is quite hard to understand the code of someone else specially with just the code and no documentation… You had you own logic when writing it and it might be completely different than the logic of someone else trying to do the same game.

Sometimes it is already hard to remember what you wanted to do when you get back to your own piece of code so imagine doing it for a code you’ve never seen before!

All that to say that I doubt you’ll get any constructive criticism except general one like @Kevin did already with naming convention and the use of classes. On the non technical aspect you can also get criticism on the gameplay itself.

As @Kevin said again, don’t bother, if it works the way you want then it is good, move on to another project. At some point you’ll use the same technics you where using and find out that there are some limits and it was not the best design. In this case you’ll think of a better solution, apply it, and that’s how you’ll improve.


#12

thanks for your input


#13

@Godemperor949 – you might want to consider adding a simple JavaDoc-style comment to the top of every function, class, and class method, like what you find in the Processing source code.

/**
 * A cell represents one of those blue televisions
 * that contains a single question. It manages the
 * question text, whether it has been etc. etc. etc.
 */ 
class Cell {

If your game works, great. From an OO perspective, I think it would be worth refactoring if you are considering other related games. For example – ones not based no a rectangular grid board.

Some things are also surprising if you are not the author of the code and have no comments. For example, I might expect question to be a method of the Cell class, or to take an explicit argument. It does neither, so it isn’t immediately obvious what a global function called “Question” does when called.