7 card hands

Further to what I discussed here, interestingly enough one of the pieces of work in the Udacity course CS212 is to write a function to find the best hand of five cards in a group of 7.

So I've still got the problem of dealing out the flop (cards on the table) as well as the hands for each of the players, but I can come back to that, this is a 'killing two birds with one stone' moment.

Now, out of seven cards, let's call them C1-C7 (or C0-C6 if you prefer), how many different hands could I have?
Hmmm. Well, out of my set of 7, I can discard any two; and working through the sets 1-2, 1-3, etc up to 6-7, I can get 21 different sets; leaving me with, conversely, 21 sets of 5.

Remembering good coding practices, however, we want to generalise. So rather than doing something like:

for i in 21:
    hand = [1, 2, 3, 4, 5, 6, 7] - discard[i]

where discard = [(1,2), (1,3)...]

Actually, hang on - maybe that isn't so bad. As long as discard is generated by a function which is more general, discard itself can be fixed values.
To explain what I'm talking about here, discard takes the form of a list, with each item on the list being the two cards to discard, leaving a five-card hand.
It is possible to have a program where discard is 'hard-coded', meaning that my code literally says:
discard = [(1,2), (1,3), (1,4), (1,5), (1,6), (1,7), (2,3), (2,4), (2,5), (2,6), (2,7), (3,4), (3,5), (3,6), (3,7), (4,5), (4,6), (4,7), (5,6), (5,7), (6,7)]
Now that has three problems with it:
1) it's ugly. Yes, this matters!
2) it's error prone - what if I mis-typed one of those options? A hand will appear twice, or not appear at all. What is more, this means that this scales poorly - or in other words, if I had to do this for sets of 100, it's massively more difficult.
3) it's hard to adjust.
Having all that data hidden away in discard is fine.
In fact, I think it will be clearer if I generate discard elsewhere and then just refer to it here.

Right, enough messing about. I decided that Python must have a way of handling what I want to do; so I did some research and found: this Python documentation of something called itertools - or methods for achieving things which require a lot of iteration.
Specifically something called combinations. Combinations("ABC", 2) would give AB AC BC - perfect!
No need to mess about with what I was talking about with discard as a first step, I've jumped to having all the possible 5-card hands output from a 7-card input.
Now what I need to do is rank the 5-card hands against each other - well, all I need is to run hand_rank (a function already written as part of the Udacity material) - and it's job done.
So after all that, the result looks like this:
def best_hand(hand):
    "From a 7-card hand, return the best 5 card hand."
    hands = itertools.combinations(hand,5)
    return max(hands, key=hand_rank)
Back to the problem of dealing.


Poker? Poke 'er!

Having mostly completed CS 101 (Introduction to Computer Science, aka Build a Search Engine), which was awesome, I just need to wait for a final exam to come out.

So I have moved on to looking at CS 212 (Design of Computer Programs), which has so far focussed on a program to judge winning poker hands, with an added bonus of a procedure to deal the hands. And here I hit a problem, because that procedure is:

def deal(numhands, n = 5, deck = [r+s for r in '23456789TJQKA' for s in 'SHDC']):
"Shuffle the deck and deal out numhands n-card hands"
return [deck[n*i:n*(i+1)] for i in range(numhands)]
Just to explain what's going on here:
First it defines (def) a function called 'deal'. Deal can take as inputs the number of hands to deal (numhands), the number of cards per hand (n), and the definition of the pack of cards (where r is the card values, and s is the suits).
Then there's a bit of a description of the function, before shuffling the pack randomly.
Finally the idea is to return an output where:
we get a number of hands (numhands), each containing n cards, by counting out the first n cards, then the next n cards, until the hands are complete.

This is okay for a whole bunch of card games; in fact, considering poker it would work for Five-card Stud, which may be what was intended. But what I've always played is Texas Hold'Em, which has a different structure.
For anyone who hasn't had the pleasure of playing Hold'Em, the basic structure is that each player receives two cards in their own hand, and there are then five cards dealt face up on the table. Each player makes their best hand of five.

So I've tried to put together a function to do that job:
def deal(numhands, n=2, face=5, deck=[r+s for r in '23456789TJQKA' for s in 'SHDC']):
    hands_out = deck[0:face] + deck[(n*i+face):(n*(i+1)+face)] for i in range(numhands)]
    return hands_out

which unfortunately...doesn't work.
I believe the logic is sound; I'm just getting some part of the syntax wrong. I'll come back to it; once this works, I will also have to make an additional function to work out the best hand of five cards from the seven available to each player, and the rest of the code should then follow.


The reason for my absence

I have been having far, far too much fun diving right into the courses at Udacity. I will raise one caveat - the instruction is very Youtube-heavy, so if your Internet is slow the experience could be somewhat frustrating.

That said, the instruction is engaging; the pace is good, and the diversions entertaining. I have also greatly appreciated the fact that you generally only sit watching/listening for a couple of minutes before you are asked to once more engage brain and do something.

Having initially focussed on the introductory course, because all the material for it is accessible now, I already feel that I have a reasonable grasp of Python.
Incidentally, as well as teaching me a wholly new programming language (shiny!), I've had a refresher on some of the principles of Object Oriented Programming, which is handy
Give it another couple of days working at it, and I expect that I will have achieved the initial promise, of writing a search engine.
Yes, that's right, using Udacity, Python and a few days of my time, I too can create a Google! Cue maniacal laughter.

Oh - and one last thing - it isn't too late to sign up!


Sire, there is no royal road to learning*

I talk a lot about the frustrations of learning to program; there are a number of reasons why there may be more than the bare minimum going on here, but there is always some frustration in programming.

So today I thought I'd share some alternative routes to getting into programming, other than this somewhat foolhardy "Dive right in and use the Internet!" approach which I've taken.
First off, there are real live courses at actual Universities, or FE Colleges near you. However, costs can be prohibitive, and a lot of the time you need to be in a classroom, so working it around a job isn't exactly practical.
So we move on to distance learning. Well, there's always the Open University; but their costs can still be substantial.
There are also books like Dummies Guide to... or a similar idea for Android specifically. I learned a bit about Visual Basic for Microsoft Excel from a tome that seemed big enough to be the Liber Paginarum Fulvarum**, though I got more from diving into already-written software and then referring to the book when in trouble than from working through it in any systematic way.
Moving right along, there are also respected academic institutions who have some or all of their courses available online. That could be very handy, but there's no interaction, so it might not be much more rewarding than a book or similar.

There's also a Wiki-based solution and other similar efforts out there; and for anyone who has a grasp of the basics but wants to get into more real problems, there is the excellent OpenHatch, set up to prepare people for getting involved in Open Source projects.

Finally, there is the one that I am trying at the moment, which I discovered through an excellent blog I read called Unequally Yoked; Leah who blogs there has recently been taking courses including How to Build A Search Engine over at Udacity and recommended it highly. The next round of classes is starting on Monday (the 16th April) so if you want to have a crack at it, come join the party!
Because I have previous programming experience but no knowledge of Python, I've gone for How To Build a Search Engine (which is an introductory class), Programming Languages and also Designing Computer Programs.
For the inexperienced I'd advise starting with How to Build a Search Engine and leaving it at that. I may find that three at once is a tad ambitious, but I had to resist the temptation to sign up for all six, so I think I'm doing well!

*Slight misquote of Euclid, speaking to Ptolemy "Sire, there is no royal road to geometry"; the meaning remains - if you want to learn something useful, expect it to take a while. Viz the 10,000 hour rule.
**by Achmed The I-Just-Get-These-Headaches


Testing Test...

Spent much of the day going around and around and around in circles. The word is argh.

I begin to wonder whether I should try condensing the app into one view for now, then trying to figure out how to add a second view separately...might be a way to separate out the issues enough to attack them productively.


Source Not Found

I haven't yet found any useful suggestions on dealing with Source Not Found; so, I'm going to take some stabs in the dark and see if any of them result in a scream.

