Book recommendation “Clean Architecture”

Hi all,

This time I’d been fortunate enough to have the book “Clean Architecture” in my hands. Why fortunate, you may ask. Well, I’d say the the book is a must read if you want to improve your skills when it come to building maintainable software.

Programming principles

The author Robert C. Martin (or “Uncle Bob” as he known as in the industry( goes through a set of software architecture principles coined the SOLID principles. I believe that following these principles you will write better software, as you’ll architect and structure it much better. And I try to do I when I’m programming.

I think the book does a good job of explaining these and through practice I believe you should be able to incorporate these whenever you program. Or at least that is my hypothesis 😉

Writing maintainable code

The book also goes to some lengths to point out what a good software architect should strive to accomplish, namely making code easy to maintain. If you look at online review there are diverging opinions on how many pages should be spent on this, but what I can say is that from my professional experience I have never seen any piece of code, component or system that over time became harder and harder to maintain, as features where added. So, in my opinion, you always need to be diligent about reducing your technical debt or maintenance overhead if you will.

You can go have a look at the book at Amazon

Problems with Wacom Touch Ring not working

I’ve had a Wacom Cintiq QHD27 for some time now. Awesome tool and way too flashy for my skill level in Photoshop. None the less I occasionally have some fun drawing on it (I actually made the graphics for my games using it).

One thing that has aggravated me from time to time is that the touch ring, on the EK remote I got with it, was not responding in Photoshop. I’d like to be using it for controlling the size of the brush, canvas rotation and such, basically speeding up my work flow, but it was just dead somehow. Trawling my way through various support sites I came across a solution that worked for me; resetting the darn thing. I’m not talking one of those software resets here, no no, that would have been too easy. No, what I had to do was pull off the back and push the reset button under the cover, doing a hard reset. But it fixed the problem in the end 😀

I got the guide from here in case you need it (thank you, Frank Doorhof for making the guide)

Hope it works for you too 🙂

Developing on a small scale – making a new app yourself

When you’ve gotten THE IDEA for your next app, how do you go about making it happen? How do you plan on developing it? Testing it? Do you make a list of features you want in it or do you just start writing code? And have you thought about how to store and version your code?

Developing apps for me is definitely development on a small scale, since I’m a team of one. Sure, I bounce ideas off people, scour the net for ways to solve coding issues and present the results to others, but the development process is pretty much just me and my code.

Working like this can be both a blessing and a curse, if you’re doing it like me. On one hand choice of tools and process is up to you and decided by you alone, so it’s going to be just like you want it. On the other hand, if you don’t do something that needs doing, no one does it. That said, having a proper set of tools can make it easier to keep track of all the moving parts and make you focus on what’s important, namely your app.

The tools

All of the tools listed below are free of charge if it’s just you or a few people using them. I find they make collaboration easier, should you need it down the road 🙂

Android Studio: If you’re developing in Java or Kotlin, I definitely recommend using Googles Android Studio. The IDE basically runs out of the box and updates are handled through the environment. This tool has sped up my development process quite a bit, compared to my previous experience with Eclipse and other tools. Android Studio gives you emulator, profiler, Git integration, build engine, a good code editor out of the box. So far that has been all I needed and wanted.

Bitbucket: Your code is probably your most important asset, and you want to keep it safe. Store it somewhere you won’t accidentally delete it. There’s a host of source control systems, but none is more known than Git. Personally I use bitbucket.org, which integrates nicely with Android Studio. If you don’t know what source control is, I recommend you look into it. I find it giving me the added benefit of always being able to switch to a specific version or branch at any time and not having to spend time on setting it up.

Trello: At my full time job (I know, “Whaaat you can’t live off this ?!?”), we’re using the agile methodology. Thru working this way I learned how user stories and features can encapsulate the work everyone is doing and going to do in a fast and efficient manner. Again I highly recommend you check it out, as I find it gives me a good toolbox for capturing and organizing relevant information, even if I’m just one person. Personally I use Trello, which integrates nicely with bitbucket.org, but there’s probably a whole bunch of these tools out there. Never again will I have to store to do lists in emails, on sketchpads etc. and have a billion ways to misplace my notes.

Stack Overflow: I don’t know if this really counts as a tool, but countless times I’ve been looking for a way to resize a button, find an example of how to use a collection or remember the symbol for that XOR operator. I’ve mostly ended up at Stack Overflow. Basically what I’m trying to say is I try not to sit and battle with a problem too long before I look up a solution on the net. Nobody is grading you on how well you know all the answers, but only on how well you application is made. If credit is due to someone who helped you solve a problem, then give it. It doesn’t detract from your ability to make great stuff.

These are the tools I’m currently using, so feel free and give them a try. In the meantime I’ll start working on my next app. I hope to document a little bit about the process for you to see.

Until then Happy New Year to everyone 😀

(feature image from Good Free Photos)

Authentication using WordPress

Hi,

From time to time I’ve made a few PHP pages, for various reasons. Some of these use a MySQL database for storing and retrieving data. When it comes to securing my data and restricting access to editing and deleting functions to me only, I’ve looked at a few simple solutions to the problem. One of the ways I’ve done this, and the one I prefer, is by letting WordPress handle the authentication of the user. The WordPress authentication can probably be bypassed, but since it’s not personal or top secret data, I’ve come to the conclusion that a basic protection is adequate for these data.

How to do it?
First off you need to use WordPress own login page and the users you want to let on to the custom page need to have access to log onto the WordPress site you’re using for authentication purposes.
In my PHP code I’ve included the following lines (the page is located in an adjacent directory to the WordPress installation):

require(‘../wp-blog-header.php’);

and

if(is_user_logged_in()) {
   // Show the page, since the user is logged in
} else {
   // The user is not logged in
}

The inclusion of the wp-blog-header.php file is required if you want to be able to use the method is_user_logged_in(). It is this method that check to see if the user is logged in (surprise, surprise!). Then all I need to do is put the magic inside the curly brackets between the if and the else. Easy, peasy 🙂

Things that I haven’t checked
There are some obvious things that I haven’t checked, like if it’s possible to circumvent the authentication and if so how easy it is. I also haven’t looked into the possibility of making a special right for the page, but just given access to any user that can log into the WordPress site that I piggyback my authentication off.
The solution is simple, I know, but it covers the bare minimum I needed for the pages I’ve created. Oh, and any updates to the login procedure is not my headache, since they get handled with the WordPress updates. Which is probably better than what I could piece together on my own anyways 😉

Cheers!
Lars

Building an AI (part 2)

This is part 2 of a series on building an AI for the dice game “10.000”. To read part 1 go here.

 

The mission

My goal is that at the end of this post I’ll have a set of formal rules that can be coded into the AI. I want to know what the odds are for scoring when rolling any number of dice and how many points I can expect to score. I also want to make a simple decision tree for when to roll and when not to roll.

 

Calculating the odds

Since I’m no genius when it comes to statistics, I read a few articles on the dice probabilities at different rolls. For those of you that are interested you can find some mathematics explained here, here and here. These articles give us a basis for determining what the chances are of not being able to score points on a given roll.

In the following I’ll be listing tables showing the number of combinations that score points and the probability of these when rolling X dice (the columns “# successes” and “Success %” respectively). Conversely I’ve also listed the chance of not rolling points (the “Failure %” column). In the first table I’ll assume no 3-of-a-kind has been rolled or what I’d like to call basic probability.

The total number of combinations is calculated as 6^x, where x is the number of dice rolled.

Basic probability

Dice # Successes Success % Failure %
6 45.576 97,69 % 2,31 %
5 7.176 92,28 % 7,72 %
4 1.092 84,26 % 15,74 %
3 156 72,22 % 27,78 %
2 20 55,56 % 44,44 %
1 2 33,33 % 66,67 %

 

When rolling 1, 2 or 3 dice it also becomes relevant if there has been rolled 3-of-a-kind previously, since it increases your odds of rolling dice that can score points. In the second table I’ll assume that there has been rolled 3-of-a-kind of 2’s, 3’s, 4’s or 6’s, since these affect the odds.

3-of-a-kind already rolled

Dice # Successes Success % Failure %
3 192 88,89 % 11,11 %
2 27 75,00 % 25,00 %
1 3 50,00 % 50,00 %

 

Diving deeper into the combinations

These odds can be divided further into the different combinations of dice. I’ll list a separate table for every number of dice rolled, sorted by their success probability in ascending order. The column “Combinations” is the name of the combination you scored. “# Successes” and “Success %” express the same as before.

The row “Two 3-of-a-kind” have been included since we use all the dice and unlock all 6 on successive rolls. The row “1 or 5” is for the cases when none of the other combinations occurs.

 

Rolling 6 dice

Combinations # Successes Success %
6-of-a-kind 6 0,01 %
5-of-a-kind 180 0,39 %
Two 3-of-a-kind 300  0,64 %
Straight 720  1,54 %
Three pairs 1.800  3,86 %
4-of-a-kind 2.250  4,82 %
3-of-a-kind 14.400  30,86 %
1 or 5 25.920  55,56 %

 

If you sum up “# Successes” you’ll luckily end up with 45.576, which is the same number as listed in the basic probability table above. In the next two tables the process is repeated for four or five dice.

 

Rolling 5 dice

Combinations # Successes Success %
5-of-a-kind 6 0,08 %
4-of-a-kind 150 1,93 %
3-of-a-kind 1.500 19,29 %
1 or 5 5.520 70,99 %

 

Rolling 4 dice

Combinations # Successes Success %
4-of-a-kind 6 0,46 %
3-of-a-kind 120 9,26 %
1 or 5 966 74,54 %

 

The next three tables are for rolling one, two or three dice without having rolled a 3-of-a-kind previously.

 

Rolling 3 dice

Combinations # Successes Success %
3-of-a-kind 6 2,78 %
1 or 5 150 69,44 %

 

Rolling 2 dice

Combinations # Successes Success %
1 or 5 20 55,56 %

 

Rolling 1 dice

Combinations # Successes Success %
1 or 5 2 33,33 %

 

You could also be rolling one, two or three dice when you’ve already rolled 3-of-a-kind. The following tables shows, where I’ve added the row “Add to 3-of-a-kind” for this. Remember the odds only change when the 3-of-a-kind is 2’s, 3’s 4’s or 6’s.

 

Rolling 3 dice with 3-of-a-kind already rolled

Combinations # Successes Success %
3-of-a-kind 5 2,31 %
Add to 3-of-a-kind 91 42,13 %
1 or 5 96 44,44 %

 

Rolling 2 dice with 3-of-a-kind already rolled

Combinations # Successes Success %
Add to 3-of-a-kind 11 30,56 %
1 or 5 16 44,44 %

 

Rolling 1 dice with 3-of-a-kind already rolled

Combinations # Successes Success %
Add to 3-of-a-kind 1 16,67 %
1 or 5 2 33,33 %

 

This covers the odds for every combination of dice.

 

Weighting the odds

We now have some probabilities down that we can use to guide our decisions (and the AI’s) on whether or not we want to roll the dice at any given point. However if you just decided to roll based on the probability of scoring alone, you would not be playing optimally i.e. scoring the most points.

Enter Expected Value and Opportunity Cost.

So what is this?

Expected Value (or EV) is the value we statistically can anticipate getting over the long run from our roll. In this case the score we can add on average. The Opportunity Cost is the value we give up by rolling; being the points we could have banked instead of rolling.

How do we use that when deciding whether or not to roll?

What we basically want to be doing is weighting out the two, by saying if my opportunity cost greater than my EV, I shouldn’t roll. But why is this correct? Let’s take an example. You have one dice left and have already scored a 1, a 5 and 3-of-a-kind of 2’s. Your chance of rolling and scoring is 50%. You could either be rolling a 1 scoring 100, a 5 scoring 50 or a 2 scoring 200 more. This would give you an average score of 116,67, but only on 50% of your rolls, giving you an EV of 58,33. Your opportunity cost is your current unbanked points of 350 times the chance you’ll miss out on points on the next roll giving you 116,67. Since the opportunity cost is greater than the EV you should be banking instead of rolling here.

To put it simply:

If EV > Opportunity cost, then roll.

Otherwise bank the points.

This is not the complete answer though. It would be if we always banked after this roll, but we could choose to roll again and again. Therefore we need to factor in the EV of successive rolls, at least to some extent. But how do we do this, since you can score and therefore remove a different number of dice after each roll? I’m certain that smarter and more mathematically experienced people would be able to solve this puzzle, but I accepted that the heuristic I’m building will not guarantee that the optimal solution would be found every time, which I why I’ll believe I could employ a short cut here. Calculate the chance of having scored all six dice at the end of the roll when rolling any number of remaining dice. Then weight this by multiplying by the EV of rolling all six dice. This post is however getting quite lengthy, so I’ll save this part for now.

 

The Expected Value of a roll

Working on the combinations I listed earlier I will now map out the EV of every combination on every number of dice rolled. By doing this we get the last set of numbers we need to make an equation for when to roll and when to not. The assumption is that you’ll score the maximum number of dice.

 

Rolling 6 dice

Combinations EV
6-of-a-kind 2.000
5-of-a-kind 1.525
Two 3-of-a-kind 1.000
Straight 1.000
Three pairs 750
4-of-a-kind 1.050
3-of-a-kind 575
1 or 5 156

 

Rolling 5 dice

Combinations EV
5-of-a-kind 1.500
4-of-a-kind 1.025
3-of-a-kind 550
1 or 5 139

 

Rolling 4 dice

Combinations EV
4-of-a-kind 1.000
3-of-a-kind 525
1 or 5 121

 

Rolling 3 dice

Combinations EV
3-of-a-kind 500
1 or 5 105

 

Rolling 2 dice

Combinations EV
1 or 5 90

 

Rolling 1 dice

Combinations EV
1 or 5 75

 

Rolling 3 dice with 3-of-a-kind already rolled

Combinations EV
3-of-a-kind 500
Add to 3-of-a-kind 3-of-a-kind value * 108/91
1 or 5 105

 

The fraction in the “Add to 3-of-a-kind” is to account for doubles and triples. I counted these by hand.

 

Rolling 2 dice with 3-of-a-kind already rolled

Combinations EV
Add to 3-of-a-kind 3-of-a-kind value * 12/11
1 or 5 94

 

Rolling 1 dice with 3-of-a-kind already rolled

Combinations EV
Add to 3-of-a-kind 3-of-a-kind value
1 or 5 75

 

That should be enough numbers to feed into the equation.

 

What if any kind of special rules take effect when playing the last round?

During the last round we could use the equation, but it is not enough to bank any number of points. It only matters if your total score exceeds that of all of your opponents. A simple solution is to keep rolling until you have more points (both banked and unbanked) than your opponent.

Put simply:

If banked + unbanked points <= opponents score, then roll.

Otherwise bank

 

That should be enough to implement a set of rules for the AI.