Log in

No account? Create an account
26 November 2013 @ 12:55 pm
Programming Class Week 6  
This week was all about classes and objects and object-oriented programming.

Hmm. It's a good thing this class doesn't cover hardware or I'd be writing about master/slave systems.

Anyway, this week we programmed a graphical implementation of Blackjack. To my surprise, the graphical part was really easy--it was the logic of getting the cards to work that was hard and required some banging of my head against the wall. The way they suggested we did it involved three object classes: a Card class that handles the individual cards, including assigning values and suits to each of them and retrieving the proper image for drawing a single card to the GUI, all of which was implemented already in the program template we got; a Hand class that combines cards into a hand, deals with drawing new cards from the deck, and calculates the value of the hand for comparison, and a Deck class that assembled the proper 52 cards into a deck, figured out which card was on top and passed that card to each hand, and shuffled itself when starting a new game. Each class also had a method to report its contents which wasn't useful in the final program but was really, really helpful when I was trying to figure out what the hell was going wrong.

And oh, did things go wrong. Not with the graphical part--that was actually really easy to implement. But getting Hand and Deck to work was an adventure. First I couldn't get it to properly calculate the value of a hand, so it was a wildcard whether you won or lost. Then it was ignoring the ace special rules completely, so the classic blackjack hand was only worth 11. Then the Deck wasn't properly drawing cards, so I was pulling cards aren't that weren't on the top. Then when I fixed that and got both hands implemented, hitting worked, but standing caused an Index Out of Range error on an attempt to access a relative list element (blahblah[-1], which should get the last element in the list no matter how long the list is). Then I went and fiddled with something totally unrelated to fix the problem that after a single game the hands weren't cleared, so a new game would start with the hands from the previous game plus two new cards for each player. When I fixed that, all of a sudden the list index stopped returning errors because reasons, and despite extensive testing I couldn't get the index errors to show up again. Then I had a problem with calculating the score properly because sometimes I was doing it one way and sometimes another way, and sometimes the testing statement I printed showed the one way when it was actually using the other way, which is why you shouldn't rely entirely on print statements to debug your code. Then I extensively tested the program as an implementation in the console and made sure everything worked.

Then I implemented the GUI without problems. Crazy, right?
Current Mood: jubilantjubilant
Current Music: 8-4 Play podcast