Push box game code of C language

Posted by adavis on Mon, 09 Dec 2019 00:51:30 +0100

 

Preface
The text and pictures of this article are from the Internet, only for learning and communication, not for any commercial purpose. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.
By Yan Yu less

text

Note to novice: if you can't find a solution to the problem in your study, you can Point me into the skirt. Inside the big guy answers and the latest C language teaching. Cheng!

 

It's a little bit easier to learn every day. A simple C language program, used to review C language, the code is easy to understand. If you have any questions, please don't hesitate to comment.

/*******************************************
Write a push box program with the simplest C language statement.
********************************************/
#include <stdio.h>  
#include <conio.h>
#include<stdlib.h> 

int map[9][11] = {
    {0,1,1,1,1,1,1,1,1,1,0},  //0 for open space
    {0,1,0,0,0,1,0,0,0,1,0},  //1 represents the wall
    {0,1,0,4,4,4,4,4,0,1,0},  //3 for destination
    {0,1,0,4,0,4,0,4,0,1,1},  //4 for box
    {0,1,0,0,0,0,0,0,4,0,1},  //5 representatives 
    {1,1,0,1,1,1,1,0,4,0,1},
    {1,0,8,3,3,3,3,1,0,0,1},  //2 3 4 5 6 7 8 9 1 0
    {1,0,3,3,3,3,3,0,0,1,1},
    {1,1,1,1,1,1,1,1,1,1,0} };

//Map / / 2D array + switch()
void DrawMap()
{
    //Traverse 2D array / / 0 print space / / 1 wall / / 3 destination / / what structure?
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 11; j++)
        {
            //if  else  switch
            switch (map[i][j])
            {
            case 0:
                printf("  ");
                break;
            case 1:
                printf("■");
                break;
            case 3:
                printf("☆");
                break;
            case 4:
                printf("□");
                break;
            case 5:
                printf("♀");  //5 person
                break;
            case 7:     //4 + 3 boxes in destination
                printf("★");
                break;
            case 8:     // 5 + 3 people in the destination?
                printf("♀");
                break;
            }
        }
        printf("\n");
    }
}


void PlayGame()
{
    int r, c;  //Human subscript//
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 11; j++)
        {
            if (map[i][j] == 5||map[i][j]==8)   //i j person's subscript?
            {
                r = i;
                c = j;
            }
        }
    }

    char ch;  //Character variable
    ch = getch();  //Keyboard input saved to characters
    // Getch() getchar() receives keyboard characters
    // getch() receives directly without displaying palindromes getchar() displays palindromes and can modify the end of the enter key

    //Change different values according to different keys
    switch (ch)
    {
    case 'W':  //W A S D direction 72 80 75 77 virtual key value ASCII windowvk up VK tab VK return
    case 'w':
    case 72:
        if (map[r - 1][c] == 0|| map[r - 1][c] == 3) 
        {
            map[r - 1][c] += 5;
            map[r][c] -= 5;
        }
        else if (map[r - 1][c] == 4 || map[r - 1][c] == 7)
        {
            if (map[r - 2][c] == 0 || map[r - 2][c] == 3)
            {
                map[r - 2][c] += 4;
                map[r - 1][c] += 1;
                map[r][c] -= 5;
            }
        }



        break;

    case 'S':  //Confirm the function of enter key to return
    case 's':
    case 80:
        if (

Topics: C less ascii