· 8 years ago · Apr 16, 2018, 08:22 PM
1# Import statements
2import turtle
3import os
4import random
5
6"""
7_____ _____ _____ _____ ___ ___ ___ _____
8| _ \ | _ \ | ____| / ___| / | / |/ | | ____|
9| |_| | | |_| | | |__ | | / /| | / /| /| | | |__
10| ___/ | _ / | __| | | _ / / | | / / |__/ | | | __|
11| | | | \ \ | |___ | |_| | / / | | / / | | | |___
12|_| |_| \_\ |_____| \_____/ /_/ |_| /_/ |_| |_____|"""
13
14
15def rules():
16 print("Goal: Make as much money as you can and make your opponents go broke\n"
17 "If you are a new player, register an account\n"
18 "Have other registered people open accounts and play against you\n"
19 "You can also play against our AIs\n"
20 "The game will consist 20 rounds, unless you choose to extend or resign.\n"
21 "You can view the game events by picking the history option\n"
22 "You can also view player's money, properties, and out of jail cards\n"
23 "Every turn starts by rolling the dice. On your turn, press enter to roll dice.\n"
24 "Your game piece will automatically move the number of spaces the dice rolls show\n"
25 "Double: If both dices show the same number, you can roll the dice again!\n"
26 "However, on the third double roll you go to jail\n"
27 "To get out of jail, you have to either roll doubles, pay $50 or use a Get out of jail card\n"
28 "If on the third roll you do not roll doubles you must pay $50 and move the amount you rolled\n"
29 "Buy Property: When you land on unowned property, the bank will offer you a chance to buy it\n"
30 "If the property is not bought, Auction will occur. \n"
31 "In Auction, all players bid on the property. The highest bid will win\n"
32 "Pay Rent: When you land on a property owned by another player, the owner collects rent from you\n"
33 "Rent is automatically taken from your bank account and given to the owner's account\n"
34 "If the property is mortgaged, no rent can be collected\n"
35 "Chance and community chest card: When you land on Chance space, you'll get the top card from the deck\n"
36 "If you pick a Get out of jail card you may hold it until you use it\n"
37 "Each you pass or land on the start, the Banker pays $200\n"
38 "You go to jail when:\n"
39 "\tYour game piece lands on the space marked 'Go to jail'\n"
40 "\tYou pick a card marked 'go to jail'\n"
41 "\tYou roll double three times on the same turn\n"
42 "While in jail:\n"
43 "\tYou may still buy, sell or trade properties\n"
44 "\tYou may still buy or sell houses and hotels\n"
45 "\tYou may still collect rent\n"
46 "Houses: When you own all the properties in a certain group you will be notified\n"
47 "You may buy houses from the bank and build them on those properties\n"
48 "You must build the houses evenly. Up to 4 houses in a property.\n"
49 "You may sell or mortgage your properties to the bank\n"
50 "You can only mortgage a property that has no houses on it\n"
51 "To unmortgage, you will have to buy the property plus 10 percent\n"
52 "If you run out of cash and cannot pay your debts, you lose!")
53
54
55def turtle_greeting():
56 t = turtle.Turtle()
57 t.shape("turtle")
58 t.hideturtle()
59 t.fillcolor("blue")
60 t.write("Welcome to the monopoly!", align="center", font=("Arial", 40, "normal"))
61 turtle.exitonclick()
62
63
64# Use this allLines any time you read information from a text file
65def remove_space(nickname):
66 file = open(nickname + '.txt', 'r')
67 allLines = file.readlines()
68 file.close()
69 for line in range(len(allLines)):
70 new = allLines[line].replace('\n', '')
71 allLines.pop(line)
72 allLines.insert(line, new)
73 return allLines
74
75
76def log_in():
77 while True:
78 nickname = input("Enter player nickname: ")
79 if os.path.exists(nickname + '.txt'):
80 break
81 continue
82 while True:
83 allLines = remove_space(nickname)
84 password = 'test' # todo: I want to use the stars for entering password, so I don't make password input yet.
85 if password in allLines:
86 break
87 return nickname
88
89
90def sign_up():
91 while True:
92 nickname = input("Pick your player name: ")
93 # To make sure they don't enter nothing and the name is not in the names
94 if len(nickname) >= 1 and nickname != ' ':
95 if nickname not in remove_space('names'):
96 namesFile = open('names.txt', 'a')
97 namesFile.write(nickname + '\n')
98 namesFile.close()
99 break
100 else:
101 print("The name already exists")
102 while True:
103 print("The password must include at least 1 upper case, 1 lower case and 1 digit.")
104 password = 'test' # todo: I want to to use the starts for entering password
105 break
106 while True:
107 email = input("Enter email(only gmail): ")
108 if '@gmail.com' in email:
109 break
110 continue
111 userallLines = open(nickname + '.txt', 'w')
112 userallLines.write(nickname + '\n' + password + '\n' + email + '\n')
113 userallLines.close()
114 return nickname
115
116
117turtle_greeting()
118allNames = open("names.txt", 'a')
119allNames.close()
120# Determine all the players in the game
121while True:
122 players = input("Enter amount of players: ")
123 try:
124 if int(players) <= 1:
125 players = int(players)
126 continue
127 break
128 except ValueError:
129 continue
130players = int(players)
131all_players = []
132for i in range(players):
133 while True:
134 print("Player %s\nWould you like to register(r) or log in(i)?" % str(i + 1))
135 direction = input()
136 if direction == 'i':
137 all_players.append(log_in())
138 break
139 elif direction == 'r':
140 all_players.append(sign_up())
141 break
142
143"""
144 _____ ___ ___ ___ _____ _____ _____ ___ _____ _____
145/ ___| / | / |/ | | ____| / ___/ |_ _| / | | _ \ |_ _|
146| | / /| | / /| /| | | |__ | |___ | | / /| | | |_| | | |
147| | _ / / | | / / |__/ | | | __| \___ \ | | / / | | | _ / | |
148| |_| | / / | | / / | | | |___ ___| | | | / / | | | | \ \ | |
149\_____/ /_/ |_| /_/ |_| |_____| /_____/ |_| /_/ |_| |_| \_\ |_| """
150# todo: Most print statements could be replaced with graphics eventually
151print("Game start:")
152print("Players: \n\t%s" % '\n\t'.join(all_players))
153# todo: I think I want to put here a class that consists of functions for each place
154class Location:
155 def jail(self):
156 print("You have been sent to jail:(")
157 return
158
159# The menu occurs after the turn is over to ask user for other things they would like to do
160def menu(player):
161 pass
162
163
164# This function can be called for every dice roll
165def dice_roll():
166 dices_to_return = []
167 dice = input("Press any key to roll two dice: ")
168 dice_one = random.randint(1, 6)
169 dice_two = random.randint(1, 6)
170 if dice_one == dice_two:
171 are_the_dices_equal = True
172 else:
173 are_the_dices_equal = False
174 dices_to_return.append(are_the_dices_equal)
175 dices_to_return.append(dice_one)
176 dices_to_return.append(dice_two)
177 return dices_to_return
178
179
180
181
182
183def turn(player, times=0):
184 roll = dice_roll()
185 are_equal = roll[0]
186 dice1 = roll[1]
187 dice2 = roll[2]
188 steps_to_move = dice1 + dice2
189 print("Dice 1: %s" % dice1)
190 print("Dice 2: %s" % dice2)
191 if are_equal:
192 if times == 2:
193 Location.jail()
194 return
195 print("Moving %s...." % steps_to_move)
196 player_position[player] += steps_to_move
197 # todo: Turtle graphics animation in here?
198 # todo: Buying/selling/rent etc
199
200
201 # dice roll returns False if the dices are not equal
202 if are_equal == True:
203 print("You rolled two dices the same! You get another turn!")
204 # Using recursion here because the player rolled double
205 times += 1
206 return turn(player, times)
207 menu(player)
208
209
210
211
212def rounds():
213 global player_position
214 positions = {1: "Go", 2: "Purple #1", 3: "Community Chest", 4: "Purple #2",
215 5: "Income Tax", 6: "Railroad #1", 7: "Light Blue #1",
216 8: "Chance", 9: "Light Blue #2", 10: "Light Blue #3",
217 11: "Visiting Jail", 12: "Purple #1", 13: "Electric Company",
218 14: "Purple #2", 15: "Purple #3", 16: "Railroad #2",
219 17: "Orange #1", 18: "Community Chest", 19: "Orange #2", 20: "Orange #3",
220 21: "Free Parking", 22: "Red #1", 23: "Chance",
221 24: "Red #2", 25: "Red #3", 26: "Railroad #3",
222 27: "Yellow #1", 28: "Yellow #2", 29: "Water works", 30: "Yellow #3",
223 31: "Go to jail", 32: "Green #1", 33: "Green #2",
224 34: "Community Chest", 35: "Green #3", 36: "Railroad #4",
225 37: "Chance", 38: "Blue #1", 39: "Luxury Tax", 40: "Blue #2"}
226 # todo: I have no idea what to call the steets names(or most of the other things fWIW)
227 # I am gonna create a dictionary with all the positions
228 player_position = {}
229 for pl in range(players):
230 player_position[all_players[pl]] = 0
231 print(player_position)
232 # because the game lasts 20 rounds
233 for round in range(20):
234 for player in range(players):
235 print("%s's turn" % all_players[player])
236 print("___________________________________________")
237 turn(all_players[i])
238
239rounds()