我正在玩一个叫1010的游戏!也许你们当中有人听说过。基本上,在编写清除算法时,我会遇到一些麻烦。

规则是,如果占用了任何行或任何列,则分别清除行和列。

得分是,每一步都获得a + 10 * b分。 a是输入段p中的平方数,b是清除的行和列的总数。

首先,我创建一个二维数组board [10] [10],用一个空的正方形填充board [] []中的每个元素。

在Square类中,它具有unset()->“清空正方形”和boolean status()->“判断正方形是否为空”的公共无效方法。在棋子类中,它具有int numofSquare->“ return计算分数的每块中的平方数”

特别是,如果行和列都被占用,因为它们在二维数组中相互交叉,我不知道如何写。
它在某些情况下无法通过测试,在这种情况下,某些正方形没有清除,但应该清除了,我很确定这是逻辑问题。

我的想法是:


循环浏览第一行和第一列中的正方形,记录占用的正方形数量(使用c和r);如果两者均为10,则清除行和列,否则清除行或列,或者什么也不做。
将c&r重置为0,在第二行第二列中遍历正方形...
更新分数。


基本上,难点在于如果我将清除列和清除行算法分开,我将首先判断行或列,然后清除它们。但是,由于每一列至少包含一个正方形属于该行,并且每一行至少包含一个正方形属于该列,所以当行和列都充满时会出错。

感谢帮助。

import java.util.ArrayList;

public class GameState{
    public static final int noOfSquares = 10;
    // the extent of the board in both directions
    public static final int noOfBoxes   = 3;
    // the number of boxes in the game

    private Square[][] board; // the current state of the board
    private Box[] boxes;      // the current state of the boxes
    private int score;        // the current score

    // initialise the instance variables for board
    // all squares and all boxes are initially empty
    public GameState()
    {
        getboard();
        score = 0;
        board = new Square[10][10];
        for(int i =0;i<board.length;i++){
            for(int j =0;j<board[i].length;j++){
                board[i][j] = new Square();
            }
        }

        boxes = new Box[3];
        for(int k =0;k<boxes.length;k++){
            boxes[k] = new Box();
        }
    }

    // return the current state of the board
    public Square[][] getBoard()
    {
        return board;
    }

    // return the current score
    public int getScore()
    {
        return score;
    }

    // place p on the board with its (notional) top-left corner at Square x,y
    // clear columns and rows as appropriate
    int r =0;
    int c = 0;
    int rowandcolumn = 0;
    for (int row=0;row<10;row++){
         for (int column=0;column<10;column++) {


           if (board[row][column].status() == true){
             c = c + 1;
             if( c == 10 ) {
              rowandcolumn = rowandcolumn + 1;

              for(int z=0;z<10;z++){
                 board[row][z].unset(); //Clear column

                }

             }
            }

           if (board[column][row].status() == true){
             r = r + 1;
             if(  r == 10) {
              rowandcolumn = rowandcolumn + 1;

              for(int q=0;q<10;q++){
                 board[q][row].unset(); //Clear row

                }

             }
            }
      }
                 r=0; //reset
                 c=0;
    }
      score = score + p.numberofBox()+10*rowandcolumn;
}

最佳答案

这个怎么样

void Background::liquidate(int &score){
int arr_flag[2][10];        //0 is row,1 is column。
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 10; j++)
    {
        arr_flag[i][j] = 1;
    }
}

//column
for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (arr[i][j].type == 0)
        {
            arr_flag[0][i] = 0;
            break;
        }
    }
}
//row
for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        if (arr[j][i].type == 0)
        {
            arr_flag[1][i] = 0;
            break;
        }
    }
}

//clear column
for (int i = 0; i < 10; i++)
{
    if (arr_flag[0][i] == 1)
    {
        for (int j = 0; j < 10; j++)
        {
            arr[i][j].Clear();
        }
    }
}
//clear row
for (int i = 0; i < 10; i++)
{
    if (arr_flag[1][i] == 1)
    {
        for (int j = 0; j < 10; j++)
        {
            arr[j][i].Clear();
        }
    }
}


}

10-05 18:33
查看更多