你好,我写的项目有问题 [英] Hello I have problem with project that I wrote

查看:58
本文介绍了你好,我写的项目有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我做了一个叫做刽子手游戏的小项目,它是基本的程序,比较估算的字符与列表中的一个,问题是当我输入整个单词它不会确认我赢了,但它继续要求更多的输入,你能告诉我如何解决这个问题,它是用eclipse编写的,c程序语言

这里是代码:

Hello,i have made some small project called hangman game,it is basicly program that compare the imputed charaters with the one taken from the list, the problem is when i enter the whole word it wont confirm that i won,but it continue to ask for more imput,can u tell me how to fix this,it is written in eclipse,c program language
here is the code:

/*
 * Razvoni_Alati.c
 *
 *  Created on: Dec 18, 2017
 *      Author: Smiley
 */
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<time.h>

#include <ctype.h>

void vesala(int x)                            /**za pokzivanje vesala**/
 {

     switch(x)
     {

     case 0:

	printf("\n\t||===== ");
	printf("\n\t||    | ");
	printf("\n\t||   %cO/",'\\');
	printf("\n\t||    | ");
	printf("\n\t||   / %c",'\\');
	printf("\n\t||      ");
	break;
     case 1:

	printf("\n\t||===== ");
	printf("\n\t||    | ");
	printf("\n\t||   %cO/",'\\');
	printf("\n\t||    | ");
	printf("\n\t||     %c",'\\');
	printf("\n\t||      ");
	break;
     case 2:

	printf("\n\t||===== ");
	printf("\n\t||    | ");
	printf("\n\t||   %cO/",'\\');
	printf("\n\t||    | ");
	printf("\n\t||      ");
	printf("\n\t||      ");
	break;
     case 3:

	printf("\n\t||===== ");
	printf("\n\t||    | ");
	printf("\n\t||   %cO/",'\\');
	printf("\n\t||      ");
	printf("\n\t||      ");
	printf("\n\t||      ");
	break;
     case 4:

	printf("\n\t||===== ");
	printf("\n\t||    | ");
	printf("\n\t||   %cO ",'\\');
	printf("\n\t||      ");
	printf("\n\t||      ");
	printf("\n\t||      ");
	break;
     case 5:

	printf("\n\t||===== ");
	printf("\n\t||    | ");
	printf("\n\t||    O ");
	printf("\n\t||      ");
	printf("\n\t||      ");
	printf("\n\t||      ");
	break;
      }
      return;
 }


