Connect 4.py - import numpy as np import pygame import sys...

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

import numpy as np import pygame import sys import math from graphics import * win = GraphWin("Hello") Text(Point(50,50),"Click me at the end").draw(win) 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 for c in range(COLUMN_COUNT - 3):

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture