My professor told me these things are wrong:1) For getting the "#" sign in your board, you need to call the createTicTacToeBoard() method to create the board. The returning type of the method is char[][] createTicTacToeBoard() not the string, and you don't need to pass any array into this method as it is the method to create the board. Inside this method, you create the board and assign the "#" sign to each cell, i.e., board [row][column] = (char)35. After the board is created and returned to the main(), you pass the board into the String displayTicTacToeBoard(char[][] board) method that return the enter board as a string back to the main().
2) For the program to finish, you need to have a post-test loop and two variables: win and draw. If someone wins or they draw, you assign true to the win or draw, and then use this statement, while(!draw && !win) to stop the program and then ask the user whether or not they want to have another game.
3) The problem is that the createTicTacToeBoard() method is used to create the initial board. After the board has been created, you don't need to call it again for every user's input as you use the for-loop to keep calling the createTicTacToeBoard() method and keeping appending the variable output at line 45. What you need to do is to create the board outside the for-loop. After that, when the user enters the input, you assign the token to the board based on the inputs and then pass the board to the String displayTicTacToeBoard(char[][] board) method to return the board to the variable output. After then, you pass the variable output to the JOptionPane.showMessageDiaglog to display the board.
import javax.swing.JOptionPane;
public class ticTacToeGame {
/**
* @param args
*/
public static void main(String[] args) {
int response;
String player1 = null;
String player2 = null;
player1 = createUser1(player1);
player2 = creatUser2(player2);
do{
int i = 0;
int w = 0;
do{
String boardCreator = JOptionPane.showInputDialog(null, "Please enter the size of your game board?",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
i = Integer.parseInt(boardCreator);
w = i;
if (i < 0)
{
JOptionPane.showMessageDialog(null, "Sorry! Please enter a positive number. " +
"Please enter again.", "Take-Home Assignment 1", JOptionPane.ERROR_MESSAGE);
}
if (1 == i)
{
JOptionPane.showMessageDialog(null, "Sorry! The size of the board cannot be less than 2 x 2. " +
"Please enter again.", "Take-Home Assignment 1", JOptionPane.ERROR_MESSAGE);
}
}while (i < 2);
char [][] board = new char [i][w];
int arrayLength = i * w;
//board [i][w] = (char)35;
String output = "";
for(int u = 0; u < arrayLength;){
output += createTicTacToeBoard(board);
u = u + 1;
String userRowSelection = JOptionPane.showInputDialog(null,"" + output + "\n" + "" + player1 + " (X): " + "Please enter the row#",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
int j = Integer.parseInt(userRowSelection);
String userColumnSelection = JOptionPane.showInputDialog(null, "" + output + "\n" +"" + player1 + " (X): " + "Please enter the column#",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
int k = Integer.parseInt(userColumnSelection);
board[j][k] = (char)88;
u = u + 1;
String userRowSelection2 = JOptionPane.showInputDialog(null, "" + output + "\n" +"" + player2 + " (O): " + "Please enter the row#",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
int p = Integer.parseInt(userRowSelection2);
String userColumnSelection2 = JOptionPane.showInputDialog(null, "" + output + "\n" +"" + player2 + " (O): " + "Please enter the column#",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
int o = Integer.parseInt(userColumnSelection2);
board[p][o] = (char)79;
}
output += createTicTacToeBoard(board);
checkCoulmn(board, i, player1, player2);
output += checkRow(board, i, player1, player2);
//output += checkDiagonal(board, i, player1, player2);
displayTicTacToeBoard(board, output);
response = JOptionPane.showConfirmDialog(null, "Play another game?",
"Lab Assignment 1", JOptionPane.YES_NO_OPTION);
}while (response != 1);
}
private static String checkDiagonal(char[][] board, int i, String player1,
String player2) {
String output ="";
boolean allX = true;
int row = 0, column = 0;
while(row < i && column < i && allX){
if (board[row][column] != (char)88)
allX = false;
else {
row++;
column++;
}
};
if (allX)
JOptionPane.showMessageDialog(null,"" + (player1) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
allX = true;
row = 0; column = 2;
while(row < 3 && column >= 0 && allX){
if (board[row][column] != (char)88)
allX = false;
else {
row++;
column++;
}
};
if (allX)
JOptionPane.showMessageDialog(null,"" + (player1) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
boolean allO = true;
row = 0; column = 0;
while(row < i && column < i && allO){
if (board[row][column] != (char)79)
allO = false;
else {
row++;
column++;
}
};
if (allO)
JOptionPane.showMessageDialog(null,"" + (player2) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
allO = true;
row = 0; column = 2;
while(row < 3 && column >= 0 && allO){
if (board[row][column] != (char)79)
allO = false;
else {
row++;
column++;
}
};
if (allO)
JOptionPane.showMessageDialog(null,"" + (player2) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
return output;
}
private static String checkCoulmn(char[][] board, int i, String player1,
String player2) {
String output ="";
boolean allX;
for (int column = 0; column < i; column++){
int row = 0; allX = true;
while(row < i && allX){
if (board[row][column] != (char)88)
allX = false;
else
row++;
};
if (allX){
JOptionPane.showMessageDialog(null,"" + (player1) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
}
}
boolean allO;
for (int column = 0; column < i; column++){
int row = 0; allO = true;
while(row < i && allO){
if (board[row][column] != (char)79)
allO = false;
else
row++;
};
if (allO){
JOptionPane.showMessageDialog(null,"" + (player2) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
}
}
return output;
}
private static String checkRow(char[][] board, int i, String player1, String player2) {
String output = "";
boolean allX;
for (int row = 0; row < i; row++){
int column = 0; allX = true;
while(column < i && allX){
if (board[row][column] != (char)88)
allX = false;
else
column++;
};
if (allX){
JOptionPane.showMessageDialog(null,"" + (player1) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
}
}
boolean allO;
for (int row = 0; row < i; row++){
int column = 0; allO = true;
while(column < i && allO){
if (board[row][column] != (char)79)
allO = false;
else
column++;
};
if (allO){
JOptionPane.showMessageDialog(null,"" + (player2) + ", You win","Take-Home Assignment 2",JOptionPane.INFORMATION_MESSAGE);
}
}
return output;
}
private static String creatUser2(String player2) {
do{
player2 = JOptionPane.showInputDialog(null, "Please enter your name, Player 2: ",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
if(player2.isEmpty()){
JOptionPane.showMessageDialog(null, "Sorry! Your name is empty. " +
"Please enter again.", "Take-Home Assignment 1", JOptionPane.ERROR_MESSAGE);
}
}while(player2.isEmpty());
return player2;
}
private static String createUser1(String player1) {
do{
player1 = JOptionPane.showInputDialog(null, "Please enter your name, Player 1: ",
"Take-Home Assignment 1", JOptionPane.QUESTION_MESSAGE);
if(player1.isEmpty()){
JOptionPane.showMessageDialog(null, "Sorry! Your name is empty. " +
"Please enter again.", "Take-Home Assignment 1", JOptionPane.ERROR_MESSAGE);
}
}while(player1.isEmpty());
return player1;
}
private static void displayTicTacToeBoard(char[][] board, String output) {
JOptionPane.showMessageDialog(null, output, "Lab Assignment 2", JOptionPane.INFORMATION_MESSAGE);
}
private static String createTicTacToeBoard(char[][] board) {
String output = "";
for (int row = 0; row < board.length; row++){
for (int column = 0; column < board[row].length; column++)
output = output + ( board [row][column]);
output = output + ("\n");
}
return output;
}
}