Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. public class TicTacToeTester
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         //This is to help you test your methods. Feel free to add code at the end to check
  6.         //to see if your checkWin method works!
  7.         TicTacToe game = new TicTacToe();
  8.         System.out.println("Initial Game Board:");
  9.         game.printBoard();
  10.        
  11.         //Prints the first row of turns taken
  12.         for(int row = 0; row < 3; row++)
  13.         {
  14.             if(game.pickLocation(0, row))
  15.             {
  16.                 game.takeTurn(0, row);
  17.             }
  18.         }
  19.         System.out.println("\nAfter three turns:");
  20.         game.printBoard();
  21.        
  22.        
  23.    
  24.     }
  25. }
  26. public class TicTacToe
  27. {
  28.    //copy over your constructor from the Tic Tac Toe Board activity in the previous lesson!
  29.    private int turn;
  30.    private String[][] t = new String[3][3];
  31.    
  32.     public TicTacToe() {
  33.         for (int x = 0; x < t.length; x++) {
  34.             String[] empty = {"-", "-", "-"};
  35.             t[x] = empty;
  36.         }
  37.     }
  38.  
  39.    //this method returns the current turn
  40.    public int getTurn()
  41.    {
  42.        return turn;
  43.    }
  44.    
  45.    /*This method prints out the board array on to the console
  46.    */
  47.    public void printBoard()
  48.    {
  49.        System.out.println("  0 1 2");
  50.        for (int x = 0; x < t.length; x++) {
  51.             System.out.print(x + " ");
  52.             for (int y = 0; y < t[x].length; y++) {
  53.                 System.out.print(t[x][y] + " ");
  54.             }
  55.             System.out.println();
  56.        }
  57.        
  58.    }
  59.    
  60.    //This method returns true if space row, col is a valid space
  61.    public boolean pickLocation(int row, int col)
  62.    {
  63.         if (t[row][col].equals("-")) return true;
  64.         return false;
  65.    }
  66.    
  67.    //This method places an X or O at location row,col based on the int turn
  68.    public void takeTurn(int row, int col)
  69.    {
  70.            if (pickLocation(row, col) && turn%2 == 0) {
  71.                t[row][col] = "X";
  72.                turn++;
  73.            }
  74.            else if (pickLocation(row, col) && turn%2 != 0) {
  75.                t[row][col] = "O";
  76.                turn++;
  77.            }
  78.    }
  79.    
  80.    //This method returns a boolean that returns true if a row has three X or O's in a row
  81.    public boolean checkRow()
  82.    {
  83.        for (int x = 0; x < t[x].length - 1; x++) {
  84.                if (t[x][0].equals(t[x][1]) && t[x][0].equals(t[x][2])) return true;
  85.        }
  86.        return false;
  87.    }
  88.    
  89.     //This method returns a boolean that returns true if a col has three X or O's
  90.    public boolean checkCol()
  91.    {
  92.         for (int x = 0; x < t.length; x++)
  93.         {
  94.             if (t[0][x].equals(t[1][x]) && t[0][x].equals(t[2][x])) return true;
  95.         }
  96.         return false;
  97.    }
  98.    
  99.     //This method returns a boolean that returns true if either diagonal has three X or O's
  100.    public boolean checkDiag()
  101.    {
  102.         if (t[0][0].equals(t[1][1]) && t[0][0].equals(t[2][2])) return true;
  103.         else if (t[0][2].equals(t[1][1]) && t[0][2].equals(t[2][0])) return true;
  104.         return false;
  105.  
  106.    }
  107.    
  108.    //This method returns a boolean that checks if someone has won the game
  109.    public boolean checkWin()
  110.    {
  111.        if (checkRow() || checkCol() || checkDiag()) return true;
  112.        return false;
  113.    }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement