This module is responsible for overseeing the entire program; invoking the other modules as appropriate. It is the module allowing the user to play the game repeatedly.
1. Initialize program variables
2. Introduce the game
3. While the player wants to keep playing
a. pick a random number between 1 and 100
b. introduce the play
c. play the game
d. ask the player if they want to continue
4. say goodbye!
/* File: guess.c
Programmer: Tep Dobry
login: tep
Date:
*/
/* This module is responsible for overseeing the entire program; invoking
the other modules as appropriate. It is the module allowing the
user to play the game repeatedly.
*/
#include <stdio.h>
#include "guess.h"
#include "play.h"
#include "tfdef.h"
#define DEBUG
main()
{ int keep_playing = TRUE; /* do we keep playing? initially true. */
int hidden_number; /* the picked random number */
/* Introduce the game */
printf("\n\n\t\tWelcome to the Guessing Game\n");
/* While the player wants to keep playing */
while( keep_playing )
{ /* pick a random number between START and STOP */
hidden_number = PICK(START,STOP);
/* introduce the play */
printf("\nI'm thinking of a number between %d "
"and %d\nCan you guess it?\n\n",START,STOP);
#ifdef DEBUG
printf("debug:main:the pick is %d\n",hidden_number);
#endif
/* play the game */
play(hidden_number);
/* ask the player if they want to continue */
printf("\nDo you want to play again?\n");
printf("(1 for yes, 0 for no): ");
scanf("%d", &keep_playing);
}
/* say goodbye */
printf("\nThanks for the game.... bye...\n");
}
/* File: guess.h
Programmer: Tep Dobry
login: tep
Date:
*/
/* This module is responsible for overseeing the entire program; invoking
the other modules as appropriate. It is the module allowing the
user to play the game repeatedly. This file contains the macros needed
for the game.
*/
#include <stdlib.h>
#define START 1 /* macro for the beginning of range of numbers */
#define STOP 100 /* macro for the end or range of numbers */
/* This macro uses rand() to pick a random number, mod to limit it
and addition to offset it */
#define PICK(start,stop) (rand() % ((stop) - (start) + 1) + (start))
This is the module that will pick a random number between 1 and 100.
1. Pick a random number
2. make sure it is between 1 and 100
3. return pick
C provides a function to generate a random number, called rand(). The prototype is in <stdlib.h>:
int rand(void);
It is given nothing (that's what void means), and returns a random integer. Unfortunately, the number is in the range 0 to the maximum positive integer we can represent (about 2 billion).
Let's look at the code in class to see how this is done.
example function
int pick (int start, int stop){
//int x = rand();
int y = (rand()%(stop - start+1))+start;
return y;
This module is responsible for playing one round of the game. We will further divide this module into two parts:
The Sequence Module:
This module is responsible for sequencing the steps of play for one round of the game.
The Test Module:
This module is responsible for testing the user guess and giving the appropriate feedback.
The Sequence Module Algorithm:
This module is responsible for sequencing the steps of play for one round of the game.
Given: the random number, Return: win or lose
1. Initialize the number of guesses
2. while there are guesses remaining
a. ask the player to guess
b. if the guess is correct
return a winner!
c. otherwise give the hint
d. update the number of guesses
3. Return a loser :-(
The Test Module Algorithm: This module is responsible for testing the user guess and giving the
appropriate feedback.
Given: the pick and the guess, Return: win, high, or low
1. If the guess matches the pick
return win!
2. Otherwise if the guess is too low
return low!
3. Otherwise if the guess is too high
return high!
/* File: play.h
Programmer: Tep Dobry
login: tep
Date:
*/
/* This module is responsible for playing for one round of the game. */
/* This file contains prototypes and macros used in the module */
/* The maximum number of guesses is 6 */
#define MAX_GUESSES 6
#define LOSE 0
#define WIN 1
#define HI 2
#define LOW 3
int play(int pick);
/* This function plays one round of the game.
Given: the random number picked
Return: win or loose
*/
int test(int picked, int guessed);
/* This function tests the players guess.
Given: the pick and the guess
Return: win, high or low
*/
/* File: sequence.c
Programmer: Tep Dobry
login: tep
Date:
*/
/* This module is responsible for sequencing the steps of play
for one round of the game.
*/
#include <stdio.h>
#include "play.h"
int play(int pick)
/* This function plays one round of the game.
Given: the random number picked
Return: win or loose
*/
{ int guess; /* the player's guess */
int num_guesses; /* the number of guesses remaining */
int result; /* the result of testing the guess */
/* Initialize the number of guesses */
num_guesses = MAX_GUESSES;
/* While there are guesses remaining */
while( num_guesses > 0 )
{
/* ask the player to guess */
printf("\tWhat is your guess? ");
scanf("%d",&guess);
/* if the guess is correct */
result = test(pick,guess);
if( result == WIN)
/* return a winner! */
{ printf("YES!!\a you guessed it!\n");
return WIN;
}
/* otherwise give the hint */
if( result == HI )
printf("Too High!!!\n");
else
printf("Too Low!!!\n");
/* update the number of guesses */
num_guesses = num_guesses -1;
}
/* Return a loser :-( */
printf("Sorry :-(, you're out of guesses\n");
printf("The number was %d\n", pick);
return LOSE;
}
/* File: test.c
Programmer: Tep Dobry
login: tep
Date:
*/
/* This module is responsible for testing the user guess and
giving the appropriate feedback.
*/
#include "play.h"
int test(int picked, int guessed)
/* This function tests the players guess.
Given: the pick and the guess
Return: win, high or low
*/
{
/* If the guess matches the pick */
if( guessed == picked )
/* return win! */
return WIN;
/* Otherwise if the guess is too low */
if( guessed < picked )
/* return low */
return LOW;
/* Otherwise if the guess is too high */
/* return high */
return HI;
}
/* File: guess.c
Programmer: Tep Dobry
login: tep
Date:
Mod :
*/
/* This module is responsible for overseeing the entire program; invoking
the other modules as appropriate. It is the module allowing the
user to play the game repeatedly.
The game has been improved to keep track of wins and losses.
*/
#include <stdio.h>
#include "guess.h"
#include "play.h"
#include "tfdef.h"
#define DEBUG
main()
{ int keep_playing = TRUE; /* do we keep playing? initially true. */
int hidden_number; /* the picked random number */
int wins, losses; /* count of wins and loses */
/* Introduce the game */
printf("\n\n\t\tWelcome to the Guessing Game\n");
/* Initialize counts */
wins = losses = 0;
/* While the player wants to keep playing */
while( keep_playing )
{ /* pick a random number */
hidden_number = PICK(START,STOP);
/* introduce the play */
printf("\nI'm thinking of a number between %d "
"and %d\nCan you guess it?\n\n",START,STOP);
#ifdef DEBUG
printf("debug:main:the pick is %d\n",hidden_number);
#endif
/* play the game */
if( play(hidden_number) == WIN) wins = wins + 1;
else losses = losses + 1;
/* ask the player if they want to continue */
printf("\nDo you want to play again?\n");
printf("(1 for yes, 0 for no): ");
scanf("%d", &keep_playing);
}
/* say goodbye */
printf("\nYou won %d times out of %d\n",wins, wins+losses);
printf("\nThanks for the game.... bye...\n");
}
# This is the default target line to make the guessing game
guess: guess.o sequence.o test.o
cc guess.o sequence.o test.o -o guess
# These are the dependency lines for the object modules.
# They show the .h dependencies used in each .c file.
guess.o: guess.h play.h tfdef.h
sequence.o: play.h
test.o: play.h
# These are utility target lines for cleaning up the directory
clean:
rm -f *.o
real_clean: clean
rm -f guess a.out core*
CFLAGS = -Aa
DEST = .
EXTHDRS =
HDRS = guess.h \
play.h \
tfdef.h
INSTALL = /etc/install
LD = cc
LDFLAGS =
LIBS =
MAKEFILE = Makefile
OBJS = guess.o \
sequence.o \
test.o
PRINT = pr
PROGRAM = guess
SHELL = /bin/sh
SRCS = guess.c \
sequence.c \
test.c
SYSHDRS = /usr/include/.unsupp/sys/_errno.h \
/usr/include/errno.h \
/usr/include/pwd.h \
/usr/include/stdio.h \
/usr/include/stdlib.h \
/usr/include/sys/errno.h \
/usr/include/sys/stdsyms.h
all: $(PROGRAM)
$(PROGRAM): $(OBJS) $(LIBS)
@echo "Linking $(PROGRAM) ..."
@$(LD) $(LDFLAGS) $(OBJS) $(LIBS) -o $(PROGRAM)
@echo "done"
clean:; @rm -f $(OBJS) core
clobber:; @rm -f $(OBJS) $(PROGRAM) core tags
depend:; @mkmf -f $(MAKEFILE) ROOT=$(ROOT)
echo:; @echo $(HDRS) $(SRCS)
index:; @ctags -wx $(HDRS) $(SRCS)
install: $(PROGRAM)
@echo Installing $(PROGRAM) in $(DEST)
@-strip $(PROGRAM)
@if [ $(DEST) != . ]; then \
(rm -f $(DEST)/$(PROGRAM); $(INSTALL) -f $(DEST) $(PROGRAM)); fi
print:; @$(PRINT) $(HDRS) $(SRCS)
tags: $(HDRS) $(SRCS); @ctags $(HDRS) $(SRCS)
update: $(DEST)/$(PROGRAM)
$(DEST)/$(PROGRAM): $(SRCS) $(LIBS) $(HDRS) $(EXTHDRS)
@$(MAKE) -f $(MAKEFILE) ROOT=$(ROOT) DEST=$(DEST) install
###
guess.o: /usr/include/stdio.h /usr/include/sys/stdsyms.h guess.h \
/usr/include/stdlib.h /usr/include/pwd.h /usr/include/errno.h \
/usr/include/sys/errno.h /usr/include/.unsupp/sys/_errno.h play.h \
tfdef.h
sequence.o: /usr/include/stdio.h /usr/include/sys/stdsyms.h play.h
test.o: play.h
/* File: guess.c
Programmer: Tep Dobry
login: tep
Date:
Mod :
*/
/* This module is responsible for overseeing the entire program; invoking
the other modules as appropriate. It is the module allowing the
user to play the game repeatedly.
The game has been improved to keep track of wins and losses.
And improved again to take y/n answers.
*/
#include <stdio.h>
#include "guess.h"
#include "play.h"
#include "tfdef.h"
#define FLUSH while(getchar() != '\n');
#define DEBUG
main()
{ char keep_playing = 'y'; /* do we keep playing? initially true. */
int hidden_number; /* the picked random number */
int wins, losses; /* count of wins and loses */
/* Introduce the game */
printf("\n\n\t\tWelcome to the Guessing Game\n");
/* Initialize counts */
wins = losses = 0;
/* While the player wants to keep playing */
while( keep_playing != 'n' )
{ /* pick a random number */
hidden_number = PICK(START,STOP);
/* introduce the play */
printf("\nI'm thinking of a number between %d "
"and %d\nCan you guess it?\n\n",START,STOP);
#ifdef DEBUG
printf("debug:main:the pick is %d\n",hidden_number);
#endif
/* play the game */
if( play(hidden_number) == WIN) wins = wins + 1;
else losses = losses + 1;
/* ask the player if they want to continue */
printf("\nDo you want to play again? (y/n): ");
scanf("%c", &keep_playing);
}
/* say goodbye */
printf("\nYou won %d times out of %d\n",wins, wins+losses);
printf("\nThanks for the game.... bye...\n");
}