Courses‎ > ‎AP Computer Science 2‎ > ‎Konstantinovich‎ > ‎

2017-02-07 *HW

posted Feb 7, 2017, 6:13 AM by Samuel Konstantinovich   [ updated Feb 8, 2017, 5:56 AM ]

Goal: N-Queens

"The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two queens threaten each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. The eight queens puzzle is an example of the more general n-queens problem of placing n queens on an n×n chessboard, where solutions exist for all natural numbers n with the exception of n=2 or n=3."

A top down design of an N-Queen solver.

Top Down design: 

Many of the functions and programs you write are simple enough to complete without designing them. This is because you can easily understand the whole algorithm. A formula, or a simple set of if statements don't seem to require a design. 

As you write more complex code, you cannot understand the whole algorithm/process AND all of the consequences of the code you need to write all at once. This is why it is a good idea to DESIGN your program rather than just hack it together by writing code until it works, even though it may seem like a waste of time for now.

Your design will be in plain English, which makes it faster and easier to write than actual code. It is also easier to find errors in your logic, and faster to fix than after you have coded the algorithm. (This is why rubber ducky debugging works because it forces you to state your algorithm in plain English)

Start with the big ideas and no code at all.
After you do that you can write pseudo-code and use high-level* function calls then refine each part as you plan it. 

*high level meaning, placeholder methods that describe complex things, rather than the code to do the complex things:

Afterward, you can think about how to represent the data, and how you will implement the code you planned.