Skip to content
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

完成 task2~task4 #2146

Merged
merged 11 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
完成task4 sui
Yann committed Dec 9, 2024
commit 7ed489652d5463a5162956253b189879c885bb2f
44 changes: 19 additions & 25 deletions mover/yanugto/code/task4/flip_coin/sources/flip_coin.move
Original file line number Diff line number Diff line change
@@ -33,42 +33,36 @@ module flip_coin::flip_coin {
transfer(admin_cap,ctx.sender());
}

/*
entry fun play(game: &mut Game,in: bool, in_coin: Coin<SUI>, _: &mut TxContext) {
// win
//let in_balance = coin::into_balance(in_coin);
entry fun play(game: &mut Game, rand: &Random, in: bool, in_coin: Coin<SUI>, ctx: &mut TxContext) {
// Check if the game has enough balance to play
assert!(game.balance.value() >= in_coin.value(), 0x0001);

let out_balance = game.balance.split(in_coin.value());
let out_coin = coin::from_balance(out_balance,_);
public_transfer(out_coin,_.sender());
public_transfer(in_coin,_.sender());
// Random number generator
let mut gen = random::new_generator(rand, ctx);
let random_result = random::generate_bool(&mut gen);

// not win
//let in_balance = coin::into_balance(in_amount);
//game.balance.join(in_balance);
// If the random number is true, the player wins
if (random_result == in) {
// Transfer the winnings to the player
let winnings = game.balance.split(in_coin.value());
public_transfer(coin::from_balance(winnings, ctx), ctx.sender());

}
*/
entry fun play(game: &mut Game,rand: &Random,in: bool, in_coin: Coin<SUI>, ctx: &mut TxContext) {
let in_balance = coin::into_balance(in_coin);
game.balance.join(in_balance);
let gen = random::new_generator(rand,ctx);
if random::gen_u64(&mut gen) % 2 == 0 {
public_transfer(in_coin,_.sender());
// Return the coin to the player
public_transfer(in_coin, ctx.sender());
} else {
public_transfer(game.balance.split(in_coin.value()),_.sender());
// Deposit the coin into the game when the player loses
game.balance.join(coin::into_balance(in_coin));
}

}

public fun admin_withdraw(game: &mut Game, admin_cap: &mut AdminCap, _: &mut TxContext) {
transfer(game,_.sender());
transfer(admin_cap,_.sender());
public fun admin_withdraw(admin_cap: &mut AdminCap, amount: u64, game: &mut Game, ctx: &mut TxContext) {
let withdraw_balance = balance::split(&mut game.balance, amount);
let withdraw_coin = coin::from_balance(withdraw_balance, ctx);
public_transfer(withdraw_coin, ctx.sender());
}

public fun admin_deposit(game: &mut Game, in_coin: Coin<SUI>, _: &mut TxContext) {
game.balance.join(coin::into_balance(in_coin));
public_transfer(in_coin,_.sender());
}

}