Unnamed
public c v1 · immutable#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>#define maxdepth 8#define empty 9999#define moveArraySize 60#define THEDEPTH 5 int board[8][5] = {empty};void printPiece(int temp);void printboard();void setup();void getValid(int moves[moveArraySize][4],int player);char convertChar(int i);int convertInt(char a);int inv(int i);void makemove();int min(int depth,int ab);int max(int depth,int ab);int checkForWinner();void getMove();int eval();void checkGameOver();void playself();int counter;clock_t start;int eval1(){ int pieceCount=0,i,j,d; for(i=0;i<8;i++){ for(j=0;j<5;j++){ if(board[i][j] != empty) pieceCount = pieceCount + board[i][j]; } } return -pieceCount;}void sortValid(int moves[moveArraySize][4],int sorted[moveArraySize]){ int k=0, a,b,t,p; int evals[moveArraySize]; int tempOld,tempNew; while(moves[k][0] + moves[k][1] + moves[k][2] + moves[k][3] != 0){ tempOld = board[moves[k][1]][moves[k][0]]; tempNew = board[moves[k][3]][moves[k][2]]; //move piece board[moves[k][3]][moves[k][2]] = tempOld; board[moves[k][1]][moves[k][0]] = empty; evals[k] = eval1(); sorted[k] = k; board[moves[k][1]][moves[k][0]] = tempOld; //move back board[moves[k][3]][moves[k][2]] = tempNew; k++; }// printf("K=%d\n",k);// for(a=0;a<k;a++) printf("%d->%d ",sorted[a],evals[a]);// printf("\n"); //we have all evals for(a=1;a<k;a++){ for(b=k-1;b>=a;b--) { if(evals[b-1] > evals[b]){ t = evals[b-1]; p = sorted[b-1]; evals[b-1] = evals[b]; sorted[b-1] = sorted[b]; evals[b] = t; sorted[b] = p; } } }// for(a=0;a<k;a++)printf("%d=>%d ",sorted[a],evals[a]);// printf("\n");}int main(void){ int p=0,i,check=0; setup(); while(check==0) { i=0; printf("You want first(1), second(2)?\n"); fflush(stdin); scanf("%d",&i); fflush(stdin); if(i > 2 || i < 1) printf("Invalid Input\n"); else check = i; } if(check == 1) { for(;;) { checkGameOver(); getMove(); checkGameOver(); makemove(); } } if(check == 2) { for(;;) { checkGameOver(); makemove(); checkGameOver(); getMove(); } } scanf("%d",&p); return 0;}void checkGameOver(){ int catchit,playerLeft=0,compLeft=0; printboard(); if(checkForWinner() == 5000){ printf("I WIN!\n"); scanf("%d",&catchit); exit(0); } if(checkForWinner() == -5000){ printf("You Won?!\n"); scanf("%d",&catchit); exit(0); }}// negative = player// positive = computerint eval(){ int pieceCount=0,i,j,d; for(i=0;i<8;i++){ for(j=0;j<5;j++){ if(board[i][j] != empty) pieceCount = pieceCount + board[i][j]; } } counter++; return -pieceCount;}int checkForWinner(){ int i,j,aKingAlive=0,bKingAlive=0,catchit; int moves[moveArraySize][4] = {0}; for(i=0;i<8;i++) { for(j=0;j<5;j++) { if(board[i][j] == -2) aKingAlive = 1; if(board[i][j] == 2) bKingAlive = 1; } } getValid(moves,1); if(moves[0][0] + moves[0][1] + moves[0][2] + moves[0][3] == 0) { return -5000; } getValid(moves,-1); if(moves[0][0] + moves[0][1] + moves[0][2] + moves[0][3] == 0) { return 5000; } if(aKingAlive == 0) return -5000; if(bKingAlive == 0) return 5000; return 0;}void getMove(){ int moves[moveArraySize][4] = {0}; getValid(moves,1); int a1=0,b=0,b1=0,c1=0,d=0,d1=0,k=0,valid=0; char a='A',c='A'; printf("These are your valid moves: "); while(moves[k][0] + moves[k][1] + moves[k][2] + moves[k][3] != 0) { printf("%c%d%c%d | ",convertChar(moves[k][0]),inv(moves[k][1]),convertChar(moves[k][2]),inv(moves[k][3])); k++; } while(valid == 0) { printf("\nEnter the in format A0B0\n"); //i have no idea why it needed to grab input twice but it did. for(k=0;k<2;k++) { fscanf(stdin,"%c%d%c%d", &a, &b1, &c, &d1); b = inv(b1); d = inv(d1); a1 = convertInt(a); c1 = convertInt(c); } if(b > 7 || b < 0 || a1 > 4 || a1 < 0 || d > 7 || d < 0 || c1 > 4 || c1 < 0) { printf("Invalid Input, Please Re-Enter\n"); } else { getValid(moves,1); k=0; while(moves[k][0] + moves[k][1] + moves[k][2] + moves[k][3] != 0 && valid == 0) { //printf("%d = %d, %d = %d, %d = %d, %d = %d\n",a1, moves[k][0],inv(b),moves[k][1],c1,moves[k][2],inv(d),moves[k][3]); if(a1 == moves[k][0] && b == moves[k][1] && c1 == moves[k][2] && d == moves[k][3]) valid = 1; k++; } if(valid == 1) { board[d][c1] = board[b][a1]; board[b][a1] = empty; } else { printf("Invalid Move, Please Re-Enter\n"); } } }}char convertBoard(char a){ switch(a){ case 'A': return 'E'; case 'B': return 'D'; case 'C': return 'C'; case 'D': return 'B'; case 'E': return 'A'; default: return 'F'; }}void makemove(){ int i=0,p=0,j=0,time=0; counter=0; int best = -20000,depth = 0,score,froma,fromb,toa,tob,tempOld=0,tempNew=0,finalBest=0; clock_t end; start = clock(); int moves[moveArraySize][4] = {0}; int sorted[moveArraySize] = {0}; getValid(moves,-1); sortValid(moves,sorted); while(moves[i][0] + moves[i][1] + moves[i][2] + moves[i][3] != 0) { i++; } while((end-start)/(double)CLOCKS_PER_SEC < 5){ for(j=i;0<j+1;j--){ // for(j=0;j<i+1;j++){ p=sorted[j]; tempOld = board[moves[p][1]][moves[p][0]]; tempNew = board[moves[p][3]][moves[p][2]]; //move piece board[moves[p][3]][moves[p][2]] = tempOld; board[moves[p][1]][moves[p][0]] = empty; //zero out move score = min(depth+1,best); if(score > best) { best = score; froma = moves[p][1]; fromb = moves[p][0]; toa = moves[p][3]; tob = moves[p][2]; finalBest = tempOld; } board[moves[p][1]][moves[p][0]] = tempOld; //move back board[moves[p][3]][moves[p][2]] = tempNew; //zero out move } maxdepth++; end = clock(); } maxdepth = THEDEPTH; printf("My Move is: %c%d to %c%d (%c%d to %c%d)\n", convertChar(fromb),inv(froma),convertChar(tob),inv(toa), convertBoard(convertChar(fromb)),froma+1,convertBoard(convertChar(tob)),toa+1); printf("I made %d Guesses",counter); board[froma][fromb] = empty; //move piece board[toa][tob] = finalBest; }int min(int depth,int ab){ int p,j; clock_t end = clock(); int moves[moveArraySize][4] = {0}; int sorted[moveArraySize] = {0}; getValid(moves,1); sortValid(moves,sorted); int best = 20000, score,i=0,tempOld=0,tempNew=0; if((end-start)/(double)CLOCKS_PER_SEC > 5) return -best; if(checkForWinner() != 0) return (checkForWinner()); if(depth == maxdepth) return (eval()); while(moves[i][0] + moves[i][1] + moves[i][2] + moves[i][3] != 0) { i++; } for(j=0;j<i+1;j++){ p=sorted[j]; tempOld = board[moves[p][1]][moves[p][0]]; tempNew = board[moves[p][3]][moves[p][2]]; //move piece board[moves[p][3]][moves[p][2]] = tempOld; board[moves[p][1]][moves[p][0]] = empty; score = max(depth+1,best); if(score < best) { best = score; } board[moves[p][1]][moves[p][0]] = tempOld; //move back board[moves[p][3]][moves[p][2]] = tempNew; //zero out move if(best <= ab) return ab; } return best;}int max(int depth,int ab){ int moves[moveArraySize][4] = {0}; int p,j; int sorted[moveArraySize] = {0}; clock_t end = clock(); getValid(moves,-1); sortValid(moves,sorted); int best = -20000, score,i=0,tempOld=0,tempNew=0; if((end-start)/(double)CLOCKS_PER_SEC > 5) return -best; if(checkForWinner() != 0) return (checkForWinner()); if(depth == maxdepth) return (eval()); while(moves[i][0] + moves[i][1] + moves[i][2] + moves[i][3] != 0) { i++; }// for(j=0;j<i;j++){ for(j=i;0<j;j--){ p = sorted[j]; tempOld = board[moves[p][1]][moves[p][0]]; tempNew = board[moves[p][3]][moves[p][2]]; //move piece board[moves[p][3]][moves[p][2]] = tempOld; board[moves[p][1]][moves[p][0]] = empty; score = min(depth+1,best); if(score > best) { best = score; } board[moves[p][1]][moves[p][0]] = tempOld; //move back board[moves[p][3]][moves[p][2]] = tempNew; //zero out move if(best >= ab) return ab; } return best;}int inv(int i){ return 8-i;}int convertInt(char a){ switch(a){ case 'A': return 0; break; case 'B': return 1; break; case 'C': return 2; break; case 'D': return 3; break; case 'E': return 4; break; }}char convertChar(int i){ switch(i){ case 0: return 'A'; break; case 1: return 'B'; break; case 2: return 'C'; break; case 3: return 'D'; break; case 4: return 'E'; break; }}void getValid(int moves[moveArraySize][4], int player){ //int moves[256][4] = {0,0,0,0}; int i,j, temp, count=0; for(i=0;i<8;i++) { for(j=0;j<5;j++) { temp = board[i][j]; if(temp != empty) //i value = type of moves allowed { if(temp < 0 && player < 0){ //player moves // printf("Piece %c, %d:\n", convertChar(j), inv(i)); if(i == 0 || i == 7) { if(j == 0 && board[i][j+1] != empty){ moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i)); } else if(j == 4 && board[i][j-1] != empty){ moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i)); } else{ if(board[i][j-1] != empty) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i)); // printboard(); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i; count++; } if(board[i][j+1] != empty){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i; count++; } } } if(i==1 || i == 6){ if(j == 0){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+1; count++; } else if(j == 4){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+1; count++; } else{ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+1; count++; } } if(i==2 || i == 5) { //Knight Code to Come if(j==0) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i+1; count++; }else if(j==4) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i+1; count++; }else if(j==1) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+2; count++; }else if(j==3) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+2; count++; }else if(j==2) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i+1; count++; } } //Bishop Moves if(i==3 || i == 4){ if(j==0) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i+2)); if(board[i+1][j+1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+3),inv(i+3)); if(board[i+2][j+2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+3; moves[count][3] = i+3; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+4),inv(i+4)); if(board[i+3][j+3] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+4; moves[count][3] = i+4; count++; } }} }else if(j==4) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+1; count++; if(board[i+1][j-1] == empty) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i+2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-3),inv(i+3)); if(board[i+2][j-2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-3; moves[count][3] = i+3; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-4),inv(i+4)); if(board[i+3][j-3] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-4; moves[count][3] = i+4; count++; } } } }else if(j == 1) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i+2)); if(board[i+1][j+1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+3),inv(i+3)); if(board[i+2][j+2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+3; moves[count][3] = i+3; count++; }} }else if(j == 2) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i+2)); if(board[i+1][j+1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i+2)); if(board[i+2][j-2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i+2; count++; } } }else if(j == 3) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i+1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i+1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i+2)); if(board[i+1][j-1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i+2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-3),inv(i+3)); if(board[i+2][j-2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-3; moves[count][3] = i+3; count++; }} } } } //computer pieces if(temp > 0 && player > 0) { // printf("Piece %c, %d:\n", convertChar(j), inv(i)); if(i == 0 || i == 7) { if(j == 0 && board[i][j+1] != empty){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i; count++; } else if(j == 4 && board[i][j-1] != empty){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i; count++; } else{ if(board[i][j-1] != empty) { //printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i; count++; } if(board[i][j+1] != empty) { //printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i; count++; } } } if(i==1 || i == 6){ if(j == 0){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-1; count++; } else if(j == 4){ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-1; count++; } else{ // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-1; count++; } } if(i==2 || i == 5) { //Knight Code to Come if(j==0) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i-1; count++; }else if(j==4) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i-1; count++; }else if(j==1) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-2; count++; }else if(j==3) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-2; count++; }else if(j==2) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-2)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i-1; count++; } } //bishop moves if(i==3 || i == 4){ if(j==0) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i-2)); if(board[i-1][j+1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+3),inv(i-3)); if(board[i-2][j+2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+3; moves[count][3] = i-3; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+4),inv(i-4)); if(board[i-3][j+3] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+4; moves[count][3] = i-4; count++; }}} }else if(j==4) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i-2)); if(board[i-1][j-1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-3),inv(i-3)); if(board[i-2][j-2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-3; moves[count][3] = i-3; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-4),inv(i-4)); if(board[i-3][j-4] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-4; moves[count][3] = i-4; count++; }}} }else if(j == 1) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i-2)); if(board[i-1][j+1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+3),inv(i-3)); if(board[i-2][j+2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+3; moves[count][3] = i-3; count++; }} }else if(j == 2) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i-2)); if(board[i-1][j-1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i-2; count++; } // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+2),inv(i-2)); if(board[i-1][j+1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+2; moves[count][3] = i-2; count++; } }else if(j == 3) { // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j+1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j+1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-1),inv(i-1)); moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-1; moves[count][3] = i-1; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-2),inv(i-2)); if(board[i-1][j-1] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-2; moves[count][3] = i-2; count++; // printf("%c,%d to %c,%d\n",convertChar(j),inv(i),convertChar(j-3),inv(i-3)); if(board[i-2][j-2] == empty) { moves[count][0] = j; moves[count][1] = i; moves[count][2] = j-3; moves[count][3] = i-3; count++; }} } } } } } } } void printboard(){ int i,j; printf("\n"); for(i=0;i<8;i++) { printf("%d ", 8-i); for(j=0;j<5;j++) { if(board[i][j] != empty) { if(board[i][j] == 2 || board[i][j] == -2) printf("K"); else printPiece(i); if(board[i][j] < 0) printf("* "); else printf(" "); } else printf("-- "); } //printf("%d ", i); //prints I row number printf("\n"); } printf(" ----------------\n"); printf(" A B C D E\n"); // printf(" 0 1 2 3 4\n"); //matching coloum values}void printPiece(int temp){ if(temp == 7 || temp == 0) printf("R"); else if(temp == 6 || temp == 1) printf("P"); else if(temp == 5 || temp == 2) printf("N"); else if(temp == 4 || temp == 3) printf("B"); }void setup(){ int i,j; for(i=0;i<8;i++) for(j=0;j<5;j++) board[i][j] = empty; // player king board[0][2] = -2; //test code //board[0][0] = -1; //rest of player pieces board[0][1] = board[0][3] = board[1][0] = board[1][1] = board[1][2] = board[1][3] = board[1][4] = board[2][2] = -1; // computer king board[7][2] = 2; //rest of computer pieces board[7][1] = board[7][3] = board[6][0] = board[6][1] = board[6][2] = board[6][3] = board[6][4] = board[5][2] = 1;}