Exemplo n.º 1
4
//check
void check(){
	int t1=0,t2=0,i,j,x1,y1,z1=0,z2=0;
	checkm arr[30];
		for(i=0;i<=7;i++){
			for(j=0;j<=7;j++){
				if(board[i][j]==-5){
					y1=i;
					x1=j;
					break;
				}
			}
		}
		for(j=0;j<=7;j++){
			for(i=0;i<=7;i++){
				if (t1==1){
				}
				if(board[j][i]>=9 && board[j][i]<=16){
					flipturn();
					t1 = pawn(j,i,y1,x1);flipturn();
		//add position of pawn to a list
					if(t1==1){
						arr[z1].y=j;arr[z1].x=i;arr[z1].piece=board[j][i];z1++;
					}
				}
				else if(board[j][i]==2 || board[j][i]==7){
					t1 = horse(j,i,y1,x1);
					if(t1==1){
					arr[z1].y=j;arr[z1].x=i;arr[z1].piece=board[j][i];z1++;
					}
				}
				else if(board[j][i]==4){
					t1 = queen(j,i,y1,x1);
					if(t1==1){
					arr[z1].y=j;arr[z1].x=i;arr[z1].piece=board[j][i];z1++;
					}
				}
				else if(board[j][i]==1 || board[j][i]==8){
					t1 = rook(j,i,y1,x1);
					if(t1==1){
						arr[z1].y=j;arr[z1].x=i;arr[z1].piece=board[j][i];z1++;
					}
				}
				else if(board[j][i]==3 || board[j][i]==6){
					t1 = bishop(j,i,y1,x1);
					if(t1==1){
						arr[z1].y=j;arr[z1].x=i;arr[z1].piece=board[j][i];z1++;
					}
				}
			 }
		}
		if(z1!=0){
			mvwprintw(chessboard,1,25,"Player 1 in CHECK");wrefresh(chessboard);
			if(turn!=0)
				checkmate(y1,x1,&arr,z1);
			else{
				mvwprintw(chessboard,2,30,"                   ");wrefresh(chessboard);
			}
		}
		z2=0;
		for(i=0;i<=7;i++){	
			for(j=0;j<=7;j++){
				if(board[i][j]==5){
					y1=i;
					x1=j;
					break;
				}
			}
		}	
		for(j=0;j<=7;j++){
			for(i=0;i<=7;i++){
				if (t2==1){	
				}
				if(board[j][i]>=(-9) && board[j][i]<=(-16)){
					flipturn();
					t2 = pawn(j,i,y1,x1);flipturn();
					if(t2==1){
							arr[z2].y=j;arr[z2].x=i;arr[z2].piece=board[j][i];z2++;
					}
				}
				else if(board[j][i]==(-2) || board[j][i]==(-7)){
					t2= horse(j,i,y1,x1);
					if(t2==1){
						arr[z2].y=j;arr[z2].x=i;arr[z2].piece=board[j][i];z2++;
					}
				}
				else if(board[j][i]==(-4)){
					t2 = queen(j,i,y1,x1);
					if(t2==1){
						arr[z2].y=j;arr[z2].x=i;arr[z2].piece=board[j][i];z2++;
					}
				}
				else if(board[j][i]==(-1) || board[j][i]==(-8)){
					t2 = rook(j,i,y1,x1);
					if(t2==1){
						arr[z2].y=j;arr[z2].x=i;arr[z2].piece=board[j][i];z2++;
					}
				}
				else if(board[j][i]==(-3) || board[j][i]==(-6)){
					t2 = bishop(j,i,y1,x1);
					if(t2==1){
						arr[z2].y=j;arr[z2].x=i;arr[z2].piece=board[j][i];z2++;
					}
				} 
			}
		}
		wrefresh(chessboard);
		if(z2!=0){	
			mvwprintw(chessboard,1,25,"Player 2 in CHECK");wrefresh(chessboard);
			if(turn==0)
				checkmate(y1,x1,&arr,z2);
			else{
				mvwprintw(chessboard,2,30,"                   ");wrefresh(chessboard);
			}
		}
		if(z1==0&&z2==0) 
			mvwprintw(chessboard,2,25,"                 ");wrefresh(chessboard);
}
Exemplo n.º 2
0
int main()
{
   queen() ;
   getchar();
   return 0;

}
void main(){
	int i,n;
	printf("\nEnter the number of Queens: ");
	scanf("%d",&n);
	queen(n);
	printf("\nTotal solutions=%d\n",count);
}
Exemplo n.º 4
0
int main()
{
int n;
printf("enter n");
scanf("%d",&n);
queen(n);
}
void main()
{
 int i,n;
 printf("Enter the number of Queens\n");
 scanf("%d",&n);
 queen(n);
 getch();
}
Exemplo n.º 6
0
int main(void)
{
	int n = 8;
	int count = queen(n);
	printf("%d solutions in %d queens problem\n", count, n);

	return 0;
}
Exemplo n.º 7
0
Arquivo: g.c Projeto: DavidToca/acm
int main()
{
	int i;

	queen(0);

	return 0;
}
Exemplo n.º 8
0
 vector<vector<string> > solveNQueens(int n) {
     int *a = new int[n+1];
     memset(a, 0, sizeof(int)*(n+1));
     
     vector<vector<string > > result;
     
     queen(a, 1, n, result);
     return result;
 }
Exemplo n.º 9
0
Arquivo: 2.c Projeto: heyuhang/linuxC
 int main()   
 {   
     int t;   
     scanf("%d",&n);   
     t = queen(1);   
     if(n == 0) //如果n=0,则可行解个数为0,这种情况一定不要忽略   
       t = 0;   
     printf("%d",t);   
     return 0;   
 }  
Exemplo n.º 10
0
int main()
{
	scanf("%d", &N);

	cnt = 0;
	queen(0);
	printf("%d\n", cnt);

	return 0;
}
Exemplo n.º 11
0
// 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;
}
Exemplo n.º 12
0
 vector<vector<string> > solveNQueens(int n) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     ret.clear();
     sum = 0;
     
     solution = new int[n];
     
     queen(0, n);   
     
     return ret;
 }
Exemplo n.º 13
0
stack<queen> queenSolver(int n) {
	// Initializings and Declarations
	stack<queen> queenStack;
	bool success(false);
	queenStack.push(queen(1,1));

	while(!success) {
		if(conflictChecker(queenStack)) {
			while(queenStack.top().col == n) {
				queenStack.pop();
				if(queenStack.empty()) return queenStack;
			}
			queenStack.top().col++;
		} else if(queenStack.size() == n) {
			success = true;
		} else {
			queenStack.push(queen(queenStack.size()+1,1));
		}
	}

	return queenStack;
}
Exemplo n.º 14
0
Arquivo: 2.c Projeto: heyuhang/linuxC
 int queen(int t)   
 {   
   int i;
     if(t>n && n>0) //当放置的皇后超过n时,可行解个数加1,此时n必须大于0   
       sum++;   
     else  
       for( i=1;i<=n;i++)   
       {   
           x[t] = i; //标明第t个皇后放在第i列   
           if(place(t)) //如果可以放在某一位置,则继续放下一皇后   
             queen(t+1);    
       }   
     return sum;   
 }   
Exemplo n.º 15
0
 void queen(int*a, int cur, int n, vector<vector<string> >&result) {
     if (cur > n) {
         output(a, n, result);
         return;
     }
     
     for (int i = 1; i <= n; i++) {
         a[cur] = i;
         if (check(a, cur)) {
             queen(a, cur+1, n, result);
             
         }
         a[cur] = 0;
     }
 }
Exemplo n.º 16
0
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);  }}
Exemplo n.º 17
0
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);
  }
}
Exemplo n.º 18
0
void queen(int baris, int jml_queen)
{
     int kolom;
     
     for(kolom=1;kolom<=jml_queen;kolom++)
     {
        if(penempatan(baris,kolom))
        {
           papan[baris]=kolom; //tidak ada konflik jadi tempatkan queen
           if(baris==jml_queen) //jalan buntu
              cetak_papan(jml_queen); //cetak papan
           else                         //coba queen dengan posisi selanjutnya
              queen(baris+1,jml_queen);
        }
     }
}
Exemplo n.º 19
0
void queen()
{
int col;
row++;
for(col=1;col<=8;col++)
{
   if(check(row,col)){
   	a[row]=col;
   	if(row==8)
   	{
      display();
      getch();
   	exit(1);
   	}
      queen();
   }
}
row--;
}
Exemplo n.º 20
0
void queen(int k)
{
	int col;
	for (col = 0; col < N; ++col) 
	{
		if (is_safe(col, k)) // 유망한가 ?
		{
			rows[k] = col;

			if (k == N - 1) // 기저사례
			{ 
				cnt++;
				return;
			}
			
			queen(k + 1);
		}
	}
}
Exemplo n.º 21
0
int main(int argc, char *argv[])
{
  int jml_queen,i,j;
  void queen(int baris, int jml_queen);
  
  do{
  system("cls");
  printf("\t\t\tN-Queen Problem Solving dengan Backtrack\n");
  printf("\t\t\t========================================\n\n");
  printf("\t\tMasukan Jumlah Queen [5..8]: ");
  scanf("%d",&jml_queen);
  }while((jml_queen<5)||(jml_queen>8));
  queen(1,jml_queen);
  printf("\n\n\t\t\t\t======================");
  printf("\n\t\t\t\tSOLUSI TELAH HABIS!!!\n\n");
  printf("\n\t\t\tDitemukan solusi sebanyak %d buah\n\n",++count-1);
  system("pause");
  return 0;
}
Exemplo n.º 22
0
 void queen(int row, int n) {
     if (row==n) {
         sum++;
         vector<string> one;
         for (int i=0;i<n;i++) {
             string row = "";
             for (int j=0;j<n;j++) {
                 if (j==solution[i]) row.push_back('Q');
                 else row.push_back('.');
             }
             one.push_back(row);
         }
         ret.push_back(one);
     } else {
         for (int i=0;i<n;i++) {
             solution[row] = i;
             if (!isConflict(row,i)) queen(row+1, n);
         }
     }
 }
Exemplo n.º 23
0
Arquivo: queens.c Projeto: hercky/spoj
void queen(int A[8][8],int row)
{
		int i;
		if(row==8)
		{
			solution++;
			printf("Solution No: %d\n",solution);
			printarr(A);
			printf("______________\n");				
		        return;
		}
		for(i=0;i<8;i++)
		{
			if(A[row][i]==0)
			{
				modify(A,row,i,1);
				queen(A,row+1);
				modify(A,row,i,-1);
			}
		}
}
Exemplo n.º 24
0
//------------------------------------------------------------------------------
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;
}
Exemplo n.º 25
0
Arquivo: g.c Projeto: DavidToca/acm
void queen(int row)
{
	int i, j;

	if (row > 7) {
		for (i = 0; i < 8; i++)
			printf(i ? ", %d" : "\t{ %d", i * 8 + pos[i]);
		printf(" },\n");
		return;
	}

	for (i = 0; i <= 7; i++) {
		for (j = 0; j < row; j++) {
			if (pos[j] == i) break;
			if ((pos[j] + row - j) == i) break;
			if ((pos[j] - row + j) == i) break;
		}
		if (j < row) continue;

		pos[row] = i;
		queen(row + 1);
	}
}
Exemplo n.º 26
0
// 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;
}
Exemplo n.º 27
0
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();
}
Exemplo n.º 28
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);
		}
	}	
}
Exemplo n.º 29
0
int main(void)  
{  
    init();  
    queen(); 
    return 0;  
}  
Exemplo n.º 30
0
//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();
						}
				}
			}
	}
}