Advertisement
Nick-O-Rama

tictactoe

Sep 11th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package tictactoesimulator;
  7.  
  8. import java.net.URL;
  9. import java.util.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.Random;
  12. import java.util.ResourceBundle;
  13. import java.util.stream.IntStream;
  14. import javafx.event.ActionEvent;
  15. import javafx.fxml.FXML;
  16. import javafx.fxml.Initializable;
  17. import javafx.scene.control.*;
  18. import javafx.scene.image.Image;
  19. import javafx.scene.image.ImageView;
  20.  
  21. /**
  22.  *
  23.  * @author Compsci
  24.  */
  25. public class FXMLDocumentController implements Initializable {
  26.    
  27.     @FXML
  28.     private Label label;
  29.     @FXML
  30.     private ImageView topLeft;
  31.     @FXML
  32.     private ImageView topMid;
  33.     @FXML
  34.     private ImageView topRight;
  35.     @FXML
  36.     private ImageView midLeft;    
  37.     @FXML
  38.     private ImageView midMid;    
  39.     @FXML
  40.     private ImageView midRight;
  41.     @FXML
  42.     private ImageView bottomLeft;    
  43.     @FXML
  44.     private ImageView bottomMid;    
  45.     @FXML
  46.     private ImageView bottomRight;  
  47.    
  48.     private String[] xo = {"file:C:/Users/Compsci/Desktop/dice/o.PNG", "file:C:/Users/Compsci/Desktop/dice/x.PNG"};
  49.     private String[] xoString = {"O", "X"};
  50.     private Random rand = new Random();
  51.     private ImageView[][] board = new ImageView[3][3];
  52.     String[][] boardArray = new String[3][3];
  53.     @FXML
  54.     private void handleButtonAction(ActionEvent event) {
  55.         //System.out.println("You clicked me!");
  56.         generateGame();
  57.         printBoard();
  58.         if (checkWinner().equals("Draw"))
  59.             label.setText("It's a draw!");
  60.         else
  61.             label.setText(checkWinner() + " wins");
  62.     }    
  63.    
  64.     public void generateGame() {
  65.         board[0][0] = topLeft;
  66.         board[0][1] = topMid;
  67.         board[0][2] = topRight;
  68.         board[1][0] = midLeft;
  69.         board[1][1] = midMid;
  70.         board[1][2] = midRight;
  71.         board[2][0] = bottomLeft;
  72.         board[2][1] = bottomMid;
  73.         board[2][2] = bottomRight;
  74.         for (int i = 0; i < boardArray.length; i++) {
  75.             for (int j = 0; j < boardArray[0].length; j++) {
  76.                 int play = rand.nextInt(2);
  77.                 boardArray[i][j] = xoString[play];
  78.                 board[i][j].setImage(new Image(xo[play]));
  79.             }
  80.         }
  81.     }
  82.     public void printBoard() {
  83.         for (int i = 0; i < boardArray.length; i++) {
  84.             for (int j = 0; j < boardArray[0].length; j++) {
  85.                 System.out.print(boardArray[i][j] + " ");
  86.             }
  87.             System.out.println();
  88.         }
  89.     }
  90.    
  91.     public String checkWinner() {
  92.         String winner = "Draw";
  93.         for (int i = 0; i < 3; i++){
  94.             if (boardArray[i][0] == boardArray[i][1] && boardArray[i][1] == boardArray[i][2]) {
  95.                 winner = boardArray[i][0];
  96.             }  
  97.         }
  98.         for (int i = 0; i < 3; i++){
  99.             if (boardArray[0][i] == boardArray[1][i] && boardArray[1][i] == boardArray[2][i]) {
  100.                 winner = boardArray[0][i];
  101.             }  
  102.         }
  103.         if (boardArray[0][0] == boardArray[1][1] && boardArray[1][1] == boardArray[2][2])
  104.             winner = boardArray[0][0];
  105.         else if (boardArray[0][2] == boardArray[1][1] && boardArray[1][1] == boardArray[2][0])
  106.             winner = boardArray[0][2];
  107.         return winner;
  108.     }
  109.    
  110.    
  111.     @Override
  112.     public void initialize(URL url, ResourceBundle rb) {
  113.         // TODO
  114.     }    
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement