Carrot in a Box

Tags: large, beginner, game, twoplayer

This is a simple and silly bluffing game for two human players. Each player has a box. One box has a carrot in it, and each player wants to have the carrot. The first player looks in their box and then tells the second player they either do or don’t have the carrot. The second player gets to decide whether to swap boxes or not.

carrot_in_a_box.py
  1"""Carrot in a Box, by Al Sweigart al@inventwithpython.com
  2A silly bluffing game between two human players. Based on the game
  3from the show, 8 Out of 10 Cats.
  4This code is available at https://nostarch.com/big-book-small-python-programming
  5Tags: large, beginner, game, two-player"""
  6
  7import random
  8
  9print('''Carrot in a Box, by Al Sweigart al@inventwithpython.com
 10
 11This is a bluffing game for two human players. Each player has a box.
 12One box has a carrot in it. To win, you must have the box with the
 13carrot in it.
 14
 15This is a very simple and silly game.
 16
 17The first player looks into their box (the second player must close
 18their eyes during this.) The first player then says "There is a carrot
 19in my box" or "There is not a carrot in my box". The second player then
 20gets to decide if they want to swap boxes or not.
 21''')
 22input('Press Enter to begin...')
 23
 24p1Name = input('Human player 1, enter your name: ')
 25p2Name = input('Human player 2, enter your name: ')
 26playerNames = p1Name[:11].center(11) + '    ' + p2Name[:11].center(11)
 27
 28print('''HERE ARE TWO BOXES:
 29  __________     __________
 30 /         /|   /         /|
 31+---------+ |  +---------+ |
 32|   RED   | |  |   GOLD  | |
 33|   BOX   | /  |   BOX   | /
 34+---------+/   +---------+/''')
 35
 36print()
 37print(playerNames)
 38print()
 39print(p1Name + ', you have a RED box in front of you.')
 40print(p2Name + ', you have a GOLD box in front of you.')
 41print()
 42print(p1Name + ', you will get to look into your box.')
 43print(p2Name.upper() + ', close your eyes and don\'t look!!!')
 44input('When ' + p2Name + ' has closed their eyes, press Enter...')
 45print()
 46
 47print(p1Name + ' here is the inside of your box:')
 48
 49if random.randint(1, 2) == 1:
 50    carrotInFirstBox = True
 51else:
 52    carrotInFirstBox = False
 53
 54if carrotInFirstBox:
 55    print('''
 56   ___VV____
 57  |   VV    |
 58  |   VV    |
 59  |___||____|    __________
 60 /    ||   /|   /         /|
 61+---------+ |  +---------+ |
 62|   RED   | |  |   GOLD  | |
 63|   BOX   | /  |   BOX   | /
 64+---------+/   +---------+/
 65 (carrot!)''')
 66    print(playerNames)
 67else:
 68    print('''
 69   _________
 70  |         |
 71  |         |
 72  |_________|    __________
 73 /         /|   /         /|
 74+---------+ |  +---------+ |
 75|   RED   | |  |   GOLD  | |
 76|   BOX   | /  |   BOX   | /
 77+---------+/   +---------+/
 78(no carrot!)''')
 79    print(playerNames)
 80
 81input('Press Enter to continue...')
 82
 83print('\n' * 100)  # Clear the screen by printing several newlines.
 84print(p1Name + ', tell ' + p2Name + ' to open their eyes.')
 85input('Press Enter to continue...')
 86
 87print()
 88print(p1Name + ', say one of the following sentences to ' + p2Name + '.')
 89print('  1) There is a carrot in my box.')
 90print('  2) There is not a carrot in my box.')
 91print()
 92input('Then press Enter to continue...')
 93
 94print()
 95print(p2Name + ', do you want to swap boxes with ' + p1Name + '? YES/NO')
 96while True:
 97    response = input('> ').upper()
 98    if not (response.startswith('Y') or response.startswith('N')):
 99        print(p2Name + ', please enter "YES" or "NO".')
100    else:
101        break
102
103firstBox = 'RED '  # Note the space after the "D".
104secondBox = 'GOLD'
105
106if response.startswith('Y'):
107    carrotInFirstBox = not carrotInFirstBox
108    firstBox, secondBox = secondBox, firstBox
109
110print('''HERE ARE THE TWO BOXES:
111  __________     __________
112 /         /|   /         /|
113+---------+ |  +---------+ |
114|   {}  | |  |   {}  | |
115|   BOX   | /  |   BOX   | /
116+---------+/   +---------+/'''.format(firstBox, secondBox))
117print(playerNames)
118
119input('Press Enter to reveal the winner...')
120print()
121
122if carrotInFirstBox:
123    print('''
124   ___VV____      _________
125  |   VV    |    |         |
126  |   VV    |    |         |
127  |___||____|    |_________|
128 /    ||   /|   /         /|
129+---------+ |  +---------+ |
130|   {}  | |  |   {}  | |
131|   BOX   | /  |   BOX   | /
132+---------+/   +---------+/'''.format(firstBox, secondBox))
133
134else:
135    print('''
136   _________      ___VV____
137  |         |    |   VV    |
138  |         |    |   VV    |
139  |_________|    |___||____|
140 /         /|   /    ||   /|
141+---------+ |  +---------+ |
142|   {}  | |  |   {}  | |
143|   BOX   | /  |   BOX   | /
144+---------+/   +---------+/'''.format(firstBox, secondBox))
145
146print(playerNames)
147
148# This modification made possible through the 'carrotInFirstBox variable
149if carrotInFirstBox:
150    print(p1Name + ' is the winner!')
151else:
152    print(p2Name + ' is the winner!')
153
154print('Thanks for playing!')

https://inventwithpython.com/bigbookpython/project9.html