Chapter 4 : Making Decisions About…and Planning Program Flow

This forum contains all the assignments and questions readers may have for each chapter.

Moderator: gary.bennett

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Fri Aug 03, 2012 1:59 pm

angelo.destefano wrote:Hi Gary, never mind! I just commented out the [pool drain] and the program ran successfully. I also completed the exercises with ease. I am enjoying this course very much and will probably finish within a week. I hope to start the next course in the sequence on August 6th so I can participate in a live class. Take care.


Great job!! Looking forward to Monday!
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Bruœ » Sat Dec 01, 2012 10:43 pm

Question one is about the random number guessing game in Alice.

After a correct guess is eventually made and I try and continue with another game by selecting "true" for Play Again, it just loops asking if I want to play again. My keepPlaying getBoolean is identical in form and outside the innermost while loop as shown on p79 if that is all that is required (as shown in the video too). If keepPlaying is still true why doesn't the loop start over? I tried adding an "If" loop below that with keep playing then true else keep playing false. I haven't been able to replay the game without restarting no matter what I do.

If keepPlaying is true, why doesn't it jump back up and get another random?

Second question (or a lightbulb popped on) I suddenly had while in Xcode. (Y or N response please)

If inside a while statement, if it finds something saying the variable is true it loops and if it is false it steps out? This is more a confirmation than a question after the hour that has passed in writing this question down for the forum.
Bruœ
 
Posts: 0
Joined: Sat Dec 01, 2012 2:26 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Bruœ » Sat Dec 01, 2012 10:49 pm

In Xcode for the random number generator game

I could place my reassuring NSLog message "Game Over" outside my main While loop curly brace but then if I tried to place a declared int roundsPlayed variable in an NSLog message to show how many rounds had been played during the game it said it was use of undeclared identifier unless I put it inside the While curly brace. Why can't I do this outside my While loop like the NSLog message? This bugs me as it took me 10 minutes to figure out what was allowed but I want to understand why.
Bruœ
 
Posts: 0
Joined: Sat Dec 01, 2012 2:26 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Sun Dec 02, 2012 9:41 am

Bruœ wrote:In Xcode for the random number generator game

I could place my reassuring NSLog message "Game Over" outside my main While loop curly brace but then if I tried to place a declared int roundsPlayed variable in an NSLog message to show how many rounds had been played during the game it said it was use of undeclared identifier unless I put it inside the While curly brace. Why can't I do this outside my While loop like the NSLog message? This bugs me as it took me 10 minutes to figure out what was allowed but I want to understand why.


Hi Bruce, if you post your code, I'd be happy to look at it.
Gary
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Bruœ » Sun Dec 02, 2012 10:10 am

gary.bennett wrote:
Bruœ wrote:In Xcode for the random number generator game

I could place my reassuring NSLog message "Game Over" outside my main While loop curly brace but then if I tried to place a declared int roundsPlayed variable in an NSLog message to show how many rounds had been played during the game it said it was use of undeclared identifier unless I put it inside the While curly brace. Why can't I do this outside my While loop like the NSLog message? This bugs me as it took me 10 minutes to figure out what was allowed but I want to understand why.


Hi Bruce, if you post your code, I'd be happy to look at it.
Gary


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {


int timesGuessed = 0;
int randomNumber = 0;
int roundsPlayed = 0;
int userGuess = 0;
bool continueGuessing = true;
bool keepPlaying = true;
char yesNo = ' ';

while (keepPlaying)
{
randomNumber = (arc4random() % 101);
NSLog(@"The RANDOM NUMER to guess is %d",randomNumber);
while (continueGuessing)
{
timesGuessed++;
NSLog(@"Pick a number between 0 and 100");
scanf ("%i", &userGuess);
fgetc(stdin);
if (userGuess == randomNumber)
{
continueGuessing=false;
NSLog(@"CORRECT!");
NSLog(@"Number of guesses was %d",timesGuessed);

}
// nested if statement
else if (userGuess > randomNumber)
{
NSLog(@"Too High!");
}
else
{
NSLog(@"Too Low!");
}
NSLog(@"The GUESS was %d",userGuess);

}
timesGuessed = 0;
roundsPlayed++;

NSLog(@"Play Again Y or N ?");
yesNo = fgetc(stdin);
if (yesNo == 'n' || yesNo == 'N')
{
keepPlaying=false;
}
continueGuessing=true;
}
// the position below removed the error
NSLog(@"Game Over! You played %d times",roundsPlayed);
}
// uncomment this next line to invoke Undeclared Identifier error
// NSLog(@"Game Over! You played %d times",roundsPlayed);

/* I originally had the above NSLog statement here after the closing While curly brace when it was just a Game Over! message. When I was doing the extra challenges at the end of the chapter, I thought this was a good spot to add my new variable roundsPlayed. I kept getting the Undeclared Identifier error and it really stumped me why as I had already declared it. The "Sorry, if it hurts then don't do it doesn't always work with me." I would love a comment about it. Thank You! If there is a way to include my code while keeping my indention formatting on the forum, please tell me. My copy'n'paste lost it.*/

return 0;
}
Bruœ
 
Posts: 0
Joined: Sat Dec 01, 2012 2:26 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Sun Dec 02, 2012 10:27 am

Bruœ wrote:
gary.bennett wrote:
Bruœ wrote:In Xcode for the random number generator game

I could place my reassuring NSLog message "Game Over" outside my main While loop curly brace but then if I tried to place a declared int roundsPlayed variable in an NSLog message to show how many rounds had been played during the game it said it was use of undeclared identifier unless I put it inside the While curly brace. Why can't I do this outside my While loop like the NSLog message? This bugs me as it took me 10 minutes to figure out what was allowed but I want to understand why.


Hi Bruce, if you post your code, I'd be happy to look at it.
Gary


#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {


int timesGuessed = 0;
int randomNumber = 0;
int roundsPlayed = 0;
int userGuess = 0;
bool continueGuessing = true;
bool keepPlaying = true;
char yesNo = ' ';

while (keepPlaying)
{
randomNumber = (arc4random() % 101);
NSLog(@"The RANDOM NUMER to guess is %d",randomNumber);
while (continueGuessing)
{
timesGuessed++;
NSLog(@"Pick a number between 0 and 100");
scanf ("%i", &userGuess);
fgetc(stdin);
if (userGuess == randomNumber)
{
continueGuessing=false;
NSLog(@"CORRECT!");
NSLog(@"Number of guesses was %d",timesGuessed);

}
// nested if statement
else if (userGuess > randomNumber)
{
NSLog(@"Too High!");
}
else
{
NSLog(@"Too Low!");
}
NSLog(@"The GUESS was %d",userGuess);

}
timesGuessed = 0;
roundsPlayed++;

NSLog(@"Play Again Y or N ?");
yesNo = fgetc(stdin);
if (yesNo == 'n' || yesNo == 'N')
{
keepPlaying=false;
}
continueGuessing=true;
}
// the position below removed the error
NSLog(@"Game Over! You played %d times",roundsPlayed);
}
// uncomment this next line to invoke Undeclared Identifier error
// NSLog(@"Game Over! You played %d times",roundsPlayed);

/* I originally had the above NSLog statement here after the closing While curly brace when it was just a Game Over! message. When I was doing the extra challenges at the end of the chapter, I thought this was a good spot to add my new variable roundsPlayed. I kept getting the Undeclared Identifier error and it really stumped me why as I had already declared it. The "Sorry, if it hurts then don't do it doesn't always work with me." I would love a comment about it. Thank You! If there is a way to include my code while keeping my indention formatting on the forum, please tell me. My copy'n'paste lost it.*/

return 0;
}


Because your variable has gone out of scoop if place it outside of the brackets that you declare it in. Hope that helps.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Bruœ » Sun Dec 02, 2012 11:50 am

gary.bennett wrote:Because your variable has gone out of scoop if place it outside of the brackets that you declare it in. Hope that helps.


Okay, I understand now! I see that it wasn't finding the declaration in the autorelease pool bracket area near the beginning of the code. Thanks for the quick replies. I also had posted a question a few minutes before this on Alice. Still trying to figure that one out. Somehow I am finding Xcode easier to follow the loops.
Bruœ
 
Posts: 0
Joined: Sat Dec 01, 2012 2:26 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Wed Dec 05, 2012 10:31 am

Bruœ wrote:
gary.bennett wrote:Because your variable has gone out of scoop if place it outside of the brackets that you declare it in. Hope that helps.


Okay, I understand now! I see that it wasn't finding the declaration in the autorelease pool bracket area near the beginning of the code. Thanks for the quick replies. I also had posted a question a few minutes before this on Alice. Still trying to figure that one out. Somehow I am finding Xcode easier to follow the loops.


Not seeing the posts on Alice. Can you email me them. thanks
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Bruœ » Wed Dec 05, 2012 11:08 am

gary.bennett wrote:
Bruœ wrote:
gary.bennett wrote:Because your variable has gone out of scoop if place it outside of the brackets that you declare it in. Hope that helps.


Okay, I understand now! I see that it wasn't finding the declaration in the autorelease pool bracket area near the beginning of the code. Thanks for the quick replies. I also had posted a question a few minutes before this on Alice. Still trying to figure that one out. Somehow I am finding Xcode easier to follow the loops.


Not seeing the posts on Alice. Can you email me them. thanks


Hmmm, I can see the question above when not logged in as the 2nd entry on this 2nd page of this Chapter 4 discussion. I will just quote it below again and if you get to it, great. I have younger person I want to show/and to use Alice so I would like to try and figure out what I was doing wrong. Thank You for your time and attention.

"After a correct guess is eventually made and I try and continue with another game by selecting "true" for Play Again, it just loops asking if I want to play again. My keepPlaying getBoolean is identical in form and outside the innermost while loop as shown on p79 if that is all that is required (as shown in the video too). If keepPlaying is still true why doesn't the loop start over? I tried adding an "If" loop below that with keep playing then true else keep playing false. I haven't been able to replay the game without restarting no matter what I do.

If keepPlaying is true, why doesn't it jump back up and get another random?"
Bruœ
 
Posts: 0
Joined: Sat Dec 01, 2012 2:26 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Wed Dec 05, 2012 11:21 am

Bruce
Remember there are two loops. Are you changing both the control variables?
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Bruœ » Wed Dec 05, 2012 12:58 pm

gary.bennett wrote:Bruce
Remember there are two loops. Are you changing both the control variables?


I got it to work. I had not set continuePlaying back to True before the end of the loop. I understand what it was doing now. Thanks! The variables keepPlaying and continuePlaying sound so alike to me that I thought I had mistakenly declared one by mistake early on in creating the code since it has been a couple days since looking at it. A step feature would have allowed easier debugging for sure - but I understand why it isn't there.

Off topic, do you have any opinion on Lumosity.com? I'm trying to get out the cobwebs!
Bruœ
 
Posts: 0
Joined: Sat Dec 01, 2012 2:26 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby mark.lallemand » Fri Dec 14, 2012 10:46 am

//
// main.m
// final project xcode verison
//
// Created by Tyler Lallemand on 12/11/12.
// Copyright (c) 2012 Tyler Lallemand. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

