Java 10*10游戏的逻辑检查
我正在做一个叫1010的游戏!也许你们中的一些人听说过。基本上,我在编写清除算法时遇到了一些麻烦 规则是,如果任何行或列被占用,则分别清除行和列 得分使得每一步都获得a+10*b分a是输入块中的方块数p,b是清除的行和列的总数 首先,我创建了一个二维数组board[10][10],将board[]]中的每个元素用一个空正方形填充 在Square类中,它有unset()->“清空Square”和boolean status()->“判断Square是否为空”的public void方法。在piece类中,它有int numofSquare->“返回每块中的方块数进行分数计算” 特别是,如果行和列都被占用,我不知道如何编写它,因为它们在二维数组中相互交叉。 在某些情况下,它无法通过测试,其中一些方块没有被清除,但它们应该被清除,我很确定这是逻辑问题。 我的想法是:Java 10*10游戏的逻辑检查,java,arrays,for-loop,logic,Java,Arrays,For Loop,Logic,我正在做一个叫1010的游戏!也许你们中的一些人听说过。基本上,我在编写清除算法时遇到了一些麻烦 规则是,如果任何行或列被占用,则分别清除行和列 得分使得每一步都获得a+10*b分a是输入块中的方块数p,b是清除的行和列的总数 首先,我创建了一个二维数组board[10][10],将board[]]中的每个元素用一个空正方形填充 在Square类中,它有unset()->“清空Square”和boolean status()->“判断Square是否为空”的public void方法。在piec
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;
}
import java.util.ArrayList;
公共类游戏状态{
公共静态最终整数方格=10;
//板在两个方向上的范围
公共静态最终int noobbox=3;
//游戏中盒子的数量
private Square[][]板;//板的当前状态
专用框[]框;//框的当前状态
private int score;//当前分数
//初始化电路板的实例变量
//所有方块和方框最初都是空的
公共游戏状态()
{
getboard();
得分=0;
董事会=新广场[10][10];
对于(inti=0;i我试图为我发布的想法编写一些代码
// 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;
}
//将p放置在板上,其(概念上)左上角在x,y的正方形处
//根据需要清除列和行
int r=0;
int c=0;
int rowandcolumn=0;
int row=FindFirstRow();
int column=FindFirstColumn();
如果(行!=-1和列!=-1)
{
rowandcolumn++;
//此处的操作:找到行和找到列
//清除行和列
净空区(row);
clearColumn(column);
}
else if(行!=-1)
{
//只找到一行
//净行
净空区(row);
}
else if(列!=-1)
{
//只找到一列
//清栏
clearColumn(column);
}
其他的
{
//什么也没找到
}
公共无效清除行(内部行)
{
对于(inti=0;i
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();
}
}
}
无效背景::清算(整数和分数){
int arr_标志[2][10];//0为行,1为列
对于(int i=0;i<2;i++)
{
对于(int j=0;j<10;j++)
{
arr_flag[i][j]=1;
}
}
//纵队
对于(int i=0;i<10;i++)
{
对于(int j=0;j<10;j++)
{
if(arr[i][j].类型==0)
{
arr_标志[0][i]=0;
打破
}
}
}
//划船
对于(int i=0;i<10;i++)
{
对于(int j=0;j<10;j++)
{
if(arr[j][i].类型==0)
{
arr_flag[1][i]=0;
打破
}
}
}
//清栏
对于(int i=0;i<10;i++)
{
如果(arr_标志[0][i]==1)
{
对于(int j=0;j<10;j++)
{
arr[i][j].Clear();
}
}
}
//净行
对于(int i=0;i<10;i++)
{
如果(arr_标志[1][i]==1)
{
对于(int j=0;j<10;j++)
{
arr[j][i].Clear();
}
}
}
}您是否注意到,如果在(i,j)处测试正方形你正在重置行i和列i?@DAO嗨。你的意思是每次列完成10个循环后,行就会重置回0吗?我想列循环重复100次,而行循环重复10次?代码审查站点还有更多内容,IMHO。@bslqy不,这不是问题。我只是想指出你算法中的一些内容:删除行i和列i。这是您想要的吗?可能是行i和列j。@blsqy我也怀疑您是否达到c=10或r=10,因为在两个循环的底部有if{}else{}。事实上,每次操作不满足if子句时,它都会自动将计数器重置为0(r=0和c=0)为了使它简短,R或C永远不会达到1010,我发送了!!!C++中的Github.编码:这里是URL:虽然它有一些bug。