First off, I know that I have said that the warnings aren't that important, as long as there aren't any actual errors. How about I just test that; I comment out the unnecessary import statements [a simple /* on one side and */ on the other does the trick. Alternatively for single lines // works.]

Commenting in this way is a great method to quickly change code around - because if I decide that removing something was a mistake, I just have to uncomment it, not remember how it went and re-type it.

Then the remaining warning is around
private DatePickerDialog.OnDateSetListener mDatePicked;
and how I use it in
mDatePicked = new DatePickerDialog.OnDateSetListener() {
In that Eclipse is telling me that mDatePicked is not used! Um, oops...what's gone wrong here then? I declare mDatePicked, as you see above, then I do some stuff onDateSet.
Now, I have a sneaking suspicion that there's some duplication between these two lines, so let's see what happens if I take one out, leaving
mDatePicked {
 Nope, that errors. Badly. Howabout taking out the first one? Also makes things worse, in that the warning has upgraded to an error.
Aha! Stack Overflow to the rescue! Looks like the whole thing is redundant now I've got
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
So I'll just try cutting out the lot, see what I get. No...that causes even more problems. I'm going around in circles here. I believe the technical term is GAH!


Testing, Testing, 1-2-1-2

To test an app, first thing you need is for Eclipse to not report any errors.

Then you need to feel that there is enough of your app complete to be worth testing. For instance, I don't really-truly care about whether the home screen looks right, if all I can do is look at it. I want to know whether it works when I press buttons.

Then you Save All (Ctrl+Shift+S), and finally you go to the green and white Button for "Run Wedding Logistics".

Having already specified a test machine (in my case using the specs for my own Android phone), it should just load up. Be aware though, this step can be veeeery slow.
And...ah. Oh dear:
Well, at least you can see what Toast looks like! As well, of course, as the appearance of the testing software [which is included in Eclipse].

It does occur to me that most programming I've done will talk about de-bugging, and this seemed to be more of a 'run program' option. Hmmm. *Looks around* Aha!
I clicked on this:
I should have gone for this:
The Debug button. Debugging is one of those terms that is more literal than you think - while the origins are obscure, it was popularised in the days of the first computers, vast buildings full of valves such as Colossus or ENIAC. - when it was possible for a bug like a moth to get into the works and cause the machine to behave in unexpected ways. The archetypal bug is pictured here.

I believe my error is linked to the fact that the tutorials from hackaday were with already completed and tested code - so the bit of seeing it work at the end was just the reward. Anyway, now I hit Debug, so I'll see what pops out:
Source not found.
Ah. I have no idea. Will have to come back to this once I've done some more reading.


Index Post up to Saturday 5th April

For convenience:

I began by introducing myself; the software that I am using; and my first decisions in the design process.

I then made a start on this project, working on a design in DroidDraw; before moving my work into Eclipse, then getting bogged down for several posts as I worked out how to solve a niggling little problem.

That got me up to last weekend, when I disappeared on my stag do, and came back a little hungover; bringing me into this week.

This week I began with switching between the two activities which currently make up the app; fun with onClickListeners, then made some slow progress which culminates in a couple of finishing touches.

Next week we go on to testing.

Finishing touches

As I said last time, I reckon there are two things left to do before I have a module that is worth testing.

First - I want to notify the user when a date is entered successfully. Now, this is not essential in the grand scheme of things, because when the whole app is complete, closing SSA will lead the user to a screen which shows the date (among other things). Whether or not I decide to retain it then, I want to have it now, just so that I know it's working when I test it!

The easiest way to do that is with what is called Toast - I imagine because when it is ready, it pops up! Toast is a kind of message that appears in a little box floating on the screen for a set duration.
Toast has three key components (or 'arguments', in this instance). The Context, being the current state of the application; the Text, being what you want your Toast to display; and the Duration, being how long to display it for.

As usual with Android's imaginative variable names, we get something like this:

Context context = getApplicationContext();
        CharSequence text = "You are getting married on " + WedDate "!";
        int duration = Toast.LENGTH_LONG;
        Toast toast = Toast.makeText(context, text, duration);
As you can see, here we will tell people "You are getting married on []!", for a duration of Toast.LENGTH_LONG. I don't know exactly how long that is, but it's a few seconds; certainly long enough to read this message.

Alright, so on to the second thing I wanted to add - and this is probably entirely temporary: a message on WLA to confirm that the app knows the user's wedding date.
I've put exactly the same Toast, just replacing "You are getting married on " with "Your wedding date is ", so that if I close SSA and get back to WLA at the same time as a Toast appears, I will know which Activity is responsible for it.

In both these cases, I already had the import statement - import android.widget.Toast - otherwise I would have to add it before Eclipse would be happy.

Now, bearing in mind that 1) This is one small corner of the planned app, and 2) That it may not even begin to work when tested - it's time to start testing this thing, and see what breaks!


Slow Improvement

Aha! It gets better - I have not only discovered that it is possible to make the calendar appear, not just the day/month/year spinners; I have also discovered a method called onDateSetListener. I've even found out how to make a value (like "Yes, a date has been picked") persist from one session to the next.

The calendar thing I'm going to just make note of for now - remembering as I said back here that as long as it works, prettiness comes later.

The method onDateSetListener might explain some of the problems I've had with onClickSetListener - it wasn't quite the right method for what I was doing. I'll try replacing it based on what I've found at the Android Developers' forum and Stack Overflow.

So from
WeddingDate = (DatePicker)findViewById(R.id.datepicker
I now have:
WeddingDate = (DatePicker)findViewById(R.id.datepicker);
        mDatePicked = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
I think that's progress, of a sort? At least, Eclipse isn't flagging any major errors, and I can put something into the method stub to cover the last point I mentioned up top - making sure that once a date is picked, the user doesn't have to do it again.

That's where this comes in. Now, the example given isn't quite right - it's for a setting that the user might change at any time, so the setting is re-saved every time the application closes. I want mine to save the setting solely when the user finishes with SSA - if they ever say they want to change the date, I'll get the app to just bring them back to SSA.

So, in WLA I have put
        SharedPreferences settings = getSharedPreferences(WedPrefs,0);
        String WedDate = settings.getString("WedDate", "");
which is aimed at getting together an object called "settings", which in addition to knowing the Wedding Date can also hold other information in future. I've picked my variable names and properties such that they match up with the if block I created here.
Coming back to SSA, where it said //TODO above, I've now got
SharedPreferences settings = getSharedPreferences(WedPrefs,0);
SharedPreferences.Editor editor = settings.edit();
WedDate = String.valueOf(year) + String.valueOf(monthOfYear) + String.valueOf(dayOfMonth);
This still isn't quite right - the string WedDate operates, and becomes year + monthOfYear + dayOfMonth [for example the 19th May 2012 would appear as 2012419* which I think is the easiest way to grab it back out afterwards] but then Eclipse isn't keen on editor.putString(WedDate); and I'm not sure why.

Well, I wasn't. I tried typing it again, and when the tips popped up:
I saw that the putString method requires two separate arguments.
By analogy with the method in WLA, the first argument has to be "WedDate".
Because of the name I've given in SSA, the second argument has to be WedDate. Fortunately one is in "" and the other is not, so that isn't confusing at all!
May want to change one of those names, I'll think about it.

In any case, this has taken a while, but I've nearly finished the initial stage. There are two immediate steps remaining - one, a message to inform the user whether the date is entered successfully, and two, something on the WLA to state whether a date already exists.
Then to build this into the full app, I'll need something to direct the app back from SSA to WLA - if I understand Android convention correctly, that will be automatic as long as I close down SSA correctly.
Then I can start testing it.

*2012419, you say? Yes. 2012 for the year; then Java counts months from 0 to 11, so in spite of being the fifth month May comes out as 4; then 19 for the 19th. I think that the month will show as 4, not 04...
Some systems counting from 0 and others from 1 is the reason for fence-post errors!


Struggling with SSA (no, not what you might think!*)

'scuse shouting, this is frustrating.

The latest thing I've tried is:
        DatePicker WeddingDate = (DatePicker)findViewById(R.id.datepicker);
 based on an example I found for implementing an OnClickListener.

[OnClickListener being a method which can look out for an action by the user and take action when it is detected.]

However, Eclipse doesn't like this - the specific error it's giving me is

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (SplashScreenActivity)
 The above is shown in red to give some inkling of how angry Eclipse is with me. Now, I actually just realised an error in the above - I already defined WeddingDate as a DatePicker above, so re-defining it like this is just going to confuse Eclipse. So I take that out to get:
WeddingDate = (DatePicker)findViewById(R.id.datepicker
Main difference? WeddingDate is now highlighted in blue - Eclipse has recognised it as a defined object. The error is still marked, however.
My best guess is that I shouldn't be using an OnClickListener on a DatePicker. Hmmm. There is a way to use auto-completion to check that.

Aha. By typing
I get a list of methods whose names begin "set" that might apply to the object WeddingDate. It's a long'un, but the beginning looks something like this:

and as I go down the list, setOnClickListener is a valid option.

Better yet, when I take the suggested variable of setOnClickListener(l) I get a different error.
l cannot be resolved to a variable
Which is fine, because it shows me that the previous error related not to the method setOnClickListener, but only to the argument, which the first time is "this" and the second time is "l". They've given me different errors because in Java "this" has a default meaning (thus the purple colouring) and "l" does not.

I do not know how to resolve this at present, but I'm sure I can come back to it.

*I realized after dubbing my SplashScreen as SSA** for the purposes of this blog that in the parlance of American Evangelicals SSA stands for same-sex attraction; as such "struggling with SSA" is the process that homophobic Evangelicals have to go through if they realise they are homosexual.
I don't feel attracted to men, and even if I did it wouldn't change the fact that I am attracted to women; so not only is the wedding still on, but the post title might be misleading to people from that world.

** I learn from the fiancée that SSA is also a term in Chemistry for the Steady-State Approximation.

† SSA also stands for the Secular Student Alliance who are all kinds of awesome if you're USian and not so keen on the Religious Right.

Nested footnotes FTW!

Do we have a date, dear?

I have this ongoing problem of how to persuade my app to listen out for word from the calendar on the front page [for a quick reminder, that looks something like the below:]
I need this page to notice when a date has been entered; save the date somewhere accessible for all other parts of the app; then call up the next page of the app.

As my last few posts have alluded to, this is where not having worked with Android or Java before is really kicking my arse - because this is basic stuff, and I'm having to do a lot of reading and looking at example code to figure it out.
As a result, some of my code may be more apt for an entry on The Daily WTF than Google Play [as the name suggests, the Daily WTF is a blog exposing code that makes programmers go WTF!?]. That in itself doesn't trouble me, as long as it works.

So, with no further ado, here's the next bit I've been working with.

        if(WedDate.matches("")) {
        Intent intent = new Intent(this, SplashScreenActivity.class);
        startActivity(intent); } 
To give a little background, this is in WLA, the main view for the app - and before loading, it is checking whether there is a value for WedDate. If one exists, then great - the app can continue in this page. If however the app doesn't know the date of the wedding, it loads SSA to ask the user for it.
As best I understand it, this is the best way to do things - I don't want to load WLA and then force new users to navigate away from it, and I don't want to make regular users faff about with the date before going on to do useful things.

So far so good - no errors being marked by Eclipse here! Doesn't mean there won't be a problem at run-time, of course, but...baby-steps.
On to SSA, and what we do when the user inputs a date.



Well, after a rip-roaring stag do (yesh, it was awesome, thank you very much), I need to get back in the groove. I spent most of today reading [yes, on topic reading! Android theory from here mostly], but not a lot of writing.
See if we can't reverse that balance tomorrow.