Skip to content

Commit

Permalink
feat: pebbles game
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqubits committed Jun 23, 2024
1 parent 0914959 commit 5ebee52
Show file tree
Hide file tree
Showing 14 changed files with 547 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
pull_request:
branches: [main]
branches: [dev]
push:
branches: [main]
branches: [dev]

env:
CARGO_TERM_COLOR: always
Expand Down
Binary file added 17191623856201.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ version = "0.1.0"
edition = "2021"

[dependencies]
gstd = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
gstd = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1",features = ["debug"] }
pebbles-game-io.path = "io"

[build-dependencies]
gear-wasm-builder = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
pebbles-game-io.path = "io"

[dev-dependencies]
gstd = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
gstd = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1", features = ["debug"] }
gtest = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
pebbles-game-io.path = "io"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ pebbles-game

The `pebbles-game-io` will contains type definitions for input, output, and internal state data.

https://idea.gear-tech.io/programs/0x849054a5c3fa3dcce136ed3196634d1af055217df03324ba2a0a72756479012a?node=wss%3A%2F%2Ftestnet.vara.network



![Alt text](17191623856201.jpg)
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ use pebbles_game_io::PebblesMetadata;

fn main() {
gear_wasm_builder::build_with_metadata::<PebblesMetadata>();
}
}
1 change: 1 addition & 0 deletions io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ gstd = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
gmeta = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
parity-scale-codec = { version = "3", default-features = false }
scale-info = { version = "2", default-features = false }
enum-iterator = "2.1.0"
16 changes: 5 additions & 11 deletions io/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_std]

use gmeta::{In, InOut, Out, Metadata};
use gmeta::{In, InOut, Metadata, Out};
use gstd::prelude::*;

// the metadata to be used by the [IDEA](https://idea.gear-tech.io/programs?node=wss%3A%2F%2Ftestnet.vara.network) portal.
Expand All @@ -15,8 +15,6 @@ impl Metadata for PebblesMetadata {
type Signal = ();
}

// When initialising the game, it is necessary to pass some initial information.
// For example, the number of pebbles (N), maximum pebbles to be removed per turn (K), difficulty level.
#[derive(Debug, Default, Clone, Encode, Decode, TypeInfo)]
pub struct PebblesInit {
pub difficulty: DifficultyLevel,
Expand All @@ -31,9 +29,6 @@ pub enum DifficultyLevel {
Hard,
}

// It needs to send actions message for every User's move and receive some event from the program.
// The action can be a turn with some count of pebbles to be removed or the give up.
// Also, there is a restart action than resets the game state .
#[derive(Debug, Clone, Encode, Decode, TypeInfo)]
pub enum PebblesAction {
Turn(u32),
Expand All @@ -45,24 +40,23 @@ pub enum PebblesAction {
},
}

// And the event reflects the game state after the User's move:
// either pebbles count removed by the Program or the end of game with the information about the winner.
#[derive(Debug, Clone, Encode, Decode, TypeInfo, PartialEq, Eq)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum PebblesEvent {
CounterTurn(u32),
Won(Player),
}

#[derive(Debug, Default, Clone, Encode, Decode, TypeInfo, PartialEq, Eq)]
#[codec(crate = gstd::codec)]
#[scale_info(crate = gstd::scale_info)]
pub enum Player {
#[default]
User,
Program,
}

// Internal game state should keep all information related to the current state of the game.
// Some information is set during initialization, the first player is chosen randomly,
// some data are change during the game.
#[derive(Debug, Default, Clone, Encode, Decode, TypeInfo)]
pub struct GameState {
pub pebbles_count: u32,
Expand Down
13 changes: 13 additions & 0 deletions pebbles-state/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "pebbles-game-state"
version = "0.1.0"
edition = "2021"

[dependencies]
gstd = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1",features = ["debug"] }
gmeta = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
pebbles-game-io.path = "io"

[build-dependencies]
gear-wasm-builder = { git = "https://github.com/gear-tech/gear.git", tag = "v1.4.1" }
pebbles-game-io.path = "io"
3 changes: 3 additions & 0 deletions pebbles-state/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
gear_wasm_builder::build_metawasm();
}
42 changes: 42 additions & 0 deletions pebbles-state/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[no_std]

use gmeta::metawasm;
use gstd::{exec, prelude::*};
use pebbles_game_io::*;

/// customsize game state function
#[metawasm]
pub mod metafns {
pub type State = GameState;

pub fn game_state(state: State) -> State {
State {
pebbles_count: state.pebbles_count,
max_pebbles_per_turn: state.max_pebbles_per_turn,
pebbles_remaining: state.pebbles_remaining,
difficulty: state.difficulty,
first_player: state.first_player,
winner: state.winner,
}
}

pub fn pebbles_count(state: State) -> u32 {
state.pebbles_count
}
pub fn max_pebbles_per_turn(state: State) -> u32 {
state.max_pebbles_per_turn
}
pub fn pebbles_remaining(state: State) -> u32 {
state.pebbles_remaining
}
pub fn get_difficulty(state: State) -> DifficultyLevel {
state.difficulty
}
pub fn first_player(state: State) -> Player {
state.first_player
}
pub fn get_winner(state: State) -> Player {
state.winner
}

}
Loading

0 comments on commit 5ebee52

Please sign in to comment.