diff --git a/src/server.c b/src/server.c new file mode 100644 index 0000000..9834042 --- /dev/null +++ b/src/server.c @@ -0,0 +1,154 @@ + +#define NONE 0 +#define PHARAO 1 +#define DJHED 2 +#define PYRAMID 3 +#define OBELISK 4 +#define OBELISK2 5 + + +#define F_ROTATE 1 +#define F_SPLIT 2 + +struct square { + int piece : 4; + int side : 1; + int dir : 2; +} board[10][8]; + + +/* +struct pieceinfo { + char ascii[]; +} pieceinfo[] = { + { //NONE + .ascii = { ' ' } } + { //PHARAO + .ascii = { '=' } } + { //DJHED + .ascii = { '/', '\\' } } + { //PYRAMID + .ascii = { 'd', 'b', 'p', 'q' } } + { //OBELISK + .ascii = { 'o' } } + { //OBELISK2 + .ascii = { '@' } } +}; +*/ + +/* +void +print_board() { + for (int y=0; y<8; y++) + for (int x=0; x<10; x++) + ; +} +*/ + +int +laser(int side) { + int x, y, dir; + int ddir; + switch (side) { + case 0: + x=9; y=-1; dir=0; + break; + case 1: + x=0; y=8; dir=2; + break; + default: + return 0; + } + + for (;;) { + switch (dir & 3) { + case 0: y++; if (y>7) return 0; break; + case 1: x++; if (x>9) return 0; break; + case 2: y--; if (y<0) return 0; break; + case 3: x--; if (x<0) return 0; break; + } + switch (board[x][y].piece) { + case NONE: + break; + case PHARAO: + return 2 + board[x][y].side; /* GAME OVER */ + case OBELISK2: + board[x][y].piece = OBELISK; + return 1; + case OBELISK: + board[x][y].piece = NONE; + return 1; + case DJHED: + ddir = board[x][y].dir - dir; + dir=(dir + 1 - 2*(ddir & 1)) & 3; + break; + case PYRAMID: + ddir = board[x][y].dir - dir; + if (ddir & 2) { + board[x][y].piece = NONE; + return 1; + } + dir=(dir + 1 - 2*(ddir & 1)) & 3; + break; + } + } +} + +int +move(int side, int x, int y, int dir, int flags) { + if ((x<0) || (x>9) || (y<0) || (y>7)) + return 0; + if (!board[x][y].piece) + return 0; + if ((board[x][y].side != side)) + return 0; + + if (flags & F_ROTATE) { + if ( !((dir==1) || (dir==-1))) + return 0; + board[x][y].dir = (board[x][y].dir + dir) & 3; + return 1; + } + + /* dir = + 0 1 2 + 7 X 3 + 6 5 4 + */ + + if ((dir<0) || (dir>7)) + return 0; + int ny = y - (dir<3) + ((dir>3) && (dir<7)); + int nx = x + ((dir>1) && (dir<5)) - ((dir<1) || (dir>5)); + if ((nx<0) || (nx>9) || (ny<0) || (ny>7)) + return 0; + + + if (flags = F_SPLIT) { + if (board[nx][ny].piece || (board[x][y].piece != OBELISK2)) + return 0; + board[x][y].piece = OBELISK; + board[nx][ny].piece = OBELISK; + board[nx][ny].side = board[x][y].side; + return 1; + } + + if (board[nx][ny].piece && + !((board[x][y].piece == DJHED) && + ((board[nx][ny].piece != DJHED) && + (board[nx][ny].piece != PHARAO)))) + return 0; + + struct square tmp; + tmp = board[nx][ny]; + board[nx][ny] = board[x][y]; + board[x][y] = tmp; + + return 1; +} + + +int +main() { + +}