Results 1 to 6 of 6
-
10-22-2009, 11:08 AM #1WHT Addict
- Join Date
- Jun 2008
- Posts
- 128
Programming a Nim game in Python (MATH HELP!!)
Hi,
I'm trying to program a Nim game for Python but I'm stuck with the mathematical logic. I have no problem in the language itself but I have trouble with the mathematical aspect.
The game starts from n = 10-100 marbles (random). Player or the computer has to pick from 1 - n/2 marbles. Whoever has the last marble left loses the game.
The problem I'm having now is to find a way to get to 1 less the power 2.
For example, if the pile size is 100, the computer will need to take away 37 marbles so that there are 63 left. If the pile size is 52, then the computer needs to take away 37 marbles to get to 15.
So how do i manipulate the math so I can get the following number from n:
3, 7, 15, 31 or 63
-
10-22-2009, 01:13 PM #2Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
Smells like homework...
-
10-22-2009, 03:01 PM #3WHT Addict
- Join Date
- Jun 2008
- Posts
- 128
I think I kind of figured it out:
if n > 63:
ai_choice = n-63
if 31 > n > 63:
ai_choice = n-31
if 15 > n > 31:
ai_choice = n-15
if 7 > n > 15:
ai_choice = n-7
if 3 > n > 7:
ai_choice = n-3
if n == 63 or n == 31 or n == 15 or n == 7 or n == 3:
ai_choice = random.randint(1,n/2)
-
10-22-2009, 08:50 PM #4WHT Addict
- Join Date
- Jun 2008
- Posts
- 128
oops this is more like it:
if n > 63:
ai_choice = n-63
n = n - ai_choice
print_choice = 1
elif 31 < n < 63:
ai_choice = n-31
n = n - ai_choice
print_choice = 1
elif 15 < n < 31:
ai_choice = n-15
n = n - ai_choice
print_choice = 1
elif 7 < n < 15:
ai_choice = n-7
n = n - ai_choice
print_choice = 1
elif 3 < n < 7:
ai_choice = n-3
n = n - ai_choice
print_choice = 1
elif 1 < n < 3:
ai_choice = 1
n = n - ai_choice
print_choice = 1
-
10-22-2009, 08:52 PM #5WHT Addict
- Join Date
- Jun 2008
- Posts
- 128
-
10-23-2009, 01:39 PM #6Web Hosting Master
- Join Date
- May 2009
- Posts
- 766
Seems to me that's the whole point of a programming course anyway...learning the fundamental concepts and logical flows. Learning the syntax is not nearly as important...
Similar Threads
-
PHP and Python for Web Programming.
By AndrewBC in forum Programming DiscussionReplies: 17Last Post: 08-05-2008, 09:24 AM -
Petals Around The Rose (Math/Mind Game)! The smarter you are, the harder the game is!
By FredTT in forum Web Hosting LoungeReplies: 44Last Post: 06-29-2004, 01:36 PM -
Multiplication table math game anyone?
By Static in forum Web Hosting LoungeReplies: 6Last Post: 06-16-2004, 01:51 PM -
Game programming.
By Mark226 in forum Web Hosting LoungeReplies: 4Last Post: 10-08-2003, 11:59 PM -
Looking for a PROGRAMMER to make Video Game. I have a GAME script (not programming sc
By awd in forum Programming DiscussionReplies: 3Last Post: 03-17-2003, 03:17 PM