Advertisement
yukcheong

Week 10 - Stones game

Dec 4th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. oristone = 100
  2.  
  3. def repeat():
  4.     game()
  5.  
  6.  
  7. def checkint(varname):
  8.     try:
  9.         int(varname)
  10.         return True
  11.     except ValueError:
  12.         return False
  13.  
  14. def playerinput(player):
  15.     print '--------------------\nPlayer'+ str(player) + 'turn\n--------------------'
  16.     stonetotake=raw_input('How many stone would you like to take ? : ')
  17.     while checkint(stonetotake) == False :
  18.         stonetotake = raw_input('Pls input a valid number : ')
  19.     else:
  20.         while int(stonetotake) <1 or int(stonetotake)  >5 or int(stonetotake) > stone:
  21.             stonetotake = raw_input('Pls input a valid number : ')
  22.             while checkint(stonetotake) == False :
  23.                 stonetotake = raw_input('Pls input a valid number : ')
  24.         else:
  25.             pass
  26.         global tempstone
  27.         tempstone=int(stonetotake)
  28.         return int(stonetotake)
  29.  
  30. def game():
  31.     global stone
  32.     stone=oristone
  33.     raw_input('Hit enter to start the game  ')
  34.     print '\nStone pile : '+str(stone)
  35.     while stone >0 :
  36.         if stone - playerinput(1) >0:
  37.             stone = stone - tempstone
  38.             print 'Stone pile : '+str(stone)
  39.             if stone - playerinput(2) >0:
  40.                 stone = stone - tempstone
  41.                 print 'Stone pile : '+str(stone)
  42.             elif stone - tempstone == 0:
  43.                 print 'Player 2 wins!'
  44.                 stone = stone - tempstone
  45.         elif stone - tempstone == 0:
  46.             print 'Player 1 wins!'
  47.             stone = stone - tempstone
  48.     else:
  49.         ans=raw_input("Do you want to play again? insert 'Y' if YES or press ENTER if NO : ")
  50.         if ans == 'Y' :
  51.             repeat()
  52.    
  53. repeat()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement