1

I am creating a tic tac toe game and have created the board using a single dimension array.

I have the following code but it returns a true even when the board is still empty and a lot mor emoves can be played. Can someone please tell me where I am going wrong. Thanks

public boolean gameIsADraw() {
    for (int i = 0; i <= 9; i++) {
        if (!board[i].equals(" ")){
            if (gameIsAWin() == false) {

            }
            return true;
        }
        }

    return false;
}

public boolean gameIsAWin() {
        for (String s: winningConditions) {
            if (winningSituations(s.charAt(0), s.charAt(1), s.charAt(2)) == true){
                return true;
            }
        }
        return false;
    }
uncleB
  • 35
  • 1
  • 4
  • 8
  • 1
    Have you tried implementing gameIsAWin() correctly? That might work. – Matthew Farwell Nov 29 '11 at 16:53
  • What is the expected functionality? You're returning a boolean. You want true to mean the game is a draw, false to mean that either someone has won OR the game is not over? – Jon Newmuis Nov 29 '11 at 16:55
  • `i < 9`, since the board has 9 tiles (index in [0..8]). – Viruzzo Nov 29 '11 at 16:55
  • @Matthew Have pasted the gameIsAWin() method - that wroks correctly thought – uncleB Nov 29 '11 at 16:57
  • What does board look like? Since the method returns true you probably have something else than a space in the boxes. I wouldn't recommend you using spaces anyhow since it makes it harder to debug. Why not use a character that's not used in the game (I'm guessing any character that's not a X or O)? – Magnus Winter Nov 29 '11 at 16:57
  • @viruzzo - I have used 10 indexes and left index 0 as NOT IN USE just for simplicty sake – uncleB Nov 29 '11 at 17:03
  • @jonathan Yes what you mentioned in your comment is what i want – uncleB Nov 29 '11 at 17:04
  • @magnus - I preffered to use a space because I'm also printing out a simulation of the board in the console and the spaces make it easier for printing a grid. I would have pasted my whole code but it is very very long – uncleB Nov 29 '11 at 17:07
  • @uncleB I understood that you wanted to print the board as is, but something like if(!board[i].equals("q")){print(board[i]);} isn't that bad and your debugging will be much easier. But that's just my two cents. – Magnus Winter Nov 29 '11 at 17:21
  • Please can you post your full code? I would recommend debugging using an IDE such as Eclipse. – davidfrancis Nov 29 '11 at 16:54
  • While they are pretty hard to read you'll find many solutions in [Code Golf: Tic Tac Toe](http://stackoverflow.com/q/2245801/2509). As usual, don't take [tag:code-golf] to be indicative of good programming style, and recall that *new* gold questions (and other objective programming puzzles) should go to [CodeGolf.SE](http://codegolf.stackexchange.com/). – dmckee --- ex-moderator kitten Nov 29 '11 at 19:50

3 Answers3

0

If you have a board 3x3 then 3 is the empty boxes you should be searching for as if 3 boxes are empty then still there is hope for both players, I hope you like my answer thanks.

similarly for a board of 4x4 you should check

if(empty>=4) // game left empty
justnajm
  • 4,422
  • 6
  • 36
  • 56
0

You could try:

public boolean gameIsADraw(){
   for(int i = 0; i <= 9; i++){
     if(!board[i].equals(" ") && !gameIsAWin){
        break;
     }else if(i == 9) return true;
   }
 return false; 
}

I think this will work, but I haven't tested it so sorry if it doesn't. The basic principal is that if any square is empty, and somebody has won the game the for loop will break, causing the method to return false. However, if the for loop does reach the last index, their must be a draw so the method returns true.

Andy
  • 3,600
  • 12
  • 53
  • 84
  • You'll need a return after the `for` loop here or else this won't compile. Think it might be a typo; is that `return false` supposed to be outside the `for` loop? – Jon Newmuis Nov 29 '11 at 17:09
  • @JonathanNewmuis thanks for that. Yes it was a typo and I have now moved the `return false` to outside the `for` loop! – Andy Nov 29 '11 at 17:13
0

In the comments above, you confirmed that there are three situations:

  • If someone has won the game, return false
  • If the game is not over, return false
  • Otherwise, the game is a draw, return true

Assuming gameIsAWin() works correctly, you can just check for those situations individually:

public boolean gameIsADraw() {
    // If someone has won the game, return false
    if (gameIsAWin()) {
        return false;
    }

    // If the game is not over, return false
    for (int i = 0; i <= 9; i++) {
        if (" ".equals(board[i])) {
            return false;
        }
    }

    // Otherwise, the game is a draw, return true
    return true;
}
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • It probably would make sense to check that the game has not been won first!] – Andy Nov 29 '11 at 17:16
  • the statement if (" ".equals(board[i])) { gives a compilation error. am quite a beginner so not quite sure why...any suggestions? – uncleB Nov 29 '11 at 17:23
  • Please explain the compilation error, what exactly does it say. Though, you'd probably be better using `!board[i].equals(" ")` instead! However, I have just thought, is the array of Strings or JButtons or neither? – Andy Nov 29 '11 at 17:27
  • If `board[i]` is a String (hence `board` is a String[]), this should work. Doing it as `" ".equals(board[i])` instead of `board[i].equals(" ")` allows you to avoid having to explicitly state the case where `board[i]` could be null. Also note that chicking `if (!board[i].equals(" "))` is **not** the same, as it will change the condition in which you have to return. – Jon Newmuis Nov 29 '11 at 18:00