1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "../Stack/stack.h"
4
5 typedef struct
6 {
7 int row;
8 int col;
9 } POSITION;
10
11 int getSize(void);
12
13 void fillBoard(STACK* stack, int boardSize);
14 void printBoard(STACK* stack, int boardSize);
15
16 bool guarded(int board[][9], int row, int col, int boardSize);
17
18 int main(void)
19 {
20 int boardSize;
21
22 STACK* stack;
23 boardSize = getSize();
24 stack = createStack();
25
26 fillBoard(stack, boardSize);
27 printBoard(stack, boardSize);
28 destroyStack(stack);
29
30 printf("\nWe hope you enjoyed Eight Queens.\n");
31 return 0;
32 }
33
34 int getSize(void)
35 {
36 int boardSize;
37
38 printf("Welcome to EightQueens. You may select\n"
39 "a board sizei from 4 x 4 to 8 x 8. I will \n"
40 "then position a queen in ezch row of the\n"
41 "board so no queen may capture another\n"
42 "queen. Note: There are no solutions for boards \n"
43 "less than 4 x 4.\n");
44 printf("\nPlease enter the board size: ");
45 scanf("%d", &boardSize);
46 while(boardSize < 4 || boardSize > 8){
47 printf("Board size must be greater than 3 and less than 9.\n"
48 "You entered %d. Please re-enter.\n"
49 "Thankyou.\a\a\n\n"
50 "Your board size: ", boardSize);
51 scanf("%d", &boardSize);
52 }
53 return boardSize;
54 }
55
56 void fillBoard(STACK* stack, int boardSize)
57 {
58 int row;
59 int col;
60 int board[9][9] = {0};
61 POSITION* pPos;
62
63 row = 1;
64 col = 0;
65
66 while(row <= boardSize){
67 while(col <= boardSize && row <= boardSize){
68 col++;
69 if(!guarded(board, row, col, boardSize)){
70 board[row][col] = 1;
71
72 pPos = (POSITION*)malloc(sizeof(POSITION));
73 pPos->row = row;
74 pPos->col = col;
75
76 pushStack(stack, pPos);
77
78 row++;
79 col = 0;
80 }
81 while(col >= boardSize){
82 pPos = popStack(stack);
83 row = pPos->row;
84 col = pPos->col;
85 board[row][col] = 0;
86 free(pPos);
87 }
88 }
89 }
90 return;
91 }
92
93 bool guarded(int board[][9], int chkRow, int chkCol, int boardSize)
94 {
95 int row;
96 int col;
97
98 // Check current col for a queen
99 col = chkCol;
100 for(row = 1; row <= chkRow; row++)
101 if(board[row][col] == 1)
102 return true;
103
104 // Check diagonal right-up
105 for(row = chkRow - 1, col = chkCol + 1;
106 row > 0 && col <= boardSize;
107 row--, col++)
108 if(board[row][col] == 1)
109 return true;
110
111 // Check diagonal left-up
112 for(row = chkRow - 1, col = chkCol - 1;
113 row > 0 && col > 0;
114 row--, col--)
115 if(board[row][col] == 1)
116 return true;
117
118 return false;
119 }
120
121 void printBoard(STACK* stack, int boardSize)
122 {
123 int col;
124
125 POSITION* pPos;
126 STACK* pOutStack;
127
128 if(emptyStack(stack)){
129 printf("There are no positions on this board\n");
130 return;
131 }
132
133 printf("\nPlace queens in following positions:\n");
134
135 pOutStack = createStack();
136 while(!emptyStack(stack)){
137 pPos = popStack(stack);
138 pushStack(pOutStack, pPos);
139 }
140
141 while(!emptyStack(pOutStack)){
142 pPos = popStack(pOutStack);
143 printf("Row %d-Col %d: \t|", pPos->row, pPos->col);
144 for(col = 1; col <= boardSize; col++){
145 if(pPos->col == col)
146 printf("Q |");
147 else
148 printf(" |");
149 }
150 printf("\n");
151 }
152 destroyStack(pOutStack);
153 return;
154 }