// insert code here...
int randomNumber = 1;
int userGuess = 1;
int guessCount = 0;
int gameCount = 1;
BOOL continueGuessing = TRUE;
BOOL keepPlaying = TRUE;
char yesNo = ' ';

while (keepPlaying){

randomNumber = (arc4random() % 101);
NSLog(@"The random number to is: %d",randomNumber);
while (continueGuessing){

NSLog(@"Pick A number between 0 and 100");
scanf("%i" , &userGuess);
fgetc(stdin);//remove CR/LF i.e extra charater

guessCount = guessCount + 1;
if (userGuess == randomNumber)
{
continueGuessing = FALSE;
NSLog(@"Your Winning like Charlie Sheen, Correct Guess!");
NSLog(@"You have guessed a total of %d times.",guessCount);

}
//nested if statement
else if (userGuess > randomNumber)
{
//user guessed too high
NSLog(@"Your guess was too high Mcfly");
NSLog(@"You have guessed a total of %d times.",guessCount);
}
else
{
// user guessed too low
NSLog(@"Your guess is too low bro");
NSLog(@"You have guessed a total of %d times.",guessCount);

}
//refactored from our Alice app. This way we only have to code once.
NSLog(@"The user guessed %d",userGuess);

}
NSLog(@"Play Again? Y, you know you want to play again. or N, are you sure you don't want to play again?");

yesNo = fgetc(stdin);

if (yesNo == 'N' || yesNo == 'n')
{
keepPlaying = FALSE;
NSLog(@"You have played %d times",gameCount);
NSLog(@"Bye, good riddens");
}
continueGuessing = TRUE;
gameCount = gameCount + 1;
guessCount = 0;


}
}
return 0;
}
This above ^ is my finish final project in Xcode :D
mark.lallemand
 
Posts: 2
Joined: Mon Jan 31, 2011 12:51 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby mark.lallemand » Mon Dec 17, 2012 7:18 am

Hey Gary i just wanted to know how you wanted me to send you my final Alice project :)
mark.lallemand
 
Posts: 2
Joined: Mon Jan 31, 2011 12:51 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby frank.irvine » Sun Jan 27, 2013 7:35 pm

AndieF wrote:Hello~

I'm trying to run through the exercise in Alice for Chapter 4 and am at a loss on the first "if / else". I can't figure out how to get to IF ( userGuess == randomNumber is TRUE). When I drag down the "If" and put it in place, it doesn't give me an option to change it to "userGuess". I can only choose continueGuessing or keepPlaying. I tried to drag down userGuess too but that didn't work either. Did I miss something here? Feel like I've been raking my brains over a cheese grater for the past hour!



Gary, I did not see the response to the above issue, I too am having troubles with this problem.
frank.irvine
 
Posts: 1
Joined: Fri Jan 04, 2013 1:20 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Mon Jan 28, 2013 9:13 am

frank.irvine wrote:
AndieF wrote:Hello~

I'm trying to run through the exercise in Alice for Chapter 4 and am at a loss on the first "if / else". I can't figure out how to get to IF ( userGuess == randomNumber is TRUE). When I drag down the "If" and put it in place, it doesn't give me an option to change it to "userGuess". I can only choose continueGuessing or keepPlaying. I tried to drag down userGuess too but that didn't work either. Did I miss something here? Feel like I've been raking my brains over a cheese grater for the past hour!



Gary, I did not see the response to the above issue, I too am having troubles with this problem.


Hi Frank,
Did you watch the video for class 3 or 4? I can cover it tonight in class tonight if you want?
Gary
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby kevingillet » Sun Mar 24, 2013 11:00 am

Hi everyone!

2 weeks ago i started reading the book.
I'm a complete newbie when it comes to programming, but it is fun to read and learn.

When i read the chapters they absolutely make sense to a complete newbie, but writing the code is a complete different story ;)

After i wrote the example with the book next to my computer i closed the book and started the exercise again and started over for a few times.
Eventually i ended up with the code posted beneath this post, but i find it difficult to figure out how the total guesses should increase.

I hope you can help me out a little bit....

Don't pay attention to the comments ;)
I did this for myself to clarify things.

Thanks!

Code: Select all
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
       
       //creating the integers to work with in the computer program
        int randomNumber = 1;
        int userGuess = 1;
        BOOL keepPlaying = TRUE;
        BOOL continueGuessing = TRUE;
        char yesNo = ' ';
        int guessCount = 0;
       
       
        //Creating a loop(block 1, while this is actually not a block)
        while (keepPlaying)
       
       
        //Creating a random number(block 2)
        {
           
        randomNumber = (arc4random() % 101);
        NSLog(@"The random number that you should guess is %d",randomNumber);
           
           
            //Creating a second loop inside the first loop because there can be two different choises. When the number is guessed correct, the user is asked to play again, because we don't write the whole code again for the Yes and No answer, we can continue playing by just ending the second loop and not the first. If we stop the first loop, the second loop automatically stops.
           
            while (continueGuessing)
            {
       
               
       
        //Scan the number given by the user(block 3)
       
           
            NSLog(@"Pick a number between 0 and 100");
            scanf("%i", &userGuess);
            fgetc(stdin);

                if (continueGuessing == TRUE)
                {
                   
                    guessCount = +1;
                   
                }
           
            //The if statement starts in block 3
            if (randomNumber == userGuess)
               
                //Now there comes a new block inside block 3, let's call it(block 3.1)
            {
                continueGuessing = FALSE;
                NSLog(@"You have guessed the correct number!");
               
            }
           
            //After the next else or else-if statement, the next block starts(block 3.2)
            else if (userGuess > randomNumber)
           
            {
                NSLog(@"Your guess is too high, please guess again");
               
            }
           
            //Else(block 3.3)
            else
            {
                NSLog(@"Your guess is too low, please guess again");
               
            }
            NSLog(@"You guessed %d", userGuess);
           
            }
        //Block 3 is closed here, we are going to ask the user if he/she wants to play again because if the game isn't finished the user will stay in the loop
       

        NSLog(@"Do you want to play again? Press Y for yes of N for no");
       
        yesNo = fgetc(stdin);
       
        //Block 4 is starting here, you asked the user Y of N, so there is a choice to be made and we have to make if-statements
       
        if (yesNo == 'N' || yesNo == 'n')
           
        {
            keepPlaying = FALSE;
           
        }
       
        continueGuessing = TRUE;
        }
       
       
        NSLog(@"You have guessed %d times", guessCount);
       
    }
    return 0;
}

kevingillet
 
Posts: 0
Joined: Mon Mar 11, 2013 1:28 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby kevingillet » Sat Mar 30, 2013 12:26 pm

Hi again everyone!

Sometimes struggling overtime will do great things with your brain ;)
I've tried to make the application a few more times and finally figured it out, while it was simpler than i thought.

Still i'm wondering if i'm walking the right path with my own made up method to print out how many times the game's been played and how many times the user guessed.

Gary, can you take a look at it and tell me if things should be done in a different way?


//
// main.m
// WillekeurigNummerApp2
//
// Created by Kevin Gillet on 30-03-13.
// Copyright (c) 2013 Kevin Gillet. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

int willekeurigNummer = 1;
int gebruikersGok = 1;
BOOL blijfSpelen = TRUE;
BOOL blijfGokken = TRUE;
char jaNee = ' ';
int gokTotaal = 0;
int spelTotaal = 0;


//De eerste loop starten zodat het spel door blijft lopen, het eerste blok start pas na het starten van een loop

while (blijfSpelen)

//Het willekeurige nummer aanmaken en aan de gebruiker laten zien(Blok 1)

{
willekeurigNummer = (arc4random() & 101);
NSLog(@"Het te raden willekeurige nummer is %d", willekeurigNummer);
spelTotaal = spelTotaal +1;

//Tweede loop starten zodat wanneer er verkeerd geraden word, hier weer begonnen wordt. De bovenliggende loop wordt alleen weer gebruikt wanneer er opnieuw gespeeld moet worden na het eindigen van het spel

while (blijfGokken)

//De gebruiker laten weten dat hij/zij een getal moet ingeven. Dit getal moet opgeslagen worden in het geheugen door gebruik van "&" en daarna weer leeg gemaakt worden

//Later heb ik een integer toegevoegd die het totaal aantal gokken optelt, deze wordt getoond zodra het juiste getal is geraden

{
NSLog(@"Raad een getal tussen 0 en 100");
scanf("%i", &gebruikersGok);
fgetc(stdin);
gokTotaal = gokTotaal +1;



//Vergelijken en if/else statements

if (gebruikersGok == willekeurigNummer)
{
NSLog(@"Je hebt het juiste nummer geraden!");
blijfGokken = FALSE;
}

else if (gebruikersGok > willekeurigNummer)

{

NSLog(@"Je hebt te hoog geraden, probeer het nog een keer");


}
else
{
NSLog(@"Je hebt te laag geraden, probeer het nog een keer");

}

}
//Na het vergelijken en het juiste antwoord moet het programma vragen of de gebruiker nog een keer wil spelen, anders wordt de loop oneindig in dit geval terwijl je het juiste antwoord geraden hebt


NSLog(@"Je hebt er %d keer over gedaan om het goede antwoord te raden", gokTotaal);

NSLog(@"Wil je nog een keer spelen? Kies J voor ja en N voor nee");

jaNee = fgetc(stdin);

if (jaNee == 'N' || jaNee =='n')
{
NSLog(@"Je hebt het spel %d keer gespeeld", spelTotaal);
blijfSpelen = FALSE;
}

else
{
gokTotaal = 0;
blijfGokken = TRUE;

}


}


}
return 0;
}
kevingillet
 
Posts: 0
Joined: Mon Mar 11, 2013 1:28 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Wed Apr 03, 2013 11:24 am

kevingillet wrote:Hi again everyone!

Sometimes struggling overtime will do great things with your brain ;)
I've tried to make the application a few more times and finally figured it out, while it was simpler than i thought.

Still i'm wondering if i'm walking the right path with my own made up method to print out how many times the game's been played and how many times the user guessed.

Gary, can you take a look at it and tell me if things should be done in a different way?


//
// main.m
// WillekeurigNummerApp2
//
// Created by Kevin Gillet on 30-03-13.
// Copyright (c) 2013 Kevin Gillet. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

int willekeurigNummer = 1;
int gebruikersGok = 1;
BOOL blijfSpelen = TRUE;
BOOL blijfGokken = TRUE;
char jaNee = ' ';
int gokTotaal = 0;
int spelTotaal = 0;


//De eerste loop starten zodat het spel door blijft lopen, het eerste blok start pas na het starten van een loop

while (blijfSpelen)

//Het willekeurige nummer aanmaken en aan de gebruiker laten zien(Blok 1)

{
willekeurigNummer = (arc4random() & 101);
NSLog(@"Het te raden willekeurige nummer is %d", willekeurigNummer);
spelTotaal = spelTotaal +1;

//Tweede loop starten zodat wanneer er verkeerd geraden word, hier weer begonnen wordt. De bovenliggende loop wordt alleen weer gebruikt wanneer er opnieuw gespeeld moet worden na het eindigen van het spel

while (blijfGokken)

//De gebruiker laten weten dat hij/zij een getal moet ingeven. Dit getal moet opgeslagen worden in het geheugen door gebruik van "&" en daarna weer leeg gemaakt worden

//Later heb ik een integer toegevoegd die het totaal aantal gokken optelt, deze wordt getoond zodra het juiste getal is geraden

{
NSLog(@"Raad een getal tussen 0 en 100");
scanf("%i", &gebruikersGok);
fgetc(stdin);
gokTotaal = gokTotaal +1;



//Vergelijken en if/else statements

if (gebruikersGok == willekeurigNummer)
{
NSLog(@"Je hebt het juiste nummer geraden!");
blijfGokken = FALSE;
}

else if (gebruikersGok > willekeurigNummer)

{

NSLog(@"Je hebt te hoog geraden, probeer het nog een keer");


}
else
{
NSLog(@"Je hebt te laag geraden, probeer het nog een keer");

}

}
//Na het vergelijken en het juiste antwoord moet het programma vragen of de gebruiker nog een keer wil spelen, anders wordt de loop oneindig in dit geval terwijl je het juiste antwoord geraden hebt


NSLog(@"Je hebt er %d keer over gedaan om het goede antwoord te raden", gokTotaal);

NSLog(@"Wil je nog een keer spelen? Kies J voor ja en N voor nee");

jaNee = fgetc(stdin);

if (jaNee == 'N' || jaNee =='n')
{
NSLog(@"Je hebt het spel %d keer gespeeld", spelTotaal);
blijfSpelen = FALSE;
}

else
{
gokTotaal = 0;
blijfGokken = TRUE;

}


}


}
return 0;
}


Kevin,
Inside each while statement increment a counter. When they guess the right number or stop playing the counter will contain the number of times played. Here is some pseudo code:

int totalGames = 0;
while (keepPlaying)
{
totalGames = totalGames +1

..
...
int totalGuesses = 0;
while (keepGuessing)
{
totalGuesses = totalGuesses +1
....
....
}
print totalGuesses
}
print totalGames
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby kevingillet » Thu Apr 04, 2013 1:11 am

Hi Gary,

thanks for replying, now i know i'm heading in the right direction.
kevingillet
 
Posts: 0
Joined: Mon Mar 11, 2013 1:28 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Thu Apr 04, 2013 10:19 am

kevingillet wrote:Hi Gary,

thanks for replying, now i know i'm heading in the right direction.


Let me know if you need anything else.
Gary
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby kevingillet » Thu Apr 25, 2013 3:29 am

Hi Gary,

i started a new program to test and practice some more if/else statements.
The program has for defined letters, that have to be guessed one by one.

The first three letters work well, but i'm missing something at the fourth letter.... the result is always 'fault' even when i type in the correct letter.

Is it because the letter 't' is also the first letter?
Or does memory not allow me to store the fourth letter?
Or is it something else?

Thanks for helping!



#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

char firstCharacter = 't';
char secondCharacter = 'e';
char thirdCharacter = 's';
char fourthCharacter = 't';
char firstCharacterGuess = ' ';
char secondCharacterGuess = ' ';
char thirdCharacterGuess = ' ';
char fourthCharacterGuess = ' ';
char yesNo = ' ';
char totalWord = ' ';
BOOL keepPlaying = TRUE;
BOOL continueGuessing1 = TRUE;
BOOL continueGuessing2 = TRUE;
BOOL continueGuessing3 = TRUE;
BOOL continueGuessing4 = TRUE;
int firstGuessCount = 0;
int secondGuessCount = 0;
int thirdGuessCount = 0;
int fourthGuessCount = 0;
int totalGuessCount= 0;



while (keepPlaying)
{
NSLog(@"You should guess a 4-letter word in steps, you have to guess the first letter first and so on...");

while (continueGuessing1)
{

NSLog(@"Guess the first letter.");
scanf("%s", &firstCharacterGuess);
firstGuessCount = firstGuessCount +1;
fgetc(stdin);

if (firstCharacter == firstCharacterGuess)
{
NSLog(@"You have guessed the correct letter, move on to letter 2.");
NSLog(@"It took you %d times to guess the first letter", firstGuessCount);
continueGuessing1 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing1 = TRUE;
}

}

while (continueGuessing2)
{

NSLog(@"Guess the second letter.");
scanf("%s", &secondCharacterGuess);
secondGuessCount = secondGuessCount +1;
fgetc(stdin);

if (secondCharacter == secondCharacterGuess)
{
NSLog(@"You have guessed the correct letter, move on to letter 3.");
NSLog(@"It took you %d times to guess the second letter", secondGuessCount);
continueGuessing2 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing2 = TRUE;
}

}

while (continueGuessing3)
{

NSLog(@"Guess the third letter.");
scanf("%s", &thirdCharacterGuess);
thirdGuessCount = thirdGuessCount +1;
fgetc(stdin);

if (thirdCharacter == thirdCharacterGuess)
{
NSLog(@"You have guessed the correct letter, move on to letter 4.");
NSLog(@"It took you %d times to guess the third letter", thirdGuessCount);
continueGuessing3 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing3 = TRUE;
}

}

while (continueGuessing4)
{

NSLog(@"Guess the fourth letter.");
scanf("%s", &fourthCharacterGuess);
fourthGuessCount = fourthGuessCount +1;
fgetc(stdin);

if (fourthCharacterGuess == fourthCharacter)
{
NSLog(@"You have guessed the correct fourth letter.");
NSLog(@"It took you %d times to guess the fourth letter", fourthGuessCount);
continueGuessing4 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing4 = TRUE;
}

}

}

totalWord = firstCharacter + secondCharacter + thirdCharacter + fourthCharacter;
NSLog(@"Great job! The word that you've guessed is %hhd.", totalWord );
totalGuessCount = firstGuessCount + secondGuessCount + thirdGuessCount + fourthGuessCount;
NSLog(@"you have guessed %d times to guess the correct word.", totalGuessCount);

firstGuessCount = 0;
secondGuessCount = 0;
thirdGuessCount = 0;
fourthGuessCount = 0;
totalGuessCount = 0;

NSLog(@"Do you want to play again? typ 'y' for 'yes and 'n' for 'no'.");

if (yesNo == 'n' || yesNo == 'N')
{
keepPlaying = FALSE;
}

else
{
keepPlaying = TRUE;
}


}
return 0;
}
kevingillet
 
Posts: 0
Joined: Mon Mar 11, 2013 1:28 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Sun Apr 28, 2013 9:22 pm

kevingillet wrote:Hi Gary,

i started a new program to test and practice some more if/else statements.
The program has for defined letters, that have to be guessed one by one.

The first three letters work well, but i'm missing something at the fourth letter.... the result is always 'fault' even when i type in the correct letter.

Is it because the letter 't' is also the first letter?
Or does memory not allow me to store the fourth letter?
Or is it something else?

Thanks for helping!


Hi Kevin, Can you email me your project and I will look at it.
Gary (garyatXcelmeDotCom
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

char firstCharacter = 't';
char secondCharacter = 'e';
char thirdCharacter = 's';
char fourthCharacter = 't';
char firstCharacterGuess = ' ';
char secondCharacterGuess = ' ';
char thirdCharacterGuess = ' ';
char fourthCharacterGuess = ' ';
char yesNo = ' ';
char totalWord = ' ';
BOOL keepPlaying = TRUE;
BOOL continueGuessing1 = TRUE;
BOOL continueGuessing2 = TRUE;
BOOL continueGuessing3 = TRUE;
BOOL continueGuessing4 = TRUE;
int firstGuessCount = 0;
int secondGuessCount = 0;
int thirdGuessCount = 0;
int fourthGuessCount = 0;
int totalGuessCount= 0;



while (keepPlaying)
{
NSLog(@"You should guess a 4-letter word in steps, you have to guess the first letter first and so on...");

while (continueGuessing1)
{

NSLog(@"Guess the first letter.");
scanf("%s", &firstCharacterGuess);
firstGuessCount = firstGuessCount +1;
fgetc(stdin);

if (firstCharacter == firstCharacterGuess)
{
NSLog(@"You have guessed the correct letter, move on to letter 2.");
NSLog(@"It took you %d times to guess the first letter", firstGuessCount);
continueGuessing1 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing1 = TRUE;
}

}

while (continueGuessing2)
{

NSLog(@"Guess the second letter.");
scanf("%s", &secondCharacterGuess);
secondGuessCount = secondGuessCount +1;
fgetc(stdin);

if (secondCharacter == secondCharacterGuess)
{
NSLog(@"You have guessed the correct letter, move on to letter 3.");
NSLog(@"It took you %d times to guess the second letter", secondGuessCount);
continueGuessing2 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing2 = TRUE;
}

}

while (continueGuessing3)
{

NSLog(@"Guess the third letter.");
scanf("%s", &thirdCharacterGuess);
thirdGuessCount = thirdGuessCount +1;
fgetc(stdin);

if (thirdCharacter == thirdCharacterGuess)
{
NSLog(@"You have guessed the correct letter, move on to letter 4.");
NSLog(@"It took you %d times to guess the third letter", thirdGuessCount);
continueGuessing3 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing3 = TRUE;
}

}

while (continueGuessing4)
{

NSLog(@"Guess the fourth letter.");
scanf("%s", &fourthCharacterGuess);
fourthGuessCount = fourthGuessCount +1;
fgetc(stdin);

if (fourthCharacterGuess == fourthCharacter)
{
NSLog(@"You have guessed the correct fourth letter.");
NSLog(@"It took you %d times to guess the fourth letter", fourthGuessCount);
continueGuessing4 = FALSE;
}

else
{
NSLog(@"Too bad, the letter you guessed isn't the one we are looking for, guess again");
continueGuessing4 = TRUE;
}

}

}

totalWord = firstCharacter + secondCharacter + thirdCharacter + fourthCharacter;
NSLog(@"Great job! The word that you've guessed is %hhd.", totalWord );
totalGuessCount = firstGuessCount + secondGuessCount + thirdGuessCount + fourthGuessCount;
NSLog(@"you have guessed %d times to guess the correct word.", totalGuessCount);

firstGuessCount = 0;
secondGuessCount = 0;
thirdGuessCount = 0;
fourthGuessCount = 0;
totalGuessCount = 0;

NSLog(@"Do you want to play again? typ 'y' for 'yes and 'n' for 'no'.");

if (yesNo == 'n' || yesNo == 'N')
{
keepPlaying = FALSE;
}

else
{
keepPlaying = TRUE;
}


}
return 0;
}
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby kevingillet » Mon Apr 29, 2013 2:24 am

Thanks Gary,

An email has been send to you.
I just found out that instead of:

char firstCharacterGuess = ' ';
char secondCharacterGuess = ' ';
char thirdCharacterGuess = ' ';
char fourthCharacterGuess = ' ';


char firstCharacterGuess , secondCharacterGuess, thirdCharacterGuess, fourthCharacterGuess = ' ';

is also syntactically correct.
kevingillet
 
Posts: 0
Joined: Mon Mar 11, 2013 1:28 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Goran » Tue Jul 16, 2013 1:25 am

i am stuck at first line in alice
>randomNumber set value to randomNumber (minimal) i cant find how i put this minnimal atribute,i try everything
anyone?
Goran
 
Posts: 0
Joined: Thu Jul 04, 2013 2:16 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Drago » Mon Nov 04, 2013 10:44 pm

Random Number Generator App.
Exercise (page 82 printed book).
The app you have been assigned to write generates a random number between 0 and 100 inclusive, and asks the user to guess the number. The user has to do this until the number is guessed. You can use any object from the Alice gallery to ask the user for his or her guess, and you can also choose any world for your object to be in. The object will provide a visual queue for each high, low, and correct guess. The number that the user guesses will be displayed on the console. When the user guesses the correct answer, he or she will be asked if he or she wants to play again.

Code: Select all
         // insert code here...
        //NSLog(@"Hello, World!");
        int randomNumber = 1;
        int userGuess = 1;
        BOOL continueGuessing = TRUE;
        BOOL keepPlaying = TRUE;
        char yesNo = ' ';
       
        while (keepPlaying) {
            randomNumber = (arc4random() % 101);
            NSLog(@"The random number to guess is: %d", randomNumber);
            while (continueGuessing) {
                NSLog(@"Pick a number between 0 and 100. ");
                scanf("%i", &userGuess);
                fgetc(stdin);
                if (userGuess == randomNumber) {
                    continueGuessing = FALSE;
                    NSLog(@"Correct number!");
                    NSLog(@"The user guessed %d", userGuess);
                }
                else if (userGuess > randomNumber){
                    //user guessed to high
                    NSLog(@"Your guess is to high");
                }
                else{
                    //no reason to check if userGuess < randomNumber. It has to be.
                    NSLog(@"Your guess is too low");
                }
            }
            NSLog(@"Play again? Y or N");
            yesNo = fgetc(stdin);
            if (yesNo =='N' || yesNo == 'n') {
                keepPlaying = FALSE;
            }
            continueGuessing = TRUE;
        }

Last edited by Drago on Tue Nov 05, 2013 8:06 pm, edited 1 time in total.
"It's more fun to be a pirate than to join the navy."
Steve Jobs.
User avatar
Drago
 
Posts: 0
Joined: Thu Jun 20, 2013 3:41 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Drago » Tue Nov 05, 2013 8:06 pm

Exercises - Chapter 4 - page 86 (printed)

1) Extend the random number generator app to print to the console how many times the user guessed before he or she guessed the correct random number.

Code: Select all
        int randomNumber = 1;
        int userGuess = 1;
        int numberNotGuesses = 0;//exercise pag 86
        BOOL continueGuessing = TRUE;
        BOOL keepPlaying = TRUE;
        char yesNo = ' ';
       
        while (keepPlaying) {
            randomNumber = (arc4random() % 101);
            NSLog(@"The random number to guess is: %d", randomNumber);
            while (continueGuessing) {
                NSLog(@"Pick a number between 0 and 100. ");
                scanf("%i", &userGuess);
                fgetc(stdin);
                if (userGuess == randomNumber) {
                    continueGuessing = FALSE;
                    NSLog(@"Correct number!");
                    NSLog(@"The user guessed %d", userGuess);
                    NSLog(@"Attempts %d", numberNotGuesses);//exercise pag 86
                }
                else if (userGuess > randomNumber){
                    //user guessed to high
                    numberNotGuesses++;//exercise pag 86
                    NSLog(@"Your guess is to high");
                }
                else{
                    //no reason to check if userGuess < randomNumber. It has to be.
                    numberNotGuesses++;//exercise pag 86
                    NSLog(@"Your guess is too low");
                }
            }
            NSLog(@"Play again? Y or N");
            yesNo = fgetc(stdin);
            if (yesNo =='N' || yesNo == 'n') {
                keepPlaying = FALSE;
            }
            continueGuessing = TRUE;
            numberNotGuesses=0;//exercise pag 86
        }


2) Extend the random number generator app to print to the console how many times the user played the app. Print this value when the user quits the app.

Code: Select all
        int randomNumber = 1;
        int userGuess = 1;
        int numberNotGuesses = 0;//exercise pag 86
        int timesPlayed = 0;//exercise pag 86
        BOOL continueGuessing = TRUE;
        BOOL keepPlaying = TRUE;
        char yesNo = ' ';
       
        while (keepPlaying) {
            randomNumber = (arc4random() % 101);
            NSLog(@"The random number to guess is: %d", randomNumber);
            while (continueGuessing) {
                NSLog(@"Pick a number between 0 and 100. ");
                scanf("%i", &userGuess);
                fgetc(stdin);
                if (userGuess == randomNumber) {
                    continueGuessing = FALSE;
                    timesPlayed++;//exercise pag 86
                    NSLog(@"Correct number!");
                    NSLog(@"The user guessed %d", userGuess);
                    NSLog(@"Attempts %d", numberNotGuesses);//exercise pag 86
                }
                else if (userGuess > randomNumber){
                    //user guessed to high
                    numberNotGuesses++;//exercise pag 86
                    timesPlayed++;//exercise pag 86
                    NSLog(@"Your guess is to high");
                }
                else{
                    //no reason to check if userGuess < randomNumber. It has to be.
                    numberNotGuesses++;//exercise pag 86
                    timesPlayed++;//exercise pag 86
                    NSLog(@"Your guess is too low");
                }
            }
            NSLog(@"Play again? Y or N");
            yesNo = fgetc(stdin);
            if (yesNo =='N' || yesNo == 'n') {
                keepPlaying = FALSE;
            }
            continueGuessing = TRUE;
            numberNotGuesses=0;//exercise pag 86
        }
        NSLog(@"Times played %d", timesPlayed);//exercise pag 86
"It's more fun to be a pirate than to join the navy."
Steve Jobs.
User avatar
Drago
 
Posts: 0
Joined: Thu Jun 20, 2013 3:41 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby shwebk » Mon Nov 18, 2013 8:40 pm

Hi Gary

In regards to the exercises in chapter 4. I have been able to get the app to tell me the guessCount and gameCount, but I have not been able to increment the value more than one. Is there a function to store the value and then add to it? Please check my code, I have highlighted my code in red. There are some parts that I commented out, that was me just learning and developing the statements.

Thanks

shwebk

int main(int argc, const char * argv[])
{

@autoreleasepool {

// insert code here...

int randomNumber = 1;
int userGuess = 1;
BOOL continueGuessing = TRUE;
BOOL keepPlaying = TRUE;
char yesNo = ' ';
int guessCount = 0;
int gameCount = 0;


while (keepPlaying)
{
randomNumber = (arc4random() % 101);
NSLog(@"The random number to guess is: %d", randomNumber);
while (continueGuessing)
{
NSLog(@"Pick a number between 0 and 100. ");
scanf("%i", & userGuess);
fgetc(stdin);//remove CR/LF i.e extra character
if (userGuess == randomNumber)
{
continueGuessing = FALSE;
NSLog(@"Correct number!");
}

//nested if statement
else if (userGuess > randomNumber)

{ //user guessed to high
NSLog(@ "Your guess is to high.");
}
else
{
// no reason to check if userGuess < randomNumber. It has to be.
NSLog(@"Your guess is too low.");

}
// refactored from our Alice app. This way we only have to code once.
NSLog(@"The user guessed. %d", userGuess);

// To get guessCount
if ( userGuess < randomNumber ) { guessCount = +1;}
//{NSLog(@"You < guessed this many times. %d", guessCount);}

if ( userGuess > randomNumber ) { guessCount = +1;}
//{NSLog(@"You > guessed this many times. %d", guessCount);}

if ( userGuess == randomNumber ) { guessCount = +1;}
//{NSLog(@"You guessed this many times. %d", guessCount);}




}
NSLog(@"Play again Y or N?");
yesNo = fgetc(stdin);

if ( userGuess == randomNumber ) { guessCount = +1;}
{NSLog(@"You guessed this many times. %d", guessCount);}


if (yesNo == 'N' || yesNo == 'n' )
{
keepPlaying = FALSE;

}

continueGuessing = TRUE;

// To get gameCount
if ( yesNo == 'N' || yesNo == 'n' || yesNo =='Y' || yesNo == 'y') { gameCount = +1;}
{NSLog(@"You played this many times. %d", gameCount);}

//if ( yesNo == 'Y' || yesNo == 'y' ) { gameCount = +1;}
//{NSLog(@"You played this many times y. %d", gameCount);}



}




}
return 0;
}
shwebk
 
Posts: 0
Joined: Mon Nov 18, 2013 5:27 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Leo » Thu Jan 16, 2014 11:48 pm

Hi Gary,

I have some problems understanding the meaning and purpose of "char" in Line 21. Also, although you've explained the use and purpose of "fgetc(stdin)", I still don't understand what it really does. Please help me.
P.S. Something weird going on with Xcode. I inserted the exact code on the book to the computer, but Xcode just keeps telling me that some part of the code is wrong. I just tried cut and paste the "wrong" code and everything went well suddenly. How did that happen?

Thanks.
Leo
 
Posts: 0
Joined: Thu Jan 16, 2014 5:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby fora » Sun Mar 02, 2014 10:51 am

Where can I find the video that shows how to drag and drop all the tiles in Alice? Thanks
fora
 
Posts: 0
Joined: Thu Feb 27, 2014 3:45 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Drago » Fri Jun 06, 2014 9:16 am

Chapter 4 - Exercises

1 - Extend the random number generator app to print to the console how many times the user guessed before he or she guessed the correct random number. Do this in both Alice and Objective-C.
2 - Extend the random number generator app to print to the console how many times the user played the app. Print this value when the user quits the app. Do this in both Alice and Objective-C.

Code: Select all
// insert code here...
        int randomNumber = 1;
        int userGuess = 1;
        int guessCount = 0;
        int gameCount = 0;
        BOOL continueGuessing = TRUE;
        BOOL keepPlaying = TRUE;
        char yesNo = ' ';
       
        while (keepPlaying) {
            randomNumber = (arc4random() % 101);
            NSLog(@"The random number to guess is: %d", randomNumber);
            while (continueGuessing) {
                NSLog(@"Pick a number between 0 and 100. ");
                scanf("%i", &userGuess);
                fgetc(stdin);//remove CR/LF i.e extra character
                if (userGuess == randomNumber) {
                    continueGuessing = FALSE;
                    NSLog(@"Correct Number!");
                }
                else if (userGuess > randomNumber) {
                    //user guess too high
                    NSLog(@"Your guess is too high!");
                    guessCount += 1;
                }
                else {
                    //no reason to check if userGuess < randomNumber. It has to be.
                    NSLog(@"Your guess is too low");
                    guessCount += 1;
                }
                //refactored from our Alice app. This way we only have to code once.
                NSLog(@"The user guessed %d", userGuess);
            }//end continueGuessing
            NSLog(@"Times until guessed the number: %d", guessCount);
            NSLog(@"Play Again? Y or N");
            yesNo = fgetc(stdin);
            if (yesNo == 'N' || yesNo == 'n') {
                keepPlaying = FALSE;
            }
            continueGuessing = TRUE;
            gameCount += 1;
        }// end while keepPlaying
        NSLog(@"Played the game %d times", gameCount);
"It's more fun to be a pirate than to join the navy."
Steve Jobs.
User avatar
Drago
 
Posts: 0
Joined: Thu Jun 20, 2013 3:41 pm

Previous

Return to Book -> Objective-C for Absolute Beginners: (2nd Edition) iPhone and Mac Programming Made Easy

cron