Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class TicTacToe {
- private char[][] board;
- private boolean xTurn;
- private Scanner keyboard;
- public TicTacToe() {
- board = new char[3][3];
- for(int r = 0; r < 3; r++) {
- for(int c = 0; c < 3; c++)
- board[r][c] = ' ';
- }
- xTurn = true;
- keyboard = new Scanner(System.in);
- }
- private void displayRow(int row) {
- System.out.println(" " + board[row][0] + " | " + board[row][1] + " | " + board[row][2]);
- }
- private void displayBoard() {
- displayRow(0);
- System.out.println("-----------");
- displayRow(1);
- System.out.println("-----------");
- displayRow(2);
- }
- private void displayMenu() {
- if(xTurn)
- System.out.println("X's Turn!");
- else
- System.out.println("O's Turn!");
- }
- private boolean getMove() {
- boolean invalid = true;
- int row = 0, column = 0;
- while(invalid) {
- System.out.println("Which row, column from 0-2 would you like to choose");
- row = keyboard.nextInt();
- column = keyboard.nextInt();
- if(row >= 0 && row <= 2 && column >= 0 && column <= 2) {
- if(board[row][column] != ' ')
- System.out.println("That position is already taken");
- else
- invalid = false;
- }
- else
- System.out.println("Invalid position");
- }
- if(xTurn)
- board[row][column] = 'X';
- else
- board[row][column] = 'O';
- return winner(row,column);
- }
- private void restart() {
- for(int r = 0; r < 3; r++) {
- for(int c = 0; c < 3; c++)
- board[r][c] = ' ';
- }
- //reset whose turn it is
- xTurn = true;
- }
- private boolean winner(int lastR, int lastC) {
- boolean winner = false; //assume there's no winner
- char symbol = board[lastR][lastC]; //the symbol for the last move either X or O
- int numFound = 0;
- for(int c = 0; c < 3; c++) {
- if(board[lastR][c] == symbol)
- numFound++;
- }
- if(numFound == 3)
- winner = true;
- numFound = 0;
- for(int r = 0; r < 3; r++) {
- if(board[r][lastC] == symbol)
- numFound++;
- }
- if(numFound == 3)
- winner = true;
- numFound = 0;
- for(int i = 0; i < 3; i++) {
- if(board[i][i] == symbol)
- numFound++;
- }
- if(numFound == 3)
- winner = true;
- numFound = 0;
- for(int i = 0; i < 3; i++) {
- if(board[i][2-i] == symbol)
- numFound++;
- }
- if(numFound == 3)
- winner = true;
- return winner;
- }
- private boolean boardFull() {
- int numSpotsFilled = 0;
- for(int r = 0; r < 3; r++) {
- for(int c = 0; c < 3; c++) {
- if(board[r][c] == 'X' || board[r][c] == 'O')
- numSpotsFilled++;
- }
- }
- return numSpotsFilled == 9;
- }
- public void play() {
- while(true) {
- displayBoard();
- displayMenu();
- int choice = keyboard.nextInt();
- if(choice == 1)
- {
- if(getMove()) {
- displayBoard();
- if(xTurn)
- System.out.println("X Wins!");
- else
- System.out.println("O Wins!");
- }
- else if(boardFull()) {
- displayBoard();
- System.out.println("Draw!");
- System.exit(0);
- }
- else {
- xTurn = !xTurn;
- }
- }
- else if(choice == 2)
- restart();
- else if(choice == 3)
- System.exit(0);
- else
- System.out.println("Invalid Option");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement