逻辑检查10 * 10游戏

时间:2015-05-24 10:56:44

标签: java arrays for-loop logic

我正在做一款名为1010的游戏!可能有些人听说过它。基本上,我在编写清除算法时遇到了一些麻烦。

规则是这样的,如果任何行或任何列被占用,则分别清除行和列。

得分是每次移动获得 a + 10 * b 点。 a 是输入片段中的平方数 p b 是已清除的行和列的总数。

首先,我创建了一个二维阵列板[10] [10],用空方块将板[] []中的每个元素包起来。

在Square类中,它具有unset() - >的公共 void 方法。 "清空广场" &安培; 布尔 status() - > "判断square是否为空"在片段类中,它有 int numofSquare - > "返回每个片段中的平方数以进行分数计算"

特别是,如果行和列都被占用,我不知道如何写它,因为它们在二维数组中相互交叉。 在某些条件下测试失败,其中一些方块没有被清除但是它们应该被清除,我很确定是逻辑问题。

我的想法是:

  1. 循环第一行和第一列的正方形,记录占用的正方形数(使用 c和r );如果两者都是10,则清除行和列,否则清除行或列或不执行任何操作。

  2. c & r 重置为0,在第二行,第二列中循环播放广场......

  3. 更新分数。

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

    感谢您的帮助。

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

2 个答案:

答案 0 :(得分:1)

这个怎么样

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

}

答案 1 :(得分:0)

我试着为我发布的想法编写somme代码

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


    int row=FindFirstRow();
    int column=FindFirstColumn();
    if(row!=-1 && column!=-1)
    {
        rowandcolumn++;
        //actions here: row found and column found
        //clear row and column
        clearRow(row);
        clearColumn(column);
    }
    else if(row!=-1)
    {
        //only row is found
        //clear row
        clearRow(row);

    }
    else if(column!=-1)
    {
        //only column is found
        //clear column
        clearColumn(column);
    }
    else
    {
        //nothing is found
    }

    public void clearRow(int row)
    {
        for(int i=0; i<10;i++)
        {
            board[row][i].unset();
        }
    }

    public void clearColumn(int column)
    {
        for(int i=0; i<10;i++)
        {
            board[i][column].unset();
        }
    }

     //this method returns the first matching row index. If nothing is found it returns -1;
    public int FindFirstRow()
    {
        for (int row=0;row<10;row++)
        {       
            int r=0;
             for (int column=0;column<10;column++) 
             {
               if (board[row][column].status() == true)
               {
                     r = r + 1;
                     if(  r == 10) 
                     {
                         //row found
                         return row;
                     }
                } 
            }

            r=0; //reset
        }

        //nothing found
        return -1;
    }


      //this method returns the first matching column index. If nothing is found it returns -1;
    public int FindFirstColumn()
    {  
         for (int column=0;column<10;column++) 
        {
            int c=0;
            for (int row=0;row<10;row++)
            {
               if (board[row][column].status() == true)
               {
                     c = c + 1;
                     if( c == 10 ) 
                     {
                        //matching column found
                        return column;
                     }
                }
            }
            c=0; //reset
        }
        //nothing found
        return -1;
    }