如何显示问候留言。 [英] How can I display the greeting message.

查看:80
本文介绍了如何显示问候留言。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Need help. I'm a beginner at programming. And I really want to learn more. I've created a simple hangman game. But something's not right. The program compiled properly, its just that whenever I or the user guessed all the correct letters, the program still asks the user to guess a letter instead of displaying a message saying, "Congratulations!". How can I make it display?

Here's the code:







#include "stdafx.h"
#include <stdio.h>   
#include <conio.h>
#include <string.h>  
#include <ctype.h>  
#include <stdlib.h> 
#include <time.h>
#include "iFunction.h"

#pragma warning(disable:4996)

#define NUM_OF_WORDS   25 // The number of words in the words list
#define NUM_OF_CHANCES 5  // The number of tries allowed
#define TRUE           1  // Bool for TRUE
#define FALSE          0  // Bool for FALSE


int _tmain(int argc, _TCHAR* argv[])
{
		// Hang Man Figure
		char *hanged[]={
						 "\t\t\t\t|=====|\n"
						 "\t\t\t\t      |\n"
						 "\t4 lives left...\t\t      |\n"
						 "\t\t\t\t      |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t|=====|\n"
						 "\t\t\t\tO     |\n"
						 "\t3 lives left...\t\t      |\n"
						 "\t\t\t\t      |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t|=====|\n"
						 "\t\t\t\tO     |\n"
						 "\t2 lives left...\t\t|     |\n"
						 "\t\t\t\t      |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t |=====|\n"
						 "\t\t\t\t O     |\n"
						 "\t1 life left...\t\t/|\\    |\n"
						 "\t\t\t\t |     |\n"
						 "\t\t\t\t     ===\n",
						 "\t\t\t\t |=====|\n"
						 "\t\t\t\t[X]    |\n"
						 "\tNo lives left...\t/|\\    |\n"
						 "\tYou Got Hanged!\t\t/ \\    |\n"
						 "\t\t\t\t     ===\n"

		};
		// set of words to be guessed by the user(s)
		char *words[] = {
						  "ARGUMENTS", "BYPASSING", "CHROMATIC", "DOWNLOADING","ELECTRICAL","FREEFLOW","GOOGLE","HACKING", "INTRANET", "JUNKFILE", "KEYWORDS", "LIFETIME",
						  "MOBILE", "NULLIFY", "PASSWORD", "QUADCORE", "RECONNECT", "STRUCTURE", "TIMELINES", "UPLOADING", "VOICECOMMAND", "WIRELESS", "XENDER",
						  "YOUTUBE", "ZIPCODES"
		};
		
		srand((unsigned) time(NULL));
		unsigned short chosen_word = rand() % NUM_OF_WORDS; // the one who will choose the random words above
		char guessed;
		size_t len = strlen(words[chosen_word]); //who count each letter of the choosen word to be guessed
		size_t i = 0;
		unsigned int found = FALSE;
		unsigned int chances = 0; //this will serve as counting value of the incorrect letters
		char repeat;

	do
	{
		system("cls");
		chances = 0; //chances reset to 0
		chosen_word = rand() % NUM_OF_WORDS; //responsible for getting another word randomly*
		len = strlen(words[chosen_word]); //counters*
		i = 0;

		ShowHeadingRules();
		ShowWordClue(len);
		char *a = (char *)malloc(len + 1);
		printf("\n Guess a letter: ");
		
		while(1)//1 bec. the loop is TRUE
		{
			guessed = toupper(getchar());

			if(guessed != '\n')
			{
				if (found = len) //correct guessed
				{
					if(strcmp(a, words[chosen_word]) == TRUE)
					{	
						Load();
						system("cls");
						printf("\n Word to be guessed: ");
						SeeInWord(guessed, words[chosen_word], a, len, &found);
						printf("\n\n Guess a letter: ");
					}

				}
			}

			else if(found != TRUE) //incorrect guess
			{
				system("cls");
				if (chances < 4)
				{
					printf("\n Word to be guessed: ");
					SeeInWord(guessed, words[chosen_word], a, len, &found); // to show every letters of the correct answers
				}
				

				
				if(chances >= 4)
				{
					system("cls");

					printf("\n The Exact Word is '%s'", words[chosen_word]); // to show the correct word
					printf("\n\n%s\n\n", hanged[chances++]);  // show the Hanged Figure
					
					if(chances == 5)
					{
						GameOver(); //ending message
						printf("\n\n\n\n\n");
						printf(" ==========================================================================\n");
						printf(" To repeat the program, press [y] or [Y]..\n");
						printf(" If you wish to terminate the program,\n");
						printf(" Just press any key: ");
						repeat = getch();
						break;
					}
				}

				
				// tells the user that he/she inputs an incorrect letters
				printf("\n\n\n\t\t\t********************");		
				printf("\n\t\t\t*       Uy!!!      *");
				printf("\n\t\t\t*Tag-ana ug tarong!*");
				printf("\n\t\t\t********************");
				printf("\n --------------------------------------------------------------------");
				printf("\n\n%s\n", hanged[chances++]);
				printf(" --------------------------------------------------------------------");
				printf("\n Guess a letter: ");
				
			}
			
			
		}
	}while(repeat == 'y' || repeat == 'Y'); //to repeat the program  "NOTED: there's a glitch" ^_^

	getch();
	return 0;
}





