📄 game.java
字号:
public class Game
{
/*
* chessBoardStates[row][col]:
* 0=初始值
* 1=自己
* -1=对方
*/
///////////////////////////////////////添加
static int oldRow = -1;
static int oldCol = -1;
static int curRow = -1;
static int curCol = -1;
private int chessBoardSize = 15;
private int[][] chessBoardStates = new int[chessBoardSize][chessBoardSize];
public Game()
{
GameControler.getGameInstance(this);
InitGame();
}
public void InitGame()
{
int row,col;
for(row=0; row<chessBoardSize; row++)
{
for(col=0; col<chessBoardSize; col++)
{
chessBoardStates[row][col] = 0;
}
}
/////////////////////////添加
oldRow = -1;
oldCol = -1;
curRow = -1;
curCol = -1;
}
public boolean isNoChess()
{
int row,col;
for(row=0; row<chessBoardSize; row++)
{
for(col=0; col<chessBoardSize; col++)
{
if(chessBoardStates[row][col]==0)
return false;
}
}
return true;
}
/*
* 返回0,未结束
* 返回1,你赢了
* 返回-1,你输了
* 返回2,和棋
*/
public int isGameEnd()
{
int row,col;
int state = chessBoardStates[curRow][curCol];
//行
int counter = 1;
for(col=curCol+1; col<chessBoardSize; col++)
{
if(state != chessBoardStates[curRow][col])
break;
counter ++;
}
for(col=curCol-1; col>=0; col--)
{
if(state != chessBoardStates[curRow][col])
break;
counter ++;
}
if(counter>=5) return state;
//列
counter = 1;
for(row=curRow+1; row<chessBoardSize; row++)
{
if(state != chessBoardStates[row][curCol])
break;
counter ++;
}
for(row=curRow-1; row>=0; row--)
{
if(state != chessBoardStates[row][curCol])
break;
counter ++;
}
if(counter>=5) return state;
//对角线
counter = 1;
for(row=curRow+1,col=curCol+1;
row<chessBoardSize&&col<chessBoardSize; row++,col++)
{
if(state != chessBoardStates[row][col])
break;
counter ++;
}
for(row=curRow-1,col=curCol-1;
row>=0&&col>=0; row--,col--)
{
if(state != chessBoardStates[row][col])
break;
counter ++;
}
if(counter>=5) return state;
//对角线
counter = 1;
for(row=curRow-1,col=curCol+1;
row>=0&&col<chessBoardSize; row--,col++)
{
if(state != chessBoardStates[row][col])
break;
counter ++;
}
for(row=curRow+1,col=curCol-1;
row<chessBoardSize&&col>=0; row++,col--)
{
if(state != chessBoardStates[row][col])
break;
counter ++;
}
if(counter>=5) return state;
//和棋
if(isNoChess())
return 2;
//还没结束
return 0;
}
public void setBoardState(int row, int col, int state)
{
chessBoardStates[row][col] = state;
/////////////////////////添加
oldRow = curRow;
oldCol = curCol;
curRow = row;
curCol = col;
}
public int getBoardState(int row, int col)
{
return chessBoardStates[row][col];
}
public int getBoardSize()
{
return chessBoardSize;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -