Skip to content

Instantly share code, notes, and snippets.

@MichaelGitHubHype
Created April 9, 2018 01:23
Show Gist options
  • Save MichaelGitHubHype/e407078a50c7cf9ef61d3d3d336510de to your computer and use it in GitHub Desktop.
Save MichaelGitHubHype/e407078a50c7cf9ef61d3d3d336510de to your computer and use it in GitHub Desktop.
# Import statements
import turtle
import os
import random
"""
_____ _____ _____ _____ ___ ___ ___ _____
| _ \ | _ \ | ____| / ___| / | / |/ | | ____|
| |_| | | |_| | | |__ | | / /| | / /| /| | | |__
| ___/ | _ / | __| | | _ / / | | / / |__/ | | | __|
| | | | \ \ | |___ | |_| | / / | | / / | | | |___
|_| |_| \_\ |_____| \_____/ /_/ |_| /_/ |_| |_____|"""
def rules():
print("Goal: Make as much money as you can and make your opponents go broke\n"
"If you are a new player, register an account\n"
"Have other registered people open accounts and play against you\n"
"You can also play against our AIs\n"
"The game will consist 20 rounds, unless you choose to extend or resign.\n"
"You can view the game events by picking the history option\n"
"You can also view player's money, properties, and out of jail cards\n"
"Every turn starts by rolling the dice. On your turn, press enter to roll dice.\n"
"Your game piece will automatically move the number of spaces the dice rolls show\n"
"Double: If both dices show the same number, you can roll the dice again!\n"
"However, on the third double roll you go to jail\n"
"To get out of jail, you have to either roll doubles, pay $50 or use a Get out of jail card\n"
"If on the third roll you do not roll doubles you must pay $50 and move the amount you rolled\n"
"Buy Property: When you land on unowned property, the bank will offer you a chance to buy it\n"
"If the property is not bought, Auction will occur. \n"
"In Auction, all players bid on the property. The highest bid will win\n"
"Pay Rent: When you land on a property owned by another player, the owner collects rent from you\n"
"Rent is automatically taken from your bank account and given to the owner's account\n"
"If the property is mortgaged, no rent can be collected\n"
"Chance and community chest card: When you land on Chance space, you'll get the top card from the deck\n"
"If you pick a Get out of jail card you may hold it until you use it\n"
"Each you pass or land on the start, the Banker pays $200\n"
"You go to jail when:\n"
"\tYour game piece lands on the space marked 'Go to jail'\n"
"\tYou pick a card marked 'go to jail'\n"
"\tYou roll double three times on the same turn\n"
"While in jail:\n"
"\tYou may still buy, sell or trade properties\n"
"\tYou may still buy or sell houses and hotels\n"
"\tYou may still collect rent\n"
"Houses: When you own all the properties in a certain group you will be notified\n"
"You may buy houses from the bank and build them on those properties\n"
"You must build the houses evenly. Up to 4 houses in a property.\n"
"You may sell or mortgage your properties to the bank\n"
"You can only mortgage a property that has no houses on it\n"
"To unmortgage, you will have to buy the property plus 10 percent\n"
"If you run out of cash and cannot pay your debts, you lose!")
def turtle_greeting():
t = turtle.Turtle()
t.shape("turtle")
t.hideturtle()
t.fillcolor("blue")
t.write("Welcome to the monopoly!", align="center", font=("Arial", 40, "normal"))
turtle.exitonclick()
# Use this allLines any time you read information from a text file
def remove_space(nickname):
file = open(nickname + '.txt', 'r')
allLines = file.readlines()
file.close()
for line in range(len(allLines)):
new = allLines[line].replace('\n', '')
allLines.pop(line)
allLines.insert(line, new)
return allLines
def log_in():
while True:
nickname = input("Enter player nickname: ")
if os.path.exists(nickname + '.txt'):
break
continue
while True:
allLines = remove_space(nickname)
password = 'test' # todo: I want to use the stars for entering password, so I don't make password input yet.
if password in allLines:
break
return nickname
def sign_up():
while True:
nickname = input("Pick your player name: ")
# To make sure they don't enter nothing and the name is not in the names
if len(nickname) >= 1 and nickname != ' ':
if nickname not in remove_space('names'):
namesFile = open('names.txt', 'a')
namesFile.write(nickname + '\n')
namesFile.close()
break
else:
print("The name already exists")
while True:
print("The password must include at least 1 upper case, 1 lower case and 1 digit.")
password = 'test' # todo: I want to to use the starts for entering password
break
while True:
email = input("Enter email(only gmail): ")
if '@gmail.com' in email:
break
continue
userallLines = open(nickname + '.txt', 'w')
userallLines.write(nickname + '\n' + password + '\n' + email + '\n')
userallLines.close()
return nickname
turtle_greeting()
allNames = open("names.txt", 'a')
allNames.close()
# Determine all the players in the game
while True:
players = input("Enter amount of players: ")
try:
if int(players) <= 1:
players = int(players)
continue
break
except ValueError:
continue
players = int(players)
all_players = []
for i in range(players):
while True:
print("Player %s\nWould you like to register(r) or log in(i)?" % str(i + 1))
direction = input()
if direction == 'i':
all_players.append(log_in())
break
elif direction == 'r':
all_players.append(sign_up())
break
"""
_____ ___ ___ ___ _____ _____ _____ ___ _____ _____
/ ___| / | / |/ | | ____| / ___/ |_ _| / | | _ \ |_ _|
| | / /| | / /| /| | | |__ | |___ | | / /| | | |_| | | |
| | _ / / | | / / |__/ | | | __| \___ \ | | / / | | | _ / | |
| |_| | / / | | / / | | | |___ ___| | | | / / | | | | \ \ | |
\_____/ /_/ |_| /_/ |_| |_____| /_____/ |_| /_/ |_| |_| \_\ |_| """
# todo: Most print statements could be replaced with graphics eventually
print("Game start:")
print("Players: \n\t%s" % '\n\t'.join(all_players))
# todo: I think I want to put here a class that consists of functions for each place
class Location:
def jail(self):
print("You have been sent to jail:(")
return
# The menu occurs after the turn is over to ask user for other things they would like to do
def menu(player):
pass
# This function can be called for every dice roll
def dice_roll():
dices_to_return = []
dice = input("Press any key to roll two dice: ")
dice_one = random.randint(1, 6)
dice_two = random.randint(1, 6)
if dice_one == dice_two:
are_the_dices_equal = True
else:
are_the_dices_equal = False
dices_to_return.append(are_the_dices_equal)
dices_to_return.append(dice_one)
dices_to_return.append(dice_two)
return dices_to_return
def turn(player, times=0):
roll = dice_roll()
are_equal = roll[0]
dice1 = roll[1]
dice2 = roll[2]
steps_to_move = dice1 + dice2
print("Dice 1: %s" % dice1)
print("Dice 2: %s" % dice2)
if are_equal:
if times == 2:
Location.jail()
return
print("Moving %s...." % steps_to_move)
player_position[player] += steps_to_move
# todo: Turtle graphics animation in here?
# todo: Buying/selling/rent etc
# dice roll returns False if the dices are not equal
if are_equal == True:
print("You rolled two dices the same! You get another turn!")
# Using recursion here because the player rolled double
times += 1
return turn(player, times)
menu(player)
def rounds():
global player_position
positions = {1: "Go", 2: "Purple #1", 3: "Community Chest", 4: "Purple #2",
5: "Income Tax", 6: "Railroad #1", 7: "Light Blue #1",
8: "Chance", 9: "Light Blue #2", 10: "Light Blue #3",
11: "Visiting Jail", 12: "Purple #1", 13: "Electric Company",
14: "Purple #2", 15: "Purple #3", 16: "Railroad #2",
17: "Orange #1", 18: "Community Chest", 19: "Orange #2", 20: "Orange #3",
21: "Free Parking", 22: "Red #1", 23: "Chance",
24: "Red #2", 25: "Red #3", 26: "Railroad #3",
27: "Yellow #1", 28: "Yellow #2", 29: "Water works", 30: "Yellow #3",
31: "Go to jail", 32: "Green #1", 33: "Green #2",
34: "Community Chest", 35: "Green #3", 36: "Railroad #4",
37: "Chance", 38: "Blue #1", 39: "Luxury Tax", 40: "Blue #2"}
# todo: I have no idea what to call the steets names(or most of the other things fWIW)
# I am gonna create a dictionary with all the positions
player_position = {}
for pl in range(players):
player_position[all_players[pl]] = 0
print(player_position)
# because the game lasts 20 rounds
for round in range(20):
for player in range(players):
print("%s's turn" % all_players[player])
print("___________________________________________")
turn(all_players[i])
rounds()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment