Skip to content

Littlefield #79

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

Open
wants to merge 14 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.

9 changes: 9 additions & 0 deletions .idea/bassill-lee-stanton-littlefield.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/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.

26 changes: 14 additions & 12 deletions Main_Board.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import pygame
from constants import BLACK, ROWS, RED, SQUARE_SIZE, COLS, WHITE
from constants import BLACK, ROWS, RED, SQUARE_SIZE, COLS, WHITE, GREEN
from pieces import Piece

class Main_Board:
Expand All @@ -13,12 +13,14 @@ class Main_Board:
evaluate the board, get all pieces, get a single piece, move a piece (left or right), create the board, draw the board,
remove a piece, and check for a winner.
"""
def __init__(self, color):
def __init__(self, color, user_color, user_two_color):
"""
The init function initializes the Main_Board class with a color and creates the board.
"""
self.board = []
self.color = color
self.user_color = user_color
self.user_two_color = user_two_color
self.red_left = self.white_left = 12
self.red_kings = self.white_kings = 0
self.create_board()
Expand Down Expand Up @@ -57,7 +59,7 @@ def move(self, piece, row, col):
piece.move(row, col)
if row == ROWS - 1 or row == 0:
piece.make_king()
if piece.color == WHITE:
if piece.color == self.user_two_color:
self.white_kings += 1
else:
self.red_kings += 1
Expand All @@ -80,9 +82,9 @@ def create_board(self):
for col in range(COLS):
if col % 2 == ((row + 1) % 2):
if row < 3:
self.board[row].append(Piece(row, col, WHITE))
self.board[row].append(Piece(row, col, self.user_two_color))
elif row > 4:
self.board[row].append(Piece(row, col, RED))
self.board[row].append(Piece(row, col, self.user_color))
else:
self.board[row].append(0)
else:
Expand All @@ -106,7 +108,7 @@ def remove(self, pieces):
for piece in pieces:
self.board[piece.row][piece.col] = 0
if piece != 0:
if piece.color == RED:
if piece.color == self.user_color:
self.red_left -= 1
else:
self.white_left -= 1
Expand All @@ -116,10 +118,10 @@ def winner(self):
The winner function checks if a winner has been found and returns the winner. If no winner has been found, None is returned.
If a user has no pieces left or no moves left, the other user is the winner.
"""
if self.red_left <= 0 or self.no_moves(RED):
return WHITE
elif self.white_left <= 0 or self.no_moves(WHITE):
return RED
if self.red_left <= 0 or self.no_moves(self.user_color):
return self.user_two_color
elif self.white_left <= 0 or self.no_moves(self.user_two_color):
return self.user_color

return None

Expand All @@ -131,10 +133,10 @@ def get_valid_moves(self, piece):
left = piece.col - 1
right = piece.col + 1
row = piece.row
if piece.color == RED or piece.king:
if piece.color == self.user_color or piece.king:
moves.update(self.move_left(row -1, max(row-3, -1), -1, piece.color, left))
moves.update(self.move_right(row -1, max(row-3, -1), -1, piece.color, right))
if piece.color == WHITE or piece.king:
if piece.color == self.user_two_color or piece.king:
moves.update(self.move_left(row +1, min(row+3, ROWS), 1, piece.color, left))
moves.update(self.move_right(row +1, min(row+3, ROWS), 1, piece.color, right))

Expand Down
14 changes: 8 additions & 6 deletions SecondMenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(self, track):
self.background_music = BackgroundMusic([track])

color = RED
user_color = RED
user_two_color = WHITE
def start_game_menu(self):
"""
The start game menu function displays the second menu of the game, which allows the user to choose between playing against another player or against the computer.
Expand Down Expand Up @@ -193,7 +195,7 @@ def start_game_vs_player(self, screen):
"""
run = True
clock = pygame.time.Clock()
game = Game(screen, self.color, player1_name.username, player2_name.username)
game = Game(screen, self.color, self.user_color, self.user_two_color, player1_name.username, player2_name.username)
global score_manager, user_scores

# Exit Button
Expand All @@ -209,12 +211,12 @@ def start_game_vs_player(self, screen):
if game.winner() != None:
print(game.winner())
run = False
if game.winner() == RED:
if game.winner() == self.user_color:
player1_name.update_win()
score_manager.update_scores(player1_name)
player2_name.update_loss()
score_manager.update_scores(player2_name)
elif game.winner() == WHITE:
elif game.winner() == self.user_two_color:
player2_name.update_win()
score_manager.update_scores(player2_name)
player1_name.update_loss()
Expand All @@ -240,7 +242,7 @@ def start_game_vs_computer(self, screen):
"""
run = True
clock = pygame.time.Clock()
game = Game(screen, self.color, player1_name.username, "Computer")
game = Game(screen, self.color, self.user_color, self.user_two_color, player1_name.username, "Computer")
global score_manager, user_scores

# Exit Button
Expand All @@ -253,8 +255,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)
if game.turn == self.user_two_color:
value, new_board = minimax(game.get_board(), 4, self.user_two_color, game, self.user_color, self.user_two_color)
game.ai_move(new_board)

if game.winner() != None:
Expand Down
Binary file modified __pycache__/Main_Board.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/MusicClass.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/MusicClass.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/Player.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/Player.cpython-312.pyc
Binary file not shown.
Binary file added __pycache__/ScoreManager.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/ScoreManager.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/SecondMenu.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/SharedObjects.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/SharedObjects.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/computer.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/computer.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/constants.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/constants.cpython-312.pyc
Binary file not shown.
Binary file modified __pycache__/game.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/pieces.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/pieces.cpython-312.pyc
Binary file not shown.
10 changes: 5 additions & 5 deletions computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pygame
from constants import RED, WHITE

def minimax(position, depth, max_player, game):
def minimax(position, depth, max_player, game, user_color, user_two_color):
"""
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 @@ -21,8 +21,8 @@ def minimax(position, depth, max_player, game):
if max_player:
maxEval = float('-inf')
best_move = None
for move in get_all_moves(position, WHITE, game):
evaluation = minimax(move, depth-1, False, game)[0]
for move in get_all_moves(position, user_two_color, game):
evaluation = minimax(move, depth-1, False, game, user_color, user_two_color)[0]
maxEval = max(maxEval, evaluation)
if maxEval == evaluation:
best_move = move
Expand All @@ -31,8 +31,8 @@ def minimax(position, depth, max_player, 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]
for move in get_all_moves(position, user_color, game):
evaluation = minimax(move, depth-1, True, game, user_color, user_two_color)[0]
minEval = min(minEval, evaluation)
if minEval == evaluation:
best_move = move
Expand Down
22 changes: 12 additions & 10 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
The game file holds the game logic and game class.
"""
import pygame
from constants import RED, WHITE, YELLOW, SQUARE_SIZE
from constants import RED, WHITE, YELLOW, SQUARE_SIZE, GREEN
from Main_Board import Main_Board

class Game:
Expand All @@ -12,7 +12,7 @@ class Game:
display the piece count, display the player names, update the board, check for a winner, select a piece, move a piece, show available moves, change the turn,
get the board, and move an AI piece.
"""
def __init__(self, win, color, player1, player2):
def __init__(self, win, color, user_color, user_two_color, player1, player2):
"""
The init function initializes the Game class with a window, color, player1, and player2, and sets the turn start time and turn timeout. The text color is set to white,
and the urgent text color is set to red. The screen is set to the window size, and the player names are set to player1 and player2.
Expand All @@ -21,9 +21,11 @@ def __init__(self, win, color, player1, player2):
self.turn_timeout = 5200 # 5.2 seconds per turn
self.win = win
self.color = color
self.user_color = user_color
self.user_two_color = user_two_color
self.selected = None
self.board = Main_Board(self.color)
self.turn = RED
self.board = Main_Board(self.color, self.user_color, user_two_color)
self.turn = user_color
self.valid_moves = {}
self.font = pygame.font.Font(None, 36) # Font for rendering text
self.text_color = WHITE # Text color
Expand Down Expand Up @@ -53,10 +55,10 @@ def display_turn(self):
"""
The display turn function displays the current turn on the screen.
"""
if self.turn == RED:
text = f"Current Turn: RED"
if self.turn == self.user_color:
text = f"Current Turn: Player One"
else:
text = f"Current Turn: WHITE"
text = f"Current Turn: Player Two"
text_surface = self.font.render(text, True, self.text_color)
self.screen.blit(text_surface, (715, 100))

Expand Down Expand Up @@ -151,10 +153,10 @@ def change_turn(self):
"""
self.valid_moves = {}
self.turn_start_time = pygame.time.get_ticks() # Reset the turn timer
if self.turn == RED:
self.turn = WHITE
if self.turn == self.user_color:
self.turn = self.user_two_color
else:
self.turn = RED
self.turn = self.user_color

def get_board(self):
"""
Expand Down
Loading