Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- char pieces[12][12];
- char gameBoard [12][12];
- char deadPieces[37];
- char play[5];
- int index[4];
- int died=1;
- int diedLast;
- int i,j;
- int valid;
- int turn;
- int king1x, king1y, king2x, king2y;
- int checkWhite, checkBlack;
- int checkMateBlack,checkMateWhite;
- void checkMateWhitePath(int x,int y);
- void readLine()
- {
- int ch, i=0;
- while((ch=getchar()) != '\n'){
- if(i<4)
- {
- play[i++]=ch;
- }
- }
- play[i]='\0';
- }
- void gameBoardBorders();
- void checkerBoard();
- void startingChessBoard();
- void printGame ();
- void scanAndConvert();
- void checkValidMove();
- void swapElements ();
- void reverseSwapElement();
- void rook();
- void knight();
- void king();
- void king();
- void bishop();
- void queen();
- void pawn();
- void moveSet();
- void fullKingCheckWhite(int x,int y);
- void fullKingCheckBlack(int x,int y);
- void king1CheckMate();
- void king2CheckMate();
- void checkMateBlackPath(int x,int y);
- int main(){
- startingChessBoard();
- gameBoardBorders();
- checkerBoard();
- printGame();
- king1x=9, king1y=6, king2x=2, king2y=6;
- turn=1;
- diedLast=0;
- checkWhite=0;
- checkBlack=0;
- while (1){
- valid = 0;
- checkMateBlack=-1;checkMateWhite=-1;
- if (turn==1){
- while ((valid==0)||(checkWhite==1)){
- printf("\nWhite's turn: ");
- scanAndConvert();
- if(valid==1){
- checkValidMove();
- }
- if(valid==1){
- moveSet();
- }
- if (valid==1){
- swapElements();
- }
- if((valid==1)&&(checkWhite==1)){
- fullKingCheckWhite(king1x,king1y);
- }
- if ((valid==1)&&(checkWhite==1)){
- printf("Invalid move.");
- reverseSwapElement();
- }
- }
- printf("\n");
- printGame();
- fullKingCheckBlack(king2x,king2y);
- if (checkMateBlack==1){
- king2CheckMate();
- if (checkMateWhite==0){
- printf("Check mate.\n");
- exit(EXIT_SUCCESS);
- }
- }
- fullKingCheckBlack(king2x,king2y);
- if (checkBlack==1){
- printf("Check black.\n");
- }
- turn=2;
- }
- else {
- while ((valid==0)||(checkBlack==1)){
- printf("\nBlack's turn: ");
- scanAndConvert();
- if(valid==1){
- checkValidMove();
- }
- if(valid==1){
- moveSet();
- }
- if(valid==1){
- swapElements();
- }
- if(checkBlack==1 && valid==1){
- fullKingCheckBlack(king2x,king2y);
- }
- if(checkBlack==1 && valid==1){
- printf("Invalid move.");
- reverseSwapElement();
- }
- }
- printf("\n");
- printGame();
- fullKingCheckWhite(king1x,king1y);
- if (checkMateWhite==1){
- king1CheckMate();
- if (checkMateBlack==0){
- printf("Check mate.\n");
- exit(EXIT_SUCCESS);
- }
- }
- fullKingCheckWhite(king1x,king1y);
- if (checkWhite==1){
- printf("Check white.\n");
- }
- turn=1;
- }
- }
- return 0;
- }
- void gameBoardBorders(){
- for (i=0;i<12;i++){
- for (j=0;j<12;j++){
- gameBoard[i][j]='\0';
- }
- }
- for (i=0;i<12;i+=11){
- for (j=2;j<10;j++){
- gameBoard[i][j]='A'+(j-2);
- }
- }
- for (j=2;j<10;j++){
- gameBoard[1][j]='-';
- gameBoard[10][j]='-';
- }
- for (i=2;i<10;i++){
- for (j=0;j<12;j+=11){
- gameBoard[i][j]='8'-(i-2);
- }
- }
- for (i=2;i<10;i++){
- gameBoard[i][1]='|';
- gameBoard[i][10]='|';
- }
- }
- void checkerBoard(){
- for (i=2;i<10;i++){
- for (j=2;j<10;j+=2){
- if (i%2==0){
- gameBoard[i][j]='.';
- }
- else {
- gameBoard[i][j]='_';
- }
- }
- }
- for (i=2;i<10;i++){
- for (j=3;j<10;j+=2){
- if (i%2==0){
- gameBoard[i][j]='_';
- }
- else {
- gameBoard[i][j]='.';
- }
- }
- }
- }
- void startingChessBoard(){
- for (i=0;i<12;i++){
- for(j=0;j<12;j++){
- pieces[i][j]='\0';
- }
- }
- pieces[2][2]= 'R';
- pieces[2][3]= 'N';
- pieces[2][4]= 'B';
- pieces[2][5]= 'Q';
- pieces[2][6]= 'K';
- pieces[2][7]= 'B';
- pieces[2][8]= 'N';
- pieces[2][9]= 'R';
- pieces[9][2]= 'r';
- pieces[9][3]= 'n';
- pieces[9][4]= 'b';
- pieces[9][5]= 'q';
- pieces[9][6]= 'k';
- pieces[9][7]= 'b';
- pieces[9][8]= 'n';
- pieces[9][9]= 'r';
- for(j=2;j<10;j++){
- pieces[3][j]= 'P';
- }
- for(j=2;j<10;j++){
- pieces[8][j]= 'p';
- }
- for(i=0;i<36;i++){
- deadPieces[i]='\0';
- }
- }
- void printGame (){
- system("cls");
- printf("\n\t\t\t\t\t\tDead\n\n");
- for (i=0;i<12;i++){
- for (j=0;j<12;j++){
- printf("%c",gameBoard[i][j]);
- printf("%c ", pieces[i][j]);
- }
- printf("\t\t%c %c %c", deadPieces[i+1], deadPieces[i+13], deadPieces[i+25]);
- printf("\n\n");
- }
- printf("\n");
- }
- void scanAndConvert(){
- readLine();
- while (play[4]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- while ((play[0]<'A')||(play[0]>'H')||(play[1]<'1')||(play[1]>'8')||(play[2]<'A')||(play[2]>'H')||(play[3]<'1')||(play[3]>'8')){
- printf("Invalid move.");
- valid=0;
- return;
- }
- index[0]=58-play[1];
- index[1]=play[0]-63;
- index[2]=58-play[3];
- index[3]=play[2]-63;
- valid=1;
- }
- void checkValidMove(){
- if (turn==1){
- if ((pieces[index[0]][index[1]]<'a')||(pieces[index[0]][index[1]]>'z')){
- printf("Invalid move.");
- valid=0;
- return;
- }
- if ((pieces[index[2]][index[3]]>='a')&&(pieces[index[2]][index[3]]<='z')){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- else if (turn==2){
- if ((pieces[index[0]][index[1]]<'A')||(pieces[index[0]][index[1]]>'Z')){
- printf("Invalid move.");
- valid=0;
- return;
- }
- if((pieces[index[2]][index[3]]>='A')&&(pieces[index[2]][index[3]]<='Z')){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- valid=1;
- }
- void swapElements (){
- if ((pieces[index[2]][index[3]])!='\0'){
- deadPieces[died]= pieces[index[2]][index[3]];
- died++;
- pieces[index[2]][index[3]]='\0';
- diedLast=1;
- }else{
- diedLast=0;
- }
- char temp=pieces[index[0]][index[1]];
- pieces[index[0]][index[1]]=pieces[index[2]][index[3]];
- pieces[index[2]][index[3]]=temp;
- }
- void reverseSwapElement(){
- if(diedLast==1){
- if(pieces[index[0]][index[1]]=='\0' || deadPieces[died-1]!='\0'){
- pieces[index[0]][index[1]]=deadPieces[died-1];
- died--;
- deadPieces[died]='\0';
- }
- }
- char temp=pieces[index[2]][index[3]];
- pieces[index[2]][index[3]]=pieces[index[0]][index[1]];
- pieces[index[0]][index[1]]=temp;
- }
- void rook(){
- if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
- for (i=1;i<index[2]-index[0];i++){
- if (pieces[index[0]+i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[0]+i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
- for (i=1;i<index[0]-index[2];i++){
- if (pieces[index[0]-i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[0]-i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[3]-index[1];i++){
- if (pieces[index[0]][index[1]+i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]+i==index[3]){
- valid=1;
- }
- }
- else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[1]-index[3];i++){
- if (pieces[index[0]][index[1]-i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]-i==index[3]){
- valid=1;
- }
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void knight(){
- if ((((abs(index[3]-index[1]))==1)&&((abs(index[2]-index[0]))==2)) || (((abs(index[3]-index[1]))==2)&&((abs(index[2]-index[0]))==1)))
- {
- valid=1;
- }else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void king(){
- if((((abs(index[3]-index[1]))==1) && ((abs(index[2]-index[0]))==1)) || (((abs(index[3]-index[1]))==1) && ((abs(index[2]-index[0]))==0)) || (((abs(index[3]-index[1]))==0) && ((abs(index[2]-index[0]))==1)))
- {
- if (turn==1){
- king1x=index[2];
- king1y=index[3];
- }
- else{
- king2x=index[2];
- king2y=index[3];
- }
- valid=1;
- }else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void bishop(){
- if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[2]-index[0];i++){
- if ((pieces[index[0]+i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]+i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void queen(){
- if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
- for (i=1;i<index[2]-index[0];i++){
- if (pieces[index[0]+i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[0]+i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
- for (i=1;i<index[0]-index[2];i++){
- if (pieces[index[0]-i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[0]-i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[3]-index[1];i++){
- if (pieces[index[0]][index[1]+i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]+i==index[3]){
- valid=1;
- }
- }
- else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[1]-index[3];i++){
- if (pieces[index[0]][index[1]-i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]-i==index[3]){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[2]-index[0];i++){
- if ((pieces[index[0]+i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]+i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void pawn(){
- char promote;
- if (pieces[index[0]][index[1]]=='p'){
- if(index[0]==8){
- if(((index[2]-index[0]==-1)||(index[2]-index[0]==-2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid=1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }else{
- if((index[2]-index[0]==-1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[2]==2){
- printf("Promote to: ");
- scanf(" %c", &promote);
- while(promote != 'R' && promote != 'N' && promote != 'B' && promote != 'Q' ){
- printf("Wrong promotion\n");
- printf("Promote to: ");
- scanf(" %c", &promote);
- }
- if(promote == 'R'){
- pieces[index[2]][index[3]]='r';
- }
- else if(promote == 'N'){
- pieces[index[2]][index[3]]='n';
- }
- else if(promote == 'B'){
- pieces[index[2]][index[3]]='b';
- }
- else if(promote == 'Q'){
- pieces[index[2]][index[3]]='q';
- }
- }
- }
- else{
- if(index[0]==3){
- if(((index[2]-index[0]==1)||(index[2]-index[0]==2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
- valid=1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }else{
- if((index[2]-index[0]==1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[2]==9){
- printf("Promote to: ");
- scanf(" %c", &promote);
- while(promote != 'R' && promote != 'N' && promote != 'B' && promote != 'Q' ){
- printf("Wrong promotion\n");
- printf("Promote to: ");
- scanf(" %c", &promote);
- }
- if(promote == 'R'){
- pieces[index[2]][index[3]]='R';
- }
- else if(promote == 'N'){
- pieces[index[2]][index[3]]='N';
- }
- else if(promote == 'B'){
- pieces[index[2]][index[3]]='B';
- }
- else if(promote == 'Q'){
- pieces[index[2]][index[3]]='Q';
- }
- }
- }
- }
- void moveSet(){
- if ((pieces[index[0]][index[1]]=='p')||(pieces[index[0]][index[1]]=='P')){
- pawn();
- }
- else if ((pieces[index[0]][index[1]]=='r')||(pieces[index[0]][index[1]]=='R')){
- rook();
- }
- else if ((pieces[index[0]][index[1]]=='n')||(pieces[index[0]][index[1]]=='N')){
- knight();
- }
- else if ((pieces[index[0]][index[1]]=='b')||(pieces[index[0]][index[1]]=='B')){
- bishop();
- }
- else if ((pieces[index[0]][index[1]]=='q')||(pieces[index[0]][index[1]]=='Q')){
- queen();
- }
- else if ((pieces[index[0]][index[1]]=='k')||(pieces[index[0]][index[1]]=='K')){
- king();
- }
- }
- void fullKingCheckWhite(int x,int y){
- if ((pieces[x-1][y+2]=='N')||(pieces[x+1][y+2]=='N')||(pieces[x-1][y-2]=='N')||(pieces[x+1][y-2]=='N')||(pieces[x+2][y-1]=='N')||(pieces[x+2][y+1]=='N')||(pieces[x-2][y-1]=='N')||(pieces[x-2][y+1]=='N')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else if ((pieces[x-1][y-1]=='P')||(pieces[x-1][y+1]=='P')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else{
- for(i=1; i<=7;i++){
- if(x+i>9){
- break;
- }
- else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='R' && pieces[x+i][y]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y]=='R'||pieces[x+i][y]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2){
- break;
- }
- else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='R' && pieces[x-i][y]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y]=='R'||pieces[x-i][y]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y+i>9){
- break;
- }
- else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='R' && pieces[x][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x][y+i]=='R'||pieces[x][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y-i<2){
- break;
- }
- else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='R' && pieces[x][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x][y-i]=='R'||pieces[x][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y+i>9){
- break;
- }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='B' && pieces[x+i][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y+i]=='B'||pieces[x+i][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y+i>9){
- break;
- }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='B' && pieces[x-i][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y+i]=='B'||pieces[x-i][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y-i<2){
- break;
- }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='B' && pieces[x-i][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y-i]=='B'||pieces[x-i][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y-i<2){
- break;
- }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='B' && pieces[x+i][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y-i]=='B'||pieces[x+i][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- }
- }
- void fullKingCheckBlack(int x,int y){
- if ((pieces[x-1][y+2]=='n')||(pieces[x+1][y+2]=='n')||(pieces[x-1][y-2]=='n')||(pieces[x+1][y-2]=='n')||(pieces[x+2][y-1]=='n')||(pieces[x+2][y+1]=='n')||(pieces[x-2][y-1]=='n')||(pieces[x-2][y+1]=='n')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else if ((pieces[x+1][y-1]=='p')||(pieces[x+1][y+1]=='p')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else{
- for(i=1; i<=7;i++){
- if(x+i>9){
- break;
- }
- else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='r' && pieces[x+i][y]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y]=='r'||pieces[x+i][y]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2){
- break;
- }
- else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='r' && pieces[x-i][y]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y]=='r'||pieces[x-i][y]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y+i>9){
- break;
- }
- else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='r' && pieces[x][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x][y+i]=='r'||pieces[x][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y-i<2){
- break;
- }
- else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='r' && pieces[x][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x][y-i]=='r'||pieces[x][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y+i>9){
- break;
- }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='b' && pieces[x+i][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y+i]=='b'||pieces[x+i][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y+i>9){
- break;
- }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='b' && pieces[x-i][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y+i]=='b'||pieces[x-i][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y-i<2){
- break;
- }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='b' && pieces[x-i][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y-i]=='b'||pieces[x-i][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y-i<2){
- break;
- }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='b' && pieces[x+i][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y-i]=='b'||pieces[x+i][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- }
- }
- void king1CheckMate(){
- if ((pieces[king1x+1][king1y]<'a')&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y);
- }
- if ((checkMateWhite==1)&&(pieces[king1x][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)){
- fullKingCheckWhite(king1x,king1y+1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x+1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y+1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x-1][king1y]<'a')&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y);
- }
- if ((checkMateWhite==1)&&(pieces[king1x][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)){
- fullKingCheckWhite(king1x,king1y-1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x-1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y-1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x+1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y-1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x-1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y+1);
- }
- if (checkMateWhite==1){
- fullKingCheckBlack(index[2],index[3]);
- }
- if (checkMateBlack==0){
- checkMateBlackPath(king1x,king1y);
- }
- }
- void king2CheckMate(){
- if (((pieces[king2x+1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y);
- }
- if (((checkMateBlack==1)&&(pieces[king2x][king2y+1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y+1>1)&&(king2y+1<10)){
- fullKingCheckBlack(king2x,king2y+1);
- }
- if (((checkMateBlack==1)&&(pieces[king2x+1][king2y+1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y+1);
- }
- if (((checkMateBlack==1)&&(pieces[king2x-1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y);
- }
- if (((checkMateBlack==1)&&(pieces[king2x][king2y-1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y-1>1)&&(king2y-1<10)){
- fullKingCheckBlack(king2x,king2y-1);
- }
- if (((checkMateBlack==1)&&(pieces[king2x-1][king2y-1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y-1);
- }
- if (((checkMateBlack==1)&&(pieces[king2x+1][king2y-1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y-1);
- }
- if (((checkMateBlack==1)&&(pieces[king2x-1][king2y+1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y+1);
- }
- if (checkMateBlack==1){
- fullKingCheckWhite(index[2],index[3]);
- }
- if (checkMateWhite==0){
- checkMateWhitePath(king2x,king2y);
- }
- }
- void checkMateWhitePath(int x,int y){
- if((index[3]-y)==0 && (index[2]-x>0)){
- for (i=1;i<index[2]-x;i++){
- fullKingCheckBlack(x+i,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)==0 && (x-index[2]>0)){
- for (i=1;i<x-index[2];i++){
- fullKingCheckBlack(x-i,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)>0 && (index[2]-x==0)){
- for (i=1;i<index[3]-y;i++){
- fullKingCheckBlack(x,y+i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((y-index[3])>0 && (index[2]-x==0)){
- for (i=1;i<y-index[3];i++){
- fullKingCheckBlack(x,y-i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
- for (i=1;i<x-index[2];i++){
- fullKingCheckBlack(x-i,y-i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
- for (i=1;i<index[2]-x;i++){
- fullKingCheckBlack(x+i,y+i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
- for (i=1;i<x-index[2];i++){
- fullKingCheckBlack(x-i,y+i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
- for (i=1;i<index[0]-index[2];i++){
- fullKingCheckBlack(x+i,y-i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- }
- void checkMateBlackPath(int x,int y){
- if((index[3]-y)==0 && (index[2]-x>0)){
- for (i=1;i<index[2]-x;i++){
- fullKingCheckBlack(x+i,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)==0 && (x-index[2]>0)){
- for (i=1;i<x-index[2];i++){
- fullKingCheckBlack(x-i,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)>0 && (index[2]-x==0)){
- for (i=1;i<index[3]-y;i++){
- fullKingCheckBlack(x,y+i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((y-index[3])>0 && (index[2]-x==0)){
- for (i=1;i<y-index[3];i++){
- fullKingCheckBlack(x,y-i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
- for (i=1;i<x-index[2];i++){
- fullKingCheckBlack(x-i,y-i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
- for (i=1;i<index[2]-x;i++){
- fullKingCheckBlack(x+i,y+i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
- for (i=1;i<x-index[2];i++){
- fullKingCheckBlack(x-i,y+i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
- for (i=1;i<index[0]-index[2];i++){
- fullKingCheckBlack(x+i,y-i);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement