How do I get my Go-Moku game to keep looping? - java

I am trying to make the game Go-Moku. The game compiles, but doesn not run properly.
When I run the game I am able to have the empty board printed and then i am prompted to enter the row and column integers, but once I hit enter to submit the column int, nothing else happens.
Here is my code:
import java.util.Scanner;
public class GoMoku extends Board{
Scanner input = new Scanner(System.in);
//play turn method
boolean play (int player){
this.printBoard();
System.out.println("It's Player "+ player + "'s turn.");
System.out.print("Choose Row: ");
int row = input.nextInt();
System.out.print("Choose Column: ");
int column = input.nextInt();
return this.place(row, column, player);
}
public static void main(String[] args) {
System.out.println("Welcome to Go-Moku!");
GoMoku gomoku = new GoMoku();
int gameLoop = 1;
//Turn taking Loop
while ( gameLoop != 0) {
//Player 1 Loop
while (gameLoop == 1) {
gameLoop++;
if (gomoku.play(1) == true) gameLoop = 0;
}
//Player 2 Loop
while (gameLoop == 2) {
gameLoop--;
if (gomoku.play(2) == true) gameLoop = 0;
}
}
}
public class Board {
int[][] board = new int[19][19];
//Board Contructor
public Board() {
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
board[i][j] = 0;
}
}
}
//Places token for player
public boolean place(int row, int column, int player) {
if (board[row][column] == 0) {
board[row][column] = player;
}
return this.hasWin(row, column, player);
}
//Checks to see if the player won the game horizontally
public boolean hasHorizontalWin(int row, int column,int player) {
int left = 0;
int right = 0;
//Counts connections made on left side of placement
for (int k = 1; k < 6; k++){
if (board[row - k][column] == player) left++;
}
//Counts connectons made on right side of placement
for (int l = 1; l < 6; l++) {
if (board[row + l][column] == player) right++;
}
int count = left + right;
if (count == 5) {
return true;
} else return false;
}
//Checks to see if the player won the game vertically
public boolean hasVerticalWin(int row, int column, int player) {
int down = 0;
int up = 0;
//Counts connections made on down side of placement
for (int n = 1; n < 6; n++){
if (board[row][column - n] == player) down++;
}
//Counts connections made on up side of placement
for (int p = 1; p < 6; p++) {
if (board[row][column + p] == player) up++;
}
int count = up + down;
if (count == 5) {
return true;
} else return false;
}
//Player wins method
boolean hasWin(int row, int column, int player) {
if (this.hasHorizontalWin(row, column, player) == true ||
this.hasVerticalWin(row, column, player) == true) {
System.out.println("Player " + player + " wins!");
return true;
} else return false;
}
//Prints board as string
public void printBoard(){
for (int m = 0; m < 19; m++){
for (int b = 0 ; b < 19; b++){
if (board[m][b] == 0) System.out.print("-");
if (board[m][b] == 1) System.out.print("o");
if (board[m][b] == 2) System.out.print("x");
}
System.out.println();
}
}
}
}
thanks

The problem is in the Board class - when you check for winning lines you can get an ArrayOutOfBounds exception.
You can fix it with something like this:
public class Board {
static final int WIDTH = 19;
static final int HEIGHT = 19;
int[][] board = new int[HEIGHT][WIDTH];
//Board Contructor
public Board() {
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
board[i][j] = 0;
}
}
}
//Places token for player
public boolean place(int row, int column, int player) {
if (board[row][column] == 0) {
board[row][column] = player;
}
return this.hasWin(row, column, player);
}
//Checks to see if the player won the game horizontally
public boolean hasHorizontalWin(int row, int column,int player) {
int total = 1;
//Counts connections made on left side of placement
for ( int k = row - 1; k >= 0 && k > row - 5; --k ) {
if ( board[k][column] != player ) break;
++total;
}
for ( int k = row + 1; k < HEIGHT && k < row + 5; ++k ) {
if ( board[k][column] != player ) break;
++total;
}
return total >= 5;
}
//Checks to see if the player won the game vertically
public boolean hasVerticalWin(int row, int column, int player) {
int total = 1;
//Counts connections made on left side of placement
for ( int k = column - 1; k >= 0 && k > column - 5; --k ) {
if ( board[row][k] != player ) break;
++total;
}
for ( int k = column + 1; k < WIDTH && k < column + 5; ++k ) {
if ( board[row][k] != player ) break;
++total;
}
return total >= 5;
}
//Player wins method
boolean hasWin(int row, int column, int player) {
if (this.hasHorizontalWin(row, column, player) == true ||
this.hasVerticalWin(row, column, player) == true) {
System.out.println("Player " + player + " wins!");
return true;
} else return false;
}
//Prints board as string
public void printBoard(){
for (int m = 0; m < HEIGHT; m++){
for (int b = 0 ; b < WIDTH; b++){
if (board[m][b] == 0) System.out.print("-");
if (board[m][b] == 1) System.out.print("o");
if (board[m][b] == 2) System.out.print("x");
}
System.out.println();
}
}
}
You can also simplify the main method:
public static void main(String[] args) {
System.out.println("Welcome to Go-Moku!");
GoMoku gomoku = new GoMoku();
int player = 1;
//Turn taking Loop
while ( true ) {
if ( gomoku.play( player ) ) break;
player = 3 - player;
}
}
Apart from that there are still other places where you can get errors (i.e. if the user enters a row or column that is negative or greater than the board size) and players can enter the same co-ordinates over and over.

public boolean verticaleWin(int ligne, int colonne, int player) {
int total = 1;
for (int k = ligne - 1; (k >= 0 && k > ligne - 5); k--) {
if (dimension[k][colonne] != player) break;
++total;
}
for (int k = ligne + 1; k < dimension.length && k < ligne + 5; k++) {
if (dimension[k][colonne] != player) break;
++total;
}
return total >= 5;
}
public boolean horizontaleWin(int ligne, int colonne, int player) {
int total = 1;
for (int k = colonne - 1; k >= 0 && k > colonne - 5; k--) {
if (dimension[ligne][k] != player) break;
++total;
}
for (int k = ++colonne; k < dimension.length && k < colonne + 5; k++) {
if (dimension[ligne][k] != player) break;
++total;
}
return total >= 5;
}
public boolean diagonaleWin(int ligne, int colonne, int player) {
int total1 = 1;
int total2 = 1;
for (int k = ligne - 1, j = colonne + 1; (k >= 0 && k > ligne - 5 && j < dimension.length && j < colonne + 5); k--, j++) {
if (dimension[k][j] != player) break;
++total1;
}
for (int k = ligne + 1, j = colonne - 1; (k < dimension.length && k < ligne + 5 && j >= 0 && j > colonne - 5); k++, j--) {
if (dimension[k][j] != player) break;
++total1;
}
for (int k = ligne + 1, j = colonne + 1; (k < dimension.length && k < ligne + 5 && j < dimension.length && j < colonne + 5); k++, j++) {
if (dimension[k][j] != player) break;
++total2;
}
for (int k = ligne - 1, j = colonne - 1; (k >= 0 && k > ligne - 5 && j >= 0 && j > colonne - 5); k--, j--) {
if (dimension[k][j] != player) break;
++total2;
}
return ((total1 >= 5) || (total2 >= 5));
}

Related

Creating a non-Attacking Queens game that is supposed to print out all 92 solutions of 8 queens on a chess board that cannot attack eachother

I made the 8x8 chess board and have a lot of the code done, but for some reason it only print out one solution, does anyone know why this may be and how I can fix it?
public class NonAttackingQueens {
private int[][] board;
private int solutionCount = 0;
private boolean solutionFound = false;
public NonAttackingQueens() {
board = new int[8][8];
}
public boolean canPlace(int x, int y) {
// Check if a queen is already placed at position (x, y)
if (board[x][y] == 1) {
return false;
}
// Check horizontal positions
for (int i = 0; i < 8; i++) {
if (board[x][i] == 1) {
return false;
}
}
// Check vertical positions
for (int i = 0; i < 8; i++) {
if (board[i][y] == 1) {
return false;
}
}
// Check diagonal positions
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1 && (Math.abs(i - x) == Math.abs(j - y))) {
return false;
}
}
}
return true;
}
public void solve() {
// Check if the solutionCount has reached 92
if (solutionCount == 92) {
return;
}
// Check if all 8 queens have been placed
int queensPlaced = 0;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1) {
queensPlaced++;
}
}
}
if (queensPlaced == 8) {
// All positions have been checked, so we have found a solution
solutionCount++;
System.out.println("Solution " + solutionCount + ":");
print();
return;
}
// Try to place a queen at each position on the board
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (canPlace(i, j)) {
// Place a queen at position (i, j) and try to solve the rest of the board
board[i][j] = 1;
solve();
// Backtrack: remove the queen from position (i, j) and try the next position
board[i][j] = 0;
}
}
}
}
public void print() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 1) {
System.out.print(" X");
} else {
System.out.print(" O");
}
}
System.out.println();
}
System.out.println("---------------");
}
}
I'm doing this in blueJ, so I tried to run the void solve(); method and it runs, but it only prints out the first of 92 solutions 92 times. It should print out all 92 different solutions.

8 Queens problem array size doesn't work after 30

This program working quite well. i wrote some exception ex N=1,2 and find others.
I have a problem with a bord part when I give a number above 30 program waiting and freezing how can I solve that? I want to write for at least 255 or 150(max value). in there if N=30 or more program will stop when you want to run.
package com.company;
import java.util.Scanner;
public class NQueenProblem {
Scanner scanner = new Scanner(System.in);
final int N = Integer.valueOf(scanner.nextLine());
void printSolution(int board[][]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
System.out.print(" " + board[i][j]
+ " ");
System.out.println();
}
}
safe condition is working correctly in big boards
boolean isSafe(int board[][], int row, int col) {
int i, j;
for (i = 0; i < col; i++)
if (board[row][i] == 1)
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j] == 1)
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j] == 1)
return false;
return true;
}
boolean solveNQUtil(int board[][], int col) {
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1) == true)
return true;
board[i][col] = 0;
}
}
return false;
}
boolean solveNQ() {
int board[][]=new int[N][N];
if (solveNQUtil(board, 0) == false) {
System.out.print("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
// driver program to test above function
public static void main(String args[]) {
NQueenProblem Queen = new NQueenProblem();
Queen.solveNQ();
}
}

Customizable TicTacToe game board with Java

I need to create a method to check wether the tictactoe game is PLAYING, DRAW, XWIN or OWIN. However, I am having difficulty writing the code to check if X or O has won, given that the size of the gameboard and the size needed to win (sizeWin) are changing according to the user's input. AND I am forced to use a 1D array for the game board. I simply do not know where to go from here. My latest idea was to use nested for loops to check for a win by row, column or diagonal but I'm not sure how to implement it. If anyone has any tips on how to approach this problem or has any other solutions I would be very grateful
private void setGameState(int i) {
// Check rows
getLines();
getColumns();
getSizeWin();
for (row = 0; row == lines; row++) {
for (col = 0; col == columns; col++) {
}
}
}
public TicTacToeGame(int lines, int columns, int sizeWin) {
// linesXcolumns game, starts with X, need sizeWin in a line/column/diag to win
this.lines = lines;
this.columns = columns;
CellValue currentCellValue = CellValue.X;
this.sizeWin = sizeWin;
// Creating board according to given size
int size = lines * columns;
this.board = new CellValue[size];
// Setting up board to be empty
for (int i = 0; i < size; i++) {
board[i] = CellValue.EMPTY;
}
}
PS. If someone were to call the operator TicTacToe(3,4,3), a game board of 3 lines and 4 columns would print. And the number of X's or O's to win would be 3.
CAM$ java TicTacToe 3 4 3
| | |
---------------
| | |
---------------
| | |
It is a little more complicated than it looks but after you get it it's simple. I've made a function that works just fine:
private static String checkGameState() {
// Looking for errors.
if (rowCount <= 0 || columnCount <= 0) {
return "ERROR: Illegal board size: " + rowCount + "*" + columnCount;
}
if (boradContent.length != rowCount * columnCount) {
return "ERROR: boradContent not compatible with rowSize and columnSize.";
}
if (sizeWin > rowCount && sizeWin > columnCount) {
return "ERROR: Board is too small for this sizeWin: " + sizeWin + ".";
}
String gameState = "PLAYING";
// Checking rows
for (int i = 0; i < rowCount; i++) {
char currentChar = getField(i, 0);
int score = 1;
for (int j = 1; j < columnCount; j++) {
if (currentChar == getField(i, j)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
if (j > columnCount - sizeWin) {
break;
}
score = 1;
currentChar = getField(i, j);
}
}
}
// Checking columns
for (int j = 0; j < columnCount; j++) {
char currentChar = getField(0, j);
int score = 1;
for (int i = 1; i < rowCount; i++) {
if (currentChar == getField(i, j)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
if (j > rowCount - sizeWin) {
break;
}
score = 1;
currentChar = getField(i, j);
}
}
}
// Checking diagonally
// Checking diagonally - from top-left to bottom-right
for (int i = 0; i < rowCount - sizeWin + 1; i++) {
for (int j = 0; j < columnCount - sizeWin + 1; j++) {
char currentChar = getField(i, j);
int score = 1;
for (int k = 1; k < sizeWin; k++) {
if (currentChar == getField(i + k, j + k)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
break;
}
}
}
}
// Checking diagonally - from top-right to bottom-left
for (int i = 0; i < rowCount - sizeWin + 1; i++) {
for (int j = sizeWin -1; j < columnCount; j++) {
char currentChar = getField(i, j);
int score = 1;
for (int k = 1; k < sizeWin; k++) {
if (currentChar == getField(i + k, j - k)) {
score++;
if (score >= sizeWin) {
if (gameState.equals("PLAYING")) {
gameState = currentChar + "WIN";
} else if (!gameState.equals(currentChar + "WIN")) {
gameState = "DRAW";
return gameState;
}
}
} else {
break;
}
}
}
}
return gameState;
}
It is worth to mention that the rowCount, columnCount, sizeWin and boradContent variables are class level variables and i used a getField(int X, int Y) method that is not a very complicated thing but is more useful. It just converts the given field coordinates to the place in a 1D array and returns it's content:
private static char getField(int X, int Y) {
return boradContent[X * columnCount + Y];
}

Can someone help me with my win scenario in my Gomoku program?

I have written a program for an assignment where we had to write a simple Gomoku program. I thought I had it all, but when I compile and run, it sets off the win scenario even if I only have 4 of a kind and even if they're not next to each other. (It should only set off a win if there are five in a row of one kind...X's or O's). I feel like I should be resetting my counter back to 0 before each turn, but I'm not sure where I should be doing that. Any tips would be appreciated!
import java.util.Scanner;
public class Gomoku1
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
char[][] map = new char [19][19];
int row = 0;
int column = 0;
//fill game with dots
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[i].length; j++)
{
map[i][j] = '.';
}
}
printMap(map);
char player1Choice = 'X';
char player2Choice = 'O';
int [] place;
while (true)
{
System.out.println("Player 1's turn!");
place = userTurn(map, player1Choice);
if (isValidMove(map, place[0], place[1]) == false)
{
System.out.println("Invalid move! Try again!");
place = userTurn(map, player1Choice);
}
if (isValidMove(map, place[0], place[1])) {
map[place[0]][place[1]] = player1Choice;
printMap(map);
}
if (isBoardFull(map) == true)
{
System.out.println("Board is full. Tied game.");
break;
}
if (hasPlayerWon(map, player1Choice) == true)
{
System.out.println("Player 1 Wins!");
break;
}
else
{
System.out.println("Player 2's turn!: ");
place = userTurn(map, player2Choice);
//System.out.println(isValidMove(map, row, column));
if (isValidMove(map, place[0], place[1]) == false)
{
System.out.println("Invalid move! Try again!");
place = userTurn(map, player2Choice);
}
if (isValidMove(map, place[0], place[1])) {
map[place[0]][place[1]] = player2Choice;
printMap(map);
}
if (isBoardFull(map) == true)
{
System.out.println("Board is full. Tied game.");
break;
}
if (hasPlayerWon(map, player2Choice) == true)
{
System.out.println("Player 2 Wins!");
break;
}
}
}
}
public static void printMap (char[][] map)
{
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map[i].length; j++)
{
System.out.printf("%2c", map[i][j]);
}
System.out.println();
}
}
public static int [] userTurn (char[][] map, char playerChoice)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter row: ");
int row = input.nextInt();
System.out.print("Enter column: ");
int column = input.nextInt();
int place [] = {row, column};
return place;
}
public static boolean isValidMove (char[][] map, int row, int column)
{
//System.out.println ("n is valid move");
if (row < 0 || row > 18 || column < 0 || column > 18 || map[row][column]=='O' || map[row][column]=='X')
{
return false;
}
else
{
return true;
}
}
public static boolean isBoardFull (char[][] map)
{
int openSpots = 0;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (!(map[i][j]=='.'))
openSpots++;
}
}
if (openSpots == 361)
{
return true;
}
return false;
}
public static boolean hasPlayerWon(char[][] map, int player)
{
if (isHorizontalWin(map, player) == true || isVerticalWin(map, player) == true || isDiagonalWin(map, player) == true)
{
return true;
}
return false;
}
public static boolean isHorizontalWin(char[][] map, int player)
{
int count = 0;
int r;
int c;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 0;
c += 1;
}
}
}
}
if (count == 5)
{
return true;
}
return false;
}
public static boolean isVerticalWin(char[][] map, int player)
{
int count = 0;
int r;
int c;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 1;
c += 0;
}
}
}
}
if (count == 5)
{
return true;
}
return false;
}
public static boolean isDiagonalWin(char[][] map, int player)
{
int count = 0;
int r;
int c;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count++;
r += 1;
c += 1;
}
}
}
}
if (count == 5)
{
return true;
}
return false;
}
}
You have problems in all three of the function that check win conditions: isHorizontalWin, isVerticalWin, and isDiagonalWin. All three increment the variable count, but this variable is never set back to zero. Additionally, the check to see if count == 5 should be made inside the loop. Here is an example on how to fix isHorizontalWin:
public static boolean isHorizontalWin(char[][] map, int player)
{
int count = 0;
int r;
int c;
for (int i = 0; i < map.length; i++)
{
for (int j = 0; j < map.length; j++)
{
if (map[i][j]==(player))
{
r = i;
c = j;
while (r >= 0 && r <= 18 && c >= 0 && c <= 18 && map[r][c] == player)
{
count ++;
r += 0;
c += 1;
}
if (count == 5)
{
return true;
} else {
count = 0;
}
}
}
}
return false;
}

Java: Tic-Tac-Toe - How To AVOID Repeated Cell From Being Input By User?

How do I prevent the same tic tac toe coordinate from being inputted by the user?
The user input is taken at the main method in the Game Class.
The tic tac toe cells with [x, y] coordinates ranging from (0-2) can be either:
0(_), 1 (X) or 2 (O)
Grid Class with alpha beta search tree pruning algorithm
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Grid {
List<Cell> availableCells;
int[][] board = new int[3][3];
Scanner scan = new Scanner(System.in);
// Set limit to search tree depth
int treeDepth = 9;
List<CellsAndScores> rootsChildrenScore = new ArrayList<>();
public int score() {
int score = 0;
// Check all columns
for (int j = 0; j < 3; ++j) {
int X = 0;
int O = 0;
for (int i = 0; i < 3; ++i) {
if (board[i][j] == 0) {
} else if (board[i][j] == 1) {
X++;
} else {
O++;
}
}
score += changeInScore(X, O);
}
// Check all rows
for (int i = 0; i < 3; ++i) {
int X = 0;
int O = 0;
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
} else if (board[i][j] == 1) {
X++;
} else {
O++;
}
}
score += changeInScore(X, O);
}
int X = 0;
int O = 0;
// Check diagonal (first)
for (int i = 0, j = 0; i < 3; ++i, ++j) {
if (board[i][j] == 1) {
X++;
} else if (board[i][j] == 2) {
O++;
} else {
}
}
score += changeInScore(X, O);
X = 0;
O = 0;
// Check Diagonal (Second)
for (int i = 2, j = 0; i > -1; --i, ++j) {
if (board[i][j] == 1) {
X++;
} else if (board[i][j] == 2) {
O++;
} else {
}
}
score += changeInScore(X, O);
return score;
}
private int changeInScore(int X, int O) {
int change;
if (X == 3) {
change = 100;
} else if (X == 2 && O == 0) {
change = 10;
} else if (X == 1 && O == 0) {
change = 1;
} else if (O == 3) {
change = -100;
} else if (O == 2 && X == 0) {
change = -10;
} else if (O == 1 && X == 0) {
change = -1;
} else {
change = 0;
}
return change;
}
public int alphaBetaMinimax(int alpha, int beta, int depth, int turn) {
if (beta <= alpha) {
System.out.println("Pruning at tree depth = " + depth + " alpha: " + alpha + " beta: " + beta);
if (turn == 1)
return Integer.MAX_VALUE;
else
return Integer.MIN_VALUE;
}
if (depth == treeDepth || gameOver()) {
return score();
}
List<Cell> cellsAvailable = getAvailableStates();
if (cellsAvailable.isEmpty()) {
return 0;
}
if (depth == 0) {
rootsChildrenScore.clear();
}
int maxValue = Integer.MIN_VALUE, minValue = Integer.MAX_VALUE;
for (int i = 0; i < cellsAvailable.size(); ++i) {
Cell cell = cellsAvailable.get(i);
int currentScore = 0;
if (turn == 1) {
placeAMove(cell, 1);
currentScore = alphaBetaMinimax(alpha, beta, depth + 1, 2);
maxValue = Math.max(maxValue, currentScore);
// Set alpha
alpha = Math.max(currentScore, alpha);
if (depth == 0) {
rootsChildrenScore.add(new CellsAndScores(currentScore, cell));
}
} else if (turn == 2) {
placeAMove(cell, 2);
currentScore = alphaBetaMinimax(alpha, beta, depth + 1, 1);
minValue = Math.min(minValue, currentScore);
// Set beta
beta = Math.min(currentScore, beta);
}
// reset board
board[cell.x][cell.y] = 0;
// Do not evaluate the rest of the branches after search tree is pruned
if (currentScore == Integer.MAX_VALUE || currentScore == Integer.MIN_VALUE)
break;
}
return turn == 1 ? maxValue : minValue;
}
public boolean gameOver() {
// Game is over is someone has won, or board is full (draw)
return (hasXWon() || hasOWon() || getAvailableStates().isEmpty());
}
public boolean hasXWon() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 1)
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 1)) {
// System.out.println("X Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i) {
if (((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 1)
|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 1))) {
// System.out.println("X Row or Column win");
return true;
}
}
return false;
}
public boolean hasOWon() {
if ((board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] == 2)
|| (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] == 2)) {
// System.out.println("O Diagonal Win");
return true;
}
for (int i = 0; i < 3; ++i) {
if ((board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] == 2)
|| (board[0][i] == board[1][i] && board[0][i] == board[2][i] && board[0][i] == 2)) {
// System.out.println("O Row or Column win");
return true;
}
}
return false;
}
public List<Cell> getAvailableStates() {
availableCells = new ArrayList<>();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0) {
availableCells.add(new Cell(i, j));
}
}
}
return availableCells;
}
public void placeAMove(Cell Cell, int player) {
board[Cell.x][Cell.y] = player; // player = 1 for X, 2 for O
}
public Cell returnBestMove() {
int MAX = -100000;
int best = -1;
for (int i = 0; i < rootsChildrenScore.size(); ++i) {
if (MAX < rootsChildrenScore.get(i).score) {
MAX = rootsChildrenScore.get(i).score;
best = i;
}
}
return rootsChildrenScore.get(best).cell;
}
public void displayBoard() {
System.out.println();
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board[i][j] == 0)
System.out.print("_" + " ");
if (board[i][j] == 1)
System.out.print("X" + " ");
if (board[i][j] == 2)
System.out.print("O" + " ");
}
System.out.println("");
}
System.out.println();
}
public void resetGrid() {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = 0;
}
}
}
}
Cell class
class Cell {
int x, y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
}
class CellsAndScores {
int score;
Cell cell;
CellsAndScores(int score, Cell cell) {
this.score = score;
this.cell = cell;
}
}
Game Class with main method - takes user input
import java.util.Random;
public class Game {
public static void main(String[] args) {
Grid grid = new Grid();
Random random = new Random();
grid.displayBoard();
System.out.print("Who moves first? [1]Computer(X) [2]User(O): ");
int turn = grid.scan.nextInt();
if (turn == 1) {
Cell p = new Cell(random.nextInt(3), random.nextInt(3));
grid.placeAMove(p, 1);
grid.displayBoard();
}
while (!grid.gameOver()) {
int x = 0, y = 0;
System.out.print("Please enter an x coordinate [0-2]: ");
x = grid.scan.nextInt();
System.out.print("Please enter an y coordinate [0-2]: ");
y = grid.scan.nextInt();
Cell userMove = new Cell(y, x);
grid.placeAMove(userMove, 2); // 2 for O and O is the user
grid.displayBoard();
if (grid.gameOver())
break;
grid.alphaBetaMinimax(Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 1);
for (CellsAndScores pas : grid.rootsChildrenScore)
System.out.println("Cell: " + pas.cell + " Score: " + pas.score);
grid.placeAMove(grid.returnBestMove(), 1);
grid.displayBoard();
}
if (grid.hasXWon()) {
System.out.println("Unfortunately, you lost!");
grid.resetGrid();
} else if (grid.hasOWon()) {
System.out.println("You win!");
grid.resetGrid();
} else {
System.out.println("It's a draw!");
grid.resetGrid();
}
}
}
My answer would be to add a boolean check method into your Grid.java class and then in your main method - call this boolean check method before the placeAMove() method.
For example, in your Grid.java class, adding the following method:
/*
* Return true if space is ok to use.
*/
public boolean isMoveOK(Cell cell) {
return board[cell.x][cell.y] == 0;
}
This way, using your pre-existing 0/1/2 values that keep track of empty/X/O space values, you may provide a check to see if the space value is zero or not.
This would be one way to use it in your main method, to answer your question of, 'How do I prevent the same tic tac toe coordinate from being inputted by the user?'
Cell userMove = new Cell(y, x);
if (grid.isMoveOK(userMove)) {
grid.placeAMove(userMove, 2); // 2 for O and O is the user
} else {
System.out.println("Please try a different space/cell");
continue;
}
grid.displayBoard();
if (grid.gameOver())
break;
In this way, I am skipping the remaining loop code in your main method loop, until there's a valid open space. (When there is, then the program should proceed to check for winning values or whether to proceed playing)
Hope this answers your question! :)
Cheers

Categories

Resources