Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Nim game???
Author Message
Thuged_Out_G

Posted: Sat Nov 15, 2003 12:54 am   Post subject: Nim game???

 code: The game of Nim starts with a random number of stones between 15 and 30. Two players alternates turns and on each turn may take either 1, 2 or 3 stones from the pile. The player forced to take the last stone loses. Create a Nim application that allows the user to play against the computer. In this version of the game, the application generates the number of stones to begin with, the number of stones the computer takes and the user goes first. Include code that prevents the user and computer from taking an illegal num ber of stones. For example, neither should be allowed to take three stones when there are only 1 or 2 left. Your program should use: Â· A valid entry function to check the number entered by the user. Â· The randint function to generate a random number between 1 and 3 for the computer's turn Â· Separate procedures for the user's turn and the computer's turn.

can anyone help me with this program, it wouldnt be hard....if i didnt have to use procedures and functions for everything

AsianSensation

Posted: Sat Nov 15, 2003 1:43 pm   Post subject: (No 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
RETARD32

Posted: Sat Nov 15, 2003 1:51 pm   Post subject: (No 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
AsianSensation

Posted: Sat Nov 15, 2003 4:30 pm   Post subject: (No 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

 code: var TotGen, Turn, PInput, CompTake : int := 0 var Flag := false TotGen := Rand.Int (15, 30) proc CheckWin     if TotGen = 1 then         put "You Lose! " ..         if Turn mod 2 = 0 then             put "Computer"         else             put "Player"         end if         Flag := true     end if end CheckWin fcn AIGetTempTotal : int     for rep : 0 .. 7         if TotGen - ((4 * rep) + 1) < 4 then             result (4 * rep) + 1         end if     end for end AIGetTempTotal loop     CheckWin     exit when Flag = true     put "We now have ", TotGen, " stones left"     put "It's now the computer's turn"     if TotGen - AIGetTempTotal not= 0 then         CompTake := TotGen - AIGetTempTotal         TotGen -= CompTake     else         CompTake := Rand.Int (1, 3)         TotGen -= CompTake     end if     put "Computer takes ", CompTake, " stones.\n"     Turn += 1     CheckWin     exit when Flag = true     put "it's now the player's turn"     put "We now have ", TotGen, " stones left"     loop         put "Player, please take 1, 2, or 3 stones from the pile: " ..         get PInput         if PInput > 3 or PInput < 1 then             put "Invalid move, try again!"         else             exit         end if     end loop     put skip     TotGen -= PInput     Turn += 1 end loop
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 4 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: