View Single Post
Old 17-06-2006, 04:10 AM   #27
guesst
Abandonia Homie
 
guesst's Avatar

 
Join Date: May 2005
Location: Aurora, United States
Posts: 606
Default

Got time for some quick responces. You never confirmed which compiler you're using. It would be helpful in the future to know what program you're working form.

As for getch() being no-go, okay, try getchar() or my personal favorate getche(). Getch is a non-standard function so it behaves differently, if it's there at all, between different compliers. Getchar is the standard version that all of my programs are written to be compatible with. However getche, unline getchar, doesn't wait until you hit the enter key before moving on. Getche will just take one character and run with it.

void main () is possible on some compilers, but is extremely bad form like plix said. Always have int main() and to be completely compliant alway use return(0) at the end of the program to indicate a successful run. (Potentially you could exit with a non-zero value to pass information like error messages to external programs, but that's mostly a Unix thing.)

So, to sum up:
  • getchar should always work.
  • guesst prefers getche.
  • int main() in every program and
  • return(0) for extra credit.
And while I'm here, and since I may not have time tomorrow...

Mugwump

This is a good game to show the difference between ints and floats that we were just talking about above. In 101 BASIC games and it's successor there were numerious hide and seek games played on a grid where by various means the player was given hints as where to find whatever ridiculous creature they were hunting for. Of all of them Mugwump was perhaps the most intersting of them. Instead of directional hints this game gives the distance to your targets. In an improvement over the original this one sorts the resultant distances so you don't know which ones are for which any more. Also, there's a timing loop that simulates a distance reading machine of some sorts with the output. Check it out.
Code:
/* Mugwump Hunt 
 * by Joseph Larson 2005
 * inspired by a BASIC game by an unknown author
 * as found in 'BASIC Computer Games' edited by David H. Ahl (c) 1978
 */
 
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#define dist(a,b) sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y))
#define NUM 5
#define SIZE 20

typedef struct {
**int x, y;
} COORD;

int greater (double *a, double *b) {return (*a > *b);}

void intro (void) {
**printf ("Mugwump Hunt\n-----------\n"
**"You are hunting Mugwumps using a state of the art radar system. There are\n"
**"%d mugwumps hidden hiding in a %d,%d grid. After every guess you will be\n"
**"told how close the remaining Mugwumps are to your guess.\n"
**"Input the your guess by typing the x and y location seperated by a comma\n"
**"(IE \"5,5\" or \"3,10\")\n"
**"(Mugwumps only hide whole number locations.)\n\n",
**NUM, SIZE, SIZE);
}

void hunt (void) {
**int c, turns, left, dsize;
**COORD h[NUM], input;
**double d[NUM];
**time_t st, cur, dit;

**for (c = 0; c < NUM; c++) {
****h[c].x = rand() % SIZE + 1;
****h[c].y = rand() % SIZE + 1;
**}
**printf ("The Mugwumps are hiding.");
**turns = 0;
**left = NUM;
**while (left > 0) {
****turns ++;
****printf ("\n%d Mugwumps left.\n", left);
** input.x = input.y = -1;
****do {
******if (input.x != -1 && input.y != -1)
********printf ("X and Y values must be between 1 and %d", SIZE);
********printf ("\nWhere do you want to search? X,Y : ");
********scanf ("%d %*c %d", &input.x, &input.y);
****} while (input.x < 1 || input.x > SIZE || input.y < 1 || input.y > SIZE);
****dsize = 0;
****printf ("Mugwump Radar Searching from %d,%d...", input.x, input.y);
****for (c = 0; c < NUM; c++)
******if (h[c].x == input.x && h[c].y == input.y) {
********printf ("\nMugwump found!");
********h[c].x = h[c].y = -1;
********left--;
******} else if (h[c].x > 0) d[dsize++] = dist(h[c], input);
****if (dsize) {
******qsort (d, dsize, sizeof(double), greater);
******time (&st); dit = st;
******for (c = 0; c < dsize; c++) {
********do {
**********time (&cur); 
**********if (difftime (cur, dit) > 0.2) {putchar ('.'); time (&dit);}
********} while (difftime (cur, st) < d[c] / 2);
********printf ("\nMugwump at distance %.2f", d[c]);
******}
****}
**}
**printf ("Congratulation! All Mugwumps found in %d turns!", turns);
}

int again (void) {
**char input;

**printf ("\nDo you want to play again? (y/n) ");
**while (!isalpha (input = getchar()));
**if (tolower (input) != 'n') return 1;
**return 0;
}

int main (void) {
**intro();
**srand (time (NULL));
**do {hunt();} while (again ());
**exit (0);
}
Now, if you're plaing the game with grid paper and a compass you should have a pretty good idea after three moves where most, if not all of the mugwumps are hiding, and each one you find will narrow it down further. However, if you're not playing with grid paper you may find your hunt a bit more challenging.

If you change the constants NUM and SIZE at the start of the program you can easily adjust the difficulty of the game. Enjoy!

And the winner for best supporting gaffer is...
  • Battleship (like the board game vs the computer)
  • Cel Life (multi-player version of John Conway's game of Life)
  • Pickup Piles (1000 games in one, set the rules and play)
  • Flash Cards (with pretty output, practice your math)
  • Hammurabi (rule a country, build your kingdom)
  • Black Box (find molecules in the inky depths)
  • Hangman (guess the word before you dangle)
  • Rotate (like those sliding block puzzles but that you rotate pieces)
  • Acey Deucy (a card game of highs, lows, and middles)
  • Reverse (order a list of number by turning them around)
  • Stars/Trap/Letter Guess (3 games in 1 update, variations on a theme)
guesst is offline                         Send a private message to guesst
Reply With Quote