//头文件



//Header file

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#pragma warning(disable:4996)

//Prototypes
void ShowHeadingRules();
void GameOver();
void SeeInWord(char, const char [], char *, const size_t, unsigned int *);
void ShowWordClue(const size_t);
void Load();



void ShowHeadingRules() //Shows the game heading and prints out the rules of the game
{
	printf("\n\t\t\t******************************");
	printf("\n\t\t\t*PROGRAMMER: Marvin M. Maasin*");
	printf("\n\t\t\t*INSTRUCTOR: Sammy L. Bastes *");
	printf("\n\t\t\t******************************\n\n");
    printf("\t\t--------------------------------------------\n");
	printf("\t\t| #  #   #   #   #  ###  #   #   #   #   # |\n");
	printf("\t\t| #  #  # #  ##  # #     ## ##  # #  ##  # |\n");
	printf("\t\t| #### ##### # # # #  ## # # # ##### # # # |\n");
	printf("\t\t| #  # #   # #  ## #   # #   # #   # #  ## |\n");
	printf("\t\t| #  # #   # #   #  ###  #   # #   # #   # |\n");
	printf("\t\t--------------------------------------------\n");
	printf("******************************************************************************\n");
    printf("*Hangman is a game where you are expected to guess a word in a certain number*\n");
    printf("*of chances that is before you hang the hangman.                             *\n");
	printf("******************************************************************************\n\n");
    printf("A word has been choosen, begin guessing it. Good luck!!!\n\n");
    printf("You have to guess this word:");
}

void GameOver() //Ending Message
{
	printf("\t[][][][][][][][][][][][][][][][][][][][][]\n");
	printf("\t[]                                      []\n");
    printf("\t[]   You killed a person! Good Bye. :'( []\n");
	printf("\t[]                                      []\n");
	printf("\t[][][][][][][][][][][][][][][][][][][][][]");
}

void ShowWordClue(const size_t len) //Flow of the loop
{
    size_t i; // Loop counter
    for(i = 0; i < len; i++)
	{
        printf(" _ ");
	}
    printf("\n\n");
}



void SeeInWord(char guessed, const char word[], char *a, const size_t len, unsigned int *found) // Used as counter
{
    size_t i;


    for(i = 0; i < len; i++)
    {
        if(guessed == word[i])
        {
            *(a + i) = guessed;
            *found = TRUE;
        }
        else
        {
            // To identify letters in the array

            if(*(a + i) >= 65 && *(a + i) <= 90 ); // Do not store '_' if there is a Capital letter in that location
            else if(*(a + i) >= 97 && *(a + i) <= 122); // Do not store '_' if there is a Small letter in that location
            else *(a + i) = '_'; // Else store an underscore.
        }
    }

    for(i = 0; i < len; i++)// Print the 'a' array where each charachter printed is followed by a white space.
    {
        printf("%c ", *(a + i));
    }
	for(i = 0; i < len; i++)
	{
		if(strcmp(a, word) == 0)
		{
			printf("\nCONGRATULATIONS! ");
		}
		
	}
}

void Load() //Loading Process
{
	for (int p = 0; p < 8; p++)
	{
		switch (p)
		{
		case 1:
			system("cls");
			printf("\n\n\n\n\t\t\t Loading ------ [10%%]");
		case 2:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [20%%]");
		case 3:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [30%%]");
		case 4:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ----   [40%%]");
		case 5:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [50%%]");
		case 6:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [60%%]");
		case 7:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [70%%]");
		case 8:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [80%%]");
		case 9:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ------ [90%%]");
		case 10:
			system("cls");
			printf("\n\n\n\n\n");
			printf("\t\t\t  Loading ---    [100%%]");
		break;
		}
	}
}





我尝试了什么:



我刚尝试用C语言制作一个简单的刽子手游戏。



What I have tried:

I just tried to make a simple hangman game in C language.

推荐答案

if(strcmp(a, words[chosen_word]) == TRUE)



这是不正确;当字符串匹配时, strcmp 返回 0 。但是常量 TRUE 等于 1 ,大豆只会在字符串不匹配时运行以下块。



注意:我没有通过你的程序的每一行,因为它只需要太长时间。您应该在调试器中运行一些测试,以尝试查看您走错路径的位置。


This is not correct; strcmp returns 0 when the strings match. However the constant TRUE is equal to 1, soy you will only run the following block when the strings do not match.

Note: I have not gone through every line of your program as it would just take too long. You should run some tests in the debugger to try and see where you are taking the wrong path.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较do。

当代码没有达到预期效果时,你就接近了一个bug。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于如何显示问候留言。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