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 22-06-2006, 02:43 AM   #31
guesst
Abandonia Homie
 
guesst's Avatar

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

@Beefonthebone - You should try this one. I think you'll find that even before it starts learning (which it only does when it loses) it'll regularly teach you a thing or two about playing the game. It's quite fun.

Without provocation or invition I will choose the next game, since no one cast a vote. Plus if all goes well I won't have time tomorrow...

Stars/Trap/Letter Guess

Oh yeah, 3 in one today. These games are all kind of variations on a theme. The sort of "guess what I'm thinking" games that entertained me so as a child. I figured no one would vote for these so I'm picking them before they're the last left on the field.

The benifit to these games is they are extremely simple and therefore extremly short. This is the sort of game that if you're writing your first game you'd want to write. (Not sure that came out ...good.) Two out of three of these are adapted from BASIC programs. I've linked the book that I got them from before, so I'm not going to worry about it now. I cracked all of these out in about a day. The challange was to not make them all look like I just edited a few lines between them. As they stand I'm not sure how successful I was at that. They all consist of only a main function and a few loops. They were just so small it seemed unnecessary to strech them out more than that.

They are also so abstract that, besides "Stars" I had nothing with which to make an illustration of. I haven't made illustrations for all of them, but the point is how would I even illustrate the first two? Ah well, here we go:

Letter Guess. This is the "higher/lower" sort of hint system. There exists a well defined method for finding the subject in question in the least number of guesses, so the game will actually tell you in the end if you took too long.

Technical note, I had actually inlined the BEFORE/AFTER bit, but took it out because as simple as the program is I didn't want to confuse new programmers with what is really a pretty odd looking bit of code.
Code:
/* Letter Guess Game
 * by Joseph Larson 2005
 * Inspired by the BASIC game 'Letter' written by Bob Albrecht
 * as found in 'BASIC Computer Games' edited by David H Ahl (c)1978
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

int main (void) {
**char input, goal;
**int num;

**srand(time(NULL));
**printf ("Guess My Letter Game\n\n");
**do {
****goal = rand() % 26 + 'A';
****num = 0;
****printf ("I have a letter between A and Z.\n");
****do {
******num ++;
******printf ("Guess #%d (A - Z) : ", num);
******do input = getchar (); while (!isalpha (input));
******input = toupper (input);
******if (input != goal) {
********printf ("\nNo, my letter comes ");
********if (input < goal) printf ("AFTER");
**********else printf ("BEFORE");
********printf (" your guess.\n");
******}
****} while (input != goal);
****printf ("\nYou got it! It took you %d tries to guess my letter.\n", num);
****if (num >= 4) printf ("I'm sure you could do better, though.\n");
****printf ("\nDo you want to play again? (y/n) ");
****do input = getchar (); while (!isalpha (input));
**} while ((input == 'y') || (input == 'Y'));
**printf ("\nGoodbye.\n");
**exit (0);
}
In Trap you try to surround the mystery number by choosing a high and a low number. Hints therefore come in 3 flavors, high, low, or trapped. To make the game more challenging you could make the high and low message exactly the same. Still, it's easy to develop a method to narrow your options fast.
Code:
/* Trap
 * written by Joseph Larson 2005
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>

#define MAX 100
#define MG 8

int main (void) {
**int x, h, l, try, temp;
**char yesno;

**printf ("Trap\n----\n"
**"In this game you have to try to guess a number between 1 and %d by\n"
**"trapping it. Every guess you type a low and high number seperated by a\n"
**"comma (like \"15, 30\") and you'll be told if the number you are trying to\n"
**"find is between your number. When you think you have it type the same\n"
**"number for both the high and low guess. And remember, you only have %d\n"
**"guesses to find the number\n"
**"Good luck!\n\n", MAX, MG);
**srand (time (NULL));
**do {
****x = rand () % MAX + 1;
****printf ("I have a number. You have %d guesses.\n", MG);
****for (try = 1; try <= MG && !(l == h && h == x); try ++) {
******printf ("\nGuess %d (low , high) : ", try);
******scanf ("%d %*c %d", &l, &h);
******if (l > h) { temp = h; h = l; l = temp;}
******if (l <= x && x <= h)
********printf ("You've trapped my number.\n");
******else printf ("My number is %s than your guesses.\n",
******(l > x) ? "lower" : "higher");
****}
****if (l == h && h == x) printf ("I am undone! You caught my number.\n\n");
****else printf ("Ha ha! That's %d guesses. My number was %d.\n\n", MG, x);
****printf ("Do you want to do play again? (y/n) ");
****while (!isalpha (yesno = getchar ()));
**} while (tolower (yesno) == 'y');
**printf ("Until next time then!\n");
**exit (0);
}
The last is Stars. The hints in Stars are sort of a geiger counter giving a visual representation of how close you're getting to the target. You'll find jumping around too much will only confuse you. Creeping up on the number is a much better idea.
Code:
/* Seeing Stars */
/* By Joseph Larson 2005 */
/* Number guessing game inspired by the BASIC game "Stars" by Bob Albrecht */
/* as found in 'BASIC Computer Games' edited by David H. Ahl (c) 1978 */

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

#define MAX 100
#define MAX_GUESS 10

int main () {
**int goal, guess, c, wins, loses;
**char yesno;
**float d;
**
**srand(time(NULL));
**wins = 0;
**loses = 0;
**printf ("Seeing Stars\n------------\n"
**"This is a number guessing game with a twist. After every guess you\n"
**"will be given a number of stars to tell you how close you are to the\n"
**"number you are trying to guess. The more stars you see, the closer\n"
**"you are.\n"
**"You only have %d guesses, so think quick.\n\n", MAX_GUESS);
**do {
****goal = rand() % MAX + 1;
****guess = 0;
****printf ("\nI'm thinking of a number between 1 and %d.", MAX);
****for (c = 0; c < MAX_GUESS && guess != goal; c++) {
******printf ("\nWhat is your guess? ");
******scanf ("%d", &guess);
******for (d = MAX; (int)d > abs(guess - goal); d /= 2) printf ("**");
****}
****if (guess != goal) {
******printf ("\nSorry, thats %d guesses. My number was %d.\n", MAX_GUESS, goal);
******loses ++;
****} else {
******printf ("******\nBingo! You got my number.\n");
******wins ++;
****}
****printf ("Do you want to play again? (y/n) ");
****while (!isalpha (yesno = getchar()));
**} while (tolower (yesno) != 'n');
**printf ("\nWell then, you won %d out of %d games, or %d%%.\n", 
****wins, wins + loses, wins * 100 / (wins + loses));
**exit (0);
}
Still plenty left. Tell me what looks good and I'll give it to you.
  • 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)
guesst is offline                         Send a private message to guesst
Reply With Quote
Old 22-06-2006, 04:52 AM   #32
Blood-Pigggy
10 GOSUB Abandonia
20 GOTO 10
 
Blood-Pigggy's Avatar

 
Join Date: Jan 2005
Location: Wilmington, United States
Posts: 2,660
Default

Cel Life, John Conway's Life was awesome.
__________________
Youtube Channel -
http://youtube.com/user/BloodPigggy

My Site -
http://sites.google.com/site/eyenixon
Blood-Pigggy is offline                         Send a private message to Blood-Pigggy
Reply With Quote
Old 23-06-2006, 07:19 AM   #33
Abi79
Home Sweet Abandonia

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

Battleship looks good.
Abi79 is offline                         Send a private message to Abi79
Reply With Quote
Old 23-06-2006, 04:56 PM   #34
guesst
Abandonia Homie
 
guesst's Avatar

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

@Abi79, you've gotten to pick the last few. I'm letting Blood-piggy choose. (Besides, he was first.) If no-one steps up after this one, then you're in the queue.

Cell Life
A Google search for John Conway's Game of life will provide more information on this than you ever wanted to know. (Wiki is also a good source)

Without taking up too much space, John Conway came up with a game that seems simple but in the end has proven to be the playground for the intelectual. The game is played on a grid, like a checker board. Markers are placed on the board. Then every "generation" the pieces live, die, or are born according to a simple set of rules:
  • A marker dies of lonelyness if it only has one or no neighbors.
  • A marker dies of overcrowing if it has neighbors on 5 or more sides.
  • Markers with two or three neighbors survive to the next generation.
  • A marker is born if an empty spot has exactly 3 neighbors.
In this program an attempt was made as giving ownership to the pieces so that two, three or four player games could be realized. So the last rule was modified thus
  • A marker is born when if an empty spot has exactly 3 neighbors. If at least two neighbors are the same type then the marker will be of that type. If no two neighbors are the same then a hybred is created.
In this game markers are called "cells" to give it an organic feel. To add an even more competitive edge to it, every generation players get to place one marker on the board to sway the game in their favor. The game is only won (at this time) with a 100% purging of the enemy cels. Thus games could potentially go on for a long time.
Here's a screenshot of a small three player game in action:
Code:
**12345
 1..@X.1
 2**OOX2
 3*..X.3
 4...*.4
 5.....5
**12345


Press ENTER to advance the generation:
Here's a large 4 player game (1 players already been elimitedy by now) in action:
Code:
************111111111122222
**1234567890123456789012345
 1........***..............1
 2.........................2
 3.......*..*..............3
 4........*..*.............4
 5.........**..............5
 6.........................6
 7.........................7
 8.........................8
 9............*............9
10..........XOX*...........10
11........XX...X...........11
12..........X..XX..........12
13........OX..XX...........13
14........O..XX............14
15..........XX.............15
16.........@...............16
17..........*O.............17
18..........*O.............18
**1234567890123456789012345
************111111111122222

Press ENTER to advance the generation:
I chose both screen shots because a hybred (@) had entered into the game.

Winning this game is a challange. Every turn the players rotate through who goes first. Going first is not an advantage because it's all to easy for your opponent to turn a birth into an overcrowing after they've seen what you're doing. However, this was the only way to insure that the game is played fair since, in this form, having everyone choose their move unseen isn't possible. Both methods have their advantages.

You can set the game for 4 computer players and just watch it go. This in and of itself is not uninteresting to see. But if you can master predicting the next generation just feel your brain grow.

Silly me, I haven't given you the code yet!
Code:
/* Cell Life
 * by Joseph Larson
 * based on a 2 player BASIC game by Bryan Wyvill 
 * as found in 'BASIC Computer Games' edited by David H Ahl (c) 1978
 * which game was derived from John Conway's Game of Life
 * as found in Scientific America, October 1970
 */

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

#define F(a,b) for (a = 0; a < (b); a++)
#define DIG v * tp(5) + w * tp(4) + x * tp(3) + y * 100 + z * 10
#define TURN ((c + f + g) % players)
#define MAX_W 26
#define MAX_H 19
#define PIECES 3

int bd[MAX_W][MAX_H], dis[8][2], pop[5], surv[5][55], hyb[10];
int height, width, players;
char tokens[6] = ".OX*#@";

void intro (void) {
**printf ("Cell Life\n---------\n\n"
**"The rules of this game are based on Conway's Game of Life. Tokens are\n"
**"placed on the board, then with every generation a token will live, die, or\n"
**"be born based on these rules :"
**" o A token with 2 or 3 neighbors survives.\n"
**" o A token with more or less than 2 or 3 neighbors dies from lonelyness or\n"
**"** overpopulation.\n"
**" o An empty space with exactly 3 neighbors gives birth to a new token.\n\n"
**"When giving birth the player with the majority of the 3 neighbors of an\n"
**"empty cell gets the new cell. If no one has the majority (in the case of\n"
**"a 3 way tie) the cell becomes a hybred that no one controls.\n"
**"Remember to count the number of neighbors same as in minesweeper. In other\n"
**"words a cell can have up to 8 neighbors.\n\n"
**"The first three moves (Generation 0) are when you place your initial\n"
**"pieces.\n\n"
**"Remember to pay close attention to who's turn it is as every turn a\n"
**"different player gets to go first.\n\n"
**"Good Luck.\n\n");
}

int tp (int x) {return ((x == 0) ? 1 : 10 * tp (x - 1)); }

void init (void) {
**int p, v, w, x, y, z, c, i, sum;

**srand (time (NULL));
**F(p, 5) {
****i**= 0;
****F(v, 4) F(w, 4) F(x, 4) F(y, 4) F(z, 4) {
******sum = v + w + x + y + z;
******if (sum == 3 || sum == 2) surv[p][i++] = (p + 1) + DIG;
******else if (sum == 1)
********surv[p][i++] = DIG + 2 * tp(p + 1);
****}
**}
**i = 0;
**F(v, 2) F(w, 2) F(x, 2) F(y, 2) F(z, 2) {
****sum = v + w + x + y + z;
****if (sum == 3) hyb[i++] = DIG;
**}
**i = 0;
**F (x, 3) F (y, 3) if (!(x == y && y == 1)) {
****dis[i][0] = x - 1; dis[i++][1] = y - 1;
**}
}

void print_bd (void) {
**int x, y;

**printf ("**"); /* 2 spaces */
**F (x, width) putchar (" 12"[x / 10]);
**putchar ('\n');
**F (y, (height + 2))
****F (x, (width + 2))
******(!y || y > height) ? (!x) ? printf ("**") /* 2 spaces */
******: putchar ((x > width) ? '\n' : '0' + (x % 10))
******: (!x) ? printf ("%2d", y) : (x > width) ? printf ("%-2d\n", y)
******: putchar (tokens [bd[x][y] % 10]);
**printf ("**"); /* 2 spaces */
**F (x, width) putchar (" 1234"[x / 10]); putchar ('\n');
}

int survives (int x) {
**int k, p;

**F (p, 5) F(k, 55) if (surv[p][k] == x) return p + 1;
**F (k, 10) if (hyb[k] == x) return 5;
**return 0;
}

void generation (void) {
**int x, y, k, p;

**F (x, width + 1) F (y, height + 1) if (p = (bd[x][y] % 10))
****F (k, 8) bd[x + dis[k][0]][y + dis[k][1]] += tp (p);
**F (k, 5) pop[k] = 0;
**F (x, width + 2) F (y, height + 2)
****if (!y || y > height || !x || x > width) bd[x][y] = 0;
****else {
******bd[x][y] = survives (bd[x][y]);
******if (bd[x][y]) pop[bd[x][y] - 1]++;
****}
}

void getxy (int *x, int *y) {
**printf ("<X,Y>: ");
**scanf ("%d %*c %d", x, y);
**while (*x > width || *x < 1 || *y > height || *y < 1) {
****printf ("Invalad location. Retry. x,y : ");
****scanf ("%d %*c %d", x, y);
**}
}

void compyturn (int p, int g) {
**int x, y, c, d, bestx, besty, r, bestr, k;
**int bdc[MAX_W][MAX_H], popc[5];

**if (g <= PIECES) if (g == 1) {
****bestx = rand() % (width - 1) + 1;
****besty = rand() % (width - 1) + 1;
**} else {
****F (x, width) F (y, width) if (bd[x][y] == (p + 1)) {bestx = x; besty = y;}
****r = 0;
****do { 
******k = rand () % 8;
******if (bd[bestx + dis[k][0]][besty + dis[k][1]]) k = -1;
******else if ((bestx + dis[k][0]) > width || (bestx + dis[k][0]) < 1) k = -1;
******else if ((besty + dis[k][1]) > height || (besty + dis[k][1]) < 1) k = -1;
****** r |= 1 << k;
****} while (k < 0 && r < 255);
****if (k < 0) {
******bestx = rand() % (width - 1) + 1; besty = rand() % (width - 1) + 1;
****} else { bestx += dis[k][0]; besty += dis[k][1]; }
**} else {
****F (c, height + 1) F (d, width + 1) bdc[c][d] = bd[c][d];
****F (k, 5) popc[k] = pop[k];
****bestx = rand() % (width - 1) + 1;
****besty = rand() % (width - 1) + 1;
****bestr = -99;
****F (y, height - 1) F (x, width - 1) if (!bd[x + 1][y + 1]) {
******bd[x + 1][y + 1] = p + 1;
******generation ();
******r = 0;
******F (k, 5) if (k == p) r += pop[k]; else r -= pop[k];
******if (r > bestr) {bestr = r; bestx = x + 1; besty = y + 1;}
******F (c, height + 1) F (d, width + 1) bd[c][d] = bdc[c][d];
******F (k, 5) pop[k] = popc[k];
****}
**}
**bd[bestx][besty] = p + 1;
**printf ("Computer Player %d (%c) : %d, %d\n", p + 1, tokens[p + 1], bestx, besty);
}

int winner (void) {
**int c, w = 0;

**F (c, players) if (pop[c]) if (w) return 0; else w = c + 1;
**return (w) ? w : 5;
}

int getint (int low, int high) {
**int t;

**scanf ("%d", &t);
**while (t < low || t > high) {
****printf ("Invalad. Choose a number between %d and %d : ", low, high);
****scanf ("%d", &t);
**}
**return t;
}

void play (void) {
**int c, d, f, x, y, comp[4], g = 0;

**printf ("How many players? (2 - 4) ");
**players = getint (2, 4);
**F(c, players) {
****printf ("Player %d, (1) human or (2) computer? : ", c + 1);
****comp[c] = getint (1, 2) - 1;
**}
**printf ("(1) Small or (2) large game? ");
**c = getint(1, 2);
**width = (--c) ? MAX_W - 1 : 5;
**height = (c) ? MAX_H - 1 : 5;
**f = rand() % players;
**printf ("Each player gets %d pieces to play.\n", PIECES);

**F (c, players) pop[c] = 1;
**do {
****g++;
****F (c, players) {
******if (pop[TURN]) {
********print_bd ();
********if (!comp[TURN]) {
**********printf ("Generation %d, player %d (%c) ", (g < PIECES) ? 0 : g - PIECES
************, TURN + 1, tokens[TURN + 1]);
**********getxy (&x, &y);
**********if (bd[x][y]) printf ("Occupied. Turn discarded.\n");
**********else bd[x][y] = TURN + 1;
********} else compyturn (TURN, g);
******}
****}
****if (g >= PIECES) {
******print_bd ();
******printf ("\nPress ENTER to advance the generation:\n");
******getchar (); getchar ();
******generation ();
****}
**} while ((g < 0) || !(d = winner ()));

**print_bd();
**printf ((d == 5) ? "\n\nTotal extinction." : 
****(d == 4) ? "\n\nHybred takeover!" : "\n\nPlayer %d wins!", d);
}

int main (void){
**intro ();
**init ();
**play ();
**getchar ();
**exit (0);
}
This particular code bears less resemblance to the BASIC game that inspired it. I've taken many liberties with it, but for those who are interested you can see the original here. The BASIC game is surprisingly compact, but at the same time not as robust as this program.

This is a game that would probably be better suited to something more graphical. In this form it's a bit hard to play. But then again, if you're the sort of person for whom Conway's Game of Life holds your interest something like console graphics is no turn off.

Abi put in a vote for Battleship next, and since i love my regurar reader (wish I could put an 's' there) that'll probably be next, but feel free to queue up your preference :
  • Battleship (like the board game vs the computer)
  • Reverse (order a list of number by turning them around)
  • Black Box (find molecules in the inky depths)
  • Acey Deucy (a card game of highs, lows, and middles)
  • Flash Cards (with pretty output, practice your math)
  • Pickup Piles (1000 games in one, set the rules and play)
  • Hangman (guess the word before you dangle)
  • Rotate (like those sliding block puzzles but that you rotate pieces)
guesst is offline                         Send a private message to guesst
Reply With Quote
Old 30-06-2006, 04:23 AM   #35
guesst
Abandonia Homie
 
guesst's Avatar

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

Sorry, I've been at school all week, so I've been unable to update with this program. But maybe absense has mad all yawl's hears go fonder. So you should be very excited to finally have...

Battleship

This is the biggest program that I have to contribute. However, it is a game that should need little introduction. Battleship. Place your ships, hunt theirs, hope you win. The computer plays against you on this one. The routine for choosing where to shoot is not the best out there, but it's not entirely stupid either.

Here's what the game looks like in play:
Code:
Your shot coordinates : f9
Miss.

My shot : g4
HIT!
I sunk your cruser.
****** R A D A R********************** F L E E T
_ 1 2 3 4 5 6 7 8 9 10**********_ 1 2 3 4 5 6 7 8 9 10
a . . . . . . . . . .********** a . . . . . o . . . .
b . . . . . . . . . .********** b . . . . . . . . . .
c o . . . . . . . . .********** c . . 2 2 2 . . . . .
d X . o . . . . . . .********** d . . . . . . . . . 5
e X . . . . . . . . .********** e . . . . 1 1 . . . 5
f X . . . o . . . o .********** f . o o . 4 4 4 4 . 5
g X . . . . o . . . .********** g o X X X . . . . . 5
h . . . . . . o . . .********** h . o . . . . . . . 5
i . . . . . . . . . .********** i . . . . . . . o . o
j . . . . . . . . o .********** j . . . . o . . . . .
****carrier : #####**************** carrier : #####
 battleship : SUNK************** battleship : ####
**** cruser : ###********************cruser : ...
**submarine : ###**************** submarine : ###
****pt ship : ##********************pt ship : ##

Your shot coordinates :c5
You may notice that the game is set up side-by-side instead of up and down like a battleship game in real life is. This is purely because up and down didn't fit, but side to side fits with enough room for you to see the results of the previous move, which is a good thing because that's where it says when you sink something.

So why don't we just get to the code:
Code:
/* Batttleship (R)
 * written by Joseph Larson 2005
 */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

#define PL 0
#define CP 1
#define FULL(x) ((x + (x < 2)) + 1)
#define ADD(a,b) if (!bd[a][b].chit) {t[i].x = a; t[i].y = b; i++;}

typedef struct {
**unsigned int x : 4;
**unsigned int y : 4;
} COORD;

typedef struct {
**unsigned int pship : 3;
**unsigned int chit : 1;
**unsigned int cship : 3;
**unsigned int phit :3;
} GRID;

GRID bd[10][10];
COORD t[51];
char ship_life[2][5];
char *ship_name[5] = {"pt ship", "submarine", "cruser", "battleship", "carrier"};

COORD getloc (void) {
**char input[10];
**COORD loc;

**loc.x = loc.y = input[0] = 0;
**do {
****if (input[0]) printf ("Invalad location, letter first then number : ");
****scanf ("%s", input);
****if (isalpha (input[0]) && (loc.x = atoi (&input[1]))) {
******loc.y = tolower (input[0]) - 'a';
******if (loc.y > 9 || loc.x > 10 || loc.x < 0) loc.x = 0;
****}
**} while (!loc.x);
**loc.x --;
**return loc;
}

void show_board (void) {
**int x, y;
**
**printf ("%16s\t\t%16s\n_ 1 2 3 4 5 6 7 8 9 10\t\t_ 1 2 3 4 5 6 7 8 9 10"
****,"R A D A R","F L E E T");
**for (y = 0; y < 10; y++) {
****printf ("\n%c ", y + 'a');
****for (x = 0; x < 10; x++)
******printf ("%c ", (bd[x][y].phit) ? (bd[x][y].cship) ? 'X' : 'o' : '.');
****printf ("\t\t%c ", y + 'a');
****for (x = 0; x < 10; x++)
******printf ("%c ", (bd[x][y].chit) ? (bd[x][y].pship) ? 'X' 
******: 'o' : ".12345"[bd[x][y].pship]);
**}
**for (y = 4; y >= 0; y--) {
****printf ("\n %10s : ", ship_name[y]);
****if (ship_life[CP][y]) for(x = 0; x < FULL(y); x++) putchar ('#');
****else printf ("SUNK");
****printf ("\t\t %10s : ", ship_name[y]);
****for (x = 0; x < FULL(y); x++) putchar (".#"[ship_life[PL][y] > x]);
**}
}

int valad_ship (COORD s, COORD e, int c) {
**int v, check, d;
**COORD step;
**
**check = abs ((s.x + 10 * s.y) - (e.x + 10 * e.y));
**if (check % (FULL(c) - 1)) {
****printf ("\nInvalad location. The %s is only %d long\n"
************"and ships can only be placed vertical or horizontal.\n",
************ship_name[c], FULL(c));
****v = 0;
**} else {
****step.x = step.y = 0;
****if ((check / (FULL(c) - 1)) - 1) step.y = 1;
****else step.x = 1;
****if (s.x > e.x) s.x = e.x;
****if (s.y > e.y) s.y = e.y;
****for (d = 0; d < FULL(c) && v; d++) {
******check = bd[s.x + d * step.x][s.y + d * step.y].pship;
******if (check && check != 7) {
********printf ("\nInvalad location. Ships can not overlap.\n");
********v = 0;
******}
****}
**}
**if (v) for (d = 0; d < FULL(c); d++)
****bd[s.x + d * step.x][s.y + d * step.y].pship = c + 1;
**return v;
}

void player_setup (void) {
**int ship, d;
**COORD start, end;
**
**for (ship = 4; ship >= 0; ship--)
****do {
******show_board ();
******printf ("\nEnter start location for your %s : ", ship_name[ship]);
******start = getloc();
******printf ("Enter end location (length %d) : ", FULL(ship));
******end = getloc();
****} while (!valad_ship (start, end, ship));
**show_board ();
}

void auto_setup (int pl) {
**COORD s, step;
**int c, d;
**
**for (c = 0; c < 5; c++) {
****do {
******s.x = rand() % 10; s.y = rand() % 10;
******step.x = step.y = 0;
******if (rand() < RAND_MAX / 2) {
********step.x = 1;
********if (s.x + FULL(c) > 10) s.x -= FULL(c);
******} else {
********step.y = 1;
********if (s.y + FULL(c) > 10) s.y -= FULL(c);
******}
******for (d = 0; d < FULL(c) && 
******(pl) ? !bd[s.x + d * step.x][s.y + d * step.y].cship
******: !bd[s.x + d * step.x][s.y + d * step.y].pship; d++);
****} while (d < FULL(c));
****for (d = 0; d < FULL(c); d++)
******(pl) ? bd[s.x + d * step.x][s.y + d * step.y].cship 
******: bd[s.x + d * step.x][s.y + d * step.y].pship = c + 1;
**}
}

