Hello everyone!! I really need some help! I am trying in implement a tic tac toe game made of different classes in which the board can be expandable. I have most of it figured it out but after I ask the first player for their move it ends the game and says that player 1 won the game. I want my check Winner function in my gameboard class to check for 4 in a row no matter the size of thew board.
This is my code:
/*
The main class first asks the user to set the parameters of the board
and creates a board to the exact size the user wants. The board has to
be at least 3 by 3 or larger. Next the user is prompted to enter in their
first move. The first spot is represented by 0 and continues to go as large
as the board. It then places the player's move and then asks player 2 for
the same information. After both players make their moves it checks for a
winner or a draw. If they both return false users are able to enter a new
move until someone wins or the game ends in a draw.
*/
import javax.swing.JOptionPane;
public class TicTacToeGame {
static int X = 0;
static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
public static void main(String[] args) {
String input;
input = JOptionPane.showInputDialog("Enter an value for the height and width of the board?");
X = Integer.parseInt(input);
char[][] board = new char[X][X];
GameBoard.clearBoard(board);
do {
Player.makeMove(board, PLAYER1);
GameBoard.drawBoard(board);
if (GameBoard.checkDraw(board, X)
|| GameBoard.checkWinner()
|| GameBoard.checkWinner())
break;
Player.makeMove(board, PLAYER2);
GameBoard.drawBoard(board);
} while (!GameBoard.checkDraw(board, X)
&& !GameBoard.checkWinner()
&& !GameBoard.checkWinner());
if (GameBoard.checkWinner() == true) {
JOptionPane.showMessageDialog(null, "Player 1 was won the game!");
}
else if (GameBoard.checkWinner() == true) {
JOptionPane.showMessageDialog(null, "Player 2 was won the game!");
}
if (GameBoard.checkDraw(board, X) == true) {
JOptionPane.showMessageDialog(null,"Game has ended in a TIE! Please play again.");
}
}
}
public class GameBoard {
static char PLAYER1 = 'X', PLAYER2 = 'O', EMPTY = '?';
enum State{Blank, X, O};
public static void drawBoard(char board[][]) {
// Reads in user input and displays board with players moves
// The board continues to store each move and replaces an empty space
// with the players X or O
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++)
System.out.print(" " + board[i][j] + " "); // prints out each space and places a space between each space to make the board easier to read
System.out.print("\n");
}
System.out.print("\n");
}
public static void clearBoard(char board[][]) {
// Asks user for input on what the height and width of the board with be
// and then creates an EMPTY board
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j] = EMPTY; //places a ? mark in each empty spot
}
}
}
public static boolean checkWinner() {
int n = 4;
State[][] board = new State[n][n];
int moveCount = 0;
int x = 0, y = 0;
State s = null;
if(board[x][y] == State.Blank){
board[x][y] = s;
}
moveCount++;
//check end conditions
//check col
for(int i = 0; i < n; i++){
if(board[x][i] != s)
return false;
if(i == n-1){
return true;
}
}
//check row
for(int i = 0; i < n; i++){
if(board[i][y] != s)
return false;
if(i == n-1){
return true;
}
}
//check diag
if(x == y){
//we're on a diagonal
for(int i = 0; i < n; i++){
if(board[i][i] != s)
return false;
if(i == n-1){
return true;
}
}
}
//check anti diag (thanks rampion)
for(int i = 0;i<n;i++){
if(board[i][(n-1)-i] != s)
return false;
if(i == n-1){
return true;
}
}
return false;
}
public static boolean checkDraw(char board[][], int X) {
// First counts up how many moves have been made and then compare
// the count to height * width to see if there is any space left.
int count = 0;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == PLAYER1 || board[i][j] == PLAYER2)
count++;
}
}
if (count == X * X)
return true;
else
return false;
}
}
import javax.swing.JOptionPane;
public class Player {
public static void makeMove(char board[][], char player) {
//Is called by the main class and is used to find out where the user would like to place their game piece
int x = 0, y = 0;
String input;
input = JOptionPane.showInputDialog("Enter an X value");
x = Integer.parseInt(input); // user input on what row they would like
input = JOptionPane.showInputDialog("Enter an Y value");
y = Integer.parseInt(input); //user input on what column they would like
board[x][y] = player; //sets the board to the correct location
}
}
amullen98 0 Newbie Poster
Taywin 312 Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.