From: Jim Weirich Date: 2006-03-25T22:32:16+09:00 Subject: Re: Newby Maze solution Alex Combas wrote: > Hello folks, > Tonight I tried out some of that good old extreme programming TDD stuff > with mixed results. Good for you! I've been using TDD for over 5 years and it has radically changed the way I approach programming. > I was initially surprised how much it helped get things started but > I noticed after a while that it kind of petered out because I wasn't > sure just how to test the code that I was writing. As with all things, it helps to practice. The secret to TDD is learning to let the tests drive the code. Never write code that isn't dictated by a test somewhere. So, with that in mind, I took your tests and threw away your tracker program. I wrote a new tracker program entirely based on your test code. The following program passes all the tests you supplied, with no extra baggage: class Tracker def initialize(maze) end def track(maze, index) index + 1 end end Hmmm ... doesn't look much like a maze solver, does it? Yet it does pass the tests. So, your next thought should be, what tests do I add to force me to write code that will make this look more like a maze solver. The key is to work with little bits of functionality at a time, so the problem is never too hard all at once. I would suggest that your current test is too complicated. I would start off with the goal of writing a maze solver that can solve really trivial mazes. For example, my first four tests would probably have it solve the following four one step mazes: #### ### #### ### #se# #s# #es# #e# #### #e# #### #s# ### ### Then I would add tests to make sure it could solve multi-step mazes: ######## #s----e# ######## Then I would make sure it could solve mazes with corners. ###### #s---# ####-# #-# #e# ### Then finally I would make sure it can solve mazes that involve possible back tracking: ##### ##### #e--# #---# ###-# ###-# #s--# #s--# ###-# ###-# #---# #e--# ##### ##### At this point, I would suspect I have a fully general maze solver (although by the time I actually reach this point, I may have discovered other potential problems that may need further testing). I might be ready to throw your test maze at the problem and see if it works. The key is little steps that build upon the previous code base. Write a test, write a little code. Write a new test, write more code. Repeat until the problem is solved. Sounds easy, and it is with practice. But it does take some experience to get it down. Good luck. -- -- Jim Weirich -- Posted via http://www.ruby-forum.com/.