SUI Move官方示例合约实践——游戏类:共享对象版井字游戏(shared_tic_tac_toe)
合约代码较长,以下仅摘取对外接口代码
create_game
)TicTacToe
游戏共享对象,指定游戏对战双方的地址 /// `x_address` and `o_address` are the account address of the two players.
public entry fun create_game(x_address: address, o_address: address, ctx: &mut TxContext) {
// TODO: Validate sender address, only GameAdmin can create games.
let id = object::new(ctx);
let gameboard = vector[
vector[MARK_EMPTY, MARK_EMPTY, MARK_EMPTY],
vector[MARK_EMPTY, MARK_EMPTY, MARK_EMPTY],
vector[MARK_EMPTY, MARK_EMPTY, MARK_EMPTY],
];
let game = TicTacToe {
id,
gameboard,
cur_turn: 0,
game_status: IN_PROGRESS,
x_address: x_address,
o_address: o_address,
};
// Make the game a shared object so that both players can mutate it.
transfer::share_object(game);
}
place_mark
)GameEndEvent
,用于删除游戏 public entry fun place_mark(game: &mut TicTacToe, row: u8, col: u8, ctx: &mut TxContext) {
assert!(row < 3 && col < 3, EInvalidLocation);
assert!(game.game_status == IN_PROGRESS, EGameEnded);
let addr = get_cur_turn_address(game);
assert!(addr == tx_context::sender(ctx), EInvalidTurn);
let cell = vector::borrow_mut(vector::borrow_mut(&mut game.gameboard, (row as u64)), (col as u64));
assert!(*cell == MARK_EMPTY, ECellOccupied);
*cell = game.cur_turn % 2;
update_winner(game);
game.cur_turn = game.cur_turn + 1;
if (game.game_status != IN_PROGRESS) {
// Notify the server that the game ended so that it can delete the game.
event::emit(GameEndEvent { game_id: object::id(game) });
if (game.game_status == X_WIN) {
transfer::transfer(Trophy { id: object::new(ctx) }, game.x_address);
} else if (game.game_status == O_WIN) {
transfer::transfer(Trophy { id: object::new(ctx) }, game.o_address);
}
}
}
delete_game
) public entry fun delete_game(game: TicTacToe) {
let TicTacToe { id, gameboard: _, cur_turn: _, game_status: _, x_address: _, o_address: _ } = game;
object::delete(id);
}
别名 | 地址 | 角色 |
---|---|---|
Jason | 0x5c5882d73a6e5b6ea1743fb028eff5e0d7cc8b7ae123d27856c5fe666d91569a |
游戏创建者(只创建不管事) |
Alice | 0x2d178b9704706393d2630fe6cf9415c2c50b181e9e3c7a977237bb2929f82d19 |
玩家X |
Bob | 0xf2e6ffef7d0543e258d4c47a53d6fa9872de4630cc186950accbd83415b009f0 |
玩家O |
export JASON=0x5c5882d73a6e5b6ea1743fb028eff5e0d7cc8b7ae123d27856c5fe666d91569a
export ALICE=0x2d178b9704706393d2630fe6cf9415c2c50b181e9e3c7a977237bb2929f82d19
export BOB=0xf2e6ffef7d0543e258d4c47a53d6fa9872de4630cc186950accbd83415b009f0
切换到Jason账号
sui client publish --gas-budget 100000000
export PACKAGE_ID=0x33052eb50ba64ffab60c0f717766c8504a8221bc492807b4fc8f7ab37ffdecdb
create_game
)切换到Jason
sui client call --function create_game --package $PACKAGE_ID --module shared_tic_tac_toe --args $ALICE $BOB --gas-budget 10000000
# PACKAGE_ID::shared_tic_tac_toe::TicTacToe
export GAME=0x24e1d1c7c60ccce6ef86670e01f32cc1ba0bfd1db21c3d3c5644dcc2846d83c1
sui client object $GAME
place_mark
)切换到Alice
sui client call --function place_mark --package $PACKAGE_ID --module shared_tic_tac_toe --args $GAME 0 0 --gas-budget 10000000
$ sui client object $GAME
place_mark
)切换到Bob
sui client call --function place_mark --package $PACKAGE_ID --module shared_tic_tac_toe --args $GAME 1 2 --gas-budget 10000000
$ sui client object $GAME
place_mark
)切换到Alice
sui client call --function place_mark --package $PACKAGE_ID --module shared_tic_tac_toe --args $GAME 1 1 --gas-budget 10000000
$ sui client object $GAME
place_mark
)切换到Bob
sui client call --function place_mark --package $PACKAGE_ID --module shared_tic_tac_toe --args $GAME 1 0 --gas-budget 10000000
$ sui client object $GAME
切换到Alice
sui client call --function place_mark --package $PACKAGE_ID --module shared_tic_tac_toe --args $GAME 2 2 --gas-budget 10000000
sui client object $GAME
delete_game
)当前实现没有权限控制,任何人可以删除游戏(可完善)
sui client call --function delete_game --package $PACKAGE_ID --module shared_tic_tac_toe --args $GAME --gas-budget 10000000
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!