Re: [Tutor] The game of nim in python

2016-04-22 Thread Danny Yoo
On Fri, Apr 22, 2016 at 5:45 AM, Henderson, Kevin (GE Aviation, US)
 wrote:
> Python to Jython.
>
> Can you help me with a Jython code for the Nim game?

If you can say more about what you're like help with, we can tailor
advice toward what you want.  I'll treat this as a quick-and-dirty
design review.


... ok, I see a lot of globals.  It might make sense to consider
modifying the functions that take in and produce values.


For example, your computer player:

> def removingStrawsComputer():
> removedNumber=random.randint(1,3)
> global strawsNumber
> while removedNumber>strawsNumber:
> removedNumber=random.randint(1,3)
> strawsNumber-=removedNumber
> return strawsNumber

essentially depends on the initial strawsNumber, and returns an
updated strawsNumber to represent how the computer is playing.



Rather than have it work on the 'strawsNumber' global directly, you
can pass in the value as an argument.

###
 def removingStrawsComputer(strawsNumber):
 removedNumber=random.randint(1,3)
 while removedNumber>strawsNumber:
 removedNumber=random.randint(1,3)
 strawsNumber-=removedNumber
 return strawsNumber
###


How would this change affect the rest of the program?  It would touch
callers of removingStrawsComputer, and in that case, that's the main
game() function.

It would change:


##
def game():
while gameover==False:
print("It's ",player2,"turn. The number of straws left: ",
removingStrawsComputer())
...
##

to:

##
def game():
while gameover==False:
currentStraws = removingStrawsComputer(currentStraws)
print("It's ",player2,"turn. The number of straws left: ",
currentStraws)
...
##

I would argue that the revised version is better for readability
because it's more clear that we're calling removingStrawsComputer so
that we can change currentStraws.




The other thing that caught my eye is related to game() too. This line:

print("It's ",player2,"turn. The number of straws left: ",
removingStrawsComputer())

Yes, we looked at this line earlier.  :P

I think this is too clever, believe it or not.  I'd recommend breaking
this out into two separate statements.

   currentStraws = removingStrawsComputer()
   print("It's ",player2,"turn. The number of straws left: ", currentStraws)

The reason is because the original is calling removingStrawsComputer()
for an essential side-effect, which is ok normally, except this is
being done in the context of a print statement.  That's the part that
can be error-prone.

Print statements are often added and removed to make things look
nicer.  I would think that commenting out a print statement would be
mostly harmless, for example, but in this case, it's *crucial* that we
don't in this situation, because it affects the state of the game.

In general, when using print(), avoid putting in side-effecting things
as arguments.  It's one of those code smells that a programmer learns
to avoid.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The game of nim in python

2016-04-22 Thread Michael Selik
What's the problem you're trying to solve? Did you get an error?

Here's a quick revision. There's probably a more elegant way, but this
seems to work.

#/usr/bin/env python

from __future__ import print_function
import random

try:
input = raw_input
except NameError:
pass # already using Python 3


player = input('Enter your name: ')

straws = random.randint(10, 20)
if straws % 4 == 1:
straws += 1

while straws:
print("It's the Computer's turn. {} straws left.".format(straws))
n = random.randint(1, min(3, straws))
straws -= n
print("The Computer removes {} straws.".format(n))

if not straws:
print("The Computer won!")
break

print("It's your turn, {}. There are {} straws
left.".format(player, straws))
n = None
while not n:
print("You can remove between 1 and {} straws.".format(min(3,
straws)))
try:
n = int(input('How many straws do you want to remove? '))
if n < 1 or n > min(3, straws):
n = None
except ValueError:
pass
straws -= n
print("You removed {} straws.".format(n))

if not straws:
print("You won!")
break


On Fri, Apr 22, 2016 at 1:58 PM Henderson, Kevin (GE Aviation, US) <
kevinm.hender...@ge.com> wrote:

> Python to Jython.
>
> Can you help me with a Jython code for the Nim game?
>
> Removing 1-4 sticks out of 13, last player who picks up the last stick wins
>
> Player 1 vs player2(Computer)
>
> player1=str(input("Enter your name. "))
> player2="Computer"
> howMany=0
> gameover=False
> strawsNumber=random.randint(10,20)
>
> if (strawsNumber%4)==1:
> strawsNumber+=1
>
> def removingStrawsComputer():
> removedNumber=random.randint(1,3)
> global strawsNumber
> while removedNumber>strawsNumber:
> removedNumber=random.randint(1,3)
> strawsNumber-=removedNumber
> return strawsNumber
>
> def removingStrawsHuman():
> global strawsNumber
> strawsNumber-=howMany
> return strawsNumber
>
> def humanLegalMove():
> global howMany
> legalMove=False
> while not legalMove:
> print("It's your turn, ",player1)
> howMany=int(input("How many straws do you want to remove?(from 1
> to 3) "))
> if  howMany>3 or howMany<1:
> print("Enter a number between 1 and 3.")
> else:
> legalMove=True
> while howMany>strawsNumber:
> print("The entered number is greater than a number of straws
> remained.")
> howMany=int(input("How many straws do you want to remove?"))
> return howMany
>
> def checkWinner(player):
> if strawsNumber==0:
> print(player," wins.")
> global gameover
>gameover=True
> return gameover
>
> def resetGameover():
> global gameover
> gameover=False
> return gameover
>
> def game():
> while gameover==False:
> print("It's ",player2,"turn. The number of straws left:
> ",removingStrawsComputer())
> checkWinner(player1)
> if gameover==True:
> break
> humanLegalMove()
> print("The number of straws left: ",removingStrawsHuman())
> checkWinner(player2)
> game()
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The game of nim in python

2016-04-22 Thread Alan Gauld via Tutor
On 22/04/16 13:45, Henderson, Kevin (GE Aviation, US) wrote:
> Python to Jython.
> 
> Can you help me with a Jython code for the Nim game?
> 

There shouldn't be much difference between a Jython or Python
implementation - unless you are building a GUI of course!

> Removing 1-4 sticks out of 13, last player who picks up the last stick wins

Ive no idea how nim is played although I re,em,ber my math
teacher talking about it as being the only practical use
of binary outside computing...

But what kind of help do you need? Does your code work?
Is there an error (if so send us the full text)?
Do you want a general code review?
What exactly do you want from us?

> player1=str(input("Enter your name. "))
> player2="Computer"
> howMany=0
> gameover=False
> strawsNumber=random.randint(10,20)
> 
> if (strawsNumber%4)==1:
> strawsNumber+=1
> 
> def removingStrawsComputer():
> removedNumber=random.randint(1,3)
> global strawsNumber
> while removedNumber>strawsNumber:
> removedNumber=random.randint(1,3)
> strawsNumber-=removedNumber
> return strawsNumber
> 
> def removingStrawsHuman():
> global strawsNumber
> strawsNumber-=howMany
> return strawsNumber
> 
> def humanLegalMove():
> global howMany
> legalMove=False
> while not legalMove:
> print("It's your turn, ",player1)
> howMany=int(input("How many straws do you want to remove?(from 1 to 
> 3) "))
> if  howMany>3 or howMany<1:
> print("Enter a number between 1 and 3.")
> else:
> legalMove=True
> while howMany>strawsNumber:
> print("The entered number is greater than a number of straws 
> remained.")
> howMany=int(input("How many straws do you want to remove?"))
> return howMany
> 
> def checkWinner(player):
> if strawsNumber==0:
> print(player," wins.")
> global gameover
>gameover=True
> return gameover
> 
> def resetGameover():
> global gameover
> gameover=False
> return gameover
> 
> def game():
> while gameover==False:
> print("It's ",player2,"turn. The number of straws left: 
> ",removingStrawsComputer())
> checkWinner(player1)
> if gameover==True:
> break
> humanLegalMove()
> print("The number of straws left: ",removingStrawsHuman())
> checkWinner(player2)
> game()

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The game of nim in python

2016-04-22 Thread Henderson, Kevin (GE Aviation, US)
Python to Jython.

Can you help me with a Jython code for the Nim game?

Removing 1-4 sticks out of 13, last player who picks up the last stick wins

Player 1 vs player2(Computer)

player1=str(input("Enter your name. "))
player2="Computer"
howMany=0
gameover=False
strawsNumber=random.randint(10,20)

if (strawsNumber%4)==1:
strawsNumber+=1

def removingStrawsComputer():
removedNumber=random.randint(1,3)
global strawsNumber
while removedNumber>strawsNumber:
removedNumber=random.randint(1,3)
strawsNumber-=removedNumber
return strawsNumber

def removingStrawsHuman():
global strawsNumber
strawsNumber-=howMany
return strawsNumber

def humanLegalMove():
global howMany
legalMove=False
while not legalMove:
print("It's your turn, ",player1)
howMany=int(input("How many straws do you want to remove?(from 1 to 3) 
"))
if  howMany>3 or howMany<1:
print("Enter a number between 1 and 3.")
else:
legalMove=True
while howMany>strawsNumber:
print("The entered number is greater than a number of straws remained.")
howMany=int(input("How many straws do you want to remove?"))
return howMany

def checkWinner(player):
if strawsNumber==0:
print(player," wins.")
global gameover
   gameover=True
return gameover

def resetGameover():
global gameover
gameover=False
return gameover

def game():
while gameover==False:
print("It's ",player2,"turn. The number of straws left: 
",removingStrawsComputer())
checkWinner(player1)
if gameover==True:
break
humanLegalMove()
print("The number of straws left: ",removingStrawsHuman())
checkWinner(player2)
game()




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] The game of nim in python

2013-04-04 Thread Dave Angel

On 03/27/2013 08:30 AM, Yasin El Guennouni wrote:


Hello,

I am trying to modify a game of nim code. The game in the code works as:
"You need to remove from 1 to 3 straws from the pile.
The player that removes the final straw is the loser."

But I would like it to be like the classic game, where you have 4 piles 
containing 1,3,5 and 7 sticks
where the drawer of the last stick is the winner. It would be awsome if I could 
print how
many sticks there are left in each pile, e.g. : I III I III.



In my opinion, you'd have to modify so much that you'd probably be 
better off starting from scratch.




--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] The game of nim in python

2013-04-04 Thread Yasin El Guennouni

Hello,

I am trying to modify a game of nim code. The game in the code works as: 
"You need to remove from 1 to 3 straws from the pile.
The player that removes the final straw is the loser."

But I would like it to be like the classic game, where you have 4 piles 
containing 1,3,5 and 7 sticks
where the drawer of the last stick is the winner. It would be awsome if I could 
print how
many sticks there are left in each pile, e.g. : I III I III.

Thanks in advance!

The code I have:

player1=str(input("Enter your name. "))
player2="Computer"
howMany=0
gameover=False
strawsNumber=random.randint(10,20)

if (strawsNumber%4)==1:
strawsNumber+=1

def removingStrawsComputer():
removedNumber=random.randint(1,3)
global strawsNumber
while removedNumber>strawsNumber:
removedNumber=random.randint(1,3)
strawsNumber-=removedNumber
return strawsNumber

def removingStrawsHuman():
global strawsNumber
strawsNumber-=howMany
return strawsNumber

def humanLegalMove():
global howMany
legalMove=False
while not legalMove:
print("It's your turn, ",player1)
howMany=int(input("How many straws do you want to remove?(from 1 to 3) 
"))
if  howMany>3 or howMany<1:
print("Enter a number between 1 and 3.")
else:
legalMove=True
while howMany>strawsNumber:
print("The entered number is greater than a number of straws remained.")
howMany=int(input("How many straws do you want to remove?"))
return howMany

def checkWinner(player):
if strawsNumber==0:
print(player," wins.")
global gameover
gameover=True
return gameover

def resetGameover():
global gameover
gameover=False
return gameover

def game():
while gameover==False:
print("It's ",player2,"turn. The number of straws left: 
",removingStrawsComputer())
checkWinner(player1)
if gameover==True:
break
humanLegalMove()
print("The number of straws left: ",removingStrawsHuman())
checkWinner(player2)
game()
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor