/*
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; i++)
{
for(int j=0; j<size; j++)
{
board[i][j] = " ";
}
}
}
static void display() {
String line = "|-------";
for (int i = 0; i < size+1; i++) {
for (int j = 0; j < size; j++) {
System.out.print(line);
}
System.out.println("|");
if (i < size) {
System.out.print(" ");
for (int j = 0; j < size; j++) {
System.out.print(board[i][j] + " ");
}
}
System.out.println();
}
System.out.println("");
}
static void turn()
{
System.out.println("( "+players[turn]+" ) :- Please make a move.(eg:2x2 or 2*2)");
String x = new Scanner(System.in).nextLine();
x = x.replace(" ","");
if (validate(x) == true)
{
int a = Character.getNumericValue(x.charAt(0));
int b = Character.getNumericValue(x.charAt(2));
if ( a > 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<size; i++)
{
c=0;
if (board[i][0].compareToIgnoreCase(" ") != 0) { cur = board[i][0]; }
else { cur = "."; }
curb = true;
for(int j=0; j<size; j++)
{
if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
if(board[i][j].compareToIgnoreCase(cur) != 0)
{
curb = false;
cur = board[i][j];
if (c >= 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<size; i++)
{
c=0;
if (board[0][i].compareToIgnoreCase(" ") != 0) { cur = board[0][i]; }
else { cur = "."; }
curb = true;
for(int j=0; j<size; j++)
{
if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
if(board[j][i].compareToIgnoreCase(cur) != 0)
{
cur = board[j][i];
curb = false;
if (c >= 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<size; i++)
{
if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
if(board[i][i].compareToIgnoreCase(cur) != 0)
{
cur = board[i][i];
curb = false;
if (c >= 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<size; i++)
{
if (cur.compareToIgnoreCase(" ") == 0) { cur = "."; }
if (board[i][size-1-i].compareToIgnoreCase(cur) != 0)
{
cur = board[i][size-1-i];
curb = false;
if (c >= 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();
}
}
}
Comments
0 B
|👍
/👎