#include<iostream>
#include<string>
#include<vector>
#include<ctime>
using namespace std;
int score = 0, score1;
class wuzilianzhu
{
public:
wuzilianzhu(int row1, int col1);
void show();
bool ifover();//判断是否结束
void move(int x1, int y1, int x2, int y2);//移动棋子
void adjustment();//棋子再生成
void remove();//消除同时显示分数
private:
int row;
int col;
int nums[10][10] = { 0 };
};
wuzilianzhu::wuzilianzhu(int row1, int col1)
{
row = row1;
col = col1;
int tmp[10][10];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
tmp[i][j] = 0;
}
}
int count1 = 0;
srand((unsigned)time(NULL));
while (1)
{
int i = rand() % 9;
int j = rand() % 9;
if (1)
{
tmp[i][j] = rand() % (7 - 1 + 1) + 1;
count1++;
}
if (count1 == 7)break;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
nums[i][j] = tmp[i][j];
}
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << nums[i][j] << " ";
}
cout << endl;
cout << endl;
}
}
void wuzilianzhu::move(int x1, int y1, int x2, int y2)
{
if (nums[x2][y2] == 0&& nums[x1][y1]!= 0)
{
int a;
a = nums[x1][y1];
nums[x1][y1] = nums[x2][y2];
nums[x2][y2] = a;
}
else if (nums[x1][y1]== 0)
{
cout << "未找到棋子";
}
else if (nums[x2][y2] != 0)
{
cout << "目标不为空";
}
}
void wuzilianzhu::remove()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int a1 = i;
int b1 = j;
int count = 0;
for (int a = i, b = j; nums[a][b] == nums[a + 1][b] && nums[a][b] != 0; a++)
{
count++;
}
if (count >= 4)
{
score = score + ((count - 4) * 2 + 10);
for (a1, b1; a1 <= i + count; a1++)
{
nums[a1][b1] = 0;
}
}//消除纵向
count = 0;
for (int a = i, b = j; nums[a][b] == nums[a][b + 1] && nums[a][b] != 0; b++)
{
count++;
}
if (count >= 4)
{
score = score + ((count - 4) * 2 + 10);
for (a1, b1; b1 <= j + count; b1++)
{
nums[a1][b1] = 0;
}
}//消除横向
count = 0;
for (int a = i, b = j; nums[a][b] == nums[a + 1][b + 1] && nums[a][b] != 0; a++, b++)
{
count++;
}
if (count >= 4)
{
score = score + ((count - 4) * 2 + 10);
for (a1, b1; a1 <= i + count && b1 <= j + count; a1++, b1++)
{
nums[a1][b1] = 0;
}
}//消除斜向右下
count = 0;
if (i > 4 && j > 4)
{
for (int a = i, b = j; nums[a][b] == nums[a - 1][b - 1] && nums[a][b] != 0; a--, b--)
{
count++;
}
if (count >= 4)
{
score = score + ((count - 4) * 2 + 10);
for (a1, b1; a1 >= i - count; a1--, b1--)
{
nums[a1][b1] = 0;
}
}//消除斜向左下
count = 0;
}
}
}
}
bool wuzilianzhu::ifover()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (nums[i][j] == 0)
return false;
}
}
return true;
}
void wuzilianzhu::adjustment()
{
int count = 0;
srand((unsigned)time(NULL));
do
{
int i = rand() % 9, j = rand() % 9;
if (nums[i][j] == 0)
{
nums[i][j] = rand() % (7 - 1 + 1) + 1;
count++;
}
} while (count < 3);
show();
}
void wuzilianzhu::show()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << nums[i][j] << " ";
}
cout << endl;
cout << endl;
}
}
int main()
{
wuzilianzhu a(9, 9);
cout << "Your score is 0" << endl;
do
{
score1 = score;
int x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
a.move(x1, y1, x2, y2);
a.remove();
if (score > score1)
{
system("cls");
a.show();
cout << "Your score is:" << score << endl;
}
else
{
system("pause");
system("cls");
a.adjustment();
cout << "Your score is:" << score << endl;
}
} while (a.ifover() == false);
return 0;
}