All pastes #2106275 Raw Edit

Miscellany

public c v1 · immutable
#2106275 ·published 2012-01-26 05:51 UTC
rendered paste body
// cgame.c// ==========================#include <stdlib.h>#include "cgame.h"int secret;   // secret number void startgame(int n) {  secret = (rand() % n) + 1;}char guess(int g) {  if (g == secret) {    return 'r';  } else if (g < secret) {    return 'l';  } else {    return 'h';  }}// cgame.h// ==========================// Start a new game of size n:void startgame(int n);// Guess whether or not g is the secret// returns 'r' (guessed right)//         'h' (too high), 'l' (too low)char guess(int g);extern int secret;//cgameui.c#include <stdio.h>#include "cgame.h"#include "playcgame.h"int main(void){   int gamesize;   printf("What size game?\n");   while (1 == scanf("%d",&gamesize)) {      printf("Game size %d finished in %d moves\n",gamesize,playgame(gamesize));      printf("New game size? Control-D (EOF) to quit\n");   }   return 0;}// playgame.c// Author:  Kevin Okal// Date:    01/25/12// Purpose: Plays a simple guessing game.#include "playcgame.h"#include "cgame.h"int guess_count, initial_size, half_guess;int autoguess(int current_guess){      guess_count = guess_count + 1;   half_guess = (initial_size / (2 * guess_count));      if (guess(current_guess) == 'r')    {          return guess_count;   }        else if (guess(current_guess) == 'l')     {      return autoguess((current_guess + half_guess));   }        else       return autoguess((current_guess - half_guess));}int playgame(int n){   initial_size = n;   guess_count = 0;   startgame(n);   return autoguess(n);}