void init (void) {
**int c, d;
**char input;
**
**srand (time (NULL));
**for (c = 0; c < 10; c++)
****for (d = 0; d < 10; d++)
******bd[c][d].pship = bd[c][d].chit = bd[c][d].cship = bd[c][d].phit = 0;
**for (c = 0; c < 5; c++)
****ship_life[PL][c] = ship_life[CP][c] = FULL(c);
**printf ("Battleship (R)\n\nDo you want (A)uto or (M)anual setup ? (a/m) ");
**while (!isalpha (input = getchar()));
**if (tolower (input) == 'm')
****player_setup ();
**else auto_setup (PL);
**auto_setup (CP);
}

int check_for_lose (int player) {
**int c;
**
**for (c = 0; c < 5 && !ship_life[player][c]; c++);
**return (c == 5);
}

void player_turn (void) {
**COORD shot;
**int ship;
**
**show_board ();
**printf ("\n\nYour shot coordinates : ");
**shot = getloc ();
**if (bd[shot.x][shot.y].phit)
****printf ("A wasted shot! You already fired there!\n");
**else {
****bd[shot.x][shot.y].phit = 1;
****if (ship = bd[shot.x][shot.y].cship) {
******printf ("HIT!\n");
******if (!(--ship_life[CP][--ship]))
********printf ("You sunk my %s.\n",ship_name[ship]);
****} else printf ("Miss.\n");
**}
}

int hit_no_sink (int x, int y) {
**if (bd[x][y].chit)
****if (bd[x][y].pship == 7)
******return 1;
****else if (bd[x][y].pship)
******if (ship_life[PL][bd[x][y].pship - 1])
********return 1;
**return 0;
}

int fill_t (void) {
**COORD c, d;
**int m[5] = {0, 1, 0, -1, 0};
**int x, i = 0;
**
**for (c.x = 0; c.x < 10; c.x++)
****for (c.y = 0; c.y < 10; c.y++)
******if (hit_no_sink (c.x,c.y)) {
********for (x = 0; x < 4; x++)
**********if (c.x + m[x] >= 0 && c.x + m[x] < 10 
************&& c.y + m[x + 1] >= 0 && c.y + m[x + 1] < 10) {
************if (hit_no_sink (c.x + m[x], c.y + m[x + 1])) {
**************d.x = c.x; d.y = c.y;
**************while (d.x >= 0 && d.x < 10 && d.y >= 0 && d.y < 10 
****************&& hit_no_sink (d.x, d.y)) {d.x -= m[x]; d.y -= m[x + 1];}
**************if (d.x >= 0 && d.x < 10 && d.y >= 0 && d.y < 10) ADD (d.x, d.y);
************}
**********}
********if (!i)
**********for (x = 0; x < 4; x++) 
************if (c.x + m[x] >= 0 && c.x + m[x] < 10 
**************&& c.y + m[x + 1] >= 0 && c.y + m[x + 1] < 10)
**************ADD (c.x + m[x], c.y + m[x + 1]);
******}
**if (!i)
****for (c.x = 0; c.x < 10; c.x++)
******for (c.y = 0; c.y < 10; c.y++)
********if ((c.x + c.y) % 2) ADD (c.x, c.y);
**return i;
}

void compy_turn (void) {
**int z, c, d, v;
**char input;
**COORD s, e;
**
**c = fill_t ();
**z = rand () % c;
**printf ("\nMy shot : %c%d\n", t[z].y + 'a', t[z].x + 1);
**bd[t[z].x][t[z].y].chit = 1;
**if (c = bd[t[z].x][t[z].y].pship) {
****printf ("HIT!\n");
****if (!(--ship_life[PL][c - 1]))
******printf ("I sunk your %s.\n", ship_name[c - 1]);
**} else printf ("Miss.\n");
}

void play (void) {
**int winner = 0;
**
**if (rand () < RAND_MAX / 2) {
****printf ("\nYou go first.\n");
****player_turn ();
**} else printf ("\nI'll go first.\n");
**do {
****compy_turn ();
****if (check_for_lose (PL)) {
******winner = 1;
******printf ("\nI win!\n");
****} else {
******player_turn ();
******if (check_for_lose (CP)) {
********winner = 1;
********printf ("\nYou win!\n");
******}
****}
**} while (!winner);
**show_board ();
}

int play_again (void) {
**char input;

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

int main (void) {
**init ();
**do {play ();} while (play_again ());
**printf ("\nIt's been fun! So long Admiral!\n");
**exit (0);
}
Techinal note; the way the computer chooses it's shot is mostly handled by the function fill_t, or "fill target list." The comptuer analizes the board and makes a list of the best shots. If it's made an isolated hit, it fills the list with the spaces up, down, left and right of it, provided they're not already misses. If it's made two hits in a row it fills the list with the shots at the end, provided they're not already misses. If the list is still 0 in length, or it hasn't found any potential targets up to now, then the list is populated with ever other shot, resulting in a checkerboard pattern of possibilities. (No point in missing 3 in a row if the smallest ship is 2 in length.) Then it just chooses randomly from this list. Check it out and see if you can see this in action.

Now, if a clever programmer out there want to try to make a smaller version of this, go say for it. I'm actually disapointed with how humongus this is. This, Awari, and Cell I wish I could reduce a bit.

Now is when I beg you for you support. How do you support this great project? Just choose a program from the following list and tell me you want to play it. Because as long as this has got your support we're not stopping until the list is empty:
  • Reverse (order a list of number by turning them around)
  • Black Box (find molecules in the inky depths)
  • Acey Deucy (a card game of highs, lows, and middles)
  • Flash Cards (with pretty output, practice your math)
  • Pickup Piles (1000 games in one, set the rules and play)
  • Hangman (guess the word before you dangle)
  • Rotate (like those sliding block puzzles but that you rotate pieces)
guesst is offline                         Send a private message to guesst
Reply With Quote
Old 01-07-2006, 10:20 AM   #36
Abi79
Home Sweet Abandonia

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

There's a small lil' problem...or maybe two problems.

1. Even if I've saved the file as a *.c, when I tried to run it in BorlandC++ for DOS, I got the following error message:
"Lvalue required" at
Code:
: bd[s.x + d * step.x][s.y + d * step.y].pship = c + 1;
I've fixed this (added another = before c + 1) and then I've started the game.

Setup: auto
At the fleet screen, I couldn't see any of my ships. I've ignored that and chosed to attack c5. Of course I missed (I always lose in games :P ), and the computer started it's turn. But nothing happened. Following the using of the ingenious key combo CTRL+Break, I managed to see that the computer entered a loop.

Code:
int hit_no_sink (int x, int y) {
**if (bd[x][y].chit)**// bd[x][y].chit=0
//*skips rest of code*
**return 0;
}
Then it goes to
Code:
if (hit_no_sink (c.x,c.y)) {** 
/*starts hit_no_sink and does the same thing as above; it doesn't go further because hit_no_sink always returns 0*/
For Setup: manual the computer does the same thing, after I finish choosing the locations of my ships and he starts his turn.

What might cause the problem?
Abi79 is offline                         Send a private message to Abi79
Reply With Quote
Old 02-07-2006, 03:45 AM   #37
guesst
Abandonia Homie
 
guesst's Avatar

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

I guess this means I have to install Borland compiler. I know it workes in DevC++ because the screenshot you see is taken from an on-the-fly compile of the exact code that got copy/pasted into the post.

It's true, tho, that not all compilers are the same. I've known this for some time. I can't promise I'll be able to get to this any time soon, so until then, what's your next vote for the project?
guesst is offline                         Send a private message to guesst
Reply With Quote
Old 04-07-2006, 01:36 PM   #38
Abi79
Home Sweet Abandonia

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

Sorry for the late reply. I was a bit busy.

Anyway, my next vote is Black Box.
Abi79 is offline                         Send a private message to Abi79
Reply With Quote
Old 10-07-2006, 11:57 PM   #39
guesst
Abandonia Homie
 
guesst's Avatar

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

I'm trying to make up for all the things I promised I'd do but haven't yet done. I'm such a slacker and Cymon's Games has suffered.

Not that anyone but Abi79 even cares.

But, in Pennance I bring you today:
BlackBox

Unfortunately there are better games I could have done my pennance with. This game isn't particularly cool to see played. To get the most out of it you should really play with a piece of graph paper next to you to take notes on. The purpose of the game is to discover the location of 6 spots in a 10x10 field. You can't see the field (it's covered) but you can shoot rays into it and observe where they exit, which will tell you likely places where the spots can be found.

The game actually contains pretty detailed instructions on how the ray reacts to the spots, so I'm gonna let it do all the talking:
Code:
/* Blackbox or AtomSweeper
 * written by Joseph Larson
 * based on a BASIC program 'Blackbox' by Jeff Keton
 * as found in 'More Basic Computer Games' edited by David H. Ahl (c) 1979
 */

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

#define SIZE 10
#define NUM 6
#define FS(a) for (a = 1; a <= SIZE; a++)
#define RS (rand () % (SIZE - 2) + 2)

void drawbox (int b[][SIZE + 2], int opt) { // 1: input coords 2: xy coords
**int x, y;
**
**printf ("\n**");
**FS(x) printf(" %c",(opt < 2) ? '0' + ((4 * SIZE - x + 1) / 10)
****: " 123"[(x / 10)]);
**printf ("\n**");
**FS(x) printf(" %c",'0' + ((opt < 2) ? ((4 * SIZE - x + 1) % 10) : (x % 10)));
**FS(y) {
****printf ("\n%2d", y);
****FS(x) printf (" %c", (opt < 3) ? '+' : (b[x][y]) ? 'O' : '.');
****if (opt < 2) printf (" %-2d", 3 * SIZE - y + 1);
**}
**if (opt < 2) {
****printf ("\n**");
****FS(x) printf(" %c"," 1234"[(SIZE + x) / 10]);
****printf ("\n**");
****FS(x) printf(" %c",'0' + ((SIZE + x) % 10));
**}
**printf("\n\n");
}

void intro (void) {
**printf ("BlackBox\n--------\n"
**"Try to find atoms that are hidden in a Black Box by firing rays into the\n"
**"box and observing where they emerge.\n"
**" 1) A ray that enters a square diagonal to an atom, so that it would be\n"
**"****next to the atom if it continued, is deflected 90 degrees.\n"
**" 2) A ray that is fired directly at an atom is reflected back the way it\n"
**"****came. It will emerge where it was fired.\n"
**" 3) A ray that is traveling so as to pass between two atoms that are one\n"
**"****square away will also be reflected.\n"
**"\t......\n"
**"\t...O..**********\t\t.....****...O\n"
**"\t--\\...Deflection\t\t---O. or ---. Reflection\n"
**"\t..|...**********\t\t.....****...O\n\n"
**"Input the starting place of the ray you want to fire by typing the\n"
**"cooresponding number on the illustration you will be given at the start\n"
**"of the game.\n"
**"If you need to see this illustration again or want to guess the\n"
**"location of the atoms input 0 (zero) for your move.\n"
**"You will gain one point for every reflection, two for every deflection,\n"
**"and 15 for every atom guessed incorrectly. Try for the lowest score!\n\n"
**"Press ENTER to begin.\n");
**getchar (); 
}

void setup (int b[][SIZE + 2]) {
**int x, y, c;

**for (x = 0; x < SIZE + 2; x++) for (y = 0; y < SIZE + 2; y++) b[x][y] = 0;
**for (c = 0; c < NUM; c++) {while (b[x = RS][y = RS]); b[x][y] = 1;
**}
**printf ("%d atoms hidden.\n", NUM);
}

int shoot (int start, int b[][SIZE + 2]) {
**int x, y, dx, dy, temp;

**switch ((start - 1) / (SIZE)) {
****case 0 : x = 1; y = start; dx = 1; dy = 0; break;
****case 1 : x = start - SIZE; y = SIZE; dx = 0; dy = -1; break;
****case 2 : x = SIZE; y = 3 * SIZE + 1 - start; dx = -1; dy = 0; break;
****case 3 : x = 4 * SIZE + 1 - start; y = 1; dx = 0; dy = 1;
**}
**while (1) {
****switch ((b[x][y] * 7) | (2 * b[x + 1][y - 1] + 2 * b[x - 1][y + 1]
******+ b[x + 1][y + 1] + b[x - 1][y - 1])) {
******case 1 : temp = dx; dx = -dy; dy = -temp; break;
******case 2 : temp = dx; dx = dy; dy = temp; break;
******case 3 :
******case 7 : puts ("The shot was reflected."); return 1;
****}
****x += dx; y += dy;
****if (!(x % (SIZE + 1)) || !(y % (SIZE + 1))) {
******if (!x) temp = y;
******else if (!y) temp = 4 * SIZE - x + 1;
******else if (x > SIZE) temp = 3 * SIZE - y + 1;
******else temp = SIZE + x;
******printf ("Ray exited at %d.\n", temp);
******return 2;
****}
**}
}

int endgame (int b[][SIZE + 2], int *s) {
**int c, x, y;
**char input;
**
**printf ("Do you want to guess now? (y/n) ");
**while (!isalpha (input = getchar ()));
**if (tolower(input) != 'y') {
****drawbox(b, 1);
****return 0;
**}
**drawbox(b, 2);
**for (c = 0; c < NUM; c++) {
****printf ("What is the location of atom #%d? <x,y> ", c);
****scanf ("%d %*c %d", &x, &y);
****if (b[x][y] == 1) {
******puts ("\nCorrect");
******b[x][y] = 10;
****} else {
******puts ("\nIncorrect. Plus 15 points.");
*******s += 15;
****}
**}
**printf ("\nYour final score : %d\nHere's the Blackbox.\n**", *s);
**drawbox (b, 3);
**return 1;
}

int playmore (void) {
**char yesno;

**printf ("\nWould you like to play again? ");
**while (!isalpha (yesno = getchar ()));
**if (tolower(yesno) != 'n') return 1;
**return 0;
}


int main (void) {
**int board[SIZE + 2][SIZE + 2], ray, score, done;

**srand (time (NULL));
**intro ();
**do {
****setup (board);
****score = 0;
****drawbox (board, 1);
****done = 0;
****do {
******printf ("Ray # ? ");
******scanf ("%d", &ray);
******if (ray && ray < 4 * SIZE) score += shoot (ray, board);
******if (!ray) done = endgame (board, &score);
****} while (!done);
**} while (playmore ());
**puts ("Goodbye!");
**exit (0);
}
Oh, and I forgot to mention that, yes, this one is a BASIC conversion with liberty taken on the fact that I didn't like the way edge spots were handled, so I made it so no spots would ever be edge spots, and changed absorption to reflection to increase potential mis-direction.

This is a game of solitare where the object is to perfect your technique to get the most acurate reading with the fewest hints. If you find 6 spots on a 10x10 field not to your liking, the code is very happy, if you change a few constants at the top, to customize for you.

Now, I know it's been a while, but the problem was I didn't have my programs with me, and darned if I was going to waste my weekends with my wife and kids on the computer. But now I have the programs with me at school and nothing better to do all day long, so expect the rest of the list to fly by. Only a few left. Make your choice or, if no one chooses, I'll choose for you. But as always, I love your feedback!
  • Reverse (order a list of number by turning them around)
  • Acey Deucy (a card game of highs, lows, and middles)
  • Flash Cards (with pretty output, practice your math)
  • Pickup Piles (1000 games in one, set the rules and play)
  • Hangman (guess the word before you dangle)
  • Rotate (like those sliding block puzzles but that you rotate pieces)
guesst is offline                         Send a private message to guesst
Reply With Quote
Old 13-07-2006, 12:23 PM   #40
Abi79
Home Sweet Abandonia

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

Sorry for answering so late, but something happened to my Internet connection. The good part is that today, when I called the customer support, the one who responded actually knew what I was talking about, and in 30 minutes my connection started working again. Romanian Data Systems rocks. :P

I choose...FlashCards. Me likey math.
Abi79 is offline                         Send a private message to Abi79
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 09:38 AM (GMT)

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