#include<iostream>
#include<string>
#include<ctime>
#include<conio.h>
#include<Windows.h>
#include<vector>
#include<cstdlib>
using namespace std;
#define Ht 20
#define Wh 10
string Map[Ht][Wh];
int Score = 0;
void Print_map()
{
for (int i = 0; i < Ht; i++) {
for (int j = 0; j < Wh; j++) {
cout << Map[i][j];
}
if (i == 10) cout << "\t\tYour Current Score is:";
if (i == 11) cout << "\t\t" << Score;
cout << endl;
}
}
class Coord
{
public:
int m_X;
int m_Y;
Coord(){}
Coord(int x,int y) {
m_X = x;
m_Y = y;
}
bool operator==(Coord C) {
return C.m_X == m_X && C.m_Y == m_Y;
}
};
class Assets
{
public:
int m_X[4];
int m_Y[4];
Assets(){}
Assets(int* s1) {
srand((unsigned int)time(NULL));
for (int i = 0; i < 4; i++) {
m_X[i] = s1[i];
}
m_Y[0] = 5;
}
};
Assets Choice[6];
//废弃不用的决策号
int a0_x[4] = { 0,0,0,0 };
Assets a0(a0_x);
/////////////
// ■ //
// ■ //
// ■■ //
// //
/////////////
int a1_x[4] = { 0,1,2,2 };
Assets a1(a1_x);
/////////////
// ■ //
// ■ //
// ■ //
// ■ //
/////////////
int a2_x[4] = { 0,1,2,3 };
Assets a2(a2_x);
/////////////
// ■ //
// ■■ //
// ■ //
// //
/////////////
int a3_x[4] = { 0,1,1,2 };
Assets a3(a3_x);
/////////////
// ■■ //
// ■■ //
// //
// //
/////////////
int a4_x[4] = { 0,0,1,1 };
Assets a4(a4_x);
/////////////
// ■■ //
// ■■ //
// //
// //
/////////////
int a5_x[4] = { 0,0,1,1 };
Assets a5(a5_x);
//////////////////////////////////////////////////////////////////////////////////////
//输入一个Assets块将其添加到Map中(在打印层面上)
void assign_ast(Assets A)
{
for (int i = 0; i < 4; i++) {
Map[A.m_X[i]][A.m_Y[i]] = "□";
}
}
//输入一个Assets块将其从Map中删除(在打印层面上)
void delete_ast(Assets A)
{
for (int i = 0; i < 4; i++) {
Map[A.m_X[i]][A.m_Y[i]] = " ";
}
}
//////////////////////////////////////////////////////////////////////////////////////
//输入a or d 来左移或者右移
void move_ast(string action ,Assets& temp)
{
int k = 1;
bool exit1 = 0, exit2 = 0;
if (action == "a") k = -k;
for (int i = 0; i < 4; i++) {
if (Map[temp.m_X[i]][temp.m_Y[i] + 1] == "■") {
exit1 = 1;
}
if (Map[temp.m_X[i]][temp.m_Y[i] - 1] == "■") {
exit2 = 1;
}
}
if (k == 1 && exit1) return;
if (k == -1 && exit2) return;
for (int i = 0; i < 4; i++) {
temp.m_Y[i] += k;
}
}
//禁止触碰的位点集(在触碰之后需要更新)
vector<Coord> Pbt_area = { {18,1},{18, 2},{18, 3},{18, 4},{18, 5},{18, 6},{18, 7},{18, 8} };
//检查是否被触碰(若位点被赋值"□",那么函数将返回是)ps:这里只输入当前正在操控的temp
bool check_if(Assets temp)
{
bool exit = 0;
Coord coord_0(temp.m_X[0], temp.m_Y[0]);
Coord coord_1(temp.m_X[1], temp.m_Y[1]);
Coord coord_2(temp.m_X[2], temp.m_Y[2]);
Coord coord_3(temp.m_X[3], temp.m_Y[3]);
Coord ALL[4] = { coord_0 ,coord_1 ,coord_2 ,coord_3 };
for (int i = 0; i < 4; i++) {
for (vector<Coord>::iterator it = Pbt_area.begin(); it < Pbt_area.end();it++) {
exit = exit || (ALL[i] == *it);
}
}
return exit;
}
int main()
{
srand((unsigned int)time(NULL));
// 画面的初始化 && 添加边框
for (int i = 0; i < Ht; i++) {
for (int j = 0; j < Wh; j++) {
Map[i][j] = " ";
}
}
for (int i = 0; i < Ht; i++) {
for (int j = 0; j < Wh; j++) {
if (j == 0 || j == Wh - 1 || i == 19) {
Map[i][j] = "■";
}
}
}
//////////////////////////////////////////////////////////////////////////////////////
//Assets块的设计
a1.m_Y[1] = a1.m_Y[0];
a1.m_Y[2] = a1.m_Y[0];
a1.m_Y[3] = a1.m_Y[0] - 1;
a2.m_Y[1] = a2.m_Y[0];
a2.m_Y[2] = a2.m_Y[0];
a2.m_Y[3] = a2.m_Y[0];
a3.m_Y[1] = a3.m_Y[0];
a3.m_Y[2] = a3.m_Y[0] - 1;
a3.m_Y[3] = a3.m_Y[0];
a4.m_Y[1] = a4.m_Y[0] - 1;
a4.m_Y[2] = a4.m_Y[0] - 1;
a4.m_Y[3] = a4.m_Y[0];
a5.m_Y[1] = a5.m_Y[0] - 1;
a5.m_Y[2] = a5.m_Y[0] - 1;
a5.m_Y[3] = a5.m_Y[0] - 2;
Assets Choice[6] = { a0,a1,a2,a3,a4,a5, };
Assets temp;
int judge;
//游戏进行循环
while (true)
{
//随机创建一个Assets块
judge = rand() % 5 + 1;
assign_ast(Choice[judge]);
temp = Choice[judge];
while (true)
{
Print_map();
//块的运动
if (_kbhit()) {
int key = _getch();
delete_ast(temp);
switch (key)
{
//输入a(代表向左移动)
case 97:
move_ast("a", temp);
break;
//输入d(代表向右移动)
case 100:
move_ast("d", temp);
break;
//输入s(代表加速下降)
case 73:
for (int i = 0; i < 4; i++) {
temp.m_X[i]++;
}
break;
//输入w(代表旋转)
case 119:
{
int x = temp.m_X[1], temp_x;
int y = temp.m_Y[1], temp_y;
if ((judge == 1 ||judge == 3) && Map[x][y + 1] == "■") break;
if (judge == 2 && (Map[x][y + 1] == "■" || Map[x][y - 1] == "■" || Map[x][y - 2] == "■")) break;
if (judge == 4) break;
if (judge == 5 && Map[x][y - 1] == "■") break;
for (int i = 0; i < 4; i++) {
temp_x = temp.m_X[i];
temp_y = temp.m_Y[i];
temp.m_X[i] = -y - x + temp_y;
temp.m_Y[i] = -y + x - temp_x;
}
break;
}
default:
break;
}
assign_ast(temp);
}
//刷新正在操控的Assets块,使其向下
delete_ast(temp);
for (int i = 0; i < 4; i++) {
temp.m_X[i]++;
}
assign_ast(temp);
//程序的出口(若正在操控的Assets块触碰到了违法位点,函数将输出1)
if (check_if(temp)) {
for (int i = 0; i <= 18; i++) {
for (int j = 1; j <= 8; j++) {
if (Map[i][j] == "□") {
Map[i][j] = "■";
}
}
}
//检查是否有行被填满
bool judge;
int k;
for (int i = 18; i >= 0; i--) {
judge = 1;
for (int j = 8; j >= 1; j--) {
judge = judge && (Map[i][j] == "■");
}
if (judge == 1) {
for (int k = i; k > 0; k--) {
for (int j = 1; j <= 8; j++) {
Map[k][j] = Map[k - 1][j];
}
}
Score += 100;
i++;
}
}
//违法位点的刷新
Pbt_area.resize(0);
for (int j = 1; j <= 8; j++) {
int i = 18;
while (i >= 0) {
while (Map[i][j] == "■") {
i--;
}
Coord C(i, j);
Pbt_area.push_back(C);
while (Map[i][j] != "■") {
i--;
if (i == -1) break;
}
}
}
system("cls");
break;
}
Sleep(75);
system("cls");
}
}
}
哪里的条件加了就崩了?