在tridomineering中寻找合法举措的问题

时间:2015-09-27 15:49:42

标签: java eclipse

我试图找到我的代码的问题,我创建一个游戏,类似于霸气,但有3个立方体而不是2.我遇到的问题是,probram无法区分玩家什么时候没有向左移动。这是完整的代码:

包装霸气;     import java.util.InputMismatchException;

// The game of Domineering
public class Domineering {
    // Array of board squares, true if occupied
    private boolean[][] squares;
    // For reading from the console
    public static final java.util.Scanner INPUT
        = new java.util.Scanner(System.in);
    // The player who plays their dominoes horizontally
    public static final boolean HORIZONTAL = false;
    // The player who plays their dominoes vertically
    public static final boolean VERTICAL = true;
    // The board is initially empty
    public Domineering(int rows, int columns) {
        squares = new boolean[rows][columns]; // Java initializes all array elements to false
    }

    // Create and play the game
    public static void main(String[] args) {
        System.out.println("Welcome to Domineering.");
        System.out.print("Enter number of board rows: ");
        int rows = INPUT.nextInt();
        System.out.print("Enter number of board columns: ");
        int columns = INPUT.nextInt();
        Domineering game = new Domineering(rows, columns);
        game.play();
    }

    public String toString() {
        String result = "  ";
        for(int i = 0; i < squares[0].length; i++) {
            result += (i + " ");
        }

        for(int row = 0; row < squares.length; row++) {
            result += "\n" + row;
            for(int column = 0; column < squares[0].length; column++) {
                if(squares[row][column]) {
                    result += " #";
                } else {
                    result += " .";
                }
            }
        }
        return result;
    }

    // Play until someone wins
    public void play() {
        boolean player = HORIZONTAL;
        boolean errorStat = true;       
        while(errorStat) {
            try { // Check for error in input
                while(true) {
                    System.out.println("\n" + this);
                    if(player == HORIZONTAL) {
                        System.out.println("Horizontal to play");
                    } else {
                        System.out.println("Vertical to play");
                    }
                    if(!(hasLegalMoveFor(player))) {
                        System.out.println("No legal moves -- you lose!");
                        return;
                    }
                    System.out.print("Row:");
                    int row = INPUT.nextInt();
                    System.out.print("Column:");                     
                    int column = INPUT.nextInt();

                    // Check if move is valid
                    if(isMoveValid(row, column, player)) {
                        playAt(row, column, player);
                        player = !player;
                        errorStat = false;
                    } else {
                        System.err.print("Invalid Move - Try Again!");
                        errorStat = true;
                    }
                }
            } catch (Exception e) {
                System.err.print("Invalid Move - Try Again!\n");
                INPUT.next(); INPUT.next();
                errorStat = true;
            }
        }
    }

    // Play a domino with its upper left corner at row, column.
    public void playAt(int row, int column, boolean player) {
        squares[row][column] = true;
        if(player == HORIZONTAL) {
            squares[row][column + 1] = true;
            squares[row][column + 2] = true;
        } else {
            squares[row + 1][column] = true;
            squares[row + 2][column] = true;
        }
    }

    // Return true if there is a legal move for the specified player
    public boolean hasLegalMoveFor(boolean player) {
        int rowOffset = 0;
        int columnOffset = 0;
        if(player == HORIZONTAL) {
            columnOffset = 2;
        } else {
            rowOffset = 2;
        }
        for(int row = 0; row < (squares.length - rowOffset); row++) {
            for(int column = 0; column < (squares[0].length - columnOffset); column++) {
                if (!(squares[row][column] || squares[row + rowOffset][column + columnOffset])) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean isMoveValid(int row, int column, boolean player) {
        if(row > squares.length || row < 0) {
            return false;
        }
        else if(column > squares[0].length || column < 0) {
            return false;
        }

        if(player) { // Check vertical
            if(squares[row][column] || squares[row + 1][column]) {
                return false;
            }
            if(squares[row][column] || squares[row + 2][column]) {
                return false;
            }else {
                return true;
            }
        } else { // Check horizontal
            if(squares[row][column] || squares[row][column + 1]) {
                return false;
            }
            if(squares[row][column] || squares[row][column + 2]) {
                return false;
            }
                return true;
        }
    }
}

问题在于这个方法:

public boolean hasLegalMoveFor(boolean player) {
    int rowOffset = 0;
    int columnOffset = 0;
    if(player == HORIZONTAL) {
        columnOffset = 2;
    } else {
        rowOffset = 2;
    }
    for(int row = 0; row < (squares.length - rowOffset); row++) {
        for(int column = 0; column < (squares[0].length - columnOffset); column++) {
            if (!(squares[row][column] || squares[row + rowOffset][column + columnOffset])) {
                return true;
            }
        }
    }
    return false;
}

我在计算中做错了什么?

1 个答案:

答案 0 :(得分:0)

我看到的一个问题是您正在检查[Flags][row][column],而不是那些之间的平方。你应该检查这三个:

[row + rowOffset][column + columnOffset]