Advertisement
fosterbl

TicTacToe1D.java - Complete

Dec 10th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TicTacToe1D{
  4.    //Declare and initialize a static String array with 9 spots. Name it board.
  5.    private static String[] board = new String[9];
  6.    //Write a static method named initBoard to initialize all elements in board to spaces.
  7.    //The method receives no parameters and returns nothing.
  8.    public static void initBoard(){
  9.       for(int i = 0; i < board.length; i++){//can't use enhanced because need to change things
  10.          board[i] = " ";
  11.       }
  12.    }
  13.    //Write a static method named displayBoard to print out the board in TicTacToe board style
  14.    //The method receives no parameters and returns nothing.
  15.    //Ex:
  16.    // X| |O
  17.    // -+-+-
  18.    // O|X|O
  19.    // -+-+-
  20.    // X|O|X
  21.    public static void displayBoard(){
  22.       String divider = "-+-+-";
  23.       System.out.println(board[0] + "|" + board[1] + "|" + board[2]);
  24.       System.out.println( divider );
  25.       System.out.println(board[3] + "|" + board[4] + "|" + board[5]);
  26.       System.out.println( divider );
  27.       System.out.println(board[6] + "|" + board[7] + "|" + board[8]);
  28.    }
  29.    
  30.    //Write a static method to do a move and name it move
  31.    //The method takes in a String representing the player (O/X)
  32.    //The method takes in an int representing the spot
  33.    //The method returns true if valid move (legal index and currently occupied by space), false otherwise
  34.    public static boolean move(String player, int spot){
  35.       if( spot < 0 || spot > 8 || !board[spot].equals(" ") ) return false;
  36.       else{
  37.          board[spot] = player;
  38.          return true;
  39.       }
  40.    }
  41.    
  42.    //Write a method called diagWin to check diagonals for win - return true if there is a winner, false otherwise
  43.    //The method receives no parameters
  44.    public static boolean diagWin(){
  45.       if( !board[0].equals(" ") && board[0].equals( board[4] ) && board[4].equals( board[8] ) ) return true;
  46.       else if( !board[2].equals(" ") && board[2].equals( board[4] ) && board[4].equals( board[6] ) ) return true;
  47.       else return false;
  48.    }
  49.    
  50.    //Write a method called vertWin to check verticals for win - return true if there is a winner, false otherwise
  51.    //The method receives no parameters
  52.    public static boolean vertWin(){
  53.       for( int i = 0; i <= 2; i++ ){
  54.          if( !board[i].equals(" ") && board[i].equals( board[i+3] ) && board[i+3].equals( board[i+6] ) ) return true;
  55.       }
  56.       return false;
  57.    }
  58.    
  59.    //Write a method called horizWin to check horizontals for win - return true if there is a winner, false otherwise
  60.    //The method receives no parameters
  61.    public static boolean horizWin(){
  62.       for(int i = 0; i <= 6; i += 3){
  63.          if( !board[i].equals(" ") && board[i].equals( board[i+1] ) && board[i+1].equals( board[i+2] ) ) return true;
  64.       }
  65.       return false;
  66.    }
  67.    
  68.    //Write a method called checkWin - if diagWin or vertWin or horizWin is true, return true, false otherwise
  69.    //The method receives no parameters
  70.    public static boolean checkWin(){
  71.       return diagWin() || vertWin() || horizWin();
  72.    }
  73.    
  74.    //Write a main method to play the game.
  75.    public static void main(String[] args){
  76.       Scanner kb = new Scanner(System.in);
  77.       TicTacToe1D.initBoard();
  78.       int turns = 0;
  79.       String player = "";
  80.       while( turns < 9 && checkWin() == false ){
  81.          TicTacToe1D.displayBoard();
  82.          System.out.print("Player (X/O): ");
  83.          player = kb.next();
  84.          System.out.print("Spot #: ");
  85.          int spot = kb.nextInt();
  86.          if( TicTacToe1D.move(player,spot) ){
  87.             System.out.println("Next player's turn");
  88.             turns++;
  89.          }
  90.          else System.out.println("INVALID MOVE. Try again.");
  91.       }
  92.       if( checkWin() == true ) System.out.println( player + " WINS!" );
  93.       else System.out.println( "Cat's Game" );
  94.    }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement