#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
int x = 0, y = 0;
// save map
int map[10][10] = { 0 };
// map 1
int map_1[10][10] = {
{0,0,1,1,1,0,0,0},
{0,0,1,4,1,0,0,0},
{0,0,1,0,1,1,1,1},
{1,1,1,0,0,2,4,1},
{1,4,2,2,0,1,1,1},
{1,1,1,3,2,1,0,0},
{0,0,0,1,4,1,0,0},
{0,0,0,1,1,1,0,0}
};
// map 2
int map_2[10][10] = {
{0,0,0,1,1,1,1,1,1,1},
{0,0,1,1,0,0,1,0,3,1},
{0,0,1,0,0,0,1,0,0,1},
{0,0,1,2,0,2,0,2,0,1},
{0,0,1,0,2,1,1,0,0,1},
{1,1,1,0,2,0,1,0,1,1},
{1,4,4,4,4,4,0,0,1,0},
{1,1,1,1,1,1,1,1,1,0}
};
// win?
int finish();
// move man
void move(int x1, int y1, int x2, int y2);
// print map
int render();
// find man
void find();
// choose map 1 or 2
void setmap(int n);
// help
void help(void);
int main(void){
int n;
char dir;
char c;
help();
printf("Please choose (1 or 2, q to exit): ");
while(scanf("%d", &n)){
getchar();
// n==0, exit
if(n == 0){
printf("game over\n");
break;
}
system("cls");
if(n == 1 || n == 2){
setmap(n); // choose map
render();
while((dir = getch()) != EOF){
system("cls");
find();
switch(dir){
case 'w': move(x - 1, y, x - 2, y); break;
case 's': move(x + 1, y, x + 2, y); break;
case 'a': move(x, y - 1, x, y - 2); break;
case 'd': move(x, y + 1, x, y + 2); break;
case 'r': setmap(n); break;
case 'q': return 0;
}
render();
if(finish()){
printf("You Win!\n");
printf("Please choose (1 or 2, q to exit): ");
break;
}
}
}
else{
printf("Please try again.\n");
printf("Please choose (1 or 2, q to exit): ");
}
}
system("pause");
return 0;
}
void help(void)
{
printf("*************Weclome to the box game*************\n"
"*\t\t\t\t\t\t*\n"
"*\t-------------------------------\t\t*\n"
"*\t\tPowered By X\t\t\t*\n"
"*\t-------------------------------\t\t*\n"
"*\t\t\t\t\t\t*\n"
"*\t\t+ : man\t\t\t\t*\n"
"*\t\t$ : address\t\t\t*\n"
"*\t\t@ : box\t\t\t\t*\n"
"*\t\t\t\t\t\t*\n"
"*\t\thelp\t\t\t\t*\n"
"*\t\tmove : wasd\t\t\t*\n"
"*\t\texit : q\t\t\t*\n"
"*\t\trestart : r\t\t\t*\n"
"*************************************************\n");
}
void move(int x1, int y1, int x2, int y2){
if(map[x][y] == 3) // find man
{
// In front of the person is the box. The Box is on the space
if(map[x1][y1] == 2){
// In front of the box is space
if(map[x2][y2] == 0) {
map[x][y] = 0;
map[x1][y1] = 3;
map[x2][y2] = 2;
}
// In front of the box is the position
else if(map[x2][y2] == 4){
map[x][y] = 0;
map[x1][y1] = 3;
map[x2][y2] = 5;
}
}
// In front of you is the box. The Box is in position
else if(map[x1][y1] == 5){
// In front of the box is space
if(map[x2][y2] == 0) {
map[x][y] = 0;
map[x1][y1] = 6;
map[x2][y2] = 2;
}
// In front of the box is the position
else if(map[x2][y2] == 4){
map[x][y] = 0;
map[x1][y1] = 6;
map[x2][y2] = 5;
}
}
// Blank in front of the person
if(map[x1][y1] == 0){
map[x1][y1] = 3;
map[x][y] = 0;
}
else if(map[x1][y1] == 4){
map[x][y] = 0;
map[x1][y1] = 6;
}
}
else if(map[x][y] == 6) // People in position
{
// Before the position is the box, the box is on the space
if(map[x1][y1] == 2)
{
// Boxes are preceded by Spaces
if(map[x2][y2] == 0){
map[x][y] = 4;
map[x1][y1] = 3;
map[x2][y2] = 2;
}
// In front of the box is the position
else if(map[x2][y2] == 4){
map[x][y] = 4;
map[x1][y1] = 3;
map[x2][y2] = 5;
}
}
// Before the position is the box, the box is in the position
else if(map[x1][y1] == 5){
// Boxes are preceded by Spaces
if(map[x2][y2] == 0){
map[x][y] = 4;
map[x1][y1] = 6;
map[x2][y2] = 2;
}
// In front of the box is the position
else if(map[x2][y2] == 4){
map[x][y] = 4;
map[x1][y1] = 6;
map[x2][y2] = 5;
}
}
// Position is in front of people
if(map[x1][y1] == 4){
map[x][y] = 4;
map[x1][y1] = 6;
}
// People are preceded by Spaces
else if(map[x1][y1] == 0){
map[x][y] = 4;
map[x1][y1] = 3;
}
}
}
void find(void){
for(x = 0; x < 10; x++){
for(y = 0; y < 10; y++){
if(map[x][y] == 3 || map[x][y] == 6){
return;
}
}
}
}
int render(void){
for(x = 0; x < 10; x++){
for(y = 0; y < 10; y++){
if(map[x][y] == 1){
printf("*"); // The output frame
}
else if(map[x][y] == 3){
printf("+"); // Output character location
}
else if(map[x][y] == 2){
printf("@"); // Output box
}
else if(map[x][y] == 4){
printf("$"); // Output destination
}
else if(map[x][y] == 0){
printf(" "); // Output space
}
else if(map[x][y] == 5){
printf("@"); // Output the icon after reaching the destination location
}
else if(map[x][y] == 6){
printf("+"); // People arrvice at the destination after the output character
}
}
printf("\n");
}
return 0;
}
void setmap(int n){
if(n == 1){
memcpy(map, map_1, sizeof(map_1));
}
else if(n == 2){
memcpy(map, map_2, sizeof(map_2));
}
}
int finish(){
for(x = 0; x < 10; x++){
for(y = 0; y < 10; y++){
if(map[x][y] == 2)
return 0;
}
}
return 1;
}
C语言推箱子小游戏
最新推荐文章于 2025-01-10 15:19:42 发布