井字游戏:我如何得到enum打印空白空间



我的课堂作业之一是使用老师提供的shell制作井字游戏。我不允许更改任何函数签名或删除任何指导员的代码。我们还没有学习过如何操作数组,所以这个赋值没有使用数组。每个回合后,在有标记的地方打印板。EMPTY,它应该有一个空的空间。然而,每当我打印它时,标记"EMPTY"被打印出来。我提前道歉,因为我的代码很乱,这是我第一次上编程课。

看起来应该像

X | | O

而不是打印成

X | EMPTY | O

我将感谢任何答案。这是作业中最后一个我想不明白的部分。谢谢你的帮助。

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;
            }
    }
}

}

如果允许,您应该实现Marker enum的toString()方法以返回正确的值。(如果是this == EMPTY返回" ",如果是this == X返回"X",依此类推)要在枚举上实现方法,请在最后一个成员后面加上分号,然后像往常一样实现该方法:

public enum Marker
{
    EMPTY, X, O;
    public String toString() { /* code here */ }
};

如果不能,则可以检查每个位置的Marker是否为EMPTY,如果是,则打印一个空格,否则打印Marker

最新更新