<commit_before>//
//  Piece.swift
//  Pods
//
//  Created by <NAME> on 04/09/2016.
//
//

import Foundation

open class Player {
   
    let color: Color!
    weak var game: Game!
    
    init(color: Color, game: Game){
        self.color = color
        self.game = game;
    }
    
    func movePiece(fromLocation: BoardLocation, toLocation: BoardLocation){
        
        // We can't move to our current location
        if fromLocation == toLocation {
            print("Cannot move piece to its current location")
        }
        
        // Get the piece
        guard let piece = self.game.board.getPiece(at: fromLocation) else {
            print("There isn't a piece of color \(color) at \(fromLocation)")
            return
        }
        
        // Check that the piece color matches the player color
        if piece.color != self.color {
            print("Player color \(color) cannot move piece of color \(piece.color)")
            return;
        }
        
        // Make sure the piece can move to the location
        if !piece.movement.canPieceMove(fromLocation: fromLocation, toLocation: toLocation, board: game.board) {
            print("Piece at \(fromLocation) cannot move to \(toLocation)")
        }
        
        // Move the piece
        
        print("Piece can be moved")
        
    }
    
}

<commit_msg>Implement player ability to move pieces<commit_after>//
//  Piece.swift
//  Pods
//
//  Created by <NAME> on 04/09/2016.
//
//

import Foundation

open class Player {
   
    let color: Color!
    weak var game: Game!
    
    init(color: Color, game: Game){
        self.color = color
        self.game = game;
    }
    
    // MARK: - Public
    
    public func movePiece(fromLocation: BoardLocation, toLocation: BoardLocation) {
        
        // Check if move is allowed
        let canMove = canMovePieceWithError(fromLocation: fromLocation, toLocation: toLocation)
        
        if let error = canMove.error {
            error.printDescription()
            print("To avoid seeing these errors, call canMovePiece(fromLocation:toLocation) first")
            return
        }
        
        // Move the piece
        game.board.movePiece(fromLocation: fromLocation, toLocation: toLocation)
    }
    
    public func canMovePiece(fromLocation: BoardLocation, toLocation: BoardLocation) -> Bool {
        return canMovePieceWithError(fromLocation: fromLocation, toLocation: toLocation).result
    }

    // MARK: - Private
    
    enum PieceMoveError : Error {
        case movingToSameLocation(description: String)
        case noPieceToMove(description: String)
        case pieceColorDoesNotMatchPlayerColor(description: String)
        case pieceUnableToMoveToLocation(description: String)
        
        func printDescription() {
            
            switch self {
            case .movingToSameLocation(let description):
                print(description)
            case .noPieceToMove(let description):
                print(description)
            case .pieceColorDoesNotMatchPlayerColor(let description):
                print(description)
            case .pieceUnableToMoveToLocation(let description):
                print(description)
            }
        }
    }
    
    private func canMovePieceWithError(fromLocation: BoardLocation, toLocation: BoardLocation) -> (result: Bool, error: PieceMoveError?) {
        
        // We can't move to our current location
        if fromLocation == toLocation {
            return (false, .movingToSameLocation(description: "Cannot move piece to its current location"))
        }
        
        // Get the piece
        guard let piece = self.game.board.getPiece(at: fromLocation) else {
            return (false, .noPieceToMove(description: "There isn't a piece of color \(color) at \(fromLocation)"))
        }
        
        // Check that the piece color matches the player color
        if piece.color != self.color {
            return (false, .pieceColorDoesNotMatchPlayerColor(description: "Player color \(color) cannot move piece of color \(piece.color)"))
        }
        
        // Make sure the piece can move to the location
        if !piece.movement.canPieceMove(fromLocation: fromLocation, toLocation: toLocation, board: game.board) {
            return (false, .pieceUnableToMoveToLocation(description: "Piece at \(fromLocation) cannot move to \(toLocation)"))
        }

        return (true, nil)
    }

}


{'fraction_non_alphanumeric': 0.08840644583706356, 'fraction_numerical': 0.0035810205908683975, 'mean_word_length': 2.0195945945945946, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7125900', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1165, 'n_words': 352}