Help with tic-tac-toe game

Hi guys, i'm making a tic-tac-toe game..

I have a boolean method with a return statement, but when i try to compile it(in bluej) it says missing return statement.. I cant figure out where ive gone wrong.

Heres the code:

(Bluej highlights the ending bracket of the isRow method and says missing return statement, even though i have put a return true;. Also, i tried adding an else return false; after the return true, but same error.)

import java.io.*;

publicclass TicTacToeDemo

{

publicvoid display()throws IOException

{

InputStreamReader isr =new InputStreamReader(System.in);

BufferedReader br =new BufferedReader(isr);

char arr[][] ={{'-','-','-'},{'-','-','-'},{'-','-','-'}};

char symbol[] ={'O','X'};

int i = 0;

boolean isDraw=true;

System.out.println("\t\t\t\tTIC TAC TOE");// 4 \t's center word

print(arr);

do

{

System.out.println("\t\t\t\tTIC TAC TOE");

print(arr);

System.out.println("PLAYER "+(i%2==0?"1":"2"));// IF i%2==0 PRINT 1 ELSE PRINT 2

System.out.println("Enter your number");

int n = Integer.parseInt(br.readLine());

char s = symbol[i%2];

fillArray(arr,s,n);

i++;

print(arr);

if (isRow(arr,s) || isColumn(arr,s) || isDiagonal(arr,s))

{

System.out.println("Player "+(i%2+1)+"Won");

isDraw=false;

}

}

while (i<9);

if (isDraw)

System.out.println("Game is Draw");

}

privatevoid print(char arr[][])

{

for (int i=0;i<3;i++)

{

for (int j=0;j<3;j++)

{

System.out.print(arr[i][j]+"\t");

}

System.out.println();

}

}

privatevoid fillArray(char arr[][],char s,int n)

{

if (n<=3)

{

arr[0][n-1]=s;

}

elseif (n>3 && n<=6)

{

arr[1][n-4]=s;

}

elseif (n>6 && n<=9)

{

arr[2][n-7]=s;

}

else

{

System.out.println("Invalid Number.");

}

}

privateboolean isRow(char arr[][],char s)

{

for (int i=0;i<3;i++)

{

if (arr[i][0]==arr[i][1] && arr[i][1]==arr[i][2])

{

if (arr[i][0]==s)

returntrue;

}

}

}

privateboolean isColumn(char arr[][],char s)

{

for (int i=0;i<3;i++)

{

if (arr[0][i]==arr[1][i] && arr[1][i]==arr[2][i])

{

if(arr[0][i]==s)

returntrue;

}

}

}

privateboolean isDiagonal(char arr[][],char s)

{

if (arr[0][0]==arr[1][1] && arr[1][1]==arr[2][2])

{

if (arr[0][0]==s)

returntrue;

}

elseif (arr[0][2]==arr[1][1] && arr[1][1]==arr[2][0])

{

if (arr[0][2]==s)

returntrue;

}

else

returnfalse;

}

}

Message was edited by:

Overkill

[7419 byte] By [Overkilla] at [2007-11-27 11:19:32]
# 1

So what would it return if it doesn't execute one of those loops? Or, regarding "isDiagonal", if it enters the first if branch but not the second, nested one?

CeciNEstPasUnProgrammeura at 2007-7-29 14:37:17 > top of Java-index,Java Essentials,Java Programming...
# 2

I think you rather want to do it like this:

boolean b = false;

for(... && !b ...) { // iterate as long as necessary, abort with b == true

for (... && !b ...) {

b = (arr[i][0]==s);

}

}

return b;

CeciNEstPasUnProgrammeura at 2007-7-29 14:37:17 > top of Java-index,Java Essentials,Java Programming...
# 3

private boolean isRow(char arr[][],char s)

{

for (int i=0;i<3;i++)

{

if (arr[i][0]==arr[i][1] && arr[i][1]==arr[i][2])

{

if (arr[i][0]==s)

return true;

}

}

}

if the condition if (arr[i][0]==arr[i][1] && arr[i][1]==arr[i][2])

was false so return true; won't work, and so the method is left without a return value.

you can solve the problem by modifying this code to this:

private boolean isRow(char arr[][],char s)

{

for (int i=0;i<3;i++)

{

if (arr[i][0]==arr[i][1] && arr[i][1]==arr[i][2])

{

if (arr[i][0]==s)

return true;

}

}

return false;

}

QussayNajjara at 2007-7-29 14:37:17 > top of Java-index,Java Essentials,Java Programming...
# 4

Ohkkk i got what you guys meant.. Thanks a lot everyone. :)

Solved it- works fine now :D

Overkilla at 2007-7-29 14:37:17 > top of Java-index,Java Essentials,Java Programming...