connect 4.py - Python 3.7.3(v3.7.3:ef4ec6ed12...

This preview shows page 1 - 2 out of 3 pages.

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license()" for more information. >>> import numpy as np import pygame import sys import math BLUE = (0,0,255) BLACK = (0,0,0) RED = (255,0,0) YELLOW = (255,255,0) ROW_COUNT = 6 COLUMN_COUNT = 7 def create_board(): board = np.zeros((ROW_COUNT,COLUMN_COUNT)) return board def drop_piece(board, row, col, piece): board[row][col] = piece def is_valid_location(board, col): return board[ROW_COUNT-1][col] == 0 def get_next_open_row(board, col): for r in range(ROW_COUNT): if board[r][col] == 0: return r def print_board(board): print(np.flip(board, 0)) def winning_move(board, piece): # Check horizontal locations for win for c in range(COLUMN_COUNT-3): for r in range(ROW_COUNT): if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece: return True # Check vertical locations for win for c in range(COLUMN_COUNT): for r in range(ROW_COUNT-3): if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece: return True # Check positively sloped diaganols

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture