Go Back   Forums > Community Chatterbox > Tech Corner > Programming
Memberlist Forum Rules Search Today's Posts Mark Forums Read
Search Forums:
Click here to use Advanced Search

Reply
 
Thread Tools Display Modes
Old 16-06-2006, 05:47 AM   #21
Abi79
Home Sweet Abandonia

 
Join Date: May 2005
Location: Oradea, Romania
Posts: 829
Send a message via Yahoo to Abi79
Default

<div class='quotetop'>QUOTE(guesst @ Jun 14 2006, 11:52 PM) [snapback]236864[/snapback]</div>
Quote:
And (2) did you even play the maze game yet? Tell me what you thought of it. [/b]
I've played it yesterday. After saying OH MY GOD HOW COOOOOOOOOOOOOOOOOOOOOOOOOOOL..., I've tried to actually get to the exit, but I couldn't find it on the easy level. Anyway, is there a way to exit the game without pressing CTRL+Break in C++ or reaching the actual exit?

This games will be very useful tomorrow at school, and will certainly make the 10 year olds also studying C++ with me have some fun...and ask the teachers how they were made again and again and again. Poor teachers.

Oh, and are the games made in C, or C++? In C++, I had to add an extra library for getche for the game to work, and I also got the warning that int main(void) should return a value. Couldn't you have used void main instead of int main(void)?

Mugwump is what I would like to be posted next. (meanwhile I have to try the other games)
Abi79 is offline                         Send a private message to Abi79
Reply With Quote
Old 16-06-2006, 10:30 AM   #22
#BlakhOle#
Game Wizzard

 
Join Date: Nov 2005
Location: ,
Posts: 201
Default

Hi im kinda new to this, but you recommended this thread to me in one of the game threads, i think othello, so im about as nooby as a noob can possibly noob, so bear with me here. I reckon this would be the most amazing thing to be able to do, and to able to do it good would be awesome. But everyone has to start off somewhere, dont they? Anyways,
i have a few questions to ask you.

First of all, in the tutorial they gave me in with the C++ program guides me through the "Hello World" program and ive already typed it in and compiled it and everything, but every time i try to run it I see the command prompty kinda box pop up for a fraction of a second then just disappear. And it does this everytime. So i went a bit further to find out about loops and put a neverending loop in, and it worked perfectly, the box came up and a neverending greeting to the world flashes down the screen. But i cant work out how to do the simple one line one. Any hints?

Secondly, ive been through a couple of the programs you have posted here but every single one is rejected by the compiler, and of course, having not even been able to configure the Hello World program correctly, I cant work out what is wrong with them.

On a smaller note could you try to explain the integers and floats a bit more coz i cant make head nor tail of them. Thanx
#BlakhOle# is offline                         Send a private message to #BlakhOle#
Reply With Quote
Old 16-06-2006, 11:46 AM   #23
Abi79
Home Sweet Abandonia

 
Join Date: May 2005
Location: Oradea, Romania
Posts: 829
Send a message via Yahoo to Abi79
Default

<div class='quotetop'>QUOTE(#BlakhOle# @ Jun 16 2006, 01:30 PM) [snapback]237123[/snapback]</div>
Quote:
First of all, in the tutorial they gave me in with the C++ program guides me through the "Hello World" program and ive already typed it in and compiled it and everything, but every time i try to run it I see the command prompty kinda box pop up for a fraction of a second then just disappear. And it does this everytime. So i went a bit further to find out about loops and put a neverending loop in, and it worked perfectly, the box came up and a neverending greeting to the world flashes down the screen. But i cant work out how to do the simple one line one. Any hints?[/b]
Here's something for you:
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello BlakhOle! Do you like C++? I bet you don't :P ");
getch();
}
See 'getch();'? Is it included in the program from the tutorial? This instruction makes everything on the screen stay there until you press a key. If it isn't included, then the results still appear, but you need to press Alt+F8 (if I remember well) to see them.

<div class='quotetop'>QUOTE(#BlakhOle# @ Jun 16 2006, 01:30 PM) [snapback]237123[/snapback]</div>
Quote:
Secondly, ive been through a couple of the programs you have posted here but every single one is rejected by the compiler, and of course, having not even been able to configure the Hello World program correctly, I cant work out what is wrong with them.[/b]
What error does the compiler give you?

<div class='quotetop'>QUOTE(#BlakhOle# @ Jun 16 2006, 01:30 PM) [snapback]237123[/snapback]</div>
Quote:
On a smaller note could you try to explain the integers and floats a bit more coz i cant make head nor tail of them. Thanx :) [/b]
I'll let that for Guesst to do. When I explain something, people get even more confused. ^_^
Abi79 is offline                         Send a private message to Abi79
Reply With Quote
Old 16-06-2006, 04:46 PM   #24
guesst
Abandonia Homie
 
guesst's Avatar

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

So much to reply to! Warms my heart.

All of the programs posted here are in ANSI standard C, that is to say, not C++. If you want to eliminate some of the problems your complier is having with them, Abi79, save the files with a .C extention (not .cpp which is probably the default) and the compiler will (generally) compile it slightly differently.

@#BlakhOle#
I assume you're using DevCPP that I recomended at the start. Yes, "hello world" does come and go quickly. The problem is that when the program runs it opens a window, does the output, and closes the window before you get a chance to see what happened. There are two ways around this. One is to add the line
Code:
**getch();getch();
just inside the last bracket. This way the last thing the program will do is wait for you to input something before exiting. Try that first.

PS, Albi79, your hello world is not technically C++ code. It's a kind of bastardized hybred. A proper C++ program would use streams like this:
Code:
#include <iostream>
** int main(int argc, char **argv)
** {
**** char c;
****** 
**** std::cout << "Hello BlakhOle! Do you like C++? I bet you don't :P" << std::endl;
**** std::cin.get();
**** return (0);
** }
Which is why I don't write C++ code much yet. To do it properly is learning a whole new language. However, the above program you would want to tack on a .CPP extension or the compiler will likely bugger it up.

Back to #BlakhOle#'s question, as for getting the programs here to work the first thing to do when you compile them if they don't run is look at the frame that pops up at the bottom. It will give you any warnings or errors that may be impeeding the execution of the program. If you can't make heads or tails of them tell us which program you're running and post the error here and we'll see if we can't help out.

Ah, floats and ints. Well, there is a simple answer and a more complicated answer to that. Most books will take the complicated answer, explaining the inner workings of your computer and whatnot. I'll take the simple one.

char, int, and long are all the same thing in that they can be used to store only whole numbers, that is to say no decimals, no fractions. 1, 2, 3, 100, 121, etc. The difference in them is that a char can only hold number less than 256 if unsigned or between -127 and 128 if signed. It's very limited but works for certian things very well. Ints can hold bigger numbers than a char. Use them for your regular counting. Longs are for when you may need a really big number. If you try to put a number that's too big into a variable that can't hold it all sorts of wonky thinks can happen.

float and double are for holding numbers that may have decimal values. 1.1, 5.6, 10057.3, 3.14159, etc. Decimal points are a tricky thing for a computer to handle, so it's recomended that if you can do the job with whole numbers not to use floats as you're only going unnecessarly bog down your computer.

Well folks, I have an eye appointment, so that's all the time I have for now. Maybe I'll do mugwump later, maybe not until tomorrow. If you have any more questions, you know where to find me.

Ta
guesst is offline                         Send a private message to guesst
Reply With Quote
Old 16-06-2006, 08:22 PM   #25
plix
Game freak

 
Join Date: Oct 2005
Location: ,
Posts: 113
Default

<div class='quotetop'>QUOTE(Abi79 @ Jun 16 2006, 01:47 AM) [snapback]237089[/snapback]</div>
Quote:
I also got the warning that int main(void) should return a value. Couldn't you have used void main instead of int main(void)?[/b]
It's bad form. The return value is also known as the exit status and important for knowing how the program exited. This doesn't generally make a big difference on Windows, but in a Unix-like environment it's important for piping (command chaining), diagnostics, etc.
plix is offline                         Send a private message to plix
Reply With Quote
Old 17-06-2006, 03:36 AM   #26
#BlakhOle#
Game Wizzard

 
Join Date: Nov 2005
Location: ,
Posts: 201
Default

Ok lets see whats worked here... first of all, C++ doesnt seem to like the getch(); command much, the compiler log comes up with
Quote:
`getch' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in)
[/b]
It seems to come up with this sort of thing alot in the programs youve posted before. It does it whether I save it as a .cpp file or just .c file.
The program you fixed from abi also worked pretty well, dunno how though. :P
Apart from that all seems well, except from the fact i still dont know what everything means. Back to the HELP file i guess...

PS @abi: Actually i dont hate C++, but i might like it a bit more if it was a bit more self explanatory :bleh:
#BlakhOle# is offline                         Send a private message to #BlakhOle#
Reply With Quote
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
Old 18-06-2006, 03:14 PM   #28
Abi79
Home Sweet Abandonia

 
Join Date: May 2005
Location: Oradea, Romania
Posts: 829
Send a message via Yahoo to Abi79
Default

<div class='quotetop'>QUOTE(#BlakhOle# @ Jun 17 2006, 06:36 AM) [snapback]237264[/snapback]</div>
Quote:
PS @abi: Actually i dont hate C++, but i might like it a bit more if it was a bit more self explanatory[/b]
Well, assuming that you have Borland C++ (the DOS one), just like I do, you could try looking in the help, but I don't guarantee that you aren't going to look like :tai: after that. :bleh:

As for guesst, Hammurabi, Hammurabi!
Abi79 is offline                         Send a private message to Abi79
Reply With Quote
Old 19-06-2006, 03:44 PM   #29
guesst
Abandonia Homie
 
guesst's Avatar

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

Hammurabi
Not feeling like typing much today. But your wish is my command. Once again this is a BASIC program that got the C treatment. Follow the link if you want to know more.

.:EDIT:. I'm feeling like typing more now. While I realize no one is going to take the time to read this, I'm posting it any ways.

Hammurabi borrows it's name from the Babylonia ruler who is famous for setting up a standard of laws and punishments. By "setting up a standard" I mean he literally built an 8 foot tall black stone monument, clearly meant to be placed in the center of town. There was no excuse for not knowing the laws in Hammurabi's reign, because they were before you day and night. In this way it was believed that Hammurabi kept order in his town.

Now it's your chance. See if you can expand your land and grow your population in a 20 year reign as king in this exciting simulation game.
Code:
/* Hammurabi 
 * by Joseph Larson
 * based on a BASIC program written by David Ahl
 * as found in 'BASIC Computer Games' edited by David H Ahl (c) 1978
 * inspired by a Focal program from an unknown author
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SHORT_LAND_MSG printf ("Hammurabi, think again."\
**" You own only %d acres. Now then,\n", acres)
#define SHORT_GRAIN_MSG printf ("Hammurabi, think again."\
**" You have only %d bushels of grain. Now then,\n", grain)

int pop, acres, grain, starved, born, yield, rats, food, year;

void resign (void) {
**printf ("Hammurabi, I can not do what you wish."
****" Get yourself another steward!\n");
**exit (0);
}

void endgame (int rank) {
**switch (rank) {
****case 0:
****printf ("Due to this extreme mismanagement you have not only been"
******" impeached\nand thrown out of office but you have been also declared"
******" a national fink!\n");
****break;
****case 1:
****printf ("You heavy handed performance smacks of Nero and Ivan IV."
******" The people\n(remaining) find you an unpleasant ruler and frankly"
******" hate your guts!!\n");
****break;
****case 2:
****printf ("Your performance could have been somewhat better but really"
******" wasn't\ntoo bad at all. %d people dearly would like to see you"
******" assassinated\nbut we all have our trivial problems.\n"
******, pop * (rand () % 80) / 100);
****break;
****default:
****printf ("A fantastic performance!!! Charlemagne, Disraeli, and Jefferson\n"
******"combined could not have done better!!\n");
**}
}

void report (void) {
**printf ("\n\nHammurabi, I beg to report to you;\n\n"
****"In year %d, %d people starved, %d came to the city.\n"
****, year, starved, born);
**if (rand() < (RAND_MAX / 100 * 15) && year > 1) {
****printf ("A horrible plague struck! Half the people died.\n");
****pop /= 2;
**}
**printf ("Population is now %d.\n"
****"The city now owns %d acres.\n"
****"You harvested %d bushels per acre.\n"
****"Rats ate %d bushels.\n"
****"You now have %d bushels in store.\n\n",
****pop, acres, yield, rats, grain);
}

void buysell (void) {
**int input, price;

**price = rand() % 10 + 17;
**printf ("Land is trading at %d bushels per acre.\n", price);
**printf ("How many acres do you wish to buy? ");
**scanf ("%d", &input);
**while (price * input > grain) {
******SHORT_GRAIN_MSG;
******printf ("how many do you wish to buy? ");
******scanf ("%d", &input);
**}
**if (input < 0) resign();
**if (input) {
****acres += input;
****grain -= price * input;
**} else {
****printf ("How man acres do you wish to sell? ");
****scanf ("%d", &input);
****while (input > acres) {
******SHORT_LAND_MSG;
******printf ("how many do you wish to buy? ");
******scanf ("%d", &input);
****}
****if (input < 0) resign ();
****acres -= input;
****grain += input * price;
**}
}

void feed (void) {
**do {
****printf ("How many bushels do you wish to feed your people? ");
****scanf ("%d", &food);
****if (food < 0) resign ();
****if (food > grain) SHORT_GRAIN_MSG;
**} while (food > grain);
}

void farm (void) {
**int input, v;

**do {
****v = 1;
****printf ("How many acres do you wish to plant with seed? ");
****scanf ("%d", &input);
****if (input < 0) resign ();
****else if (input > acres) {SHORT_LAND_MSG; v = 0;}
****else if (input / 2 > grain) {SHORT_GRAIN_MSG; v = 0;}
****else if (input > 10 * pop) {
******printf ("But you only have %d people to tend the fields! Now then,\n"
******, pop);
******v = 0;
****}
**} while (!v);
**grain -= input / 2;
**yield = rand () % 5 + 1;
**rats = rand () % 5 + 1;
**rats = (rats % 2) ? grain / rats : 0;
**grain += input * yield - rats;
}

int main (void) {
**int cstarved = 0;
**int cps = 0;

**srand (time (NULL));
**printf ("Hammurabi\n\n"
**"Try your hand at governing ancient summeria for a ten-year term of office.");
**pop = 100;
**grain = 2800;
**yield = 3;
**rats = 200;
**acres = 1000;
**born = 5;
**for (year = 1; year <= 10 && starved < pop * 45 / 100; year ++){
****report ();
****buysell ();
****feed ();
****farm ();
****born = ((rand () % 5 + 1) * (20 * acres + grain) / pop / 100) + 1;
****starved = (pop < food / 20) ? 0 : pop - food / 20;
****pop += born - starved;
****cstarved += starved;
****cps += starved * 100 / pop;
**}
**if (starved >= pop * 45 / 100) {
****printf ("\nYou starved %d people in one year!!!\n", starved);
****endgame (0);
**} else {
****year--;
****report ();
****cps /= 10;
****printf ("In your 10-year term of office %d%% of the people died per year\n"
******"on the average, IE a total of %d people died!\n", cps, cstarved);
****acres /= pop;
****printf ("You started with 10 acres per person and ended with %d acres per\n"
******"person.\n\n", acres);
****endgame ((cps<34 && acres>6)+(cps<11 && acres>8)+(cps<4 && acres>9));
**}
**exit (0);
}
The game is made tricky by having this out of logical order forcing you to calculate and plan ahead. This of course could be remidied by changing the order of the funciton calls in the main for loop in main. Ideally I should encapulate this so as to make them easier to find, but meh.

Keep 'em comming
  • 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)
  • 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
Old 19-06-2006, 05:38 PM   #30
BeefontheBone
10 GOSUB Abandonia
20 GOTO 10
 
BeefontheBone's Avatar

 
Join Date: Feb 2005
Location: Leeds, England
Posts: 2,166
Default

Oooh, Awari! My dad has a beautiful Awari board he got when he was in Africa - 's a great game.
BeefontheBone is offline                         Send a private message to BeefontheBone
Reply With Quote
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Happy Birthday Guesst Japo Birthdays & Celebrations 8 06-07-2007 09:50 AM
Happy Birthday, Guesst Shrek Blah, blah, blah... 10 08-07-2006 09:31 AM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump
 


The current time is 03:52 PM (GMT)

 
Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.