import pygame, sys

from board import Board

"""
A simple four in a row game.

Author: Isaac Arvestad
"""
class Game:
    def __init__(self):
        pygame.init()
        
        self.screen = pygame.display.set_mode((600,600))
        
        self.background = pygame.Surface(self.screen.get_size())
        self.background.fill((255,255,255))
        self.background = self.background.convert()
        
        self.screen.blit(self.background, (0,0))
        
        self.screen_width = self.screen.get_size()[0]
        self.screen_height = self.screen.get_size()[1]
        
        self.board = Board(7, 7)
        self.piece_width = 50
        self.piece_height = 50
        self.piece_spacing = 10

        self.cursor_column = 3
        self.cursor_width = 30
        self.cursor_height = 30
        self.cursor_color = (128, 128, 128)
        
    def update(self):
        "Handle input."
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return False
                if event.key == pygame.K_LEFT:
                    if self.cursor_column > 0:
                        self.cursor_column -= 1
                if event.key == pygame.K_RIGHT:
                    if self.cursor_column < 6:
                        self.cursor_column += 1
                if event.key == pygame.K_SPACE:
                    if self.board.add_piece(self.cursor_column, 1) == True:
                        evaluation = self.board.evaluate_board()
                        if evaluation == 1:
                            print("Player 1 won!")
                        elif evaluation == 2:
                            print("Player 2 won!")
                        elif evaluation == 3:
                            print("Draw!")
                        elif evaluation == 0:
                            ai_move = self.board.get_ai_move()
                            self.board.add_piece(ai_move, 2)
                    
        return True

    def render(self, screen):
        self.screen.fill((255,255,255))
        
        board_width = self.board.columns * (self.piece_width + self.piece_spacing)
        board_height = self.board.rows * (self.piece_height + self.piece_spacing)

        x_start = self.screen_width/2 - board_width/2
        y_start = self.screen_height/2 - board_height/2
        
        for x in range(self.board.columns):
            for y in range(self.board.rows):
                x_position = x_start + x*(self.piece_width+self.piece_spacing)
                y_position = y_start + y*(self.piece_height+self.piece_spacing)
                piece_color = (0,0,0)
                if self.board.matrix.item((y, x)) == 1:
                    piece_color = (255,0,0)
                elif self.board.matrix.item((y, x)) == 2:
                    piece_color = (0,255,0)
                    
                pygame.draw.rect(screen, piece_color, (x_position, y_position, self.piece_width, self.piece_height))

        x_cursor = x_start + self.cursor_column * (self.piece_width + self.piece_spacing) + (self.piece_width - self.cursor_width)/2
        y_cursor = y_start - self.piece_height + (self.piece_height - self.cursor_height)/2
        pygame.draw.rect(screen, self.cursor_color, (x_cursor, y_cursor, self.cursor_width, self.cursor_height))

    "Starts the game."
    def start(self):
        while True:
            if not self.update():
                break

            self.render(self.screen)
            
            pygame.display.update()
        self.exit()

    def exit(self):
        pygame.quit()
        sys.exit()

game = Game()
game.start()

@@ -1576,374 +1576,40 @@
     
-evaluation = self.board.evaluate_board()%0A                        if evaluation == 1:%0A                            print(%22Player 1 won!%22)%0A                        elif evaluation == 2:%0A                            print(%22Player 2 won!%22)%0A                        elif evaluation == 3:%0A                            print(%22Draw!%22)%0A                        elif evaluation == 0
+if self.evaluate_board() == True
 :%0A  
@@ -3098,24 +3098,543 @@
 r_height))%0A%0A
+    %22%22%22%0A    Evaluates the board. If a player has won or there is a draw,%0A    print out the result and return false. If play should instead %0A    continue, return true.%0A    %22%22%22%0A    def evaluate_board(self):%0A        evaluation = self.board.evaluate_board()%0A        if evaluation == 1:%0A            print(%22Player 1 won!%22)%0A        elif evaluation == 2:%0A            print(%22Player 2 won!%22)%0A        elif evaluation == 3:%0A            print(%22Draw!%22)%0A        elif evaluation == 0:%0A            return True%0A        return False%0A    %0A
     %22Starts 

Add evaluate board method for handling game over.