General solution to solve different sports results in different languages

Posted by sq2 on Programmers See other posts from Programmers or by sq2
Published on 2012-10-06T13:55:01Z Indexed on 2012/10/06 15:51 UTC
Read the original article Hit count: 258

Filed under:
|
|

I currently have some code that checks if squash and tennis scores are valid, both in javascript and PHP.

This results in 4 blocks of code existing, 2 languages * 2 sports, which does not scale well should any extra sports come around, or extra languages...

How can one describe the valid scores of games via a settings/text file, so that each language can parse them and apply these rules.

I'm stumped with the strange tie break situations in tennis should it reach 6-6 in a set, and also infinite play off in the final set should it reach 2 sets all.

ie:

tennis = {
    "format": [
        {
            "name": "sets"
            "min": 3,
            "max": 5,
            "winby": 1
        },
        {
            "name": "games"
            "min": 6,
            "max": 7,
            "winby": 2
        }
    ]
}

squash = {
    "format": [
        {
            "name": "games"
            "min": 3,
            "max": 5,
            "winby": 1
        },
        {
            "name": "points"
            "min": 15,
            "max": 0,
            "winby": 2
        }
    ]
}

© Programmers or respective owner

Related posts about php

Related posts about JavaScript