Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __pycache__/board.cpython-39.pyc
Binary file not shown.
23 changes: 0 additions & 23 deletions board.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import random


# Global variables


def dice_roll(player):
first_dice = random.randint(1, 6)
second_dice = random.randint(1, 6)
total_dice = first_dice + second_dice
# if first_dice == second_dice:
# print(f'{player.username} rolled doubles of {first_dice}!') # Change player name
# print(f'{player.username} rolled {total_dice}', end='\n\n') # Change player name
return total_dice


# models board:
# testVar = 20

Expand Down Expand Up @@ -155,16 +142,6 @@ def dice_roll(player):


# Moves list, change this to display on the web page
def display_moves():
# Display player stats(name, cash in hand, rounds played, position)
print('-------------Moves-------------')

print("{:<30}{}".format('Roll dice', 'r'))
print("{:<30}{}".format('Build a house', 'h'))
print("{:<30}{}".format('Build a hotel', 'f'))
print("{:<30}{}".format('View assets owned', 'v'))
print("{:<30}{}".format('Sell property', 's'))
print("{:<30}{}".format('End turn', 'x'), end='\n\n\n')

# display_moves()

Expand Down
Binary file added database/__pycache__/Dao.cpython-39.pyc
Binary file not shown.
Binary file added database/__pycache__/DaoConstants.cpython-39.pyc
Binary file not shown.
5 changes: 2 additions & 3 deletions game_driver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import board
from board import display_moves, dice_roll
from utils.driver import game_over
from utils.driver import game_over, dice_roll, moves_list
from models import player
import database.db_connect

Expand Down Expand Up @@ -79,7 +78,7 @@ def game_start():
print(f'Cash - {player.balance}\t Rounds played - {player.game_round}\t Player position - {player.position}')
print('----------------------------------------------------------------')

display_moves()
moves_list()

turn_ended = False
has_rolled = False
Expand Down
Binary file modified models/__pycache__/player.cpython-39.pyc
Binary file not shown.
Binary file added models/__pycache__/tile.cpython-39.pyc
Binary file not shown.
33 changes: 15 additions & 18 deletions models/player.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import List, Any
import mysql.connector

import board


class Player:
def __init__(self, room_id, player_id, username,money,position,game_round):
Expand All @@ -22,10 +20,7 @@ def player_moves(self, dice_value):
self.add_balance(200)
self.game_round += 1
# print(player.position)
print(f'You are now at {board.BOARD_TILES[self.position]}')
print(
f'Description - {board.BOARD_TILES_INFO[board.BOARD_TILES[self.position]][0]}',
end='\n\n\n')
return self.position

def display_player_details(self):
print(f'Name - {self.username}')
Expand All @@ -34,37 +29,39 @@ def display_player_details(self):
print(f'Assets owned - {self.assets_owned}')

def buy_tile(self, tile):
self.assets_owned.append(tile)
price = board.BOARD_TILES_INFO[tile][2]
# Adding name of the tile to the list of assets owned
self.assets_owned.append(tile.tile_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think u need to append the tile id so that we can store it in player property table, since that table only stores room id player id and tile id

price = tile.cost
self.balance = self.balance - price

def sell_tile(self, tile):
self.assets_owned.remove(tile)
price = board.BOARD_TILES_INFO[tile][2]
# Removing name of the tile from list of assets owned
self.assets_owned.remove(tile.tile_name)
price = tile.cost / 2
self.balance = self.balance + price
print(f'Property Sold! New Balance - {self.balance}')

def build_house(self, tile):
if tile in self.assets_owned:
self.balance -= board.BOARD_TILES_INFO[tile][5]
print(f'House built on {tile}')
self.balance -= tile.house_cost
print(f'House built on {tile.tile_name}')
else:
print('Asset is not owned!')
return False
pass

def build_hotel(self, tile):
if tile in self.assets_owned:
self.balance -= board.BOARD_TILES_INFO[tile][6]
print(f'Hotel built on {tile}')
self.balance -= tile.hotel_cost
print(f'Hotel built on {tile.tile_name}')
else:
print('Asset is not owned!')
return False
pass

def charge_rent(self, tile):
rent = board.BOARD_TILES_INFO[board.BOARD_TILES[tile]][4][0]
print(rent)
rent = tile.rent
# print(rent)
self.balance -= rent
return rent

Expand All @@ -75,7 +72,7 @@ def reduce_balance(self, reduced_amount):
self.balance -= reduced_amount

def check_balance(self, tile):
if self.balance > board.BOARD_TILES_INFO[board.BOARD_TILES[tile]][2]:
if self.balance > tile.cost:
return True
else:
return False
Expand All @@ -85,7 +82,7 @@ def go_to_jail(self):
print('Sent to Jail!', end='\n\n')
self.game_round += 2

def printPlayer(self):
def print_player(self):
print(vars(self))


Expand Down
Binary file not shown.
Binary file added service/__pycache__/room.cpython-39.pyc
Binary file not shown.
142 changes: 72 additions & 70 deletions service/monopoly_Instance.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import board
from board import display_moves, dice_roll
#from utils.driver import game_over
from models.player import Player
from utils.driver import dice_roll, moves_list
from models.player import Player
from utils.loging import log

import database.Dao as databaseObj
import database.DaoConstants as DaoConst
from utils.loging import log


class monopoly_Instance:
def __init__(self, roomID, player_list):
self.db=databaseObj.Dao()
self.daoConst=DaoConst.DaoConstants()
self.player_list=player_list
self.is_game_over= False
self.tiles= self.db.select_all_query(self.daoConst.GET_PROPERTY_LIST,True)
self.logger=log()

def special_cards(self,tile, player):
self.db = databaseObj.Dao()
self.daoConst = DaoConst.DaoConstants()
self.player_list = player_list
self.is_game_over = False
self.tiles = self.db.select_all_query(self.daoConst.GET_PROPERTY_LIST, True)
self.logger = log()

def special_cards(self, tile, player):
if tile == 'Start/GO':
player.add_balance(200)
print('Collected $200!')
Expand All @@ -34,67 +34,72 @@ def special_cards(self,tile, player):
self.logger.log_info('Income Tax of 200 has been deducted. Remove this print statement at the end')
print('Income Tax of 200 has been deducted. Remove this print statement at the end')


def game_winner(self):
highest_balance = 0
winner = None #add proper logic to get the sume of all property values(money) owned plus money and evaluate for each
winner = None # add proper logic to get the sume of all property values(money) owned plus money and evaluate for each

for player in self.player_list:
if player.balance > highest_balance:
highest_balance = player.balance
winner = player
#Write a dao stuff for updating winner for the room
# Write a dao stuff for updating winner for the room
return winner


def game_start(self):
#global is_game_over
# print('This is playerLIst',self.player_list)
# global is_game_over
# print('This is playerLIst',self.player_list)
while not self.is_game_over:
correct_move= True
correct_move = True
turn_ended = False
for player in self.player_list:

# Displays player details
message= "%s's turn" % player.username
message = "%s's turn" % player.username
self.logger.log_info(message)
self.db.insertion_query(self.daoConst.INSERT_LOG, (message.replace("'","''") , player.room_id))
self.db.insertion_query(self.daoConst.INSERT_LOG, (message.replace("'", "''"), player.room_id))

print(f'Cash - {player.balance}\t Rounds played - {player.game_round}\t Player position - {player.position}')
print(
f'Cash - {player.balance}\t Rounds played - {player.game_round}\t Player position - {player.position}')
print('----------------------------------------------------------------')

display_moves()
moves_list()

turn_ended = False
has_rolled = False

# Move validation
while not turn_ended:
game_input = input('Enter your choice: ') # insert in log and display.
# game_input = await request.body()
game_input = input('Enter your choice: ') # insert in log and display.
# game_input = await request.body()
# Roll dice
if game_input.casefold() == 'r':
if not has_rolled:
has_rolled = True
player.player_moves(dice_roll(player)) #diceroll rolls 2 dices for a player and moves them

current_tile=self.tiles[player.position]

asset_owned = False # assign this by

#Execute to get the tile_owner
tileInput=[current_tile.tile_id,player.room_id]
#tileOWNERInput
tileOWNERInput=self.db.select_query( self.daoConst.GET_PROPERTY_OWNER,tileInput)
print('This is tile owner',tileOWNERInput)
tile_owner=None
if tileOWNERInput :
tile_owner = Player(room_id=tileOWNERInput[0][0], player_id=tileOWNERInput[0][1], username=tileOWNERInput[0][2], money=tileOWNERInput[0][3], position=tileOWNERInput[0][4], game_round=tileOWNERInput[0][5])

if current_tile.cost != None :
if tile_owner is None:
player.player_moves(dice_roll(player)) # diceroll rolls 2 dices for a player and moves them

current_tile = self.tiles[player.position]

print(f'{player.username} landed on {current_tile.tile_name} - {current_tile.description}')

print(player.print_player())

asset_owned = False # assign this by

# Execute to get the tile_owner
tileInput = [current_tile.tile_id, player.room_id]
# tileOWNERInput
tileOWNERInput = self.db.select_query(self.daoConst.GET_PROPERTY_OWNER, tileInput)
print('This is tile owner', tileOWNERInput)
tile_owner = None
if tileOWNERInput:
tile_owner = Player(room_id=tileOWNERInput[0][0], player_id=tileOWNERInput[0][1],
username=tileOWNERInput[0][2], money=tileOWNERInput[0][3],
position=tileOWNERInput[0][4], game_round=tileOWNERInput[0][5])

if current_tile.cost != None:
if tile_owner is None:
print('The current tile owner is None')
#BUYs LOGIC
# Nobody owns the tile, player can buy
is_buy = input(f'Buy {current_tile.tile_name}? [Y/N]')

if is_buy.casefold() == 'y':
Expand All @@ -105,36 +110,36 @@ def game_start(self):
print("insufficient Funds !!")

elif tile_owner.player_id != player.player_id:
print('The current tile cost is: ',tile_owner.player_id )
print('The current tile cost is: ', tile_owner.player_id)
# Deduct from current player
rent = player.charge_rent(player.position)
rent = player.charge_rent(player.position)
print(f'{tile_owner.username} charges you {rent} as rent')
tile_owner.add_balance(rent)# insert updated money in DB
asset_owned = True # create entry in player_property
break
tile_owner.add_balance(rent) # insert updated money in DB
asset_owned = True # create entry in player_property
break

else:
print('Player is on his own tile')
#asset_owned = True
break
else :
print('SPECIAL CARD!!!' ,vars(current_tile))

print('Player is on his own tile')
# asset_owned = True
break
else:
print('SPECIAL CARD!!!', vars(current_tile))
# Insert non-buyable logic special card
self.special_cards(current_tile, player)

else:
print('You have already rolled the dice')# insert in log and display.
print('You have already rolled the dice') # insert in log and display.
continue



# The house and hotel are designed in such a way that only if you step on the tile, you can build them
# Build a house
elif game_input.casefold() == 'h':
# Insert house logic here
current_tile = board.BOARD_TILES[player.position]
house_cost = board.BOARD_TILES_INFO[current_tile][4][1]
current_tile = self.tiles[player.position]
house_cost = current_tile.house_cost

if player.balance > house_cost:
player.build_house(current_tile)
Expand All @@ -144,10 +149,10 @@ def game_start(self):
# Build a hotel
elif game_input.casefold() == 'f':
# Insert hotel logic here
current_tile = board.BOARD_TILES[player.position]
house_cost = board.BOARD_TILES_INFO[current_tile][4][1]
current_tile = self.tiles[player.position]
hotel_cost = current_tile.hotel_cost

if player.balance > house_cost:
if player.balance > hotel_cost:
player.build_house(current_tile)
else:
print('Insufficient Funds!')
Expand All @@ -169,7 +174,7 @@ def game_start(self):
if player.assets_owned:
[print(i, end=', ') for i in player.assets_owned]
print(end='\n\n')
sell_property = int(input(f'Enter 0 - {len(player.assets_owned)-1}: '))
sell_property = int(input(f'Enter 0 - {len(player.assets_owned) - 1}: '))
player.sell_tile(player.assets_owned[sell_property])
print(end='\n\n')
else:
Expand All @@ -187,26 +192,23 @@ def game_start(self):
else:
print('Error! Incorrect Input')


# Game over condition
self.is_game_over = self.game_over(player)
if(self.is_game_over):
if (self.is_game_over):
self.game_winner(self)
break


# Displays game stats at the end
def getGameStats(self):
def getGameStats(self):
for player in self.player_list:
player.display_player_details()
print('------------------------', end='\n')

#Check if 15 rounds are reached
def game_over(self,player):
# Check if 15 rounds are reached
def game_over(self, player):
if player.game_round == 15 or player.balance <= 0:
print('Game Over!')
return True

else:
return False

2 changes: 1 addition & 1 deletion service/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def play(self):
# Modify this insert to fetch position
player_list.append(player.Player(room_id=player_details[i][0], player_id= player_details[i][1],username= usernames[i][0],
money=player_details[i][2],game_round=player_details[i][5],position= player_details[i][4]))
print('Printing list:',player_list[0].printPlayer())
print('Printing list:', player_list[0].print_player())

m=monopoly.monopoly_Instance(roomID=1,player_list=player_list) # need to get roomid over here from web
m.game_start()
Expand Down
Binary file added utils/__pycache__/loging.cpython-39.pyc
Binary file not shown.
Loading