Hex is a two player abstract strategy board game in which players attempt to connect opposite sides of a rhombus-shaped board made of hexagonal cells. Hex was invented by mathematician and poet Piet Hein in 1942 and later rediscovered and popularized by John Nash.
This contract implements hex-game backed by storage on NEAR blockchain.
Contract in src/lib.rs
provides methods to create new game, make an allowed move in one of already existing games or view information about game by index.
The project is divided into separate files, each file contains one of the structures, that is used to keep information about the game, and tests for this structure.
Deployed game contract in testnet: hex-game.klimoza.testnet
Creates new game with given parameters and returns index of created game. For example:
➜ near call hex-game.klimoza.testnet create_game '{"first_player": "crossword.klimoza.testnet", "second_player": "klimoza.testnet", "field_size": 2}' --accountId hex-game.klimoza.testnet --amount 2
Scheduling a call: hex-game.klimoza.testnet.create_game({"first_player": "crossword.klimoza.testnet", "second_player": "klimoza.testnet", "field_size": 2})
Doing account.functionCall()
Log [hex-game.klimoza.testnet]: Created board:
Log [hex-game.klimoza.testnet]: . .
Log [hex-game.klimoza.testnet]: . .
4
Tries to make a move in the game at the given index and returns Game if move is correct(panics otherwise). Used structures:
pub type GameIndex = u64;
pub enum MoveType {
PLACE,
SWAP,
}
pub struct Cell {
pub x: usize,
pub y: usize,
}
You can omit the cell
parameter if move_type
is SWAP
(i.e. applying swap rule on the current move). For example:
➜ near call hex-game.klimoza.testnet make_move '{"index": 4, "move_type": "SWAP"}' --accountId klimoza.testnet
Scheduling a call: hex-game.klimoza.testnet.make_move({"index": 4, "move_type": "SWAP"})
Doing account.functionCall()
Log [hex-game.klimoza.testnet]: Old board:
Log [hex-game.klimoza.testnet]: . R
Log [hex-game.klimoza.testnet]: . .
Log [hex-game.klimoza.testnet]: New board:
Log [hex-game.klimoza.testnet]: . .
Log [hex-game.klimoza.testnet]: B .
{
first_player: 'crossword.klimoza.testnet',
second_player: 'klimoza.testnet',
turn: 2,
board: { size: 2, field: 'IA==' },
current_block_height: 96244955,
prev_block_height: 96244934,
is_finished: false
}
Returns the game at the given index(if there is one). For example:
➜ near call hex-game.klimoza.testnet get_game '{"index": 4}' --accountId hex-game.klimoza.testnet
Scheduling a call: hex-game.klimoza.testnet.get_game({"index": 4})
Doing account.functionCall()
Log [hex-game.klimoza.testnet]: Game board:
Log [hex-game.klimoza.testnet]: R B
Log [hex-game.klimoza.testnet]: B .
{
first_player: 'crossword.klimoza.testnet',
second_player: 'klimoza.testnet',
turn: 4,
board: { size: 2, field: 'KQ==' },
current_block_height: 96244985,
prev_block_height: 96244971,
is_finished: true
}
Checks for a locked, expirable, active Roketo stream going from account_id
to hex_game_account
. Returns Promise. For example:
➜ near call wrap.testnet ft_transfer_call '{"receiver_id": "streaming-r-v2.dcversus.testnet", "amount": "2200000000000000000000000", "memo": "Roketo transfer", "msg": "{\"Create\":{\"request\":{\"balance\":\"2000000000000000000000000\", \"owner_id\": \"klimoza.testnet\",\"receiver_id\":\"hex-game.klimoza.testnet\",\"token_name\": \"wrap.testnet\", \"tokens_per_sec\":\"6666666666666666666667\", \"is_locked\": true, \"is_expirable\": true}}}"}' --accountId klimoza.testnet --depositYocto 1 --gas 200000000000000
Doing account.functionCall()
Log [wrap.testnet]: Transfer 2200000000000000000000000 from klimoza.testnet to streaming-r-v2.dcversus.testnet
Log [wrap.testnet]: Memo: Roketo transfer
Log [wrap.testnet]: Transfer 2100000000000000000000000 from streaming-r-v2.dcversus.testnet to finance-r-v2.dcversus.testnet
'2200000000000000000000000'
➜ near call hex-game.klimoza.testnet check_premium_account '{"account_id": "klimoza.testnet"}' --accountId klimoza.testnet
Scheduling a call: hex-game.klimoza.testnet.check_premium_account({"account_id": "klimoza.testnet"})
true
At the moment, the projects contains 33 tests, each of which lies in the file of the structure it is testing. You can run them all using following command:
cargo test
Alternatively, you can specify test group you want to run, for example:
cargo test cell_tests