java - Why cannot find symbol - variable gameState -
structure of greenfoot https://www.dropbox.com/s/t4pau2mk3mh9npu/structure.jpg did initiate var gamestate
public class mineboard extends gworld { int gamestate = 0; and when try access sub class call "block" under actor this
case 2: { //two bombs known=true; setimage("bomb[2].png"); joptionpane.showmessagedialog(new jinternalframe(), "you lose!","mine sweeper", joptionpane.information_message); gworld.gamestate = 2; it keeps telling me cannot find symbol - variable gamestate please help
the whole code of mineboard
import greenfoot.*; // (world, actor, greenfootimage, greenfoot , mouseinfo) import java.util.scanner; import java.io.*; import javax.swing.joptionpane; import javax.swing.jinternalframe; public class mineboard extends gworld { int gamestate; // 0 playing, 1 win, 2 lose, 3 game ended int nbomb = 0; //the number of bombs on mineboard /** * constructor objects of class mineboard. * hint: * width, height: size of mine board * nbombs: total number of bombs in mine board */ public mineboard(int width, int height, int nbombs) { // create greenfoot world width , height super(width, height, 25); // initialize minefield initialize(); } /** * initialize world */ public void initialize() { // create blocks world width & height setnbomb(); createblocks(); gamestate = 0; // place bombs randomly on board placebombs(); } public void setnbomb () { int sizeofboard = gworld.width(); switch (sizeofboard) { case 5 : nbomb=3; break; case 8 : nbomb=10; break; case 10: nbomb=15; break; default: break; } } /** * create blocks * done */ public void createblocks() { // create "block" objects according difficult user choose int maxl = gworld.width(); (int i=0; i<maxl; i++) { (int u=0; u<maxl; u++) { block block = new block(); gworld.addoneobject(block, i,u); //create blocks left right, top bottom } } } /** * place bombs randomly on board * hint: * int random(int lowerlimit, int upperlimit) * use function generate randome number between * lowerlimit(inlusive) , upperlimit(inclusive) * block.max_bomb: max number of bombs per block * block block = (block)getoneobjectat(x, y, "block"); * place specified number of bombs. no more! * todo: part 1 */ public void placebombs() { //this method place bombs randomly on mineboard int bombplanted=0; //initialize bombs placed 0 while (bombplanted < nbomb) { int = random(0,block.max_bomb+1); //random amount of bombs place in next block int u = random (0, gworld.width()-1); int y = random (0, gworld.height()-1); //random place place bomb(s) block b = (block) getoneobjectat(u, y, "block"); //access specific block if ((bombplanted+i < nbomb) && (b.bombinside ==0)) { b.bombinside = i; //place numbers of bombs bombplanted = bombplanted+i; //count amount of bombs placed } if ((bombplanted+i > nbomb) && (b.bombinside ==0)) { b.bombinside = nbomb-bombplanted; //place bombs until reaching maximum value bombplanted=nbomb; //allow loop end without breaking instantly } } } /** * reveal blocks * hint: * if flagged, reveal anyway * if it's not bomb, * part 1, show blockclicked[0].png * part 2, show image correct number. * todo: part 1 & part 2 */ public void revealallblocks() { //receal blocks (int i=0; i<gworld.width(); i++) { for(int u=0; u<gworld.height(); u++) { block b = (block) getoneobjectat(i, u, "block"); //access blocks 1 one b.reveal(); //use reveal method reveal blocks 1 one } } } /** * check game win. * hint: * correct per block bombs * correct total bombs * block should either flagged or revealed * if win, set gamestate = 1, return true; * todo: part 2 */ public boolean checkgamewin() { int bombsremain = nbomb; int clickedblock = 0; int totalblocks = 0; totalblocks = gworld.width()*gworld.width(); (int = 0; i<gworld.width(); i++) { (int u = 0; u<gworld.height();u++) { block b = (block) getoneobjectat(i, u, "block"); if (b.bombinside == b.flagged) { bombsremain = bombsremain - b.bombinside; } if (b.known == true || b.flagged>0) { clickedblock=clickedblock+1; } } } if (bombsremain == 0 && totalblocks == clickedblock) { gamestate = 1; return true; } return false; } /** * save board "filename" * per block states { * number of bombs: 0,1,2 * number of flags: 0,1,2 * reveal state: true/false * } * todo: part 2 */ protected void saveboard(string filename) throws exception { if ( gamestate != 0 ) { showmessage ("not allowed. game finished."); return ; } else { file saving = new file(filename); printwriter output = new printwriter(saving); (int = 0; i<gworld.width(); i++) { (int u = 0; u<gworld.height(); u++) { //get block block b = (block) getoneobjectat(u, i, "block"); //num of bombs output.print(b.bombinside + " "); //num of flags output.print(b.flagged + " "); //known or not output.print(b.known + " "); } output.println(""); } output.close(); //close file } } /** * load board "filename" * hint: * first load blocks * show correct images * todo: part 2 */ protected void loadboard(string filename) throws exception { clearboard(); gamestate = 0; file saving = new file(filename); scanner input = new scanner(saving); (int = 0; i<gworld.width(); i++) { (int u = 0; u<gworld.height(); u++) { //get block block b = (block) getoneobjectat(u, i, "block"); int inputint=0; string inputstr = "false"; inputint = integer.parseint(input.next()); b.bombinside = inputint; inputint = integer.parseint(input.next()); b.bombinside = inputint; inputstr = input.next(); b.known = boolean.getboolean(inputstr); } } input.close(); //close file // add codes here } /** * gathers blocks , disposes of them. */ public void clearboard() { actor [] blocks = getallobjects("block"); removeobjectsfromworld(blocks); } /** * game lose. */ public void gamelose() { gamestate = 2; revealallblocks(); } /** * check game states */ public void act() { if ( gamestate == 2 ) { showmessage ( "sorry, lose!" ); gamestate = 3; // game finished } if ( gamestate == 1 ) { showmessage ( "congratulations! win!" ); gamestate = 3; // game finished } } } the whole code of block
import greenfoot.*; // (world, actor, greenfootimage, greenfoot , mouseinfo) import java.util.list; import javax.swing.joptionpane; import javax.swing.jinternalframe; import java.util.scanner; import java.io.*; public class block extends actor ![enter image description here][2]{ static string defaultblockimg = "block.png"; static int max_bomb = 2; int flagged=0; //how many flags placed on block boolean known = false; //revealed or not int bombinside = 0; //how many bomb inside block /** * constructor objects of class block. * */ public block() { setimage(defaultblockimg); } public void reveal() { //reveal current block , show correspondent picture switch(bombinside) { case 1: { //one bomb //gworld.gamestate = 2; known=true; setimage("bomb[1].png"); joptionpane.showmessagedialog(new jinternalframe(), "you lose!","mine sweeper", joptionpane.information_message); } break; case 2: { //two bombs known=true; setimage("bomb[2].png"); joptionpane.showmessagedialog(new jinternalframe(), "you lose!","mine sweeper", joptionpane.information_message); //gworld.gamestate = 2; } break; case 3: { //three bombs known=true; setimage("bomb[3].png"); joptionpane.showmessagedialog(new jinternalframe(), "you lose!","mine sweeper", joptionpane.information_message); //gworld.gamestate=2; } break; default: { //no bombs known=true; int b=getnumofnearbybombs(); //that block revealed string numofbombsinsidestr = "blockclicked[" + b + "].png"; setimage(numofbombsinsidestr); //show image of nearby bombs if (b == 0) { propagate(); //expand nearby bomb-free & nearby-bomb-free area } } break; } } //set amount of flags on block public void flag() { if (known == false) { switch(flagged) { case 0: { //if no flag nor revealed, give 1 flag flagged++; setimage("blockflagged[1].png"); } break; case 1: { //if have 1 flag , not revealed, give 2 flags flagged++; setimage("blockflagged[2].png"); } break; case 2: { //if have 2 flags , not revealed, give 3 flags flagged++; setimage("blockflagged[3].png"); } break; default: { //if 3 flags , not revealed, remove flags flagged=0; setimage("block.png"); } break; } } else { //since revealed, denied request system.out.println("revealed already!"); } } protected void leftclick() { reveal(); } protected void rightclick() { flag(); } public int getnumofnearbybombs() { int bombsnearby = 0; //ini var bombsnearby int xpos = getx(); //x position int ypos = gety(); //y position //if block not on edge of board if (xpos>0 && xpos <gworld.width()-1 && ypos>0 && ypos < gworld.height()-1) { (int = xpos - 1 ; i<xpos+2; i++) { //count bombs on above colume block b = (block) gworld.getoneobjectat(i, ypos-1, "block"); bombsnearby = bombsnearby+b.bombinside; } (int = xpos - 1 ; i<xpos+2; i++) { //count bombs on below colume block bl = (block) gworld.getoneobjectat(i, ypos+1, "block"); bombsnearby = bombsnearby+bl.bombinside; } //bombs in lhs block block blo = (block) gworld.getoneobjectat(xpos-1, ypos, "block"); bombsnearby = bombsnearby+blo.bombinside; //bombs in rhs block block bloc = (block) gworld.getoneobjectat(xpos+1, ypos, "block"); bombsnearby = bombsnearby+bloc.bombinside; } else { //top row if (ypos==0 && xpos!=0 && xpos!=gworld.width()-1) { //second row, row right below first (int i=xpos-1; i<xpos+2; i++) { block block = (block) gworld.getoneobjectat(i, 1, "block"); bombsnearby = bombsnearby+block.bombinside; } block blockl = (block) gworld.getoneobjectat(xpos-1, 0, "block"); block blockr = (block) gworld.getoneobjectat(xpos+1, 0, "block"); bombsnearby = bombsnearby+blockl.bombinside; bombsnearby = bombsnearby+blockr.bombinside; //bombs on left //bombs on right } else //bottom row if (ypos==gworld.height()-1 && xpos!=0 && xpos!=gworld.width()-1) { (int i=xpos-1; i<xpos+2; i++) { block block = (block) gworld.getoneobjectat(i, ypos-1, "block"); bombsnearby = bombsnearby+block.bombinside; } block blockl = (block) gworld.getoneobjectat(xpos-1, ypos, "block"); block blockr = (block) gworld.getoneobjectat(xpos+1, ypos, "block"); bombsnearby = bombsnearby+blockl.bombinside; bombsnearby = bombsnearby+blockr.bombinside; //bombs on top //bombs on left //bombs on right } else //right colume if (xpos==gworld.width()-1 && ypos!=0 && ypos!=gworld.height()-1) { (int i=ypos-1; i<ypos+2; i++) { block block = (block) gworld.getoneobjectat(xpos-1, i, "block"); bombsnearby = bombsnearby+block.bombinside; } block blocku = (block) gworld.getoneobjectat(xpos, ypos-1, "block"); block blockl = (block) gworld.getoneobjectat(xpos, ypos+1, "block"); bombsnearby = bombsnearby+blocku.bombinside; bombsnearby = bombsnearby+blockl.bombinside; } else //left colume if (xpos==0 && ypos!=0 && ypos!=gworld.height()-1) { (int i=ypos-1; i<ypos+2; i++) { block block = (block) gworld.getoneobjectat(1, i, "block"); bombsnearby = bombsnearby+block.bombinside; } block blocku = (block) gworld.getoneobjectat(0, ypos-1, "block"); block blockl = (block) gworld.getoneobjectat(0, ypos+1, "block"); bombsnearby = bombsnearby+blocku.bombinside; bombsnearby = bombsnearby+blockl.bombinside; } else //top left if (xpos==0 && ypos==0) { block block1 = (block) gworld.getoneobjectat(0, 1, "block"); block block2 = (block) gworld.getoneobjectat(1, 0, "block"); block block3 = (block) gworld.getoneobjectat(1, 1, "block"); bombsnearby = bombsnearby+block1.bombinside; bombsnearby = bombsnearby+block2.bombinside; bombsnearby = bombsnearby+block3.bombinside; //3 statements 3 blocks } else //bottom left if (xpos==0 && ypos==gworld.height()-1) { block block1 = (block) gworld.getoneobjectat(0, ypos-1, "block"); block block2 = (block) gworld.getoneobjectat(1, ypos, "block"); block block3 = (block) gworld.getoneobjectat(1, ypos-1, "block"); bombsnearby = bombsnearby+block1.bombinside; bombsnearby = bombsnearby+block2.bombinside; bombsnearby = bombsnearby+block3.bombinside; //3 statements 3 blocks } else //top right if (xpos==gworld.width()-1 && ypos==0) { block block1 = (block) gworld.getoneobjectat(xpos, 1, "block"); block block2 = (block) gworld.getoneobjectat(xpos-1, 1, "block"); block block3 = (block) gworld.getoneobjectat(xpos-1, 0, "block"); bombsnearby = bombsnearby+block1.bombinside; bombsnearby = bombsnearby+block2.bombinside; bombsnearby = bombsnearby+block3.bombinside; //3 statements 3 blocks } else //bottom right if (xpos==gworld.width()-1 && ypos==gworld.height()-1) { block block1 = (block) gworld.getoneobjectat(xpos, ypos-1, "block"); block block2 = (block) gworld.getoneobjectat(xpos-1, ypos, "block"); block block3 = (block) gworld.getoneobjectat(xpos-1, ypos-1, "block"); bombsnearby = bombsnearby+block1.bombinside; bombsnearby = bombsnearby+block2.bombinside; bombsnearby = bombsnearby+block3.bombinside; //3 statements 3 blocks } } return bombsnearby; } /** * propagate empty block * todo: part 2 */ public void propagate() { //too long website, skipped } /** * act - whatever block wants do. method called whenever * 'act' or 'run' button gets pressed in environment. */ public void act() { // handle mouse message handlemouseclick(); } /** * handle mouse message */ protected void handlemouseclick() { if ( ((mineboard)getworld()).gamestate != 0 ) return; if( greenfoot.mouseclicked(this) ) { if ( greenfoot.getmouseinfo().getbutton() == 1 ) leftclick(); else if ( greenfoot.getmouseinfo().getbutton() == 3 ) rightclick(); if ( ((mineboard)getworld()).gamestate == 0 ) ((mineboard)getworld()).checkgamewin(); } } /** * nearby blocks */ public block[] getnearbyblocks() { list<block> blocks = getneighbours(1,true,block.class); return blocks.toarray(new block[blocks.size()]); } }
change gworld.gamestate = 2; this.gamestate = 2; if accessing subclass (and since not static variable).
according "inheritance" section of java tutotial
a subclass inherits of public , protected members of parent, no matter package subclass in. if subclass in same package parent, inherits package-private members of parent. note:
since gamestate variable package-private, in order inherited subclass, 2 classes have in same package.
Comments
Post a Comment