Question

One of my assignments for class is to use the shell the instructor provided and make the tic tac toe game. I am not allowed to change any function signatures or remove any of the instructor's code. We have not learned how to do arrays, so this assignment does not use them. The board is printed after every turn, where ever there is the MARKER.EMPTY, it should have an empty space. However I whenever I print it the marker "EMPTY" is printed. I apologize in advanced as my code is very messy, I am in my first programming class.

It should look like

X | | O

Instead its printing like

X | EMPTY | O

I would appreciate any answers. This is the last part of the assignment that I can't figure out. Thank you for any help.

import java.util.Scanner;
public class TicTacToe
{

public enum Marker
{
    EMPTY,
    X,
    O
};

private static Marker position1 = Marker.EMPTY;
private static Marker position2 = Marker.EMPTY;
private static Marker position3 = Marker.EMPTY;
private static Marker position4 = Marker.EMPTY;
private static Marker position5 = Marker.EMPTY;
private static Marker position6 = Marker.EMPTY;
private static Marker position7 = Marker.EMPTY;
private static Marker position8 = Marker.EMPTY;
private static Marker position9 = Marker.EMPTY;

private static Marker turn = Marker.X;

public static void main(String[]args)
{
Scanner scan = new Scanner(System.in);
printExampleboard();
for (int x = 0;x<10;x++)
    {
    if (hasWon(Marker.X) == true)
    {
    System.out.println("Player X has won!");
    System.exit(0);
    }
    if (hasWon(Marker.O) == true)
    {
    System.out.println("Player O has won!");
    System.exit(0);
    }
    boolean tie = isTie();
    if (tie == true)
        {
        System.out.println("This game is a tie!");
        }   
    else
    {
    if (x%2 == 1)
        {
        boolean oinput = true;
        System.out.println("Player O, please enter a position to play");
        while (oinput == true)
            {
            turn = Marker.O;
            int pos = scan.nextInt();
            if (isValidMove(pos) == true)
                {
                markTheBoard(turn, pos);
                printBoard();
                oinput = false;
                }
            else 
                {
                System.out.println("Position " + pos + " is already occupied or an invalid , please pick another position."); 
                }
            }
        }
    else
        {
        boolean input = true;
        System.out.println("Player X, please enter a position to play");
        while (input == true)
            {
            turn = Marker.X;
            int pos = scan.nextInt();
            if (isValidMove(pos) == true)
                {
                markTheBoard(turn, pos);
                printBoard();
                input = false;
                }
            else 
                {
                System.out.println("Position " + pos + " is already occupied or an invalid , please pick another position."); 
                }
            }
        }
    }   
    }


}

/**
  * This function returns true if the spot is empty, false if not. boolean
  */
public static boolean isValidMove(int pos)
{
    if (pos>0 && pos <10)
        {
        Marker m = Marker.EMPTY;
        switch(pos)
            {
            case 1: 
                {
                m = position1; 
                break;
                }
            case 2: 
                {
                m = position2; 
                break;
                }
            case 3: 
                {
                m = position3; 
                break;
                }
            case 4: 
                {
                m = position4; 
                break;
                }
            case 5: 
                {
                m = position5; 
                break;
                }
            case 6: 
                {
                m = position6; 
                break;
                }
            case 7: 
                {
                m = position7; 
                }
                break;
            case 8: 
                {
                m = position8; 
                break;
                }
            case 9: 
                {
                m = position9; 
                break;
                }
            }
                if (m == Marker.EMPTY)
                    {
                    return true;
                    }
                    else 
                    {
                    return false;
                    }
        }
    else 
        {

        return false;
        }   
}

public static void printExampleboard()
{
    System.out.println("We are about to play Tic Tac Toe, when asked to enter a position,\nplease refer to the below map:");
    for (int x = 0; x < 9; x+=3)
    {
    System.out.println("-------------");
    System.out.println("| " + (x + 1) + " | " + (x + 2) + " | "+ (x + 3) + " |");
    }
    System.out.println("-------------");
}
/**
 * This method will print the board as shown in the above example.
 */
public static void printBoard()
{
System.out.println("-------------");
System.out.println("| " + position1 + " | " + position2 + " | " + position3 + " | ");
System.out.println("-------------");
System.out.println("| " + position4 + " | " + position5 + " | " + position6 + " | ");
System.out.println("-------------");
System.out.println("| " + position7 + " | " + position8 + " | " + position9 + " | ");
System.out.println("-------------");    
}

/**
 * Checks if a particular player has won.
 * 
 * @param m The player to check
 * @return true if the player won, false if not boolean
 */
public static boolean hasWon(Marker m)
{
if ((position1 == m && position2 == m && position3 == m) ||(position4 == m && position5 == m && position6 == m) ||
    (position7 == m && position8 == m && position9 == m) ||(position1 == m && position4 == m && position7 == m) ||
    (position2 == m && position5 == m && position8 == m) ||(position3 == m && position6 == m && position9 == m) ||
    (position1 == m && position5 == m && position9 == m) ||(position3 == m && position5 == m && position7 == m))
    {
    return true;
    }
return false;
}

/**
 * Checks if the board is full with no winner
 * @return true if the board is full with no winner, false otherwise boolean
 */
public static boolean isTie()
{
boolean tie = true;
Marker m = Marker.EMPTY;
if (position1 != m && position2 != m && position3 != m && 
    position4 != m && position5 != m && position6 != m && 
    position7 != m && position8 != m && position9 != m)
    {
    return tie;
    }
else
    {
    tie = false;
    return tie;
    }
}

/**
 * Mark the given position with the given marker
 * @param m The marker of the player given
 * @param pos The position that we are marking
 */

public static void markTheBoard(Marker m, int pos)
{
switch (pos)
    {
        case 1: 
            {
            position1 = m;
            break;
            }
        case 2: 
            {
            position2 = m;
            break;
            }
        case 3: 
            {
            position3 = m;
            break;
            }
        case 4: 
            {
            position4 = m;
            break;
            }
        case 5:
            {
            position5 = m;
            break;
            }
        case 6:
            {
            position6 = m;
            break;
            }
        case 7:
            {
            position7 = m;
            break;
            }
        case 8:
            {
            position8 = m;
            break;
            }
        case 9:
            {
            position9 = m;
            break;
            }

    }

}

}

Était-ce utile?

La solution

If you're allowed to, you should implement the toString() method of the Marker enum to return the correct value. (Return " " if this == EMPTY, "X" if this == X, and so on.) To implement methods on enumerations, put a semicolon after the last member, then implement the method just as usual:

public enum Marker
{
    EMPTY, X, O;

    public String toString() { /* code here */ }
};

If you can't, then for each position you can check if the Marker is EMPTY, and if so print a space, and otherwise print the Marker.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top