-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
200 lines (188 loc) · 6.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# Nicholas J. Grosso 7/4/2021
import numpy as np
import pygame
import math
import sys
import tkinter
import random
# Global variables
top = tkinter.Tk() # Prompt variable
top.title("Select mode: ") # Title for prompt window
top.geometry("250x100") # Prompt window size
# Colors
BLUE = (0,0,255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# Test for in game and for what version, 2 people or AI
in_session = True # is the game still in progress?
first_round = True # Bool value to see if we are in the first round
AI = False # Bool to see if the game was started up to play with AI instead
round = 0 # Empty slot is 0
# Values for board construction
HUMAN = 1 # Human is a 1
ALG = 2 # Algorithm is a 2
COLMS = 7 # Set up colms used
ROWS = 6 # Set up rows used
#Functions for prompt
def byeBF():
global AI
AI = False
top.destroy()
def byeBT():
global AI
AI = True
top.destroy()
# First button for prompt window for PVP
B1 = tkinter.Button(top, text ="Player vs Player", command = byeBF).pack()
# Second button for prompt window for alg
#B2 = tkinter.Button(top, text = "Player vs MinMax", command = byeBT).pack()
top.mainloop() # Keeps prompt live until button destroys it
pygame.init()
myfont = pygame.font.SysFont("monospace", 75)
SQUARESIZE = 100
width = COLMS * SQUARESIZE
height = ( ROWS + 1) * SQUARESIZE
size = (width, height)
screen = pygame.display.set_mode(size)
radius = int(SQUARESIZE/2 - 5)
def draw_board(board):
for i in range(COLMS):
for j in range(ROWS):
pygame.draw.rect(screen, BLUE, (i*SQUARESIZE, j*SQUARESIZE+SQUARESIZE , SQUARESIZE, SQUARESIZE))
if board[j][i] == 0:
pygame.draw.circle(screen, BLACK, (int(i*SQUARESIZE+SQUARESIZE/2), int(j*SQUARESIZE+SQUARESIZE/2)), radius)
elif board[j][i] == 1:
pygame.draw.circle(screen, RED, (int(i*SQUARESIZE+SQUARESIZE/2), int(j*SQUARESIZE+SQUARESIZE/2)), radius)
elif board[j][i] == 2:
pygame.draw.circle(screen, YELLOW, (int(i*SQUARESIZE+SQUARESIZE/2), int(j*SQUARESIZE+SQUARESIZE/2)), radius)
pygame.display.update()
# def ask():
# a = int(input())
# if a <= 0 or a > COLMS:
# print("CANNOT BE LESS MIN COLMS OR GREATER THAN MAX COLMS")
# return ask()
# a -= 1 # correcting colm location (Ex. If input is 1 it chooses 1st colm instead of needing to input 0 for first colm)
# return a
def create_board():
board = np.zeros((ROWS, COLMS))
return board
board = create_board()
draw_board(board)
pygame.display.update()
def insert(board, action, row, player):
update = row + 1
board[ROWS-update][action] = player
def fupq(board, action): # fupq - filled up ?
return board[0][action] == 0
def won(board, piece):
# Verticle check
for i in range(COLMS):
for j in range(ROWS-3):
if board[j][i] == piece and board[j+1][i] == piece and board[j+2][i] == piece and board[j+3][i] == piece:
return True
# Check all same 4 pieces horizantal
for i in range(COLMS-3):
for j in range(ROWS):
if board[j][i] == piece and board[j][i+1] == piece and board[j][i+2] == piece and board[j][i+3] == piece:
return True
# Check diag going upward
for i in range(COLMS-3):
for j in range(ROWS-3, ROWS):
if board[j][i] == piece and board[j-1][i+1] == piece and board[j-2][i+2] == piece and board[j-3][i+3] == piece:
return True
# Check diag going downward
for i in range(COLMS-3):
for j in range(ROWS-3):
if board[j][i] == piece and board[j+1][i+1] == piece and board[j+2][i+2] == piece and board[j+3][i+3] == piece:
return True
draw_board(board)
while in_session == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
posx = event.pos[0]
if round == 0:
pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)), radius)
else:
pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE/2)), radius)
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))
if round == 0: # Its player 1 (or humans) turn
posx = event.pos[0]
colmSelected = int(math.floor(posx/SQUARESIZE))
print("Human make your move: ")
if fupq(board, colmSelected):
for row in range(ROWS):
update = row + 1
n = ROWS - update
if board[n][colmSelected] == 0:
insert(board, colmSelected, row, HUMAN)
if won(board, 1):
draw_board(board)
label = myfont.render("PLAYER 1 WINS!!!", 1, RED)
screen.blit(label, (40,10))
pygame.display.flip()
pygame.event.pump()
pygame.time.delay(3000)
in_session = False
break
round += 1
round = round % 2
print(board)
draw_board(board)
else:
print("!!! ERROR NOT VALID MOVE !!!")
round += 1 # Restarts a new round again for the same player
elif AI == False and round == 1:
posx = event.pos[0]
colmSelected = int(math.floor(posx/SQUARESIZE))
if fupq(board, colmSelected):
for row in range(ROWS):
update = row + 1
n = ROWS - update
if board[n][colmSelected] == 0:
insert(board, colmSelected, row, ALG)
if won(board, 2):
draw_board(board)
label = myfont.render("PLAYER 2 WINS!!!", 1, YELLOW)
screen.blit(label, (40,10))
pygame.display.flip()
pygame.event.pump()
pygame.time.delay(3000)
in_session = False
break
round += 1
round = round % 2
print(board)
draw_board(board)
else:
print("!!! ERROR NOT VALID MOVE !!!")
round += 1 # Restarts a new round again for the same player
if AI == True and round == 1: # If MinMax is selected
colmSelected = random.randint(0, COLMS-1)
if fupq(board, colmSelected):
for row in range(ROWS):
update = row + 1
n = ROWS - update
if board[n][colmSelected] == 0:
insert(board, colmSelected, row, ALG)
if won(board, 2):
draw_board(board)
label = myfont.render("PLAYER 2 WINS!!!", 1, YELLOW)
screen.blit(label, (40,10))
pygame.display.flip()
pygame.event.pump()
pygame.time.delay(3000)
in_session = False
break
print(board)
draw_board(board)
round += 1
round = round % 2
else:
print("!!! ERROR NOT VALID MOVE !!!")
round += 1 # Restarts a new round again for the same player