From 6fadac87339103cb7fca2936db0128de6090ad61 Mon Sep 17 00:00:00 2001 From: ckjolhede Date: Tue, 18 Jun 2024 09:38:10 -0600 Subject: [PATCH] game setup and add player --- Pipfile | 2 + lib/cli.py | 131 +++++- lib/debug.py | 25 +- lib/helpers.py | 9 - .canvas => lib/models/.canvas | 0 lib/models/__init__.py | 2 +- lib/models/game.py | 96 +++++ lib/models/game_space.py | 81 ++++ lib/models/helper.py | 36 ++ lib/models/model_1.py | 0 lib/models/player.py | 88 ++++ lib/models/space.py | 81 ++++ wireframe.drawio | 776 ++++++++++++++++++++++++++++++++++ 13 files changed, 1301 insertions(+), 26 deletions(-) delete mode 100644 lib/helpers.py rename .canvas => lib/models/.canvas (100%) create mode 100644 lib/models/game_space.py create mode 100644 lib/models/helper.py delete mode 100644 lib/models/model_1.py create mode 100644 wireframe.drawio diff --git a/Pipfile b/Pipfile index dd782fb..648cc4d 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,8 @@ name = "pypi" ipdb = "*" faker = "*" pytest = "7.1.3" +rich = "13.7.1" +pick = "2.3.2" [dev-packages] diff --git a/lib/cli.py b/lib/cli.py index edd1b62..65a32f5 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -1,28 +1,129 @@ # lib/cli.py +from rich import print +from rich import console +import os +import random +import time +from models.player import Player +from models.game_space import Game_space +from models.game import Game +from models.helper import Helper -from helpers import ( - exit_program, - helper_1 -) +player_home_positions = [3, 9, 15, 21] +def main_menu(): + print("Please select an option:") + print("1 Start New Game") + print("2 Exit Game") +def exit_program(): + print("Goodbye!") + exit() + def main(): while True: - menu() - choice = input("> ") - if choice == "0": + main_menu() + choice = input("What would you like to do?\n Enter the number of your choice") + if choice == "1": + new_game_setup() + elif choice == "2": exit_program() - elif choice == "1": - helper_1() else: print("Invalid choice") +if __name__ == "__main__": + main() -def menu(): - print("Please select an option:") - print("0. Exit the program") - print("1. Some useful function") +def new_game_setup_menu(): + print("New Game Menu:") + print("1 Add / Remove Players") + print("2 Change $ amount to win") + print("3 Start Game") + print("4 Quit Game") + +def new_game_setup(): + game = Game.create() + player_house_positions = [3, 9, 15, 21] + player_home_position = random.sample(player_house_positions, k=4) + os.system('clear') + new_game_setup_menu() + choice = input() + if choice == "1": + player_setup(game) + elif choice == "2": + print("Enter net worth needed to win") + print("Must be between 5000 and 20000") + game.win_condition == input(10000) + game.update() + elif choice == "3": + start_game() + elif choice == "4": + exit_program_prestart() + else: + print("That is not a valid input.") + print("Enter the number next to your choice") + + +def player_setup_menu(): + print("Players") + print("1 Add Player") + print("2 See All Players") + print("3 Remove Player") + print("4 Edit Player") + print("5 Return to Game Setup") + print("6 Quit Game") + +def player_setup(game): + os.system('clear') + player_setup_menu() + choice = input() + if choice == "1": + enter_new_player(game) + +def enter_new_player(game): + while 0 < len(name) < 16: + print("Enter Your Player's Name (required)") + print("Name must be less than 16 characters") + name = input() + name = name.upper + if 0 < len(name) < 16: + print("Name is invalid") + + print("\n, \n, \n, \n, \n") + print("Enter which type of player you would like to be") + print('1 REALTOR = The REALTOR receives 10 percent of all property purchases') + print('2 COP = The COP receives $50 from any player occupying the same space') + print('3 BUILDER = The BUILDER recieves a 20 percent discount on home purchases') + print('4 PILOT = The PILOT can roll an unlimited number of doubles') + value = input() + if value == "1": + player_type = "REALTOR" + elif value == "2": + player_type = "COP" + elif value == "3": + player_type = "BUILDER" + elif value == "4": + player_type = "PILOT" + else: + print("You must choose from the 4 player types") + player = Player.create(name, player_type, 0, 1800, 1800, game.id) + position = player_home_positions.pop + enter_player_home(position, player, game) + +def enter_player_home(position, player, game): + print("\n, \n, \n, \n, \n") + print("Each player begins with a home property") + print("What is your home's street name?") + street_name = input() + if len(street_name) == 0: + print("Street cannot be left blank") + + Game_space(game.id, player.id, street_name, 0, 100, position, None, 0, False) + -if __name__ == "__main__": - main() +def start_game(): + pass + +def exit_program_prestart(): + pass \ No newline at end of file diff --git a/lib/debug.py b/lib/debug.py index 6fadf6e..e080e53 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -3,6 +3,29 @@ from models.__init__ import CONN, CURSOR import ipdb +import os -ipdb.set_trace() +#ipdb.set_trace() +os.system("echo 'hello world'") +os.system("say -v Zarvox 'hello world'") + + + + + +#Mac OS X voices for using with the ‘say’ command + +#You can pick alternate voice profiles with the -v argument. + +#You can also use effects tags: + +#Having fun with say command + +#[[ slnc 5000 ]] : silence for 5s. +#[[volm 0.9]] changes the volume to the indicated level. +#[[volm +0.1]] increases the volume by the indicated level. +#[[rate 150]] changes the speed +#[[pbas 50]] changes the pitch. +#[[ rset ]] resets all these parameters to default +#'word' :quotes also put the emphasis on the word. \ No newline at end of file diff --git a/lib/helpers.py b/lib/helpers.py deleted file mode 100644 index f10df04..0000000 --- a/lib/helpers.py +++ /dev/null @@ -1,9 +0,0 @@ -# lib/helpers.py - -def helper_1(): - print("Performing useful function#1.") - - -def exit_program(): - print("Goodbye!") - exit() diff --git a/.canvas b/lib/models/.canvas similarity index 100% rename from .canvas rename to lib/models/.canvas diff --git a/lib/models/__init__.py b/lib/models/__init__.py index d5b061e..58043cf 100644 --- a/lib/models/__init__.py +++ b/lib/models/__init__.py @@ -1,4 +1,4 @@ import sqlite3 -CONN = sqlite3.connect('company.db') +CONN = sqlite3.connect('../monopolython.db') CURSOR = CONN.cursor() diff --git a/lib/models/game.py b/lib/models/game.py index e69de29..abbad59 100644 --- a/lib/models/game.py +++ b/lib/models/game.py @@ -0,0 +1,96 @@ +from __init__ import CURSOR, CONN +from sqlite3 import IntegrityError +from helper import Helper +from game_space import Game_space +from player import Player +from space import Space + +class Game(Helper): + + @classmethod + def create_table(cls): + """ Create a new table to persist the attributes of Game instances """ + try: + with CONN: + CURSOR.execute( + """CREATE TABLE IF NOT EXISTS games ( + win_condition INTEGER, + current_player TEXT, + next_player TEXT);""") + except IntegrityError as e: + return e + + @classmethod + def drop_table(cls): + """ Drop the table that persists Game instances """ + sql = """ + DROP TABLE IF EXISTS games; + """ + CURSOR.execute(sql) + CONN.commit() + + @classmethod + def create(cls, win_condition = 10000): + """ Initialize a new Game instance and save the object to the database """ + game = cls(win_condition) + game.save() + return game + + def __init__(self, win_condition, id = None): + self.win_condition = win_condition + self.players = [] + self.curr_player = "" + self.next_player = "" + self.id = id + + def __repr__(self): + return f"" + + def save(self): + sql = """ + INSERT INTO games (win_condition, curr_player, next_player) + VALUES (?, ?, ?) + """ + CURSOR.execute(sql, (self.win_condition, self.curr_player, self.next_player)) + CONN.commit() + self.id = CURSOR.lastrowid + + def update(self): + sql = """ + UPDATE games + SET win_condition = ?, curr_player = ?, next_player = ? + WHERE id = ? + """ + CURSOR.execute(sql, (self.win_condition, self.curr_player, self.next_player, self.id)) + CONN.commit() + + def delete(self): + sql = """ DELETE FROM games WHERE id = ? """ + CURSOR.execute(sql, (self.id,)) + CONN.commit() + + @property + def win_condition(self): + return self._win_condtion + + @win_condition.setter + def win_condition(self, win_condition): + if not isinstance(win_condition, int): + raise TypeError("Win Condition must be an integer") + elif 5000 < self.win_condition < 20000: + raise ValueError("Dollar amount must be between 5000 and 20000") + else: + self._win_condition = win_condition + + @property + def id(self): + return self._id + + @id.setter + def id(self, id): + if hasattr(self, "id"): + raise AttributeError("Cannot change player id") + elif not isinstance(id, int): + raise TypeError("Id must be an integer") + else: + self._id = id \ No newline at end of file diff --git a/lib/models/game_space.py b/lib/models/game_space.py new file mode 100644 index 0000000..32b9470 --- /dev/null +++ b/lib/models/game_space.py @@ -0,0 +1,81 @@ +from __init__ import CURSOR, CONN +from sqlite3 import IntegrityError +from space import Space +from game import Game +from player import Player +from helper import Helper + +class Game_space: + + @classmethod + def create_table(cls): + try: + with CONN: + CURSOR.execute( + """CREATE TABLE IN NOT EXISTS game_spaces ( + id INTEGER PRIMARY KEY, + game_id INTEGER FOREIGN KEY, + player_id INTEGER FOREIGN KEY, + street_name TEXT, + price INTEGER, + rent INTEGER, + position INTEGER, + neighborhood TEXT, + houses INTEGER + monopoly BOOLEAN);""") + except IntegrityError as e: + return e + + @classmethod + def drop_table(cls): + sql = """ + DROP TABLE IF EXISTS game_spaces; + """ + CURSOR.execute(sql) + CONN.commit() + + @classmethod + def create(cls, game_id, player_id, street_name, price, rent, position, neighborhood, houses, monopoly): + game_space = cls(game_id, player_id, street_name, price, rent, position, neighborhood, houses, monopoly) + game_space.save() + return game_space + + def __init__(self, game_id, position, id = None): + space = Space.find_by_space_position(position) + self.game_id = game_id + self.player_id = Game.curr_player.id + self.street_name = space.street_name + self.price = space.price + self.rent = space.rent + self.position = position + self.neighborhood = space.neighborhood + self.houses = 0 + self.monopoly = False + self.id = id + + def __repr__(self): + return f"<{self.street_name}: Price = {self.price}: Rent = {self.rent}: Neighborhood = {self.neighborhood}: Number of Houses = {self.houses}: Owner = {(self.find_owner_by_playerid(self.player_id)).name}>" + + def save(self): + sql = """ + INSERT INTO game_spaces (game_id, player_id, street_name, price, rent, position, neighborhood, houses, monopoly) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?); + """ + CURSOR.execute(sql, (self.game_id, self.player_id, self.street_name, self.price, self.rent, self.position, self.neighborhood, self.houses, self.monopoly)) + CONN.commit() + self.id = CURSOR.lastrowid + + def update(self): + sql = """ + UPDATE game_spaces + SET player_id = ?, street_name = ?, rent = ?, houses = ?, monopoly = ? + WHERE id = ?; + """ + CURSOR.execute(sql, (self.player_id, self.street_name, self.rent, self.houses, self.monopoly, self.id)) + CONN.commit() + + def delete(self): + sql = """ DELETE FROM game_spaces WHERE id = ?;""" + CURSOR.execute(self, (self.id,)) + CONN.commit() + \ No newline at end of file diff --git a/lib/models/helper.py b/lib/models/helper.py new file mode 100644 index 0000000..43c1a2e --- /dev/null +++ b/lib/models/helper.py @@ -0,0 +1,36 @@ +# lib/helper.py +import os +from game import Game + + +class Helper(): + + + + + + def start_game(): + pass + + + + + + +# Larger example that inserts many records at a time +#purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00), +# ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00), +# ('2006-04-06', 'SELL', 'IBM', 500, 53.00), +# ] +#cur.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases) + +# exec(string) will execute the string as regular code, string must be properly formatted +# from random import choice, sample, choices +# names = list[str] = ['a', 'b', 'c', 'd'] + #winner: str = choice(names) + #print(winner) produces random selecton from list + + #winners: list[str] = sample(names, k=(#number of selections from list you want) <-- output unique list + #winners: list[str] = choices(names, k=(#number of selections from list you want) <-- may have repeat selections + +from random import randint as ri \ No newline at end of file diff --git a/lib/models/model_1.py b/lib/models/model_1.py deleted file mode 100644 index e69de29..0000000 diff --git a/lib/models/player.py b/lib/models/player.py index e69de29..3dc3d5f 100644 --- a/lib/models/player.py +++ b/lib/models/player.py @@ -0,0 +1,88 @@ +from __init__ import CURSOR, CONN +from sqlite3 import IntegrityError +from helper import Helper +from game import Game +from game_space import Game_space +from space import Space + +class Player(Helper): + + @classmethod + def create_table(cls): + try: + with CONN: + CURSOR.execute( + """CREATE TABLE IF NOT EXISTS players ( + id INTEGER PRIMARY KEY, + name TEXT, + player_type TEXT, + curr_pos INTEGER, + money INTEGER, + net_worth INTEGER, + game_id INTEGER FOREIGN KEY);""") + except IntegrityError as e: + return e + + @classmethod + def drop_table(cls): + sql = """ + DROP TABLE IF EXISTS players; + """ + CURSOR.execute(sql) + CONN.commit() + + @classmethod + def create(cls, name, player_type, curr_pos = 0, money = 1800, net_worth = 1800, game_id = None): + player = cls(name, player_type, curr_pos, money, net_worth, game_id) + player.save() + return player + + def __init__(self, name, player_type, curr_pos = 0, money = 1800, net_worth = 1800, game_id = None, id = None): + self.name = name + self.player_type = player_type + self.curr_pos = curr_pos + self.money = money + self.net_worth = net_worth + self.game_id = game_id + self.id = id + + def __repr__(self): + return f"<{self.name}: Player Type = {self.player_type}: Money = {self.money}: Net Worth = {self.net_worth}: Postion = {Game_space.find_by_position(self.curr_pos).street_name}>" + + def save(self): + sql = """ + INSERT INTO players (name, player_type, curr_pos, money, net_worth, game_id) + VALUES (?, ?, ?, ?, ?, ?}; + """ + CURSOR.execute(sql, (self.name, self.player_type, self.curr_pos, self.money, self.net_worth, self.game_id)) + CONN.commit() + self.id = CURSOR.lastrowid + + def update(self): + sql = """ + UPDATE players + SET name = ?, player_type = ?, curr_pos = ?, money = ?, net_worth = ? + WHERE id = ?; + """ + CURSOR.execute(sql, (self.name, self.player_type, self.curr_pos, self.money, self.net_worth, self.id)) + CONN.commit() + + def delete(self): + sql = """ DELETE FROM players WHERE id = ?;""" + CURSOR.execute(sql, (self.id,)) + CONN.commit() + + + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + if not isinstance(name, int): + raise TypeError("Player name must be a string") + elif 0 < len(name) < 16: + raise ValueError("Player name must be less than 16 characters") + else: + self._name = name \ No newline at end of file diff --git a/lib/models/space.py b/lib/models/space.py index e69de29..0f156dc 100644 --- a/lib/models/space.py +++ b/lib/models/space.py @@ -0,0 +1,81 @@ +from __init__ import CURSOR, CONN +import sqlite3 +from sqlite3 import IntegrityError +from helper import Helper +from game_space import Game_space +from player import Player + +CONN = sqlite3.connect('resources.db') +CURSOR = CONN.execute() + +class Space(Helper): + + @classmethod + def create_table(cls): + sql = """(CREATE TABLE IF NOT EXISTS spaces + (id = INTEGER PRIMARY KEY, + street_name TEXT, + price INTEGER, + rent INTEGER, + position = INTEGER, + neighborhood = TEXT);""" + CURSOR.execute(sql) + CONN.commit() + + @classmethod + def drop_table(cls): + sql = """ DROP TABLE IF EXISTS spaces; """ + CURSOR.execute(sql) + CONN.commit() + + @classmethod + def create(cls, street_name, price, rent, position, neighborhood): + space = cls(street_name, price, rent, position, neighborhood) + space.save() + return space + + @classmethod + def find_by_space_position(cls, position): + sql = """ SELECT * FROM space WHERE position = ? LIMIT 1;""" + row = CURSOR.execute(sql, (position,)).fetchone() + return cls.instance_from_db(row) if row else None + + @classmethod + def instance_from_db(cls, row): + return cls( + id=row[0], + street_name=row[1], + price=row[2], + rent=row[3], + position=row[4], + neighborhood=row[5]) + + def __init__(self, street_name, price, rent, position, neighborhood, id = None): + self.street_name = street_name + self.price = price + self.rent = rent + self.position = position + self.neighborhood = neighborhood + self.id = id + + def save(self): + sql = """INSERT INTO spaces (street_name, price, rent, position, neighborhood) + VALUES (?, ?, ?, ?); """ + CURSOR.execute(sql (self.street_name, self.price, self.rent, self.position, self.neighborhood)) + CONN.commit() + self.id = CURSOR.lastrowid + + def update(self): + sql = """UPDATE spaces + SET street_name = ?, price = ?, rent = ?, position = ?, neighborhood = ? + WHERE id = ?;""" + CURSOR.execute(sql, (self.street_name, self.price, self.rent, self.position, self.neighborhood)) + CONN.commit() + + def delete(self): + sql = """ DELETE FROM spaces WHERE id = ?;""" + CURSOR.execute(sql, (self.id,)) + CONN.commit() + + def __repr__(self): + return f"<{self.street_name}: Price = {self.price}: Rent = {self.rent}: Neighborhood = {self.neighborhood}>" \ No newline at end of file diff --git a/wireframe.drawio b/wireframe.drawio new file mode 100644 index 0000000..3fd9446 --- /dev/null +++ b/wireframe.drawio @@ -0,0 +1,776 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file