Numbering A Crossword Java ACM Graphics -


the problem asks acm graphics program reads txt file this:

      r            fun          sales        receipt      mere@farm    dove@@@rail  more@@@@@draw  hard@@@tied    lion@sand      evening        evade                     d       

and makes crossword puzzle, blank squares on letters, black squares on '@', , nothing on empty spaces. problem asks "if square @ beginning of word running across, down, or both, square should contain number assigned sequentially through puzzle."

i have square drawing working, i'm stuck on drawing numbers correctly. there wrong how i'm detecting null space , black squares. can tell me i'm doing wrong, please?

here code:

import acm.program.*; import java.io.*; import java.util.*; import acm.graphics.*; import java.awt.*;  public class crossword extends graphicsprogram { public void run() {     string filename = "crosswordfile.txt";     makecrosswordpuzzle(filename); } private static final int sqcon = 15; // constant square x , y dimensions private int y = 0; public void makecrosswordpuzzle(string filename) {     bufferedreader rd;     int y = 0; // y value square being added during loop. increments sqcon after every line     int wordnumber = 1; // variable numbers added boxes. increments every time program adds number     try {         rd = new bufferedreader(new filereader(filename));         string line = rd.readline(); //reads 1 line of text document @ time , makes string          while (line != null) {             int x = 0;               (int = 0; < line.length(); i++) {                  char linechar = line.charat(i);// character being examined each loop                  grect whitesq = new grect(sqcon,sqcon); //grect blank squares                  grect blacksq = new grect(sqcon,sqcon);//grect black squares                 blacksq.setfilled(true);                 blacksq.setfillcolor(color.black);                  if (linechar == '@'){                     add (blacksq,x,y);                  }                 if (character.isletter(linechar)) {                      add (whitesq, x, y);                      // if element above or left of current focus null or blacksq, place number , increment wordnumber                     gobject above = getelementat(x+sqcon/2,y-sqcon/2);                     gobject left = getelementat(x-sqcon/2, y+sqcon/2);                      glabel wordnumberlabel = new glabel(integer.tostring(wordnumber));                     if (above == null || left == null || above == blacksq || left == blacksq) {                          add(wordnumberlabel,x,y+sqcon);                         wordnumber++;                     }                 }                 x += sqcon;             }             line = rd.readline();             y += sqcon;         }         rd.close();     }     catch (ioexception e) {         throw new errorexception(e);     } } 

}

edited add:

i copied code on eclipse , ran it. here's result.

crossword solution

you did fine on upper half, missed down numbers on lower half.

here's same code, reformatted it's easier read.

import java.awt.color; import java.io.bufferedreader; import java.io.filereader; import java.io.ioexception;  import acm.graphics.glabel; import acm.graphics.gobject; import acm.graphics.grect; import acm.program.graphicsprogram; import acm.util.errorexception;  public class crossword extends graphicsprogram {     private static final long   serialversionuid    = -7971434624427958742l;      public void run() { //      string filename = "crosswordfile.txt";         string filename = "c:/eclipse/eclipse-4.2-work/com.ggl.testing/crosswordfile.txt";         makecrosswordpuzzle(filename);     }      private static final int    sqcon   = 15;   // constant square x , y                                                 // dimensions     private int                 y       = 0;      public void makecrosswordpuzzle(string filename) {         bufferedreader rd;         int y = 0; // y value square being added during loop.                     // increments sqcon after every line         int wordnumber = 1; // variable numbers added boxes.                             // increments every time program adds number         try {             rd = new bufferedreader(new filereader(filename));             string line = rd.readline(); // reads 1 line of text document                                             // @ time , makes string              while (line != null) {                 int x = 0;                  (int = 0; < line.length(); i++) {                      char linechar = line.charat(i);// character being                                                     // examined each loop                      grect whitesq = new grect(sqcon, sqcon); // grect blank                                                                 // squares                      grect blacksq = new grect(sqcon, sqcon);// grect black                                                             // squares                     blacksq.setfilled(true);                     blacksq.setfillcolor(color.black);                      if (linechar == '@') {                         add(blacksq, x, y);                      }                     if (character.isletter(linechar)) {                          add(whitesq, x, y);                          // if element above or left of current                         // focus null or blacksq, place number ,                         // increment wordnumber                         gobject above = getelementat(x + sqcon / 2, y - sqcon                                 / 2);                         gobject left = getelementat(x - sqcon / 2, y + sqcon                                 / 2);                          glabel wordnumberlabel = new glabel(                                 integer.tostring(wordnumber));                         if (above == null || left == null || above == blacksq                                 || left == blacksq) {                              add(wordnumberlabel, x, y + sqcon);                             wordnumber++;                         }                     }                     x += sqcon;                 }                 line = rd.readline();                 y += sqcon;             }             rd.close();         } catch (ioexception e) {             throw new errorexception(e);         }     }  } 

i followed advice of own comment. created crossword puzzle answer, numbered crossword puzzle answer, , drew crossword puzzle answer.

here's applet result:

crossword answer

i kept list of crossword puzzle cells. way, determine length , width of puzzle number of characters on row , number of rows of input text file. didn't have hard code dimensions.

for each crossword cell, kept track of whether or not letter, , whether or not dark space.

when determining put numbers, followed 2 rules.

  1. an across number placed cell left of cell empty or dark, , there 3 or more letters across.

  2. a down number placed cell above cell empty or dark, there 3 or more letters down, , there no across number.

you can see in code had debug printing crossword puzzle clue numbering correct. broke process many methods keep each method simple possible.

finally, drew crossword puzzle answer information in list.

here's code:

import java.awt.color; import java.awt.point; import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.list;  import acm.graphics.glabel; import acm.graphics.grect; import acm.program.graphicsprogram; import acm.util.errorexception;  public class crossword extends graphicsprogram {     private static final boolean debug = false;      private static final long serialversionuid = -7971434624427958742l;      private list<crosswordcell> crosswordcelllist;      @override     public void run() {         this.crosswordcelllist = new arraylist<crosswordcell>(); //      string filename = "crosswordfile.txt";         string filename = "c:/eclipse/eclipse-4.2-work/" +                  "com.ggl.testing/crosswordfile.txt";         try {             readcrosswordanswer(filename);             if (debug) printcrosswordanswer();             numbercrosswordcells();             if (debug) printcrosswordanswer();             drawcrosswordanswer();         } catch (filenotfoundexception e) {             throw new errorexception(e);         } catch (ioexception e) {             throw new errorexception(e);         }     }      private void readcrosswordanswer(string filename)              throws filenotfoundexception, ioexception {         bufferedreader reader =                  new bufferedreader(new filereader(filename));         string line = "";         int row = 0;         while ((line = reader.readline()) != null) {             (int column = 0; column < line.length(); column++) {                 crosswordcell cell = new crosswordcell(column, row);                 char linechar = line.charat(column);                 if (linechar == '@') {                     cell.setdarkcell(true);                 } else if (character.isletter(linechar)) {                     cell.setletter(true);                 }                 crosswordcelllist.add(cell);             }             row++;         }         reader.close();     }      public void printcrosswordanswer() {         (crosswordcell cell : crosswordcelllist) {             system.out.println(cell);         }     }      private void numbercrosswordcells() {         int cluenumber = 1;         (crosswordcell cell : crosswordcelllist) {             if (cell.isletter()) {                 cluenumber = testcell(cell, cluenumber);             }         }     }      private int testcell(crosswordcell cell, int cluenumber) {         point p = cell.getlocation();         crosswordcell leftcell = getleftcell(p.x, p.y);         list<crosswordcell> acrosslist = getrightcells(p.x, p.y);         if (debug) {             system.out.print(p);             system.out.println(", " + leftcell + " " +                      acrosslist.size());         }         if ((leftcell == null) && (acrosslist.size() >= 3)) {             cell.setcluenumber(cluenumber++);         } else {             crosswordcell abovecell = getabovecell(p.x, p.y);             list<crosswordcell> downlist = getbelowcells(p.x, p.y);             if (debug) {                 system.out.print(p);                 system.out.println(", " + abovecell + " " +                          downlist.size());             }             if ((abovecell == null) && (downlist.size() >= 3)) {                 cell.setcluenumber(cluenumber++);             }         }         return cluenumber;     }      private crosswordcell getabovecell(int x, int y) {         int yy = y - 1;         return getcell(x, yy);     }      private crosswordcell getleftcell(int x, int y) {         int xx = x - 1;         return getcell(xx, y);     }      private list<crosswordcell> getbelowcells(int x, int y) {         list<crosswordcell> list = new arraylist<crosswordcell>();         (int = y; < (y + 3); i++) {             crosswordcell cell = getcell(x, i);             if (cell != null) {                 list.add(cell);             }         }         return list;     }      private list<crosswordcell> getrightcells(int x, int y) {         list<crosswordcell> list = new arraylist<crosswordcell>();         (int = x; < (x + 3); i++) {             crosswordcell cell = getcell(i, y);             if (cell != null) {                 list.add(cell);             }         }         return list;     }      private crosswordcell getcell(int x, int y) {         (crosswordcell cell : crosswordcelllist) {                   point p = cell.getlocation();             if ((p.x == x) && (p.y == y)) {                 if (cell.isdarkcell()) {                     return null;                 } else if (cell.isletter()){                     return cell;                 } else {                     return null;                 }             }         }         return null;     }      private void drawcrosswordanswer() {         int sqcon = 32;         (crosswordcell cell : crosswordcelllist) {             point p = cell.getlocation();             if (cell.isdarkcell()) {                 drawdarkcell(p, sqcon);             } else if (cell.isletter()) {                 drawlettercell(cell, p, sqcon);             }         }     }      private void drawdarkcell(point p, int sqcon) {         grect blacksq = new grect(sqcon, sqcon);         blacksq.setfilled(true);         blacksq.setfillcolor(color.black);         add(blacksq, p.x * sqcon, p.y * sqcon);     }      private void drawlettercell(crosswordcell cell, point p, int sqcon) {         grect whitesq = new grect(sqcon, sqcon);         add(whitesq, p.x * sqcon, p.y * sqcon);         if (cell.getcluenumber() > 0) {             string label = integer.tostring(cell.getcluenumber());             glabel wordnumberlabel = new glabel(label);             add(wordnumberlabel, p.x * sqcon + 2, p.y * sqcon + 14);         }     }      class crosswordcell {         private boolean darkcell;         private boolean isletter;          private int cluenumber;          private point location;          public crosswordcell(int x, int y) {             this.location = new point(x, y);             this.cluenumber = 0;             this.darkcell = false;             this.isletter = false;         }          public boolean isdarkcell() {             return darkcell;         }          public void setdarkcell(boolean darkcell) {             this.darkcell = darkcell;         }          public boolean isletter() {             return isletter;         }          public void setletter(boolean isletter) {             this.isletter = isletter;         }          public int getcluenumber() {             return cluenumber;         }          public void setcluenumber(int cluenumber) {             this.cluenumber = cluenumber;         }          public point getlocation() {             return location;         }          @override         public string tostring() {             stringbuilder builder = new stringbuilder();             builder.append("crosswordcell [location=");             builder.append(location);             builder.append(", cluenumber=");             builder.append(cluenumber);             builder.append(", darkcell=");             builder.append(darkcell);             builder.append(", isletter=");             builder.append(isletter);             builder.append("]");             return builder.tostring();         }      }  } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -