bslqy
bslqy

Reputation: 233

Logic check for a 10*10 game

I am doing a game called 1010! Probably some of you have heard of it. Bascially I encouter some trouble when writing the Algorithm for clearance.

The rule is such that if any row or any column is occupied, then clear row and column respectively.

The scoring is such that each move gains a+10*b points. a is the number of square in the input piece p and b is the total number of row&column cleared.

To start, I create a two dimensional Array board[10][10], poulate each elements in the board[][] with an empty square.

In the class of Square, it has public void method of unset()-> "empty the square" & boolean status() -> "judge if square is empty"In the class of piece, it has int numofSquare -> "return the number of square in each piece for score calculation"

In particular, I don't know how to write it if both row and column are occupied as they are inter-cross each other in an two dimensional array. It fail the test under some condition, in which some of the squares are not cleared but they should have been cleared and I am pretty sure is the logic problem.

My thinking is that:

  1. Loop through squares in first row and first column, record the number of square that are occupied (using c and r); if both are 10, clear row&column, otherwise clear row or column or do nothing.

  2. reset the c &r to 0, loop through square in the second row, second column…

  3. update score.

Basically the hard part is that if I seperate clear column and clear row algorithm ,I will either judge row or column first then clear them . However, as every column contains at least one square belong to the row, and every row contains at least one square belong to the column, there will be mistake when both row and column are full.

Thanks for help.

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

Upvotes: 1

Views: 1070

Answers (2)

Creator
Creator

Reputation: 11

how about this

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

}

Upvotes: 1

user4624062
user4624062

Reputation:

I tried to write somme code for the idea I posted

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

Upvotes: 0

Related Questions