This is a simple C language program that will locate 8 queen in chess board with all of the states and you can determine the position of first queen on the chess board.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | /***********By : FARSHAD MOJTABAI*********/ #include <stdio.h> #include <conio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <time.h> #include <graphics.h> #include <dos.h> int place(int); void queen(int); void Board(); void Q(int, int); void CounT(int, int, int); int XYGraphic(int); int x[9]; int main(void) { queen(8); return 0; } void queen(int n) { int k = 1, z, X, Y, counter = 0, x2, y2, loc; int extern x[9]; char answer; int gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph( & gdriver, & gmode, "C:\\TURBOC3\\BGI"); x[1] = 0; printf("WHETHER YOU WANT TO CHOOSE A COORDINATE ORn"); printf("COMPUTER TAKES ALL POSSIBLE CASES :"); scanf("%c", & answer); if (answer == 'c' || answer == 'C') { printf("X,Y:"); scanf("%d%d", & x2, & y2); } clearviewport(); while (k > 0) { x[k]++; while (x[k] <= n && !place(k)) x[k]++; if (x[k] <= n) { if (k == n) { if (answer == 'a' || answer == 'A') { Board(); counter++; for (int m = 1; m < 9; m++) { z = x[m]; X = XYGraphic(m); Y = XYGraphic(z); Q(X, Y); CounT(m, z, counter); } delay(3000); } else if (answer == 'c' || answer == 'C') { for (int i = 1; i < 9; i++) if (i == x2 && x[i] == y2) loc = 1; if (loc == 1) { Board(); counter++; for (int m = 1; m < 9; m++) { z = x[m]; X = XYGraphic(m); Y = XYGraphic(z); Q(X, Y); CounT(m, z, counter); } loc = 0; delay(3000); } } if (kbhit()) exit(0); clearviewport(); } else { k++; x[k] = 0; } } else k--; } } int place(int l) { int i = 1; int extern x[9]; while (i < l) { if ((x[i] == x[l]) || abs(x[i] - x[l]) == abs(i - l)) return (0); i += 1; } return (1); } int XYGraphic(int p) { switch (p) { case 1: return 65; case 2: return 115; case 3: return 165; case 4: return 215; case 5: return 265; case 6: return 315; case 7: return 365; case 8: return 415; } return 0; } void CounT(int x3, int y3, int c) { gotoxy(60, 4); printf("[%d]", c); gotoxy(59, 6 + x3); printf("(%c,%d)", 96 + x3, y3); } void Q(int x1, int y1) { for (int i = 0; i <= 15; i++) { setcolor(i); circle(x1, y1, i); } } void Board() { int m, n; outtextxy(65, 20, "a"); outtextxy(115, 20, "b"); outtextxy(165, 20, "c"); outtextxy(215, 20, "d"); outtextxy(265, 20, "e"); outtextxy(315, 20, "f"); outtextxy(365, 20, "g"); outtextxy(415, 20, "h"); outtextxy(20, 65, "1"); outtextxy(20, 115, "2"); outtextxy(20, 165, "3"); outtextxy(20, 215, "4"); outtextxy(20, 265, "5"); outtextxy(20, 315, "6"); outtextxy(20, 365, "7"); outtextxy(20, 415, "8"); for (int j = 0; j < 8; j++) for (int i = 0; i < 8; i++) rectangle(50 * i + 40, 50 * j + 40, 50 * (i + 1) + 40, 50 * (j + 1) + 40); for (int l = 0; l < 8; l++) { if (l % 2 == 0) { m = 0; n = 0; } else { m = 1; n = 50; } for (int k = 0; k < 4 + m; k++) { setfillstyle(SOLID_FILL, getmaxcolor()); floodfill(100 * k - n + 41, 50 * l + 41, getmaxcolor()); } } } |