Java Reference
In-Depth Information
6 import javafx.scene.layout.GridPane;
7 import javafx.scene.layout.Pane;
8 import javafx.scene.paint.Color;
9 import javafx.scene.shape.Line;
10 import javafx.scene.shape.Ellipse;
11
12 public class TicTacToe extends Application {
13
main class TicTacToe
// Indicate which player has a turn, initially it is the X player
14
private char whoseTurn = 'X' ;
15
16
// Create and initialize cell
17
private Cell[][] cell = new Cell[ 3 ][ 3 ];
18
19
// Create and initialize a status label
20
private Label lblStatus = new Label( "X's turn to play" );
21
22 @Override // Override the start method in the Application class
23 public void start(Stage primaryStage) {
24 // Pane to hold cell
25 GridPane pane = new GridPane();
26 for ( int i = 0 ; i < 3 ; i++)
27 for ( int j = 0 ; j < 3 ; j++)
28 pane.add(cell[i][j] = new Cell(), j, i);
29
30 BorderPane borderPane = new BorderPane();
31 borderPane.setCenter(pane);
32 borderPane.setBottom(lblStatus);
33
34 // Create a scene and place it in the stage
35 Scene scene = new Scene(borderPane, 450 , 170 );
36 primaryStage.setTitle( "TicTacToe" ); // Set the stage title
37 primaryStage.setScene(scene); // Place the scene in the stage
38 primaryStage.show(); // Display the stage
39 }
40
41
hold nine cells
create a cell
tic-tac-toe cells in center
label at bottom
/** Determine if the cell are all occupied */
42
public boolean isFull() {
check isFull
43
for ( int i = 0 ; i < 3 ; i++)
44
for ( int j = 0 ; j < 3 ; j++)
45
if (cell[i][j].getToken() == ' ' )
46
return false ;
47
48
return true ;
49 }
50
51 /** Determine if the player with the specified token wins */
52 public boolean isWon( char token) {
53 for ( int i = 0 ; i < 3 ; i++)
54 if (cell[i][ 0 ].getToken() == token
55 && cell[i][ 1 ].getToken() == token
56 && cell[i][ 2 ].getToken() == token) {
57
check rows
return true ;
58 }
59
60 for ( int j = 0 ; j < 3 ; j++)
61 if (cell[ 0 ][j].getToken() == token
62 && cell[ 1 ][j].getToken() == token
63 && cell[ 2 ][j].getToken() == token) {
64
check columns
return true ;
65 }
 
 
Search WWH ::




Custom Search