Using Emacs org-mode for a tipping game

I have to give credits for this solution to my colleague Andreas, who wrote the Lisp Code and the Org-Mode table formula. Thanks Andreas.

tl;dr Using Emacs org-mode for a tipping game. Tips are written into an org-mode table. With Lisp and table-formulas it’s easy to count and sum the points, each player got.

Step 1: Rules of the game.

  • Tendency: 2 Points (also when Draw),
  • Goal Difference: 3 Points,
  • Result: 4 Points (also when Draw).

Step 2: Creating the table.

|  1 | DATUM              | G | PARTIE                   | ERG | AXEL | Pnt | ANDR | Pnt |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  2 | WELTMEISTERTIPP    |   |                          | ??? |  Bra |     |  Por |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  3 | DO 12.6. 22.00 Uhr | A | Brasilien - Kroatien     | 3-1 |  2-0 |     |  2-1 |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  4 | FR 13.6. 18.00 Uhr | A | Mexiko - Kamerun         | 1-0 |  1-0 |     |  2-1 |     |
|  5 | FR 13.6. 21.00 Uhr | B | Spanien - Niederlande    | 1-5 |  1-1 |     |  2-1 |     |
|  6 | FR 13.6. 24.00 Uhr | B | Chile - Australien       | 3-1 |  0-0 |     |  2-1 |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  7 | SA 14.6. 18.00 Uhr | C | Kolumbien - Griechenland | 3-0 |  2-0 |     |  2-1 |     |
|  8 | SA 14.6. 21.00 Uhr | D | Uruguay - Costa Rica     | 1-3 |  1-0 |     |  2-1 |     |
|  9 | SA 14.6. 24.00 Uhr | D | England - Italien        | 1-2 |  0-1 |     |  1-2 |     |
| 10 | SA 14.6. 27!!! Uhr | C | Elfenbeinküste - Japan   | 2-1 |  0-2 |     |  2-1 |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
| 11 | SUMME              |   |                          |     |      |     |      |     |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  1 | 2                  | 3 | 4                        |   5 |    6 |   7 |    8 |   9 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|

This is a simple table in org-mode containing the columns for the game information (date and time, group, game itself), the column for the result and then the tips by me and Andreas. After each column containing the tips, I’ve included a column for the points, each from us gets for his tip (named “Pnt”). These columns are filled when I run the function below.

I’ve added a column containing the row-numbers at the left and a row containing the column-numbers at the bottom. We need this information for the org-table formula later (org-table-current-column and org-table-current-dline)

Step 3: The calculation logic

This lisp-code gets the actual result of the game (act) and the bet value (erg) to compare them according to the rules above. It calculates either 4, 3, 2 or 0.


: ;; Tipprunde
: (defun wm-calc-pts (act erg)
: (if (or (not (string-match "-" act))
: (not (string-match "-" erg)))
: 0
: (let ((a (string-to-number (car (split-string act "-"))))
: (b (string-to-number (cadr (split-string act "-"))))
: (x (string-to-number (car (split-string erg "-"))))
: (y (string-to-number (cadr (split-string erg "-")))))
: (cond
: ((and (= a x) (= b y)) 4)
: ((= (- b a) (- y x)) 3)
: ((or
: (and (> a b) (> x y))
: (and (< a b) (< x y))
: (and (= a b) (= x y))) 2)
: (t 0)))))

Step 4: The Table Function

The table function is placed below the table. It does two things:

– Use the lisp function wm-calc-pts to calculate the points for each bet.
– Sum the points and write them to a cell.

: #+TBLFM: $7='(wm-calc-pts $5 $6)::@11$7=vsum(@4..@-1)

The two calulcations are divided by the “::” mark.

$7='(wm-calc-pts $5 $6) uses column 7 (Pnt for player Axel) and fills it with the results from the wm-calc-pts function. The input parameters for this function is filled with the values from column $5 (the actual result, act) and column $7 (the bet from player Axel, erg).

@11$7=vsum(@4..@-1) fills the cell at row 11 and column 7 (for player Axel) with the sum from the column, beginning at line 4 and ending at line 10 (11 minus 1).

This is done for each player, so we added an additional stack of formulars for player Andr: $9='(wm-calc-pts $5 $8)::@11$9=vsum(@2..@-1)

The whole formular looks like this:

#+TBLFM: $7='(wm-calc-pts $5 $6)::@11$7=vsum(@2..@-1)::$9='(wm-calc-pts $5 $8)::@11$9=vsum(@2..@-1)

Step 5: Executing the function

Executing the function is easy. Place your cursor inside the line and hit C-c C-c. The values will be calculated and the cells are filled with the calculated values.

|  1 | DATUM              | G | PARTIE                   | ERG | AXEL | Pnt | ANDR | Pnt |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  2 | WELTMEISTERTIPP    |   |                          | ??? |  Bra |   0 |  Por |   0 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  3 | DO 12.6. 22.00 Uhr | A | Brasilien - Kroatien     | 3-1 |  2-0 |   3 |  2-1 |   2 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  4 | FR 13.6. 18.00 Uhr | A | Mexiko - Kamerun         | 1-0 |  1-0 |   4 |  2-1 |   3 |
|  5 | FR 13.6. 21.00 Uhr | B | Spanien - Niederlande    | 1-5 |  1-1 |   0 |  2-1 |   0 |
|  6 | FR 13.6. 24.00 Uhr | B | Chile - Australien       | 3-1 |  0-0 |   0 |  2-1 |   2 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  7 | SA 14.6. 18.00 Uhr | C | Kolumbien - Griechenland | 3-0 |  2-0 |   2 |  2-1 |   2 |
|  8 | SA 14.6. 21.00 Uhr | D | Uruguay - Costa Rica     | 1-3 |  1-0 |   0 |  2-1 |   0 |
|  9 | SA 14.6. 24.00 Uhr | D | England - Italien        | 1-2 |  0-1 |   3 |  1-2 |   4 |
| 10 | SA 14.6. 27!!! Uhr | C | Elfenbeinküste - Japan   | 2-1 |  0-2 |   0 |  2-1 |   4 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
| 11 | SUMME              |   |                          |     |      |  12 |      |  17 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
|  1 | 2                  | 3 | 4                        |   5 |    6 |   0 |    8 |   0 |
|----+--------------------+---+--------------------------+-----+------+-----+------+-----|
#+TBLFM: $7='(wm-calc-pts $5 $6)::@11$7=vsum(@2..@-1)::$9='(wm-calc-pts $5 $8)::@11$9=vsum(@2..@-1)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s