views:

53

answers:

2

I've recently completed an algorithmic sudoku solver in C, one in which three different approaches are taken to solve the puzzle. It was a piece of code I wrote for a Project Euler solution. Quite a bit of fun, I might add...

Anywho, so I'm really interested in getting this little solver into an iPhone app. I really am at a loss for what approach I am to take into getting the grid represented on screen. The worst way I can imagine would be having 81 individual outlets for 81 individual UITextFields... In a cocoa app I would simply embed them into a NSMatrix and be on my way, but there isn't a replacement for NSMatrix on the iPhone.

I'm thinking now about generating an HTML file and displaying that in a UIWebView, but even that doesn't seem the best way to go about this. What would you recommend?

A: 

Write your own specialised UIView and do the drawing yourself.

Gary
+1  A: 

I'd start with something quite simple: a UIView subclass that has an 81 element array and knows how to render this into a 9x9 grid either by drawing in its drawRect: call or by adding subviews or sublayers in layoutSubviews. It should be pretty easy to map between array indexes and grid rects. This view can also draw whatever background grid/lines you want.

Jason Foreman
This sounds like a great idea. I've also never subclassed UIView in this fashion before, and it would be a great learning experience. Are there any good guides on how to go about this? My two iPhone books have nothing on the subclassing of anything...
Matt Egan
It's really as simple as creating a UIView subclass (Xcode has a template for this when you add a new file), then putting some drawing code into the -drawRect: method. For the drawing, you can look into UIBezierPath for creating your grid, and NSString has some convenience methods for drawing text. Or you can drop down to CoreGraphics/Quartz for some additional drawing APIs. The Quartz 2d programming guide is a good place to start if you go that route.
Jason Foreman
Thanks! I've gotten a grid drawing itself now, and am starting to work on drawing the text... do you recommend using placing a UILabel as a subview, or drawing the text with CoreGraphics?In both situations, I'd like the text to be fairly centered in the box, vertically and horizontally. How would I find out how much width and height a string of characters (a number) is going to take?
Matt Egan
You probably want to look into the NSString UIKit Additions category, which has methods to calculate the size and draw string within a given rect. You could just add UILabel subviews too if you don't mind managing 81 of them.
Jason Foreman
Jason, thanks so much for your help. I've now got a working Sudoku grid. I've set up the grid in a CGLayer and the Numbers in another CGLayer, and they get drawn if either of them is nil when draw rect is called, the Numbers layer can also be redrawn if the numbers need updating. A blue square can be moved around from square to square, and then the two layers are drawn. The grid even detects touch events and moves the selected square, and the view accepts changing numbers at the selected space. I've even got solving roughly implemented :D Thanks again!
Matt Egan