javafx - Tic Tac Toe Program -
i have tictactoe game , instructor wanted make wild tic tac toe. user needs able select if want x or o whenever. unsure of how this. have buttons need assign them actions, can please me out , how can make them play again? here tictactoe class. thank in advance.
import javafx.application.application; import javafx.stage.stage; import javafx.scene.scene; import javafx.scene.control.label; import javafx.scene.layout.borderpane; import javafx.scene.layout.gridpane; import javafx.scene.layout.pane; import javafx.scene.paint.color; import javafx.scene.shape.line; import javafx.scene.shape.ellipse; import javafx.scene.control.button; import javafx.scene.layout.stackpane; import javafx.scene.layout.vbox; import javafx.scene.layout.hbox; import javafx.geometry.pos; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.application.platform; public class tictactoe extends application { private char whoseturn = 'x'; private cell[][] cell = new cell[3][3]; private label lblstatus = new label("x's turn play"); @override public void start(stage primarystage) { button xbutton = new button("x"); button obutton = new button("o"); button exit = new button("exit"); button play = new button("play again?"); xbutton.setstyle("-fx-font-size: 15pt;"); obutton.setstyle("-fx-font-size: 15pt;"); exit.setstyle("-fx-font-size: 15pt;"); play.setstyle("-fx-font-size: 15pt;"); exit.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { platform.exit(); } }); gridpane pane = new gridpane(); (int = 0; < 3; i++) (int j = 0; j < 3; j++) pane.add(cell[i][j] = new cell(), j, i); borderpane borderpane = new borderpane(); borderpane.setcenter(pane); borderpane.setbottom(lblstatus); hbox buttonbar = new hbox(); buttonbar.setspacing(87.0); buttonbar.getchildren().addall(xbutton, obutton, play,exit); borderpane.settop(buttonbar); scene scene = new scene(borderpane, 550, 470); primarystage.settitle("wild tictactoe"); primarystage.setscene(scene); primarystage.show(); primarystage.setresizable(false); } /** determine if cell occupied */ public boolean isfull() { (int = 0; < 3; i++) (int j = 0; j < 3; j++) if (cell[i][j].gettoken() == ' ') return false; return true; } /** determine if player specified token wins */ public boolean iswon(char token) { (int = 0; < 3; i++) if (cell[i][0].gettoken() == token && cell[i][1].gettoken() == token && cell[i][2].gettoken() == token) { return true; } (int j = 0; j < 3; j++) if (cell[0][j].gettoken() == token && cell[1][j].gettoken() == token && cell[2][j].gettoken() == token) { return true; } if (cell[0][0].gettoken() == token && cell[1][1].gettoken() == token && cell[2][2].gettoken() == token) { return true; } if (cell[0][2].gettoken() == token && cell[1][1].gettoken() == token && cell[2][0].gettoken() == token) { return true; } return false; } public class cell extends pane { private char token = ' '; public cell() { setstyle("-fx-border-color: black"); this.setprefsize(2000, 2000); this.setonmouseclicked(e -> handlemouseclick()); } /** return token */ public char gettoken() { return token; } /** set new token */ public void settoken(char c) { token = c; if (token == 'x') { line line1 = new line(10, 10, this.getwidth() - 10, this.getheight() - 10); line1.endxproperty().bind(this.widthproperty().subtract(10)); line1.endyproperty().bind(this.heightproperty().subtract(10)); line line2 = new line(10, this.getheight() - 10, this.getwidth() - 10, 10); line2.startyproperty().bind( this.heightproperty().subtract(10)); line2.endxproperty().bind(this.widthproperty().subtract(10)); // add lines pane this.getchildren().addall(line1, line2); } else if (token == 'o') { ellipse ellipse = new ellipse(this.getwidth() / 2, this.getheight() / 2, this.getwidth() / 2 - 10, this.getheight() / 2 - 10); ellipse.centerxproperty().bind( this.widthproperty().divide(2)); ellipse.centeryproperty().bind( this.heightproperty().divide(2)); ellipse.radiusxproperty().bind( this.widthproperty().divide(2).subtract(10)); ellipse.radiusyproperty().bind( this.heightproperty().divide(2).subtract(10)); ellipse.setstroke(color.black); ellipse.setfill(color.white); getchildren().add(ellipse); // add ellipse pane } } /* handle mouse click event */ private void handlemouseclick() { // if cell empty , game not on if (token == ' ' && whoseturn != ' ') { settoken(whoseturn); // set token in cell // check game status if (iswon(whoseturn)) { lblstatus.settext(whoseturn + " won! game over. play again?"); whoseturn = ' '; // game on } else if (isfull()) { lblstatus.settext("draw! game over. play again?"); whoseturn = ' '; // game on } else { // change turn whoseturn = (whoseturn == 'x') ? 'o' : 'x'; // display turn lblstatus.settext(whoseturn + "'s turn."); } } } } /** * main method needed ide limited * javafx support. not needed running command line. */ public static void main(string[] args) { launch(args); } }
if want allow player choose piece (x
or o
), easiest way decouple presentation use of pieces.
by that, mean have player represented within program integer 1
, computer 0
. never changes , logic uses these values.
when displaying board however, translate value correct form.
so, 2 things. first, allow user choose pieces (psuedo-code):
def piece array[2] computer = 0 player = 1 if playerwantstobex: piece[computer] = 'o' piece[player] = 'x' else: piece[computer] = 'x' piece[player] = 'o'
then, when presenting board, use values. whereas before have had like:
print cell[x,y]
now have:
print piece[cell[x,y]]
Comments
Post a Comment