Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class TicTacToe {
- public static char X = 'X';
- public static char O = 'O';
- public static char B = ' ';
- public static void main(String[] args) {
- /*
- * Squares are associated with following variables.
- * 1 2 3
- * 1 | a | b | c |
- * 2 | d | e | f |
- * 3 | g | h | i |
- */
- // Initialize squares to blank.
- char a = B, b = B, c = B, d = B, e = B, f = B, g = B, h = B, i = B;
- Scanner input = new Scanner(System.in);
- System.out.println("Welcome to Tic-tac-toe");
- // Get player's names.
- System.out.print("Enter player 1's name? ");
- String player1 = input.nextLine();
- System.out.print("Enter player 2's name? ");
- String player2 = input.nextLine();
- // Print blank board.
- printBoard(a, b, c, d, e, f, g, h, i);
- for (int move = 0; move < 9; move++) {
- char turn;
- // Check whose turn it is...
- if (move % 2 == 0) {
- System.out.println("It is " + player1 + "'s turn...");
- turn = X;
- } else {
- System.out.println("It is " + player2 + "'s turn...");
- turn = O;
- }
- // Choose a square.
- System.out.print("Pick a row number: ");
- int row = input.nextInt();
- System.out.print("Pick a column number: ");
- int column = input.nextInt();
- // Based on whose turn it is and the picked square, fill in either an X or an O.
- if (row == 1 && column == 1 && a == B) {
- a = turn;
- } else if (row == 1 && column == 2 && b == B) {
- b = turn;
- } else if (row == 1 && column == 3 && c == B) {
- c = turn;
- } else if (row == 2 && column == 1 && d == B) {
- d = turn;
- } else if (row == 2 && column == 2 && e == B) {
- e = turn;
- } else if (row == 2 && column == 3 && f == B) {
- f = turn;
- } else if (row == 3 && column == 1 && g == B) {
- g = turn;
- } else if (row == 3 && column == 2 && h == B) {
- h = turn;
- } else if (row == 3 && column == 3 && i == B) {
- i = turn;
- } else {
- System.out.println("You entered incorrect input! Try again...");
- move--;
- }
- // Print board.
- printBoard(a, b, c, d, e, f, g, h, i);
- // Check if there is a winner.
- checkWinner(a, b, c, d, e, f, g, h, i, player1, player2);
- }
- // We played all 9 moves and no one has won!
- System.out.println("Stalemate!");
- }
- public static void checkWinner(
- char a,
- char b,
- char c,
- char d,
- char e,
- char f,
- char g,
- char h,
- char i,
- String player1,
- String player2
- ) {
- // Either X or O.
- char winnerType;
- if (a == b && b == c) {
- // First row.
- winnerType = a;
- } else if (d == e && e == f) {
- // Second row.
- winnerType = d;
- } else if (g == h && h == i) {
- // Third row.
- winnerType = g;
- } else if (a == d && d == g) {
- // First column.
- winnerType = a;
- } else if (b == e && e == h) {
- // Second column.
- winnerType = b;
- } else if (c == f && f == i) {
- // Third column.
- winnerType = c;
- } else if (a == e && b == i) {
- // First diagonal.
- winnerType = a;
- } else if (g == e && e == c) {
- // Second diagonal.
- winnerType = g;
- } else {
- return;
- }
- // Check if someone won and end the game.
- if (winnerType == X) {
- System.out.println(player1 + " won the game!!!");
- System.exit(0);
- } else if (winnerType == O) {
- System.out.println(player2 + " won the game!!!");
- System.exit(0);
- }
- }
- public static void printBoard(
- char a,
- char b,
- char c,
- char d,
- char e,
- char f,
- char g,
- char h,
- char i
- ) {
- // Print the board in a tabular form.
- System.out.println();
- System.out.println(" \t \t1\t \t2\t \t3");
- System.out.println("1\t|\t" + a + "\t|\t" + b + "\t|\t" + c + "\t|\t");
- System.out.println("2\t|\t" + d + "\t|\t" + e + "\t|\t" + f + "\t|\t");
- System.out.println("3\t|\t" + g + "\t|\t" + h + "\t|\t" + i + "\t|\t");
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement