Data save and call

edited October 2017 in How To...

i am making a jeopardy game and i want to make a code that creates the categories and the questions, save them, and then open them in the jeopardy game. is this possible? If so, how?

Tagged:

Answers

  • edited October 2017 Answer ✓

    Save and load

    look at saveTable and loadTable OR

    saveStrings and loadStrings

    https://www.processing.org/reference/

    to use different sketches on the same files (like one sketch is creating them and the other sketch is loading them) store in a separate folder and use absolute paths.

    Display text in a grid

    Then use text() to display it in a grid. Use a double for-loop

    How to choose the Categories

    The harder part is to choose from lots of categories randomly and then choose questions randomly that fit the category. It’s probably easy when you have just 17 categories and just the fixed number of questions for each category.

  • If you search the reference for the word "save" as @Chrisir suggests:

    ...you will also see that your save/load options include a file of strings, a csv Table, JSON, and XML. If you are not already familiar with JSON then it sounds like Table might work best for you -- it is fairly simple and also flexible.

    One approach is to have a list of six categories, and a table of questions, with the category (1-6) and the price (1-5) each listed in a table column next to each question. Then you can search the table by category/price to fill slots in your grid with specific questions.

  • thanks, I'm stupid, didn't look too well apparently

  • Don't worry -- let us know in this thread if you have follow-up questions after you try working with these functions.

  • Answer ✓

    still some errors in it but I show you a simple grid

    // source data
    String[] sourceData = {"City,Population,Area", 
      "Tokyo-Yokohama,37239000,8547", 
      "Jakarta (Jabotabek),26746000,2784", 
      "Seoul-Incheon,22868000,2163", 
      "Delhi DL-HR-UP,22826000,1943", 
      "Shanghai SHG,21766000,3497", 
      "Manila,21241000,1437", 
      "Karachi,20877000,803", 
      "New York NY-NJ-CT,20673000,11642", 
      "Sao Paulo,20568000,3173", 
      "Mexico City,20032000,2046"}; 
    
    // table where the data gets moved into 
    Table table;
    
    // headlines of table (from sourceData) 
    String[] firstLine; 
    
    // -----------------------------------------------------------
    
    void setup() {
    
      size(1200, 675); //size of screen
    
      // Font 
      PFont myFont=createFont("Arial", 12);
      textFont(myFont);
    
      // ----
      // get data into table 
    
      table = new Table(); // or use loadTable on csv 
    
      // headlines!
      firstLine = split(sourceData[0].trim(), ',');
    
      // columns from headlines 
      for (int i = 0; i < firstLine.length; i++) {
        table.addColumn(firstLine[i].trim());
      }//for 
    
      // rows from sourceData
      for (int i = 1; i < sourceData.length; i++) {
        println(sourceData[i]);
    
        // split one line from sourceData
        String[] dataLine=split(sourceData[i].trim(), ',');
    
        println(dataLine[1]);
        println(dataLine[2]);
    
        // put into row 
        TableRow newRow = table.addRow();
        newRow.setString(firstLine[0], dataLine[0] );
        newRow.setInt(firstLine[1], int(dataLine[1]));
        newRow.setInt(firstLine[2], int(dataLine[2]));
      }//for
      //
    }//function 
    
    void draw() {   
    
      background(0); 
    
      // calc the width of one column 
      //  float factor = (width - 25) / table.getRowCount();  
    
      // headline in green -----------------
      fill(0, 255, 0); // green 
      int i2 = 0; 
      text (firstLine[0], 22, i2*34+44); 
      text (firstLine[1], 222, i2*34+44); 
      text (firstLine[2], 333, i2*34+44);
    
      stroke(0, 255, 0); // green 
      line(0, i2*34+44+8, width, i2*34+44+8); 
    
    
      // rows in white ----------------------------
      for (int i = 0; i < table.getRowCount(); i++) {
    
        TableRow thisLine = table.getRow(i);
    
        int lado = (thisLine.getInt("Area"));
    
        fill(255);
        text (thisLine.getString("City"), 22, i*34+77); 
        text (lado, 222, i*34+77); 
        text (thisLine.getInt("Population"), 333, i*34+77); 
    
        //When the mouse hovers over a line the name of the city appears.
        if (mouseY > i*34+77-13 && 
          mouseY < i*34+77+13) {
    
          // draw a box
          float dist = 103;
          stroke(111);       //   
          fill(253, 248, 22); // yellow 
          rect (300+dist, 
            i*34+77-60+dist, 
            textWidth(thisLine.getString("City"))+10, 
            32, 
            9);
          noStroke(); 
    
          // draw text 
          fill(0); 
          text(thisLine.getString("City"), 300+dist+3, 
            i*34+77-60+dist + 18 );
        } // if
      }//for
      //
    } // draw 
    //
    
Sign In or Register to comment.