coding-horror / basic-computer-games

An updated version of the classic "Basic Computer Games" book, with well-written examples in a variety of common MEMORY SAFE, SCRIPTING programming languages. See https://coding-horror.github.io/basic-computer-games/
The Unlicense
10.84k stars 1.33k forks source link

Another possible tourist trade bug in King #891

Closed GKnirps closed 1 year ago

GKnirps commented 1 year ago

I'm currently working on a rust implementation of King, and I found a possible bug in the King game (game 53).

In Addition to the bug #346 (which is already documented in the README.md, there are these lines in the code that handle income from tourists:

1400 V1=INT(((B-P1)*22)+(RND(1)*500))
1405 V2=INT((2000-D)*15)
1410 PRINT " YOU MADE";ABS(INT(V1-V2));"RALLODS FROM TOURIST TRADE."

Now B (population) is around 500 at the start of the game (P1 handles migration and is not of much relevance for this issue). D (land owned) is between 2000 and 1000, so if all land is sold (D = 1000) and the population is around the original level (which is unlikely if all land has been sold), V1 is roughly around 12000 while V2 is 15000.

From what I can tell by the following code, V1 is the amount made from tourist, while V2 is a malus you get for environmental damage. but ABS(INT(V1-V2)) means that if you have enough environmental damage, you get money from tourists again (where realistically, it should cap out at 0).

In the original game, this did not really matter because of bug #346. However, the fix for bug #346 recommends to use ABS(INT(V1-V2)) to get the amount earned by tourism. The python implementation for example uses this fix.

From my understanding, it would be better to use V1 - V2 only if V1 is greater than V2 and 0 otherwise. However, I am not completely sure about this and I would like to have some feedback before I make a PR that documents this behaviour as a bug.

coding-horror commented 1 year ago

Thank you! If it's a bug in the original listing make sure it goes in the porting notes at the root of that program!

GKnirps commented 1 year ago

Documentation added in PR #892, fix for python implementation, other implementations may still have this bug.