This site requires JavaScript, please enable it in your browser!
Greenfoot back
magentaink
magentaink wrote ...

2024/11/29

Checking the Solution for a Picross Game

1
2
magentaink magentaink

2024/11/29

#
Hi! I'm pretty new to Greenfoot and Java,, at the moment I'm making a picross game. What would be the best way for me to check the solution to the puzzles against what the user enters? I was thinkin boolean arrays?
danpost danpost

2024/11/30

#
magentaink wrote...
Hi! I'm pretty new to Greenfoot and Java,, at the moment I'm making a picross game. What would be the best way for me to check the solution to the puzzles against what the user enters? I was thinkin boolean arrays?
Is your program going to be checking that each entry is correct or not, or just checking the end result of the solve? The end result check should be simple enough -- in that all the clues must be satisfied (just check that). If checking along the way, then the solution should be stored in memory to see that each entry in correct (checking entry with correct value). Obviously, if the solution is stored, then, either way, you can check cell by cell.
magentaink magentaink

2024/12/12

#
I'm aiming to have it so that every time a square is clicked, it checks to see if the current state of the puzzle aligns with the solution. I currently have it so that when the image for a square is filled, its state is true and if the image is unfilled, its state is false but am struggling to implement a good way to store and check it against the solution.
danpost danpost

2024/12/13

#
magentaink wrote...
I'm aiming to have it so that every time a square is clicked, it checks to see if the current state of the puzzle aligns with the solution. I currently have it so that when the image for a square is filled, its state is true and if the image is unfilled, its state is false but am struggling to implement a good way to store and check it against the solution.
An array of ones and zeros would be sufficient:
public int[][] grid = new int[HEIGHT][WIDTH];
Just need to keep it updated as things changes
magentaink magentaink

2024/12/17

#
How would I implement this? I have each square as an individual object.
danpost danpost

2024/12/17

#
magentaink wrote...
How would I implement this? I have each square as an individual object.
Maybe I should ask how the solution is stored. Plus, how the clues are given.
magentaink magentaink

2024/12/23

