Jump to content
Sign in to follow this  
itairus

Tang Mana Silap Nye?

Recommended Posts

Aku ader prob tang coding... leh tlg tengokkan tak?

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

void initialize(void);

int readGuess(void);

void playGame(int target);

#define HIGH 1000

#define FALSE 0

#define TRUE 1

main()

{

int target;

initialize();

target = random();

playGame(target);

}

void initialize(void)

{

SeedRand();

printf("Hye there. This is a GUESSING NUMBER GAME!\n");

printf("Feel free to play?? Think any number between 1 and %d.\n", HIGH-1);

printf("Try to guess any number then press enter.\n");

printf("I will tell you whether your guess is too high or too lower\n");

}

void playGame(int target)

{

int readGuess(void);

int guess, i;

int GuessAgain;

GuessAgain= TRUE;

do{

guess = readGuess();

for (i=0; i<=10; i++)

if (guess < target)

printf("Your guess is too low!\n\n");

else if (guess > target)

printf ("Your guess is too high!\n\n");

else

printf("Congratulations! You guessed is correct!\n");

GuessAgain= FALSE;

}

while (GuessAgain);

int readGuess( );

{

int guess;

printf("Your guess: ");

scanf("%d", &guess);

return guess;

}

Sln dia camni:

write a program that selects a random integer between 1 and 1000. The user is asked to guess this number. If this guess is correct, the user is told that they have chosen the correct number and the game ends. Otherwise, they are told if their guess was too high or too low. The user has 10 goes to guess corectly before the game ends and they lose.

PS: Aku ngaku nih lab test aku... aku just x puas hati coz aku x dpt run coding nih. then aku terpaksa antar coding nih gak sbb dah time's up. Member aku kata ader jln lebih simple, guna for, if-else... tp aku mmg x brpa lam loop2.

So harap leh tlg. TQ...cheers!

Share this post


Link to post
Share on other sites

 // lala.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>

#define HIGH 1000

void initialize();
int readGuess();
void playGame();

int target;
int guess;

int _tmain(int argc, _TCHAR* argv[])
{

    initialize();
    readGuess();

    getch();
    return 0;
}

void initialize()
{

printf("Hye there. This is a GUESSING NUMBER GAME!\n");
printf("Feel free to play?? Think any number between 1 and %d.\n", HIGH-1);
printf("Try to guess any number then press enter.\n");
printf("I will tell you whether your guess is too high or too lower\n");

}

//function play game
void playGame()
{

    if(guess<HIGH)
    {
        printf("too low");
    }
     else if  (guess==HIGH)
    {
        printf("nombor equal");
    }
    else 
    {
        printf("too high");
    }
    printf("\n");

    readGuess();
}

//function get input
int readGuess()
{

    printf("Your guess: ");
    scanf("  %d", &guess);

    playGame();
    return true;

}

ni aku da wat sikit..sekali tengok macam da siap..tapi lom siap lagik..aku nak ko wat bahagian computer pilih nombor dalam loop tu dan exit program..pandai2 ko la sesuaikan ngan compiler ko tu

Share this post


Link to post
Share on other sites

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

void initialize(void);

int readGuess(void);

void playGame(int target);

void die();

int high = 1000;

#define FALSE 0;

#define TRUE 1;

main()

{

int target;

initialize();

target = rand() % high; //rand() bukan random()

playGame(target);

}

void initialize(void)

{

/* SeedRand(); // ni function aper */

printf("Hye there. This is a GUESSING NUMBER GAME!\n");

printf("Feel free to play?? Think any number between 1 and %d.\n", high-1 );

printf("Try to guess any number then press enter.\n");

printf("I will tell you whether your guess is too high or too lower\n");

}

void playGame(int target)

{

int readGuess(void);

int guess, i;

int GuessAgain;

i = 10;

GuessAgain= TRUE;

do{

if ( i == 0 ) {

printf("Die!!\n");

die();

}

printf("%d times more left to guess!\n", i);

guess = readGuess();

if (guess < target){

printf("Your guess is too low!\n\n");

--i;

}

if (guess > target){

printf ("Your guess is too high!\n\n");

--i;

}

}

while (guess != target);

printf("Right!!\n");

}

int readGuess()

{

int guess;

printf("Your guess: ");

scanf("%d", &guess);

return guess;

}

void die()

{

int target;

initialize();

target = rand() % high;

playGame(target);

}

Code yang awak buat berterabur, sepatutunya game ni boleh dibuat dengan cara yang amat senang.

KISS

Share this post


Link to post
Share on other sites

terima kasih byk2 korang!!!

tau ader jln singkat, tapi tak reti nak guna loop...

need to work hard on C prog lepas nih. Doakan yea.

Terima Kasih!!

NAK WISH SELAMAT HARI RAYA & MAAF ZAHIR BATIN!

Share this post


Link to post
Share on other sites

Little bit modified... Mmm.. rasanya ni lagi ringkas...

#include <stdio.h>
#include <windows.h>

#define HIGH 1000
#define CHANCE 10
void initialize(), playGame(int), readGuess();
int target, guess, tGuess=0, computer_think=1;

void main()
{ 
    computer_think = GetTickCount() % HIGH;
    
    printf("--< GUESSING NUMBER GAME >--\n\n"
            "Feel free to play?? Think any number between 1 and %d.\n"
            "Try to guess any number then press enter.\n"
            "I will tell you whether your guess is too high or too lower\n", HIGH-1);

    readGuess();
}

void playGame(int ur_guess)
{
    if (ur_guess < computer_think) printf("too low\n");
    else if    (ur_guess == computer_think)
    {    
        printf("YOU'RE THE WINNER!!!\n");
        exit(0);
    }
    else printf("too high\n");
}

void readGuess()
{
    do{
        printf("\nYou have %d times chance to guess. Your guess no.: ", CHANCE - tGuess);
        scanf("%d", &guess);
        playGame(guess);
        tGuess++;
    }while(tGuess <= CHANCE-1);
    printf("\nLOOOOOOOOOOOSERR! JAWAPANNYA: %d\n", computer_think);
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...