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?
public int[][] grid = new int[HEIGHT][WIDTH];
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; }
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 } };
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 }, };
int [ colNumber ] [ rowNumber ]
int [ rowNumber ] [ colNumber ]
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; //} }
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; // } //}
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; } }
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]; }