int main()
{
	setvbuf(stdout, NULL, _IONBF, 0);
	srand(time(NULL));
    char Uneta_rec[100];
    char pogodjena_slova[100];
    int brojac_greske = 6 , brojac_pogodjenih = 0;

    int brojac = 0 , pozicija = 0, pobeda, duzina , i;
    char unet_karakter;

    char nasumicna_rec [10][20];
    int ubacene_reci=0;

    FILE *lista_reci=fopen("Lista.txt","r");
    if(lista_reci==NULL){


    	printf("failed to open the file");
    	return 0;
    }
    char input[20];

    while(fgets(input,20,lista_reci)){
    	sscanf(input,"%s",nasumicna_rec[ubacene_reci]);
    	printf("scannd:input:%s nasumicna_rec[%d]:%s\n",input,ubacene_reci,nasumicna_rec[ubacene_reci]);
    	ubacene_reci++;
    }
    printf("Total words read in:%d\n",ubacene_reci);
    fclose(lista_reci);


    int nasumicni_broj= rand() % ubacene_reci;




    duzina = strlen(nasumicna_rec[nasumicni_broj]);

    for ( i = 0; i <9999; i++ )
    {
        printf("\n");/**za pomeranje konzole na dole kako se ne bi videlo resenje(zadata rec)**/

    }

    printf("\n\n Dobrodosli :D \n\n");
    printf("\n\n Imate 5 pokusaja da pogodite zadatu rec");


    printf("\n\n Pritisnite:Enter \n\n ");
    getchar();



        printf("\n\t||===== ");                 /**prikaz vesala**/
	printf("\n\t||    | ");
        printf("\n\t||      ");
        printf("\n\t||      ");
        printf("\n\t||      ");
        printf("\n\t||      ");

    printf("\n\n   Zadata rec ima %d slova \n\n",duzina);  /**za merenje duzine zadate reci,i obavestavanje igracu koliko zadata rec ima slova**/
    for( i = 0; i < duzina ; i++)
    {
        pogodjena_slova[i] = '_';
        pogodjena_slova[duzina] = '\0';
    }

    for(i = 0 ; i < duzina ; i++)
    {
        printf(" ");
        printf("%c",pogodjena_slova[i]);        /**za ispis n praznih crtica gde je n broj slova u zadatoj reci**/

    }
    while(brojac_greske != 0)
    {
        brojac_pogodjenih = 0;
        printf("\n\n   Unesite slovo od a-z!");
        printf("\n\n\t Unesite ovde --> ");

	    scanf("%c",&unet_karakter);

	    if(toupper(unet_karakter) < 'A' || toupper(unet_karakter) > 'Z') /**ako igrac unese karakter koji ne ulazi u opseg od a-z,program trazi da se unese ponovo**/
	        {

	           printf("\n\n\t Netacan karakter,pokusajte ponovo. ");
	            brojac_pogodjenih = 2;
	        }

    if (brojac_pogodjenih != 2)
    {
        for(brojac=0;brojac<duzina;brojac++)    /**za proveru da li uneto slovo postoji u nizu zadate reci**/
	    {
		    if(tolower(unet_karakter)==(nasumicna_rec[nasumicni_broj])[brojac])
		     {
		       brojac_pogodjenih = 1;
                     }
            }

	   if(brojac_pogodjenih == 0)                      /**u slucaju pogresno pogodjenog slova**/
	    {
     	      printf("\n\n Imate jos %d pokusaja :( \n\n ",--brojac_greske);

              vesala(brojac_greske);

	    }

	   else
	   {
	     for(brojac = 0; brojac < duzina; brojac++)
             {
     	         brojac_pogodjenih = 0;
                 if(tolower(unet_karakter) == (nasumicna_rec[nasumicni_broj])[brojac])
	          {
     		     pozicija = brojac ;
     		     brojac_pogodjenih = 1;
	          }
    	      if(brojac_pogodjenih == 1)
	          {
                 for(i = 0 ; i < duzina ; i++)
                 {
                      if( i == pozicija)
                  	  {
                          pogodjena_slova[i] = tolower(unet_karakter); /**za stavljanje pogodjenog slova na pravo mesto**/
                      }
                      else if( pogodjena_slova[i] >= 'a' && pogodjena_slova[i] <= 'z' ) /** ako pogodjeno slovo vec postoji na tom mesto ono se preskoci **/
                      {
                          continue;
                  	  }

                      else
                      {
                          pogodjena_slova[i] = '_';            /** za stavaljenje donje crte na mestu ne pogodjenih slova **/
                      }
                }
                Uneta_rec[pozicija] = tolower(unet_karakter);      /**za uporedjivanje unesenog slova i slova u zadatoj reci**/

                pobeda = strcmp(Uneta_rec,nasumicna_rec[nasumicni_broj]);      /**za uproredjivanje stringova**/

                if(pobeda == 0)                             /**u slucaju da igrac pogodi svako slovo zadate reci**/
                {
                    printf("\n\n\n  POBEDA!!!");
                    printf("\n\n\t zadata rec je bila : %s ",nasumicna_rec[nasumicni_broj]);
                    printf("\n\n Preziveli ste!!\n\n");
                    getchar();
                    return 0;
                }
	       }
	    }
      }
     }

    printf("\n\n\t");
    for(i = 0 ; i < duzina ; i++)
      {
          printf(" ");
          printf("%c",pogodjena_slova[i]);                /**pokazuje prazna mesta zajedno sa tacno unetim slovima**/
      }

    getchar();
    }

      if(brojac_greske <= 0)                                 /**u slucaju da igrac ne pogodi rec**/
      {
          printf("\n\n\t Zadata rec je bila: %s ",nasumicna_rec[nasumicni_broj]);
          printf("\n\n\n Stavljeni ste na vesala :/ ");


      }
getchar();
return 0;
}





我的尝试:



帮助请:DD,需要我的等级



What I have tried:

Help pls :DD,need it for my grade

推荐答案

这是你的功课,让它正常工作是任务的一部分 - 所以我们不能只告诉你问题是什么以及如何解决它! (说实话,我不知道它在做什么,因为没有变量名或评论对我有意义 - 我不会说南斯拉夫语!)



所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。你如何使用它取决于你的编译器系统,但是一个快速的谷歌用于你的IDE名称和调试器应该给你你需要的信息。



放一个断点在函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
This is your homework, and getting it working properly is part of the task - so we can't just tell you what the problem is and how to fix it! (And to be honest, I have no idea what it's doing as none of the variable names or comments make sense to me - I don't speak Yugoslavian!)

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


我对程序逻辑无能为力因为我无法阅读所有评论,变量和字符串所用的语言。



我可以给你一些关于绘图的建议。首先,这里有一些绘制对象的代码:

I can't help with the program logic because I can't read the language all of the comments, variables, and strings are in.

I can give you a little advice about the drawing though. First, here is some code to draw the object :
// with zero wrong guesses - just started
printf("\t||=====\n" );
printf("\t||    |\n" );
printf("\t||     \n");
printf("\t||     \n");
printf("\t||     \n");
printf("\t||     \n");
printf("\t||     \n");

// with all wrong guesses - game lost
printf("\t||=====\n" );
printf("\t||    |\n");
printf("\t||    O\n");
printf("\t||    |\n");
printf("\t||   /|\\\n");
printf("\t||    |\n");
printf("\t||   / \\\n");

需要注意两点:我总是将换行符放在末尾,所以每一行都从零位开始。如果要从缩进位置开始,可以添加间距或其他任何内容。此外,反斜杠充当转义字符。要显示反斜杠,则必须转义,因此请使用双反斜杠。您可以通过右臂和右腿看到这一点。



使用最少的重复代码绘制它的最简单方法是使用逻辑并将其基于多少错误的猜测已经完成。您可以根据错误猜测的数量来决定如何绘制图形的每一行。以下是一些代码:

Two points to note : I always put the newline character at the end so each new line starts at position zero. You can add spacing or whatever if you want to start at an indented position. Also, the backslash acts as an escape character. To display a backslash then it has to be 'escaped' so use a double backslash. You can see this with the right arm and right leg.

The easiest way to draw this, with minimal repeated code, is to use logic and base it on how many wrong guesses have been made. You can decide how to draw each line of the figure based on how many wrong guesses there were. Here is some code to do that :

void DrawObject( int wrongGuesses )
{
    char emptyLine[] = "\t||\n";
    // these two lines are always drawn
    printf( "\t||=====\n" );
    printf( "\t||    |\n" );
    // third line - just the head
    if( wrongGuesses > 0 )
        printf( "\t||    O\n" ); 
    else
        printf( emptyLine );
    // fourth line - adds the upper torso
    if( wrongGuesses > 1 )
        printf( "\t||    |\n" ); 
    else
        printf( emptyLine );
    // fifth line - adds the left arm
    if( wrongGuesses > 2 )
        printf( "\t||   /|\n" ); 
    else
        printf( emptyLine );
    // sixth line - adds the right arm
    if( wrongGuesses > 3 )
        printf( "\t||   /|\\\n" ); 
    else
        printf( emptyLine );
    // seventh line - adds the abdomen
    if( wrongGuesses > 4 )
        printf( "\t||    |\n" ); 
    else
        printf( emptyLine );
}

对于6和7的错误猜测(> 5和> 6),您可以复制代码以绘制部分。



如果您更喜欢三元运算符,您可以编写如下逻辑:

For wrong guesses of 6 and 7 (>5 and >6) you can copy the code to draw the arms section.

If you prefer the ternary operator you can write the logic like this :

void DrawObject( int wrongGuesses )
{
    char emptyLine[] = "\t||\n";
    // these two lines are always drawn
    printf( "\t||=====\n" );
    printf( "\t||    |\n" );
    // third line - just the head
    printf( wrongGuesses > 0 ? "\t||    O\n" : emptyLine );
    // fourth line - adds the upper torso
    printf( wrongGuesses > 1 ? "\t||    |\n" : emptyLine ); 
    // fifth line - adds the left arm
    printf( wrongGuesses > 2 ? "\t||   /|\n" : emptyLine );
    // sixth line - adds the right arm
    printf( wrongGuesses > 3 ? "\t||   /|\\\n" : emptyLine );
    // seventh line - adds the abdomen
    printf( wrongGuesses > 4 ? "\t||    |\n" : emptyLine );
    // eighth line - adds the left leg
    printf( wrongGuesses > 5 ? "\t||   /\n" : emptyLine );
    // ninth line - adds the right leg
    printf( wrongGuesses > 6 ? "\t||   / \\\n" : emptyLine );


这篇关于你好,我写的项目有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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