├── README.md ├── black-jack └── black-jack.py ├── caesercipher └── caesarCipherAlgorithm.py ├── closestpair └── closestPairProblem.py ├── musicplayer └── pythonMusicPlayer.py └── tictactoe └── ticTacToe.py /README.md: -------------------------------------------------------------------------------- 1 | # python-projects 2 | 3 | ## Intermediate-level Projects in Python 4 | 5 | 1. Blackjack 6 | 2. Caeser Cipher Alogrithm 7 | 3. Closest Pair Algorithm 8 | 4. Music Player 9 | 5. Tic Tac Toe 10 | -------------------------------------------------------------------------------- /black-jack/black-jack.py: -------------------------------------------------------------------------------- 1 | #!/Users/Utsav/downloads/udemy python 2 | 3 | # For using the same code in either Python 2 or 3 4 | # from __future__ import print_function 5 | 6 | """ milestone_project_1.py: BlackJack Game """ 7 | 8 | __author__ = "Utsav Shah" 9 | __copyright__ = "Copyright 2017, The Austin Side Project" 10 | __credits__ = ["Hustle"] 11 | __license__ = "UTS" 12 | __version__ = "1.1.0" 13 | __maintainer__ = "Utsav Shah" 14 | __email__ = "utsavshah507@gmail.com" 15 | __status__ = "Productive" 16 | 17 | ## Milestone Project 2 18 | 19 | # Importing libraries -- used for shuffling cards 20 | import random 21 | 22 | # Boolean type to know whether play is in hand 23 | playing = False 24 | 25 | # Amount for buy-in 26 | chip_pool = 100 27 | # raw_input('Enter the amount for buy-in: ') 28 | print 'Your buy-in amount is: ',chip_pool 29 | 30 | bet = 1 31 | 32 | restart_phrase = "Press d to deal the cards again, or press q to quit." 33 | 34 | # Hearts, Diamonds, Clubs, Spades 35 | suits = ('H','D','S','C') 36 | 37 | # Possible Card Ranks 38 | ranking = ('A','2','3','4','5','6','7','8','9','10','J','Q','K') 39 | 40 | # Point Val Dict (Dual existence of Ace is defined later) 41 | card_val = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':10, 'Q':10, 'K':10} 42 | 43 | # Creating Card Class 44 | 45 | class Card: 46 | 47 | def __init__(self, suit, rank): 48 | self.suit = suit 49 | self.rank = rank 50 | 51 | def __str__(self): 52 | return self.suit + self.rank 53 | 54 | def grab_suit(self): 55 | return self.suit 56 | 57 | def grab_rank(rank): 58 | return self.rank 59 | 60 | def draw(self): 61 | print (self.suit + self.rank) 62 | 63 | # Creating Hand Class 64 | # Gives dual existence to Ace 65 | 66 | class Hand: 67 | 68 | def __init__(self): 69 | self.cards = [] 70 | self.value = 0 71 | 72 | # Aces can be 1 0r 11 as defined below 73 | self.ace = False 74 | 75 | def __str__(self): 76 | '''Return a string of current hand composition''' 77 | hand_comp = "" 78 | 79 | # List Comprehension 80 | for card in self.cards: 81 | card_name = card.__str__() 82 | hand_comp += " " + card_name 83 | 84 | return 'The hand has %s' %hand_comp 85 | 86 | def card_add(self,card): 87 | '''Add another card to the hand''' 88 | self.cards.append(card) 89 | 90 | # Checking for Aces 91 | if card.rank == 'A': 92 | self.ace = True 93 | self.value += card_val[card.rank] 94 | 95 | def calc_val(self): 96 | '''Calculating value of hand, making aces = 1 if they don't bust the hand''' 97 | 98 | if (self.ace == True and self.value < 12): 99 | return self.value + 10 100 | else: 101 | return self.value 102 | 103 | def draw(self, hidden): 104 | if hidden == True and playing == True: 105 | # Don't show first hidden card 106 | starting_card = 1 107 | else: 108 | starting_card = 0 109 | for x in range(starting_card, len(self.cards)): 110 | self.cards[x].draw() 111 | 112 | # Creating Class Deck 113 | 114 | class Deck: 115 | 116 | def __init__(self): 117 | '''Creating a deck in order''' 118 | self.deck = [] 119 | for suit in suits: 120 | for rank in ranking: 121 | self.deck.append(Card(suit,rank)) 122 | 123 | def shuffle(self): 124 | '''Shuffles the deck, using python's built-in random library''' 125 | random.shuffle(self.deck) 126 | 127 | def deal(self): 128 | '''Grabbing the first item in the deck''' 129 | single_card = self.deck.pop() 130 | return single_card 131 | 132 | def __str__(self): 133 | deck_comp = " " 134 | for card in self.cards: 135 | deck_comp += " " + deck_comp.__str__() 136 | 137 | return "The deck has " + deck_comp 138 | 139 | # End of Classes 140 | 141 | # First Bet 142 | 143 | def make_bet(): 144 | '''Ask the player for the bet amount and ''' 145 | 146 | global bet 147 | bet = 0 148 | 149 | print 'What amount of chips would you like to bet? (Please enter whole integer) ' 150 | 151 | # While loop to keep asking for the bet 152 | while bet == 0: 153 | # Using bet_comp as a checker 154 | bet_comp = raw_input() 155 | bet_comp = int(bet_comp) 156 | 157 | # Check to make sure the bet is within the remaining amount of chips left 158 | if bet_comp >= 1 and bet_comp <= chip_pool: 159 | bet = bet_comp 160 | else: 161 | print "Invalid bet, you only have " + str(chip_pool) + " remaining" 162 | 163 | def deal_cards(): 164 | '''This function deals out cards and sets up round''' 165 | 166 | # Set up all global variables 167 | global result, playing, deck, player_hand, dealer_hand, chip_pool, bet 168 | 169 | # Creating a deck 170 | deck = Deck() 171 | 172 | # Shuffle it 173 | deck.shuffle() 174 | 175 | # Set up the bet 176 | make_bet() 177 | 178 | # Set up both player and dealer hands 179 | player_hand = Hand() 180 | dealer_hand = Hand() 181 | 182 | # Deal out initial cards 183 | player_hand.card_add(deck.deal()) 184 | player_hand.card_add(deck.deal()) 185 | 186 | result = "Hit or Stand? Press h for hit or s for stand: " 187 | 188 | if playing == True: 189 | print 'Fold, Sorry' 190 | chip_pool -= bet 191 | 192 | # Set up to know currently playing hand 193 | playing = True 194 | game_step() 195 | 196 | 197 | # Hit Function 198 | 199 | def hit(): 200 | '''Implementing the hit button''' 201 | 202 | global playing, chip_pool, deck, player_hand, dealer_hand, result, bet 203 | 204 | # If hand is in play add card 205 | if playing: 206 | if player_hand.calc_val() <= 21: 207 | player_hand.card_add(deck.deal()) 208 | 209 | print "Player hand is %s" %player_hand 210 | 211 | if player_hand.calc_val() > 21: 212 | result = 'Busted!' + restart_phrase 213 | 214 | chip_pool -= bet 215 | playing = False 216 | 217 | else: 218 | result = "Sorry, can't hit" + restart_phrase 219 | 220 | game_step() 221 | 222 | # Stand Function 223 | 224 | def stand(): 225 | global playing, chip_pool, deck, player_hand, dealer_hand, result, bet 226 | 227 | '''This function plays the dealers hand, since stand was chosen''' 228 | 229 | if playing == False: 230 | if player_hand.calc_val() > 0: 231 | result = "Sorry, you can't stand!" 232 | 233 | # Going through all other possible options 234 | else: 235 | 236 | # Sfot 17 Rule 237 | while dealer_hand.calc_val() < 17: 238 | dealer_hand.card_add(deck.deal()) 239 | 240 | # Dealer Busts 241 | if dealer_hand.calc_val() > 21: 242 | result = 'Dealer busts! You win! ' + restart_phrase 243 | 244 | chip_pool += bet 245 | playing = False 246 | 247 | # Player has better hand than dealer 248 | elif dealer_hand.calc_val() < player_hand.calc_val(): 249 | result = 'You beat the dealer, you win! ' + restart_phrase 250 | chip_pool += bet 251 | playing = False 252 | 253 | # Push 254 | elif dealer_hand.calc_val == player_hand.calc_val(): 255 | result = 'Tied up, push!' + restart_phrase 256 | playing = False 257 | 258 | # Dealer beats player 259 | else: 260 | result = 'Dealer Wins! ' + restart_phrase 261 | chip_pool -= bet 262 | playing = False 263 | 264 | game_step() 265 | 266 | # Function to print results and ask user for next step 267 | 268 | def game_step(): 269 | '''Function to print game step/status on output''' 270 | 271 | # Display Player Hand 272 | print "" 273 | print ('Player Hand is: '),player_hand.draw(hidden = False) 274 | 275 | print '' 276 | print 'Player hand total is: ' +str(player_hand.calc_val()) 277 | 278 | # Display Dealer Hand 279 | print '' 280 | print('Dealer Hand is: '), dealer_hand.draw(hidden = True) 281 | 282 | # If game round is over 283 | if playing == False: 284 | print " --- for a total of " + str(dealer_hand.calc_val()) 285 | print "Chip Total: " +str(chip_pool) 286 | 287 | # Otherwise, don't know the second card yet 288 | else: 289 | print " with another card hidden upside down" 290 | 291 | # Print result of hit or stand 292 | print '' 293 | print result 294 | 295 | player_input() 296 | 297 | 298 | # Function to exit the game 299 | 300 | def game_exit(): 301 | print 'Thanks for playing!' 302 | exit() 303 | 304 | # Function to read user input 305 | 306 | def player_input(): 307 | '''Read user input, lower case it jsuts to be safe''' 308 | 309 | plin = raw_input().lower() 310 | 311 | if plin == 'h': 312 | hit() 313 | elif plin == 's': 314 | stand() 315 | elif plin == 'd': 316 | deal_cards() 317 | elif plin == 'q': 318 | game_exit() 319 | else: 320 | print "Invalid Input. Enter h, s, d, or q: " 321 | player_input() 322 | 323 | # Intro to game 324 | 325 | def intro(): 326 | statement = '''Welcome to BlackJack! Get as close to 21 as you can without getting over! 327 | Dealer hits until she reaches 17. Aces count as 1 or 11. Card output goes a letter followed by a number of face notation. ''' 328 | 329 | print statement 330 | print '' 331 | 332 | # Playing the Game 333 | 334 | '''The following code will initiate the game! 335 | (Note: Need to Run a 11 Cells)''' 336 | 337 | # Create a Deck 338 | deck = Deck() 339 | 340 | # Shuffle it 341 | deck.shuffle() 342 | 343 | # Create player and dealer hands 344 | print '' 345 | player_hand = Hand() 346 | print '' 347 | deal_hand = Hand() 348 | 349 | # Print the intro 350 | intro() 351 | 352 | # Deal out the cards and start the game! 353 | deal_cards() -------------------------------------------------------------------------------- /caesercipher/caesarCipherAlgorithm.py: -------------------------------------------------------------------------------- 1 | #!/Users/Utsav/downloads/udemy python 2 | 3 | # For using the same code in either Python 2 or 3 4 | # from __future__ import print_function 5 | 6 | """ milestone_project_1.py: Tic Tac Toe Game """ 7 | 8 | __author__ = "Utsav Shah" 9 | __copyright__ = "Copyright 2017, The Austin Side Project" 10 | __credits__ = ["Hustle"] 11 | __license__ = "UTS" 12 | __version__ = "1.1.0" 13 | __maintainer__ = "Utsav Shah" 14 | __email__ = "utsavshah507@gmail.com" 15 | __status__ = "Productive" 16 | 17 | ## Milestone Project 2 18 | 19 | # Importing libraries -- used for shuffling cards 20 | import random 21 | 22 | # Boolean type to know whether play is in hand 23 | playing = False 24 | 25 | # Amount for buy-in 26 | chip_pool = 100 27 | # raw_input('Enter the amount for buy-in: ') 28 | print 'Your buy-in amount is: ',chip_pool 29 | 30 | bet = 1 31 | 32 | restart_phrase = "Press d to deal the cards again, or press q to quit." 33 | 34 | # Hearts, Diamonds, Clubs, Spades 35 | suits = ('H','D','S','C') 36 | 37 | # Possible Card Ranks 38 | ranking = ('A','2','3','4','5','6','7','8','9','10','J','Q','K') 39 | 40 | # Point Val Dict (Dual existence of Ace is defined later) 41 | card_val = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, '10':10, 'J':10, 'Q':10, 'K':10} 42 | 43 | # Creating Card Class 44 | 45 | class Card: 46 | 47 | def __init__(self, suit, rank): 48 | self.suit = suit 49 | self.rank = rank 50 | 51 | def __str__(self): 52 | return self.suit + self.rank 53 | 54 | def grab_suit(self): 55 | return self.suit 56 | 57 | def grab_rank(rank): 58 | return self.rank 59 | 60 | def draw(self): 61 | print (self.suit + self.rank) 62 | 63 | # Creating Hand Class 64 | # Gives dual existence to Ace 65 | 66 | class Hand: 67 | 68 | def __init__(self): 69 | self.cards = [] 70 | self.value = 0 71 | 72 | # Aces can be 1 0r 11 as defined below 73 | self.ace = False 74 | 75 | def __str__(self): 76 | '''Return a string of current hand composition''' 77 | hand_comp = "" 78 | 79 | # List Comprehension 80 | for card in self.cards: 81 | card_name = card.__str__() 82 | hand_comp += " " + card_name 83 | 84 | return 'The hand has %s' %hand_comp 85 | 86 | def card_add(self,card): 87 | '''Add another card to the hand''' 88 | self.cards.append(card) 89 | 90 | # Checking for Aces 91 | if card.rank == 'A': 92 | self.ace = True 93 | self.value += card_val[card.rank] 94 | 95 | def calc_val(self): 96 | '''Calculating value of hand, making aces = 1 if they don't bust the hand''' 97 | 98 | if (self.ace == True and self.value < 12): 99 | return self.value + 10 100 | else: 101 | return self.value 102 | 103 | def draw(self, hidden): 104 | if hidden == True and playing == True: 105 | # Don't show first hidden card 106 | starting_card = 1 107 | else: 108 | starting_card = 0 109 | for x in range(starting_card, len(self.cards)): 110 | self.cards[x].draw() 111 | 112 | # Creating Class Deck 113 | 114 | class Deck: 115 | 116 | def __init__(self): 117 | '''Creating a deck in order''' 118 | self.deck = [] 119 | for suit in suits: 120 | for rank in ranking: 121 | self.deck.append(Card(suit,rank)) 122 | 123 | def shuffle(self): 124 | '''Shuffles the deck, using python's built-in random library''' 125 | random.shuffle(self.deck) 126 | 127 | def deal(self): 128 | '''Grabbing the first item in the deck''' 129 | single_card = self.deck.pop() 130 | return single_card 131 | 132 | def __str__(self): 133 | deck_comp = " " 134 | for card in self.cards: 135 | deck_comp += " " + deck_comp.__str__() 136 | 137 | return "The deck has " + deck_comp 138 | 139 | # End of Classes 140 | 141 | # First Bet 142 | 143 | def make_bet(): 144 | '''Ask the player for the bet amount and ''' 145 | 146 | global bet 147 | bet = 0 148 | 149 | print 'What amount of chips would you like to bet? (Please enter whole integer) ' 150 | 151 | # While loop to keep asking for the bet 152 | while bet == 0: 153 | # Using bet_comp as a checker 154 | bet_comp = raw_input() 155 | bet_comp = int(bet_comp) 156 | 157 | # Check to make sure the bet is within the remaining amount of chips left 158 | if bet_comp >= 1 and bet_comp <= chip_pool: 159 | bet = bet_comp 160 | else: 161 | print "Invalid bet, you only have " + str(chip_pool) + " remaining" 162 | 163 | def deal_cards(): 164 | '''This function deals out cards and sets up round''' 165 | 166 | # Set up all global variables 167 | global result, playing, deck, player_hand, dealer_hand, chip_pool, bet 168 | 169 | # Creating a deck 170 | deck = Deck() 171 | 172 | # Shuffle it 173 | deck.shuffle() 174 | 175 | # Set up the bet 176 | make_bet() 177 | 178 | # Set up both player and dealer hands 179 | player_hand = Hand() 180 | dealer_hand = Hand() 181 | 182 | # Deal out initial cards 183 | player_hand.card_add(deck.deal()) 184 | player_hand.card_add(deck.deal()) 185 | 186 | result = "Hit or Stand? Press h for hit or s for stand: " 187 | 188 | if playing == True: 189 | print 'Fold, Sorry' 190 | chip_pool -= bet 191 | 192 | # Set up to know currently playing hand 193 | playing = True 194 | game_step() 195 | 196 | 197 | # Hit Function 198 | 199 | def hit(): 200 | '''Implementing the hit button''' 201 | 202 | global playing, chip_pool, deck, player_hand, dealer_hand, result, bet 203 | 204 | # If hand is in play add card 205 | if playing: 206 | if player_hand.calc_val() <= 21: 207 | player_hand.card_add(deck.deal()) 208 | 209 | print "Player hand is %s" %player_hand 210 | 211 | if player_hand.calc_val() > 21: 212 | result = 'Busted!' + restart_phrase 213 | 214 | chip_pool -= bet 215 | playing = False 216 | 217 | else: 218 | result = "Sorry, can't hit" + restart_phrase 219 | 220 | game_step() 221 | 222 | # Stand Function 223 | 224 | def stand(): 225 | global playing, chip_pool, deck, player_hand, dealer_hand, result, bet 226 | 227 | '''This function plays the dealers hand, since stand was chosen''' 228 | 229 | if playing == False: 230 | if player_hand.calc_val() > 0: 231 | result = "Sorry, you can't stand!" 232 | 233 | # Going through all other possible options 234 | else: 235 | 236 | # Sfot 17 Rule 237 | while dealer_hand.calc_val() < 17: 238 | dealer_hand.card_add(deck.deal()) 239 | 240 | # Dealer Busts 241 | if dealer_hand.calc_val() > 21: 242 | result = 'Dealer busts! You win! ' + restart_phrase 243 | 244 | chip_pool += bet 245 | playing = False 246 | 247 | # Player has better hand than dealer 248 | elif dealer_hand.calc_val() < player_hand.calc_val(): 249 | result = 'You beat the dealer, you win! ' + restart_phrase 250 | chip_pool += bet 251 | playing = False 252 | 253 | # Push 254 | elif dealer_hand.calc_val() == player_hand.calc_val(): 255 | result = 'Tied up, push!' + restart_phrase 256 | playing = False 257 | 258 | # Dealer beats player 259 | else: 260 | result = 'Dealer Wins! ' + restart_phrase 261 | chip_pool -= bet 262 | playing = False 263 | 264 | game_step() 265 | 266 | # Function to print results and ask user for next step 267 | 268 | def game_step(): 269 | '''Function to print game step/status on output''' 270 | 271 | # Display Player Hand 272 | print "" 273 | print ('Player Hand is: '),player_hand.draw(hidden = False) 274 | 275 | print '' 276 | print 'Player hand total is: ' +str(player_hand.calc_val()) 277 | 278 | # Display Dealer Hand 279 | print '' 280 | print('Dealer Hand is: '), dealer_hand.draw(hidden = True) 281 | 282 | # If game round is over 283 | if playing == False: 284 | print " --- for a total of " + str(dealer_hand.calc_val()) 285 | print "Chip Total: " +str(chip_pool) 286 | 287 | # Otherwise, don't know the second card yet 288 | else: 289 | print " with another card hidden upside down" 290 | 291 | # Print result of hit or stand 292 | print '' 293 | print result 294 | 295 | player_input() 296 | 297 | 298 | # Function to exit the game 299 | 300 | def game_exit(): 301 | print 'Thanks for playing!' 302 | exit() 303 | 304 | # Function to read user input 305 | 306 | def player_input(): 307 | '''Read user input, lower case it jsuts to be safe''' 308 | 309 | plin = raw_input().lower() 310 | 311 | if plin == 'h': 312 | hit() 313 | elif plin == 's': 314 | stand() 315 | elif plin == 'd': 316 | deal_cards() 317 | elif plin == 'q': 318 | game_exit() 319 | else: 320 | print "Invalid Input. Enter h, s, d, or q: " 321 | player_input() 322 | 323 | # Intro to game 324 | 325 | def intro(): 326 | statement = '''Welcome to BlackJack! Get as close to 21 as you can without getting over! 327 | Dealer hits until she reaches 17. Aces count as 1 or 11. Card output goes a letter followed by a number of face notation. ''' 328 | 329 | print statement 330 | print '' 331 | 332 | # Playing the Game 333 | 334 | '''The following code will initiate the game! 335 | (Note: Need to Run a 11 Cells)''' 336 | 337 | # Create a Deck 338 | deck = Deck() 339 | 340 | # Shuffle it 341 | deck.shuffle() 342 | 343 | # Create player and dealer hands 344 | print '' 345 | player_hand = Hand() 346 | print '' 347 | deal_hand = Hand() 348 | 349 | # Print the intro 350 | intro() 351 | 352 | # Deal out the cards and start the game! 353 | deal_cards() 354 | -------------------------------------------------------------------------------- /closestpair/closestPairProblem.py: -------------------------------------------------------------------------------- 1 | #!/Users/Utsav/downloads/udemy python 2 | 3 | # For using the same code in either Python 2 or 3 4 | from __future__ import print_function 5 | 6 | """ milestone_project_1.py: Tic Tac Toe Game """ 7 | 8 | __author__ = "Utsav Shah" 9 | __copyright__ = "Copyright 2017, The Austin Side Project" 10 | __credits__ = ["Hustle"] 11 | __license__ = "UTS" 12 | __version__ = "1.0.0" 13 | __maintainer__ = "Utsav Shah" 14 | __email__ = "utsavshah507@gmail.com" 15 | __status__ = "Productive" 16 | 17 | ## Milestone Project 1 18 | 19 | # Importing libraries 20 | from IPython.display import clear_output 21 | 22 | # Function to display board 23 | def display_board(board): 24 | 25 | clear_output() 26 | 27 | print('Reference Table for numbers to enter as positions: ') 28 | print(' | | ') 29 | print(' 7 | 8 | 9 ') 30 | print(' | | ') 31 | print('-----------') 32 | print(' | | ') 33 | print(' 4 | 5 | 6 ') 34 | print(' | | ') 35 | print('-----------') 36 | print(' | | ') 37 | print(' 1 | 2 | 3 ') 38 | print(' | | ') 39 | 40 | print('') 41 | 42 | print(' | | ') 43 | print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) 44 | print(' | | ') 45 | print('-----------') 46 | print(' | | ') 47 | print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) 48 | print(' | | ') 49 | print('-----------') 50 | print(' | | ') 51 | print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) 52 | print(' | | ') 53 | 54 | # Function to take player input 55 | def player_input(): 56 | 57 | marker = '' 58 | 59 | while not (marker == 'X' or marker == 'x' or marker == 'O' or marker == 'o'): 60 | marker = raw_input('Player 1: Do you want to be X or O? ').upper() 61 | 62 | if marker == 'X': 63 | return('X','O') 64 | else: 65 | return('O','X') 66 | 67 | # Function to place marker on desired mark 68 | def place_marker(board, marker, position): 69 | board[position] = marker 70 | 71 | # Function to check if player has won 72 | def win_check(board, mark): 73 | return ((board[1] == mark and board[2] == mark and board[3] == mark) or # Horizontal Bottom 74 | (board[4] == mark and board[5] == mark and board[6] == mark) or # Horizontal Middle 75 | (board[7] == mark and board[8] == mark and board[9] == mark) or # Horizontal Top 76 | (board[7] == mark and board[4] == mark and board[1] == mark) or # Vertical Left 77 | (board[8] == mark and board[5] == mark and board[2] == mark) or # Vertical Middle 78 | (board[9] == mark and board[6] == mark and board[3] == mark) or # Vertical Right 79 | (board[7] == mark and board[5] == mark and board[3] == mark) or # Diagonal Left to Right Bottom 80 | (board[9] == mark and board[5] == mark and board[1] == mark)) # Diagonal Right to Left Bottom 81 | 82 | # Function to decide which player goes first 83 | import random 84 | def choose_first(): 85 | if random.randint(0,1) == 0: 86 | return 'Player 2' 87 | else: 88 | return 'Player 1' 89 | 90 | # Function to check whether space on board is free 91 | def space_check(board, position): 92 | return board[position] == ' ' 93 | 94 | # Function to check if entire board is full 95 | def full_board_check(board): 96 | for i in range(1,10): 97 | if space_check(board, i): 98 | return False 99 | return True 100 | 101 | # Function to ask player's next position (1-9) 102 | def player_choice(board): 103 | position = ' ' 104 | 105 | while position not in '1 2 3 4 5 6 7 8 9'.split() or not space_check(board, int(position)): 106 | position = raw_input('Choose your next position (1 -9): ') 107 | return int(position) 108 | 109 | # Function to ask players if they want to play again 110 | def replay(): 111 | return raw_input('Do you want to play again? Enter Yes or No: ').lower().startswith('y') 112 | 113 | # Main part to run the game 114 | 115 | print('Welcome to Tic Tac Toe!') 116 | 117 | while True: 118 | 119 | # Reset the board 120 | theBoard = [' '] * 10 121 | player1_marker, player2_marker = player_input() 122 | turn = choose_first() 123 | print(turn + ' will go first') 124 | game_on = True 125 | 126 | while game_on: 127 | if turn == 'Player 1': 128 | # Player 1's turn 129 | 130 | display_board(theBoard) 131 | position = player_choice(theBoard) 132 | place_marker(theBoard, player1_marker, position) 133 | 134 | if win_check(theBoard, player1_marker): 135 | display_board(theBoard) 136 | print('Congratulations, PLAYER 1, you have won the game!') 137 | game_on = False 138 | 139 | else: 140 | if full_board_check(theBoard): 141 | display_board(theBoard) 142 | print('The game is a draw!') 143 | break 144 | else: 145 | turn = 'Player 2' 146 | 147 | else: 148 | # Player 2's turn 149 | 150 | display_board(theBoard) 151 | position = player_choice(theBoard) 152 | place_marker(theBoard, player2_marker, position) 153 | 154 | if win_check(theBoard, player2_marker): 155 | display_board(theBoard) 156 | print('Congratulations, PLAYER 2, you have won the game!') 157 | game_on = False 158 | 159 | else: 160 | if full_board_check(theBoard): 161 | display_board(theBoard) 162 | print('The game is a draw!') 163 | break 164 | else: 165 | turn = 'Player 1' 166 | if not replay(): 167 | break -------------------------------------------------------------------------------- /musicplayer/pythonMusicPlayer.py: -------------------------------------------------------------------------------- 1 | """ capstone_project_3.py: Python Music Player - using Tkinter and pygame """ 2 | 3 | __author__ = "Utsav Shah" 4 | __copyright__ = "Copyright 2017, The Austin Side Project" 5 | __credits__ = ["Hustle"] 6 | __license__ = "UTS" 7 | __version__ = "1.4.0" 8 | __maintainer__ = "Utsav Shah" 9 | __email__ = "utsavshah507@gmail.com" 10 | __status__ = "Productive" 11 | 12 | ''' 13 | This python program is used to implement a Python 14 | Music Player while browsing songs on one's computer 15 | ''' 16 | 17 | from Tkinter import * 18 | import tkFileDialog as tk 19 | import tkMessageBox as tk2 20 | import pygame 21 | 22 | playlist = [] 23 | 24 | class Application(Frame): 25 | 26 | def __init__(self,master): 27 | Frame.__init__(self, master) 28 | # super(Application,self).__init__(master) 29 | 30 | # self.create_widgets() 31 | self.playlistbox = Listbox(self, width = 40, height = 10, selectmode = SINGLE) #TODO: ---> BROWSE, MULTIPLE, EXTENDED (p.379) 32 | for song in playlist: 33 | self.playlistbox.insert(END, song) 34 | 35 | self.grid(rowspan=5, columnspan=4) 36 | self.playlistbox.grid(row = 1) 37 | self.playButton = Button(self, text = 'Play', command = self.play) 38 | self.loopButton = Button(self, text = 'Loop', command = self.loop) 39 | self.addButton = Button(self, text = 'Add', command = self.add) 40 | self.playButton.grid(row=4, column = 0) 41 | self.loopButton.grid(row=4, column = 1) 42 | self.addButton.grid(row=4, column = 2) 43 | self.pack() 44 | 45 | # Pygame Initialize 46 | pygame.init() 47 | 48 | def play(self): 49 | if(len(playlist) == 0): 50 | tk2.showinfo('Notice', 'No songs in your playlist!\nClick Add to add songs.') 51 | else: 52 | pygame.mixer.music.stop() 53 | selectedSongs = self.playlistbox.curselection() 54 | global playlistbox 55 | playIt = playlist[int(selectedSongs[0])] 56 | pygame.mixer.music.load(playIt) 57 | pygame.mixer.music.play(0, 0.0) 58 | 59 | def loop(self): 60 | pygame.mixer.music.stop() 61 | pygame.mixer.music.play(-1,0.0) 62 | 63 | def add(self): 64 | # Directory to folder containing songs desired to play with this Python Music Player 65 | file = tk.askopenfilenames(initialdir='/Users/Utsav/Movies/Music/January 2016 Playlist') 66 | songsTuple = root.splitlist(file) # Turn user's opened filenames into tuple 67 | songsList = list(songsTuple) # Convert to list 68 | # Add the full filename of songto playlist list, and a shortened version to the listBox 69 | for song in songsList: 70 | playlist.append(song); 71 | tempArray = song.split('/') 72 | songShort = tempArray[len(tempArray)-1] 73 | self.playlistbox.insert(END, songShort) 74 | 75 | root = Tk() 76 | root.title('Python Music PLAYer') 77 | root.geometry('500x200') 78 | app = Application(root) 79 | app.mainloop() 80 | -------------------------------------------------------------------------------- /tictactoe/ticTacToe.py: -------------------------------------------------------------------------------- 1 | #!/Users/Utsav/downloads/udemy python 2 | 3 | # For using the same code in either Python 2 or 3 4 | from __future__ import print_function 5 | 6 | """ milestone_project_1.py: Tic Tac Toe Game """ 7 | 8 | __author__ = "Utsav Shah" 9 | __copyright__ = "Copyright 2017, The Austin Side Project" 10 | __credits__ = ["Hustle"] 11 | __license__ = "UTS" 12 | __version__ = "1.0.0" 13 | __maintainer__ = "Utsav Shah" 14 | __email__ = "utsavshah507@gmail.com" 15 | __status__ = "Productive" 16 | 17 | ## Milestone Project 1 18 | 19 | # Importing libraries 20 | from IPython.display import clear_output 21 | 22 | # Function to display board 23 | def display_board(board): 24 | 25 | clear_output() 26 | 27 | print('Reference Table for numbers to enter as positions: ') 28 | print(' | | ') 29 | print(' 7 | 8 | 9 ') 30 | print(' | | ') 31 | print('-----------') 32 | print(' | | ') 33 | print(' 4 | 5 | 6 ') 34 | print(' | | ') 35 | print('-----------') 36 | print(' | | ') 37 | print(' 1 | 2 | 3 ') 38 | print(' | | ') 39 | 40 | print('') 41 | 42 | print(' | | ') 43 | print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) 44 | print(' | | ') 45 | print('-----------') 46 | print(' | | ') 47 | print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) 48 | print(' | | ') 49 | print('-----------') 50 | print(' | | ') 51 | print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) 52 | print(' | | ') 53 | 54 | # Function to take player input 55 | def player_input(): 56 | 57 | marker = '' 58 | 59 | while not (marker == 'X' or marker == 'x' or marker == 'O' or marker == 'o'): 60 | marker = raw_input('Player 1: Do you want to be X or O? ').upper() 61 | 62 | if marker == 'X': 63 | return('X','O') 64 | else: 65 | return('O','X') 66 | 67 | # Function to place marker on desired mark 68 | def place_marker(board, marker, position): 69 | board[position] = marker 70 | 71 | # Function to check if player has won 72 | def win_check(board, mark): 73 | return ((board[1] == mark and board[2] == mark and board[3] == mark) or # Horizontal Bottom 74 | (board[4] == mark and board[5] == mark and board[6] == mark) or # Horizontal Middle 75 | (board[7] == mark and board[8] == mark and board[9] == mark) or # Horizontal Top 76 | (board[7] == mark and board[4] == mark and board[1] == mark) or # Vertical Left 77 | (board[8] == mark and board[5] == mark and board[2] == mark) or # Vertical Middle 78 | (board[9] == mark and board[6] == mark and board[3] == mark) or # Vertical Right 79 | (board[7] == mark and board[5] == mark and board[3] == mark) or # Diagonal Left to Right Bottom 80 | (board[9] == mark and board[5] == mark and board[1] == mark)) # Diagonal Right to Left Bottom 81 | 82 | # Function to decide which player goes first 83 | import random 84 | def choose_first(): 85 | if random.randint(0,1) == 0: 86 | return 'Player 2' 87 | else: 88 | return 'Player 1' 89 | 90 | # Function to check whether space on board is free 91 | def space_check(board, position): 92 | return board[position] == ' ' 93 | 94 | # Function to check if entire board is full 95 | def full_board_check(board): 96 | for i in range(1,10): 97 | if space_check(board, i): 98 | return False 99 | return True 100 | 101 | # Function to ask player's next position (1-9) 102 | def player_choice(board): 103 | position = ' ' 104 | 105 | while position not in '1 2 3 4 5 6 7 8 9'.split() or not space_check(board, int(position)): 106 | position = raw_input('Choose your next position (1 -9): ') 107 | return int(position) 108 | 109 | # Function to ask players if they want to play again 110 | def replay(): 111 | return raw_input('Do you want to play again? Enter Yes or No: ').lower().startswith('y') 112 | 113 | # Main part to run the game 114 | 115 | print('Welcome to Tic Tac Toe!') 116 | 117 | while True: 118 | 119 | # Reset the board 120 | theBoard = [' '] * 10 121 | player1_marker, player2_marker = player_input() 122 | turn = choose_first() 123 | print(turn + ' will go first') 124 | game_on = True 125 | 126 | while game_on: 127 | if turn == 'Player 1': 128 | # Player 1's turn 129 | 130 | display_board(theBoard) 131 | position = player_choice(theBoard) 132 | place_marker(theBoard, player1_marker, position) 133 | 134 | if win_check(theBoard, player1_marker): 135 | display_board(theBoard) 136 | print('Congratulations, PLAYER 1, you have won the game!') 137 | game_on = False 138 | 139 | else: 140 | if full_board_check(theBoard): 141 | display_board(theBoard) 142 | print('The game is a draw!') 143 | break 144 | else: 145 | turn = 'Player 2' 146 | 147 | else: 148 | # Player 2's turn 149 | 150 | display_board(theBoard) 151 | position = player_choice(theBoard) 152 | place_marker(theBoard, player2_marker, position) 153 | 154 | if win_check(theBoard, player2_marker): 155 | display_board(theBoard) 156 | print('Congratulations, PLAYER 2, you have won the game!') 157 | game_on = False 158 | 159 | else: 160 | if full_board_check(theBoard): 161 | display_board(theBoard) 162 | print('The game is a draw!') 163 | break 164 | else: 165 | turn = 'Player 1' 166 | if not replay(): 167 | break --------------------------------------------------------------------------------