Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Programming "NIM," having massive difficulty

Author
Message
Thistle Studios
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Kirkcaldy
Posted: 5th Mar 2011 21:52
I thought I would fire up DBPro again to try and do some TrueBASIC style programming, but sadly I realised that many of the commands were difficult, so I battled through and realised my program is not doing what I want it to do, despite the fact my code seems to look fine.
I know the strategy to NIM, having to get the number down to a multiple of n+1, but I cant get the AI to do that. In fact, I cant get the program to behave AT ALL.
First problem: When prompted for a number of coins to remove, the program wont accept any coins other than the maximum number, even though my code specifically states:


Secondly, my AI chooses a random number of coins to remove, but the number of coins doesnt actually decrease...

Here is my current code:



I dont understand why my program misbehaves so much... Maybe someone can shed some light on this situation and tell me what I am doing wrong.


Pentium 4 2.61ghz 1.5gb DDR 333Mhz RAM NVIDIA GeForce 7600GS AGP 8X
Endangered City. An Endangered Species
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 6th Mar 2011 18:49
Quote: "First problem: When prompted for a number of coins to remove, the program wont accept any coins other than the maximum number, even though my code specifically states:"


The problem is the last part of the UNTIL loop... you tell it that remove has to be between 0 and coinmax and equal to coinmax. Get rid of the "remove = INT(coinmax)" and it'll be fine.

The INT() command makes sure numbers are whole but a normal variable is already an integer so in this case there's no need for the INT() command at all (anywhere in your code).

Quote: "Secondly, my AI chooses a random number of coins to remove, but the number of coins doesnt actually decrease..."


You've got one too many n's in coinnum. "coinnnum = coinnum-remove"

The other things I noticed is that you need a RANDOMIZE TIMER() at the top of your code. Any program that picks random numbers needs to have that at least once at the top of the code to make sure that the program doesn't pick the same random numbers every time.

Also when getting text input like the Heads/Tails it's best to change that input to higher case or lower case letters because if the user types "heads" and you check for "Heads" it's not the same thing... but if it converts it to higher case with the UPPER$() command any way the user types in "heaDS" will convert to "HEADS" to detect the right word.

Oh and it's really pointless to have "GOTO coinflip" when the label is right under the GOTO.

Here's your code modified (I had to change "random" to "Flip" because it conflicted with IanMs Matrix 1 Utilities Plugin):


Thistle Studios
17
Years of Service
User Offline
Joined: 1st Mar 2007
Location: Kirkcaldy
Posted: 6th Mar 2011 19:33
Something I have noticed in the code change is that the "strategy" has been removed. I know now of what my errors were, but my question was actually "How would I implement the nim strategy?"
According to what I was taught, the strategy is the remove a number of coins which leaves the pile as a multiple of the maximum number you can take off plus one. For example:
30 Coins
6 Max
First turn I would take off 2 to make 28, which is divisible by 7 (6+1)

Intel Core i5 3.2 GHz, 4 GB RAM, NVIDIA GeForce 9800 GT
Endangered City. An Endangered Species
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Mar 2011 00:43
Ah... I know not what this "nim" is or where it's "strategy" went but if you want you can make the computer use any formula you want rather than a random number.

Login to post a reply

Server time is: 2024-11-23 02:28:57
Your offset time is: 2024-11-23 02:28:57