So here's what I've got:
public class TicTacToeGame { static int [][] gameboard; static final int EMPTY = 0; static final int NOUGHT = -1; static final int CROSS = 1; static void createBoard(int row, int col){ int gameboard[][]= new int [row][col]; } static int winOrTie(){ for (int r=0; r<gameboard.length; r++) { int x1=gameboard[r][0]; int x2=gameboard[r][1]; int x3=gameboard[r][2]; if (x1 == x2 && x2 == x3 && x1 != EMPTY){ return x1; } for (int c=0; c<gameboard.length; c++){ int y1=gameboard[0][c]; int y2=gameboard[1][c]; int y3=gameboard[2][c]; if (y1 == y2 && y2 == y3 && y1 != EMPTY){ return y1; } } int z1 = gameboard[0][0]; int z2 = gameboard[1][1]; int z3 = gameboard[2][2]; if (z1 == z2 && z2 == z3 && z1 != EMPTY){ return z1; } int d1 = gameboard[0][0]; int d2 = gameboard[1][1]; int d3 = gameboard[2][2]; if (d1 == d2 && d2 == d3 && d1 != EMPTY){ return d1; } for(int row=0; row<gameboard.length; row++){ for (int col=0; col<gameboard.length; col++){ if(gameboard[row][col] == EMPTY) return -2; } } } return 0; } public static void main(String[] args) { createBoard(3,3); int turn = 0; int playerVal; int outcome; java.util.Scanner scan = new java.util.Scanner(System.in); do { displayBoard(); playerVal = (turn % 2 == 0)? NOUGHT : CROSS; if (playerVal == NOUGHT) System.out.println("O's turn"); else{ System.out.println("X's turn"); } System.out.print("Enter row and column:"); try { set(playerVal, scan.nextInt(), scan.nextInt()); } catch (IllegalArgumentException ex) {System.err.println(ex); } turn ++; outcome=winOrTie(); } while (-2 == outcome); displayBoard(); switch(outcome) { case NOUGHT: System.out.println("O wins!"); break; case CROSS: System.out.println("X wins!"); break; case 0: System.out.println("Tie."); break; } } static void set (int val, int row, int col) throws IllegalArgumentException { if (gameboard[row][col] == EMPTY) gameboard[row][col] = val; else throw new IllegalArgumentException("Player already there!"); } static void displayBoard() { for (int row=0; row<gameboard.length; row++) { System.out.print("|"); for (int col = 0; col<gameboard[row].length; col++) { switch (gameboard[row][col]) { case NOUGHT: System.out.print("O"); break; case CROSS: System.out.print("X"); break; default: System.out.print(" "); } System.out.print("|"); { System.out.println("\n-------\n"); } } } } }
It's supposed to be a Tic-Tac-Toe game that displays a 3x3 gameboard. I'm using NetBeans to run it, and it's displaying each space on a separate line. Apparently there's a problem with the createBoard() method on line 19. It won't let me use the variable gameboard, but it won't let me use any other name either. Can someone give me some idea as to what I need to do to make this work?