What was a time you got blocked on a simple problem?

A lot of the simple problems I've been facing recently boil down to unfamiliarity with the syntax of the new languages I'm learning. In the last excercise of this sprint, we were told to use getElementsByClassName to assign a certain div to a variable. I duly copied out this code and tried to make it work, but I had forgotten to append this to document, so the method came back as undefined. I had to look into the console to see why it wasn't working, and then google the error message to see some examples of people actually using the function correctly. It was a simple, basic mistake that hopefully I'll stop making with more practise, and I feel that way a lot with web development.


What was a time you solved a problem in an elegant way?

The funny thing about problems is they seem a lot less problematic once you have solved them, which makes it harder to remember your solutions, especially elegant ones, since they always look obvious in retrospect. But there is one that sticks out in my mind.

While I was relearning Java I decided to make a set of generic classes to represent board games, which I could then extend to make specific games. The only one I've made so far is noughts and crosses, but I'm planning on making some more interesting ones later. The programme creates objects to represent the players and stores them in an array. The method I came up with for each player taking their turn looks like this:


                public void runGame() {
                    while (isWon == false) {            
                        for (Player player:players) {
                            takeTurn(player);
                        }            
                    }
                }
            

Essentially what this is saying is: 'as long as no-one has won the game, every player in the array of players takes one turn.' It seems like a simple and elegant way of running the game, but when you put it into practise, you find that the game continues after it has been won - because every player takes a turn provided the game is not won when the while loop evaluates. I had to come up with a solution and the one I settled on was this:


                public void runGame() {
                    while (isWon == false) {            
                        for (Player player:players) {
                            if(isWon == false) {
                                takeTurn(player);
                            }
                        }            
                    }
                }
            

This is really, really inelegant. The programme checks whether the game is won, then goes through each player, checks if the game is won again, then lets them take their turn if it isn't. Ugh. What I need to do (and will do at some point) is change the data structure holding the player objects into a circular linked list, so the method just looks like:


                	public void runGame() {
                        while (isWon == false) {
                            takeTurn(currentPlayer);
                            currentPlayer = currentPlayer.next();               
                        }
                    }
            

Which is to say 'as long as the game isn't won, let the next player take a turn'.


To index