Computer Science Teacher
Computer Science Teacher - Thoughts and Information from Alfred Thompson

September, 2010

  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Would you play this game?

    • 11 Comments

    I love puzzles and small little programming projects. When you can combine those two at get a project that you can use for a good learning/teaching experience it is just a bit of gold. When the concepts you can get into the project go beyond programming so much the better. And I think I have found one of these nuggets. About a week ago someone sent the following puzzle to a mailing list I am on.

    You walk into a casino and discover that they are offering an exciting new game called 52-Card Match, which is played with two ordinary 52-card decks of playing cards.

    The player is handed one deck, and the dealer takes the other. The dealer thoroughly shuffles his deck and sets it face down in front of you. You can shuffle your deck as thoroughly as you please and then also set it face down in front of you.

    The cards are then turned over one at a time from each deck, i.e. one from each deck is turned over simultaneously each time. If any pair matches exactly (rank and suit), the game is over and the casino wins. If you make it through the whole deck without a match then the player wins, and is paid out at 2:1 odds.

    Should you play this game? What would fair odds be?

    Now there is surely a good mathematical way to figure this out. No doubt it would make a great project for a class in statistics. But I’m a programming guy. I did take statistics in college – two or three courses of it – but it’s been 35 years and I haven’t kept up with it. What  I do know how to do is simulate the game. So that is what I did. Basically what I did (rough outline) was this:

    1. Create two 52 element integer arrays and fill them with the integer values 1 through 52.
    2. “Shuffle” the arrays. I used random numbers to pick two elements to swap and did that n times. I tried a couple of values for n.
    3. Create a loop to check each element of the arrays to see if the values in element x in both arrays was the same and report that if it happened.
    4. Then I put all of that in a loop so I could run the simulation multiple times. I wound up running it a couple of million times.

    The results surprised me. Surprised me a lot. In fact this morning I had to take out two decks of real cards and try it in real life. Simulation and real life had the same sort of results. Makes one feel good when that happens.

    I think this might be fun for students as well. And of course you have arrays (parallel arrays probably but maybe a two dimensional array if you want), loops, and the ever popular random numbers. The math/statistics involved are potentially interesting as well because you can compare the expected results with what you see in the simulations.

    For added fun (and complexity) you could keep track of how many times there was a match in each run of the deck comparisons. Perhaps there is a variation (you have to match three or more times  to lose for example) you could find that actually makes more sense to play? Get the kids in AP Statistics to do the math for you and compare their results with the simulation.



  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Being Careful About Examples

    • 5 Comments

    Yesterday I posted some very simple sample code. I have to admit that I spent a lot of time on some of the details but may not have spent enough on some of the other details. This is always an issue when working on sample code for demonstrating a specific, often detailed concept. This is also an issue with code for tests and quizzes BTW. I remember one of my APCS students complaining that the APCS test was all about showing bad coding examples. A bit harsh perhaps but when writing code for a test one is very constrained by space. The good news is that these examples can lead to having some great conversations with students. For example, in the case of yesterday’s code samples I received email from a teacher friend of mine calling me out. Sort of.

    Should you teach students to break out of loops or to use a better loop to meet the need? In your programs today(?) you use a for loop to go through the elements until the end, but if you find a bad value break out. Shouldn’t you have used a do while loop? That way you place clear conditions for when to stop looping?

    One example of the code in question is this C# example.

    for (int i = 0; i < t.Length; i++)
    {
        if (!Char.IsLetter(t, i))
        {
            MessageBox.Show("Alphabetic characters required");
            break;
        }
    }

    In that code I use a break statement to exit the loop early. Standard structured programming, as I learned in college many years ago, says that is bad. The “rule” is that a loop should have one and only one exit point. When you add more exit points you add the potential for problems with people understanding the code, maintenance issues, and unexpected consequences when programs are extended or modified in the future.

    A more strictly by the rules structured programming code might me something like:

    int i = 0;
     
    while ( i < t.Length && Char.IsLetter(t, i))
    {
        i++;
    }
    if (!Char.IsLetter(t, i))
    {
        MessageBox.Show("Alphabetic characters required");
    }

    That’s probably still not the best way but it at least demonstrates the principle of one exit for the loop. This brings up the point I was making earlier about simple samples being more difficult than it may seem. The first example shows the comparison pretty clearly in a simple if statement. The second example shows the same thing but the Boolean expression in the while is more complicated then the one in if statement. Also in the second example the loop “feels” kind of forced to me. Of course often times in a real program there would be other things going on inside a loop that we are using. In a more complex “real” program the value of using the while  loop might actually be more obvious.

    We could write a very complicated example that would demonstrate several concepts or features of course. The problem there is both one of focus  and potentially too much complexity making it hard for beginners to understand the code. Often then we are faced with the real trade off between great code all up and simple easy to understand samples that may not be ideal code.

    When these examples sparks conversation in or outside of class be sure to take advantage of these as real learning opportunities. You can always pretend you did it on purpose. Smile



  • Computer Science Teacher - Thoughts and Information from Alfred Thompson

    Students And Intellectual Property Rights

    • 2 Comments

    Earlier this week a friend sent me a couple of links to articles on intellectual property and students. One was about piracy (Future Tense: Piracy Revisited) and it consists of an author explaining how “getting stuff for free” can be stealing. This article mirrors very closely conversations I had with students over the years. It doesn’t often go well. Very often students don’t see how these issues (piracy) really impact them. And lets face it for many students, especially in high school and earlier, it is “all about them.” That is where the second article comes into play.

    The second article is about students being able to retain the rights to their own work. (Analysis: Solutions to Student Ownership and The DigiPen IP Problem) The issue here is that some of the top game development educational programs retain the intellectual property rights of the students in their programs. In other words, students who create games as part of their course work are not permitted to sell or otherwise distribute what they create. So if a student creates a game for a course and wants to submit it to the Xbox Live Marketplace they can’t. I would imagine that a lot of students would like to recover some of that tuition money this way.

    Now this is not limited to game development or even computer science courses. Take a look at the WPI IP rules for students. I have heard stories of students deciding that they had a school project that had real money making potential and dropping out of college to go into business. I don’t see this as a great win for anyone. The school loses good will and tuition for no return. The student may or may not make money from their project but they do lose out on more education.

    This issue comes up with competitions by the way. The Imagine Cup competition from Microsoft tries to make very clear that students retain the rights to their intellectual property if they enter. Though, admittedly,  sometimes the legal talk makes that confusing. A number of teams have taken their Imagine Cup projects and made them into businesses though. And that is a great thing! Also as I wrote about the other day, students who take advantage of the DreamSpark program (and are over 18) can enter projects into the Windows Phone 7 market place. Encouraging that entrepreneurial spirit is a good thing.

    In the long run I think most schools would benefit most by letting their students retain intellectual property rights. But you should read some of those articles to see all sides of the issue. It’s not all black and white.



Page 1 of 6 (18 items) 12345»