我今天分享一个小项目 用C\C++写一个老版的九缺一的拼图游戏
代码如下
/****************************************
项 目: 拼图游戏
作 者: 零起点
编译坏境: VS2015
时 间: 2017年2月9日
版 本: 1.0
****************************************/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#define N 3
#pragma comment(lib,"winmm.lib")
int main();
void output(void);
bool judge(void);
void direction(void);
int picture[N][N] = { { 0,6,5 },
{ 7,3,8 },
{ 9,4,2 } };
int sort[N][N]={ {0,2,3},
{4,5,6},
{7,8,9} };
int main()
{
char ch;
printf("\n\n\n\t\t\t┏━━━━━━━━━━┓\n");
printf("\t\t\t┃ ┃\n");
printf("\t\t\t┃ 1.开始游戏 ┃\n");
printf("\t\t\t┃ 2.游戏说明 ┃\n");
printf("\t\t\t┃ 3.退出游戏 ┃\n");
printf("\t\t\t┃ ┃\n");
printf("\t\t\t┗━━━━━━━━━━┛\n");
printf("请输入:");
scanf("%c", &ch);
if (ch == '1')
{
output();
direction();
}
else if (ch == '2')
{
char ch11;
system("cls");
printf("操作说明:1.w为上\n");
printf(" 2.a为左\n");
printf(" 3.s为下\n");
printf(" 4.d为右\n");
printf("是否进入游戏Y/N:");
//fflush(stdin);
getchar();
scanf("%c", &ch11);
if (ch11 == 'y' || ch11 == 'Y')
{
output();
direction();
}
else
exit(-1);
}
else
exit(-1);
//scanf("%c", ch);
//output();
/*if (judge())
MessageBox(NULL, L"你赢了", L"提示", MB_OK);*/
system("pause");
return 0;
}
void output(void)
{
IMAGE jpg[9];//保存图片
initgraph(300, 303);//控制 窗口的大小
loadimage(&jpg[0], L"./images/timg_01.jpg");
loadimage(&jpg[1], L"./images/timg_02.jpg");
loadimage(&jpg[2], L"./images/timg_03.jpg");
loadimage(&jpg[3], L"./images/timg_04.jpg");
loadimage(&jpg[4], L"./images/timg_05.jpg");
loadimage(&jpg[5], L"./images/timg_06.jpg");
loadimage(&jpg[6], L"./images/timg_07.jpg");
loadimage(&jpg[7], L"./images/timg_08.jpg");
loadimage(&jpg[8], L"./images/timg_09.jpg");
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
//输出图片
switch (picture[i][j])
{
case 1:putimage(100*j, 101*i, &jpg[0]); break;
case 2:putimage(100*j, 101 * i, &jpg[1]); break;
case 3:putimage(100*j, 101 * i, &jpg[2]); break;
case 4:putimage(100 * j, 101 * i, &jpg[3]); break;
case 5:putimage(100 * j, 101 * i, &jpg[4]); break;
case 6 :putimage(100 * j, 101 * i, &jpg[5]);break;
case 7:putimage(100 * j, 101 * i, &jpg[6]); break;
case 8:putimage(100 * j, 101 * i, &jpg[7]); break;
case 9:putimage(100 * j, 101 * i, &jpg[8]); break;
}
}
bool judge(void)
{
int sum = 0;
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
if (picture[i][j] == sort[i][j])
++sum;//统计与原图的一样的照片
}
if (sum == N*N)
return true;//如果与原图个数一样那么返回真
else
return false;//如果与原图个数不一样那么返回假
}
void direction(void)
{
int t;
int x_poy =0;
int y_poy = 0;
while (1)
{
char ch = getch();
switch (ch)
{
case 's':
case 'S':
if (y_poy - 1 != -1)
{
/*将两数交互换*/
t = picture[y_poy - 1][x_poy];
picture[y_poy - 1][x_poy] = picture[y_poy][x_poy];
picture[y_poy][x_poy] = t;
y_poy-=1;
output();//更新窗口
}
if (judge())//判断是否一样
{
MessageBox(NULL, L"你赢了", L"提示", MB_OK);//如果与原图相符就提示信息并退出程序 以下同理
exit(-1);
}
break;
case 'D':
case 'd':
if (x_poy - 1 != -1)
{
t = picture[y_poy][x_poy-1];
picture[y_poy][x_poy-1] = picture[y_poy][x_poy];
picture[y_poy][x_poy] = t;
x_poy -= 1;
output();
}
if (judge())
{
MessageBox(NULL, L"你赢了", L"提示", MB_OK);
exit(-1);
}
break;
case 'A':
case 'a':
if (x_poy + 1 != 3)
{
t = picture[y_poy][x_poy + 1];
picture[y_poy][x_poy + 1] = picture[y_poy][x_poy];
picture[y_poy][x_poy] = t;
x_poy += 1;
output();
}
if (judge())
{
MessageBox(NULL, L"你赢了", L"提示", MB_OK);
exit(-1);
}
break;
case 'w':
case 'W':
if (y_poy + 1 != 3)
{
t = picture[y_poy + 1][x_poy];
picture[y_poy + 1][x_poy] = picture[y_poy][x_poy];
picture[y_poy][x_poy] = t;
y_poy += 1;
output();
}
if (judge())
{
MessageBox(NULL, L"你赢了", L"提示", MB_OK);
exit(-1);
}
break;
default:
break;
}
}
}