#
The clues are just images on the side of the grid. At the moment I have the solution in the square class like this:
 private void solutionOne()
{
gridone|0][0]=0:gridone[1][0]=0:gridone[21[0]=0:gridone[3][0]=0:gridone[41[0]=0
gridone[0][1]=1;gridone[1][1]=0;grid0ne[2][1]=0;gridone[3][1]=0;gridone[4][1]=1;
gridone[0][2]=0;gridone[1][2]=0;gridone[2][2]=0;gridone[3][2]=0;gridone[4][2]=0;
gridone[0][3]=1;gridone[1][3]=0;gridOne[2][3]=0;gridone[3][3]=0;gridone[4][3]=1;
gridone[0][4]=1;gridone[1][4]=1;gridOne[2][4]=1;gridone[3][4]=1;gridone[4][4]=1; 
} 
Thanks :'] edit: just realised there was a typo
danpost danpost

2024/12/23

#
magentaink wrote...
The clues are just images on the side of the grid. At the moment I have the solution in the square class like this: << Code Omitted >>
A better way to assign the entries is like this:
gridone = new int[][] {
    { 0, 1, 0, 1, 1 },
    { 0, 0, 0, 0, 1 },
    { 0, 0, 0, 0, 1 },
    { 0, 0, 0, 0, 1 },
    { 0, 1, 0, 1, 1 }
};
Furthermore, it should not be in your square class -- it should be in your game world class. Also, as above, you should be able to visually see the solution within the entries. If the rows and columns are mixed up and the visual looks tipped on its side, then switch the rows and columns. That is, use the following to assign the entire array in one statementrr:
gridone = new int[][] {
    { 0, 0, 0, 0, 0 },
    { 1, 0, 0, 0, 1 },
    { 0, 0, 0, 0, 0 },
    { 1, 0, 0, 0, 1 },
    { 1, 1, 0, 1, 1 },
};
(I always have the entries as such:
int [ colNumber ] [ rowNumber ]
That is only in code block because brackets are being used here (which will not work well in standard text. Anyway, it is easier to check that you entered the solution properly when everything is aligned. As far as the clues -- you might want to represent them among your data as values. That way, your world can accept (as parameters) as two 2-D arrays (a 3-D array which contains two 2-D arrays for (1) row clues and (2) column clues. Then you can write an algorithm that compares the current solve state with what the clues should produce to verify correctness of the solve. You must already have a way to determine whether a square is filled or not -- otherwise, you would not be able to change its state. So, you should be able to check each squares state against the solution.
magentaink magentaink

2025/1/7

#
Thanks! is it possible for me to check the squares images from the world class?
danpost danpost

2025/1/7

#
magentaink wrote...
Thanks! is it possible for me to check the squares images from the world class?
Of course, it is possible. Let me make a correction first. The entries as I prefer to give them are in the form:
 int [ rowNumber ] [ colNumber ] 
Anyway, you will need to provide the Square class codes along with your world class codes. Cannot fix unseen codes.
magentaink magentaink

2025/1/14

#
Hi, thanks for all the help. My world and square classes are a bit of a mess at the moment since I've tried a bunch of different things. Some code I've temporarily made into comments while I'm testing other things out so it's all a bit jumbled. MyWorld:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

// uhh the world

public class MyWorld extends World
{
    public boolean solution;
    public boolean state;
    public GreenfootImage filled1;
    public GreenfootImage unfilled1;
    public GreenfootImage icon;
    public GreenfootImage instructions;
    public int[][] gridOneInt= new int[5][5];
    public int[][] solutionOne;
    public int[][] solutionTwo;
    public int[][] solutionThree;
    public int[][] solutionFour;
    public int[][] solutionFive;
    public square[][] gridOne = new square[5][5];
   

    // constructor for objects of class MyWorld
    public MyWorld()
    {   
        super(600, 400, 1); 

        LevelTitle levelTitle = new LevelTitle();
        addObject(levelTitle,100,40);

        InstructionIcon instructionIcon = new InstructionIcon();
        addObject(instructionIcon,490,63);

        //complete();
        gridOne();
        //gridTwo();
        //gridThree();
        //gridFour();
        //gridFive(); 
        //getImages(square.class);
        
    }

    public boolean getState()
    {
      return state;
    }
    
    //The grid for the first level
    public void gridOne()
    {
        One one = new One();
        addObject(one,210,45);

        gridNumbersLeft gridNumbersLeft = new gridNumbersLeft();
        addObject(gridNumbersLeft,7,263);

        gridNumbersTop gridNumbersTop = new gridNumbersTop();
        addObject(gridNumbersTop,177,88);

        Background background = new Background();
        addObject(background,137,220);

        Background background2 = new Background();
        addObject(background2,137,299);

        Background background3 = new Background();
        addObject(background3,213,220);

        Background background4 = new Background();
        addObject(background4,213,299);

        square[][] gridOneObject = new square[5][5];
        
        //for(int a=0;a<=gridOne.length;a++)
        {
        
            square square1 = new square();
            addObject(square1,95,180);
            square1 = gridOne[0][0];

            square square2 = new square();
            addObject(square2,135,180);
            square2 = gridOne[1][0];

            square square3 = new square();
            addObject(square3,175,180);
            square3 = gridOne[2][0];

            square square4 = new square();
            addObject(square4,215,180);
            square4 = gridOne[3][0];

            square square5 = new square();
            addObject(square5,255,180);
            square5 = gridOne[4][0];

            square square6 = new square();
            addObject(square6,95,220);
            square6 = gridOne[0][1];

            square square7 = new square();
            addObject(square7,135,220);
            square7 = gridOne[1][1];

            square square8 = new square();
            addObject(square8,175,220);
            square8 = gridOne[2][1];

            square square9 = new square();
            addObject(square9,215,220);
            square9 = gridOne[3][1];

            square square10 = new square();
            addObject(square10,255,220);
            square10 = gridOne[4][1];

            square square11 = new square();
            addObject(square11,95,260);
            square11 = gridOne[0][2];

            square square12 = new square();
            addObject(square12,135,260);
            square12 = gridOne[1][2];

            square square13 = new square();
            addObject(square13,175,260);
            square13 = gridOne[2][2];

            square square14 = new square();
            addObject(square14,215,260);
            square14 = gridOne[3][2];

            square square15 = new square();
            addObject(square15,255,260);
            square15 = gridOne[4][2];

            square square16 = new square();
            addObject(square16,95,300);
            square16 = gridOne[0][3];

            square square17 = new square();
            addObject(square17,135,300);
            square17 = gridOne[1][3];

            square square18 = new square();
            addObject(square18,175,300);
            square18 = gridOne[2][3];

            square square19 = new square();
            addObject(square19,215,300);
            square19 = gridOne[3][3];

            square square20 = new square();
            addObject(square20,255,300);
            square20 = gridOne[4][3];

            square square21 = new square();
            addObject(square21,95,340);
            square21 = gridOne[0][4];

            square square22 = new square();
            addObject(square22,135,340);
            square22 = gridOne[1][4];

            square square23 = new square();
            addObject(square23,175,340);
            square23 = gridOne[2][4];

            square square24 = new square();
            addObject(square24,215,340);
            square24 = gridOne[3][4];

            square square25 = new square();
            addObject(square25,255,340);
            square25 = gridOne[4][4];
        } 
        
        solutionOne = new int[][] {
            {0,0,0,0,0},
            {1,0,0,0,1},
            {0,0,0,0,0},
            {1,0,0,0,1},
            {1,1,1,1,1}
        };
        
    }
    
    
    public void gridTwo()
    { // 6x6
        Two two = new Two();
        addObject(two,210,45);
        
        solutionTwo = new int[][] {
            {1,1,0,0,1,1},
            {1,1,0,0,1,1},
            {1,1,1,1,1,1},
            {1,1,1,1,1,1},
            {0,1,1,1,1,0},
            {0,0,1,1,0,0}
        };
    }

    public void gridThree()
    {//7x7
        Three three = new Three();
        addObject(three,210,45);
        
        solutionThree = new int[][] {
            {0,0,0,1,0,0,0},
            {0,0,1,0,1,0,0},
            {0,1,0,1,0,1,0},
            {1,0,1,0,1,0,1},
            {0,1,0,1,0,1,0},
            {0,0,1,0,1,0,0},
            {0,0,0,1,0,0,0}
        };
    }

    public void gridFour()
    {//8x8
        Four four = new Four();
        addObject(four,210,45);
        
         solutionFour = new int[][] {
            {0,1,0,0,0,0,1,0},
            {1,0,1,1,1,1,0,1},
            {1,0,0,0,0,0,0,1},
            {1,0,1,0,0,1,0,1},
            {1,0,0,0,0,0,0,1},
            {1,0,1,1,1,1,0,1},
            {0,1,0,0,0,0,1,0},
            {0,0,1,1,1,1,0,0}
        };
    }

    public void gridFive()
    {//9x9
        Five five = new Five();
        addObject(five,210,45);
        
        solutionFive = new int[][] {
            {0,0,0,0,1,0,0,0,0},
            {1,0,0,0,1,0,0,0,1},
            {1,1,1,1,1,1,1,1,1},
            {0,0,1,1,1,1,1,0,0},
            {0,0,0,1,1,1,0,0,0},
            {0,0,1,1,0,1,1,0,0},
            {0,0,1,1,0,1,1,0,0},
            {0,1,1,0,0,0,1,1,0},
            {1,0,0,0,0,0,0,0,1}
        };
    }

    //first array to be compared to the current state of the user's input 
   // public static void solutionOne()
   // {
        //state(square1)== false && square2(state)=false && square3=false
        // && square4=false&&square5==false&&square6=false
        //&&square7=false &&square8=true&& square9=false
        //&&square10==true&&square11==false&&square12==false
        //&&square13==false&&square14==false&&square15==false
        //&&square16==true&&square17==false&&square18==false
        //&&square19==false&&square20==true&&square21==true
        //&&square22==true&&square23==true&&square24==true&&
        //square25=true; 
        
 //   }

    //second array to be compared to the current state of the user's input 
  //  public static void solutionTwo()
  //  {

  //  }

    //third array to be compared to the current state of the user's input 
  //  public static  void solutionThree()
  //  {

  //  }

    //fourth array to be compared to the current state of the user's input 
  //  public static void solutionFour()
  //  {

   // }

    //fifth array to be compared to the current state of the user's input 
  //  public static void solutionFive()
  //  {

   // }

    //this will be a method
    //that checks the current input against the solution
    //this is still a work in progress since I'm unsure how to implement this
  // public boolean checkSolution()
  //  {
    //   if (getImage(square.class).equals(filled1))
    //  {
     //  return true ;
 // }
 //else
 // {
 //  return false;
 // }
        
        
    //   if (getState(square.class)==true)
     //    {
             
     //   }


    //     if(        gridOne[0]== 0 && gridOne[1]== 0 && gridOne[2]== 0 && gridOne[3]== 0 && gridOne[4]== 0 && 
    //                gridOne[5]== 1 && gridOne[6]== 0 && gridOne[7]== 0 && gridOne[8]== 0 && gridOne[9]== 1 &&
    //            gridOne[10]== 0 && gridOne[11]== 0 && gridOne[12]== 0 && gridOne[13]== 0 && gridOne[14]== 0 &&
      //          gridOne[15]== 1 && gridOne[16]== 0 && gridOne[17]== 0 && gridOne[18]== 0 && gridOne[19]== 1 && 
     //           gridOne[20]== 1 && gridOne[21]== 1 &&gridOne[22]== 1 && gridOne[23]== 1 && gridOne[24]== 1)
     //   {

      //      return true;
     //   }   
     //    else
      //   {

     //  return false;
    //     }
         
      //  }
    

    //this will run when the player has completed a level
    public void complete()
    {
        Congratulations Congratulations = new Congratulations();
        addObject(Congratulations, 420,200);
    } 
    
  //  public int getSquareValue()
  //  {
      //  state();
  
  //  }
    
        //public boolean getState()
   // {
        
      
    // if (getObjects(square.class)==filled1)
   //  {
    //     return true;
    // }   
    // else
    // {
    //     return false;
//}
         
    }

square:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

// THE SQUARES! THE SQUARES!
 
public class square extends Actor
{
    public GreenfootImage filled1;
    public GreenfootImage unfilled1;
    public boolean solutionOne;
    public int state;
   public int[][] gridOne= new int[5][5];
    public int[][] solution1;
    
    public void act()
    {   
          onAndOff();
          state();
         // solutionOne();
          
      //    if(Greenfoot.mouseClicked(this))
       //   {
        //      checkSolution();
        //      if (checkSolution()==true)
          //    {
                  
          //    }
          }
    
    
   // initialises the image
   public square()
    {
        filled1=new GreenfootImage ("filled1.png");
        unfilled1=new GreenfootImage ("unfilled1.png");
        setImage(unfilled1);
    }
    
    //allows the squares to be clicked on and off
    public void onAndOff()
       {
            if(Greenfoot.mouseClicked(this))
       {
           if (getImage() == unfilled1)
           {
               setImage(filled1);
           }
           else if (getImage() == filled1)
           {
               setImage(unfilled1);
           }
           else
           {
               setImage(unfilled1);
           } 
           
       }
           
       }

  //  assigns '1' if the square is on and '0' if off
  public int state()
   {
        getWorld();
   {   
    for(int x=0;x<gridOne.length;x++)
   {
        
     if (getImage() == filled1)
    {
      return 1;
    }
         
     else
    {
      return 0;
    }
  }
    
 }
  return 1; //probably something to do with the brackets - unsure what - but it doesn't think there's a return value. Or is it impossible to return data using conditions in this way?
}
}
    
//public int[] currentState()

//{
    // getWorld();
    //[return an array of numbers from state]
 
     // return something
//}
 
//first array to be compared to the current state of the user's input 
//  private void solutionOne()
  
//  {
      
         //  gridOne[0][0]=0;gridOne[1][0]=0;gridOne[2][0]=0;gridOne[3][0]=0;gridOne[4][0]=0;
         //  gridOne[0][1]=1;gridOne[1][1]=0;gridOne[2][1]=0;gridOne[3][1]=0;gridOne[4][1]=1;
         //  gridOne[0][2]=0;gridOne[1][2]=0;gridOne[2][2]=0;gridOne[3][2]=0;gridOne[4][2]=0;
         //  gridOne[0][3]=1;gridOne[1][3]=0;gridOne[2][3]=0;gridOne[3][3]=0;gridOne[4][3]=1;
           //  gridOne[0][4]=1;gridOne[1][4]=1;gridOne[2][4]=1;gridOne[3][4]=1;gridOne[4][4]=1;   
    
           // new int[4][4] = solution1[4][4]= 0,0,0,0,0,
           //                                  1,0,0,0,1,
            //                                 0,0,0,0,0,
            //                                 1,0,0,0,1,
            //                                 1,1,1,1,1
  //  }    
    
 // public boolean checkSolution()
 
 // {
     
      //  getWorld();
    
    
    //  System.out.println(currentState());
        
       //if (state == 1)
       // {
           //   return true ;
        //  }
       // else
     //  {
      // return false;
       // }
                   
  //}  


    
danpost danpost

2025/1/14

#
magentaink wrote...
Hi, thanks for all the help. My world and square classes are a bit of a mess at the moment since I've tried a bunch of different things. Some code I've temporarily made into comments while I'm testing other things out so it's all a bit jumbled. << Code Omitted >>
Your square class can be reduced to the following, for which I removed all unnecessary items. I also capitalized the name of the class as this is how classes are named (by convention). Note that the class only deals with the square objects themselves. Nothing "worldly" should be dealt with here. The world is what controls the game and any checks for game over is done there in the world. Here is the simplified Square class:
import greenfoot.*;
  
public class Square extends Actor
{
    public GreenfootImage[] images = new GreenfootImage[2];
    public int state;
    
    // initialize images
    public Square()
    {
        images[0] = new GreenfootImage ("filled1.png");
        images[1] = new GreenfootImage ("unfilled1.png");
        setImage(images[state]);
    }
    
    // change state on mouse clicks
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            state = (state+1)%2; // update state
            setImage(images[state]); // adjust image
        }
    }
    
    // returns the state of this square
    public int getState()
    {
        return state;
    }
}
In your world class, you can combine all the solutions into one array set where the index will refer to the level number itself (minus one):
private int[][][] solutions = new int[][][]
{
    {
        { 0, 0, 0, 0, 0 },
        { 1, 0, 0, 0, 1 },
        { 0, 0, 0, 0, 0 },
        { 1, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1 }
    |,
    {
        { 1, 1, 0, 0, 1, 1 },
        { 1, 1, 0, 0, 1, 1 },
        { 1, 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1, 1 },
        { 0, 1, 1, 1, 1, 0 },
        { 0, 0, 1, 1, 0, 0 }
    },
    {
        { 0, 0, 0, 1, 0, 0, 0 },
        { 0, 0, 1, 0, 1, 0, 0 },
        { 0, 1, 0, 1, 0, 1, 0 },
        { 1, 0, 1, 0, 1, 0, 1 },
        { 0, 1, 0, 1, 0, 1, 0 },
        { 0, 0, 1, 0, 1, 0, 0 },
        { 0, 0, 0, 1, 0, 0, 0 }
    },
    {
        { 0, 1, 0, 0, 0, 0, 1, 0 },
        { 1, 0, 1, 1, 1, 1, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 0, 0, 1, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 1, 1, 1, 0, 1 },
        { 0, 1, 0, 0, 0, 0, 1, 0 },
        { 0, 0, 1, 1, 1, 1, 0, 0 }
    },
    {
        { 0, 0, 0, 0, 1, 0, 0, 0, 0 },
        { 1, 0, 0, 0, 1, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 0, 0, 1, 1, 1, 1, 1, 0, 0 },
        { 0, 0, 0, 1, 1, 1, 0, 0, 0 },
        { 0, 0, 1, 1, 0, 1, 1, 0, 0 },
        { 0, 0, 1, 1, 0, 1, 1, 0, 0 },
        { 0, 1, 1, 0, 0, 0, 1, 1, 0 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1 }
    }
};
int[][] grid;
private int level;

private void loadLevel()
{
    grid = new int[solutions.length][solutions[0].length];
}
"grid" is the current grid, doesn't matter, first, second, whatever. The "level" value starts at zero ('0'), so when displaying, use "1+level" as the level number. The only thing that needs done when changing levels is adjusting the "level" counter and loading the new grid (plus other resetting stuff not dealing with level or grid). The "top" and "left" clues can also be combined separately using the same index value for corresponding grids.
danpost danpost

2025/1/14

#
Just need some code to load a new level to be added to the loadLevel method. You know, whatever needs done to update the display, mainly (displaying the new level number, etc).
magentaink magentaink

2025/1/22

#
It's probably obvious but I still can't work out how the input would be checked against the solution?
danpost danpost

2025/1/22

#
magentaink wrote...
It's probably obvious but I still can't work out how the input would be checked against the solution?
Why would you check input with the solution? You should only need to check that the complete solution coincides with the clues. Doing this check can be limited to when the proper number of "on" cells is achieved. This also can be limited to when the solver submits the current solve state as a possible solution (where upon the number of "on" cells is checked first).
There are more replies on the next page.
1
2