简体   繁体   English

如何在 Peg-Solitaire 中为我们的游戏板在 Java 中编写“获取字段”function

[英]How to program a “get field” function in Java for our game board in Peg-Solitaire

public class SolitaireBoard {

    private static final int empty = 0;
    private static final int occupied = 1;
    private static final int invalid = 2;
    public int col;
    public int row;
    public SolitaireBoard(){
        int [] [] board = {
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
            {1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 0, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1},
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
        };
    }
}

I need to programm a get field function that has row and col (column) as an input and returns the state of the specific field.我需要编写一个获取字段 function ,它具有行和列(列)作为输入并返回特定字段的 state。 I was not able to find any solution online.我无法在网上找到任何解决方案。

It seems your class should not have row,col attributs, but a board one, like看来您的 class 不应该有row,col属性,而是一个board ,比如

public class SolitaireBoard {
    private static final int empty = 0;
    private static final int occupied = 1;
    private static final int invalid = 2;
    private int[][] board;
    public SolitaireBoard(){
        this.board = new int[][]{
            {2, 2, 2, 1, 1, 1, 2, 2, 2}, // Should be {invalid, invalid, invalid, occupied, ...}
            {2, 2, 2, 1, 1, 1, 2, 2, 2},
            ...
        };
    }
}

Then build a method to access a box然后构建一个访问盒子的方法

public int getField(int row, int col){
    return this.board[row][col];
}

To manipulate more easily your constants, you could reduce their name, like EM, OC, IN and build the array with them为了更轻松地操作常量,您可以减少它们的名称,如EM, OC, IN并用它们构建数组

new int[][]{
    {IN, IN, IN, OC, OC, OC, IN, IN, IN},
    {IN, IN, IN, OC, OC, OC, IN, IN, IN},
    {IN, IN, IN, OC, OC, OC, IN, IN, IN},
    {OC, OC, OC, OC, OC, OC, OC, OC, OC},
    {OC, OC, OC, OC, EM, OC, OC, OC, OC},
    {OC, OC, OC, OC, OC, OC, OC, OC, OC},
    {IN, IN, IN, OC, OC, OC, IN, IN, IN},
    {IN, IN, IN, OC, OC, OC, IN, IN, IN},
    {IN, IN, IN, OC, OC, OC, IN, IN, IN},
};

You have to make board a property, so you can get a specific field from another method.您必须将 board 设为属性,以便您可以从其他方法获取特定字段。 If you want to access a field you have to count from zero.如果你想访问一个字段,你必须从零开始计数。 For example if you want to access a field from the first row, you have to type in the index 0 for the row.例如,如果您想从第一行访问一个字段,您必须输入该行的索引 0。

public class SolitaireBoard {

    private static final int empty = 0;
    private static final int occupied = 1;
    private static final int invalid = 2;
    public int[][] board;
    public int col;
    public int row;

    public SolitaireBoard(){
        board = new int[][]{
                {2, 2, 2, 1, 1, 1, 2, 2, 2},
                {2, 2, 2, 1, 1, 1, 2, 2, 2},
                {2, 2, 2, 1, 1, 1, 2, 2, 2},
                {1, 1, 1, 1, 1, 1, 1, 1, 1},
                {1, 1, 1, 1, 0, 1, 1, 1, 1},
                {1, 1, 1, 1, 1, 1, 1, 1, 1},
                {2, 2, 2, 1, 1, 1, 2, 2, 2},
                {2, 2, 2, 1, 1, 1, 2, 2, 2},
                {2, 2, 2, 1, 1, 1, 2, 2, 2},
        };
    }

    public int getField(int row, int column) {
        return board[row][column];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM