Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TicTacToe
- {
- public static char[][] board = new char[3][3];
- public Scanner keyboard;
- public static void writeBoard()
- {
- System.out.println("-----------------");
- System.out.println("|R/C| 1 | 2 | 3 |");
- System.out.println("-----------------");
- for (int i = 0; i < 3; i++) //i is the row number
- {
- System.out.println("| " + (i + 1) + " | " + board[i][0] + " | " + board[i][1] + " | " + board[i][2] + " |");
- System.out.println("-----------------");
- }
- }
- /*Postcondition:
- Prints the TicTacToe board to screen with user moves in the correct rows and columns*/
- /*Precondition:
- The board has been written and a new game has been started or O has just placed their move*/
- public static void getMoveX()
- {
- System.out.println("X's turn.");
- System.out.println("Where would you like your X placed?");
- System.out.println("Please enter row number and column number separated by a space.");
- Scanner keyboard = new Scanner(System.in);
- String rowcol = keyboard.nextLine(); //user input assigned to rowcol
- int row = rowcol.charAt(0);
- int column = rowcol.charAt(2);
- System.out.println("You have entered row #" + (row - 48) + " and column #" + (column - 48)); /*prints the row and column that the user entered*/
- if (board[(row - 49)][(column - 49)] != '\0') /*if the spot entered by the user is taken...*/
- {
- System.out.println("That cell is already taken.");
- System.out.println("Please make another selection.");
- getMoveX();
- }
- else //if the spot is not taken...
- {
- System.out.println("Thank you for your selection.");
- board[row - 49][column - 49] = 'X'; //X assigned to user row and column
- winner(); //checks to see if X is winner
- }
- }
- /*Postcondition:
- Gets move from player X and places it in the correct spot on the board, prints board to screen*/
- /*Precondition:
- X has moved and has chosen a valid spot, there is no winner yet*/
- public static void getMoveO()
- {
- System.out.println("O's turn.");
- System.out.println("Where would you like your O placed?");
- System.out.println("Please enter row number and column number separated by a space.");
- Scanner keyboard = new Scanner(System.in);
- String rowcol1 = keyboard.nextLine(); //user input assigned to rowcol1
- int row1 = rowcol1.charAt(0);
- int column1 = rowcol1.charAt(2);
- System.out.println("You have entered row #" + (row1 - 48) + " and column #" + (column1 - 48)); /*prints the users row and column number*/
- row1--;
- column1--;
- if (board[row1 - 48][column1 - 48] != '\0') /*if the row and column are taken...*/
- {
- System.out.println("That cell is already taken.");
- System.out.println("Please make another selection.");
- getMoveO(); //starts method over
- }
- else /*if row and column are not taken...*/
- {
- System.out.println("Thank you for your selection.");
- board[row1 - 48][column1 - 48] = 'O'; //O placed in designated row and column
- winner(); //checks for winner
- }
- }
- /*Precondition:
- X or O have placed their move*/
- public static boolean winner()
- {
- boolean winner;
- /*checks rows to see if X is winner*/
- for (int i = 0; i < 3; i++)
- {
- if ((board[i][0] == 'X') && (board[i][0] == board[i][1]))
- {
- if (board[i][0] == board[i][2])
- {
- winner = true;
- System.out.println(board[i][0] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- else
- winner = false;
- }
- /*checks columns to see if X is winner*/
- for (int i = 0; i < 3; i++)
- {
- if ((board[0][i] == 'X') && (board[0][i] == board[1][i]))
- {
- if (board[0][i] == board[2][i])
- {
- winner = true;
- System.out.println(board[0][i] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- else
- winner = false;
- }
- /*checks diagonals to see if X is winner*/
- if ((board[0][0] == 'X') && (board[0][0] == board[1][1]))
- {
- if (board[0][0] == board[2][2])
- {
- winner = true;
- System.out.println(board[0][0] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- if ((board[0][2] == 'X') && (board[0][2] == board[1][1]))
- {
- if (board[1][1] == board[2][0])
- {
- winner = true;
- System.out.println(board[0][2] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- /*checks rows to see if O is winner*/
- for (int i = 0; i < 3; i++)
- {
- if ((board[i][0] == 'O') && (board[i][0] == board[i][1]))
- {
- if (board[i][0] == board[i][2])
- {
- winner = true;
- System.out.println(board[i][0] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- else
- winner = false;
- }
- /*checks columns to see if O is winner*/
- for (int i = 0; i < 3; i++)
- {
- if ((board[0][i] == 'O') && (board[0][i] == board[1][i]))
- {
- if (board[0][i] == board[2][i])
- {
- winner = true;
- System.out.println(board[0][i] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- else
- winner = false;
- }
- /*checks diagonals to see if O is winner*/
- if ((board[0][0] == 'O') && (board[0][0] == board[1][1]))
- {
- if (board[0][0] == board[2][2])
- {
- winner = true;
- System.out.println(board[0][0] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- if ((board[0][2] == 'O') && (board[0][2] == board[1][1]))
- {
- if (board[1][1] == board[2][0])
- {
- winner = true;
- System.out.println(board[0][2] + " IS THE WINNER!");
- writeBoard();
- System.exit(0);
- }
- else
- winner = false;
- }
- else
- winner = false;
- return winner; //true or false
- }
- public static void newGame()
- {
- /*assigns null characters to all indexes in the array*/
- for (int i = 0; i < 3; i++)
- {
- board[i][0] = '\0';
- board[i][1] = '\0';
- board[i][2] = '\0';
- }
- System.out.println("New Game: X goes first.");
- }
- /*Postcondition:
- Prints board to screen with blank board*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement