user12699544
user12699544

Reputation:

Tic-Tac-Toe game results in draw every time

I'd like to think the code is pretty clear. I am a beginner programmer and every thing looks fine to me. What should you do in situations like these when everything looks perfect but the program doesn't run as planned?

In short, I'm asking to advice for outlook on coding projects & a solution as to why my method I spent over 45 minutes on does nothing.

import java.util.Scanner;

public class TicTacToe {

    public static boolean elementsAreAllEqual(char[] arr) {

        for(int i = 0; i < arr.length; i++) {
            if(arr[i] != arr[i + 1]) {
                return false;
            }
        }
        return true;
    }

    public static boolean some1HasWon(char[][] g) {

        char[] treE = new char[3];

        // horizontal straight lines (rows of 3 alike)
        for(int r = 0; r < g.length; r++) {
            for(int c = 0; c < g.length; c++) {
                treE[c] = g[r][c];
            }
            if(elementsAreAllEqual(treE)) {
                            return true;
                    }
            treE = new char[3];
        }

        // vertical straight lines (columns of 3 alike)
        for(int c = 0; c < g.length; c++) {
                        for(int r = 0; r < g.length; r++) {
                                treE[r] = g[c][r];
                        }
                        if(elementsAreAllEqual(treE)) {
                            return true;
                    }
                        treE = new char[3];
                }

        // upward diagonal
        for(int i = 0; i < g.length; i++) {
            treE[i] = g[g.length-i-1][i];
        }
        if(elementsAreAllEqual(treE)) {
            return true;
        }
        treE = new char[3];

        // downward diagonal
        for(int i = 0; i < g.length; i++) {
            treE[i] = g[i][i];
        }
        if(elementsAreAllEqual(treE)) {
                        return true;
                }

        return false;
    }

    public static void main(String args[]) {

        char[][] grid = new char[3][3];
        Scanner s = new Scanner(System.in);
        System.out.println("Tic-Tac-Toe\n");

        int goCount = 0;
        char player;
        boolean draw = true;
        boolean end = false;

        do {
            if(goCount % 2 == 0) {
                player = 'X';
                System.out.print("'" + player + "', choose your location (row, column): ");
            }
            else {
                player = 'O';
                System.out.print("'" + player + "', choose your location (row, column): ");
            }
            String position = s.nextLine();
            String[] p = position.split(" ");
            int[] pos = {Integer.parseInt(p[0]), Integer.parseInt(p[1])}; 

            grid[pos[0]][pos[1]] = player;

            System.out.println();

            for(int y = 0; y < 3; y++) {
                System.out.print("\t");
                for(int x = 0; x < 3; x++) {
                    if(grid[y][x] != ' ') {
                        System.out.print(grid[y][x] + " ");
                    }
                    else {
                        System.out.print("  ");
                    }
                }
                System.out.println();
            }

            System.out.println();

            if(goCount > 4) {
                if(some1HasWon(grid)) {
                    end = true;
                }
                else if(goCount >= 8) {
                    draw = true;
                    end = true;
                }
            }
            goCount++;
        }
        while(end != true);

        if(draw) {
            System.out.println("The game was a draw.");
        }
        else if(goCount % 2 == 0) {
            System.out.println("Player 'X' wins the game.");
        }
        else {
            System.out.println("Player 'O' wins the game.");
        }
    }

}

Upvotes: 1

Views: 82

Answers (1)

CaptainDaVinci
CaptainDaVinci

Reputation: 1065

Your method elementaAreAllEqual accesses elements outside the array when i = arr.length - 1, you should iterate only upto arr.length - 1.

public static boolean elementsAreAllEqual(char[] arr) {

        for(int i = 0; i < arr.length - 1; i++) {
            if(arr[i] != arr[i + 1]) {
                return false;
            }
        }
        return true;
    }

Your initialization of draw = true is the reason why you're getting a draw every time. Irrespective of the outcome of some1HasWon the variable draw is always true.

if(goCount > 4) {
   if(some1HasWon(grid)) {
        end = true;
        draw = false;
   } else if(goCount >= 8) {
         draw = true;
         end = true;
   }
}

Upvotes: 2

Related Questions