//I have been working on this code for the past week on and off... the main issue I have been having is finding a winner without listing down all the possibilities...
//THIS IS MY METHODS CLASS
import java.util.Scanner;
import java.util.Arrays;
public class TicTacToe1 {
private String Player1Name;
private String Player2Name;
private char Pl1Choice;
private char pl2Choice;
private int row;
private int column;
private char board[][] = new char[4][4];
public TicTacToe1() {
}
public TicTacToe1(String name1, String name2, char choice1, char choice2, int r, int c, char b[][]) {
this.Player1Name = name1;
this.Player2Name = name2;
this.Pl1Choice = choice1;
this.pl2Choice = choice2;
this.row = r;
this.column = c;
this.board = b;
}
public void PlayerInfo(String name1, String name2, char choice1, char choice2) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Name Of Player-1");
this.Player1Name = sc.nextLine();
System.out.println("\nEnter The Name Of Player-2");
this.Player2Name = sc.nextLine();
}// PlayerInfo Method
public void InitializeBoard() {
for (int l = 1; l < board.length; l++) {
for (int g = 1; g < board[l].length; g++) {
board[l][g] = '*';
}
}
System.out.println();
System.out.println("--------------");
for (int i = 1; i < board.length; i++) {
for (int j = 1; j < board[i].length; j++) {
System.out.print(" ");
System.out.print(board[i][j] + "| ");
}
System.out.println();
System.out.print("--------------");
System.out.println();
}
}
public void PrintBoard() {
System.out.println();
System.out.println("--------------");
for (int i = 1; i < board.length; i++) {
for (int j = 1; j < board[i].length; j++) {
System.out.print(" ");
System.out.print(board[i][j] + "| ");
}
System.out.println();
System.out.print("--------------");
System.out.println();
}
}
public char WhoPlaysFirst() {
Scanner sc = new Scanner(System.in);
char secondplayer = ' ';
while (true) {
System.out.println("\nWho Would Like To Go First???");
System.out.println(this.Player1Name + " - 1");
System.out.println(this.Player2Name + " - 2");
char firstplayer = sc.next().charAt(0);
if (firstplayer == '1' || firstplayer == '2') {
if (firstplayer == '1') {
return firstplayer = '1';
} else if (firstplayer == '2') {
return firstplayer = '2';
}
} else {
System.out.println("Enter A Valid Option Pls");
continue;
}
}
}
public Boolean IsElementExist() {
if (board[this.row][this.column] == '*') {
if (board[this.row][this.column] == 'X' || board[this.row][this.column] == '0')
return true;
else
return false;
}
return true;
}
public void AddElement1() {
Scanner sc = new Scanner(System.in);
boolean check;
while (true) {
System.out.println(this.Player1Name + " Plays...");
System.out.println("\nEnter The Row You Would Like To Enter The Element");
this.row = sc.nextInt();
System.out.println("\nEnter The Column You Would Like To Enter The Element");
this.column = sc.nextInt();
if (this.row >= 1 && this.row <= 3) {
if (this.column >= 1 && this.column <= 3) {
check = true;
break;
}
} else {
System.out.println("Pls Enter Values Within The Board Size");
System.out.println("\nEnter The Row You Would Like To Enter The Element");
this.row = sc.nextInt();
System.out.println("\nEnter The Column You Would Like To Enter The Element");
this.column = sc.nextInt();
continue;
}
}
while (true) {
boolean poscheck = true;
if (board[this.row][this.column] == '*') {
board[this.row][this.column] = 'X';
break;
} else if (board[this.row][this.column] == 'X' || board[this.row][this.column] == '0') {
poscheck = false;
}
if (poscheck)
break;
else {
System.out.println("This Spot Is Already Filled... Try Another Position Pls!")
continue;
}
}
}// AddElement1
public void AddElement2() {
Scanner sc = new Scanner(System.in);
boolean check;
while (true) {
System.out.println(this.Player2Name + " Plays...");
System.out.println("\nEnter The Row You Would Like To Enter The Element");
this.row = sc.nextInt();
System.out.println("\nEnter The Column You Would Like To Enter The Element");
this.column = sc.nextInt();
if (this.row >= 1 && this.row <= 3) {
if (this.column >= 1 && this.column <= 3) {
check = true;
break;
}
} else {
System.out.println("Pls Enter Values Within The Board Size");
continue;
}
}
board[this.row][this.column] = '0';
}
public Boolean HorizontalWin() {
boolean won = false;
/*
* for (int i = 1; i < 4; i++) { if (board[i][1] == board[i][2] && board[i][2]
* == board[i][3]) { won = true; break; } else continue; }
*/
char start;
for (int i = 1; i < board.length - 1 && !won; i++) {
start = board[i][1];
if (start != '*') {
for (int j = 2; j < board[i].length - 1; j++) {
if (board[i][j] != '*')
if (board[i][j] == start && board[i][j + 1] == start) {
won = true;
System.out.println("Horizontal");
break;
} else {
won = false;
}
}
}
}
return won;
}// HorizontalWin
public Boolean VerticalWin() {
boolean won = false;
char start;
for (int i = 1; i < board.length - 1 && !won; i++) {
start = board[1][i];
if (start != '*') {
for (int j = 2; j < board[i].length - 1; j++) {
if (board[j][i] != '*')
if (board[j][i] == start && board[j + 1][i] == start) {
won = true;
System.out.println("Vertical");
break;
} else {
won = false;
}
}
}
}
/*
* for (int i = 1; i < 4; i++) { if (board[1][i] == board[2][i] && board[2][i]
* == board[3][i]) { won = true; break; } else continue; }
*/
return won;
}// VerticalWin
public Boolean DiagnolWin() {
boolean won = false;
char start;
for (int i = 1; i < board.length - 1 && !won; i++) {
start = board[1][1];
if (start != '*')
if (board[i][i] != '*')
if (board[2][2] == start && board[3][3] == start) {
won = true;
// System.out.println("check");
} else
won = false;
}
for (int j = 1; j < board.length - 1 && !won; j++) {
start = board[3][1];
if (start != '*')
if (board[j][board.length - j - 1] != '*')
if (board[2][2] == start && board[1][3] == start) {
won = true;
System.out.println("check");
} else
won = false;
}
return won;
}// DiagnolWin
public boolean winner() {
return (VerticalWin() || HorizontalWin() || DiagnolWin());
}
}// class
public class TicTacToe2 {//MAIN CLASS WHERE I CALL MY METHODS.
public static void main(String[] args) {
TicTacToe1 obj = new TicTacToe1();
int counter = 1; int i = 1;
boolean check = false;
obj.PlayerInfo(null, null, ' ', ' ');
obj.InitializeBoard();
if (obj.WhoPlaysFirst()=='1')
{
while (i < 6)
{
obj.AddElement1();
obj.PrintBoard();
if (i >= 3)
{
if (obj.winner())
{
check = true;
System.out.println("Winner");
break;
}
else
check = false;
}
obj.AddElement2();
obj.PrintBoard();
if (i >= 3)
{
if (obj.winner())
{
check = true;
//System.out.println("Winner");
break;
}
else
check = false;
}
i++;
}
}
else if (obj.WhoPlaysFirst()=='2')
{
while (i <= 9)
{
obj.AddElement2();
obj.PrintBoard();
if (i >= 3)
{
if (obj.winner())
{
check = true;
break;
}
else
check = false;
}
obj.AddElement1();
obj.PrintBoard();
if (i >= 3)
{
if (obj.winner())
{
check = true;
break;
}
else
check = false;
}
i++;
}
}
if (check) System.out.println("Game Over!");
else
System.out.println("It Is A Draw");
/ *
// ЭТО ВЫХОД Я ПОЛУЧИЛ, НО НЕ ПОБЕДИТЕЛЬ ...
Введите имя игрока-1
Аднан
Введите имя игрока-2
Хан
* | * | * |
* | * | * |
* | * | * |
Кто хотел бы пойти первым ???
Аднан - 1
Хан - 2
1
Аднан играет ...
Введите строку, которую вы хотели бы ввести в элемент
2
Введите столбец, который вы хотите ввести в элемент
2
* | * | * |
* | X | * |
* | * | * |
Хан играет ...
Введите строку, которую вы хотели бы ввести в элемент
3
Введите столбец, который вы хотите ввести в элемент
1
* | * | * |
* | X | * |
0 | * | * |
Аднан играет ...
Введите строку, которую вы хотели бы ввести в элемент
1
Введите столбец, который вы хотите ввести в элемент
2
* | X | * |
* | X | * |
0 | * | * |
Хан играет ...
Введите строку, которую вы хотели бы ввести в элемент
3
Введите столбец, который вы хотите ввести в элемент
2
* | X | * |
* | X | * |
0 | 0 | * |
Аднан играет ...
Введите строку, которую вы хотели бы ввести в элемент
1
Введите столбец, который вы хотите ввести в элемент
1
X | X | * |
* | X | * |
0 | 0 | * |
Хан играет ...
Введите строку, которую вы хотели бы ввести в элемент
3
Введите столбец, который вы хотите ввести в элемент
3
X | X | * |
* | X | * |
0 | 0 | 0 |
Аднан играет ...
Введите строку, которую вы хотите ввести в элемент * /