Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TicTacToe1D{
- //Declare and initialize a static String array with 9 spots. Name it board.
- private static String[] board = new String[9];
- //Write a static method named initBoard to initialize all elements in board to spaces.
- //The method receives no parameters and returns nothing.
- public static void initBoard(){
- for(int i = 0; i < board.length; i++){//can't use enhanced because need to change things
- board[i] = " ";
- }
- }
- //Write a static method named displayBoard to print out the board in TicTacToe board style
- //The method receives no parameters and returns nothing.
- //Ex:
- // X| |O
- // -+-+-
- // O|X|O
- // -+-+-
- // X|O|X
- public static void displayBoard(){
- String divider = "-+-+-";
- System.out.println(board[0] + "|" + board[1] + "|" + board[2]);
- System.out.println( divider );
- System.out.println(board[3] + "|" + board[4] + "|" + board[5]);
- System.out.println( divider );
- System.out.println(board[6] + "|" + board[7] + "|" + board[8]);
- }
- //Write a static method to do a move and name it move
- //The method takes in a String representing the player (O/X)
- //The method takes in an int representing the spot
- //The method returns true if valid move (legal index and currently occupied by space), false otherwise
- public static boolean move(String player, int spot){
- if( spot < 0 || spot > 8 || !board[spot].equals(" ") ) return false;
- else{
- board[spot] = player;
- return true;
- }
- }
- //Write a method called diagWin to check diagonals for win - return true if there is a winner, false otherwise
- //The method receives no parameters
- public static boolean diagWin(){
- if( !board[0].equals(" ") && board[0].equals( board[4] ) && board[4].equals( board[8] ) ) return true;
- else if( !board[2].equals(" ") && board[2].equals( board[4] ) && board[4].equals( board[6] ) ) return true;
- else return false;
- }
- //Write a method called vertWin to check verticals for win - return true if there is a winner, false otherwise
- //The method receives no parameters
- public static boolean vertWin(){
- for( int i = 0; i <= 2; i++ ){
- if( !board[i].equals(" ") && board[i].equals( board[i+3] ) && board[i+3].equals( board[i+6] ) ) return true;
- }
- return false;
- }
- //Write a method called horizWin to check horizontals for win - return true if there is a winner, false otherwise
- //The method receives no parameters
- public static boolean horizWin(){
- for(int i = 0; i <= 6; i += 3){
- if( !board[i].equals(" ") && board[i].equals( board[i+1] ) && board[i+1].equals( board[i+2] ) ) return true;
- }
- return false;
- }
- //Write a method called checkWin - if diagWin or vertWin or horizWin is true, return true, false otherwise
- //The method receives no parameters
- public static boolean checkWin(){
- return diagWin() || vertWin() || horizWin();
- }
- //Write a main method to play the game.
- public static void main(String[] args){
- Scanner kb = new Scanner(System.in);
- TicTacToe1D.initBoard();
- int turns = 0;
- String player = "";
- while( turns < 9 && checkWin() == false ){
- TicTacToe1D.displayBoard();
- System.out.print("Player (X/O): ");
- player = kb.next();
- System.out.print("Spot #: ");
- int spot = kb.nextInt();
- if( TicTacToe1D.move(player,spot) ){
- System.out.println("Next player's turn");
- turns++;
- }
- else System.out.println("INVALID MOVE. Try again.");
- }
- if( checkWin() == true ) System.out.println( player + " WINS!" );
- else System.out.println( "Cat's Game" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement