Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Fucking program...

Name: Anonymous 2006-09-24 22:50

Dear /prog/,
I'm wirting a program for my CS class which solves Hitori puzzles (not unlike sudoku, see http://en.wikipedia.org/wiki/Hitori for example.) Now, it seems like it should be a damn simple program to write in java, but I'm absolutely fucked when it comes to getting part of it to work. My program will get the puzzle down to one of each # per row/column, but I can't for the life of me get a properly working algorthm to ensure that all whitespace is connected. Please 4chan, would anyone be willing to help a brother out, by finding what's wrong with my code? I'll post it if anyone's willing.

Name: Anonymous 2006-09-24 22:53

FAILS for using JAVA

Name: Anonymous 2006-09-24 22:56

>2
Yeah, because, y'know, not like that's the first language my university teaches for it's BCSC degree. OH SHI-

Name: Anonymous 2006-09-24 23:34

What the hell? Just post the code already...

Name: Anonymous 2006-09-24 23:41 (sage)

Do your own homework, shitcock.

Name: Anonymous 2006-09-25 0:00

>5
Eat a fucking dick you whiny cunt, that's what I'm trying to do, I just need someone to help me debug this shit. Code forthcoming...

Name: Anonymous 2006-09-25 0:02

Here it is. Expect it to be illegible, and goddamn messy. I just need to know why it goes into a loop when it tries to check for continuity. Everything else omitted for berevity.
Starts here:

   public boolean solve(int location){
        int row = location / x;
        int col= location % x;
        //display();
        if (location == x * y) //at end of puzzle
           return true;
        else { //Try to make the location "white"
           puzzle[row][col].setWhite(); attempts++;
           if ((solve(location+1))&&(checkLocation(row, col))){
               return true;}
           else{ //Try to make the location "black"
              puzzle[row][col].setBlack(); attempts++;
              if (solve(location+1)&&(checkLocation(row, col))&&!breaksContinuity(location+1)){
                  return true;}
              else { //This location can not be white or black so set unknown and backtrack
                 puzzle[row][col].setUnknown();
                  return false;

              }
           }
        }
         }
    boolean checkLocation(int row, int col){
        int cellValue = puzzle[row][col].cellValue;
         int cvOccurances=0;
        for(int i=col; i<y; i++){
            if((puzzle[row][i].cellValue==cellValue)&&!(puzzle[row][i].isBlack()))
                cvOccurances++;
        }
        if(cvOccurances>1)
            return false;
        cvOccurances=0;

        for(int i=row; i<x; i++){
            if((puzzle[i][col].cellValue==cellValue)&&!(puzzle[i][col].isBlack()))
                cvOccurances++;
        }
        if(cvOccurances>1)
            return false;
        cvOccurances=0;
        return true;
   
    }
    boolean checkContinuity(int location){
        int row = location / x;
        int col= location % x;
        if (puzzle[row][col].isBlack()||puzzle[row][col].cellChecked)
            return false;
        countedSquares++;
        puzzle[row][col].cellChecked=true;
         if (countedSquares==whiteSquares){
             return true;
         }
         else if(location-1>=0&&checkContinuity(location-1)){
             return true;
         }
         else if(location+1<puzzleSize&&checkContinuity(location+1)){
             return true;
         }
         else if(location+x<puzzleSize&&checkContinuity(location+x)){
             return true;
         }
         else if(location-x>=0&&checkContinuity(location-x)){
             return true;
         }
         else
             return false;
        
            
       
   
    }
    boolean breaksContinuity(int location){
        int row = location / x;
        int col= location % x;
        int i;
        puzzle[row][col].setBlack();
        for(i=0;puzzle[i/x][i%x].isBlack(); i++);
        return((!checkContinuity(i))&&uncheckCells());
    }
    boolean uncheckCells(){
        for(int i=0; i<x;i++)
            for(int j=0; j<y;j++)
                puzzle[i][j].cellChecked=false;
        return true;
    }
   
}

Name: Anonymous 2006-09-25 1:30

else if(location-1>=0&&checkContinuity(location-1)){
I don't think it could cause an infinite loop, but the first test of all four of these ifs is wrong anyway; eg assume that location is the first square of the second row, loc-1>=0 holds, but there is no square to the left.

Also it might simplify things if you let checkCont return an int with the number of white squares in its part of the fill tree, instead of the boolean mess and global accumulator you have now.

Name: Anonymous 2006-09-25 5:11

Ok you have a problem in the way checkContinuity recurses - it calls itself in such a way that it doesn't terminate.

The breaksContinuity method calls checkContinuity(0) first of all (when i is 0). Upon reaching this:

if(location+1<puzzleSize&&checkContinuity(location+1)) ...

checkContinuity(1) is called, which then reaches this statement:

if(location-1>=0&&checkContinuity(location-1)) ...

and calls checkContinuity(0), which causes checkContinuity(1) to be called, and so on. There is your infinite loop.

Name: Anonymous 2006-09-25 8:46

Bug number one found: It uses Java.

Name: Anonymous 2006-09-25 9:15

>>9
uh, that's what cellChecked prevents

Name: Anonymous 2006-09-25 14:06

Thanks for the assistance boys (no girls on internet n' all), I think I've fixed the problems. That, of course, only brought up more, smaller errors, but I'll be fine with those. Thanks y'all.

Name: Anonymous 2006-09-25 17:24

>>12
You still haven't fixed the bug reported by >>10

Name: Anonymous 2009-01-14 13:23

Can moderators ban this faggot?

Name: Anonymous 2011-02-03 8:01

Name: Sgt.Kabuꇌꬭkiman島 2012-05-28 20:21

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List