|
| 1 | +import numpy as np |
| 2 | +import math |
| 3 | +import pygame |
| 4 | +import sys |
| 5 | + |
| 6 | +NB_COL = 7 |
| 7 | +NB_ROW = 6 |
| 8 | +BLUE = (0, 0, 255) # RGB value |
| 9 | +BLACK = (0, 0, 0) |
| 10 | +RED = (255, 0, 0) |
| 11 | +YELLOW = (255, 255, 0) |
| 12 | + |
| 13 | + |
| 14 | +def make_board(): |
| 15 | + board = np.zeros((NB_ROW, NB_COL)) |
| 16 | + return board |
| 17 | + |
| 18 | + |
| 19 | +def drop_piece(board, row, col, piece): |
| 20 | + board[row][col] = piece |
| 21 | + |
| 22 | + |
| 23 | +def is_valid_location(board, col): |
| 24 | + return board[NB_ROW-1][col] == 0 |
| 25 | + |
| 26 | + |
| 27 | +def get_next_open_row(board, col): |
| 28 | + for r in range(NB_ROW): |
| 29 | + if board[r][col] == 0: |
| 30 | + return r |
| 31 | + |
| 32 | + |
| 33 | +def print_board(board): |
| 34 | + print(np.flip(board, 0)) |
| 35 | + |
| 36 | + |
| 37 | +def winning_move(baord, piece): |
| 38 | + # Check hozizontal locations for win |
| 39 | + for c in range(NB_COL-3): |
| 40 | + for r in range(NB_ROW): |
| 41 | + if ( |
| 42 | + board[r][c] == piece and board[r][c+1] == piece and |
| 43 | + board[r][c+2] == piece and board[r][c+3] == piece |
| 44 | + ): |
| 45 | + return True |
| 46 | + # Check verticle locations for win |
| 47 | + for c in range(NB_COL): |
| 48 | + for r in range(NB_ROW-3): |
| 49 | + if ( |
| 50 | + board[r][c] == piece and board[r+1][c] == piece and |
| 51 | + board[r+2][c] == piece and board[r+3][c] == piece |
| 52 | + ): |
| 53 | + return True |
| 54 | + # check for positively sloped diags |
| 55 | + for c in range(NB_COL-3): |
| 56 | + for r in range(NB_ROW-3): |
| 57 | + if( |
| 58 | + board[r][c] == piece and board[r+1][c+1] == piece and |
| 59 | + board[r+2][c+2] == piece and board[r+3][c+3] == piece |
| 60 | + ): |
| 61 | + return True |
| 62 | + # check for negitively sloped diags |
| 63 | + for c in range(NB_COL-3): |
| 64 | + for r in range(3, NB_ROW): |
| 65 | + if( |
| 66 | + board[r][c] == piece and board[r-1][c+1] == piece and |
| 67 | + board[r-2][c+2] == piece and board[r-3][c+3] == piece |
| 68 | + ): |
| 69 | + return True |
| 70 | + |
| 71 | + |
| 72 | +def draw_board(board): |
| 73 | + for c in range(NB_COL): |
| 74 | + for r in range(NB_ROW): |
| 75 | + pygame.draw.rect(screen, BLUE, |
| 76 | + (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, |
| 77 | + SQUARESIZE, SQUARESIZE)) |
| 78 | + pygame.draw.circle(screen, BLACK, |
| 79 | + (int(c*SQUARESIZE+SQUARESIZE/2), |
| 80 | + int(r*SQUARESIZE+1.5*SQUARESIZE)), RADIUS) |
| 81 | + |
| 82 | + for c in range(NB_COL): |
| 83 | + for r in range(NB_ROW): |
| 84 | + if board[r][c] == 1: |
| 85 | + pygame.draw.circle(screen, RED, |
| 86 | + (int(c*SQUARESIZE+SQUARESIZE/2), |
| 87 | + height-int(r*SQUARESIZE+SQUARESIZE/2)), |
| 88 | + RADIUS) |
| 89 | + elif board[r][c] == 2: |
| 90 | + pygame.draw.circle(screen, YELLOW, |
| 91 | + (int(c*SQUARESIZE+SQUARESIZE/2), |
| 92 | + height-int(r*SQUARESIZE+SQUARESIZE/2)), |
| 93 | + RADIUS) |
| 94 | + pygame.display.update() |
| 95 | + |
| 96 | + |
| 97 | +board = make_board() |
| 98 | +game_over = False |
| 99 | +turn = 0 |
| 100 | + |
| 101 | +pygame.init() |
| 102 | + |
| 103 | +SQUARESIZE = 100 # in pixels |
| 104 | + |
| 105 | +width = NB_COL*SQUARESIZE |
| 106 | +height = (NB_ROW+1)*SQUARESIZE # extra row for piece |
| 107 | + |
| 108 | +size = (width, height) |
| 109 | +RADIUS = int(SQUARESIZE/2-5) |
| 110 | +screen = pygame.display.set_mode(size) |
| 111 | +draw_board(board) |
| 112 | +pygame.display.update() |
| 113 | +myfont = pygame.font.SysFont('monospace', 75) |
| 114 | + |
| 115 | +while not game_over: |
| 116 | + for event in pygame.event.get(): |
| 117 | + if event.type == pygame.QUIT: |
| 118 | + sys.exit() |
| 119 | + if event.type == pygame.MOUSEMOTION: |
| 120 | + pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) |
| 121 | + pos_x = event.pos[0] |
| 122 | + if turn == 0: |
| 123 | + pygame.draw.circle(screen, RED, |
| 124 | + (pos_x, int(SQUARESIZE/2)), RADIUS) |
| 125 | + else: |
| 126 | + pygame.draw.circle(screen, YELLOW, |
| 127 | + (pos_x, int(SQUARESIZE/2)), RADIUS) |
| 128 | + pygame.display.update() |
| 129 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 130 | + pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE)) |
| 131 | + if turn is 0: |
| 132 | + pos_x = event.pos[0] |
| 133 | + col = int(math.floor(pos_x/SQUARESIZE)) |
| 134 | + if is_valid_location(board, col): |
| 135 | + row = get_next_open_row(board, col) |
| 136 | + drop_piece(board, row, col, 1) |
| 137 | + # Adds winning condition |
| 138 | + if winning_move(board, 1): |
| 139 | + label = myfont.render('player 1 wins!', 1, RED) |
| 140 | + screen.blit(label, (40, 10)) |
| 141 | + game_over = True |
| 142 | + # ask for player 2 input |
| 143 | + else: |
| 144 | + pos_x = event.pos[0] |
| 145 | + col = int(math.floor(pos_x/SQUARESIZE)) |
| 146 | + if is_valid_location(board, col): |
| 147 | + row = get_next_open_row(board, col) |
| 148 | + drop_piece(board, row, col, 2) |
| 149 | + if winning_move(board, 2): |
| 150 | + label = myfont.render('player 2 wins!', 1, YELLOW) |
| 151 | + screen.blit(label, (40, 10)) |
| 152 | + game_over = True |
| 153 | + draw_board(board) |
| 154 | + turn += 1 |
| 155 | + turn = turn % 2 # resets turn to zero to allow player 1 to go |
| 156 | + |
| 157 | + if game_over: |
| 158 | + pygame.time.wait(3000) |
0 commit comments