Join Simon Allardice for an in-depth discussion in this video Eight Queens, part of Code Clinic: Swift.
- View Offline
- Welcome to Code Clinic for Swift. I'm Simon Allardice. Code Clinic is where each month a programming challenge is given to a group of lynda.com developer authors and we each solve that problem using our language of choice. This gives you a chance to see different ways to approach a problem, the benefits of some languages and technologies over others, a chance for me to dive into some deeper issues than I usually get to explore in other courses, and a few real-world tips and tricks to bring into your day-to-day development. We have a classic, old school computer programming problem called the eight queens.
This well-known problem is used in coding challenges and even more intense interviews and it's not new. It's been around for a long time. Eight queens was proposed by Max Bezzel back in 1848 and first solved in 1850. It's survived for so long and still relevant now because it's a wonderfully pure problem. We can describe it in a minute and invent different ways to solve it for years. But to solve this problem, it won't be about coming up with an innovative user interface design or be about using the latest framework or database technology.
No, this Code Clinic is all about the algorithm, figuring out a repeatable set of steps. To solve this problem well, we're going to need techniques like recursion and backtracking, powerful programming methods that are incredibly useful for a software developer. The problem is simple. We start with a chess board and eight queens, and we need to place all eight queens on the board where no queen can attack any other queen. There are billions of potential combinations to place eight queens on a chessboard, but we've known since 1850 there are only 92 possible solutions, 92 ways to arrange eight queens on a chessboard where they can't directly attack each other.
If you've never played chess, that's not a problem. You don't need to know the rules here. For this, all you need to understand is that in chess, we have a board that's eight squares by eight. We can place one piece in any of these 64 squares, and that a queen can attack by moving any number of spaces in three directions. It can go horizontally, vertically, or diagonally. This means no two queens can be on the same row or the same column, and nor can they be directly diagonal from each other. And yes, there are still 92 arrangements to allow this.
In the following video, I'll show you my solution to the eight queens problem. I'd encourage you to also look at the solutions from other Code Clinics. You'll be able to compare different author's styles and different languages.
Simon introduces challenges and then provides an overview of his solutions in Swift. Challenges include topics such as statistical analysis, searching directories for images, and accessing peripheral devices.