Click here to Skip to main content
15,909,193 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
C#
#include "stdafx.h"
#include

int pairs(char *brd[],int r,int c)
{
    int attacks = 0;
    int tr;
    int tc;

    for(int cc=0;cc<8;cc++)
        attacks += ((cc!=c)&&(brd[r][cc]=='Q'))?1:0;

    for (int k=-7;k=0)&&(tr=0)&&(tc=0)&&(tr=0)&&(tc<8))
            if((tr!=r) && (tc!=c))
                  attacks += (brd[tr][tc]=='Q');

    }
    return(attacks);
}

int _tmain(int argc, _TCHAR* argv[])
{
    char *board[8];
    int *attackers[8];

    while(1)
    {
        for(int r=0;r<8;r++)        // main search loop
        {
            board[r] = new(char[8]);
            for(int c=0;c<8;c++) board[r][c] = ' ';
            attackers[r] = new(int[8]);
        }
        for(int c =0;c<8;c++)
        {
            int r = rand()%8;
            board[r][c] = 'Q';
        }

        int attempts = 0;
        while(attempts<10)
        {
            // number of attackers
            for(int r=0;r<8;r++)
                for(int c=0;c<8;c++)
                    attackers[r][c] = pairs(board,r,c);
            // if the sum of the attackers at the queen positions = 0; done
            int attacks =0;
            for(int r=0;r<8;r++)
                for(int c=0;c<8;c++)
                    attacks += (board[r][c]=='Q')?pairs(board,r,c):0;
            if (attacks == 0)
            {
            printf("\n FOUND ONE*********");    // Found a solution to the problem
                printf("\n Queens");
                // prints out the board
                for(int r=0;r<8;r++)
                {   printf("\n %d:",r);
                    for(int c=0;c<8;c++)
                        printf("%c",board[r][c]);
                }
                printf("\n");
                break;
            }

            // find min
            int min = 8;
            int minr=0;
            int minc=0;
            for(int r=0;r<8;r++)
                for(int c=0;c<8;c++)
                    if(attackers[r][c]<min)
                    {
                        min = attackers[r][c];
                        minr=r;
                        minc=c;
                    }

            // move the queen to that spot
            for(int r=0;r<8;r++)
                board[r][minc] = ' ';
            board[minr][minc] = 'Q';
            // dump the board
          attempts++;

        }
    }
    return 0;
}
Posted
Updated 5-Nov-14 4:58am
v3
Comments
PIEBALDconsult 5-Nov-14 10:45am    
Is this suuposed to be an NQueens app? It's not very good; you'll be better off plagiarizing a better one.
Muhammad0001 5-Nov-14 10:53am    
yes that is but want to convert c# for better understanding and to know other plate form...so plzzzz
Sergey Alexandrovich Kryukov 5-Nov-14 13:22pm    
For better understanding, it's millions times more useful to write your own code, debug it to see how it works...
Just don't waste your time.
—SA

No.
Why not? A number of reasons.
Firstly, because C and C# are very different languages which share a lot of common syntax: converting the code will not result in good quality C# code because there are very, very different frameworks and principles involved.
Secondly, because we do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!
 
Share this answer
 
Heed these advices: conversion c source code to c#[^]
The bottom line is, you have got to learn to do it yourself.
 
Share this answer
 
Comments
Muhammad0001 5-Nov-14 10:43am    
i try it earlier but i can't able to convertit so i ask question after giving much time to it .thaks
Sergey Alexandrovich Kryukov 5-Nov-14 13:24pm    
Stop wasting your time on "conversion". Do something useful for yourself. In essence, this is what all answers tell you, including my own answer referenced above. Please read those answers more thoroughly and think on them.
—SA
Sergey Alexandrovich Kryukov 5-Nov-14 13:23pm    
5ed.
—SA
 
Share this answer
 
Comments
Muhammad0001 5-Nov-14 10:55am    
yes that is but want to convert c# for better understanding and to know other plate form...so plzzzz
PIEBALDconsult 5-Nov-14 10:59am    
No, you really don't, that code isn't very good.
here is Eight Queens Problem using VB.NET[^] version. Study it, you can easily convert to c# if you want. there are lot of online sites you can try.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900