# include <iostream>
# include <windows.h>
# include <conio.h>
# define ABS(x) (((x)>0) ? (x) : (0-(x)))
using namespace std;
class Console
{
public:
Console(void)
{
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor;
cursor.dwSize = 10;
cursor.bVisible = false;
SetConsoleCursorInfo(hOut, &cursor);
COORD bSize = {80, 25};
SetConsoleScreenBufferSize(hOut, bSize);
SMALL_RECT wSize = {0,0, 80-1, 25-1};
SetConsoleWindowInfo(hOut, true, &wSize);
SetConsoleTitle("数字拼图游戏");
}
protected:
HANDLE hOut;
void SetColor(int code)
{
switch (code)
{
case 0:
{
SetConsoleTextAttribute(hOut, 0x07);
break;
}
case 1:
{
SetConsoleTextAttribute(hOut, 0x70);
break;
}
case 2:
{
SetConsoleTextAttribute(hOut, 0x80);
break;
}
}
}
void GotoXy(int x, int y)
{
COORD pos = {x, y};
SetConsoleCursorPosition(hOut, pos);
}
void GotoPos(int x, int y)
{
Console::GotoXy(y*6+2, x*2+1);
}
};
class Game : private Console
{
public:
Game(void)
{
char *tableline[] = {
{"┏━━┳━━┳━━┓"},
{"┃ ┃ ┃ ┃"},
{"┣━━╋━━╋━━┫"},
{"┗━━┻━━┻━━┛"}
};
Console::SetColor(0);
for (int count = 0; count < 7; count++)
{
switch (count)
{
case 0:
{
cout<<tableline[0]<<endl;
break;
}
case 6:
{
cout<<tableline[3]<<endl;
break;
}
default:
{
if (count % 2) cout<<tableline[1]<<endl;
else cout<<tableline[2]<<endl;
}
}
}
}
void New()
{
int count_1, count_2;
int temp = 8;
for (count_1 = 0; count_1 < 3; count_1++)
{
for (count_2 = 0; count_2 < 3; count_2++)
{
if (count_1 == 1 && count_2 == 1)
{
sz[count_1][count_2] = 0;
continue;
}
else sz[count_1][count_2] = temp--;
}
}
}
bool Draw()
{
int count_1, count_2;
int count = 1;
bool pd = true;
for (count_1 = 0; count_1 < 3; count_1++)
{
for (count_2 = 0; count_2 < 3; count_2++)
{
if ((sz[count_1][count_2] == count++ || sz[count_1][count_2] == 0) && pd)
{
if (sz[count_1][count_2] == 0) count--;
Game::DrawPos(count_1, count_2, 2);
}
else
{
Game::DrawPos(count_1, count_2, 1);
pd = false;
}
}
}
if (pd == true) return true;
else return false;
Console::SetColor(0);
}
void swap(int number, int null = 0)
{
int nx, ny, ux, uy;
int a = find(number), b = find(null);
nx = a/10-1; ny = a%10-1;
ux = b/10-1; uy = b%10-1;
if (nx == ux && ny != uy || ny == uy && nx != ux)
{
if (ABS(nx - ux) == 1 || ABS(ny - uy) == 1)
{
sz[nx][ny] ^= sz[ux][uy];
sz[ux][uy] ^= sz[nx][ny];
sz[nx][ny] ^= sz[ux][uy];
Game::Draw();
}
}
}
int zh(int code)
{
switch (code)
{
case 7: return sz[0][0];
case 8: return sz[0][1];
case 9: return sz[0][2];
case 4: return sz[1][0];
case 5: return sz[1][1];
case 6: return sz[1][2];
case 1: return sz[2][0];
case 2: return sz[2][1];
case 3: return sz[2][2];
}
return -1;
}
private:
int sz[3][3];
void DrawPos(int x, int y, int code)
{
switch (sz[x][y])
{
case 0:
{
Console::GotoPos(x, y);
Console::SetColor(0);
cout<<" ";
break;
}
default:
{
Console::SetColor(code);
Console::GotoPos(x, y);
cout<<" "<<sz[x][y]<<" ";
break;
}
}
}
int find(int number)
{
int count_1, count_2;
for (count_1 = 1; count_1 < 4; count_1++)
{
for (count_2 = 1; count_2 < 4; count_2++)
{
if (sz[count_1-1][count_2-1] == number) return count_1*10 + count_2;
}
}
return -1;
}
};
int main(void)
{
Game game;
int number;
while (1)
{
game.New();
while (!game.Draw())
{
number = getch()-48;
if (number > 0 && number <= 9)
{
game.swap(game.zh(number));
}
}
getch();
}
return 0;
}
数字拼图游戏
最新推荐文章于 2023-08-11 09:31:11 发布