2020-06-20 12:26:54 -04:00
|
|
|
/*
|
|
|
|
.-.
|
|
|
|
| '
|
|
|
|
'._.HECKERS
|
|
|
|
|
|
|
|
Authored by abakh <abakh@tuta.io>
|
2021-04-23 09:39:03 -04:00
|
|
|
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
|
2020-06-20 12:26:54 -04:00
|
|
|
|
2021-04-23 09:39:03 -04:00
|
|
|
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
2020-06-20 12:26:54 -04:00
|
|
|
*/
|
|
|
|
|
2019-03-20 10:16:33 -04:00
|
|
|
#include <curses.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <math.h>
|
2020-06-06 02:14:56 -04:00
|
|
|
#include <stdbool.h>
|
2020-09-12 06:38:45 -04:00
|
|
|
#include "config.h"
|
2019-03-20 10:16:33 -04:00
|
|
|
#define LIGHT -1
|
|
|
|
#define DARK 1
|
|
|
|
#define KING 2
|
|
|
|
#define DOESNT_MATTER 1
|
|
|
|
#define IMAGINARY 0
|
|
|
|
#define NORMAL 1
|
|
|
|
#define ALT_IMG 2
|
|
|
|
#define ALT_NRM 3
|
|
|
|
#define WIN 100000
|
|
|
|
typedef signed char byte;
|
2020-06-20 12:26:54 -04:00
|
|
|
|
2019-03-20 10:16:33 -04:00
|
|
|
byte py,px;//cursor
|
|
|
|
byte cy,cx;//selected(choosen) piece
|
|
|
|
int dpt;
|
|
|
|
byte game[8][8];
|
|
|
|
byte computer[2]={0,0};
|
|
|
|
byte score[2];//set by header()
|
|
|
|
bool endgame=false;
|
|
|
|
byte jumpagainy , jumpagainx;
|
|
|
|
bool kinged;//if a piece jumps over multiple others and becomes a king it cannot continue jumping
|
|
|
|
|
|
|
|
bool in(byte A[4],byte B[4],byte a,byte b){
|
2020-06-20 12:26:54 -04:00
|
|
|
for(byte c=0;c<4;++c)
|
2020-05-18 14:59:29 -04:00
|
|
|
if(A[c]==a && B[c]==b)
|
|
|
|
return true;
|
|
|
|
return false;
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
void rectangle(byte sy,byte sx){
|
|
|
|
byte y,x;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<=8+1;++y){
|
2019-03-20 10:16:33 -04:00
|
|
|
mvaddch(sy+y,sx,ACS_VLINE);
|
|
|
|
mvaddch(sy+y,sx+8*2,ACS_VLINE);
|
|
|
|
}
|
2020-06-20 12:26:54 -04:00
|
|
|
for(x=0;x<=8*2;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
mvaddch(sy,sx+x,ACS_HLINE);
|
|
|
|
mvaddch(sy+8+1,sx+x,ACS_HLINE);
|
|
|
|
}
|
|
|
|
mvaddch(sy,sx,ACS_ULCORNER);
|
|
|
|
mvaddch(sy+8+1,sx,ACS_LLCORNER);
|
|
|
|
mvaddch(sy,sx+8*2,ACS_URCORNER);
|
|
|
|
mvaddch(sy+8+1,sx+8*2,ACS_LRCORNER);
|
|
|
|
}
|
|
|
|
void header(void){
|
|
|
|
score[0]=score[1]=0;
|
|
|
|
byte y,x;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
if(game[y][x]){
|
|
|
|
if(game[y][x]<0)
|
|
|
|
score[0]++;
|
|
|
|
else
|
|
|
|
score[1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mvprintw(0,0," .-.");
|
|
|
|
mvprintw(1,0,"| ' %2d:%2d",score[0],score[1]);
|
|
|
|
mvprintw(2,0,"'._,HECKERS ");
|
|
|
|
}
|
|
|
|
void draw(byte sy,byte sx){//the game's board
|
|
|
|
rectangle(sy,sx);
|
|
|
|
chtype ch ;
|
|
|
|
byte y,x;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
ch=A_NORMAL;
|
|
|
|
if(y==py && x==px)
|
|
|
|
ch |= A_STANDOUT;
|
|
|
|
if(y==cy && x==cx)
|
|
|
|
ch |= A_BOLD;
|
|
|
|
if(game[y][x]){
|
|
|
|
if(game[y][x]<0){
|
|
|
|
if(has_colors())
|
|
|
|
ch|=COLOR_PAIR(1);
|
|
|
|
else
|
|
|
|
ch |= A_UNDERLINE;
|
|
|
|
}
|
|
|
|
if(abs(game[y][x])<2)
|
|
|
|
ch |='O';
|
|
|
|
else
|
|
|
|
ch |='K';
|
|
|
|
}
|
2020-05-18 14:59:29 -04:00
|
|
|
else if( (y%2) != (x%2) )
|
2019-03-20 10:16:33 -04:00
|
|
|
ch|='.';
|
|
|
|
else
|
|
|
|
ch|=' ';
|
|
|
|
mvaddch(sy+1+y,sx+x*2+1,ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//place the pieces on the board
|
|
|
|
void fill(void){
|
|
|
|
byte y,x;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
game[y][x]=0;
|
|
|
|
if( (y%2) != (x%2)){
|
|
|
|
if(y<3) game[y][x]=1;
|
|
|
|
if(y>4) game[y][x]=-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//fill mvy/x with possible moves
|
|
|
|
bool moves(byte ty,byte tx,byte mvy[4],byte mvx[4]){
|
|
|
|
bool ret=0;
|
|
|
|
byte ndx=0;
|
|
|
|
byte t= game[ty][tx];
|
|
|
|
move(15,0);
|
|
|
|
byte dy,dx;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(dy=-1;dy<2;++dy){
|
|
|
|
for(dx=-1;dx<2;++dx){
|
2019-03-20 10:16:33 -04:00
|
|
|
if( !dy || !dx || (!ty && dy<0) || (!tx && dx<0) || (dy==-t) || (ty+dy>=8) || (tx+dx>=8) )
|
|
|
|
;
|
|
|
|
else if(!game[ty+dy][tx+dx]){
|
|
|
|
ret=1;
|
|
|
|
mvy[ndx]=ty+dy;
|
|
|
|
mvx[ndx]=tx+dx;
|
2020-06-20 12:26:54 -04:00
|
|
|
++ndx;
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
else
|
2020-06-20 12:26:54 -04:00
|
|
|
++ndx;
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//would be much faster than applying moves() on every tile
|
|
|
|
bool can_move(byte side){
|
|
|
|
byte y , x ,t, dy , dx;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
if( (t=game[y][x])*side > 0 ){
|
2020-06-20 12:26:54 -04:00
|
|
|
for(dy=-1;dy<2;++dy){
|
|
|
|
for(dx=-1;dx<2;++dx){
|
2019-03-20 10:16:33 -04:00
|
|
|
if( !dy || !dx || (!y && dy<0) || (!x && dx<0) || (dy==-t) || (y+dy>=8) || (x+dx>=8) )
|
|
|
|
;
|
|
|
|
else if( !game[y+dy][x+dx] )
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
//fill mvy/x with possible jumping moves
|
|
|
|
bool jumps(byte ty,byte tx,byte mvy[4],byte mvx[4]){
|
|
|
|
bool ret=0;
|
|
|
|
byte ndx=0;
|
|
|
|
byte ey,ex;
|
|
|
|
byte t= game[ty][tx];
|
|
|
|
byte dy,dx;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(dy=-1;dy<2;++dy){
|
|
|
|
for(dx=-1;dx<2;++dx){
|
2019-03-20 10:16:33 -04:00
|
|
|
ey = dy*2;
|
|
|
|
ex = dx*2;
|
|
|
|
if(!dy || !dx ||(dy==-t)|| (ty+ey<0) || (tx+ex<0) || (ty+ey>=8) || (tx+ex>=8) )
|
|
|
|
;
|
|
|
|
else if(!game[ty+ey][tx+ex] && game[ty+dy][tx+dx]*t<0){
|
|
|
|
ret=1;
|
|
|
|
mvy[ndx]=ty+ey;
|
|
|
|
mvx[ndx]=tx+ex;
|
2020-06-20 12:26:54 -04:00
|
|
|
++ndx;
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
else
|
2020-06-20 12:26:54 -04:00
|
|
|
++ndx;
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//same as can_move for jumps
|
|
|
|
byte can_jump(byte ty,byte tx){
|
|
|
|
byte dy,dx,t=game[ty][tx];
|
|
|
|
byte ey,ex;
|
|
|
|
byte ret=0;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(dy=-1;dy<2;++dy){
|
|
|
|
for(dx=-1;dx<2;++dx){
|
2019-03-20 10:16:33 -04:00
|
|
|
ey=dy*2;
|
|
|
|
ex=dx*2;
|
|
|
|
if((dy==-t)||(ty+ey<0)||(tx+ex<0)||(ty+ey>=8)||(tx+ex>=8) )
|
|
|
|
;
|
|
|
|
else if(!game[ty+dy*2][tx+dx*2]&&game[ty+dy][tx+dx]*t<0){
|
2020-06-20 12:26:54 -04:00
|
|
|
++ret;
|
2019-03-20 10:16:33 -04:00
|
|
|
if(ret>1)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//see if the side is forced to do a jump
|
|
|
|
byte forced_jump(byte side){
|
|
|
|
byte y,x;
|
|
|
|
byte foo,ret;
|
|
|
|
foo=ret=0;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
if(game[y][x]*side>0 && (foo=can_jump(y,x)) )
|
|
|
|
ret+=foo;
|
|
|
|
if(ret>1)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
byte cmove(byte fy,byte fx,byte sy,byte sx){//really move/jump , 'move' is a curses function
|
|
|
|
byte a = game[fy][fx];
|
|
|
|
byte ret=0;
|
|
|
|
game[fy][fx]=0;
|
|
|
|
game[sy][sx]=a;
|
|
|
|
if(abs(fy-sy) == 2){
|
|
|
|
ret =game[(fy+sy)/2][(fx+sx)/2];
|
|
|
|
game[(fy+sy)/2][(fx+sx)/2]=0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//make the pawn a king
|
|
|
|
bool king(byte y,byte x){
|
|
|
|
byte t= (4-y)*game[y][x];
|
|
|
|
if( (y==7 || !y) && t<0 && t>-5 ){
|
|
|
|
game[y][x]*=2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
double advantage(byte side){
|
|
|
|
unsigned char own,opp;
|
|
|
|
own=opp=0;
|
|
|
|
byte foo;
|
|
|
|
byte y,x;
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
foo=game[y][x]*side;
|
|
|
|
if(foo>0){
|
2020-06-20 12:26:54 -04:00
|
|
|
++own;//so it wont sacrfice two pawns for a king ( 2 kings == 3 pawns)
|
2019-03-20 10:16:33 -04:00
|
|
|
own+=foo;
|
|
|
|
}
|
|
|
|
else if(foo<0){
|
2020-06-20 12:26:54 -04:00
|
|
|
++opp;
|
2019-03-20 10:16:33 -04:00
|
|
|
opp-=foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!own)
|
|
|
|
return 0;
|
|
|
|
else if(!opp)
|
|
|
|
return WIN;
|
|
|
|
else
|
|
|
|
return (double)own/opp;
|
|
|
|
}
|
|
|
|
double posadvantage(byte side){
|
|
|
|
double adv=0;
|
|
|
|
double oppadv=0;
|
|
|
|
byte foo;
|
|
|
|
byte y,x;
|
|
|
|
byte goal= (side>0)*7 , oppgoal=(side<0)*7;
|
|
|
|
/*This encourages the AI to king its pawns and concentrate its kings in the center.
|
|
|
|
The idea is : With forces concentrated in the center, movements to all of the board would be in the game tree's horizon of sight(given enough depth);
|
|
|
|
and with forces being focused , its takes less movements to make an attack. */
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
foo=game[y][x]*side;
|
|
|
|
if(foo>0){
|
|
|
|
adv+=foo;
|
2020-06-20 12:26:54 -04:00
|
|
|
++adv;
|
2019-03-20 10:16:33 -04:00
|
|
|
if(foo==1)
|
|
|
|
adv+= 1/( abs(y-goal) );//adding positional value
|
|
|
|
else if(foo==2)
|
|
|
|
adv+= 1/( fabs(y-3.5)+ fabs(x-3.5) );
|
|
|
|
}
|
|
|
|
else if( foo<0 ){
|
|
|
|
oppadv-=foo;
|
2020-06-20 12:26:54 -04:00
|
|
|
++oppadv;
|
2019-03-20 10:16:33 -04:00
|
|
|
if(foo==-1)
|
|
|
|
adv+=1/( abs(y-oppgoal) );
|
|
|
|
else if(foo==-2)
|
|
|
|
adv+= 1/( fabs(y-3.5)+ fabs(x-3.5) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!adv)
|
|
|
|
return 0;
|
|
|
|
else if( !oppadv )
|
|
|
|
return WIN;
|
|
|
|
else
|
|
|
|
return adv/oppadv;
|
|
|
|
return adv;
|
|
|
|
}
|
|
|
|
//the AI algorithm
|
|
|
|
double decide(byte side,byte depth,byte s){//s is the type of move, it doesn't stand for anything
|
|
|
|
byte fj=forced_jump(side);//only one legal jump if returns 1
|
|
|
|
byte nextturn;
|
|
|
|
|
|
|
|
byte mvy[4],mvx[4];
|
|
|
|
byte n;
|
|
|
|
|
|
|
|
bool didking;
|
|
|
|
byte captured;
|
|
|
|
|
|
|
|
double adv=0;
|
|
|
|
byte toy,tox;
|
|
|
|
byte y,x;
|
|
|
|
|
|
|
|
double wrstadv=WIN+1;
|
|
|
|
|
|
|
|
double bestadv=0;
|
|
|
|
byte besttoy,besttox;
|
|
|
|
byte besty,bestx;
|
|
|
|
bestx=besty=besttox=besttoy=-100;
|
|
|
|
bool canmove=0;
|
|
|
|
|
|
|
|
byte nexts ;
|
|
|
|
if(s == IMAGINARY || s == NORMAL )
|
|
|
|
nexts=IMAGINARY;
|
|
|
|
else
|
|
|
|
nexts=ALT_IMG;
|
|
|
|
|
2020-06-20 12:26:54 -04:00
|
|
|
for(y=0;y<8;++y){
|
|
|
|
for(x=0;x<8;++x){
|
2019-03-20 10:16:33 -04:00
|
|
|
if(fj && (s==NORMAL || s==ALT_NRM) && jumpagainy>=0 && (jumpagainy!=y || jumpagainx!=x) )
|
|
|
|
continue;
|
|
|
|
if(game[y][x]*side>0){
|
|
|
|
canmove=0;
|
|
|
|
memset(mvy,-1,4);
|
|
|
|
memset(mvx,-1,4);
|
|
|
|
if(fj)
|
|
|
|
canmove=jumps(y,x,mvy,mvx);
|
|
|
|
else
|
|
|
|
canmove=moves(y,x,mvy,mvx);
|
|
|
|
if(canmove){
|
2020-06-20 12:26:54 -04:00
|
|
|
for(n=0;n<4;++n){
|
2019-03-20 10:16:33 -04:00
|
|
|
if(mvy[n] != -1){//a real move
|
|
|
|
toy=mvy[n];
|
|
|
|
tox=mvx[n];
|
|
|
|
captured=cmove(y,x,toy,tox);//do the imaginary move
|
|
|
|
if(fj && can_jump(toy,tox) ) //its a double jump
|
|
|
|
nextturn=side;
|
|
|
|
else
|
|
|
|
nextturn=-side;
|
|
|
|
didking=king(toy,tox);
|
|
|
|
|
|
|
|
//see the advantage you get
|
|
|
|
if(fj==1 && (s==ALT_NRM || s==NORMAL) )
|
|
|
|
adv= DOESNT_MATTER;//you have to do the move anyway
|
|
|
|
else if(!depth){
|
|
|
|
if(s==IMAGINARY || s==NORMAL)//calculating advantage only based on numerical superiority
|
|
|
|
adv=advantage(side);
|
|
|
|
else
|
|
|
|
adv=posadvantage(side);//taking to account the position of the pieces
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if(nextturn==side)
|
|
|
|
adv=decide(nextturn,depth,nexts);
|
|
|
|
else{ //best move is the one that gives least advantage to the opponet
|
|
|
|
adv=decide(nextturn,depth-!fj,nexts);
|
|
|
|
if(adv==WIN)
|
|
|
|
adv=0;
|
|
|
|
else if(adv)
|
|
|
|
adv=1/adv;
|
|
|
|
else
|
|
|
|
adv=WIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//undo the imaginary move
|
|
|
|
if(didking)
|
|
|
|
game[toy][tox]/=2;
|
|
|
|
game[y][x]=game[toy][tox];
|
|
|
|
game[toy][tox]=0;
|
|
|
|
if(fj)
|
|
|
|
game[(toy+y)/2][(tox+x)/2]=captured;
|
|
|
|
|
2019-07-30 14:13:10 -04:00
|
|
|
if(besty<0 || adv>bestadv || (adv==bestadv && ( rand()%2 )) ){
|
2019-03-20 10:16:33 -04:00
|
|
|
besty=y;
|
|
|
|
bestx=x;
|
|
|
|
besttoy=toy;
|
|
|
|
besttox=tox;
|
|
|
|
bestadv=adv;
|
|
|
|
}
|
|
|
|
if(adv<wrstadv)
|
|
|
|
wrstadv=adv;
|
|
|
|
if(fj == 1)
|
|
|
|
goto EndLoop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EndLoop:
|
|
|
|
if( (s==NORMAL || s==ALT_NRM) && besty >= 0 ){
|
|
|
|
if(endgame && fj!=1 && s==NORMAL && bestadv==wrstadv ){//the algorithm is not given enough depth to determine which move is better
|
|
|
|
if(wrstadv == WIN){//the randomization in the algorithm may cause an illusion of an inevitable win in several moves
|
|
|
|
if(depth > 1)
|
|
|
|
decide(side,depth-1,NORMAL);
|
|
|
|
else
|
|
|
|
goto Move;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
decide(side,depth,ALT_NRM);//change your opinion about what advantage means
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
Move:
|
|
|
|
cmove(besty,bestx,besttoy,besttox);
|
|
|
|
kinged=king(besttoy,besttox);
|
|
|
|
if(!kinged && can_jump(besttoy,besttox) ){
|
|
|
|
jumpagainy = besttoy;//so the next player (itself) can only continue the chain of jumps from there
|
|
|
|
jumpagainx = besttox;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
jumpagainy=jumpagainx=-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestadv;
|
|
|
|
}
|
|
|
|
//peacefully close when ^C is pressed
|
|
|
|
void sigint_handler(int x){
|
|
|
|
endwin();
|
|
|
|
puts("Quit.");
|
|
|
|
exit(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mouseinput(void){
|
2020-09-12 06:38:45 -04:00
|
|
|
#ifndef NO_MOUSE
|
2020-05-18 14:59:29 -04:00
|
|
|
MEVENT minput;
|
2019-07-30 15:50:57 -04:00
|
|
|
#ifdef PDCURSES
|
|
|
|
nc_getmouse(&minput);
|
|
|
|
#else
|
|
|
|
getmouse(&minput);
|
|
|
|
#endif
|
2020-05-18 14:59:29 -04:00
|
|
|
if( minput.y-4 <8 && minput.x-1<16){
|
|
|
|
py=minput.y-4;
|
|
|
|
px=(minput.x-1)/2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
if(minput.bstate & (BUTTON1_CLICKED|BUTTON1_PRESSED|BUTTON1_RELEASED) )
|
|
|
|
ungetch('\n');
|
2020-09-12 06:38:45 -04:00
|
|
|
#endif
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
void help(void){
|
2020-05-18 14:59:29 -04:00
|
|
|
erase();
|
2019-03-20 10:16:33 -04:00
|
|
|
header();
|
2020-05-18 14:59:29 -04:00
|
|
|
attron(A_BOLD);
|
|
|
|
mvprintw(3,0," **** THE CONTROLS ****");
|
|
|
|
mvprintw(9,0,"YOU CAN ALSO USE THE MOUSE!");
|
|
|
|
attroff(A_BOLD);
|
|
|
|
mvprintw(4,0,"RETURN/ENTER : Select or move the piece");
|
|
|
|
mvprintw(5,0,"SPACE : Flag/Unflag");
|
|
|
|
mvprintw(6,0,"hjkl/ARROW KEYS : Move cursor");
|
|
|
|
mvprintw(7,0,"q : quit");
|
2019-03-20 10:16:33 -04:00
|
|
|
mvprintw(8,0,"F1 & F2 : Help on controls & gameplay");
|
2020-05-18 14:59:29 -04:00
|
|
|
mvprintw(11,0,"Press a key to continue");
|
2019-03-20 10:16:33 -04:00
|
|
|
refresh();
|
2020-05-18 14:59:29 -04:00
|
|
|
getch();
|
2019-03-20 10:16:33 -04:00
|
|
|
erase();
|
|
|
|
}
|
|
|
|
void gameplay(void){
|
2020-05-18 14:59:29 -04:00
|
|
|
erase();
|
|
|
|
header();
|
|
|
|
attron(A_BOLD);
|
|
|
|
mvprintw(3,0," **** THE GAMEPLAY ****");
|
|
|
|
attroff(A_BOLD);
|
2019-03-20 10:16:33 -04:00
|
|
|
move(4,0);
|
|
|
|
printw("1) The game starts with each player having 12 men;\n");
|
|
|
|
printw(" men can only diagonally move forwards \n");
|
|
|
|
printw(" (toward the opponet's side).\n\n");
|
|
|
|
printw("2) Men can become kings by reaching the opponet's \n");
|
|
|
|
printw(" first rank; kings can diagonally move both forwards\n");
|
|
|
|
printw(" and backwards.\n\n");
|
|
|
|
printw("3) Pieces can capture opponet's pieces by jumping over them\n");
|
|
|
|
printw(" also they can capture several pieces at once by doing a\n");
|
|
|
|
printw(" chain of jumps.\n\n");
|
|
|
|
printw("4) You have to do a jump if you can.\n\n");
|
|
|
|
printw("5) A player wins when the opponet can't do a move e. g. \n");
|
|
|
|
printw(" all of their pieces are captured.\n\n");
|
2020-05-18 14:59:29 -04:00
|
|
|
refresh();
|
|
|
|
getch();
|
|
|
|
erase();
|
2019-03-20 10:16:33 -04:00
|
|
|
}
|
|
|
|
int main(int argc,char** argv){
|
|
|
|
dpt=4;
|
|
|
|
if(argc>2){
|
|
|
|
printf("Usage: %s [AIpower]\n",argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if(argc==2){
|
|
|
|
if(sscanf(argv[1],"%d",&dpt) && dpt<128 && dpt>0)
|
|
|
|
;
|
|
|
|
else{
|
|
|
|
puts("That should be a number from 1 to 127.");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initscr();
|
2020-09-12 06:38:45 -04:00
|
|
|
#ifndef NO_MOUSE
|
2019-03-20 10:16:33 -04:00
|
|
|
mousemask(ALL_MOUSE_EVENTS,NULL);
|
2020-09-12 06:38:45 -04:00
|
|
|
#endif
|
2019-03-20 10:16:33 -04:00
|
|
|
noecho();
|
|
|
|
cbreak();
|
|
|
|
keypad(stdscr,1);
|
|
|
|
int input ;
|
|
|
|
printw("Dark plays first.\nChoose type of the dark player(H/c)\n" );
|
2019-08-01 12:21:10 -04:00
|
|
|
refresh();
|
2019-03-20 10:16:33 -04:00
|
|
|
input=getch();
|
|
|
|
if(input=='c'){
|
|
|
|
computer[0]=dpt;
|
|
|
|
printw("Computer.\n");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
computer[0]=0;
|
|
|
|
printw("Human.\n");
|
|
|
|
}
|
2020-05-18 14:59:29 -04:00
|
|
|
printw("Choose type of the bright player(h/C)\n");
|
2019-08-01 12:21:10 -04:00
|
|
|
refresh();
|
2019-03-20 10:16:33 -04:00
|
|
|
input=getch();
|
|
|
|
if(input=='h'){
|
|
|
|
computer[1]=0;
|
|
|
|
printw("Human.\n");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
computer[1]=dpt;
|
|
|
|
printw("Computer.\n");
|
|
|
|
}
|
|
|
|
if(has_colors()){
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
|
|
|
init_pair(1,COLOR_RED,-1);
|
|
|
|
}
|
|
|
|
signal(SIGINT,sigint_handler);
|
|
|
|
Start:
|
2019-07-30 14:13:10 -04:00
|
|
|
srand(time(NULL)%UINT_MAX);
|
2019-03-20 10:16:33 -04:00
|
|
|
fill();
|
|
|
|
cy=cx=-1;
|
|
|
|
py=px=0;
|
|
|
|
byte mvy[4],mvx[4];
|
|
|
|
memset(mvy,-1,4);
|
|
|
|
memset(mvx,-1,4);
|
|
|
|
byte turn=1;
|
|
|
|
bool t=1;
|
|
|
|
bool fj;
|
|
|
|
byte result;
|
|
|
|
byte todraw=0;
|
|
|
|
double adv = 1;//used to determine when the game is a draw
|
|
|
|
double previousadv =1;
|
|
|
|
Turn:
|
2019-05-13 09:58:19 -04:00
|
|
|
curs_set(0);
|
2019-03-20 10:16:33 -04:00
|
|
|
jumpagainy=jumpagainx=-1;
|
|
|
|
kinged=0;
|
|
|
|
turn =-turn;
|
|
|
|
t=!t;//t == turn<0 that's turn in binary/array index format
|
|
|
|
fj = forced_jump(turn);
|
|
|
|
erase();
|
|
|
|
flushinp();
|
|
|
|
header();
|
|
|
|
draw(3,0);
|
|
|
|
if(t){
|
|
|
|
previousadv=adv;
|
|
|
|
adv= advantage(1) + (score[0]*score[1]);//just taking the dry scores to account too,nothing special
|
|
|
|
if(previousadv==adv)
|
2020-06-20 12:26:54 -04:00
|
|
|
++todraw;
|
2019-03-20 10:16:33 -04:00
|
|
|
else
|
|
|
|
todraw=0;
|
|
|
|
}
|
|
|
|
if(!score[0] || (turn==-1 && !fj && !can_move(-1))){
|
|
|
|
result=1;
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
else if(!score[1] || (turn==1 && !fj && !can_move(1))){
|
|
|
|
result=-1;
|
|
|
|
goto End;
|
|
|
|
}
|
2019-05-13 09:58:19 -04:00
|
|
|
else if(todraw==50){ // 50 turns without any gain for either side
|
2019-03-20 10:16:33 -04:00
|
|
|
result=0;
|
|
|
|
goto End;
|
|
|
|
}
|
|
|
|
endgame= score[t]<=5 || score[!t]<=5;
|
|
|
|
draw(3,0);
|
|
|
|
refresh();
|
|
|
|
while(computer[t]){
|
|
|
|
mvprintw(13,0,"Thinking...");
|
|
|
|
refresh();
|
|
|
|
computer[t]=dpt+ (score[!t] != score[t]) + endgame;
|
|
|
|
decide(turn,computer[t],1);
|
|
|
|
if(!(fj && jumpagainy>=0 && !kinged )){
|
|
|
|
goto Turn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while(1){
|
|
|
|
erase();
|
|
|
|
draw(3,0);
|
|
|
|
header();
|
2020-05-18 14:59:29 -04:00
|
|
|
if(!(computer[0]||computer[1])){
|
|
|
|
if(t)
|
|
|
|
addstr(" Bright's turn");
|
|
|
|
else{
|
|
|
|
attron(COLOR_PAIR(1));
|
|
|
|
addstr(" Dark's turn");
|
|
|
|
attroff(COLOR_PAIR(1));
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 10:16:33 -04:00
|
|
|
refresh();
|
|
|
|
input=getch();
|
|
|
|
if( input == KEY_F(1) || input=='?' )
|
|
|
|
help();
|
|
|
|
if( input == KEY_F(2) )
|
|
|
|
gameplay();
|
|
|
|
if( input == KEY_MOUSE )
|
|
|
|
mouseinput();
|
|
|
|
if( (input=='k' || input==KEY_UP) && py>0)
|
2020-06-20 12:26:54 -04:00
|
|
|
--py;
|
2019-03-20 10:16:33 -04:00
|
|
|
if( (input=='j' || input==KEY_DOWN) && py<7)
|
2020-06-20 12:26:54 -04:00
|
|
|
++py;
|
2019-03-20 10:16:33 -04:00
|
|
|
if( (input=='h' || input==KEY_LEFT) && px>0)
|
2020-06-20 12:26:54 -04:00
|
|
|
--px;
|
2019-03-20 10:16:33 -04:00
|
|
|
if( (input=='l' || input==KEY_RIGHT) && px<7)
|
2020-06-20 12:26:54 -04:00
|
|
|
++px;
|
2019-03-20 10:16:33 -04:00
|
|
|
if( input=='q'){
|
|
|
|
result=2;
|
|
|
|
goto End;
|
|
|
|
}
|
2020-07-09 06:24:25 -04:00
|
|
|
if(input=='\n' || input==KEY_ENTER){
|
2019-03-20 10:16:33 -04:00
|
|
|
if(game[py][px]*turn>0){
|
|
|
|
cy=py;
|
|
|
|
cx=px;
|
|
|
|
memset(mvy,-1,4);
|
|
|
|
memset(mvx,-1,4);
|
|
|
|
if(!fj)
|
|
|
|
moves(py,px,mvy,mvx);
|
|
|
|
jumps(py,px,mvy,mvx);
|
|
|
|
}
|
|
|
|
if( in(mvy,mvx,py,px) && !(jumpagainy>=0 && (cy !=jumpagainy || cx != jumpagainx) ) ){
|
|
|
|
memset(mvy,-1,4);
|
|
|
|
memset(mvx,-1,4);
|
|
|
|
cmove(cy,cx,py,px);
|
|
|
|
kinged=king(py,px);
|
|
|
|
cy=-1;
|
|
|
|
cx=-1;
|
|
|
|
if( !(fj && can_jump(py,px) && !kinged ) ){
|
|
|
|
goto Turn;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
jumpagainy=py;
|
|
|
|
jumpagainx=px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
End:
|
|
|
|
move(13,0);
|
|
|
|
switch(result){
|
|
|
|
case -1:
|
|
|
|
printw("The dark side has won the game.");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
printw("Draw.");
|
|
|
|
break;
|
|
|
|
case 1:
|
2020-05-18 14:59:29 -04:00
|
|
|
printw("The bright side has won the game.");
|
2019-03-20 10:16:33 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
printw("You resigned.");
|
|
|
|
}
|
|
|
|
printw(" Wanna rematch?(y/n)");
|
|
|
|
curs_set(1);
|
|
|
|
input=getch();
|
|
|
|
if(result==2){
|
|
|
|
if (input=='Y' || input=='y')
|
|
|
|
goto Start;
|
|
|
|
}
|
|
|
|
else if(input!='n' && input!='N' && input!= 'q'){
|
|
|
|
/*byte b=computer[0]; //switch sides, i don't know if it's necessary
|
|
|
|
computer[0]=computer[1];
|
|
|
|
computer[1]=b;*/
|
|
|
|
goto Start;
|
|
|
|
}
|
|
|
|
endwin();
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|