基本思路:1.由8块方块和一个空块组成的拼图效果,八块由数字1~8,0表示空的块,用3×3的数组存储(棋盘2)。
2.移动的方法:键盘按‘j’表示移动当前块,函数Check搜索附近前后左右四个块是否有空块,有空块就把当前块移动过去。
3.由于3×3的数组只能存9个小符号,视觉体验感极差,所以另设了9*9的棋盘,将棋盘2的1~8块按照1比9的比例等量放大,每一个数字代表着心形的1/9(心形是我自己eng画出来的,所以。。赋值语句会非常多)
4.光标的储存还是用棋盘1,方法和前面的几个游戏没什么区别。
5.在ShowGet函数里给棋盘3赋值,画出心形块的形状,为后面的输出页面做准备。
6.最后胜利时会把空块删掉,输出一个完整的心形,因为这样看起来很爽。
eg:随机将1~8还有0放入数组的函数没写,完成功能就行,初始形状可以自己设,也可以写个随机函数随机生成初始图。
#include<iostream>
#include<conio.h>
using namespace std;
int qiPan[3][3] = { 0 };
int qiPanTwo[3][3] = { 2,0,3,1,4,6,7,5,8 };
int qiPanThree[9][9] = { 0 };
int qiPanFour[9][9] = { 0 };
int X = 1; //2.声明并初始化光标的横纵坐标
int Y = 1;
int temp = 0;
void CSH()
{
qiPan[1][1] = 9; //更改棋盘中光标所在元素的值
}
int Check(int x, int y)
{
if (qiPanTwo[x][y] == 0) {
return 0;
}
else {
//用两个循环当前方块周围8格扫一遍
for (int i = -1; i <= 1; i++)
{
int nx = x + i;
if (!(nx == x) && (nx >= 0 && nx <= 3 – 1))
{
if (qiPanTwo[nx][y] == 0)
{
qiPanTwo[nx][y] = qiPanTwo[x][y];
qiPanTwo[x][y] = 0;
return 0;
}
}
}
for (int j = -1; j <= 1; j++)
{
int ny = y + j;
if (!(ny == y) && (ny >= 0 && ny <= 3 – 1))
{
if (qiPanTwo[x][ny] == 0)
{
qiPanTwo[x][ny] = qiPanTwo[x][y];
qiPanTwo[x][y] = 0;
return 0;
}
}
}
}
return 0;
}
void ShowGet()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (qiPan[i][j] == 9)
{
qiPanFour[i * 3 + 1][j * 3 + 1] = 9;
if (temp == 0) {
if (qiPanTwo[i][j] == 1)
{
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 2)
{
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
}
else if (qiPanTwo[i][j] == 3)
{
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 4)
{
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
qiPanThree[i * 3][j * 3] = 1;
qiPanThree[i * 3][j * 3 + 1] = 1;
qiPanThree[i * 3][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 5)
{
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3][j * 3] = 1;
qiPanThree[i * 3][j * 3 + 1] = 1;
qiPanThree[i * 3][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 6)
{
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
for (int k = -1; k <= 1; k++)
{
qiPanThree[i * 3][j * 3 + k + 1] = 1;
}
}
else if (qiPanTwo[i][j] == 7)
{
qiPanThree[i * 3][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 8)
{
for (int k = -1; k <= 1; k++)
{
qiPanThree[i * 3][j * 3 + k + 1] = 1;
}
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
}
else if (qiPanTwo[i][j] == 0)
{
qiPanThree[i * 3 + 1][j * 3] = 2;
qiPanThree[i * 3 + 1][j * 3 + 2] = 2;
qiPanThree[i * 3][j * 3] = 2;
qiPanThree[i * 3][j * 3 + 1] = 2;
qiPanThree[i * 3][j * 3 + 2] = 2;
qiPanThree[i * 3 + 2][j * 3] = 2;
qiPanThree[i * 3 + 2][j * 3 + 1] = 2;
qiPanThree[i * 3 + 2][j * 3 + 2] = 2;
}
}
}
else if (qiPanTwo[i][j] == 1)
{
qiPanThree[i * 3 + 1][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 2)
{
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
}
else if (qiPanTwo[i][j] == 3)
{
qiPanThree[i * 3 + 1][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 4)
{
qiPanThree[i * 3 + 1][j * 3 + 1] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
qiPanThree[i * 3][j * 3] = 1;
qiPanThree[i * 3][j * 3 + 1] = 1;
qiPanThree[i * 3][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 5)
{
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 1][j * 3 + 1] = 1;
qiPanThree[i * 3 + 1][j * 3 + 2] = 1;
qiPanThree[i * 3][j * 3] = 1;
qiPanThree[i * 3][j * 3 + 1] = 1;
qiPanThree[i * 3][j * 3 + 2] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 6)
{
qiPanThree[i * 3 + 1][j * 3 + 1] = 1;
qiPanThree[i * 3 + 1][j * 3] = 1;
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
qiPanThree[i * 3 + 2][j * 3] = 1;
for (int k = -1; k <= 1; k++)
{
qiPanThree[i * 3][j * 3 + k + 1] = 1;
}
}
else if (qiPanTwo[i][j] == 7)
{
qiPanThree[i * 3][j * 3 + 2] = 1;
}
else if (qiPanTwo[i][j] == 8)
{
for (int k = -1; k <= 1; k++)
{
qiPanThree[i * 3][j * 3 + k + 1] = 1;
}
for (int k = -1; k <= 1; k++)
{
qiPanThree[i * 3 + 1][j * 3 + k + 1] = 1;
}
qiPanThree[i * 3 + 2][j * 3 + 1] = 1;
}
else if (qiPanTwo[i][j] == 0)
{
qiPanThree[i * 3 + 1][j * 3] = 2;
qiPanThree[i * 3 + 1][j * 3 + 1] = 2;
qiPanThree[i * 3 + 1][j * 3 + 2] = 2;
qiPanThree[i * 3][j * 3] = 2;
qiPanThree[i * 3][j * 3 + 1] = 2;
qiPanThree[i * 3][j * 3 + 2] = 2;
qiPanThree[i * 3 + 2][j * 3] = 2;
qiPanThree[i * 3 + 2][j * 3 + 1] = 2;
qiPanThree[i * 3 + 2][j * 3 + 2] = 2;
}
}
}
}
void Show()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (qiPanFour[i][j] == 9)
{
cout << “·”;
}
else if (qiPanThree[i][j] == 0)
{
cout << “■”;
}
else if (qiPanThree[i][j] == 1)
{
cout << “□”;
}
else if (qiPanThree[i][j] == 2)
{
cout << “×”;
}
}
cout << endl;
}
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
qiPanThree[i][j] = 0;
}
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
qiPanFour[i][j] = 0;
}
}
void Judge()
{
if (qiPanTwo[0][0] == 1 && qiPanTwo[0][1] == 2 && qiPanTwo[0][2] == 3 && qiPanTwo[1][0] == 4 && qiPanTwo[1][1] == 5 && qiPanTwo[1][2] == 6 &&
qiPanTwo[2][0] == 7 && qiPanTwo[2][1] == 8 && qiPanTwo[2][2] == 0)
{
temp = 1;
}
}
int main()
{
CSH();
ShowGet();
Show();
char xx; //控制台从键盘获得一个字符的函数(方法)
while (xx = _getch())
{
switch (xx) //控制
{
case ‘w’: //上
X = X – 1; //使光标纵坐标-1
qiPan[X][Y] = 9; //将光标写入棋盘
qiPan[X + 1][Y] = 0; //使原本位置的值还原
system(“cls”);
ShowGet();
Show();
break;
case ‘s’: //下
X = X + 1;
qiPan[X][Y] = 9;
qiPan[X – 1][Y] = 0;
system(“cls”);
ShowGet();
Show();
break;
case ‘a’: //左
Y = Y – 1;
qiPan[X][Y] = 9;
qiPan[X][Y + 1] = 0;
system(“cls”);
ShowGet();
Show();
break;
case ‘d’: //右
Y = Y + 1;
qiPan[X][Y] = 9;
qiPan[X][Y – 1] = 0;
system(“cls”);
ShowGet();
Show();
break;
case ‘j’: //选定位置
system(“cls”);
Check(X, Y);
ShowGet();
Judge();
if (temp == 0)
Show();
else
{
system(“cls”);
ShowGet();
for (int i = 7; i < 9; i++)
for (int j =6 ; j < 9; j++)
{
qiPanThree[i][j] = 0;
}
qiPanThree[6][6] = 1;
qiPanThree[6][7] = 0;
qiPanThree[6][8] = 0;
cout << “你赢了!” << endl;
Show();
}
break;
}
}
}
原创文章,作者:wdmbts,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/268187.html