ThisIt is a simple Poker balance/bid tracker to automate calculations for players' scores.
ThisIt is a project I made to practice the concepts of lists and indices. And thus heavily relies on indexing and list search.
I know I could have used dictionaries, but I chose not to in order to get familiar with previously mentioned concepts.
I'm still learning using MIT online course, so I'm only familiar with a limited number of topics, including Branching and Iteration, String Manipulation / bisectional search / Guess And Check / Approximation, Decomposition / Abstraction / Function Definition, Sequences including lists, tuples, and Dictionaries.
Please takeconsider that into consideration when providing an answer.
import string
def new_line(n):
'''
prints (n) new lines
'''
for n in range(n):
print()
def x_player(list_secondary, string, list_primary):
'''
returns the item from a list(list_primary) at
the index of another string (string) in another list(list_secondary)
'''
return list_primary[list_secondary.index(string)]
def get_space(pos_list, pos_string, string, extra_length):
'''
returns the given string(string) argument multiplied by
the length of the longest string in a list(pos_list)
minus the length of another_string (pos_string)
plus any extra length determined by the user.
'''
lengths = [ len(string) for string in pos_list]
h_length = max(lengths) + extra_length
return ( string * ( h_length - len(pos_string)))
def list_players(player_names, player, list_):
'''
prints the player name + appropriate space + an item
at the index of : (player in player_names) from a different
list (list_)
'''
print ( player + get_space(player_names, player, ' ', 1) + ': '+ str(x_player(player_names, player, list_)))
def list_balances(player_names, balances):
'''
lists the player names and their balances.
'''
print('Current Balances:')
for player in player_names:
list_players(player_names, player, balances)
def list_bids(player_names, bids):
'''
lists the player names and their bids
'''
print('Current Bids:')
for player in player_names:
list_players(player_names, player, bids)
def poker():
'''
starts the poker score counter
'''
# ask for number of players and their names.
n_players = int(input('Number Of Players?... '))
player_names = []
for n in range(n_players):
player = input('Player # ' + str(n+1) + ' .... ')
player_names.append(player)
# iniate lists for balances, bids and list to determine if a player has folded or not.
balances = [ 90 for n in range(n_players)]
bids = [10 for n in range(len(player_names))]
fold_code = [ 0 for n in range(len(player_names)) ]
#balance_codes = [ 0 for n in range(n_players)]
# this variable will determine if a round is over or not.
this_round = ''
# total bid:
total = sum(bids)
# getting user's confirmation to start the counter.
# this variable will not be used again.
stop = input('Press Enter To Continue...')
print('Starting Game...')
# Intitial Balance / Bid listing.
list_balances(player_names, balances)
list_bids(player_names, bids)
while True:
# to determine if a round is over.
if this_round == 'y':
print('Who Won?')
winner = input()
# adding the total bid to the winner's balance.
balances[player_names.index(winner)] += total
new_line(1)
print(winner, 'Won This Round')
new_line(1)
list_balances(player_names, balances)
# checking for players who are out.
# a for loop that iterates n_player times. to avoid
# indices being out of range after removing a player from the list.
for var in range(n_players):
if 0 in balances:
print (player_names[balances.index(0)].upper(), 'Is Out...')
player_names.pop(balances.index(0))
bids.pop(balances.index(0))
balances.pop(balances.index(0))
# self-explanatory using the print statement.
for player in player_names:
balances[player_names.index(player)] -= 10
# re-intializing bids and fold code after each round.
# fold code is used to determine if a player has folded for the round.
bids = [ 10 for n in range(len(player_names))]
fold_code = [0 for n in range(len(player_names))]
print('Deducting 10$ from all players for the new round...')
new_line(1)
list_balances(player_names, balances)
new_line(1)
list_bids(player_names, bids)
new_line(1)
print('-' * 35)
for player in player_names:
# if a player folds for the round the value in the list ( fold-code) 'at the same index'
# gets assigned to be '1' therefore the loop ends and moves on to the next player.
if fold_code[player_names.index(player)] == 1:
continue
# asking for user input ( player action for the round.
print('>' * 15, player.upper(), '>' * 15 )
action = input ()
# c i.e : 'call', which then evaluates the difference between the max bid and
# the current player's bid and later on in the program subtracts it from the \
# player's balance and adds it to the total bid.
if action == 'c':
bid = max(bids) - x_player(player_names, player, bids)
print(player, 'Called...')
# # r i.e : 'raise', does the same process using the 'bid' variable but adds the raise value to it.
elif action == 'r':
print('Raise By?')
r = int(input())
bid = max(bids) - x_player(player_names, player, bids) + r
print(player, 'Raised By', r)
# fold makes the 'bid' variable = 0 and makes the program skip the player's turn until the round is over.
elif action == 'f' :
fold_code[player_names.index(player)] = 1
bid = 0
print(player, 'Folded For the round')
# similar to fold but doesn't skip the player's turn
else:
bid = 0
print(player, 'Checked...')
# deducting the 'bid' variable from the player's balance and adding it to the respective value in the bids list.
balances[player_names.index(player)] -= bid
bids[player_names.index(player)] += bid
total = sum(bids)
new_line(1)
list_balances(player_names, balances)
new_line(1)
list_bids(player_names, bids)
new_line(1)
print('Total Bid : ' + str(total))
new_line(2)
print('-' * 35)
# the decision to end a round is left to the user, plans to implement automatic round termination has been made.
print('Round Over?')
this_round = input()
new_line(20)
poker()
import string
def new_line(n):
'''
prints (n) new lines
'''
for n in range(n):
print()
def x_player(list_secondary, string, list_primary):
'''
returns the item from a list(list_primary) at
the index of another string (string) in another list(list_secondary)
'''
return list_primary[list_secondary.index(string)]
def get_space(pos_list, pos_string, string, extra_length):
'''
returns the given string(string) argument multiplied by
the length of the longest string in a list(pos_list)
minus the length of another_string (pos_string)
plus any extra length determined by the user.
'''
lengths = [ len(string) for string in pos_list]
h_length = max(lengths) + extra_length
return ( string * ( h_length - len(pos_string)))
def list_players(player_names, player, list_):
'''
prints the player name + appropriate space + an item
at the index of : (player in player_names) from a different
list (list_)
'''
print ( player + get_space(player_names, player, ' ', 1) + ': '+ str(x_player(player_names, player, list_)))
def list_balances(player_names, balances):
'''
lists the player names and their balances.
'''
print('Current Balances:')
for player in player_names:
list_players(player_names, player, balances)
def list_bids(player_names, bids):
'''
lists the player names and their bids
'''
print('Current Bids:')
for player in player_names:
list_players(player_names, player, bids)
def poker():
'''
starts the poker score counter
'''
# ask for the number of players and their names.
n_players = int(input('Number Of Players?... '))
player_names = []
for n in range(n_players):
player = input('Player # ' + str(n+1) + ' .... ')
player_names.append(player)
# iniate lists for balances, bids and list to determine if a player has folded or not.
balances = [ 90 for n in range(n_players)]
bids = [10 for n in range(len(player_names))]
fold_code = [ 0 for n in range(len(player_names)) ]
#balance_codes = [ 0 for n in range(n_players)]
# this variable will determine if a round is over or not.
this_round = ''
# total bid:
total = sum(bids)
# getting user's confirmation to start the counter.
# this variable will not be used again.
stop = input('Press Enter To Continue...')
print('Starting Game...')
# Intitial Balance / Bid listing.
list_balances(player_names, balances)
list_bids(player_names, bids)
while True:
# to determine if a round is over.
if this_round == 'y':
print('Who Won?')
winner = input()
# adding the total bid to the winner's balance.
balances[player_names.index(winner)] += total
new_line(1)
print(winner, 'Won This Round')
new_line(1)
list_balances(player_names, balances)
# checking for players who are out.
# a for loop that iterates n_player times. to avoid
# indices being out of range after removing a player from the list.
for var in range(n_players):
if 0 in balances:
print (player_names[balances.index(0)].upper(), 'Is Out...')
player_names.pop(balances.index(0))
bids.pop(balances.index(0))
balances.pop(balances.index(0))
# self-explanatory using the print statement.
for a player in player_names:
balances[player_names.index(player)] -= 10
# re-initializing bids and fold code after each round.
# fold code is used to determine if a player has folded for the round.
bids = [ 10 for n in range(len(player_names))]
fold_code = [0 for n in range(len(player_names))]
print('Deducting 10$ from all players for the new round...')
new_line(1)
list_balances(player_names, balances)
new_line(1)
list_bids(player_names, bids)
new_line(1)
print('-' * 35)
for player in player_names:
# if a player folds for the round the value in the list ( fold-code) 'at the same index'
# gets assigned to be '1' therefore the loop ends and moves on to the next player.
if fold_code[player_names.index(player)] == 1:
continue
# asking for user input ( player action for the round.
print('>' * 15, player.upper(), '>' * 15 )
action = input ()
# c i.e : 'call', which then evaluates the difference between the max bid and
# the current player's bid and later on in the program subtracts it from the \
# player's balance and adds it to the total bid.
if action == 'c':
bid = max(bids) - x_player(player_names, player, bids)
print(player, 'Called...')
# # r i.e : 'raise', does the same process using the 'bid' variable but adds the raise value to it.
elif action == 'r':
print('Raise By?')
r = int(input())
bid = max(bids) - x_player(player_names, player, bids) + r
print(player, 'Raised By', r)
# fold makes the 'bid' variable = 0 and makes the program skip the player's turn until the round is over.
elif action == 'f' :
fold_code[player_names.index(player)] = 1
bid = 0
print(player, 'Folded For the round')
# similar to fold but doesn't skip the player's turn
else:
bid = 0
print(player, 'Checked...')
# deducting the 'bid' variable from the player's balance and adding it to the respective value in the bids list.
balances[player_names.index(player)] -= bid
bids[player_names.index(player)] += bid
total = sum(bids)
new_line(1)
list_balances(player_names, balances)
new_line(1)
list_bids(player_names, bids)
new_line(1)
print('Total Bid : ' + str(total))
new_line(2)
print('-' * 35)
# the decision to end a round is left to the user, plans to implement automatic round termination has been made.
print('Round Over?')
this_round = input()
new_line(20)
poker()
```