code:
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.random; /*note:y must go second in searches in loops because variable gameboard contains first section works x-axis rather y-axis. however, if search vertical, x may go second. z go first regardless.*/ //todo main interface public class sample { jframe window = new jframe("sample"); //creates our window jpanel row1 = new jpanel(); //this 1 holds buttons incorporates game modes jbutton btclassic = new jbutton("classic"); jbutton btaction = new jbutton("action"); jbutton btblitz = new jbutton("countdown"); board row2 = null; //a jpanel class cell[][] cell = null; //jcomponent rotator[] rotateup = null; //to rotate rotator[] rotateside = null; //to rotate side side rotator[] rotatedown = null; //to rotate down nullspot[] nullspot = null; //a null spot make equal control control = null; public sample() { control = new control(this); window.setsize(350,400); window.setbackground(color.black); window.setresizable(false); window.setdefaultcloseoperation(jframe.exit_on_close); flowlayout layout = new flowlayout(); window.setlayout(layout); flowlayout layout1 = new flowlayout(); row1.setlayout(layout1); row1.add(btclassic); row1.add(btaction); row1.add(btblitz); window.add(row1); btclassic.addactionlistener(control); gridlayout layout2 = new gridlayout(10,10); row2 = new board(this); row2.setlayout(layout2); rotateup=new rotator[8]; //used rotate rotateside=new rotator[16]; //used rotate side side rotatedown=new rotator[8]; //used rotate down cell = new cell[8][8]; //used interact game nullspot = new nullspot[4]; //used for(int x = 0; x < 16; x++) { if(x <= 3){nullspot[x] = new nullspot();} //declare null spot if(x<=7){ rotateup[x] = new rotator(); //declare rotator rotatedown[x] = new rotator(); //declare down rotator } rotateside[x] = new rotator(); //declare side rotator } //first part sets 2 null spots , rotators row2.add(nullspot[0]); for(int x = 0; x<8;x++) { row2.add(rotateup[x]); //rotators } row2.add(nullspot[1]); //then sets rotators , cells interaction for(int x = 0; x < 8; x++) { row2.add(rotateside[(x*2)]); //the left rotator for(int y = 0; y < 8; y++) { //note:y goes first because actual array treats x , y correctly. cell[x][y] = new cell(row2,control); row2.add(cell[x][y]); cell[x][y].setxvalue=((x*32)+32); cell[x][y].setyvalue=((y*32)+32); cell[x][y].myarrayx=x; cell[x][y].myarrayy=y; cell[x][y].addmouselistener(control); //system.out.println(cell[x][y].setxvalue + ","+cell[x][y].setyvalue); } row2.add(rotateside[(x*2)+1]); //the right rotator } //lastly, sets 2 null spots , rotators row2.add(nullspot[2]); for(int x = 0; x<8;x++) { row2.add(rotatedown[x]); //down rotator } row2.add(nullspot[3]); row2.setpreferredsize(new dimension(32*10,32*10)); window.add(row2); window.setvisible(true); } public static void main(string[] args) { sample game = new sample(); //creates our game here } } //todo handles of actions class control implements actionlistener, mouselistener { sample gui; boolean gamebegan=false; boolean selected=false; int selectedx,selectedy; int[][][] gameboard = new int[8][8][8]; public control(sample gui) { this.gui=gui; } //todo public void actionperformed(actionevent e) { object input = e.getsource(); if(input.equals(gui.btclassic)) { gui.btclassic.setenabled(false); gui.btblitz.setenabled(false); gui.btaction.setenabled(false); gamebegan=true; setupboard(); } } //todo public void mousepressed(mouseevent e) { object input = e.getsource(); if(gamebegan==true) {//for(int z =0; z < 8; z++) { for(int x= 0; x < 8; x++) //y { for(int y= 0; y < 8; y++) //x { if(input.equals(gui.cell[x][y])) //find cell { if(selected==true)//if there selected { if(gui.cell[x][y].selected==true) { selected=false; gui.cell[x][y].selected=false; system.out.println("("+y+","+x+")!"); } //end fourth if //this parts deals swapping of gems //checks down,right,up, , left if( (((x-1)==selectedx) && (y==selectedy)) || (((y-1)==selectedy)&& (x==selectedx)) || (((x+1)==selectedx)&& (y==selectedy)) ||(((y+1)==selectedy) && (x==selectedx))) //down { swapcircle(x,y,0); //deals circle swapping } //if clicked farther if(((y-1) > selectedy) || ((y+1) < selectedy) || ((x-1) > selectedx) ||((x+1) < selectedx)|| (((x-1)==selectedx)&&((y-1)==selectedy)) ||(((x+1)==selectedx)&&((y-1)==selectedy)) ||(((x-1)==selectedx)&&((y+1)==selectedy)) ||(((x+1)==selectedx)&&((y+1)==selectedy))) { gui.cell[selectedx][selectedy].selected=false; gui.cell[selectedx][selectedy].repaint(); selectedx=x; selectedy=y; gui.cell[x][y].selected=true; } }//end third if else //if there nothing selected { getselected(x,y); } //end else }//end second if refreshscreen(); } //end second } //end first } }//end first if } // todo used redraw screen when chain reaction occurs void refreshscreen() { for(int x = 0; x < 8; x++) { for(int y = 0; y < 8; y++) { gui.cell[x][y].repaint(); } } } //todo void drawboard() { //for(int z =0; z<8; z++) { for(int y = 0; y< 8; y++) { for(int x = 0; x < 8; x++) { system.out.print(gameboard[x][y][0]); } system.out.print("\n"); } system.out.print("\n"); } } //todo deals when user clicks on 1 vertically or horizontally away void swapcircle(int x, int y,int z) { int temporarycolor=gameboard[y][x][z]; //used transferring colors system.out.println(temporarycolor); boolean check = false; int checktimes=1; system.out.println("selected: "+gameboard[selectedy][selectedx][z]+" switch: "+gameboard[y][x][z]); gameboard[y][x][z]=gameboard[selectedy][selectedx][z]; gameboard[selectedy][selectedx][z]=temporarycolor; gui.cell[x][y].circlecolor=gui.cell[selectedx][selectedy].circlecolor; gui.cell[selectedx][selectedy].circlecolor=temporarycolor; system.out.println("selected: "+gameboard[selectedy][selectedx][z]+" switch: "+gameboard[y][x][z]); drawboard(); system.out.println(gui.cell[x][y].circlecolor + " @ ("+(x+1)+","+(y+1)+")!"); while(checktimes>=0) //while checking not finished { /*first goes t-shape match * next goes l-shape match * last goes single row match*/ switch(checktimes) { //case 1:check=checkmultirowmatch(); break; //t , l shape case 0:check=checksinglerowmatch(); break; //single row match }//end switch if((check==false)&&(checktimes==0)) //if there no match @ all, put back. { gameboard[selectedy][selectedx][z]=gameboard[y][x][z]; gameboard[y][x][z]=temporarycolor; gui.cell[selectedx][selectedy].circlecolor=gui.cell[x][y].circlecolor; gui.cell[x][y].circlecolor=temporarycolor; } else if(check==true)//if there match { int times=0; //tracks cascade checktimes=1; //resets checking highest while(checktimes>=0) { if(check==true) //if there match, add cascade { times++; checktimes=1; //go highest } else //otherwise, go next check { checktimes--; } //generatenewcircles(); //generate new circles replace matched ones switch(checktimes) { //case 1:check=checkmultirowmatch(); break; //t , l shape case 0:check=checksinglerowmatch(); break; //3 match }//end switch } system.out.println("occurred " + times +" times!"); } checktimes--; //system.out.println(checktimes); } check=false; selected=false; gui.cell[selectedx][selectedy].selected=false; } //todo grabs selected void getselected(int x, int y) { selected=true; selectedx=x; //tracks y coordinate y selectedy=y; //tracks x coordinate x //system.out.println("("+y+","+x+")!"); gui.cell[x][y].selected=true; gui.cell[x][y].repaint(); } //todo used @ beginning. allows set board making sure void setupboard() { random generator = new random(); int pickedupnumber=0; //stores picked number int occurred=0; //tracks how many of same color used in row boolean horizontalandzaxispass=true; //turns false if fails vertical , z-axis test for(int z =0; z<8; z++) { for(int y = 0; y< 8; y++) { for(int x = 0; x < 8; x++) { for(;;) { horizontalandzaxispass=true; gameboard[x][y][z]=math.abs(generator.nextint()%6)+1; //pick number @ random if(pickedupnumber==gameboard[x][y][z])//if it's same number, track { occurred++; } //if last number not equal current number or if same number has not occurred 3 times if((pickedupnumber!=gameboard[x][y][z]) || (occurred<2)) { //vertical test-if 2 top cells not same color(pass=false,fail=true) if((y>=2)&&(gameboard[x][y-2][z]==gameboard[x][y][z]) && (gameboard[x][y-2][z]==gameboard[x][y-1][z])) //the vertical test { horizontalandzaxispass=false;//there 2 above of same color } if((z>=2)&&(gameboard[x][y][z-2]==gameboard[x][y][z-1]) && (gameboard[x][y][z]==gameboard[x][y][z-1])) //the z-axis test { horizontalandzaxispass=false;//there 2 on z-axis of same color } if(horizontalandzaxispass==true) { if(pickedupnumber!=gameboard[x][y][z]) //if it's new number, reset track { occurred=0; } break; } } } pickedupnumber = gameboard[x][y][z]; //system.out.println(pickedupnumber +" ("+(y+1)+","+(x+1)+")!"); if(z==0) { gui.cell[x][y].circlecolor=gameboard[x][y][z]; gui.cell[x][y].repaint(); //system.out.println("("+(x+1)+","+(y+1)+"): "+gui.cell[x][y].circlecolor); } system.out.print(gameboard[x][y][z]); } system.out.print("\n"); } system.out.print("\n"); } //end loop }//end function //todo check single row match: last 1 go boolean checksinglerowmatch() { int coloroccurance=0; //how many times has color occurred? int currentcolor=-1; //tracks recent color int numbercheck=8; while(numbercheck>=3) { //horizontal check for(int z =0; z<8;z++) { for(int y = 0; y < 8; y++) { for(int x =0; x < 8; x++) { //system.out.println(gameboard[x][y][z]); if((currentcolor!=gameboard[x][y][z])&& (gameboard[x][y][z]!=0)) //if we're dealing new color, set , reset count { coloroccurance=1; currentcolor=gameboard[x][y][z]; /*if(z==0) { system.out.println(currentcolor + "at (" +(x+1)+","+(y+1)+")!"); }*/ } else if(gameboard[x][y][z]!=0) //otherwise { coloroccurance++; } if(coloroccurance==numbercheck) { for(int reverse=numbercheck-1; reverse >= 0;reverse--)//deals destroying circles { gui.row2.piece[x-reverse][y].removespot(); gameboard[x-reverse][y][z]=0; gui.cell[x-reverse][y].circlecolor=0; gui.cell[x-reverse][y].repaint(); } drawboard(); system.out.println(numbercheck+" match horizontal!"); return true; } } coloroccurance=0; currentcolor=-1; }} //vertical check for(int z =0; z<8;z++) { for(int x = 0; x < 8; x++) { for(int y =0; y < 8; y++) { if((currentcolor!=gameboard[x][y][z]) && (gameboard[x][y][z]!=0)) { coloroccurance=1; currentcolor=gameboard[x][y][z]; } else if(gameboard[x][y][z]!=0) { coloroccurance++; } if(coloroccurance==numbercheck) { for(int reverse=numbercheck-1; reverse >= 0;reverse--)//deals destroying circles { system.out.println("found it!"); gui.row2.piece[x-reverse][y].colorpiece=0; gameboard[y-reverse][x][z]=0; gui.cell[x-reverse][y].circlecolor=0; gui.cell[x-reverse][y].repaint(); } system.out.println(numbercheck+" match!"); return true; } } coloroccurance=0; currentcolor=-1; } } numbercheck--; } return false; } @override public void mouseclicked(mouseevent arg0) { // todo auto-generated method stub } @override public void mouseentered(mouseevent arg0) { // todo auto-generated method stub } @override public void mouseexited(mouseevent arg0) { // todo auto-generated method stub } @override public void mousereleased(mouseevent arg0) { // todo auto-generated method stub } } //todo handles animation portion class board extends jpanel { sample gui; circlepiece[][] piece = new circlepiece[8][8];; public board(sample gui) { this.gui=gui; validate(); for(int x = 0; x < 8; x++) { for(int y= 0;y<8;y++) { piece[x][y] = new circlepiece(this); } } } public void paintcomponent(graphics g) { super.paintcomponent(g); system.out.println("printing!"); for(int x = 0; x<8; x++) { for(int y = 0; y<8; y++) { if((gui.control.gamebegan==true) && (piece[x][y].colorpiece!=0))//if spot exists { switch(piece[x][y].colorpiece) { case 1: g.setcolor(color.blue);break; case 2: g.setcolor(color.red); break; case 3: g.setcolor(color.green); break; case 4: g.setcolor(color.yellow);break; case 5: g.setcolor(new color(255, 165, 0)); break; case 6: g.setcolor(color.white);break; } } else if (gui.control.gamebegan==true) { g.setcolor(getbackground()); } if((piece[x][y].myy>=31) && (piece[x][y].colorpiece!=0)) { g.filloval(piece[x][y].myx,piece[x][y].myy, 31, 31); } } } } //allows generate new pieces void generatecircle(int piecenumber,int xarray, int yarray, int endy) { piece[xarray][yarray].colorpiece=piecenumber; piece[xarray][yarray].myx=(xarray*32)+32; piece[xarray][yarray].myy=0; piece[xarray][yarray].endyvalue=endy; piece[xarray][yarray].gravityturnon(); } } //the actual piece class circlepiece implements actionlistener { int colorpiece=0; //handles piece part int myx=0; int myy=0; int endyvalue; board board; timer animatedowngravity;//deals public circlepiece(board board) { this.board=board; } int gravitymove=25; void removespot() { system.out.println("will remove!"); colorpiece=0; system.out.println(myy); board.repaint(); } void gravityturnon() { if(animatedowngravity==null) { animatedowngravity = new timer(1,this); animatedowngravity.start(); } } public void actionperformed(actionevent arg0) { if(animatedowngravity!=null) { myy++; board.repaint(); if(myy==endyvalue) { animatedowngravity.stop(); animatedowngravity=null; }} } } //todo allows interact board class cell extends jcomponent { int circlecolor=0; int setxvalue=0; int setyvalue=0; int myarrayx,myarrayy; boolean haspiece=false; //create piece once boolean selected=false; //determines whether cell selected board board; control control; public cell(board board,control control) { this.board=board; this.control = control; } public void paintcomponent(graphics g) { //system.out.println("printed!"); super.paintcomponent(g); g.setcolor((selected==false?new color(90,90,90):color.cyan)); g.drawrect(0, 0, 31, 31); if((haspiece==false) && (control.gamebegan==true)) { board.generatecircle(circlecolor,myarrayx,myarrayy,setyvalue); //board.repaint(); haspiece=true; } //g.drawoval(setxvalue, setyvalue, 31, 31); } } - //todo handles rotating part class rotator extends jcomponent { boolean canuse=true; public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor((canuse==true?new color(95, 158, 160):color.gray)); g.filloval(0, 0, 31, 31); } } //todo nothing , has used sadly class nullspot extends jcomponent { public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawrect(0, 0, 31, 31); } }
board class drawn , circlepiece refers each game piece you're suppose match. have written generating function previous project replaces pieces when needed. i'm trying make pieces disappear when match occurs. have searched issue, nothing tends work. doing wrong?