Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch with changes #76

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/Project-02-checkers.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Player.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, username, score):
self.username = username
self.score = 0
self.win = 0
self.difficulty = 2


def draw_text_input(self, player_name, error_msg="name error"):
Expand Down
4 changes: 2 additions & 2 deletions ScoreManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def update_scores(self, player):
self.user_scores[player.username] -= 50

player.score = self.user_scores[player.username]

self.save_scores()




107 changes: 99 additions & 8 deletions SecondMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,44 @@ def start_game_menu(self):
button_text_rect2 = button_text2.get_rect(
center=(Width // 2, Height // 3 + button_height + spacing + button_height // 2))

# Create button on screen using position and size parameters
#dificulty select buttons
easyButton= button_font.render("Easy", True, (32, 252, 3))
easyButton_text_rect = easyButton.get_rect(
center= (Width // 2 * 1.35, Height // 3 + button_height + spacing + button_height // 2 * .8))
normalButton= button_font.render("Normal", True, (240, 205, 7))
normalButton_text_rect = normalButton.get_rect(
center= (Width // 2 * 1.50, Height // 3 + button_height + spacing + button_height // 2 * .8))
hardButton= button_font.render("Hard", True, (240, 42, 7))
hardButton_text_rect = hardButton.get_rect(

center= (Width // 2 * 1.65, Height // 3 + button_height + spacing + button_height // 2 * .8))

# I'm pretty sure you can't remove anything blitted to the screen, so the only way to get rid of it is to
# put something on top of it. This is a workaround to do that with the difficulty buttons

arrow_idiot_workaround = button_font.render("^", True, (98, 0, 168))

arrow = button_font.render("^", True, (133, 9, 222))

arrow_text_rect = hardButton.get_rect(center=(Width // 2 * 1.38, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)
arrow_text_rect = hardButton.get_rect(center=(Width // 2 * 1.54, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)
arrow_text_rect = hardButton.get_rect(center=(Width // 2 * 1.70, Height // 3 + button_height + spacing + button_height // 2 * 1.5))

start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)

# Create buttons on screen using position and size parameters
pygame.draw.rect(start_game_screen, color, pygame.Rect(position, size))
start_game_screen.blit(button_text2, button_text_rect2)

start_game_screen.blit(easyButton, easyButton_text_rect)
start_game_screen.blit(normalButton, normalButton_text_rect)
start_game_screen.blit(hardButton, hardButton_text_rect)

button_rect_2 = pygame.Rect(position, size)



# Exit Second Menu Button
position = (Width // 2-150, Height // 3 + 135)
size = (300, 50) # width, height
Expand All @@ -134,11 +167,17 @@ def start_game_menu(self):

pygame.draw.rect(start_game_screen, cursor_color, button_rect_3) # Change color when cursor hovered over
start_game_screen.blit(button_text3, button_text_rect3)

pygame.display.update()
elif button_rect_2.collidepoint(mouse):

pygame.draw.rect(start_game_screen, cursor_color, button_rect_2)
start_game_screen.blit(button_text2, button_text_rect2)

start_game_screen.blit(easyButton, easyButton_text_rect)
start_game_screen.blit(normalButton, normalButton_text_rect)
start_game_screen.blit(hardButton, hardButton_text_rect)

pygame.display.update()

elif button_rect.collidepoint(mouse):
Expand All @@ -153,12 +192,17 @@ def start_game_menu(self):

pygame.draw.rect(start_game_screen, color, button_rect_2)
start_game_screen.blit(button_text2, button_text_rect2)


start_game_screen.blit(easyButton, easyButton_text_rect)
start_game_screen.blit(normalButton, normalButton_text_rect)
start_game_screen.blit(hardButton, hardButton_text_rect)


pygame.draw.rect(start_game_screen, color, button_rect)
start_game_screen.blit(button_text1, button_text_rect1)

pygame.display.update()


for event in pygame.event.get():
score_manager.load_scores()
Expand Down Expand Up @@ -186,7 +230,54 @@ def start_game_menu(self):
# score_manager.save_scores() # now inside elif so scores are updated before returning to main
elif event.type == self.background_music.SONG_END:
self.background_music.handle_event(event)

elif easyButton_text_rect.collidepoint(event.pos):
player1_name.difficulty = 1
arrow_text_rect = hardButton.get_rect(
center=(
Width // 2 * 1.38, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
screen.blit(arrow, arrow_text_rect)

arrow_text_rect = hardButton.get_rect(center=(
Width // 2 * 1.54, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)
arrow_text_rect = hardButton.get_rect(center=(
Width // 2 * 1.70, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)

pygame.display.update()

elif normalButton_text_rect.collidepoint(event.pos):
player1_name.difficulty = 2
arrow_text_rect = hardButton.get_rect(
center=(
Width // 2 * 1.54, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
screen.blit(arrow, arrow_text_rect)

arrow_text_rect = hardButton.get_rect(center=(
Width // 2 * 1.38, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)
arrow_text_rect = hardButton.get_rect(center=(
Width // 2 * 1.70, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)

pygame.display.update()

elif hardButton_text_rect.collidepoint(event.pos):
player1_name.difficulty = 3
arrow_text_rect = hardButton.get_rect(
center=(
Width // 2 * 1.7, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
screen.blit(arrow, arrow_text_rect)
arrow_text_rect = hardButton.get_rect(center=(
Width // 2 * 1.38, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)
arrow_text_rect = hardButton.get_rect(center=(
Width // 2 * 1.54, Height // 3 + button_height + spacing + button_height // 2 * 1.5))
start_game_screen.blit(arrow_idiot_workaround, arrow_text_rect)


pygame.display.update()

def start_game_vs_player(self, screen):
"""
The start game vs player function starts the game against another player by creating an object of the game class and passing the screen, color, and player names.
Expand Down Expand Up @@ -254,8 +345,8 @@ def start_game_vs_computer(self, screen):
while run:
clock.tick(60)
if game.turn == WHITE:
value, new_board = minimax(game.get_board(), 4, WHITE, game)
game.ai_move(new_board)
value, new_board = minimax(game.get_board(), 4, WHITE, game,player1_name.difficulty)
game.ai_move(new_board)

if game.winner() != None:
print(game.winner())
Expand Down
13 changes: 13 additions & 0 deletions Untitled Diagram.drawio

Large diffs are not rendered by default.

Binary file added checkers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 15 additions & 6 deletions computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

from copy import deepcopy
import pygame
from random import randint,choice
from constants import RED, WHITE

def minimax(position, depth, max_player, game):
def minimax(position, depth, max_player, game, difficulty):
"""
The minimax function is the minimax algorithm for AI to play checkers, and has parameters position, depth, max_player, and game parameters.
The function returns the best move for the computer to make.
Expand All @@ -22,21 +23,29 @@ def minimax(position, depth, max_player, game):
maxEval = float('-inf')
best_move = None
for move in get_all_moves(position, WHITE, game):
evaluation = minimax(move, depth-1, False, game)[0]
evaluation = minimax(move, depth-1, False, game,difficulty)[0]
maxEval = max(maxEval, evaluation)
if maxEval == evaluation:
best_move = move

return maxEval, best_move
#returns best move on hardest, choses random move on lowest, on normal returns random move half the time
if difficulty == 3:
return maxEval, best_move
elif difficulty == 2:
if randint(0, 1) == 1:
return maxEval,choice(get_all_moves(position, WHITE, game))
else:
return maxEval,best_move
return maxEval, choice(get_all_moves(position, WHITE, game))

else:
minEval = float('inf')
best_move = None
for move in get_all_moves(position, RED, game):
evaluation = minimax(move, depth-1, True, game)[0]
evaluation = minimax(move, depth-1, True, game,difficulty)[0]
minEval = min(minEval, evaluation)
if minEval == evaluation:
best_move = move

return minEval, best_move

def simulate_move(piece, move, board, game, skip):
Expand Down
25 changes: 24 additions & 1 deletion game.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import pygame
from constants import RED, WHITE, YELLOW, SQUARE_SIZE
from Main_Board import Main_Board
import sys



class Game:
"""
Expand All @@ -31,7 +34,7 @@ def __init__(self, win, color, player1, player2):
self.screen = pygame.display.set_mode((1000, 700))
self.player1 = player1
self.player2 = player2

def check_turn_timeout(self):
"""
The check turn timeout function checks the turn timeout and displays the move timer on the screen. If the time is running out, the text color is set to red.
Expand Down Expand Up @@ -82,6 +85,25 @@ def display_player_names(self, player1, player2):
self.screen.blit(text_surface, (715, 350))
self.screen.blit(text_surface2, (715, 400))

def displayExit(self):

self.exit_button_rect=pygame.Rect(715, 450, 140, 50)
pygame.draw.rect(self.screen, RED, self.exit_button_rect)

text_display = self.font.render("exit app", True, self.text_color)
self.screen.blit(text_display, (715, 450))

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if self.exit_button_rect.collidepoint(event.pos): # Assuming 'game' is your Game instance
pygame.quit()
sys.exit()



def update(self):
"""
The update function updates the board to show the current board and features.
Expand All @@ -92,6 +114,7 @@ def update(self):
self.display_turn()
self.display_piece_count()
self.display_player_names(self.player1, self.player2)
self.displayExit()
pygame.display.update()

def winner(self):
Expand Down
Loading