View Single Post
Old 20-07-2006, 01:02 AM   #8
guesst
Abandonia Homie
 
guesst's Avatar

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

Pickup Piles

Before you is a pile and your opponent. You can only take a certian number of pieces, and you have to take at least a certian number. Maybe you want to be the one to take the last piece. Maybe you want to take everything but the last piece. So many things to keep track of, do you have the mind to win?

The frustrating thing about this game is the outcome is almost always predictable from the first move, if at least one player knows what they are doing. Thus the only way to actually win is to stack the deck in your favor.

Where this game gets fun is the endless variations it provides. Playing with the settings and getting good at this game is what it's about. When you get the hang of the game and figure out what the trick is take your skill to family and friends, maybe even play this it in the bar and make <strike>a few dollars</strike> some new friends.

Seeing how the computer plays a perfect game can teach you the trick. It is of course mathematical in nature, so bone up on your division skills.

The computer plays a perfect game but every so often, depending on the difficulty level, it will just make a shot in the dark random move that may or may not be the right move. This can also be part of the fun, catching the computer's mistakes.
Code:
/* Pickup Pile */
/* by Joseph Larson */
/* Inspired by a BASIC game Batnum by John Kemeny */
/* as found in 'BASIC Computer Games' edited by David H. Ahl © 1978 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#define random(x) ((int) rand() / ((float)(RAND_MAX + 1) / x))

int num, max, min;
int takelast, comfirst;
int skill;

void intro (void) {
**printf ("\nPick Up Pile\n------\n"
**"Pick Up Pile is a game with many variations on the same idea.\n"
**"You and the computer each take turns picking up items from an\n"
**"imaginary pile of objects. Depending on the type of game you choose\n"
**"to play you will have different restrictions on how much you can\n"
**"pick up at a time, and whether you want to be the one to pick up the\n"
**"last piece or not.\n");
}

void random_setup (void) {
******num = random (25) + 10; max = random (5) + 3;
******do min = random (3) + 1; while ( max < min );
******takelast = random (2); comfirst = random (2);
******printf ("\n%d in pile, %s first, winner %s last piece.\n", num,
********(comfirst) ? "computer goes" : "you go",
********(takelast) ? "takes" : "leaves" );
******printf ("Take at least %d and at most %d per turn.\n", min, max);
}

void custom_setup(void) {
**int input;
**char yesno;

**printf ("\nFor any option input 0 to have the computer randomly select a value.");
**printf ("\nChoose the size of the pile (at least 3) ");
**do scanf("%d", &num);
**while (num < 3 && num != 0);
**if (num == 0) {
******num = random (30) + 10;
******printf("%d in pile.", num);
**}****
**printf ("\nWhat is the least that can be taken in a turn? ");
**do scanf("%d", &min);
**while (min < 0);
**if (min == 0) {
******min = random (5) + 1;
******printf("\n%d minimum taken per turn.", min);
**}****
**printf ("\nWhat is the most that can be taken in a turn (at least %d)? ", min + 1);
**do scanf("%d", &max);
**while (max < min && max != 0);
**if (max == 0) {
******max = random (5) + min + 2;
******printf("%d maximum taken per turn.", max);
**}****
**printf ("\nShould the winner take the last peace? (y/n/0)");
**do scanf("%c", &yesno);
**while (yesno != 'y' && yesno != 'Y' && yesno != 'n' && yesno != 'N' && yesno != '0');
**switch (yesno) {
****case 'y': case 'Y': takelast = 1; break;
****case 'n': case 'N': takelast = 0; break;
****case '0':
******takelast = random (2);
******printf("Winner %s last peace.", (takelast) ? "takes" : "leaves" );
**}****
**printf ("\nDo you want to go first? (y/n/0)");
**do scanf("%c", &yesno);
**while (yesno != 'y' && yesno != 'Y' && yesno != 'n' && yesno != 'N' && yesno != '0');
**switch (yesno) {
****case 'y': case'Y': comfirst = 0; break;
****case 'n': case 'N': comfirst = 1; break;
****case '0':**** 
******comfirst = random (2);
******printf("%s first.", (comfirst) ? "Computer goes" : "You go" );
**}** 
}

int humanmove(void) {
**int input;

**printf ("\nHow many do you want to take (%d - %d) ", 
****(min > num) ? num : min, (max > num) ? num : max);
**input = 0;
**do {
****if (input) printf ("\nInvalid move. Try again. ");
****scanf ("%d", &input);
****if (num <= min) input = (input == num) ? input : -1;
******else if (input < min || input > max || input > num) input = -1;
**}
**while (input < 0);
**num -= input;
**printf ("You leave %d", num);
**return ((num) ? 0 : (takelast)? 1 : 2);
}

int compmove (void) {
**int c;
**int move;
**
**c = min + max; move = num - !takelast;
**move = move % c;
**if (move < min || move > max || skill < random (5))
****move = random((max - min + 1)) + min;
**if (move > num) move = num;
**num -= move;
**printf ("\nComputer takes %d and leaves %d", move, num);
**return ((num) ? 0 : (takelast)? 2 : 1);
}

void playgame (void) {
**int input;
**int winner;

**printf ("\n(1) 23 Matches (23 in pile, take at most 3, last piece looses)\n");
**printf ("(2) Random\n");
**printf ("(3) Custom\n");
**printf ("\nChoose a game type: ");
**input = 0;
**do {
****if (input) printf ("\nChoose 1, 2, or 3 please. ");
****scanf ("%d", &input);
**} while (input < 1 || input > 3);
**switch (input) {
****case 1 : 
******num = 23; max = 3; min = 1; takelast = 0; comfirst = 0;
******break; 
****case 2 : 
******random_setup();
******break; 
****case 3 : 
******custom_setup ();
**}
**printf ("\nOn a scale of 1 to 5, 5 being best,\n");
**printf ("how well do you want the computer to play? (1-5) ");
**scanf("%d", &skill);
**winner = 0;
**if (!comfirst) {
****printf ("\n%d in pile.", num);
****winner = humanmove ();
**}
**while (!winner) {
****winner = compmove ();
****if (!winner) winner = humanmove ();
**}
**if (winner == 2)
****printf ("\nComputer wins!");
**else printf ("\nYou win!");
}

int playagain (void) {
**char input;

**printf ("\nThat was fun. Would you like to play again? (y\\n) ");
**do input = getchar();
**while (!isalpha(input));
**if (input == 'y' || input == 'Y') return (1);
**else return(0);
}

int main (void) {
**srand (time(NULL));
**intro ();
**do playgame (); while (playagain ());
**exit (0);
}
Well, this is it, folks. The last two. One will be the people's choice, and the other will be the grand finale. No losers here. (Hmmm, I wonder if I have one more running around somewhere that I can make my big surprise grand finale? I don't think so. Maybe I finally need to write Wumpus.)
  • Reverse (order a list of number by turning them around)
  • Acey Ducey (A card game of highs, lows, and middles)
guesst is offline                         Send a private message to guesst
Reply With Quote