forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Example/CLI] Tic-tac-toe (MystenLabs#18557)
## Description Write a CLI to talk to the shared and multi-sig tic-tac-toe example Move packages. ## Test plan Manual testing: ``` $ tic-tac-toe view 0xab06bb92cc1e6c95f9e6eed7e6c01fade6bb5eaa241e2100eb54d914597295ed X | O | ---+---+--- O | X | ---+---+--- | | X X: <address> -> O: <address> GAME: 0xab06bb92cc1e6c95f9e6eed7e6c01fade6bb5eaa241e2100eb54d914597295ed ADMIN: <public key> X WINS! ``` Also, confirmed that the CLI works with the React front-end. ## Stack - MystenLabs#18525 - MystenLabs#18526 --- ## Release notes Check each box that your changes affect. If none of the boxes relate to your changes, release notes aren't required. For each box you select, include information after the relevant heading that describes the impact of your changes that a user might notice and any actions they must take to implement updates. - [ ] Protocol: - [ ] Nodes (Validators and Full nodes): - [ ] Indexer: - [ ] JSON-RPC: - [ ] GraphQL: - [ ] CLI: - [ ] Rust SDK:
- Loading branch information
Showing
12 changed files
with
1,226 additions
and
3 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
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,2 @@ | ||
cli/localnet.env | ||
cli/Cargo.lock |
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 @@ | ||
[package] | ||
name = "tic-tac-toe" | ||
edition = "2021" | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
anyhow = "1.0.86" | ||
bcs = "0.1.6" | ||
clap = { version = "4.5", features = ["derive", "env", "wrap_help"] } | ||
dirs = "5.0.1" | ||
fastcrypto = "0.1.8" | ||
move-core-types = { path = "../../../external-crates/move/crates/move-core-types" } | ||
serde = { version = "1.0.203", features = ["derive"] } | ||
shared-crypto = { path = "../../../crates/shared-crypto" } | ||
sui-keys = { path = "../../../crates/sui-keys" } | ||
sui-sdk = { path = "../../../crates/sui-sdk" } | ||
sui-types = { path = "../../../crates/sui-types" } | ||
tokio = { version = "1.38.0", features = ["time"] } |
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,80 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use serde::Deserialize; | ||
use std::fmt; | ||
use sui_types::base_types::{ObjectID, SuiAddress}; | ||
|
||
#[derive(Deserialize)] | ||
pub(crate) struct Board { | ||
pub id: ObjectID, | ||
pub marks: Vec<u8>, | ||
pub turn: u8, | ||
pub x: SuiAddress, | ||
pub o: SuiAddress, | ||
} | ||
|
||
#[derive(Eq, PartialEq)] | ||
pub(crate) enum Player { | ||
X, | ||
O, | ||
} | ||
|
||
impl Board { | ||
pub(crate) fn next_player(&self) -> Player { | ||
if self.turn % 2 == 0 { | ||
Player::X | ||
} else { | ||
Player::O | ||
} | ||
} | ||
|
||
pub(crate) fn prev_player(&self) -> Player { | ||
if self.turn % 2 == 0 { | ||
Player::O | ||
} else { | ||
Player::X | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for Board { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let m = |i: usize| match self.marks[i] { | ||
0 => ' ', | ||
1 => 'X', | ||
2 => 'O', | ||
_ => unreachable!(), | ||
}; | ||
|
||
writeln!(f, "{: >31} {} | {} | {}", ' ', m(0), m(1), m(2))?; | ||
writeln!(f, "{: >31}---+---+---", ' ')?; | ||
writeln!(f, "{: >31} {} | {} | {}", ' ', m(3), m(4), m(5))?; | ||
writeln!(f, "{: >31}---+---+---", ' ')?; | ||
writeln!(f, "{: >31} {} | {} | {}", ' ', m(6), m(7), m(8))?; | ||
writeln!(f)?; | ||
|
||
use Player as P; | ||
let next = self.next_player(); | ||
|
||
write!(f, "{}", if next == P::X { " -> " } else { " " })?; | ||
writeln!(f, "X: {}", self.x)?; | ||
|
||
write!(f, "{}", if next == P::O { " -> " } else { " " })?; | ||
writeln!(f, "O: {}", self.o)?; | ||
|
||
let with_prefix = true; | ||
write!(f, " GAME: {}", self.id.to_canonical_display(with_prefix))?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl fmt::Display for Player { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Player::X => write!(f, "X"), | ||
Player::O => write!(f, "O"), | ||
} | ||
} | ||
} |
Oops, something went wrong.