/* Tic Tac Toe Game by manish Created on request for Lucifer */ import java.util.*; public class TicTacToe { static int size,turn=0,moves=0,win=0; static String cur,reason="",players[] = new String[2]; static String board[][] = new String[size][size]; static String symbol[] = {"X","O"}; static boolean curb; public static void main(String s[]) { String players[] = new String[2]; Scanner sc = new Scanner(System.in); System.out.println(" * * * Welcome to Tic Tac Toe! * * * "); System.out.println("Enter the size of the board. The size cannot be greater than 6 or smaller than 3."); size = sc.nextInt(); if (size <= 2 || size >= 7) { System.out.println("The size you have entered is out of range."); return; } else { board = new String[size][size]; fill(); input(); display(); turn(); } } static void input() { System.out.println(" Enter the names of the two players to begin."); players[0] = new Scanner(System.in).nextLine(); players[1]= new Scanner(System.in).nextLine(); } static void fill() { for(int i=0; i size || b > size || a < 1 || b < 1) { System.out.println("Input is out of bounds or invalid! Try again!"); turn(); } else if (board[a-1][b-1].compareToIgnoreCase(" ") != 0) { System.out.println("The position you selected is already taken. Please try again."); turn(); } else { board[a-1][b-1] = symbol[turn]; verify(); } } else { System.out.println("Invalid input! Try again."); turn(); } } static boolean validate(String p) { if (p.length() != 3) { return false; } else if (p.charAt(1) != '*' && p.charAt(1) != 'x') { return false; } else if (!Character.isDigit(p.charAt(0)) || !Character.isDigit(p.charAt(2))) { return false; } return true; } static void verify() { int c=0,cmax=0; for(int i=0; i= cmax) { cmax = c; } c = 1; } else ++c; } if (c >= cmax) { cmax = c; } if (curb == true || cmax >= 3) { cmax = 0; win = 1; reason = reason + " " + "(Three In A Row)"; } } c=0; cmax = 0; for(int i=0; i= cmax ) { cmax = c; } c = 1; } else ++c; } if (c >= cmax) { cmax = c; } if (curb == true || cmax >= 3) { cmax=0; win = 1; reason = reason + " " + "(Three In A Column)"; } } c = 0; cmax = 0; if (board[0][0].compareToIgnoreCase(" ") != 0) { cur = board[0][0]; } else { cur = "."; } curb = true; for(int i=0; i= cmax ) { cmax = c; } c=1; } else ++c; } if (c >= cmax) { cmax = c; } if (curb == true || cmax >= 3) { cmax=0; win = 1; reason = reason + " " + "(Three In A Diagonal)"; } c = 0; cmax = 0; if (board[0][size-1].compareToIgnoreCase(" ") != 0) { cur = board[0][size-1]; } else { cur = "."; } curb = true; for(int i=0; i= cmax ) { cmax = c; } c=1; } else ++c; } if (c >= cmax) { cmax = c; } if (curb == true || cmax >= 3) { cmax = 0; win = 1; reason = reason + " " + "(Three In A Diagonal)"; } if (win == 1) { display(); System.out.println("So we have a winner now! Congratulations, "+players[turn]+ " has won the game!! "+reason); replay(); } else { ++moves; if (moves == size * size) { System.out.println(" Nobody won! Nobody lost! Congratulations, it's a draw!"); replay(); } else { ++turn; if (turn == 2) { turn = 0; } display(); turn(); } } } static void replay() { System.out.println("To replay the game with the same board, type R. Otherwise, type anything else to exit."); String rep = new Scanner(System.in).nextLine(); if (rep.compareToIgnoreCase("R") != 0) return; else { fill(); turn = 0; moves = 0; win = 0; reason = ""; curb = false; display(); turn(); } } }