Created
November 20, 2023 07:55
-
-
Save 0xNB-dev/5798fd9783647397a86d1e28a5f3a9b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract Monaco { | |
struct CarData { | |
uint32 balance; // 0 means the car has no money. | |
uint32 speed; // 0 means the car isn`t moving. Each turn, the y will be updated by the speed, to next turn y will be equal to y + speed | |
uint32 y; // 0 means the car hasn`t moved. y is the position on the axis between 0 and 1000. Car starts at 0 and wins at 1000. | |
uint32 shield; // Where 0 means the car isn`t shielded. | |
address car; // the car address on the EVM | |
} | |
function getShellCost(uint256 amount) external pure returns (uint256) { | |
return amount; | |
} | |
function getSuperShellCost(uint256 amount) external pure returns (uint256) { | |
return amount * 2; | |
} | |
function getBananaCost() external pure returns (uint256) { | |
return 1; | |
} | |
function getShieldCost(uint256 amount) external pure returns (uint256) { | |
return amount * 3; | |
} | |
function getAccelerateCost(uint256 amount) external pure returns (uint256) { | |
return amount; | |
} | |
function buyShell(uint256 amount) external pure returns (uint256) { | |
return amount; | |
} | |
function buySuperShell(uint256 amount) external pure returns (uint256) { | |
return amount * 2; | |
} | |
function buyBanana() external pure returns (uint256) { | |
return 1; | |
} | |
function buyShield(uint256 amount) external pure returns (uint256) { | |
return amount * 3; | |
} | |
function buyAcceleration(uint256 amount) external pure returns (uint256) { | |
return amount; | |
} | |
} | |
interface ICar { | |
function sayMyName() external pure returns (string memory); | |
function takeYourTurn( | |
Monaco monaco, | |
Monaco.CarData[] calldata allCars, | |
uint256[] calldata bananas, | |
uint256 yourCarIndex | |
) external; | |
} | |
contract BalancedCar is ICar { | |
function takeYourTurn( | |
Monaco monaco, | |
Monaco.CarData[] calldata allCars, | |
uint256[] calldata bananas, | |
uint256 yourCarIndex | |
) external override { | |
Monaco.CarData memory ourCar = allCars[yourCarIndex]; | |
// If the first car's position is under y=700 and our balance (money) is above 10000 | |
if (allCars[0].y < 700 && ourCar.balance > 10000) { | |
uint256 banCost = monaco.getBananaCost(); | |
// If the banana costs less than 10, use it. | |
if (banCost < 10) { | |
// Buy a banana (default to 1) and update the car balance | |
ourCar.balance -= uint24(monaco.buyBanana()); | |
} | |
bool getShell = false; | |
bool getSuperShell = false; | |
// Get the cost for 1 shell | |
uint256 shellCost = monaco.getShellCost(1); | |
// Get the cost for 1 super shell | |
uint256 superShellCost = monaco.getSuperShellCost(1); | |
// If the shell cost is less or equal than 50, mark it for purchase | |
if (shellCost <= 50) { | |
getShell = true; | |
} | |
// If super shell cost if less or equal than 50, mark it for purchase | |
if (superShellCost <= 50) { | |
getSuperShell = true; | |
} | |
// If we are not the first player and the speed of the car ahead of us is higher than 20, and shell costs less than 500, we mark the shell for purchase | |
if (ourCarIndex != 0 && allCars[ourCarIndex - 1].speed > 20 && shellCost <= 500) { | |
getShell = true; | |
} | |
// If the speed of the first car is higher than 20, and super shell costs less than 500, we mark the super shell for purchase | |
if (allCars[0].speed > 20 && superShellCost <= 500) { | |
getSuperShell = true; | |
} | |
// If we marked getSuperShell and we have enough balance | |
if (getSuperShell && ourCar.balance > superShellCost) { | |
// Buy one super shell and udpate the balance | |
ourCar.balance -= uint24(monaco.buySuperShell(1)); | |
// If the shell is less than 50 | |
if (shellCost <= 50) { | |
// Buy 1 shell and update the balance | |
ourCar.balance -= uint24(monaco.buyShell(1)); | |
} | |
// If we marked getShell and we have enough balance | |
} else if (getShell && ourCar.balance > shellCost) { | |
// Buy 1 shell and update the balance | |
ourCar.balance -= uint24(monaco.buyShell(1)); | |
} | |
return; | |
} | |
// Win if possible. | |
// If our car's y position is more than 850 (meaning we are close from the end) | |
// And we have enough balance to buy (1000 - our next position) acceleration | |
if ( | |
ourCar.y > 850 && | |
ourCar.balance >= | |
monaco.getAccelerateCost(1000 - (ourCar.y + ourCar.speed)) | |
) { | |
// Buy enough acceleration to reach through the finish line | |
monaco.buyAcceleration(1000 - (ourCar.y + ourCar.speed)); | |
return; | |
} | |
bool getShell = false; | |
bool getSuperShell = false; | |
uint256 shellCost = monaco.getShellCost(1); | |
uint256 superShellCost = monaco.getSuperShellCost(1); | |
if (shellCost <= 100) { | |
getShell = true; | |
} | |
if (superShellCost <= 100) { | |
getSuperShell = true; | |
} | |
if (ourCarIndex != 0) { | |
if (allCars[0].y + allCars[0].speed >= 1000) { | |
getShell = allCars[0].shield != 0 && ourCarIndex == 1; | |
getSuperShell = true; | |
} | |
if ( | |
allCars[ourCarIndex - 1].speed >= 8 | |
) { | |
if (superShellCost <= 500) getSuperShell = true; | |
else if (shellCost <= 500 && allCars[ourCarIndex - 1].shield == 0) getShell = true; | |
} | |
if ( | |
ourCar.balance > 3500 && | |
allCars[ourCarIndex - 1].speed >= 25 | |
) { | |
if (superShellCost <= 1500) getSuperShell = true; | |
else if (shellCost <= 1500 && allCars[ourCarIndex - 1].shield == 0) getShell = true; | |
} | |
if ( | |
ourCar.balance > 6000 && | |
allCars[ourCarIndex - 1].speed >= 75 | |
) { | |
if (superShellCost <= 4000) getSuperShell = true; | |
else if (shellCost <= 4000 && allCars[ourCarIndex - 1].shield == 0) getShell = true; | |
} | |
} | |
if (ourCarIndex == 2) { | |
if ( | |
superShellCost <= 500 && | |
ourCar.balance > 2000 && | |
(allCars[0].speed > 8 || allCars[1].speed > 8) | |
) { | |
getSuperShell = true; | |
} | |
if ( | |
superShellCost <= 1000 && | |
ourCar.balance > 4000 && | |
(allCars[0].speed > 25 || allCars[1].speed > 25) | |
) { | |
getSuperShell = true; | |
} | |
} | |
if (ourCar.balance > 1000 && ourCar.speed > 50) { | |
if (superShellCost <= 2000) { | |
getSuperShell = true; | |
} | |
if (shellCost <= 2000) { | |
getShell = true; | |
} | |
} | |
if (ourCar.balance > 1000 && ourCar.shield == 0) { | |
bool getShield = false; | |
uint256 shieldCost = monaco.getShieldCost(2); | |
if (ourCarIndex == 0) { | |
if (shieldCost < 100) getShield = true; | |
} else if (ourCarIndex == 1) { | |
if (shieldCost < 30) getShield = true; | |
} else { | |
if (shieldCost < 20) getShield = true; | |
} | |
if (getShield && ourCar.balance > shieldCost) { | |
ourCar.balance -= uint24(monaco.buyShield(2)); | |
} | |
} | |
uint256 maxCost = 250; | |
if ( | |
(ourCar.balance > 12000) || | |
(ourCar.balance > 8000 && ourCar.y > 600) || | |
(ourCar.balance > 5000 && ourCar.y > 800) | |
) { | |
maxCost = 500; | |
} | |
if ( | |
(ourCar.balance > 2500 && ourCar.y > 900) | |
) { | |
maxCost = 500; | |
} | |
if (ourCar.balance < 1000) { | |
maxCost = 100; | |
} | |
uint256 i = 0; | |
uint256 prevI = 0; | |
{ | |
uint256 cost = 0; | |
while (i < 200 && cost < maxCost && cost <= ourCar.balance) { | |
prevI = i; | |
if (i < 10) i++; | |
else if (i < 20) i += 5; | |
else i += 50; | |
cost = monaco.getAccelerateCost(i); | |
} | |
} | |
if (prevI >= 3) { | |
uint256 cost = monaco.getAccelerateCost(prevI); | |
if (ourCar.balance >= cost) { | |
ourCar.balance -= uint24(monaco.buyAcceleration(prevI)); | |
ourCar.speed += uint32(prevI); | |
} | |
} | |
if (ourCar.speed < 3) { | |
if (ourCar.balance > 1000 && monaco.getAccelerateCost(1) <= 100) { | |
ourCar.balance -= uint24(monaco.buyAcceleration(1)); | |
ourCar.speed += 1; | |
} | |
} | |
if (ourCar.balance > 1000 && ourCar.shield == 0) { | |
bool getShield = false; | |
uint256 shieldCost = monaco.getShieldCost(2); | |
if (ourCarIndex == 0) { | |
if (shieldCost < 100) getShield = true; | |
} else if (ourCarIndex == 1) { | |
if (shieldCost < 30) getShield = true; | |
} else { | |
if (shieldCost < 20) getShield = true; | |
} | |
if (getShield && ourCar.balance > shieldCost) { | |
ourCar.balance -= uint24(monaco.buyShield(2)); | |
} | |
} | |
} | |
function sayMyName() external pure returns (string memory) { | |
return "BalancedCar"; | |
} | |
} | |
``` | |
This implementation focuses on a healthy economic balance by buying a moderate amount of accelerations, | |
shells, super shells, and shields. It also buys a banana if it can afford it and if it is beneficial to do so. | |
The car will also try to win the race by buying enough accelerations to reach the finish line. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment