diff --git a/mover/dyingforge/code/task4/guess_dice/Move.lock b/mover/dyingforge/code/task4/guess_dice/Move.lock new file mode 100644 index 000000000..1e2383048 --- /dev/null +++ b/mover/dyingforge/code/task4/guess_dice/Move.lock @@ -0,0 +1,49 @@ +# @generated by Move, please check-in and do not edit manually. + +[move] +version = 3 +manifest_digest = "FD01B187C8C9D6B2D76FCD572FB9AE44B9ABB1F7A415E0BFC56A08244C9E58D4" +deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600" +dependencies = [ + { id = "Sui", name = "Sui" }, + { id = "task2", name = "task2" }, +] + +[[move.package]] +id = "MoveStdlib" +source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" } + +[[move.package]] +id = "Sui" +source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" } + +dependencies = [ + { id = "MoveStdlib", name = "MoveStdlib" }, +] + +[[move.package]] +id = "task2" +source = { local = "/home/dyingforge/letsmove/mover/dyingforge/code/task2" } + +dependencies = [ + { id = "Sui", name = "Sui" }, +] + +[move.toolchain-version] +compiler-version = "1.37.3" +edition = "2024.beta" +flavor = "sui" + +[env] + +[env.testnet] +chain-id = "4c78adac" +original-published-id = "0x13e34ba3dcfb7ca0941d6b21c31831ca915f038619c39facd260c9c24936b352" +latest-published-id = "0x13e34ba3dcfb7ca0941d6b21c31831ca915f038619c39facd260c9c24936b352" +published-version = "1" + +[env.mainnet] +chain-id = "35834a8a" +original-published-id = "0x2c3faadfbc7176762d563ffbb1375c7d5459b408aa5043b3b4ab7bd22c6b58a7" +latest-published-id = "0x2c3faadfbc7176762d563ffbb1375c7d5459b408aa5043b3b4ab7bd22c6b58a7" +published-version = "1" diff --git a/mover/dyingforge/code/task4/guess_dice/Move.toml b/mover/dyingforge/code/task4/guess_dice/Move.toml new file mode 100644 index 000000000..590bbc6fc --- /dev/null +++ b/mover/dyingforge/code/task4/guess_dice/Move.toml @@ -0,0 +1,37 @@ +[package] +name = "guess_dice" +edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move +# license = "" # e.g., "MIT", "GPL", "Apache 2.0" +# authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"] + +[dependencies] +Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } +task2 ={local = "/home/dyingforge/letsmove/mover/dyingforge/code/task2"} +# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`. +# Revision can be a branch, a tag, and a commit hash. +# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" } + +# For local dependencies use `local = path`. Path is relative to the package root +# Local = { local = "../path/to" } + +# To resolve a version conflict and force a specific version for dependency +# override use `override = true` +# Override = { local = "../conflicting/version", override = true } + +[addresses] +guess_dice = "0x0" + +# Named addresses will be accessible in Move as `@name`. They're also exported: +# for example, `std = "0x1"` is exported by the Standard Library. +# alice = "0xA11CE" + +[dev-dependencies] +# The dev-dependencies section allows overriding dependencies for `--test` and +# `--dev` modes. You can introduce test-only dependencies here. +# Local = { local = "../path/to/dev-build" } + +[dev-addresses] +# The dev-addresses section allows overwriting named addresses for the `--test` +# and `--dev` modes. +# alice = "0xB0B" + diff --git a/mover/dyingforge/code/task4/guess_dice/sources/guess_dice.move b/mover/dyingforge/code/task4/guess_dice/sources/guess_dice.move new file mode 100644 index 000000000..3049a155d --- /dev/null +++ b/mover/dyingforge/code/task4/guess_dice/sources/guess_dice.move @@ -0,0 +1,81 @@ +/* +/// Module: guess_dice +module guess_dice::guess_dice; +*/ +/* +/// Module: guess_number +module guess_number::guess_number; +*/ +module guess_dice::guess_dice; + +use sui::balance::{Self, Balance}; +use sui::coin::{Self, Coin}; +use sui::random::{Self, Random}; +use sui::transfer::{public_transfer, share_object, transfer}; +use task2::my_token_faucet::MY_TOKEN_FAUCET; + +const INERROR: u64 = 0x2222; +const GUESSERROR: u64 = 0x111; + +public struct GuessDice has key { + id: UID, + amt: Balance, +} + +public struct AdminCap has key { + id: UID, +} + +//游戏初始化 +fun init(ctx: &mut TxContext) { + let game = GuessDice { id: object::new(ctx), amt: balance::zero() }; + let admin = AdminCap { id: object::new(ctx) }; + share_object(game); + transfer(admin, ctx.sender()); +} + +entry fun play( + guess: u8, + in_coin: Coin, + rand: &Random, + game: &mut GuessDice, + ctx: &mut TxContext, +) { + //设定投入资金不能超过游戏池的一半 + let in_value = in_coin.value(); + let game_value = game.amt.value(); + assert!(game_value>=in_value*2, INERROR); + + //随机数,设置筛子的大小 + let min: u8 = 1; + let max: u8 = 6; + let mut gen = random::new_generator(rand, ctx); + let dice = random::generate_u8_in_range(&mut gen, min, max); + + //设定猜测的数字在1——6之间 + assert!(guess>6, GUESSERROR); + + //游戏规则,如果猜测正确,则带走与投入金额相同的SUI,错误则没收 + if (guess == dice) { + let out_balance = game.amt.split(in_value); + let out_coin = out_balance.into_coin(ctx); + public_transfer(in_coin, ctx.sender()); + public_transfer(out_coin, ctx.sender()); + } else { + let in_bal = coin::into_balance(in_coin); + game.amt.join(in_bal); + }; +} + +//向游戏池里面加资金 +public entry fun add(in_coin: Coin, game: &mut GuessDice, _: &mut TxContext) { + let in_balance = coin::into_balance(in_coin); + game.amt.join(in_balance); +} + +//转移游戏池资金到自己账户 +public entry fun remove(_: &AdminCap, amt: u64, game: &mut GuessDice, ctx: &mut TxContext) { + let out_balance = game.amt.split(amt); + let out_coin = coin::from_balance(out_balance, ctx); + public_transfer(out_coin, ctx.sender()); +} diff --git a/mover/dyingforge/code/task4/guess_dice/tests/guess_dice_tests.move b/mover/dyingforge/code/task4/guess_dice/tests/guess_dice_tests.move new file mode 100644 index 000000000..3063a57ff --- /dev/null +++ b/mover/dyingforge/code/task4/guess_dice/tests/guess_dice_tests.move @@ -0,0 +1,18 @@ +/* +#[test_only] +module guess_dice::guess_dice_tests; +// uncomment this line to import the module +// use guess_dice::guess_dice; + +const ENotImplemented: u64 = 0; + +#[test] +fun test_guess_dice() { + // pass +} + +#[test, expected_failure(abort_code = ::guess_dice::guess_dice_tests::ENotImplemented)] +fun test_guess_dice_fail() { + abort ENotImplemented +} +*/ diff --git a/mover/dyingforge/readme.md b/mover/dyingforge/readme.md index b4888c6f8..dd3e3de1c 100644 --- a/mover/dyingforge/readme.md +++ b/mover/dyingforge/readme.md @@ -33,10 +33,10 @@ ## 04 Move Game -- [] game package id : -- [] deposit Coin hash: -- [] withdraw `Coin` hash: -- [] play game hash: +- [x] game package id :0x2c3faadfbc7176762d563ffbb1375c7d5459b408aa5043b3b4ab7bd22c6b58a7 +- [x] deposit Coin hash:HP99MLdCsYNs12R3bmTy256yTQ7JmozqrMJ1TivjkGzJ +- [x] withdraw `Coin` hash:CkHyXx4oR7F1sF2T3iSBrgPSeWYkfB5tUxzx6aUgcSPt +- [x] play game hash:4aQJUJujGZLcTocCYdxsSbHNXRozaA75LhK4sMQ4qtu3 ## 05 Move Swap - [] swap package id :