contract TokenGame {
    using SafeMath for uint256;

    // The ME-Token being minted
    MintableToken public token;
    // The NFT being minted, thought it might be useful
    ERC721Token public pass;
    // A mapping of all the people who funded to the presale
    mapping(address => uint256) public funders;
    // An array of all the people who beat the game
    mapping(address => bool) public isWinner;
    // amount of raised money in wei
    uint256 public weiRaised;
    // cap of the Crowdsale
    uint256 public cap = 7000 ether;
    // My wallet
    address public wallet;

    event GameWon(address indexed winner, uint256 valueUnlocked);

    //Constructor
    function TokenGame(ERC721Token _pass, MintableToken _token) public {
        pass = _pass;
        token = _token;
        wallet = msg.sender;
    }

    // Beating the game should be as simple as sending all of your ETH to the contract address
    // Idk tho I could be wrong :)
    function () public payable {
        beatGame();
    }

    // If you call this function you should beat the game... I think
    function beatGame() public payable {
        require(weiRaised.add(msg.value) <= cap);
        weiRaised = weiRaised.add(msg.value); // update state
        funders[msg.sender] = funders[msg.sender].add(msg.value);
        wallet.transfer(msg.value);
    }

    // Check the balance
    function getBalance() public view returns(uint256 balance) {
        return this.balance;
    }

    // You might wanna check if the cap has been reached before doing anything
    function capReached() public view returns(bool) {
        return weiRaised >= cap;
    }

    function changeOwner() public {
        require(msg.sender == wallet);
        token.transferOwnership(wallet);
        pass.transferOwnership(wallet);
    }

    // I wouldn't call this if I were you, who knows what could happen
    function loseGame() public {
        require(this.balance > 0);
        weiRaised = weiRaised.add(this.balance); // update state

        isWinner[msg.sender] = true;
        funders[msg.sender] = funders[msg.sender].add(this.balance);

        receivePrize(msg.sender, this.balance);
        GameWon(msg.sender, this.balance);

        wallet.transfer(this.balance);
    }

    function getTokenAmount(uint256 weiAmount) internal view returns(uint256) {
        // how many token units a buyer gets per wei
        return weiAmount.mul(10000);
    }

    function receivePrize(address _winner, uint256 _prizeMoney) private {
        // The code for token minting
        uint256 tokens = getTokenAmount(_prizeMoney);
        token.mint(_winner, tokens);
        // The code for the pass minting
        pass.mint(_winner, _prizeMoney);
    }

}
//Token Game contract - allows skilled players to gain access to the ERC-ME beta
function getBalance() public view returns(uint256 balance) {
        return this.balance;
    }
// Check the balance