XP is just a number | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Hi, heres a few comments, plus an alternate solution to the problem. I fully empathize with your difficultly finding a way to learn to program. There is a large gap between the beginners books and the reference books. It may be helpful to try something that you can use yourself. A very simple address book, or todo list would be a good start. If you do try something like this however make sure you start very simply. Don't try to add features until the basics are working. A simple, working application is much better than an ambitious broken one. Now on to your code. You've written this in an OO'ish way, but thats not always necessary. I can appreciate that you might be practicing OO coding of course, and your syntax isn't bad. I think the largest problem with OO coding in your example is that your one object corresponds to the entire game. You are making it more difficult for yourself, as your trouble with the @history array shows. It needs to be visible to a couple of subs, but its not part of your object. Your _checkSum($self,$pile); is better written as $self->_checkSum($pile) $self is automatically passed in as the first argument, because of the -> method call syntax. I don't see a repetition of _checkSum if its successful. The rules of the game state that if you can remove the three cards you should check again, and keep removing any matches until you can't match. Below is another solution to the problem. I haven't used any OO features. I also didn't bother implementing reading the card lists from STDIN. Here are a few notes about my code that might interest you. Most of the variables are visible throughout, since they are needed by the subs. I've made liberal use of array slices. @$stack[0,1,-1] return the first, second and last items in from the arrayref $stack. The % (modulo) operator returns the remainder from a division. 10 20 or 30 % 10 all return 0, while any of the other possible card sums will return some number. So we can condense three tests into one. After getting the values form the stacks and pushing them on the deck, you have to remove the elements from the array. You did this with shifting, poping and spicing, but it can also be accomplished in other ways. I set the elements values to undef, then grep'ed the list onto itself. To fix the bugs in your own program, you might want to add warn statements throughout similar to those below. I spent most of my time trying to figure out what was the "bottom" or "top" of the deck and stacks. But looking at the warn statements and checking each against the game rules let me fix the errors. Good luck coding! There's always plenty more to learn, and I by no means consider myself proficient. If anyone wants to improve my code I'll be very happy.
In reply to Re: '96 ACM Problem A
by Anonymous Monk
|
|