introduced a move struct, added some functions using it in game.c, written a function terminal_game in server.c for local games

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2008-10-10 21:58:24 +00:00
parent 716cfbc50e
commit b00565ea66
3 changed files with 278 additions and 166 deletions

View File

@ -2,6 +2,7 @@
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "net.h"
#include "game.h"
@ -39,6 +40,12 @@ char classic_setup[] =
"H30H30H50H50" // OBELISK
;
int
game_turn(game_t *game)
{
return game->move % 2;
}
int
laser_at(game_t *game, int x, int y)
{
@ -59,7 +66,7 @@ laser(game_t *game, int side) {
2
*/
net_all_printf("LASER (%s)\n", sideinfo[side].name);
//printf("LASER (%s)\n", sideinfo[side].name);
assert(side == 0 || side == 1);
switch (side) {
@ -71,13 +78,16 @@ laser(game_t *game, int side) {
break;
}
game->last_hit.piece = NONE;
for (;;) {
net_all_printf("pos: (%d,%d)\n", y, x);
//printf("pos: (%d,%d)\n", y, x);
switch (game->board[x][y].piece) {
case NONE:
break;
case PHARAO:
game->last_hit = game->board[x][y];
game->winner = 1-game->board[x][y].side;
lreturn(SILVER_WINS + 1-game->board[x][y].side); /* GAME OVER */
case OBELISK2:
game->last_hit = game->board[x][y];
@ -113,7 +123,7 @@ laser(game_t *game, int side) {
break;
case 3: x--;
if (x<0) lreturn(L_WALL);
break;
break;
}
}
@ -136,33 +146,53 @@ square_allowed(int x, int y, int side)
}
int
movable(game_t *game, int row, int col, int side)
movable(game_t *game, int x, int y, int side)
{
return
row>=0 && row<BOARD_HEIGHT &&
col>=0 && col<BOARD_WIDTH &&
game->board[col][row].piece != NONE &&
game->board[col][row].side == side;
y>=0 && y<BOARD_HEIGHT &&
x>=0 && x<BOARD_WIDTH &&
game->board[x][y].piece != NONE &&
game->board[x][y].side == side;
}
int
rotate(game_t *game, int side, int row, int col, rot_t dir)
rotate(game_t *game, int side, int x, int y, rot_t dir)
{
int incr;
if (!movable(game, row, col, side))
if (game->move % 2 != side)
return E_NOT_YOUR_TURN;
if (!movable(game, x, y, side))
return E_ILLEGAL_MOVE;
assert(dir==R_CW || dir==R_CCW);
incr = dir==R_CW ? 1 : -1;
game->board[col][row].dir = (game->board[col][row].dir + incr) & 3;
game->board[x][y].dir = (game->board[x][y].dir + incr) & 3;
game->move++;
return HUGE_SUCCESS;
}
void
move_dest(move_t m, int *nx, int *ny)
{
*ny = m.y - (m.dir<3) + ((m.dir>3) && (m.dir<7));
*nx = m.x + ((m.dir>1) && (m.dir<5)) - ((m.dir<1) || (m.dir>5));
}
int
move(game_t *game, int side, int row, int col, int dir, int flags) {
if (!movable(game, row, col, side))
move(game_t *game, int side, move_t m)
{
int x, y, dir, nx, ny;
x = m.x;
y = m.y;
dir = m.dir;
if (game->move % 2 != side)
return E_NOT_YOUR_TURN;
if (!movable(game, x, y, side))
return E_ILLEGAL_MOVE;
/* dir =
@ -172,80 +202,146 @@ move(game_t *game, int side, int row, int col, int dir, int flags) {
*/
assert((dir>=0) || (dir<=7));
int nrow = row - (dir<3) + ((dir>3) && (dir<7));
int ncol = col + ((dir>1) && (dir<5)) - ((dir<1) || (dir>5));
if ((ncol<0) || (ncol>9) || (nrow<0) || (nrow>7))
move_dest(m, &nx, &ny);
if ((nx<0) || (nx>9) || (ny<0) || (ny>7))
return E_ILLEGAL_MOVE;
if (flags & F_SPLIT) {
if (game->board[ncol][nrow].piece || (game->board[col][row].piece != OBELISK2))
if (m.type == M_MOVE_SPLIT) {
if (game->board[nx][ny].piece || (game->board[x][y].piece != OBELISK2))
return E_ILLEGAL_MOVE;
game->board[col][row].piece = OBELISK;
game->board[ncol][nrow].piece = OBELISK;
game->board[ncol][nrow].side = game->board[col][row].side;
game->board[x][y].piece = OBELISK;
game->board[nx][ny].piece = OBELISK;
game->board[nx][ny].side = game->board[x][y].side;
return HUGE_SUCCESS;
}
if (!square_allowed(ncol, nrow, side))
if (!square_allowed(nx, ny, side))
return E_ILLEGAL_MOVE;
if (game->board[ncol][nrow].piece &&
!((game->board[col][row].piece == DJHED) &&
((game->board[ncol][nrow].piece != DJHED) &&
(game->board[ncol][nrow].piece != PHARAO))))
if (game->board[nx][ny].piece &&
!((game->board[x][y].piece == DJHED) &&
((game->board[nx][ny].piece != DJHED) &&
(game->board[nx][ny].piece != PHARAO))))
return E_ILLEGAL_MOVE;
struct square tmp;
tmp = game->board[ncol][nrow];
game->board[ncol][nrow] = game->board[col][row];
game->board[col][row] = tmp;
tmp = game->board[nx][ny];
game->board[nx][ny] = game->board[x][y];
game->board[x][y] = tmp;
game->move++;
return HUGE_SUCCESS;
}
void
place(game_t *game, int row, int col, int piece, int side, int dir)
place(game_t *game, int x, int y, int piece, int side, int dir)
{
if (game->board[col][row].piece == OBELISK &&
game->board[col][row].side == side &&
if (game->board[x][y].piece == OBELISK &&
game->board[x][y].side == side &&
piece == OBELISK) {
piece = OBELISK2;
} else if (game->board[col][row].piece != NONE &&
} else if (game->board[x][y].piece != NONE &&
piece != NONE) {
fprintf(stderr, "error: trying to place a piece on a non-empty square\n");
exit(EXIT_FAILURE);
}
game->board[col][row].piece = piece;
game->board[col][row].side = side;
game->board[col][row].dir = dir;
game->board[x][y].piece = piece;
game->board[x][y].side = side;
game->board[x][y].dir = dir;
}
int
perform_move(game_t *game, int side, move_t m)
{
int retval;
switch (m.type) {
case M_MOVE:
case M_MOVE_SPLIT:
retval = move(game, side, m);
break;
case M_ROTATE:
retval = rotate(game, side, m.x, m.y, m.dir);
break;
default:
assert(0);
return 0;
}
if (retval == HUGE_SUCCESS) {
laser(game, side);
game->last_move = m;
}
return retval;
}
int
parse_move(char *buf, move_t *m)
{
if (strlen(buf) < 6)
return E_FAILURE;
switch (buf[0]) {
case 'M': m->type = M_MOVE; break;
case 'R': m->type = M_ROTATE; break;
default: return E_FAILURE;
}
m->y = buf[2]-'A';
m->x = buf[3]-'0';
if (m->type == M_MOVE) {
switch (buf[5]) {
case 'N':
m->dir = (buf[6]=='W' ? D_NW : (buf[6]=='E' ? D_NE : D_N));
break;
case 'S':
m->dir = (buf[6]=='W' ? D_SW : (buf[6]=='E' ? D_SE : D_S));
break;
case 'E':
m->dir = D_E;
break;
case 'W':
m->dir = D_W;
break;
default:
return E_FAILURE;
}
} else if (m->type == M_ROTATE) {
m->dir = (buf[5]=='L' ? R_CCW : R_CW);
}
return HUGE_SUCCESS;
}
void
clear_board(game_t *game)
{
int i, j;
for (i = 0; i < BOARD_HEIGHT; i++)
for (j = 0; j < BOARD_WIDTH; j++)
place(game, i, j, 0, 0, 0);
int x, y;
for (y = 0; y < BOARD_HEIGHT; y++)
for (x = 0; x < BOARD_WIDTH; x++)
place(game, x, y, 0, 0, 0);
}
void
setup_board_place_piece(game_t *game, int piece, int side, char *place_desc)
{
int row, col, dir;
int x, y, dir;
row = place_desc[0]-'A';
col = place_desc[1]-'0';
y = place_desc[0]-'A';
x = place_desc[1]-'0';
dir = place_desc[2]-'0';
if (side == 1) {
row = BOARD_HEIGHT-row-1;
col = BOARD_WIDTH-col-1;
y = BOARD_HEIGHT-y-1;
x = BOARD_WIDTH-x-1;
dir = (dir+2)&3;
}
place(game, row, col, piece, side, dir);
place(game, x, y, piece, side, dir);
}
void

View File

@ -21,8 +21,14 @@
#define F_ROTATE 1
#define F_SPLIT 2
#define E_ILLEGAL_MOVE -1
#define E_UNKNOWN_COMMAND -2
#define M_MOVE 1
#define M_MOVE_SPLIT 2
#define M_ROTATE 3
#define E_FAILURE -1
#define E_ILLEGAL_MOVE -2
#define E_UNKNOWN_COMMAND -3
#define E_NOT_YOUR_TURN -4
#define HUGE_SUCCESS 1
// return values for laser():
@ -31,18 +37,28 @@
#define SILVER_WINS 2
#define RED_WINS 3
typedef enum { R_CW, R_CCW } rot_t;
typedef enum { D_NW=0, D_N, D_NE, D_E, D_SE, D_S, D_SW, D_W } dir_t;
typedef struct square {
unsigned int piece : 4;
unsigned int side : 1;
unsigned int dir : 2;
} square_t;
typedef struct {
int type;
int x, y;
int dir;
} move_t;
typedef struct {
int move;
int winner;
square_t board[BOARD_WIDTH][BOARD_HEIGHT];
int laser_pos[2];
square_t last_hit;
move_t last_move;
} game_t;
typedef struct {
@ -55,21 +71,24 @@ typedef struct {
char *name;
} sideinfo_t;
typedef enum { R_CW, R_CCW } rot_t;
typedef enum { D_NW=0, D_N, D_NE, D_E, D_SE, D_S, D_SW, D_W } dir_t;
extern char classic_setup[];
extern sideinfo_t sideinfo[];
extern pieceinfo_t pieceinfo[];
int game_turn(game_t *game);
int laser_at(game_t *game, int x, int y);
int laser(game_t *game, int side);
int movable(game_t *game, int row, int col, int side);
int rotate(game_t *game, int side, int row, int col, rot_t dir);
int move(game_t *game, int side, int row, int col, int dir, int flags);
int move(game_t *game, int side, move_t m);
void move_dest(move_t m, int *nx, int *ny);
void place(game_t *game, int row, int col, int piece, int side, int dir);
void setup_board(game_t *game, char *desc);
void init_game(game_t *game);
int parse_move(char *buf, move_t *m);
int perform_move(game_t *game, int side, move_t m);
#endif

View File

@ -10,10 +10,11 @@
#include "asciiart.h"
#define MAX_MSG_LEN 50
typedef enum { MSG_MOVE, MSG_ROTATE } move_t;
/*
typedef enum { MSG_MOVE, MSG_ROTATE } message_type_t;
typedef struct {
move_t type;
message_type_t type;
union {
struct {
int row, col;
@ -26,75 +27,6 @@ typedef struct {
} args;
} message_t;
// FIXME rewrite this to format the board in a char *?
/*
void
print_board_small()
{
int x, y;
char vline, hline, xline, lline;
char dead_piece[] = "##";
vline = '|', hline = '-', xline = '+';
lline = '#';
net_all_printf("%s(%c)\n", sideinfo[RED].name, sideinfo[RED].ch);
// column numbers
net_all_printf(" %c", vline);
for (x = 0; x < BOARD_WIDTH; x++) {
net_all_printf(" %d%c", x, vline);
}
net_all_printf("\n");
// line
net_all_printf("%c%c%c", hline, hline, xline);
for (x = 0; x < BOARD_WIDTH; x++)
net_all_printf("%c%c%c",
laser_at(x, -1) ? lline : hline,
laser_at(x, -1) ? lline : hline,
xline);
net_all_printf("%c%c\n", hline, hline);
for (y = 0; y < BOARD_HEIGHT; y++) {
// row name
net_all_printf(" %c%c", 'A'+y, laser_at(-1,y) ? lline : vline);
// row
for (x = 0; x < BOARD_WIDTH; x++) {
if (laser_at(x, y))
net_all_printf("%s", dead_piece);
else
print_piece_small(board[x][y].piece,
board[x][y].side,
board[x][y].dir);
net_all_printf("%c", (x==BOARD_WIDTH-1 && laser_at(x+1, y)) ? lline : vline);
}
// row name
net_all_printf("%c\n", 'A'+y);
// line
net_all_printf("%c%c%c", hline, hline, xline);
for (x = 0; x < BOARD_WIDTH; x++)
net_all_printf("%c%c%c",
(y==BOARD_HEIGHT-1 && laser_at(x, y+1)) ? lline : hline,
(y==BOARD_HEIGHT-1 && laser_at(x, y+1)) ? lline : hline,
xline);
net_all_printf("%c%c\n", hline, hline);
}
// column numbers
net_all_printf(" %c", vline);
for (x = 0; x < BOARD_WIDTH; x++)
net_all_printf(" %d%c", x, vline);
net_all_printf("\n");
for (x = 0; x < (BOARD_WIDTH+2)*3-1-strlen(sideinfo[SILVER].name)-3; x++)
net_all_printf(" ");
net_all_printf("%s(%c)\n", sideinfo[SILVER].name, sideinfo[SILVER].ch);
}
*/
int
read_message(int side, message_t *m)
{
@ -163,6 +95,90 @@ execute_message(game_t *game, message_t msg, int side)
return E_UNKNOWN_COMMAND;
}
}
*/
void
terminal_game(void)
{
game_t game;
move_t move;
char aabuf[ASCIIART_SMALL_BUFSZ];
char linebuf[10];
init_game(&game);
setup_board(&game, classic_setup);
asciiart_small_draw_board(aabuf, &game);
printf("%s", aabuf);
for (;;) {
printf("%s's move\n", sideinfo[game_turn(&game)].name);
fgets(linebuf, 10, stdin);
if (parse_move(linebuf, &move) != HUGE_SUCCESS) {
printf("not a valid move description: '%s'\n", linebuf);
continue;
}
if (perform_move(&game, game_turn(&game), move) != HUGE_SUCCESS) {
printf("invalid move\n");
continue;
}
if (game.last_move.type == M_ROTATE) {
printf("move: %c%c rotated %s\n",
game.last_move.y+'A',
game.last_move.x+'0',
game.last_move.dir==R_CW ? "R" : "L");
} else {
int nx, ny;
move_dest(game.last_move, &nx, &ny);
printf("move: %c%c moved to %c%c\n",
game.last_move.y+'A',
game.last_move.x+'0',
ny+'A', nx+'0');
}
if (game.last_hit.piece != NONE) {
printf("laser hits %s %s at %c%c\n",
sideinfo[game.last_hit.side].name,
pieceinfo[game.last_hit.piece].name,
game.laser_pos[1]+'A', game.laser_pos[0]+'0');
} else {
printf("laser hits wall at %c%c\n",
game.laser_pos[1]+'A', game.laser_pos[0]+'0');
}
asciiart_small_draw_board(aabuf, &game);
printf("%s", aabuf);
switch (game.winner) {
case SILVER:
printf("SILVER WINS!\n");
return;
case RED:
printf("RED WINS!\n");
return;
}
}
}
int
read_move(int side, move_t *m)
{
char buf[MAX_MSG_LEN+1];
char c;
int i;
for (i = 0; i < MAX_MSG_LEN+1; i++) {
c = net_getc(side);
printf("input '%c'\n", c);
if (c == '\n')
break;
buf[i] = c;
}
buf[i] = '\0';
while (c != '\n') c = net_getc(side);
return parse_move(buf, m);
}
void
simple_game(void)
@ -179,7 +195,7 @@ simple_game(void)
//print_board_small();
for (;;) {
message_t msg;
move_t m;
int r;
net_all_printf("%s's move\n", sideinfo[side].name);
@ -187,59 +203,40 @@ simple_game(void)
net_side_printf(side, "> ");
net_all_flush();
r = read_message(side, &msg);
switch (r) {
case HUGE_SUCCESS: break;
case E_UNKNOWN_COMMAND:
r = read_move(side, &m);
if (r != HUGE_SUCCESS) {
net_side_printf(side, "unknown command\n");
goto prompt;
default:
net_side_printf(side, "uh oh (strange return value from read_message: %d)\n", r);
goto prompt;
}
r = execute_message(&game, msg, side);
switch (r) {
case HUGE_SUCCESS:
//print_board_small();
asciiart_small_draw_board(aabuf, &game);
net_all_printf("%s", aabuf);
r = laser(&game, side);
//print_board_small();
asciiart_small_draw_board(aabuf, &game);
net_all_printf("%s", aabuf);
switch (r) {
case SILVER_WINS:
net_all_printf("SILVER WINS!\n");
return;
case RED_WINS:
net_all_printf("RED WINS!\n");
return;
case L_WALL:
net_all_printf("laser hits wall at %c%c\n",
game.laser_pos[1]+'A', game.laser_pos[0]+'0');
break;
case L_PIECE:
net_all_printf("laser hits %s %s at %c%c\n",
sideinfo[game.last_hit.side].name,
pieceinfo[game.last_hit.piece].name,
game.laser_pos[1]+'A', game.laser_pos[0]+'0');
break;
}
side = 1-side;
break;
case E_ILLEGAL_MOVE:
if (perform_move(&game, side, m) != HUGE_SUCCESS) {
net_side_printf(side, "illegal move\n");
goto prompt;
default:
net_side_printf(side, "uh oh (strange return value from execute_message: %d)\n", r);
goto prompt;
}
asciiart_small_draw_board(aabuf, &game);
net_all_printf("%s", aabuf);
switch (game.winner) {
case SILVER:
net_all_printf("SILVER WINS!\n");
return;
case RED:
net_all_printf("RED WINS!\n");
return;
}
side = game.move % 2;
}
}
int
main() {
main(int argc, char **argv) {
if (argc > 1 && strcmp(argv[1], "local")==0) {
terminal_game();
return 0;
}
net_server(15239);
simple_game();
return 0;
}