home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 January
/
usenetsourcesnewsgroupsinfomagicjanuary1994.iso
/
sources
/
games
/
volume13
/
engarde
/
part01
/
board.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-02-11
|
5KB
|
210 lines
/*********************************************************************
Engarde
File: board.c
This file contains the routines for the data of the board, magnet
comparisons, and movement legality.
Version 1: 1/9/92 (Bill James)
*********************************************************************/
#include <malloc.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include "board.h"
extern int H,W;
char *SIGNSTRING[] = {"-","*","+"};
BoardType *b;
/*
Flips piece from black to white or vice versa
*/
void FlipPiece(p)
PieceType *p;
{
p->color = (p->color == PIECE_BLACK)? PIECE_WHITE:PIECE_BLACK;
}
/*
Mallocs and initializes board entity items
*/
void InitBoard(s, h, w)
SideType *s;
int h,w;
{
int i,j;
PieceType *pc;
b = (BoardType *) malloc (sizeof (BoardType));
b->magnets = (char **) malloc (sizeof(char*) * h);
b->pieces = (PieceType ***) malloc (sizeof(PieceType**) * h);
for (i = 0; i < h; i++) {
b->magnets[i] = (char *) malloc (w);
b->pieces[i] = (PieceType **) malloc (w * sizeof(PieceType*));
for (j = 0; j < w; j++) {
b->magnets[i][j] = 0;
b->pieces[i][j] = NULL;
}
}
for (i = 0; i < NPIECES; i++) {
pc = (PieceType *) malloc (sizeof(PieceType));
s[PIECE_ROUND].p[i] = pc;
s[PIECE_ROUND].p[i]->type = PIECE_ROUND;
s[PIECE_ROUND].p[i]->color = PIECE_ROUND;
pc = (PieceType *) malloc (sizeof(PieceType));
s[PIECE_SQUARE].p[i] = pc;
s[PIECE_SQUARE].p[i]->type = PIECE_SQUARE;
s[PIECE_SQUARE].p[i]->color = PIECE_SQUARE;
}
b->height = h;
b->width = w;
}
/*
Deletes board memory items
*/
void KillBoard(s)
SideType s[2];
{
int i,j;
for (i = 0 ; i < b->height; i++) {
if (s[0].p[i]) free(s[0].p[i]);
if (s[1].p[i]) free(s[1].p[i]);
for (j = 0; j < b->width; j++) {
if (b->pieces[i][j]) free(b->pieces[i][j]);
}
free((char *) b->magnets[i]);
}
free((char *) b->magnets);
free((char *) b->pieces);
}
#define GetPiece(b,h,w) (b)->pieces[(h)][(w)]
#define GetPolarity(b,h,w) (b)->magnets[(h)][(w)]
#define CKHW(b,h1,w1) (((h1) < (b)->height) && ((w1) < (b)->width))
/*
Move piece from h1,w1 to h2,w2
*/
int MovePiece(h1, w1, h2, w2)
int h1,h2,w1,w2;
{
if (!(CKHW(b,h1,w1))) return(1);
if (!(CKHW(b,h2,w2))) return(1);
if (b->pieces[h1][w1]) {
if (b->pieces[h2][w2]) return(1);
b->pieces[h2][w2] = b->pieces[h1][w1];
Magnetize(h2,w2);
b->pieces[h1][w1] = NULL;
return(0);
}
else return (1);
}
/*
Randomly shuffles board magnets
*/
void BoardShuffle()
{
int i,j;
char k;
for ( i = 0; i < b->height; i++)
for (j = 0; j < b->width; j++) {
k = (char) (random() % 2);
if (k) b->magnets[i][j] = 1;
else b->magnets[i][j] = -1;
k = (char) (random() % 10);
if (!k) b->magnets[i][j] = 0;
Magnetize(i,j);
}
}
/*
Sets piece color to correct color for it's shape/position
*/
void Magnetize(h, w)
int h,w;
{
PieceType *p;
p = b->pieces[h][w];
if (!(b->magnets[h][w])) return;
if (p) {
if ((p->type == PIECE_ROUND) && (b->magnets[h][w] == POSITIVE))
p->color = PIECE_BLACK;
if ((p->type == PIECE_ROUND) && (b->magnets[h][w] == NEGATIVE))
p->color = PIECE_WHITE;
if ((p->type == PIECE_SQUARE) && (b->magnets[h][w] == POSITIVE))
p->color = PIECE_WHITE;
if ((p->type == PIECE_SQUARE) && (b->magnets[h][w] == NEGATIVE))
p->color = PIECE_BLACK;
}
}
/*
Sums the colors of the set of four pieces (i,j),(i+dx,j+dy),
(i+2dx,j+2dy),(i+3dx,j+3dy), or returns -1 if any of the
spaces have no piece.
*/
int SumSet(i,j,dx,dy)
int i,j,dx,dy;
{
int s,l,m,r;
s = 0;
for (r = 0, l = i, m = j; ((r < 4) && (l < W) && (l >= 0)) &&
((m < H) && (m >= 0)); r++, l+=dx, m+=dy) {
if (!b->pieces[l][m]) return(-1);
s += b->pieces[l][m]->color;
}
if (((l >= W) || (l < 0) || (m >= H) || (m < 0)) && (r < 4)) return(-1);
return(s);
}
/*
Checks the board to find any set of 4 spaces in a row,column,diag
that are the same color.
*/
int CheckWinner(i, j, dx, dy)
int *i,*j,*dx,*dy;
{
int c,d,x,y,g;
for (c = 0; c < W; c++)
for (d = 0; d < H; d++)
for (x = -1; x < 2; x++) {
for (y = -1; y < 2; y++) {
if ((!x) && (!y)) continue;
g = SumSet(c,d,x,y);
if (g == (4 * PIECE_BLACK)) {
*dx = x; *dy = y;
*i = c; *j = d;
return(1);
}
if (g == (4 * PIECE_WHITE)) {
*dx = x; *dy = y;
*i = c; *j = d;
return(1);
}
}
}
return(0);
}
void Randomize()
{
time_t g;
int *h;
g = time();
h = (int *) &g;
srandom(*h);
}