multidimensional array - Java TicTacToeGame, code keeps changing the Xs to Os and vise verse -
i building tic tac toe game in java. have gotten game stage can display title, reset button, grid , draw xs , os when player clicks in grid (the game has no game logic add later). problem @ moment, each time player clicks on grid, x's change os , os xs. think problem within paint() method of tictactoegame.java, can't work out still learning. if offer advice, appreciated. here code classes makes game.
tictactoegame.java
import java.applet.applet; import java.awt.button; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.label; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class tictactoegame extends applet { private final int start_x = 40; private final int start_y = 100; private final int rows = 3; private final int cols = 3; private final int box_width = 50; private final int box_height = 50; private final color backcolor = color.black; private final color bordercolor = color.white; private final label title = new label("tic-tac-toe"); private int player = 1; private final font titlefont = new font("arial", font.bold, 16); //used format title tictactoe private tictactoebox boxes[][]; //used create game elements private button resetbutton; //button resetting game /** * method used initalize game */ public void init() { add(title); //adds title title.setfont(titlefont); //sets titles fonts resetbutton = new button("new game/reset"); // creates reset/newgame button resetbutton.setlocation(20, 88); //use position reset button doesn't seem work. add(resetbutton); // adds reset button resetbutton.addactionlistener(new actionlistener() { // creates // action // listener // botton public void actionperformed(actionevent e) { // // action // // performed // when // reset // button // clicked buildboxes(); // method used build boxes repaint(); // method used redraw grid } // end actionperformed() }); // end addactionlistener() boxes = new tictactoebox[rows][cols]; // initializes 2d array 3 rows , 3 columns buildboxes(); // builds boxes resize(200, 300); //resizes applet }//end init() /** * method used create graphics */ public void paint(graphics g) { // loop through boxes , draw them. (int row = 0; row < boxes.length; row++) { (int col = 0; col < boxes[row].length; col++) { boxes[row][col].draw(g); if (boxes[row][col].isclicked()) { removemouselistener(boxes[row][col]); //removes mouse listener boxes[row][col] can nolonger clicked if (player == 1) { boxes[row][col].setplayer("x"); //if player variable ==1 set boxes[row][col].player variable x boxes[row][col].setplayerseclection(true); } else { boxes[row][col].setplayer("y"); //if player variable other, set boxes[row][col].player variable y boxes[row][col].setplayerseclection(true); } //end if/else if(boxes[row][col].playerseclection == true) { //used draw xs , os based on previous selections if (boxes[row][col].player.equals("x")) { //used draw xs boxes[row][col].drawcross(boxes[row][col].getx(),boxes[row][col].gety(),boxes[row][col].getheight(),boxes[row][col].getwidth(), g); player = 2; //sets variable player 2 } else{ //used draw ys boxes[row][col].drawnought(boxes[row][col].getx(),boxes[row][col].gety(),boxes[row][col].getheight(),boxes[row][col].getwidth(), g); player = 1; //sets variable player 1 }//end if/else } //end if }//end if(isclicked) }//end loop }//end loop }//end paint private void removemouselisteners() { (int row = 0; row < boxes.length; row++) { (int col = 0; col < boxes[row].length; col++) { removemouselistener(boxes[row][col]); }//end loop }//end loop }//end removemouselisteners() /** * method used build boxes game */ private void buildboxes() { removemouselisteners(); //removemouselisteners() (int row = 0; row < boxes.length; row++) { (int col = 0; col < boxes[row].length; col++) { boxes[row][col] = new tictactoebox(start_x + col * box_width,start_y + row * box_height, box_width, box_height, backcolor, bordercolor, this); addmouselistener(boxes[row][col]); }//end loop }//end loop }// end of buildboxes() }//end class
tictactoebox.java
import java.awt.color; import java.awt.container; import java.awt.graphics; public class tictactoebox extends clickablebox { string player; //used store player click box boolean playerseclection = false; //determines box has been clicked or not /** * constructor building tictactoe board * * @param x * starting position box along x axis * @param y * starting position box along y axis * @param width * width of box * @param height * height of box * @param parent * container additional elements */ public tictactoebox(int x, int y, int width, int height, color backcolor, color bordercolor, container parent) { super(x, y, width, height, backcolor, bordercolor, parent); }//end tictactoebox() /** * method used value of getplayer. either x or o * @return */ public string getplayer() { return player; }//ends getplayer /** * method used set variable setplayer either x or 0 * @param player */ public void setplayer(string player) { this.player = player; } /** * method used draw cross * @param x starting x position of box * @param y starting y position of box * @param height height of box * @param width width of box * @param g graphic object drawing */ public void drawcross(int x, int y, int height, int width, graphics g){ g.setcolor(color.red); //sets crosses color g.drawline(x, y, x + width, y + height); //draws first line g.drawline(x, y + height, x + width, y); //draws second line }//end drawx() /** * method used draw nought * @param x starting position of box * @param y starting y position of box * @param height height of box * @param width width of box * @param g graphics object */ public void drawnought(int x, int y, int height, int width, graphics g){ g.setcolor(color.blue); //sets oval color g.drawoval(x, y, width-2, height-2); //draws oval } /** * method used player selection * @return */ public boolean getplayerseclection() { return playerseclection; } /** * method used set wether or not player has selected box * @param playerseclection */ public void setplayerseclection(boolean playerseclection) { this.playerseclection = playerseclection; } }
clickablebox.java
import java.awt.event.mouseadapter; import java.awt.color; import java.awt.graphics; import java.awt.event.mouseevent; import java.awt.container; public class clickablebox extends mouseadapter { private int x, y, width, height; // storage position, width , height // of clickable box private color bordercolor, backcolor, oldcolor; // storage border // color, background color // , old/previous color // of clickable box private boolean drawborder, clicked; // true/false storage variable // drawborder , clicked private container parent; // storage container object variable /** * constructor method * * @param x * top left position of mask x axes * @param y * top left position of mask y axes * @param width * how wide mask * @param height * how high mask * @param bordercolor * border color of mask * @param backcolor * background color of mask * @param drawborder * boolean weather border drawn? * @param parent * parent container?? */ public clickablebox(int x, int y, int width, int height, color bordercolor, color backcolor, boolean drawborder, container parent) { this.x = x; this.y = y; this.width = width; this.height = height; this.backcolor = backcolor; this.bordercolor = bordercolor; this.drawborder = drawborder; this.parent = parent; }// end clickablebox() constructor public clickablebox(int x, int y, int width, int height, color bordercolor, color backcolor, container parent) { this.x = x; this.y = y; this.width = width; this.height = height; this.backcolor = backcolor; this.bordercolor = bordercolor; this.parent = parent; }// end clickablebox() constructor /** * method variable x * * @return variable x */ public int getx() { return x; }// end getx() /** * method used set variable x * * @param x * value x set */ public void setx(int x) { this.x = x; }// end setx() /** * method y variable * * @return returns variable y */ public int gety() { return y; }// end gety() /** * method used set variable y * * @param y * value y set */ public void sety(int y) { this.y = y; }// end sety() /** * method getwidth used width variable width * * @return returns variable width */ public int getwidth() { return width; }// end getwidth() /** * method setwidth used set width of clickable box * * @param width * value width set @ */ public void setwidth(int width) { this.width = width; }// end setwidth() /** * method getheight() used value of variable height * * @return returns value of height */ public int getheight() { return height; }// end getheight() /** * method setheight() used set height value * * @param height * height of clickable box */ public void setheight(int height) { this.height = height; }// end setheight() /** * method getbordercolor() used color of border * * @return returns color of border */ public color getbordercolor() { return bordercolor; }// end getbordercolor() /** * method setbordercolor() used set border's color * * @param bordercolor * color value used border's color */ public void setbordercolor(color bordercolor) { this.bordercolor = bordercolor; }// end getbordercolor() /** * method getbackcolor() used return background color of * clickable box * * @return returns background color */ public color getbackcolor() { return backcolor; }// end getbackcolor() /** * method setbackcolor() used set background color * * @param backcolor * color value used background color */ public void setbackcolor(color backcolor) { this.backcolor = backcolor; }// end setbackcolor() /** * method getoldcolor() used return previous color * * @return returns previous color */ public color getoldcolor() { return oldcolor; }// end getoldcolor() public void setoldcolor(color oldcolor) { this.oldcolor = oldcolor; } public boolean isdrawborder() { return drawborder; } public void setdrawborder(boolean drawborder) { this.drawborder = drawborder; } /** * method draw() used draw clickable box * * @param g * graphics variable */ public void draw(graphics g) { g.setcolor(backcolor); g.fillrect(x, y, width, height); g.setcolor(bordercolor); g.drawrect(x, y, width, height); } public void mousereleased(mouseevent e) { if (x < e.getx() && e.getx() < x + width && y < e.gety() && e.gety() < y + height) { clicked = true; parent.repaint(); } } public boolean isclicked() { return clicked; } public void setclicked(boolean clicked) { this.clicked = clicked; } }
i think problem clicked
never set false. want call setclicked(false) after have used information change state. otherwise, boxes stay in clicked state , toggled time...
Comments
Post a Comment