Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Kyo Yamamoto
Kyo Yamamoto
516 Points

Having troubles with making Tic Tac Toe#java

I am a beginner in Java.

I am getting compile errors with " getCurrentPlayer". if anyone helps me out, much appreciated. Many thanks.

import java.util.Scanner;

public class Game {

private static char[][] board;
private  char getCurrentPlayerMark(){
    return getPlayerMark;
}

public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
    board = new char[3][3];
    char currentPlayerMark = 'x';
    initializeBoard();

    System.out.println("Tic-Tac-Toe!");
    do
        {
            System.out.println("Current board layout:");
            printBoard();
            int row;
            int col;
        do
            {
                System.out.println("Player " + getCurrentPlayerMark() + ", enter an empty row and column to place your mark!");
                row = scan.nextInt()-1;
                col = scan.nextInt()-1;
            }
        while (placeMark(row, col));
        changePlayer();
        }
    while(!checkForWin() && !isBoardFull());
    if (isBoardFull() && !checkForWin())
        {
            System.out.println("The game was a tie!");
        }
    else
     {
            System.out.println("Current board layout:");
            printBoard();
            changePlayer();
            System.out.println(Character.toUpperCase(getCurrentPlayerMark()) + " Wins!");
        }
}

public static void initializeBoard() {

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


        for (int j = 0; j < 3; j++) {
            board[i][j] = '-';
        }
    }
}

public static void printBoard() {
    System.out.println("-------------");

    for (int i = 0; i < 3; i++) {
        System.out.print("| ");
        for (int j = 0; j < 3; j++) {
            System.out.print(board[i][j] + " | ");
        }
        System.out.println();
        System.out.println("-------------");
    }
}


public static boolean isBoardFull() {
    boolean isFull = true;

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            if (board[i][j] == '-') {
                isFull = false;
            }
        }
}

    return isFull;
}

public static boolean checkForWin() {
    return (checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin());
}


private static boolean checkRowsForWin() {
    for (int i = 0; i < 3; i++) {
        if (checkRowCol(board[i][0], board[i][1], board[i][2]) == true) {
            return true;
        }
    }
    return false;
}


private static boolean checkColumnsForWin() {
    for (int i = 0; i < 3; i++) {
            return true;
        }
    }
    return false;
}


private static boolean checkDiagonalsForWin() {
    return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true) || (checkRowCol(board[0][2], board[1][1], board[2][0]) == true));
}


private static boolean checkRowCol(char c1, char c2, char c3) {
    return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}


public static void changePlayer() {
    if (currentPlayerMark == 'x') {
        currentPlayerMark = 'o';
   }
    else {
        currentPlayerMark = 'x';
    }
}

public static boolean placeMark(int row, int col) {


    if ((row >= 0) && (row < 3)) {
        if ((col >= 0) && (col < 3)) {
            if (board[row][col] == '-') {
                board[row][col] = currentPlayerMark;
                return true;
            }
        }
    }

    return false;

} }

Juraj Sallai
Juraj Sallai
7,188 Points

Hi Kyo,

Variable getPlayerMark is not declared anywhere in your code. Method getCurrentPlayerMark() does not know what to return.

private  char getCurrentPlayerMark(){
    return getPlayerMark;
}

1 Answer

Kyo Yamamoto
Kyo Yamamoto
516 Points

Aha!Thank you, Jurai!