|
| 1 | +class Player: |
| 2 | + ''' |
| 3 | + ' A class to represent each player |
| 4 | + ' Attributes: name and color |
| 5 | + ''' |
| 6 | + |
| 7 | +class Game: |
| 8 | + ''' |
| 9 | + ' A connect four game object. |
| 10 | + ' Attributes: game board (7x6 2D nested list) |
| 11 | + ' Methods: |
| 12 | + ' get_height(position) returns int of how many pieces occupy a column |
| 13 | + ' move(player,position) adds a player token to a column after figuring out the current height of the column |
| 14 | + ' calc_winner(): returns True if four in a row are found |
| 15 | + ' is_full(): returns true if all board positions are occupied |
| 16 | + ' is_game_over(): returns true if the game is over (a winnder is found or the board is full) |
| 17 | + ' play_turn(): function that plays one turn |
| 18 | + ' play(): function that plays whole game, given two player |
| 19 | + ' recursive_chip_checker(): recurisvely checks in all directions to find if theres a connect 4 |
| 20 | + ''' |
| 21 | + |
| 22 | +if __name__ == '__main__': |
| 23 | + # if this module is run directly, this code will run |
| 24 | + # if the module is imported into another, it won't run |
| 25 | + |
| 26 | + game = Game() |
| 27 | + with open('1 Python/docs/class_code_examples/connect-four-moves.txt', 'r') as moves: |
| 28 | + |
| 29 | + for move_num, line in enumerate(moves): |
| 30 | + print(line) |
| 31 | + col_to_play = int(line.strip()) - 1 |
| 32 | + player = 'Y' if move_num % 2 != 0 else 'R' |
| 33 | + game.play_turn(player, col_to_play) |
| 34 | + # OPTIONAL/BONUS: print board after each turn |
| 35 | + game.is_game_over() # could be called w/in play_turn, or in a different order |
| 36 | + |
0 commit comments