Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TicTacToe {
- public static char[][] board = new char[3][3];
- static Scanner scan = new Scanner(System.in);
- public static String keepPlaying;
- public static char yesOrNo;
- public static boolean playing;
- public static int row, col;
- public static User human1 = new User();
- public static User human2 = new User();
- public static void main(String[] args) {
- human1.getUser1Name();
- human2.getUser2Name();
- System.out.println("nPlayer 1: " + human1.human1Name() + ": X's");
- System.out.println("Player 2: " + human2.human2Name() + ": O'sn");
- initializeBoard();
- displayBoard();
- playing = true;
- while (playing == true)
- {
- System.out.println("n" + human1.human1Name() + " it's your turn.");
- human1.getChoice();
- human1.convert();
- if (human1.returnRow() == 0 && human1.returnCol() == 0/* && board[0][0] == ' '*/)
- {
- board[0][0] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 0 && human1.returnCol() == 1 && board[1][0] == ' ')
- {
- board[1][0] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 0 && human1.returnCol() == 2 && board[2][0] == ' ')
- {
- board[2][0] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 1 && human1.returnCol() == 0 && board[0][1] == ' ')
- {
- board[0][1] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 1 && human1.returnCol() == 1 && board[1][1] == ' ')
- {
- board[1][1] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 1 && human1.returnCol() == 2 && board[2][1] == ' ')
- {
- board[2][1] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 2 && human1.returnCol() == 0 && board[0][2] == ' ')
- {
- board[0][2] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 2 && human1.returnCol() == 1 && board[1][2] == ' ')
- {
- board[1][2] = 'X';
- displayBoard();
- }
- else if (human1.returnRow() == 2 && human1.returnCol() == 2 && board[2][2] == ' ')
- {
- board[2][2] = 'X';
- displayBoard();
- }
- if (checkForWinner() == true)
- {
- System.out.println("n" + human1.human1Name() + " has won the game!!!");
- playAgain();
- if (playing == false)
- {
- break;
- }
- }
- if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' ' && board[1][1] != ' '
- && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
- {
- System.out.println("It's a draw!!!");
- playAgain();
- if (playing == false)
- {
- break;
- }
- }
- System.out.println("n" + human2.human2Name() + " it's your turn.");
- human2.getChoice();
- human2.convert();
- if (human2.returnRow() == 0 && human2.returnCol() == 0 /*&& board[0][0] == ' '*/)
- {
- board[0][0] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 0 && human2.returnCol() == 1 && board[1][0] == ' ')
- {
- board[1][0] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 0 && human2.returnCol() == 2 && board[2][0] == ' ')
- {
- board[2][0] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 1 && human2.returnCol() == 0 && board[0][1] == ' ')
- {
- board[0][1] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1] == ' ')
- {
- board[1][1] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 1 && human2.returnCol() == 2 && board[2][1] == ' ')
- {
- board[2][1] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 2 && human2.returnCol() == 0 && board[0][2] == ' ')
- {
- board[0][2] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 2 && human2.returnCol() == 1 && board[1][2] == ' ')
- {
- board[1][2] = 'O';
- displayBoard();
- }
- else if (human2.returnRow() == 2 && human2.returnCol() == 2 && board[2][2] == ' ')
- {
- board[2][2] = 'O';
- displayBoard();
- }
- if (checkForWinner() == true)
- {
- System.out.println("n" + human2.human2Name() + " has won the game!!!");
- playAgain();
- }
- if (board[0][0] != ' ' && board[0][1] != ' ' && board[0][2] != ' ' && board[1][0] != ' ' && board[1][1] != ' '
- && board[1][2] != ' ' && board[2][0] != ' ' && board[2][1] != ' ' && board[2][2] != ' ')
- {
- System.out.println("It's a draw!!!");
- playAgain();
- }
- }
- }
- public TicTacToe() {
- board = new char[3][3];
- }
- public static void initializeBoard() {
- for (int row = 0; row < 3; row++)
- {
- for (int col = 0; col < 3; col++)
- {
- board[row][col] = ' ';
- }
- }
- }
- public static void displayBoard() {
- System.out.println(" A B C");
- System.out.println(" -------------");
- for (int row = 0; row < 3; row++)
- {
- System.out.print((row + 1) + " | ");
- for (int col = 0; col < 3; col++)
- {
- System.out.print(board[row][col] + " | ");
- }
- System.out.println();
- System.out.println(" -------------");
- }
- }
- public static boolean checkForWinner() {
- if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][1] != ' ')
- {
- return true;
- }
- if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][1] != ' ')
- {
- return true;
- }
- if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][1] != ' ')
- {
- return true;
- }
- if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[1][0] != ' ')
- {
- return true;
- }
- if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[1][1] != ' ')
- {
- return true;
- }
- if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[1][2] != ' ')
- {
- return true;
- }
- if (board[0][0] == board[1][1] && board [1][1] == board[2][2] && board[1][1] != ' ')
- {
- return true;
- }
- if (board[0][2] == board [1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
- {
- return true;
- }
- return false;
- }
- public static boolean playAgain() {
- System.out.println("nWould you like to play again?");
- keepPlaying = scan.nextLine();
- yesOrNo = keepPlaying.toUpperCase().charAt(0);
- if (yesOrNo == 'N')
- {
- System.out.println("nThanks for playing!");
- playing = false;
- initializeBoard();
- return playing;
- }
- else if (yesOrNo == 'Y')
- {
- initializeBoard();
- displayBoard();
- return true;
- }
- return playing;
- }
- public static boolean notValid() {
- if (human1.returnRow() > 2 || human1.returnRow() < 0 || human1.returnCol() > 2 || human1.returnCol() < 0 || human2.returnRow() > 2
- || human2.returnRow() < 0 || human2.returnCol() > 2 || human2.returnCol() < 0 || !isEmpty())
- {
- System.out.println("Invalid");
- return true;
- }
- else
- {
- return false;
- }
- }
- public static boolean isEmpty() {
- if (board[0][0] == ' ' && board[0][1] == ' ' && board[0][2] == ' ' && board[1][0] == ' ' && board[1][1] == ' '
- && board[1][2] == ' ' && board[2][0] == ' ' && board[2][1] == ' ' && board[2][2] == ' ')
- {
- return true;
- }
- else
- {
- System.out.println("That position is taken.n");
- return false;
- }
- }
- else if (human2.returnRow() == 1 && human2.returnCol() == 1 && board[1][1]==' ')
- {
- board[1][1] = 'O';
- displayBoard();
- }
- boolean turnOver = false;
- while(!turnOver){
- if(board[human2.returnRow()][human2.returnCol()] != ' '){
- System.out.println("That space is taken")
- }else{
- if(human2.returnRow() == 1 && human2.returnCol() == 1){
- board[1][1] = 'O';
- displayBoard();
- }
- //other cases
- turnOver = true;
- }
Add Comment
Please, Sign In to add comment