##### Connect 4 assigment
 Connect 4 assigment deanhystad Da Bishop Posts: 1,974 Threads: 11 Joined: Feb 2020 Reputation: Jan-22-2021, 05:06 AM There are a few things about game play that, if taken into account, make writing code for this game a lot easer. 1. As far as game play is concerned, there is only one player. The current player. Only the current player can modify the board and only the current player can win the game. 2. Players take turns being the current player. You don't have to write separate code for player 1 and player 2. The only difference between these players is the marker they use when making their play. 3. The last play is the only play that can result in a win. Any streak of 4 markers will pass through the last marker placed on the board. ```"""Connect4 terminal game Object of the game is to get 4 player makers in a row; horizontally, verically or diagonally. Two players take turns placing their marker on the board. A player palces the marker by entering the number appearing below the column in the game board. The marker falls to the bottom open spot. Each time the player places a marker the game checks for a winner. If the last play did not result in a win, the board is redrawn to display the new state and the other player takes their turn. """ ROWS = 6 # Size of the playing board COLUMNS = 7 BOARD = [[2]*ROWS for i in range(COLUMNS)] MARKERS = ['X', 'O', ' '] # Player markers for the board def draw_board(): """Draw the board using X and O to mark players 0 and 1 markers""" for row in range(ROWS-1, -1, -1): print(' '.join([MARKERS[BOARD[col][row]] for col in range(COLUMNS)])) print(' '.join([str(i) for i in range(1, COLUMNS+1)])) def in_a_row(col, row, colx, rowx, count=None): """Count number of consecutive matching markers starting at col, row and moving in direction colx, rowx """ marker = BOARD[col][row] if count is None: # Start by scanning in the opposite direction count = in_a_row(col, row, -colx, -rowx, 1) col += colx row += rowx # Continue counting until you find an empty slot, a different marker, # or run into an edge. while 0 <= col < COLUMNS and 0 <= row < ROWS and BOARD[col][row] == marker: count += 1 col += colx row += rowx return count def winner(spot): """Does the latest move connect 4 or more?""" return in_a_row(*spot, 0, 1) > 3 or \ in_a_row(*spot, 1, 0) > 3 or \ in_a_row(*spot, 1, 1) > 3 or \ in_a_row(*spot, 1, -1) > 3 def drop(col, player): """Drop player's marker in column. Marker falls to the bottom open row. return resting spot (column, row). """ for row in range(ROWS): if BOARD[col][row] == 2: # Empty BOARD[col][row] = player return(col, row) return False def move(player): """Have player add a marker to the board. Return position of the marker (col, row)""" draw_board() while True: try: col = int(input(f'{MARKERS[player]}: '))-1 except ValueError: print(f'Enter number [1:{COLUMNS}]') else: if spot := drop(col, player): return spot print(f'{col} is full') for i in range(ROWS * COLUMNS): # Max number of moves PLAYER = i % 2 if winner(move(PLAYER)): draw_board() print(f'{MARKERS[PLAYER]} wins!') break else: draw_board() print('Game ends in a tie')``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Sorting list - Homework assigment ranbarr 1 56 Yesterday, 04:45 PM Last Post: Yoriz Python homework assigment makisha 3 1,175 Feb-28-2019, 10:21 PM Last Post: Yoriz Display school assigment Vittya 2 1,739 Nov-09-2017, 02:13 PM Last Post: sparkz_alot

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020