Menu

[Solved] Object Oriented Program Book One Difference M Requiring Test Code Main Old Book Question Q37225771

This is an object oriented program. book with ONE difference, Im requiring you test your code (a main). If you have the old

This is an object oriented program. book with ONE difference, Im requiring you test your code (a main). If you have the oldBacktracking 179 agonal attack, and you finally place a queen in the third square of column 2, as Figure 5-7b ustrates The blRecursion as a CHAPTERS e7se Remone the new gueen return true he method doEightQueens, which callsp thod doE ight The Eight QThis is an object oriented program. book with ONE difference, I’m requiring you test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem. This is #1 on page 187 of your add a client program to On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of your book are some suggestions to implementing two ADTs to solve the problem in C. I want you to follow their suggestions to implement the solution. . First create a Board class data type. This class data type should represent the chess board (you can use any implementation you want, a two dimensional array (however this may waste space but not if you’re displaying in my opinion), a vector (it could represent only the board places occupied by queens), an array of vectors etc-there is more than one right way!). The board should keep track of the Queens currently placed on it and contain operations/methods (such as placeQueens and displayBoard) to solve the problem and display a solution. You can add other methods as you need them. Note the board will need to contain a Queen (or perhaps an array or vector of queens) I haven’t done this problem yet butI immediately thought composition might be needed. 2. Next create a Queen data type. A queen instance should be able to keep track of its current position (row and column) and be able to move to the next row. Again you can add other methods as you see fit. 3. Add a client program to test your code: m should place the first queen anywhere on the his progra board and then call placeQueens to see if a solution can be reached. If the solution can be reached the program should display the board with the queens properly placed. This is an object oriented program. book with ONE difference, I’m requiring you test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem. This is #1 on page 187 of your add a client program to On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of your book are some suggestions to implementing two ADTs to solve the problem in C. I want you to follow their suggestions to implement the solution. . First create a Board class data type. This class data type should represent the chess board (you can use any implementation you want, a two dimensional array (however this may waste space but not if you’re displaying in my opinion), a vector (it could represent only the board places occupied by queens), an array of vectors etc-there is more than one right way!). The board should keep track of the Queens currently placed on it and contain operations/methods (such as placeQueens and displayBoard) to solve the problem and display a solution. You can add other methods as you need them. Note the board will need to contain a Queen (or perhaps an array or vector of queens) I haven’t done this problem yet butI immediately thought composition might be needed. 2. Next create a Queen data type. A queen instance should be able to keep track of its current position (row and column) and be able to move to the next row. Again you can add other methods as you see fit. 3. Add a client program to test your code: m should place the first queen anywhere on the his progra board and then call placeQueens to see if a solution can be reached. If the solution can be reached the program should display the board with the queens properly placed. Backtracking 179 agonal attack, and you finally place a queen in the third square of column 2, as Figure 5-7b ustrates The black dots in the figure indicate squares that are rejected because a queen in that square is subject to attack by another queen in an earlier column. The blue dots indicate the dditional squares that the new queen can attack We continue to place queens in this manner until we get to column 6 as Figure 5.7e shows if reach an Although the five placed queens cannot attack each other, they can attack any square in column 6, impasse, backtrack and therefore, you cannot place a queen in column 6. You must back up to column 5 and move to the previous ts queen to the next possible square in column 5, which is in the last row, as Figure 5-7f indi- cates. When you consider column 6 once again, there are still no choices for a queen in that col- umn. Because you have exhausted the possibilities in column 5, you must back up to column 4 s Figure 5-7g shows, the next possible square in column 4 is in row 7. You then consider col- umn 5 again and place a queen in row 2 (Figure 5-7h). How can you use recursion in the process that was just described? Consider an algorithm that places a queen in a column, given that you have placed queens correctly in the preceding columns. First, if there are no more columns to consider, you are finished; this is the base case. Otherwise, after you successfully place a queen in the current column, you need to consider the next column. That is, you need to solve the same problem with one fewer column; this is the recursive step. Thus, you begin with eight columns, consider smaller problems that decrease in size by one column at each recursive step, and reach the base case when you have a problem with no columns This solution appears to satisfy the criteria for a recursive solution. However, you do not cur- know whether you can successfully place a queen in the current column. If you can, you re backtrack, as has already been described. that the previous columns contain queens that cannot attack one another nly cosder the netolun If ou cannot place aquenin the curent column you ned to The following pseudocode describes the algorithm for placing queens in columns, given Places queens in eight columns placeQueens(queen: Queen, row: integer, column: integer): boolean The solution combines recursion with if (column BOARD_SIZE) The problem is solved else (unconsidered squares exist in the given column and while the problem is unsolved) Find the next square in the given colun that is not under attack by a queen in an earlier cohmn if (such a square exists) Place a queen in the square Try nexi column if (IplaceQueens (a new queen, first Row, colunn 1)) No queen is possible in the nexi colwnn Remove the new queen Move the last queen that was placed on the board to the next row in that column Recursion as a CHAPTERS e7se Remone the new gueen return true he method doEightQueens, which callsp thod doE ight The Eight Queens problem is initiated by the me with a new queen in the upper-Jeft corner of the board dol ight Queens( placedueens (a new queen, firstRow, firstColumn) After do sghtQveens has completed, we can display the board, if a solution was found. FIGURE S-8 A solution to the Eight Queens problem the Eight Quess Figure S-8 indicates the solution that the previous algorithm finds. By modifying the argu- ments to placeQeens, you can discover other solutions to the Eight Queens problem. The Pro- gramming Problems at the end of this chapter ask you to consider other solutions to this algorithm, as well as additional modifications. Question 7 Consider a Four Queens problem, which has the same rules as the Eight Queens problem but uses a 4 x 4 board. Find all solutions to this new problem by applying backtracking by hand. Implementing a solution to the Eight Queens problem. You can write a solution to the Queens problem in a variety of ways For example, you can define two classes: a Board class to represent the chessboard and a Queen class to represent a queen on the board. A Queen objo could keep track of its row and column placement and be able to move to the next row. A object keeps track of the Queen objects currently on the board and contains operation placeQueensto solve the Eight Queens problem and display the solution. Eight Board The Board class in the solution described thus far may be represented in a number of ways The simplest representation would be a two-dimensional array, however, such an array wastes Show transcribed image text This is an object oriented program. book with ONE difference, I’m requiring you test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem. This is #1 on page 187 of your add a client program to On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of your book are some suggestions to implementing two ADTs to solve the problem in C. I want you to follow their suggestions to implement the solution. . First create a Board class data type. This class data type should represent the chess board (you can use any implementation you want, a two dimensional array (however this may waste space but not if you’re displaying in my opinion), a vector (it could represent only the board places occupied by queens), an array of vectors etc-there is more than one right way!). The board should keep track of the Queens currently placed on it and contain operations/methods (such as placeQueens and displayBoard) to solve the problem and display a solution. You can add other methods as you need them. Note the board will need to contain a Queen (or perhaps an array or vector of queens) I haven’t done this problem yet butI immediately thought composition might be needed. 2. Next create a Queen data type. A queen instance should be able to keep track of its current position (row and column) and be able to move to the next row. Again you can add other methods as you see fit. 3. Add a client program to test your code: m should place the first queen anywhere on the his progra board and then call placeQueens to see if a solution can be reached. If the solution can be reached the program should display the board with the queens properly placed.
This is an object oriented program. book with ONE difference, I’m requiring you test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem. This is #1 on page 187 of your add a client program to On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of your book are some suggestions to implementing two ADTs to solve the problem in C. I want you to follow their suggestions to implement the solution. . First create a Board class data type. This class data type should represent the chess board (you can use any implementation you want, a two dimensional array (however this may waste space but not if you’re displaying in my opinion), a vector (it could represent only the board places occupied by queens), an array of vectors etc-there is more than one right way!). The board should keep track of the Queens currently placed on it and contain operations/methods (such as placeQueens and displayBoard) to solve the problem and display a solution. You can add other methods as you need them. Note the board will need to contain a Queen (or perhaps an array or vector of queens) I haven’t done this problem yet butI immediately thought composition might be needed. 2. Next create a Queen data type. A queen instance should be able to keep track of its current position (row and column) and be able to move to the next row. Again you can add other methods as you see fit. 3. Add a client program to test your code: m should place the first queen anywhere on the his progra board and then call placeQueens to see if a solution can be reached. If the solution can be reached the program should display the board with the queens properly placed.
Backtracking 179 agonal attack, and you finally place a queen in the third square of column 2, as Figure 5-7b ustrates The black dots in the figure indicate squares that are rejected because a queen in that square is subject to attack by another queen in an earlier column. The blue dots indicate the dditional squares that the new queen can attack We continue to place queens in this manner until we get to column 6 as Figure 5.7e shows if reach an Although the five placed queens cannot attack each other, they can attack any square in column 6, impasse, backtrack and therefore, you cannot place a queen in column 6. You must back up to column 5 and move to the previous ts queen to the next possible square in column 5, which is in the last row, as Figure 5-7f indi- cates. When you consider column 6 once again, there are still no choices for a queen in that col- umn. Because you have exhausted the possibilities in column 5, you must back up to column 4 s Figure 5-7g shows, the next possible square in column 4 is in row 7. You then consider col- umn 5 again and place a queen in row 2 (Figure 5-7h). How can you use recursion in the process that was just described? Consider an algorithm that places a queen in a column, given that you have placed queens correctly in the preceding columns. First, if there are no more columns to consider, you are finished; this is the base case. Otherwise, after you successfully place a queen in the current column, you need to consider the next column. That is, you need to solve the same problem with one fewer column; this is the recursive step. Thus, you begin with eight columns, consider smaller problems that decrease in size by one column at each recursive step, and reach the base case when you have a problem with no columns This solution appears to satisfy the criteria for a recursive solution. However, you do not cur- know whether you can successfully place a queen in the current column. If you can, you re backtrack, as has already been described. that the previous columns contain queens that cannot attack one another nly cosder the netolun If ou cannot place aquenin the curent column you ned to The following pseudocode describes the algorithm for placing queens in columns, given Places queens in eight columns placeQueens(queen: Queen, row: integer, column: integer): boolean The solution combines recursion with if (column BOARD_SIZE) The problem is solved else (unconsidered squares exist in the given column and while the problem is unsolved) Find the next square in the given colun that is not under attack by a queen in an earlier cohmn if (such a square exists) Place a queen in the square Try nexi column if (IplaceQueens (a new queen, first Row, colunn 1)) No queen is possible in the nexi colwnn Remove the new queen Move the last queen that was placed on the board to the next row in that column
Recursion as a CHAPTERS e7se Remone the new gueen return true he method doEightQueens, which callsp thod doE ight The Eight Queens problem is initiated by the me with a new queen in the upper-Jeft corner of the board dol ight Queens( placedueens (a new queen, firstRow, firstColumn) After do sghtQveens has completed, we can display the board, if a solution was found. FIGURE S-8 A solution to the Eight Queens problem the Eight Quess Figure S-8 indicates the solution that the previous algorithm finds. By modifying the argu- ments to placeQeens, you can discover other solutions to the Eight Queens problem. The Pro- gramming Problems at the end of this chapter ask you to consider other solutions to this algorithm, as well as additional modifications. Question 7 Consider a Four Queens problem, which has the same rules as the Eight Queens problem but uses a 4 x 4 board. Find all solutions to this new problem by applying backtracking by hand. Implementing a solution to the Eight Queens problem. You can write a solution to the Queens problem in a variety of ways For example, you can define two classes: a Board class to represent the chessboard and a Queen class to represent a queen on the board. A Queen objo could keep track of its row and column placement and be able to move to the next row. A object keeps track of the Queen objects currently on the board and contains operation placeQueensto solve the Eight Queens problem and display the solution. Eight Board The Board class in the solution described thus far may be represented in a number of ways The simplest representation would be a two-dimensional array, however, such an array wastes

Expert Answer


Answer to This is an object oriented program. book with ONE difference, I’m requiring you test your code (a main). If you have the… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *