0
#![cfg_attr(not(feature = "std"), no_std, no_main)]

// use ink_lang as ink;

#[ink::contract]
mod memory_game {
    use ink::{
        prelude::vec::Vec
    };
    #[ink(storage)]
    pub struct MemoryGame {
        tiles: Vec<(TileContent, TileState)>,
        try_count: u32,
        best_count: u32,
    }

    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
     #[ink::scale_derive(Encode, Decode, TypeInfo)]
    pub enum TileContent {
        Hearts,
        WaterDrop,
        DiceSixFacesFive,
        Umbrella,
        Cube,
        BeachBall,
        Dragonfly,
        Hummingbird,
        FlowerEmblem,
        OpenBook,
    }

    #[derive(Debug, PartialEq, Eq, Clone, Copy)]
    pub enum TileState {
        Start,
        Flipped,
        Matched,
    }

    impl MemoryGame {
        #[ink(constructor)]
        pub fn new() -> Self {
            MemoryGame {
                tiles: Vec::new(),
                try_count: 0,
                best_count: 0,
            }
        }

        #[ink(message)]
        pub fn get_tiles(&self, tile_count: u32) -> Vec<(TileContent, TileState)> {
            // Throw error if count is not even.
            assert!(tile_count % 2 == 0, "The number of tiles must be even.");

            // Use the existing list if it exists.
            if !self.tiles.is_empty() {
                return self.tiles.clone();
            }

            let pair_count = tile_count / 2;

            // Take only the items we need from the list of possibilities.
            let used_tile_contents = [
                TileContent::Hearts,
                TileContent::WaterDrop,
                TileContent::DiceSixFacesFive,
                TileContent::Umbrella,
                TileContent::Cube,
                TileContent::BeachBall,
                TileContent::Dragonfly,
                TileContent::Hummingbird,
                TileContent::FlowerEmblem,
                TileContent::OpenBook,
            ];

            // Double the array and shuffle it.
            // let mut rng = ink_env::hash::Sha2_256::default();
            let mut shuffled_contents = used_tile_contents
                .iter()
                .cycle()
                .take(pair_count as usize * 2)
                .copied()
                .collect::<Vec<_>>();
            // shuffled_contents.shuffle(&mut rng);

            self.tiles = shuffled_contents
                .into_iter()
                .map(|content| (content, TileState::Start))
                .collect();

            self.tiles.clone()
        }

        #[ink(message)]
        pub fn flip(&mut self, index: u32) {
            // Is the tile already flipped? We don’t allow flipping it back.
            if self.tiles[index as usize].1 == TileState::Flipped {
                return;
            }

            // How many tiles are currently flipped?
            let flipped_tiles = self
                .tiles
                .iter()
                .filter(|(_, state)| *state == TileState::Flipped)
                .count();

            // Don't allow more than 2 tiles to be flipped at once.
            if flipped_tiles == 2 {
                return;
            }

            // On the second flip, check if the tiles match.
            if flipped_tiles == 1 {
                self.try_count += 1;

                let already_flipped_tile = self
                    .tiles
                    .iter()
                    .find(|(_, state)| *state == TileState::Flipped)
                    .unwrap()
                    .0;
                let just_flipped_tile = self.tiles[index as usize].0;

                let new_state = if already_flipped_tile == just_flipped_tile {
                    TileState::Matched
                } else {
                    TileState::Start
                };

                // If all tiles are matched, the game is over.
                if self
                    .tiles
                    .iter()
                    .all(|(_, state)| *state == TileState::Matched)
                {
                    self.end();
                }

                self.tiles.iter_mut().for_each(|(_, state)| {
                    if *state == TileState::Flipped {
                        *state = new_state;
                    }
                });
            }

            self.tiles[index as usize].1 = TileState::Flipped;
        }

        fn end(&mut self) {
            // Your end logic goes here
        }
    }
}

while trying to deploy the ink smart contract i get this error,I don't have an idea how to fix it

12 |         tiles: Vec<(TileContent, TileState)>,
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ink::parity_scale_codec::Decode` is not implemented for `Vec<(TileContent, TileState)>`
   |
   = help: the trait `ink::parity_scale_codec::Decode` is implemented for `Vec<T>`
   = note: required for `Vec<(TileContent, TileState)>` to implement `Packed`
   = note: required for `Vec<(TileContent, TileState)>` to implement `StorableHint<()>`
   = note: required for `Vec<(TileContent, TileState)>` to implement `AutoStorableHint<ManualKey<760146325>>`

error[E0277]: the trait bound `[(TileContent, TileState)]: Encode` is not satisfied
  --> \\?\C:\Users\Admin\Documents\mightymeld-memory\contracts\tastymatch\lib.rs:12:16
   |
12 |         tiles: Vec<(TileContent, TileState)>,
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode` is not implemented for `[(TileContent, TileState)]`
   |
   = help: the following other types implement trait `Encode`:
             [T; N]
             [T]
   = note: required for `Vec<(TileContent, TileState)>` to implement `Encode`
   = note: required for `Vec<(TileContent, TileState)>` to implement `Packed`
   = note: required for `Vec<(TileContent, TileState)>` to implement `StorableHint<()>`
   = note: required for `Vec<(TileContent, TileState)>` to implement `AutoStorableHint<ManualKey<760146325>>`

error[E0277]: the trait bound `[(TileContent, TileState)]: Encode` is not satisfied
  --> \\?\C:\Users\Admin\Documents\mightymeld-memory\contracts\tastymatch\lib.rs:50:53
   |
50 |         pub fn get_tiles(&self, tile_count: u32) -> Vec<(TileContent, TileState)> {
   |                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode` is not implemented for `[(TileContent, TileState)]`
   |
   = help: the following other types implement trait `Encode`:
             [T; N]
             [T]
   = note: required for `Vec<(TileContent, TileState)>` to implement `Encode`
note: required by a bound in `DispatchOutput`
  --> C:\Users\Admin\.cargo\registry\src\index.crates.io-6f17d22bba15001f\ink-5.0.0-rc.3\src\codegen\dispatch\type_check.rs:69:8
   |
67 | pub struct DispatchOutput<T>(T)
   |            -------------- required by a bound in this struct
68 | where
69 |     T: scale::Encode + 'static;
   |        ^^^^^^^^^^^^^ required by this bound in `DispatchOutput`

1 Answer 1

0

you didn't derive Encode, Decode for TileState

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum TileState {
    Start,
    Flipped,
    Matched,
}
1
  • 1
    thank you solved
    – Davy King
    Commented Mar 12 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.