Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tictactoesimulator;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Random;
- import java.util.ResourceBundle;
- import java.util.stream.IntStream;
- import javafx.event.ActionEvent;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.*;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- /**
- *
- * @author Compsci
- */
- public class FXMLDocumentController implements Initializable {
- @FXML
- private Label label;
- @FXML
- private ImageView topLeft;
- @FXML
- private ImageView topMid;
- @FXML
- private ImageView topRight;
- @FXML
- private ImageView midLeft;
- @FXML
- private ImageView midMid;
- @FXML
- private ImageView midRight;
- @FXML
- private ImageView bottomLeft;
- @FXML
- private ImageView bottomMid;
- @FXML
- private ImageView bottomRight;
- private String[] xo = {"file:C:/Users/Compsci/Desktop/dice/o.PNG", "file:C:/Users/Compsci/Desktop/dice/x.PNG"};
- private String[] xoString = {"O", "X"};
- private Random rand = new Random();
- private ImageView[][] board = new ImageView[3][3];
- String[][] boardArray = new String[3][3];
- @FXML
- private void handleButtonAction(ActionEvent event) {
- //System.out.println("You clicked me!");
- generateGame();
- printBoard();
- if (checkWinner().equals("Draw"))
- label.setText("It's a draw!");
- else
- label.setText(checkWinner() + " wins");
- }
- public void generateGame() {
- board[0][0] = topLeft;
- board[0][1] = topMid;
- board[0][2] = topRight;
- board[1][0] = midLeft;
- board[1][1] = midMid;
- board[1][2] = midRight;
- board[2][0] = bottomLeft;
- board[2][1] = bottomMid;
- board[2][2] = bottomRight;
- for (int i = 0; i < boardArray.length; i++) {
- for (int j = 0; j < boardArray[0].length; j++) {
- int play = rand.nextInt(2);
- boardArray[i][j] = xoString[play];
- board[i][j].setImage(new Image(xo[play]));
- }
- }
- }
- public void printBoard() {
- for (int i = 0; i < boardArray.length; i++) {
- for (int j = 0; j < boardArray[0].length; j++) {
- System.out.print(boardArray[i][j] + " ");
- }
- System.out.println();
- }
- }
- public String checkWinner() {
- String winner = "Draw";
- for (int i = 0; i < 3; i++){
- if (boardArray[i][0] == boardArray[i][1] && boardArray[i][1] == boardArray[i][2]) {
- winner = boardArray[i][0];
- }
- }
- for (int i = 0; i < 3; i++){
- if (boardArray[0][i] == boardArray[1][i] && boardArray[1][i] == boardArray[2][i]) {
- winner = boardArray[0][i];
- }
- }
- if (boardArray[0][0] == boardArray[1][1] && boardArray[1][1] == boardArray[2][2])
- winner = boardArray[0][0];
- else if (boardArray[0][2] == boardArray[1][1] && boardArray[1][1] == boardArray[2][0])
- winner = boardArray[0][2];
- return winner;
- }
- @Override
- public void initialize(URL url, ResourceBundle rb) {
- // TODO
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement