void main() { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); cleardevice(); king("ch1"); int Mstatus = initmouse(); int button,m,n; if(Mstatus == 0 ) outtextxy(50,50, "Mouse Support Not Available"); settextstyle(2,0,4); while(1) { showmouseptr(); getmousepos(&button,&m,&n); if(m>245 && m<325 && n>220 && n<232) { setcolor(RED); outtextxy(250,220,"PLAYER vs PLAYER"); } else { setcolor(WHITE); outtextxy(250,220,"PLAYER vs PLAYER"); if(m>245 && m<325 && n>240 && n<252) { setcolor(RED); outtextxy(250,240,"CHESS PUZZLES"); } else { setcolor(WHITE); outtextxy(250,240,"CHESS PUZZLES"); if(m>245 && m<325 && n>260 && n<272) { setcolor(RED); outtextxy(250,260,"BACK"); } else { setcolor(WHITE); outtextxy(250,260,"BACK"); }}} if(button == 1) { if(m>245 && m<325 && n>220 && n<232) { hidemouseptr(); video(450,150); createnewfile(); spawnl(P_WAIT,"load.exe", NULL); // load spawnl(P_WAIT,"chess.exe", NULL); // player vs player cleardevice(); king("ch1"); } if(m>245 && m<325 && n>240 && n<252) { hidemouseptr(); video(450,150); spawnl(P_WAIT,"menu6.exe", NULL); } // chess puzzles menu // exit(0); } if(m>245 && m<325 && n>260 && n<272) { hidemouseptr(); video(450,150); // spawnl(P_WAIT,"g:\\exe\\menu1.exe", NULL); // back to main menu break; }}}}
/* * Prints a string representation of the board. */ void printBoard(struct State *state) { int x,y; for (y=0; y<8; ++y) { for (x=0; x<8; ++x) { if (!empty(state->board[y][x])) { if (state->board[y][x] & White) { if (king(state->board[y][x])) { fprintf(stderr, "W"); } else { fprintf(stderr, "w"); } } else { if (king(state->board[y][x])) { fprintf(stderr, "R"); } else { fprintf(stderr, "r"); } } } else { if (x%2 == y%2) { fprintf(stderr, "â§"); } else { fprintf(stderr, "â¡"); } } } fprintf(stderr, "\n"); } }
bool blackCheck() { int x, y; for (x = 0; x < 8; x++) { for (y = 0; y < 8; y++) if (table[x][y] == 'k') break; if (table[x][y] == 'k') break; } int i, j; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) if (table[i][j] == 'P' && whitePawn(i, j, x, y)) return true; else if (table[i][j] == 'N' && knight(i, j, x, y)) return true; else if (table[i][j] == 'B' && bishop(i, j, x, y)) return true; else if (table[i][j] == 'R' && rook(i, j, x, y)) return true; else if (table[i][j] == 'Q' && queen(i, j, x, y)) return true; else if (table[i][j] == 'K' && king(i, j, x, y)) return true; return false; }
double evalBoard1(State *state) { int x,y; double rval = 0.0; evals++; for(x=0;x<8;x++) for(y=0;y<8;y++) { if(x%2 != y%2 && !empty(state->board[y][x])) { if(king(state->board[y][x])) { /* King */ if(((state->board[y][x] & White) && !player1) || (!(state->board[y][x] & White) && player1) ) rval+=2.0; else rval-=2.0; } else if(piece(state->board[y][x])) { /* Piece */ if(((state->board[y][x] & White) && !player1) || (!(state->board[y][x] & White) && player1) ) rval+=1.0; else rval-=1.0; } } } //PrintBoard(state->board); //fprintf(stderr,"Value = %g\n",rval); return rval; }
bool whiteCheck() { int x, y; for (x = 0; x < 8; x++) { for (y = 0; y < 8; y++) if (table[x][y] == 'K') break; if (table[x][y] == 'K') break; } int i, j; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) if (table[i][j] == 'p' && blackPawn(i, j, x, y)) return true; else if (table[i][j] == 'n' && knight(i, j, x, y)) return true; else if (table[i][j] == 'b' && bishop(i, j, x, y)) return true; else if (table[i][j] == 'r' && rook(i, j, x, y)) return true; else if (table[i][j] == 'q' && queen(i, j, x, y)) return true; else if (table[i][j] == 'k' && king(i, j, x, y)) return true; return false; }
// Parses Donna chess format string for one side. Besides [K]ing, [Q]ueen, [R]ook, // [B]ishop, and k[N]ight the following pseudo pieces could be specified: // // [M]ove: specifies the right to move along with the optional move number. // For example, "M42" for Black means the Black is making 42nd move. // Default value is "M1" for White. // // [C]astle: specifies castle right squares. For example, "Cg1" and "Cc8" encode // allowed kingside castle for White, and queenside castle for Black. // By default all castles are allowed, i.e. defult value is "Cc1,Cg1" // for White and "Cc8,Cg8" for Black. The actual castle rights are // checked during position setup to make sure they do not violate // chess rules. If castle rights are specified incorrectly they get // quietly ignored. // // [E]npassant: specifies en-passant square if any. For example, "Ed3" marks D3 // square as en-passant. Default value is no en-passant. // //------------------------------------------------------------------------------ Position *setup_side(Position *self, char *side, uint8 color) { char *buffer = (char *)calloc(strlen(side) + 1, 1); // strdup() the string so we could tokenize it. strcat(buffer, side); char *move = strtok(buffer, ","); while(move) { if (move[0] == 'M') { // TODO: parse move number. self->color = color; } else { int sq, size = strlen(move); assert(size == 2 || size == 3); // Don't panic. if (size == 2) { // Pawn, ex. "e4". sq = square(move[1] - '1', move[0] - 'a'); } else { // Piece, ex. "Qd1". sq = square(move[2] - '1', move[1] - 'a'); } switch(move[0]) { case 'K': self->pieces[sq] = king(color); break; case 'Q': self->pieces[sq] = queen(color); break; case 'R': self->pieces[sq] = rook(color); break; case 'B': self->pieces[sq] = bishop(color); break; case 'N': self->pieces[sq] = knight(color); break; case 'E': self->enpassant = (uint8)sq; break; case 'C': if (sq == C1 + (int)color || sq == C8 + (int)color) { self->castles |= castleQueenside[color]; } else if (sq == G1 + (int)color || sq == G8 + (int)color) { self->castles |= castleKingside[color]; } break; default: // When everything else fails, read the instructions. self->pieces[sq] = pawn(color); } } move = strtok(NULL, ","); // <-- Get next move token. } free(buffer); return self; }
void coins() // draw coins { for(int i=0;i<8;i++) for(int j=0;j<8;j++) { if(A[i][j].coin=='R') rook(A[i][j].x,A[i][j].y,A[i][j].color); if(A[i][j].coin=='H') knight(A[i][j].x,A[i][j].y,A[i][j].color); if(A[i][j].coin=='B') bishop(A[i][j].x,A[i][j].y,A[i][j].color); if(A[i][j].coin=='K') king(A[i][j].x,A[i][j].y,A[i][j].color); if(A[i][j].coin=='Q') queen(A[i][j].x,A[i][j].y,A[i][j].color); if(A[i][j].coin=='P') pawn(A[i][j].x,A[i][j].y,A[i][j].color); }}
int main(){ char s1[3],s2[3]; int a,b,c,d,i,j,n; scanf("%d",&n); for(;n;n--){ if(scanf("%s %s",s1,s2)<0)return 0; a=s1[0]-'a',b=s2[0]-'a'; c=s1[1]-'1',d=s2[1]-'1'; i=abs(a-b),j=abs(c-d); if(!i&&!j){puts("0 0 0 0");continue;} king(i,j); queen(i,j); luke(i,j); ele(i,j); } }
void learning_tools_unit_test_kkpp() { KKPP g_kkpp; g_kkpp.set(SQ_NB, 10000 , 0); u64 n = 0; for (int k = 0; k<SQ_NB; ++k) for (int i = 0; i<10000; ++i) // 試しに、かなり大きなfe_endを想定して10000で回してみる。 for (int j = 0; j < i; ++j) { auto kkpp = g_kkpp.fromKKPP(k, (BonaPiece)i, (BonaPiece)j); auto r = kkpp.toRawIndex(); ASSERT_LV3(n++ == r); auto kkpp2 = g_kkpp.fromIndex(r + g_kkpp.min_index()); ASSERT_LV3(kkpp2.king() == k && kkpp2.piece0() == i && kkpp2.piece1() == j); } }
AKQReduceStrategy(SComplex& _complex): DefaultReduceStrategy<SComplex>(_complex) { Stopwatch total; // std::cout << "Starting init\n"; maxExtractDim = _complex.getDim(); // std::cout << __LINE__ << " @ " << total << std::endl; extractDim = 0; int csize = _complex.maxId(); // std::cout << __LINE__ << " @ " << total << std::endl; //int csize = _complex.size(); // std::cout << __LINE__ << " @ " << total << std::endl; int card = distance(_complex.iterators(1).allCells().begin(), _complex.iterators(1).allCells().end()); morse.resize(csize, 0); kingsUsed = 0; { Cell king(_complex); kingPool.resize(card/2 + 10, king); // std::cout << __LINE__ << " [kingPool allocation] @ " << total << std::endl; } infosUsed = 0; { PathsInfo pi; pathsInfoPool.resize(card, pi); // std::cout << __LINE__ << " [pathsInfoPool allocation] @ " << total << std::endl; } dimIndexer = DimIndexerType(this->complex.iterators(1).allCells().begin(), this->complex.iterators(1).allCells().end(), maxExtractDim); std::cout << __LINE__ << " @ " << total << std::endl; extractIt = dimIndexer.begin(); std::cout << __LINE__ << " @ " << total << std::endl; extractEnd = dimIndexer.end(); std::cout << __LINE__ << " @ " << total << std::endl; followMemoTable.resize(csize, 0); std::cout << __LINE__ << " @ " << total << std::endl; akq.resize(csize); std::cout << __LINE__ << "[AKQ resize] @ " << total << std::endl; // kerKing.resize(csize, Cell(this->complex)); // std::cout << __LINE__ << " [herKing allocation] @ " << total << std::endl; kerKing.resize(csize, 0); std::cout << __LINE__ << " [herKing allocation] @ " << total << std::endl; // std::cout << "Init done in " << total << std::endl; }
/* Determines all of the legal moves possible for a given state */ int FindLegalMoves(struct State *state) { int x, y, i; char move[12], board[8][8]; memset(move,0,12*sizeof(char)); jumpptr = numLegalMoves = 0; memcpy(board,state->board,64*sizeof(char)); /* Loop through the board array, determining legal moves/jumps for each piece */ for(i=1; i<=32; ++i) { NumberToXY(i, &x, &y); if(color(board[y][x]) == state->player && !empty(board[y][x])) { if(king(board[y][x])) { /* King */ move[0] = number(board[y][x])+1; FindKingJump(state->player,board,move,1,x,y); if(!jumpptr) FindKingMoves(board,x,y); } else if(piece(board[y][x])) { /* Piece */ move[0] = number(board[y][x])+1; FindJump(state->player,board,move,1,x,y); if(!jumpptr) FindMoves(state->player,board,x,y); } } } if(jumpptr) { for(x=0; x<jumpptr; x++) for(y=0; y<12; y++) state->movelist[x][y] = jumplist[x][y]; state->numLegalMoves = jumpptr; } else { for(x=0; x<numLegalMoves; x++) for(y=0; y<12; y++) state->movelist[x][y] = movelist[x][y]; state->numLegalMoves = numLegalMoves; } return (jumpptr+numLegalMoves); }
//------------------------------------------------------------------------------ Score threats(uint8 color, Bitmask hisAttacks, Bitmask herAttacks) { Score score = {}; Position *p = eval.position; // Find weak enemy pieces: the ones under attack and not defended by // pawns (excluding a king). Bitmask weak = p->outposts[color^1] & hisAttacks & ~eval.attacks[pawn(color^1)]; weak &= ~p->outposts[king(color^1)]; if (weak) { // Threat bonus for strongest enemy piece attacked by our pawns, // knights, or bishops. Bitmask targets = weak & (eval.attacks[pawn(color)] | eval.attacks[knight(color)] | eval.attacks[bishop(color)]); if (targets) { Piece piece = strongest_piece(p, color^1, targets); score = add(score, bonusMinorThreat[kind(piece) / 2]); } // Threat bonus for strongest enemy piece attacked by our rooks // or queen. targets = weak & (eval.attacks[rook(color)] | eval.attacks[queen(color)]); if (targets) { Piece piece = strongest_piece(p, color^1, targets); score = add(score, bonusMajorThreat[kind(piece) / 2]); } // Extra bonus when attacking enemy pieces that are hanging. Side // having the right to move gets bigger bonus. int hanging = count(weak & ~herAttacks); if (hanging > 0) { if (p->color == color) { hanging++; } score = add(score, times(hangingAttack, hanging)); } } return score; }
double evalBoard2(State *currBoard) { int y,x; double score=0.0; evals++; for(y=0; y<8; y++) for(x=0; x<8; x++) { if(king(currBoard->board[y][x])) { //if(color(currBoard->board[y][x]) == White) score += 2.0; if(currBoard->board[y][x] & White) score += 2.0; else score -= 2.0; } else if(piece(currBoard->board[y][x])) { if(currBoard->board[y][x] & White) score += 1.0; else score -= 1.0; } } return currBoard->player==1 ? -score : score; }
//during the game //when u get the coordinates of the click void game(int x, int y){ MEVENT ev; bpos *m; bool a; m=malloc(sizeof(bpos)); int b,c,fx,fy; if(board[x][y]==0) { wrongstart(); } else { if(turn==0) { if(board[x][y]<0) { wrongstart(); } } else if(turn!=0){ if(board[x][y]>0){ wrongstart(); } } } //then the user should click in the final position and we need to find the corresponding box c = wgetch(chessboard); c = wgetch(chessboard); wrefresh(menuwin); switch(c) { case KEY_MOUSE: if(getmouse(&ev) == OK){ if(ev.bstate & BUTTON1_PRESSED){ fy=ev.y;fx=ev.x; m=position(fx,fy); fy=m->col; fx=m->row; if(!((m->row+1)*(m->col+1))){ mvwprintw(menuwin,10,10,"not valid click again "); wrefresh(menuwin);return; } if((turn==0)&&(board[fx][fy]<0)){ mvwprintw(menuwin,2,6,"choose num 2 "); } else if((turn!=0)&&(board[fx][fy]>0)){ mvwprintw(menuwin,2,6,"choose num1"); } wrefresh(menuwin); switch(abs(board[x][y])) { case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16: b=pawn(x,y,fx,fy); break; case 2: case 7: b=horse(x,y,fx,fy); break; case 4: b=queen(x,y,fx,fy); break; case 3:case 6:b=bishop(x,y,fx,fy); break; case 1:case 8: b=rook(x,y,fx,fy); break; case 5: b=king(x,y,fx,fy); break; } if(b==1){ if((turn==0)&&board[x][y]<0) {return;} else if((turn!=0)&&board[x][y]>0) {return;} if((turn==0)&&board[fx][fy]>0) {return;} else if((turn!=0)&&board[fx][fy]<0) {return;} if((board[fx][fy]>0)&&(turn!=0)){ karr[count]=board[fx][fy];count++;printelement(); } else if((board[fx][fy]<0)&&(turn==0)){ karr[count]=board[fx][fy];count++;printelement(); } wrefresh(menuwin);move1.prevval=board[fx][fy]; move1.newval=board[x][y]; movepiece(x,y,fx,fy);wrefresh(menuwin); move1.srow=x;move1.scol=y;move1.erow=fx;move1.ecol=fy; push(&s,move1); flipturn();check(); display_menu(); } } } } }
/* * Evaluates the board from the perspective of this AI player and returns a * score indicating the board's utility. * * Higher board scores are better boards for this player than lower ones. * * The board evaluation assigns a score to each player based on the number and * type of pieces it controls and the positions they occupy. The two scores are * then used to create a ratio of (AI_player / opponent). A score of 1.0 * means the players are evenly matched. Scores less than 1.0 mean a * disadvantage to the this AI player. Scores greater than 1.0 mean an * advantage to the AI player. * * @param state the board state being evaluated. * * @return the utility score for this board state from the AI player's * perspective. */ double evaluateBoard(struct State *state) { double score = 0.0; double red_score = 0.0; double white_score = 0.0; int i, x, y; for (i=1; i<=32; ++i) { NumberToXY(i, &x, &y); /* Only evaluate positions with pieces. */ if (empty(state->board[y][x])) continue; /* WHITE PIECE */ if (state->board[y][x] & White) { /* King or Pawn? */ if (king(state->board[y][x])) { white_score += 2.0; } else { white_score += 1.0; /* Is this pawn in enemy territory? */ if (y > 3) { white_score += 0.5; } } /* Next to safe edge? */ if (x==0 || x==7) { white_score += 0.25; } /* On main diagonal? Increases likelihood of jumping. */ if (x + y == 7) { white_score += 0.5; } } /* RED PIECE */ else { /* King or Pawn? */ if (king(state->board[y][x])) { red_score += 2.0; } else { red_score += 1.0; /* Is this pawn in enemy territory? */ if (y < 4) { red_score += 0.5; } } /* Next to safe edge? */ if (x==0 || x==7) { red_score += 0.25; } /* On main diagonal? Increases likelihood of jumping. */ if (x + y == 7) { red_score += 0.5; } } } /* If the other player has no pawns my score should be high. */ red_score += (me == 1 && white_score == 0) ? 50 : 0; white_score += (me == 2 && red_score == 0) ? 50 : 0; score = (me == 1) ? red_score / white_score : white_score / red_score; /* printBoard(state); fprintf(stderr, "Red Score = %f\n", red_score); fprintf(stderr, "White Score = %f\n", white_score); fprintf(stderr, "Board Score = %f\n\n", score); */ /* Return the score as is if we are player1 (Red) and negated otherwise. */ return score; }
void main() { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); cleardevice(); king("ch1"); int Mstatus = initmouse(); int button,m,n,slot; settextstyle(2,0,4); outtextxy(230,130,"LOADING"); if(Mstatus == 0 ) outtextxy(50,50, "Mouse Support Not Available"); while(!kbhit()) { showmouseptr(); getmousepos(&button,&m,&n); if(m>245 && m<325 && n>320 && n<332) { setcolor(RED); outtextxy(250,320,"BACK"); } else { setcolor(WHITE); outtextxy(250,320,"BACK"); if(m>215 && m<300 && n>164 && n<177) { setcolor(RED); outtextxy(220,165,"Slot 1"); } else { setcolor(WHITE); outtextxy(220,165,"Slot 1"); if(m>215 && m<300 && n>179 && n<192) { setcolor(RED); outtextxy(220,180,"Slot 2"); } else { setcolor(WHITE); outtextxy(220,180,"Slot 2"); if(m>215 && m<300 && n>194 && n<207) { setcolor(RED); outtextxy(220,195,"Slot 3"); } else { setcolor(WHITE); outtextxy(220,195,"Slot 3"); if(m>215 && m<300 && n>209 && n<222) { setcolor(RED); outtextxy(220,210,"Slot 4"); } else { setcolor(WHITE); outtextxy(220,210,"Slot 4"); if(m>215 && m<300 && n>224 && n<237) { setcolor(RED); outtextxy(220,225,"Slot 5"); } else { setcolor(WHITE); outtextxy(220,225,"Slot 5"); if(m>215 && m<300 && n>239 && n<252) { setcolor(RED); outtextxy(220,240,"Slot 6"); } else { setcolor(WHITE); outtextxy(220,240,"Slot 6"); if(m>215 && m<300 && n>254 && n<267) { setcolor(RED); outtextxy(220,255,"Slot 7"); } else { setcolor(WHITE); outtextxy(220,255,"Slot 7"); if(m>215 && m<300 && n>269 && n<282) { setcolor(RED); outtextxy(220,270,"Slot 8"); } else { setcolor(WHITE); outtextxy(220,270,"Slot 8"); if(m>215 && m<300 && n>284 && n<297) { setcolor(RED); outtextxy(220,285,"Slot 9"); } else { setcolor(WHITE); outtextxy(220,285,"Slot 9"); if(m>215 && m<300 && n>299 && n<312) { setcolor(RED); outtextxy(220,300,"Slot 10"); } else { setcolor(WHITE); outtextxy(220,300,"Slot 10"); }}}}}}}}}}} if(button == 1) { if(m>245 && m<325 && n>320 && n<332) { hidemouseptr(); video(450,150); // spawnl(P_WAIT,"g:\\exe\\menu1.exe", NULL); // back to main menu break; } if(m>215 && m<300 && n>164 && n<177) { hidemouseptr(); video(450,150); slot=1; loadgame(slot); } if(m>215 && m<300 && n>179 && n<192) { hidemouseptr(); video(450,150); slot=2; loadgame(slot); } if(m>215 && m<300 && n>194 && n<207) { hidemouseptr(); video(450,150); slot=3; loadgame(slot); } if(m>215 && m<300 && n>209 && n<222) { hidemouseptr(); video(450,150); slot=4; loadgame(slot); } if(m>215 && m<300 && n>224 && n<237) { hidemouseptr(); video(450,150); slot=5; loadgame(slot);} if(m>215 && m<300 && n>239 && n<252) {hidemouseptr(); video(450,150); slot=6; loadgame(slot);} if(m>215 && m<300 && n>254 && n<267) {hidemouseptr(); video(450,150); slot=7; loadgame(slot);} if(m>215 && m<300 && n>269 && n<282) {hidemouseptr(); video(450,150); slot=8; loadgame(slot);} if(m>215 && m<300 && n>284 && n<297) {hidemouseptr(); video(450,150); slot=9; loadgame(slot);} if(m>215 && m<300 && n>299 && n<312) {hidemouseptr(); video(450,150); slot=10; loadgame(slot);}}}}
void main() { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); cleardevice(); // char str[1]; str[1]='\0'; file.open("Info.TXT",ios::in|ios::binary); if(!file) cout<<"File couldnt be opened"; x=20; y=getmaxy()-70; settextstyle(2,0,5); setvalues(); board(); coins(); getch(); typeon(); getch(); cleardevice(); x=20; y=200; pic("ch pic"); typeon(); y+=20; typeon(); getch(); cleardevice(); pic("ch pic2"); x=20; y=200; typeon(); getch(); cleardevice(); king(getmaxx()/2-30,30,1); x=20; y=100; typeon(); getch(); cleardevice(); rook(getmaxx()/2-30,30,1); x=20; y=100; typeon(); getch(); cleardevice(); bishop(getmaxx()/2-30,30,1); x=20; y=100; typeon(); getch(); cleardevice(); queen(getmaxx()/2-30,30,1); x=20; y=100; typeon(); getch(); cleardevice(); knight(getmaxx()/2-30,30,1); x=20; y=100; typeon(); getch(); cleardevice(); pawn(getmaxx()/2-30,30,1); x=20; y=100; typeon(); getch(); cleardevice(); pic("ch pic3"); x=20; y=50; typeon(); getch(); file.close(); closegraph(); }
// Decodes a string in long algebraic notation and returns a move. If the // notation is not recognized the move is returned as (Move)0. //------------------------------------------------------------------------------ Move new_move_from_string(Position *self, char *e2e4) { bool queen_side_castle = !strcmp(e2e4, "0-0-0"); bool king_side_castle = !strcmp(e2e4, "0-0") && !queen_side_castle; if (king_side_castle || queen_side_castle) { bool king_side_ok = false, queen_side_ok = false; can_castle(self, self->color, &king_side_ok, &queen_side_ok); if (king_side_castle && king_side_ok) { int from = self->king[self->color]; int to = G1 + self->color * A8; return validate(self, new_castle(self, from, to)); } if (queen_side_castle && queen_side_ok) { int from = self->king[self->color]; int to = C1 + self->color * A8; return validate(self, new_castle(self, from, to)); } return (Move)0; } char notation[strlen(e2e4)]; char *src = e2e4, *dst = notation; // Strip all optional characters. while (*src) { if (strchr("KkQqRrBbNn12345678acdefgh", *src)) { *dst++ = *src; } src++; } *dst = '\0'; if (strlen(notation) >= 4 && strlen(notation) <= 5) { // Validate optional piece character to make sure the actual piece it // represents is there. Piece piece = pawn(self->color); switch (notation[0]) { case 'K': piece = king(self->color); break; case 'Q': piece = queen(self->color); break; case 'R': piece = rook(self->color); break; case 'B': piece = bishop(self->color); break; case 'N': piece = knight(self->color); } int sq; if (is_pawn(piece)) { sq = square(notation[1] - '1', notation[0] - 'a'); } else { sq = square(notation[2] - '1', notation[1] - 'a'); } if (sq >= A1 && sq <= H8) { if (self->pieces[sq] == piece) { return validate(self, new_move_from_notation(self, is_pawn(piece) ? notation : notation + 1)); } } } return (Move)0; }
/*check where the king can move if the king can move to a place will he be in check? if yes continue to check other conditions else no checkmate*/ int checkmate(int row,int col,checkm arr[],int q) { int ccount,valid,i,j,x,y,z,t=0,possible=0,n=0; y=row-1; x=col-1; while(x<0){ x++; } while(y<0) y++; if(turn!=0){ while((y<=row+1)&&(x<=col+1)){ valid=king(row,col,y,x); if((turn!=0)&&board[y][x]<0 || (x==8 || y==8)){ valid=0; wprintw(menuwin," invalid turn 1");wrefresh(menuwin); } if(valid){ t=0; for(j=0;j<=7;j++){ for(i=0;i<=7;i++){ if(t==1){ break; } if(board[j][i]>=9 && board[j][i]<=16) t = pawn(j,i,y,x); else if(board[j][i]==2 || board[j][i]==7) t = horse(j,i,y,x); else if(board[j][i]==4) t = queen(j,i,y,x); else if(board[j][i]==1 || board[j][i]==8) t = rook(j,i,y,x); else if(board[j][i]==3 || board[j][i]==6) t = bishop(j,i,y,x); } } if(t==1){ ccount++; } else if(t==0){ possible++; } } else{ ccount++; } if(y==row+1){ x++; } if(y<row+1){ y++; } } if(possible!=0){ return; } for(z=0;z<q;z++) { x=arr[z].x;y=arr[z].y; switch(abs(arr[z].piece)) { case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16:break; case 4:if(arr[z].y-row==arr[z].x-col) goto c3; //go to bishop case if(arr[z].y==row||arr[z].x==col) //go to rook case case 1:case 8://rook if(row==arr[z].y) { for(i=arr[z].x+1;i<col;i++) { if(board[row][i]==0) { if(friendlymove(row,i)){ possible++;goto end;} } } for(i=arr[z].x-1;i>col;i--) { if(board[row][i]==0) { if(friendlymove(row,i)){ possible++;goto end;} } } } else if(col==arr[x].x) { for(i=arr[z].y+1;i<row;i++) { if(board[i][col]==0) { if(friendlymove(i,col)){ possible++;goto end;} } } for(i=arr[z].y-1;i>col;i--) { if(board[i][col]==0) { if(friendlymove(i,col)){ possible++;goto end;} } } } break; //generate mid way blocks and check if friendly piece can move in case 2:case 7://horse c3:case 3:case 6: //bishop if((x<col)&&(y<row)) { for(i=1;(i+x)<col;i++) { if(board[y+i][x+i]==0) { if(friendlymove(y+i,x+i)){ possible++;goto end;} } } } else if((x>col)&&(y>row)){ for(i=1;(x-i)>col;i++){ if(board[y-i][x-i]==0) if(friendlymove(y-i,x-i)){ possible++;goto end;} } } else if((x>col)&&(y<row)){ for(i=1;(x-i)>col;i++){ if(board[y+i][x-i]==0) if(friendlymove(y+i,x-i)){ possible++;goto end;} } } else if((x<col)&&(y>row)){ for(i=1;(y-i)>row;i++){ if(board[y-i][x+i]==0) if(friendlymove(y-i,x+i)){ possible++;goto end;} } } break; } } end:if(possible==0){ mvwprintw(chessboard,2,30," player 1 CHECKMATE");wrefresh(chessboard); } else{ mvwprintw(chessboard,2,30," ");wrefresh(chessboard); } } else if(turn==0){ while((y<=row+1)&&(x<=col+1)){ valid=king(row,col,y,x); if((turn==0)&&board[y][x]>0){ valid=0; } if(valid){ t=0; for(j=0;j<=7;j++){ for(i=0;i<=7;i++){ if(t==1) break; if(board[j][i]>=-9 && board[j][i]<=-16) t = pawn(j,i,y,x); else if(board[j][i]==-2 || board[j][i]==-7) t = horse(j,i,y,x); else if(board[j][i]==-4) t = queen(j,i,y,x); else if(board[j][i]==-1 || board[j][i]==-8) t = rook(j,i,y,x); else if(board[j][i]==-3 || board[j][i]==-6) t = bishop(j,i,y,x); } } if(t==1){ ccount++; } else if(t==0) { possible++; } } else { ccount++; } if(y==row+1){ x++; } if(y<row+1){ y++; } } if(possible!=0) return; for(z=0;z<=q;z++) { x=arr[z].x;y=arr[z].y; switch(arr[z].piece) { case -9:case -10:case -11:case -12:case -13:case -14:case -15:case -16:break; case -4:if(arr[z].y-row==arr[z].x-col) goto c4; //go to bishop case if(arr[z].y==row||arr[z].x==col) //go to rook case case -1:case -8://rook if(row==arr[z].y) { for(i=arr[z].x+1;i<col;i++){ if(board[row][i]==0){ if(friendlymove(row,i)){ possible++;goto end;} } } for(i=arr[z].x-1;i>col;i--) { if(board[row][i]==0) { if(friendlymove(row,i)){ possible++;goto end;} } } } else if(col==arr[x].x) { for(i=arr[z].y+1;i<row;i++) { if(board[i][col]==0) { if(friendlymove(i,col)){ possible++;goto end;} } } for(i=arr[z].y-1;i>col;i--) { if(board[i][col]==0) { if(friendlymove(i,col)){ possible++;goto end;} } } } break; //generate mid way blocks and check if friendly piece can move in case -2:case -7://horse c4:case -3:case -6: //bishop if((x<col)&&(y<row)) { for(i=1;(i+x)<col;i++) { if(board[y+i][x+i]==0) { if(friendlymove(y+i,x+i)){ possible++;goto end2;} } } } else if((x>col)&&(y>row)){ for(i=1;(x-i)>col;i++){ if(board[y-i][x-i]==0) if(friendlymove(y-i,x-i)){ possible++;goto end2;} } } else if((x>col)&&(y<row)){ for(i=1;(x-i)>col;i++){ if(board[y+i][x-i]==0) if(friendlymove(y+i,x-i)){ possible++;goto end2;} } } else if((x<col)&&(y>row)){ for(i=1;(y-i)>row;i++){ if(board[y-i][x+i]==0) if(friendlymove(y-i,x+i)){ possible++;goto end2;} } } break; } } end2:if(possible==0){ mvwprintw(chessboard,2,30," player 2 CHECKMATE");wrefresh(chessboard); } else{ mvwprintw(chessboard,2,30," ");wrefresh(chessboard); } } }
int main(void){ int i, j; int game; char board[BOARD_SIZE][BOARD_SIZE+1]; char white[BOARD_SIZE+2+2][BOARD_SIZE+2+2]; char black[BOARD_SIZE+2+2][BOARD_SIZE+2+2]; char *pboard[BOARD_SIZE]; char *pblack[BOARD_SIZE+4]; char *pwhite[BOARD_SIZE+4]; int bk_row; int bk_col; int wk_row; int wk_col; #ifdef _DEBUG_ FILE *f; f = fopen("in.txt", "r"); #endif for(i=0; i<BOARD_SIZE; i++){ #ifdef _DEBUG_ fscanf(f, "%s", &board[i][0]); #else scanf("%s", &board[i][0]); #endif } for(i=0; i<BOARD_SIZE; i++)pboard[i] = &board[i][0]; for(i=0; i<BOARD_SIZE+4; i++){ pwhite[i] = &white[i][0]; pblack[i] = &black[i][0]; } game = 0; while(boardIsNotEmpty(&board[0][0])){ game++; memset(white, 0, sizeof(white)); memset(black, 0, sizeof(black)); for(i=0; i<BOARD_SIZE; i++){ for(j=0; j<BOARD_SIZE; j++){ if(board[i][j] == 'k'){ bk_row = i; bk_col = j; } if(board[i][j] == 'K'){ wk_row = i; wk_col = j; } } } for(i=0; i<BOARD_SIZE; i++){ for(j=0; j<BOARD_SIZE; j++){ switch(board[i][j]){ case('k'): king(pboard, i, j, pblack, BOARD_SIZE, 'K'); break; case('K'): king(pboard, i, j, pblack, BOARD_SIZE, 'k'); break; case('p'): black[2+i+1][2+j-1]++; black[2+i+1][2+j+1]++; break; case('P'): white[2+i-1][2+j-1]++; white[2+i-1][2+j+1]++; break; case('r'): rook(pboard, i, j, pblack, BOARD_SIZE, 'K'); break; case('R'): rook(pboard, i, j, pwhite, BOARD_SIZE, 'k'); break; case('b'): bishop(pboard, i, j, pblack, BOARD_SIZE, 'K'); break; case('B'): bishop(pboard, i, j, pwhite, BOARD_SIZE, 'k'); break; case('q'): rook(pboard, i, j, pblack, BOARD_SIZE, 'K'); bishop(pboard, i, j, pblack, BOARD_SIZE, 'K'); break; case('Q'): rook(pboard, i, j, pwhite, BOARD_SIZE, 'k'); bishop(pboard, i, j, pwhite, BOARD_SIZE, 'k'); break; case('n'): knight(i, j, pblack); break; case('N'): knight(i, j, pwhite); break; default: break; } } } #ifdef _DEBUG_ printf("---------------------\n"); printf("bk: %d %d wk: %d %d\n", bk_row, bk_col, wk_row, wk_col); for(i=0; i<BOARD_SIZE; i++){ for(j=0; j<BOARD_SIZE; j++){ printf("%01d", white[2+i][2+j]); } printf(" "); for(j=0; j<BOARD_SIZE; j++){ printf("%01d", black[2+i][2+j]); } printf(" "); for(j=0; j<BOARD_SIZE; j++){ printf("%c", board[i][j]); } printf("\n"); } printf("---------------------\n"); #endif if((white[2+bk_row][2+bk_col]==0)&&(black[2+wk_row][2+wk_col]==0)){ printf("Game #%d: no king is in check.\n", game); } if(white[2+bk_row][2+bk_col] != 0){ printf("Game #%d: black king is in check.\n", game); } if(black[2+wk_row][2+wk_col] != 0){ printf("Game #%d: white king is in check.\n", game); } for(i=0; i<BOARD_SIZE; i++){ #ifdef _DEBUG_ fscanf(f, "%s", &board[i][0]); #else scanf("%s", &board[i][0]); #endif } } return 0; }
void main() { int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, "\\tc\\bgi"); cleardevice(); char name[30]={"ch1"}; king(name); hand3(375,215,92,30); int Mstatus = initmouse(); int button,m,n,slot; int z=0; settextstyle(2,0,4); outtextxy(230,130,"LOADING"); /* outtextxy(220,165,"Slot 1"); // rectangle(215,164,300,177); // rectangle(215,179,300,192); // rectangle(215,194,300,207); rectangle(215,209,300,222); rectangle(215,224,300,237); rectangle(215,239,300,252); rectangle(215,254,300,267); rectangle(215,269,300,282); rectangle(215,284,300,297); outtextxy(220,180,"Slot 2"); outtextxy(220,195,"Slot 3"); outtextxy(220,210,"Slot 4"); outtextxy(220,225,"Slot 5"); outtextxy(220,240,"Slot 6"); outtextxy(220,255,"Slot 7"); outtextxy(220,270,"Slot 8"); outtextxy(220,285,"Slot 9"); outtextxy(220,300,"Slot 10"); */ if(Mstatus == 0 ) outtextxy(50,50, "Mouse Support Not Available"); // changecursor(cursor); while(!kbhit()) { showmouseptr(); getmousepos(&button,&m,&n); if(m>245 && m<325 && n>320 && n<332) { setcolor(RED); outtextxy(250,320,"BACK"); } else { setcolor(WHITE); outtextxy(250,320,"BACK"); if(m>215 && m<300 && n>164 && n<177) { setcolor(RED); outtextxy(220,165,"Slot 1"); } else { setcolor(WHITE); outtextxy(220,165,"Slot 1"); if(m>215 && m<300 && n>179 && n<192) { setcolor(RED); outtextxy(220,180,"Slot 2"); } else { setcolor(WHITE); outtextxy(220,180,"Slot 2"); if(m>215 && m<300 && n>194 && n<207) { setcolor(RED); outtextxy(220,195,"Slot 3"); } else { setcolor(WHITE); outtextxy(220,195,"Slot 3"); if(m>215 && m<300 && n>209 && n<222) { setcolor(RED); outtextxy(220,210,"Slot 4"); } else { setcolor(WHITE); outtextxy(220,210,"Slot 4"); if(m>215 && m<300 && n>224 && n<237) { setcolor(RED); outtextxy(220,225,"Slot 5"); } else { setcolor(WHITE); outtextxy(220,225,"Slot 5"); if(m>215 && m<300 && n>239 && n<252) { setcolor(RED); outtextxy(220,240,"Slot 6"); } else { setcolor(WHITE); outtextxy(220,240,"Slot 6"); if(m>215 && m<300 && n>254 && n<267) { setcolor(RED); outtextxy(220,255,"Slot 7"); } else { setcolor(WHITE); outtextxy(220,255,"Slot 7"); if(m>215 && m<300 && n>269 && n<282) { setcolor(RED); outtextxy(220,270,"Slot 8"); } else { setcolor(WHITE); outtextxy(220,270,"Slot 8"); if(m>215 && m<300 && n>284 && n<297) { setcolor(RED); outtextxy(220,285,"Slot 9"); } else { setcolor(WHITE); outtextxy(220,285,"Slot 9"); if(m>215 && m<300 && n>299 && n<312) { setcolor(RED); outtextxy(220,300,"Slot 10"); } else { setcolor(WHITE); outtextxy(220,300,"Slot 10"); }}}}}}}}}}} if(button == 1) { if(m>245 && m<325 && n>320 && n<332) { hidemouseptr(); video(450,150,0); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>164 && n<177) { hidemouseptr(); video(450,150,0); slot=1; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>179 && n<192) { hidemouseptr(); video(450,150,0); slot=2; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>194 && n<207) { hidemouseptr(); video(450,150,0); slot=3; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>209 && n<222) { hidemouseptr(); video(450,150,0); slot=4; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>224 && n<237) { hidemouseptr(); video(450,150,0); slot=5; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>239 && n<252) {hidemouseptr(); video(450,150,0); slot=6; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>254 && n<267) {hidemouseptr(); video(450,150,0); slot=7; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>269 && n<282) {hidemouseptr(); video(450,150,0); slot=8; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>284 && n<297) {hidemouseptr(); video(450,150,0); slot=9; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); } if(m>215 && m<300 && n>299 && n<312) {hidemouseptr(); video(450,150,0); slot=10; save(slot); spawnl(P_WAIT,"h:\\exe\\menu1.exe", NULL); exit(0); }}} }