title: The bean game,using a loop
body: <p>The bean game is a game where two players play against each and have to pick up beans out of a pile that contains 16 beans.You are allowed to take 1,2,or 3 beans per turn. The player to pick the last bean loses.</p>
  
  <pre><code> if you start 
   total = 16 you chose 3 making the total 13
   the opponent chooses 1,2,3 making the total 12,11,10
   to get your next answer you force the opponent to land on 9
   so in turn you take the total 12,11,10 -9 to get your output of 1,2 or 3
  
   Now you want them to land on 5 to ensure you victory.
   the iteration begins here, where the opponent chooses 1,2,3 making the total 8,7,6
   so in turn you take the total 8,7,6 -5 to get your output of 1,2,or 3.
  
   By doing this you now have control of the numbers 4,3,and 2.
   If they chose 1 total=4 you chose 3 you win.
   If they chose 2 total=3 you chose 2 you win.
   If they chose 3 total=2 you chose 1 you win.
  </code></pre>
  
  <p>Here is the code it is tested in.</p>
  
  <hr>
  
  <h1>Beans test driver</h1>
  
  <pre><code>from myPlayer_28 import player as otherPlayer  # load opponent's player code
  from simple2  import player as me           # load your player code
  
  def play(player0, player1):
      """Plays a single game of 'Heap of Beans'.  Winning player's # (0 or 1) is  returned"""
      beans = 16  # Starting bean heap
      while True:
          take = player0(beans) # find out how many beans player0 takes
          if 1 &lt;= take &lt;= 3 and take &lt;= beans:  # legal # of beans taken?
              beans -= take                     # adjust count of remaining beans
          else:  # user made bad move
              print("Bad move:", beans, "bean(s) and", take, "taken.")
              beans = 0  # force lose for bad move
          if beans == 0: # does player0 lose?
              return 1   # player 1 wins
          take = player1(beans) # find out how many beans player1 takes
          if 1 &lt;= take &lt;= 3 and take &lt;= beans:  # legal # of beans taken?
              beans -= take                     # adjust count of remaining beans
          else:  # user made bad move
              print("Bad move:", beans, "bean(s) and", take, "taken.")
              beans = 0  # force lose for bad move
          if beans == 0: # does player1 lose?
              return 0   # player 0 wins    
  
  def main():
      """Play 100 games of 'Heap of Beans' with each player starting 50 times."""
      wins = [0,0] # win accumulators for player0 and player1
      for i in range(100):
          if i%2:
              winner = play(me,otherPlayer)
          else:  
              winner = play(otherPlayer,me)
              winner = (winner+1)%2
          wins[winner] += 1
      print("My wins:", wins[0], "  Other wins:", wins[1])
  
  main()  
  </code></pre>
  
  <hr>
  
  <p>This is simple 2 the program I am running against.</p>
  
  <pre><code>import random
  
  def player(beans):
      if beans &gt; 4:
          return random.randrange(1,4)
      if beans == 1:
          return 1
  return beans - 1
  </code></pre>
  
  <hr>
  
  <p>NEW CODE I have significantly changed my code.</p>
  
  <pre><code>def player(beans):
      if beans==16:
          return 3
      if beans==15:
          return 2
      if beans==14:
          return 1
      if beans==13:
          return 1
  
      if beans &lt;=12 and beans &gt;=10:
          return beans-9
      if beans==9:
          return 1
  
      if beans &gt;=6 and beans&lt;9:
          return beans -5
      if beans ==5:
          return 1
  
      else:
          if beans ==4:
              return 3
          elif beans ==3:
              return 2
          elif beans==2:
              return 1
          elif beans ==1:
              return 1
  </code></pre>
  
  <p>I apologize for having such a LARGE amount of code for this question. However I wanted to show those who look at it what it is being tested in and so if they would like they could copy all the code to run it themselves.</p>
  
  <hr>
  
  <p>"NEW" My code wins 96% of the time, but id like it to win 100% of the time is that impossible? </p>