gpt4 book ai didi

java - 10*10 游戏的逻辑检查

转载 作者:行者123 更新时间:2023-11-30 08:10:42 25 4
gpt4 key购买 nike

我正在做一个叫1010的游戏!也许你们有些人听说过它。基本上,我在编写算法以供通关时遇到了一些麻烦。

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

得分是这样的:每次移动都会获得a+10*b分。 a是输入棋子中的方格数pb是清除的行列总数。

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

在Square类中,有公共(public)void方法unset()->“清空方 block ”和boolean status()->“判断是否为方 block 为空"在棋子类中,有int numofSquare -> "返回每个棋子中的方格数,用于分数计算"

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

我的想法是:

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

  2. cr重置为0,循环遍历第二行第二列的方 block ...

  3. 更新分数。

基本上,困难的部分是,如果我将清除列和清除行算法分开,我将首先判断行或列,然后清除它们。但由于每一列至少包含一个属于该行的方格,而每一行也至少包含一个属于该列的方格,所以当行和列都满时就会出现错误。

感谢您的帮助。

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

}

关于java - 10*10 游戏的逻辑检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30422722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com