kremala

Συζητήσεις για την γλώσσα C και C++

Συντονιστές: WebDev Moderators, Super-Moderators

Απάντηση
xarris81
Δημοσιεύσεις: 1
Εγγραφή: 12 Ιαν 2006 01:55

kremala

Δημοσίευση από xarris81 » 12 Ιαν 2006 01:58

paidia boithiste oso to dinaton grigorotera.Thelo ena aplo kodika gia ti dimimourgia kremalas, an mporeite steilte to sto xarris81@hotmail.com. Paidia SOS kaigomai. Euxaristo ek ton proteron

LightForce
WebDev Moderator
Δημοσιεύσεις: 3812
Εγγραφή: 13 Απρ 2003 23:49

kremala

Δημοσίευση από LightForce » 12 Ιαν 2006 21:08

xarri στο google μπορείς πολύ ευκολα να βρείς κώδικα κρεμάλας για οτι γλώσσα θέλεις...

Άβαταρ μέλους
myle
Δημοσιεύσεις: 205
Εγγραφή: 18 Ιουν 2005 23:59
Τοποθεσία: Πάτρα
Επικοινωνία:

kremala

Δημοσίευση από myle » 17 Φεβ 2006 15:57

Κώδικα για μια κρεμάλα στην C που παίζετε όμως μόνο με δύο παίκτες:

Κώδικας: Επιλογή όλων

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

#define size 127

char *position&#40;char * , char * , char , int &#41;;
int found_letter&#40;char * ,char , int &#41;;
char *insert&#40;char * &#41;;
char insertchar&#40;char &#41;;

int main&#40;void&#41;
&#123;
int i, length_of_word, tries;
char player1&#91;size&#93;, player2&#91;size&#93;, c;
char hidden_word&#91;size&#93;;
char *revealed_word;

system&#40;"cls"&#41;;

printf&#40;"Player 1, please insert your name&#58;\n"&#41;;
scanf&#40;"%s", player1&#41;;
printf&#40;"Player 2, please insert your name&#58;\n"&#41;;
scanf&#40;"%s", player2&#41;;

printf&#40;"Please insert the number of tries to find %s the word&#58;\t", player1&#41;;
scanf&#40;"%d", &tries&#41;;
while&#40;tries < 1 || tries > 26&#41;&#123;
    if&#40;tries > 26&#41;
        printf&#40;"Congratulation %s!! You are the winner!\n", player1&#41;;
    printf&#40;"Please insert again the number of tries\n"&#41;;
    scanf&#40;"%d", &tries&#41;;
&#125;

printf&#40;"%s&#58;\n\tPlease, look away\n", player1&#41;;
printf&#40;"%s&#58;\n\tNumbers are not allowed and they will be removed automatically\n", player2&#41;;
printf&#40;"\tPlease insert one hidden word&#58;\n"&#41;;
scanf&#40;"%s", hidden_word&#41;;
strcpy&#40;hidden_word, insert&#40;hidden_word&#41;&#41;;
length_of_word = strlen&#40;hidden_word&#41;;
while&#40;!length_of_word&#41;&#123;
    printf&#40;"Please insert again your hidden word\n"&#41;;
    scanf&#40;"%s", hidden_word&#41;;
    strcpy&#40;hidden_word, insert&#40;hidden_word&#41;&#41;;
    length_of_word = strlen&#40;hidden_word&#41;;
&#125;
printf&#40;"\nYou inserted the following word&#58; %s\n", hidden_word&#41;;

printf&#40;"Press enter to clear the screen\n\n"&#41;;
i = getc&#40;stdin&#41;;
while&#40;i = getc&#40;stdin&#41;&#41;&#123;
    if&#40;i == '\n'&#41;&#123;
        if&#40;i == '\n'&#41;&#123;
        system&#40;"cls"&#41;;
        break;
        &#125;
    &#125;
&#125;

printf&#40;"Now, %s, you have to guess who is the hidden word\n", player1&#41;;
revealed_word = &#40;char *&#41;malloc&#40;&#40;length_of_word + 1&#41; * sizeof&#40;char&#41;&#41;;
for&#40;i = 0; i < length_of_word; i++&#41;
    revealed_word&#91;i&#93; = '_';
revealed_word&#91;++i&#93; = '\0';

printf&#40;"%s\n", revealed_word&#41;;
tries++;
while&#40;&#40;--tries&#41; && &#40;strncmp&#40;hidden_word, revealed_word, length_of_word&#41;&#41; != 0&#41;&#123;
    printf&#40;"\nRemaining tries&#58; %d\n", tries&#41;;
    c = getc&#40;stdin&#41;;
    while&#40;c == '\n'&#41;
        c = getc&#40;stdin&#41;;
    c = insertchar&#40;c&#41;;
    printf&#40;"%s", position&#40;hidden_word, revealed_word, c, length_of_word&#41;&#41;;
    if&#40;found_letter&#40;hidden_word, c, length_of_word&#41;&#41;
        tries++;
&#125;
if&#40;strncmp&#40;hidden_word, revealed_word, length_of_word&#41; == 0&#41;
    printf&#40;"\n\nThe winner is... %s! Congratulations!!\n", player1&#41;;
else
    printf&#40;"\n\nThe winner is... %s!! Congratulations!!\n", player2&#41;;
printf&#40;"\nPress enter to exit\n"&#41;;

i = getc&#40;stdin&#41;;
while&#40;i = getc&#40;stdin&#41;&#41;&#123;
    if&#40;i == '\n'&#41;&#123;
        if&#40;i == '\n'&#41;&#123;
        system&#40;"cls"&#41;;
        return 0;
        &#125;
    &#125;
&#125;    
&#125;

char *position&#40;char *unknown_word, char *known_word, char c, int length&#41;
&#123;
int i;

for&#40;i = 0; i < length; i++&#41;&#123;
    if&#40;unknown_word&#91;i&#93; == c&#41;
        known_word&#91;i&#93; = c;
&#125;

return known_word;
&#125;

int found_letter&#40;char *unknown_word, char c, int length&#41;
&#123;
int i, found = 0;

for&#40;i = 0; i < length; i++&#41;
    if&#40;unknown_word&#91;i&#93; == c&#41;
        found = 1;

return found;
&#125;

char *insert&#40;char *string&#41;
&#123;
int i, scout_number;
for&#40;i = 0, scout_number = 0; string&#91;i&#93; != '\0'; i++, scout_number++&#41;&#123;
    if&#40;string&#91;i&#93; >= 'a' && string&#91;i&#93; <= 'z'&#41;
        string&#91;i&#93; += 'A' - 'a';
    while&#40;string&#91;scout_number&#93; >= '0' && string&#91;scout_number&#93; <= '9'&#41;
        scout_number++;
    string&#91;i&#93; = string&#91;scout_number&#93;;
    if&#40;string&#91;i&#93; >= 'a' && string&#91;i&#93; <= 'z'&#41;
        string&#91;i&#93; += 'A' - 'a';
&#125;
return string;
&#125;

char insertchar&#40;char character&#41;
&#123;
if&#40;character >= 'a' && character <= 'z'&#41;
        character += 'A' - 'a';
while&#40;character >= '0' && character <= '9'&#41;&#123;
        printf&#40;"Please re-insert a character\n"&#41;;
        character = getc&#40;stdin&#41;;
        while&#40;character == '\n'&#41;
                character = getc&#40;stdin&#41;;
&#125;
return character;
&#125;
Εικόνα

Απάντηση

Επιστροφή στο “C, C++”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες