QUOTE(horace @ 22 Mar, 2007 - 02:38 AM)

for a start
CODE
public static void main(String[] args) throws java.io.IOException
{
initialize();
char_player = 'X';
the last line should be
CODE
char player = 'X';
now see if you can find any more errors
CODE
public class TicTacToe
{
static char[][] board;
public static void main(String[] args)
{
initialize();
char player = 'X';
while(inGame())
{
player_moves(player);
player = switch_player(player);
print_board();
}
print_winner();
}
void initialize()
{
board = new char[3][3];
for(int i = 0; i< 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = '_';
}
}
}
boolean inGame()
{
boolean winner;
winner = win('X');
if(winner)
return false;
winner = win('O');
if(winner)
return false;
int count = 0;
for(int i = 0; i < 3; i++)
{
int three_in_a_row = 0;
for(int j = 0; j < 3; j++)
{
if(board[i][j] != '_')
count++;
}
}
if(count == 9)
return false;
return true;
}
void player_moves(char player)
{
Scanner s = new Scanner(System.in);
int move = 0;
boolean move_legal = false;
while(! move_legal)
{
System.out.println("Enter number for next " +player+ "move.");
move = s.nextInt();
move_legal = legal_move(move);
}
draw(move,player);
}
char switch_player(char player)
{
if(player == 'X')
return 'O';
else
return 'X';
}
void print_board()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.println(board[i][j] + " ");
}
System.out.println();
}
}
void print_winner()
{
if(win('X'))
System.out.println("X is the winner");
else if(win('O'))
System.out.println("O is the winner");
else
System.out.println("No winner");
}
boolean win(char c)
{
if(board[0][0] == c && board[0][1] == c && board[0][2] == c)
return true;
if(board[1][0] == c && board [1][1] == c && board[1][2] == c)
return true;
if(board[2][0] == c && board[2][1] == c && board[2][2] == c)
return true;
if(board[0][0] == c && board[1][0] == c && board[2][0] == c)
return true;
if(board[0][1] == c && board[1][1] == c && board[2][1] == c)
return true;
if(board[0][2] == c && board[1][2] == c && board[2][2] == c)
return true;
if(board[0][0] == c && board[1][1] == c && board[2][2] == c)
return true;
if(board[0][2] == c && board[1][1] == c && board[2][0] == c)
return true;
return false;
boolean legal_move(int position)
{
if(position < 1 || position > 9)
{
System.out.println("Error: invalid position" + position);
return false;
}
else
{
int i = (position - 1)/3;
int j = (position -1)%3;
if(board[i][j] != '_')
{
System.out.println("You can't move here. The square is occupied.");
return false;
}
return true;
}
}
void draw(int position, char c)
{
int i = (position - 1)/3;
int j = (position - 1)%3;
board[i][j] = c;
}
}
}
I am getting a "; expected" error on line 127. I cant sem to find the error (i.e. there are semicolons after each statement). Also, in terms of efficiency (less running time), would a switch statement be more efficient than a series of if statements?
This post has been edited by tukeywilliams: 22 Mar, 2007 - 04:39 PM