-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2014 from dyingforge/task4
Task4
- Loading branch information
Showing
5 changed files
with
189 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ([email protected])", "John Snow ([email protected])"] | ||
|
||
[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" | ||
|
81 changes: 81 additions & 0 deletions
81
mover/dyingforge/code/task4/guess_dice/sources/guess_dice.move
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MY_TOKEN_FAUCET>, | ||
} | ||
|
||
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<MY_TOKEN_FAUCET>, | ||
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<MY_TOKEN_FAUCET>, 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()); | ||
} |
18 changes: 18 additions & 0 deletions
18
mover/dyingforge/code/task4/guess_dice/tests/guess_dice_tests.move
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
*/ |
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