📄 game.cpp
字号:
#include <windows.h>
#include "game.h"
int Random(long maxValue)
{
int temp = rand();
if (maxValue != 0)
return temp /(RAND_MAX/maxValue);
else return 1;
}
CGame::CGame()
{
Reset();
}
CGame::~CGame()
{
}
void CGame::Reset()
{
Height = ROWS-1;
Width = COLUMNS-1;
int n = 0;
for (int i=0; i<=Height; i++)
for (int j=0; j<=Width; j++)
{
Desk[i][j] = n;
n++;
}
Desk[Height][Width] = -1;
moves = 0;
}
int CGame::GetCell(int row, int col)
{
return Desk[row][col];
}
bool CGame::MoveIt(int row, int col)
{
bool can_move = false;
if ((row < 0) | (row > Height) | (col < 0) | (col > Width))
return false;
if (Desk[row][col] == -1)
return false;
if (Desk[row][col-1] == -1)
{
Desk[row][col-1] = Desk[row][col];
Desk[row][col] = -1;
can_move = true;
}
if (Desk[row][col+1] == -1)
{
Desk[row][col+1] = Desk[row][col];
Desk[row][col] = -1;
can_move = true;
}
if (Desk[row-1][col] == -1)
{
Desk[row-1][col] = Desk[row][col];
Desk[row][col] = -1;
can_move = true;
}
if (Desk[row+1][col] == -1)
{
Desk[row+1][col] = Desk[row][col];
Desk[row][col] = -1;
can_move = true;
}
if (can_move)
moves++;
return can_move;
}
void CGame::Mix(int quality)
{
for (int i=0;i<quality;i++)
MoveIt(Random(Height+1),Random(Width+1));
moves = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -