-
Tic Tac Toe game
Hello
please help me to fix my code, try it in ur devices to see the result:
Code:
package Tictactoe;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ituser
*/
import java.util.*;
public class Tictactoe1 {
private static final int ROWS = 3;
private static final int COLUMNS = 3;
private static String[][] board;
//construct an empty board
public Tictactoe1() {
board = new String[ROWS][COLUMNS];
//Fill with spaces
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
board[i][j] = " ";
}
}
}
//set the fields in the board
public String set(int i, int j, String player) {
if (board[i][j].equals(" ")) {
board[i][j] = player;
} else {
return " ";
}
return board[i][j];
}
//set the fields in the board
public boolean checkDiagL(String player) {
boolean found = false;
int count = 0;
int i = 0;
int j = 0;
while (i < ROWS) {
if (board[i][j].equals(player)) {
count++;
i++;
j++;
} else {
break;
}
}
if (count == ROWS) {
found = true;
}
return found;
}
public boolean checkDiagR(String player) {
boolean found = false;
int count = 0;
int i = 0;
int j = COLUMNS - 1;
while (i < ROWS) {
if (board[i][j].equals(player)) {
count++;
i++;
j--;
} else {
break;
}
}
if (count == ROWS) {
found = true;
}
return found;
}
public boolean checkHorizontal(String player) {
boolean found = false;
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[i][j].equals(player)) {
count++;
} else {
break;
}
}
if (count == ROWS) {
found = true;
break;
} else {
count = 0;
}
}
return found;
}
public boolean checkVertical(String player) {
boolean found = false;
int count = 0;
for (int j = 0; j < COLUMNS; j++) {
for (int i = 0; i < ROWS; i++) {
if (board[i][j].equals(player)) {
count++;
} else {
break;
}
}
if (count == COLUMNS) {
found = true;
break;
} else {
count = 0;
}
}
return found;
}
public static String preventdiagonalsLeft() {
String player = "x";
boolean found = false;
int i = 0;
int j = 0;
int count = 0;
String block = "";
while (i < ROWS) {
if (board[i][j] == player) {
count++;
} else if (board[i][j] == " ") {
block = "Block: " + i + " " + j;
}
i++;
j++;
}
if (count == 2) {
found = true;
return block;
}
return "";
}
public static String preventdiagonalsRight() {
String player = "x";
boolean found = false;
int i = 0;
int j = 2;
int count = 0;
String block = "";
while (i < ROWS) {
if (board[i][j] == player) {
count++;
} else if (board[i][j] == " ") {
block = "Block: " + i + " " + j;
}
i++;
j--;
}
if (count == 2) {
found = true;
return block;
}
return "";
}
public static String preventhorizontal() {
String player = "x";
boolean found = false;
int count = 0;
String block = "";
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
if (board[i][j] == player) {
count++;
} else if (board[i][j] == " ") {
block = "Block: " + i + " " + j;
}
}
if (count == 2) {
found = true;
return block;
} else {
count = 0;
}
}
return "";
}
public static String preventvertical() {
String player = "x";
boolean found = false;
int count = 0;
String block = "";
for (int j = 0; j < COLUMNS; j++) {
for (int i = 0; i < ROWS; i++) {
if (board[i][j] == player) {
count++;
} else if (board[i][j] == " ") {
block = "Block: " + i + " " + j;
}
}
if (count == 2) {
found = true;
return block;
} else {
count = 0;
}
}
return "";
}
public static int[] nextMove(String player) {
int counter = 0;
int pos[] = new int[18];
for (int i = 0; i < 18; i++) {
pos[i] = -1;
}
/* lucky position in the center of board*/
if (board[1][1] == " ") {
return new int[]{1, 1};
}
/* choose available move */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == " ") {
pos[counter] = i;
}
pos[counter + 1] = j;
counter = counter + 2;
}
}
return pos;
}
public boolean checkWinner(String player) {
if (checkDiagL(player)) {
return true;
} else if (checkDiagR(player)) {
return true;
} else if (checkHorizontal(player)) {
return true;
} else if (checkVertical(player)) {
return true;
} else {
return false;
}
}
public static String preventAll() {
String block = preventdiagonalsLeft();
if (block.equals("")) {
block = preventdiagonalsRight();
if (block.equals("")) {
block = preventhorizontal();
if (block.equals("")) {
block = preventvertical();
}
}
}
return block;
}
/**creates a string representation of the board
* @return the string representation
*/
public String toString() {
String r = "";
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLUMNS; j++) {
r += board[i][j];
if (j < COLUMNS - 1) {
r += "|";
}
}
r += "\n";
if (i < ROWS - 1) {
for (int j = 0; j < (COLUMNS * 2 - 1); j++) {
r += "-";
}
r += "\n";
}
}
return r;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String player = "x";
Tictactoe1 game = new Tictactoe1();
boolean done = false;
System.out.println(game.toString());
while (!done) {
int row = 0, column = 0;
if (player.equals("o")) {
nextMove("o");
preventAll();
} else {
System.out.print("Row for " + player + " (-1 to exit): ");
row = in.nextInt();
if (row < 0) {
done = true;
break;
} else {
System.out.print("Column for " + player + ": ");
column = in.nextInt();
}
}
if (game.set(row, column, player).equals(player)) {
System.out.println(game.toString());
if (game.checkWinner(player)) {
System.out.println("Result: The Winner is " + player);
break;
}
if (player.equals("x")) {
player = "o";
} else {
player = "x";
}
} else {
System.out.println("Position selected!");
}
}
}
}
I am looking for your reply