Hi, I am in need of help with this tic tac toe game. I have been working on it longer than I care to admit. I am trying to get it to compile correctly. I am working on the winorTie method and been having trouble figure out what code to insert. I understand that I need to write code that goes through the game board row by row and counts how many X's or O's have been marked. Yet every time I've inserted code it doesn't compile correctly. Right now I have the foundation for the if statement but unsure about the code.
Whenever I compile I get the below in the output section.
| | | |
------ | | | | ------ | | | | ------ -0's turn-
When compiled correctly it is supposed to look like this in the output section.
|||| ------- |||| ------- |||| ------- --O’s turn-- |O| | | ------- |||| ------- |||| ------- --X’s turn-- |O|X| | ------- |||| ------- |||| ------- --O’s turn-- |O|X| | ------- | |O| | ------- |||| ------- --X’s turn-- |O|X| | ------- | |O| | ------- |X| | | ______ —O’s turn— |O|X| | ------- | |O| | ------- |X| |O| O wins!
Here is my current code..
/* * To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoegame;
/**
*
* @author Doug Henderson
*/
public class tictactoegame {
static int [] [] gameboard;
static final int EMPTY = 0;
static final int NOUGHT = -1;
static final int CROSS = 1;
static void set (int val, int row)
throws IllegalArgumentException {
int col = 0;
if (gameboard[row] [col] == EMPTY)
gameboard[row] [col] = val;
else throw new
IllegalArgumentException("Player already there!");
}
static void displayBoard () {
for (int[] gameboard1 : gameboard) {
System.out.print("|");
for (int c = 0; c < gameboard1.length; c++) {
switch (gameboard1[c]) {
case NOUGHT:
System.out.print("0");
break;
case CROSS:
System.out.print("X");
break;
default: //Empty
System.out.print(" ");
}
System.out.print("|");
}
System.out.println("\n------\n");
}
}
static void createBoard(int rows, int cols) {
gameboard = new int [rows] [cols];
}
static int winOrTie() {
if ();
return ;
else if ();
return ;
else if ();{
return ;
else {
return ;
}
/**
* @param args the command line arguments
*/ /**
* @param args the command line arguments
*/
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 ("\n-0's turn-");
} else {
System.out.println("\n-X's turn-");
}
System.out.print("Enter row and Column:");
try {
set(playerVal, scan.nextInt());
} catch (IllegalArgumentException ex)
{System.err.println(ex);}
turn ++;
outcome = winOrTie();
} while ( outcome == -2 );
displayBoard();
switch (outcome) {
case NOUGHT:
System.out.println("0 wins!");
break;
case CROSS:
System.out.println("X wins!");
break;
case 0:
System.out.println("Tie.");
break;
}
}
}