首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >10*10游戏的逻辑检查

10*10游戏的逻辑检查
EN

Stack Overflow用户
提问于 2015-05-24 10:56:44
回答 2查看 1.1K关注 0票数 1

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

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

得分是这样的,每一次移动都能获得a+10*b积分。a是输入段中的平方数,pb是清除的行和列的总数。

首先,我创建了一个二维的数组board10,用一个空的正方形对板中的每个元素进行填充。

在Square类中,它采用了unset()->“空方”的公共无效方法&在片类中使用布尔状态() ->“判断方格是否为空”,它有int numofSquare ->“返回每块中的方块数以供得分计算”。

特别是,如果行和列在二维数组中相互交叉,那么我不知道如何编写它。在某些条件下不能通过测试,在这种情况下,一些方块没有被清除,但是它们应该被清除,我很确定这就是逻辑问题。

我的想法是:

  1. 循环遍历第一行和第一列中的平方,记录占用的平方数(使用c和r);如果两者都是10,则清除行和列,否则清除行或列,否则什么也不做。
  2. 将c &r重置为0,循环通过第二行,第二列…中的平方
  3. 更新分数。

基本上,困难的部分是,如果我分离清楚列和清楚行算法,我要么判断行或列,然后清除他们。但是,由于每个列至少包含一个属于该行的正方形,而每一行至少包含一个属于该列的正方形,因此当行和列都已满时,将出现错误。

谢谢你帮忙。

代码语言:javascript
运行
复制
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;
}
EN

回答 2

Stack Overflow用户

发布于 2015-06-11 11:30:29

这个怎么样?

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2015-05-24 15:59:38

我试着为我发布的想法写一些代码

代码语言:javascript
运行
复制
    // 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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30422722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档