View Single Post
Old 05-06-2006, 08:14 PM   #4
guesst
Abandonia Homie
 
guesst's Avatar

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

So, which one to continue with? I'll start with a small one and move on to a somewhat bigger one, then let you all choose.

Jumping Balls (please hold back the grade school snickering)

What inspired this little programming spree was when I found a couple of old books of BASIC programs that I would read in High School. Of course, by High School BASIC was a thing of the past, so all I could do was read the programs an imagine how cool it'd be to play them.

So when I found the entire text of both books online years later I decided that I would like to finally see these programs run. Only, instead of whipping out gwBASIC (or whatever) I'd convert the programs in it to C!

One of these programs I found such a suscinct way of expressing in C that I decided to see if it would fit in a sig. It did, and in 3 lines no less:
Code:
main(){char b[11]="\toooo.OOOO";int e=5,n,t=0;puts("\t123456789");puts(b);for(;n
&&strcmp("\tOOOO.oooo",b);puts(b)){n=getche();if(n-='0'){if(n>0&&n<=9&&(abs(n-e)
==2||abs(n-e)==1)){b[e]=b[n];b[n]='.';e=n;t++;}}}if(n)printf("\n%d moves\n",t);}
This is a simple game of solitare. You start with 4 little 'o's on the left and 4 big 'O's on the right with an empty sopt (.) in the middle. The game is over when the order is reversed. To move you just choose an piece that can either move once into the empty piece, or jump over another piece into the empty space.

In a a perfect game you can win in 24 moves. How well can you do?

Below is the full, more readable, and longer more compelte version
Code:
/* Jumping Balls
 * By Joseph Larson
 * Inspired by a BASIC game of the same name by Anthony Rizzolo
 * as found in 'More BASIC Computer Games' edited by David H. Ahl (c) 1979
 */

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

int main (void) {
**char yn, b[10], *start = "XXXX.OOOO", *end = "OOOO.XXXX";
**int empty, input, t;
**
**puts ("Jumping Balls\n------- -----\n"
**"This is a game of solitare. It is played with 8 balls in 9 holes. The X's\n"
**"and O's are the balls in this game and the period ('.') is the empty hole.\n"
**"The object is to get all the X's and O's to switch sides. You can move by\n"
**"either moving a ball into the empty space or jumping one ball over another\n"
**"into the empty space.\n\n"
**"On your turn input the number from the left of the space of the ball you\n"
**"want to move or jump into the empty space.\n\n"
**"If you want to quit input '0' as your move.\n\n"
**"Press ENTER to start...");
**getchar ();
**do {
****empty = 4; t = 0; strcpy (b, start);
****puts ("\t123456789");
****printf ("\t%s\t? ", b); scanf ("%d", &input);
****while (input && strcmp (b, end)) {
******if (input--) {
********if (input >= 0 && input < 9 && 
**********(abs (input - empty) == 2 || abs (input - empty) == 1)) {
**********b[empty] = b[input]; b[input] = '.'; empty = input; t ++;
********} else puts ("Invalad move.");
******}
******printf ("\t%s\t? ", b); if (strcmp (b, end)) scanf ("%d", &input);
****}
****if (!strcmp (b, end))
******printf ("\nCongratulations! You solved it in %d moves!\n\n"
******"Do you want to try for a better score? (y/n) ", t);
****else printf ("Do you want to start again ? (y/n) ");
****do yn = getchar (); while (!isalpha (yn));
**} while (tolower (yn) != 'n');
**puts ("Thanks for playing!");
**exit (0);
}
guesst is offline                         Send a private message to guesst
Reply With Quote