Computer Science Canada Nim game??? |
Author: | Thuged_Out_G [ Sat Nov 15, 2003 12:54 am ] | ||
Post subject: | Nim game??? | ||
can anyone help me with this program, it wouldnt be hard....if i didnt have to use procedures and functions for everything |
Author: | AsianSensation [ Sat Nov 15, 2003 1:43 pm ] |
Post subject: | |
cool, this is like an old Euclid question or something like that. anyways, suppose there are 5 stones, and computer goes first, then obviously the human player wins. now if we have 4k + 1 stones, where k is an integer, such as 17 stones, and computer go first, then the human player will always win also. Because whatever the computer takes, the player may take 4 minus that number, to bring the total down to another multiple of 4 plus 1. So therefore, it's possible to see that whenever it's a person's move, and they have a multiple of 4 plus 1 stones left to take from, then they will lose. So all you have to do for your AI is to make sure he takes enough stones such that 4k + 1 stones will be left after the AI finishes it's move. Needless to say, that if you generate a multiple of 4 plus 1 to begin with, then your AI will always lose, assuming perfect game played by the player |
Author: | RETARD32 [ Sat Nov 15, 2003 1:51 pm ] |
Post subject: | |
Here's my stab at it, the a.i. , I think could be improved a bit var stones, p1, p2, lose : int randint (stones, 15, 30) fcn checkit (p1, stones : int) : int if p1 > 3 or p1 > stones then put "Invalid Input" result stones - 0 else result stones - p1 end if end checkit proc human put "How many stones? (max 3)" loop get p1 stones := checkit (p1, stones) exit when p1=1 exit when p1=2 exit when p1=3 end loop end human proc cpu if stones = 2 then p1 := 1 elsif stones = 1 then p1 := 1 elsif stones / 2 = stones div 2 then p1 := 1 elsif stones = 4 then p1 := 3 elsif stones = 3 then p1 := 2 else randint (p1, 1, 3) end if stones := checkit (p1, stones) put "CPU takes:", p1 put " " delay (1000) end cpu proc showstone locate (1, 35) put "Total stones : ", stones end showstone loop showstone human if stones = 0 then lose := 1 end if exit when stones = 0 showstone cpu if stones = 0 then lose := 2 end if exit when stones = 0 cls end loop if lose = 1 then cls put "YOU LOSE!" elsif lose = 2 then cls put "YOU WIN!" end if |
Author: | AsianSensation [ Sat Nov 15, 2003 4:30 pm ] | ||
Post subject: | |||
here, complete program with AI, the AI will never lose, unless the generated total at the beginning is in the form 4k + 1 and the player plays a perfect game
|