Question

For Some reason when I programmed getWinner() it only works in 2 cases (for the last row). I have everything else as far as diagonals and columns, but row 2 (well, three, but arrays, so 2) only works with o's basically. x's only win when o's are in 0,0 and 1,1 or 0,2 and 1,1. (Essentially,only if the o's are in a top corner and the center.

import java.util.Scanner;  
public class TicTacToeRunner  
{  
    public static void main(String[] args)  
    {  
        Scanner in = new Scanner(System.in);  
        String player = "x";  
        TicTacToe game = new TicTacToe();  
        boolean done = false;  
        while (!done)  
        {  
            System.out.print(game.toString());   
            System.out.print(  
            "Row for " + player + " (-1 to exit): ");  
            int row = in.nextInt();  
            if (row < 0)  
            { 
            done = true;  
            } 
            else if (row > 2) 
            System.out.println("Invalid, please enter a number from 0 to 2"); 
            else
            {  
                System.out.print("Column for " + player + ": ");  
                int temp = in.nextInt(); 
                if (temp>2) 
                System.out.println("Invalid, please enter a number from 0 to 2"); 
                else
                { 
                int column = temp;  
                game.set(row, column, player);  
                if (player.equals("x"))   
                player = "o";   
                else 
                player = "x";   
            } 


            if(game.getWinner().equals("x")) 
            {     
                System.out.println("x is the winner!"); 
                done = true; 
            } 
            if(game.getWinner().equals("o")) 
            {     
                System.out.println("o is the winner!"); 
                done = true; 
            } 
            } 
            } 
    }  

}

public class TicTacToe  
{  
    private String[][] board;  
    private static final int ROWS = 3;  
    private static final int COLUMNS = 3;  
    public TicTacToe()  
    {  
        board = new String[ROWS][COLUMNS];  
        for (int i = 0; i < ROWS; i++)  
        for (int j = 0; j < COLUMNS; j++)  
            board[i][j] = " ";  
    }  
    public void set(int i, int j, String player)  
    {  
        if (board[i][j].equals(" "))  
            board[i][j] = player;  
            //what if the spot is filled??? 
    }  
    public String toString()  
    {  
        String r = "";  
        for (int i = 0; i < ROWS; i++)  
        {  
            r = r + "|";  
            for (int j = 0; j < COLUMNS; j++)   

                r = r + board[i][j];  
                r = r + "|\n";  
        }  
        return r;  
    }  
    public String getWinner()  //which one isn't working?  I tried a few attempts at the third col and third row, and both worked. 
    { 
        for (int i = 0; i <= 2; i++) //HORIZONTAL 
           {  
               if (board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])) 
                    return board[i][0]; 
        } 
        for (int j = 0; j <= 2; j++) //VERTICAL 
          {  if (board[0][j].equals(board[1][j]) && board[1][j].equals(board[2][j])) 
            return board[0][j]; 
        } 
        if (board[0][0].equals(board[1][1]) && board[1][1].equals(board[2][2])) 
            return board[0][0]; 
        if (board[0][2].equals(board[1][1]) && board[1][1].equals(board[2][0])) 
            return board[1][1];                 
        return " "; 
        } 

} Any advice?

Was it helpful?

Solution

If no space is taken in a row or column or diagonal, then they will all be " ". However, that will still satisfy a condition, such as

board[i][0].equals(board[i][1]) && board[i][1].equals(board[i][2])

Then it will return " " right away and not check the other positions. " " is the winner! But you also use " " to indicate that there's no winner.

Add an additional check to your conditions to make sure that the first position you check is not equal to " " before checking the rest of the conditional expression:

!" ".equals(board[i][0]) &&
board[i][0].equals(board[i][1]) &&
board[i][1].equals(board[i][2])

And you can change the other conditions similarly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top