-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayProblems_8QueensProblem.java
More file actions
168 lines (138 loc) · 5.83 KB
/
ArrayProblems_8QueensProblem.java
File metadata and controls
168 lines (138 loc) · 5.83 KB
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
/**
* Program Name: ArrayProblems_8QueensProblem.java
* Purpose: This code solves the classic "8 Queens" Problem - Tester code provided by Texas_University CS307 course file
* Coder: Carlton Branch, 0771107
* Date: Jul 9, 2016
*/
public class ArrayProblems_8QueensProblem {
/**
* Method Name: isQueenSafe
* Purpose: Call each predicate testing method to determine if the current Queen is safe from attack
* Accepts: the 2d array representing the board and the coordinates of the current Queen being checked
* Returns: boolean
*/
public static boolean isQueenSafe( char[][] board , int startX , int startY ){
if ( isQueenSafeVertical ( board , startX , startY ) )
if ( isQueenSafeHorizontal(board , startX , startY ) )
if ( isQueenSafeDiagonal(board , startX , startY ) )
return true; //The queen is safe from attack by other queens
return false; //The current queen is not safe from attack by other queens
}
/**
* Method Name: isQueenSafeVertical
* Purpose: determine if current queen is safe from attack in the current column (vertical axis)
* Accepts: the 2d array representing the board and the coordinates of the current Queen being checked
* Returns: true if safe, false if not safe
*/
public static boolean isQueenSafeVertical( char[][] board , int positionY , int positionX )
{
int count = 0;
for ( int i = 0 ; i < board.length ; ++i )
if ( board[ i ][ positionX ] == 'q' )
count++;
//if there are more than one queen in the column, the current queen is not safe from attack
if ( 1 < count )
return false;
return true;
}
/**
* Method Name: isQueenSafeHorizontal
* Purpose: determine if current queen is safe from attack in the current row (horizontal axis)
* Accepts: the 2d array representing the board and the coordinates of the current Queen being checked
* Returns: true if safe, false if not safe
*/
public static boolean isQueenSafeHorizontal(char[][] board, int positionY, int positionX)
{
int count = 0;
for ( int i = 0 ; i < board[ positionY ].length ; ++i )
if ( board[ positionY ][ i ] == 'q' )
count++;
//if there are more than one queen in the row, the current queen is not safe from attack
if ( 1 < count )
return false;
return true;
}
/**
* Method Name: isQueenSafeDiagonal
* Purpose: determine if current queen is safe from attack on both sides diagonally
* Accepts: the 2d array representing the board and the coordinates of the current Queen being checked
* Returns: true if safe, false if not safe
*/
public static boolean isQueenSafeDiagonal( char[][] board , int rowStart , int colStart )
{
//check flag used to determine the results of each directional test
boolean flag = true;
//SOUTH-EAST
for ( int i = 1 ; i < board[0].length ; ++i )
if ( rowStart + i < board.length && colStart + i < board[0].length )
if ( board[ rowStart + i ][ colStart + i ] == 'q' )
flag = false;
//NORTH-EAST
for ( int i = 1 ; i < board[0].length ; ++i )
if ( 0 <= rowStart - i && colStart + i < board[0].length )
if ( board[ rowStart - i ][ colStart + i ] == 'q')
flag = false;
//SOUTH-WEST
for ( int i = 1 ; i < board[0].length ; ++i )
if ( 0 <= colStart - i && rowStart + i < board.length)
if ( board[ rowStart + i ][colStart - i ] == 'q')
flag = false;
//NORTH-WEST
for ( int i = 1 ; i < board[0].length ; ++i )
if ( 0 <= colStart - i && 0 <= rowStart-i )
if (board[rowStart - i][colStart - i ] == 'q' )
flag = false;
return flag;
}
/**METHOD HEADER PROVIDED BY CS307 ASSIGNMENT .JAVA FILE
* Determine if the chess board represented by board is a safe set up.
* <p>pre: board != null, board.length > 0, board is a square matrix.
* (In other words all rows in board have board.length columns.),
* all elements of board == 'q' or '.'. 'q's represent queens, '.'s
* represent open spaces.<br>
* <p>post: return true if the configuration of board is safe,
* that is no queen can attack any other queen on the board.
* false otherwise.
* the parameter <tt>board</tt> is not altered as a result of
* this method.
* @param board the chessboard
* @return true if the configuration of board is safe,
* that is no queen can attack any other queen on the board.
* false otherwise.
*/
public static boolean queensAreSafe(char[][] board) {
char[] validChars = {'q', '.'};
for (int i = 0; i < board.length; ++i)
for (int j= 0; j < board[i].length; ++j)
if (board[i][j] == 'q')
if (!isQueenSafe(board, i, j))
return false;
return true; //All the Queens on the board are safe
}
public static void main(String[] args){
//TESTER CODE BELOW PROVIDED BY CS307 TEXAS UNIVERSITY ASSIGNMENT .JAVA FILE
boolean expectedBool, actualBool;
char[][] safe = { {'.', '.', '.'},
{'q', '.', '.'},
{'.', '.', 'q'}};
expectedBool = true;
actualBool = queensAreSafe(safe);
System.out.print("\nTest 5 expected value: " + expectedBool + ", actual value: " + actualBool);
if ( expectedBool == actualBool )
System.out.println(" passed test 5, queensAreSafe");
else
System.out.println(" failed test 5, queensAreSafe");
//test 6
char[][] unsafe = { {'.', '.', '.', 'q'},
{'.', '.', '.', '.'},
{'.', '.', '.', '.'},
{'q', '.', '.', '.'}};
expectedBool = false;
actualBool = queensAreSafe(unsafe);
System.out.print("\nTest 6 expected value: " + expectedBool + ", actual value: " + actualBool);
if ( expectedBool == actualBool )
System.out.println(" passed test 8, queensAreSafe");
else
System.out.println(" failed test 8, queensAreSafe");
}
}