• We hope you have a fantastic holiday season to close out 2025! Thank you all so much for being part of the Tom's Guide community!

*URGENT* Tick Tack Toe in Java

tipoo

Distinguished
May 4, 2006
271
0
18,930
I know this code is a steaming pile of crap but i need to get it fixed ASAP.



import java.io.*;
public class Ass9{
public static String GetString(){
String line=" ";
InputStreamReader input=new InputStreamReader(System.in);
BufferedReader reader=new BufferedReader(input);
try{
line=reader.readLine();
}
catch(Exception e){return line;}
return line;
}
public static void Output (String out){
System.out.println(out);
}
public static void OutputSame (String out){
System.out.print(out);
}

public static void InitializeBoard(String board[][]) {
for(int x = 0; x < board.length; x ++) {
for(int y = 0; y < board[x].length; y++) {
board[x][y] = "?";

}
}
}

public static void OutputBoard(String board[][]) {
for(int x = 0; x < board.length; x ++) {
for(int y = 0; y < board[x].length; y++) {
System.out.print(board[x][y] + " ");
}
System.out.print("\n");
}
}



public static void ChangeBoard(String board[][], int row, int col, String letter) {
board[row][col]=letter;
}





public static boolean CheckPosition(String board[][], String letter, int row, int col){

if(board[row][col].equals("?")){

return true;
}
else{

return false;
}

}

public static boolean CheckFull(String board[][], String letter, int row, int col) {
boolean full = false;
for(row = 0; row < board.length; row ++) {
for(col = 0; col < board[row].length; col++) {
return full;
}
}
}


public static boolean CheckVertical(String letter, String gameboard[][]){
for (int col=1; col<gameboard.length; col++){
if(gameboard[1][col].equals (letter)&&gameboard[2][col].equals(letter)&&gameboard[3][col].equals(letter)) {
return true;
}

}
return false;
}



public static boolean CheckHorizontal(String letter, String board[][]) {
for (int row=1; row<board.length; row++){
if(board[row][1].equals (letter)&&board[row][2].equals(letter)&&board[row][3].equals(letter)) {
return true;
}
else {
return false;
}
}
}



public static boolean CheckDiagonal1(String letter, String gameboard) {

for (int col=1; col<gameboard.length; col++){
if(gameboard[1][1].equals (letter)&&gameboard[2][2].equals(letter)&&gameboard[3][3].equals(letter)) {
return true;
}
else{
return false;
}
}
}

public static boolean CheckDiagonal2(String letter, String gameboard) {
for (int col=1; col<board.length; col++){
if(gamboard[1][col].equals (letter)&&gameboard[2][row].equalsletter&&gameboard[3][row].equals(letter)) {
return true;
}
else {
return false;
}
}
}


public static boolean CheckWinner(String board[][], String letter) {
boolean winnervertical = false;
boolean winnerhorizontal = false;
winnervertical = CheckVertical(board, letter);
winnerhorizontal = CheckHorizontal(board, letter);
if(winnervertical || winnerhorizontal) {
return true;
}
else {
return false;
}
}

//checks both winnervertical and winnerhorizontal to see if there are 3 of any kind in a row.







public static void main(String args[]){
String gameboard[][];
String letter = "X";
boolean winner = false;
gameboard = new String[4][4];
InitializeBoard(gameboard);
OutputBoard(gameboard);

int row=0;
int col=0;


InitializeBoard(gameboard);


while(!winner){
OutputBoard(gameboard);
Output("Where would you like to put your letter?");
OutputSame("row: ");
row=Integer.parseInt(GetString());
OutputSame("Col: ");
col=Integer.parseInt(GetString());

ChangeBoard(gameboard, row, col, letter);

CheckFull(gameboard, row, col, letter);


if(CheckPosition(row, col, gameboard)){
ChangeBoard(gameboard, row, col, letter);
}
if (letter.equalsIgnoreCase("X")){

letter="O";
}

else{
letter="X";
}

if(Checkfull(gameboard)){
break;
}


else {
Output("Please try again");
}

CheckFull(gameboard, row, col, letter);
CheckPosition(gameboard, row, col, letter);
ChangeBoard(gameboard, row, col, letter);

}


}
}
 
Cramming for a school project eh? 😛 Doesn't the Java dev kit have a tic-tac-toe applet built in? Maybe you can look at its source code?
 
Oh man, there was SO MUCH wrong with my code, not just one thing. Its fixed, thats what matters 😀
 
BTW, I think this forum supports the [ code ] markers for code which can make code much more readable. ex:
Code:
public static void InitializeBoard(String board[][]) {
   for(int x = 0; x < board.length; x ++) {
      for(int y = 0; y < board[x].length; y++) {
         board[x][y] = "?";
      }
   }
}