关于C语言两个小游戏的提示和源码(猜词游戏与控制移动游戏)

关于C语言两个小游戏的提示和源码(猜词游戏与控制移动游戏),第1张

关于C语言两个小游戏的提示和源码(猜词游戏与控制移动游戏)

我完全是一个C语言的菜鸡,从大一下半学期才开始接触的,属于是入门很晚的,最近的课上有两个任务,是写两个小程序,我认为非常有意思,这里做一点分享。

猜词游戏(刽子手游戏)
游戏大家应该都很熟悉,小学英语课堂老师应该带着玩过,规则是你可以输入你想要在多少个单词里进行猜测,界面会给你这些单词是什么,你根据_的个数进行猜测,每一次可以输入一个单词。当然,如果你一开始就猜到了完整的单词,直接输入就算成功。你一共有五条命,输错一次减一条命,绞刑架也会出现你身体的一部分,当你的身体完整出现时,你就输了。

下面是英文版(更详细)
2. Word-guessing game

You are to write a program to implement a simple word guessing game. Given a list of words, 1 word is randomly selected and then the each letter of the word is displayed as underscore characters ‘_’. The user can guess a letter and if that letter is in the word then the appropriate underscores are replaced with that letter. If the letter is not in the word then the player loses 1 life. The player has 5 lives in total and the game is over when lives reach zero.

The aim of the game is to guess the word. If the user thinks they know the word then, instead of entering an individual character, they can enter an entire word. If the word is correct they win the game. If the word is wrong then they lose a life.

When the program starts, it will prompt to read in an integer which is the number of words in the word list. The program then reads in that number of words. You can assume that all the words in the game have a maximum of 31 characters. After they have all been entered, the program randomly selects a word to use for the next game. (In a future exercise, we will read this list of words from a file; for now we just have the user type them in.)

The game then starts. The player is shown the word with the appropriate number of underscores and is prompted to enter either a word or character guess. The game the proceeds as described above until the player wins or dies. After displaying an appropriate message, the program asks the user if they want to play again. If yes, then a new word is randomly selected from the word list and the game starts again. If no, the program exits.

The words typed in the word list should be single words, only containing lowercase letters ‘a’ - ‘z’ and should not contain numbers, punctuation, spaces, etc.

For generating random numbers, you can use the “rand” and “srand” functions to generate random integers. See the book section 5.10, particularly pages 206 and 210, and example program Fig 5.13.
Example I/O:

Number of words: 4
Enter word: football
Enter word: player
Enter word: apples
Enter word: incredible

Word: _ _ _ _ _ _

Guess (5 lives): e

Word: _ _ _ _ e _

Guess (5 lives): t

Word: _ _ _ _ e _

Guess (4 lives): heater

Word: _ _ _ _ e _

Guess (3 lives): a

Word: a _ _ _ _ e _

Guess (3 lives): p

Word: a p p _ e _

Guess (3 lives): apples

Correct!
Do you want to player again (y/n)? n

  1. Hangman

The word guessing game above is based on the British game Hangman. In this game, the process of playing the game is exactly the same but as well as writing out the current state of the word, the computer should draw a man hanging from a gallows. As the player gets guesses wrong, more and more of the body of the man is drawn. When the entire body is draw, the player has lost all their lives and the game is over. An example can be seen on the Wikipedia page.

Extend your solution to the above task to also draw a man, using text characters, in the same way that you drew squares and triangles using text characters in earlier exercises in the textbook (eg, 2.21, 2.25, 2.27, 3.32, 3.33, 3.39, 4.16, 4.31). You can decide how to split the man into 5 pieces and how to draw him.

下面是源码:

#include
#include
#include
#include 
#include 
#include 


int main(){
      
     char WORDS[10][20]={"Apple","Banana","Cucumber","Dumpling","Eggplant","Fish","Garlic","Ham","Icecream","Jellies"}; 
     //这里运用到了二维数组的知识,对题库进行一个存储。
       
      int num;
      printf("Enter the number of words to guess (<=10):");
      scanf("%d",&num);
      int count[num]; 
      int i;
 
      srand(time(NULL));
      //srand函数是一个时间函数,这里相当于设置了一个随机时间的种子,目的是为了后面的随机选词,不会的话可以搜一下这个关键词
           
      for(i=0;i0)
            {
                printf("Guess (%d lives): ",life);
                scanf("%s",guess);
                printf("n");
                                
                if(strlen(guess)!=1)
                //当玩家已经猜出单词的时候
                {
                    if(strlen(guess)==strlen(WORDS[x]))
                    {
                        int judge = 0;
                        for(i=0;i 

控制移动游戏
给你一张地图,外围是墙,一个小玩意可以在墙内任意移动,按8上移,按2下移,按4左移,按6右移(记得开NumLock)小玩意触墙不动并提示无效移动。

下面是题目英文版(更详细)
Rogue-like game movement
We are going to develop the first stages of a turn-based “Rogue-like” game. The most
important part of a game is where the players play - ie, the world. In our game, the
world will consist of a flat area of land that will be represented using text characters.
the map should look like below:
##########
#…@…#
#…#
#…#
#…#
#…#
#…#
##########

Command:

The dimensions of the map are 10 x 8 locations. The character ‘@’ represents the
player, the ‘#’ represents a solid wall which the player cannot move through, and the ‘.’
represents empty space which the player can move through. Below the map there is a
blank line then a prompt for the user to enter a command and press return.
If the user enters ‘8’, the character should move 1 location up.
If the user enters ‘2’, the character should move 1 location down.
If the user enters ‘4’, the character should move 1 location left.
If the user enters ‘6’, the character should move 1 location right.
If the user enters ‘0’, the program should exit.
If the user tries to move the character into a position that is impossible, the character
should stay in the current position, and an error message should appear between the
bottom of the map and the command prompt saying “Invalid move!”.
You can represent the game map any way you wish. One way is to use a 2D char
array to represent the map.

下面是源码

#include 
#include 
#include 

int main()
{
int i,j;
//Draw the map
char arr[8][10]={
{"##########"},
{"#........#"},
{"#........#"},
{"#...@....#"},
{"#........#"},
{"#........#"},
{"#........#"},
{"##########"}
};
//用二维数组存储这个地图
//char arr[3][4]="@" 原点,即玩家的初始位置;

for(i=0;i<8;i++)
{
        for(j=0;j<10;j++)
        {
        printf("%c",arr[i][j]);
        }
printf("n");
}
printf("n");
//输出这张地图


//Command
int P;
int flag=0;
i=3,j=4;
int t,u;
//设置t,u的目的是防止后面i,j改变的时候无法正确输出图像的问题


while(flag==0)
{

printf("Command: ");
scanf("%d",&P);
printf("n");

        //Move
        if(P==8)
        {
                if(i!=1)
                //还没碰到墙的时候
                {
                        {
                        i=i-1;
                        arr[i][j]='@';
                        arr[i+1][j]='.';
                        //这里的单引号非常关键,双引号编译器会认为双引号里的东西是个integer,这里可以记一下
                        }
                
                        for(t=0;t<8;t++)
                        {
                                for(u=0;u<10;u++)
                                {
                                printf("%c",arr[t][u]);
                                }
                        printf("n");
                        }
                        printf("n");
                        }
                        //输出移动过的地图
                 else
                 //触墙
                 {
                 printf("Invalid move!n");
                 printf("n");
                 }
        }
        
        
        if(P==2)
        {
                if(i!=7)
                {
                        {
                        i=i+1;
                        arr[i][j]='@';
                        arr[i-1][j]='.';
                        }
                
                        for(t=0;t<8;t++)
                        {
                                for(u=0;u<10;u++)
                                {
                                printf("%c",arr[t][u]);
                                }
                        printf("n");
                        }
                        printf("n");
                }
                else
                {
                printf("Invalid move!n");
                printf("n");
                }
        }
        
        
        if(P==4)
        {
                if(j!=1)
                {
                        {
                        j=j-1;
                        arr[i][j]='@';
                        arr[i][j+1]='.';
                        }
                
                        for(t=0;t<8;t++)
                        {
                                for(u=0;u<10;u++)
                                {
                                printf("%c",arr[t][u]);
                                }
                        printf("n");
                        }
                        printf("n");
                }
                else
                {
                printf("Invalid move!n");
                printf("n");
                }
        }
        
        
        if(P==6)
        {
                if(j!=9)
                {
                        {
                        j=j+1;
                        arr[i][j]='@';
                        arr[i][j-1]='.';
                        }
                
                        for(t=0;t<8;t++)
                        {
                                for(u=0;u<10;u++)
                                {
                                printf("%c",arr[t][u]);
                                }
                        printf("n");
                        }
                        printf("n");
                }
                else
                {
                printf("Invalid move!n");
                printf("n");
                }
        }
        
       
        if(P==0)
        //退出条件
        {
        flag=1;
        break;
        }
}      
exit(1);
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5433354.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存