DegenZKhess
A provable chess game where you can assign unique tokens and NFTs to pieces, adding complexity to strategies for a more engaging and entertaining gameplay.
Screenshots






Problem Statement
Implementation of a provable game of chess where players have the opportunity, before starting the game, to assign NFTs and tokens to their chess pieces. When a piece with tokens is captured, the capturing player receives the associated tokens, adding a new layer of strategic complexity. The winner of the chess match gets a token prize. These extra dynamics can influence players’ moves, as decisions are now driven not only by traditional chess strategies but also by the potential to gain valuable tokens, making the game more engaging.
Solution
The chess game project is implemented using “dojoengine” in Cairo, with main contract/provable components detailed in 'board.cairo' and 'action.cairo' files. The front-end builds upon the boot-camp repository showcased during the Starthackvideo presentation for dojoengineand using the "@dojoengine/react-recs-core-utils" libraries (see theclient folder).The front-end is connects theCairo contractsdeployed to both local and publicly available Katana and Torii instances, utilizing Slot. Here the public RPC: https://api.cartridge.gg/x/degenchess/katanaThere are two main Cairo contracts:'board.cairo'and'actions.cairo'. The board contract contains all the required models. The ``'Board' model represents a game state (hardcoded with id 1), with variables indicating the players’ addresses, the current turn (a boolean, false for white and true for black), and whether the game has started (is_started), which means that the players have already assigned their tokens to the pieces, or if it has finished and who the winner is. The chessboard is modeled as a list of 'Cell' entities, where each cell’s value represents a chess piece usingFEN notation. The value field is a 'Type' enum where each item is a piece type according to the FEN notation, e.g., PAWN (capital letter for white) and bishop (lowercase for black pieces, for example, the black bishop). These representations make it easier to get the FEN string that can be used as input for chess engines and rendering libraries like the npm 'chess.js' and 'react-chessboard'. Finally, there are two simple implementations for 'ERC20' and 'ERC721' token models.The 'actions.cairo' contract is responsible for checking and updating the states of the models.The'spawn method',initializes the chessboard,allocates mock NFTsandtokensto the two players (black and white), and checks the game state—verifying if it has started, if tokens have been assigned to pieces, and if the game has concluded with a winner.The'ride_piece'and'assign_tokens'actionscan only be called if the chess game has not started and ensures that the tokens belong to the calling player, checking balances, and confirming the correct piece color for assigning tokens to pieces.Thestart_game methodis called after assigning tokens and allows the chess game to start.Themove_piece methodcan only be called after the game has started, checking if it is the calling player’s turn and enforcing traditional chess rules, such as a bishop moving diagonally or a knight in an L-shape (see cheks rules in the end of this section). Additionally, if a piece with tokens is captured, the assigned tokens are instantly transferred to the capturing player. Finally, it also checks if the captured piece was the king, and in that case,assigns 10,000 tokensto the winner and gives back the non-captured NFTs, and sets the game to finish.EXTRA INFOUseful queries to get the provable state of the game here in thisReadme sectionLonger video version with Graphql playground deep-dive :youtube URLThe shorter version within 4 minutes:youtube URLExample of allowed moves for a piece using the array (FEN) notation, where the entire chessboard is represented as an array. Please note that these checks have not been fully tested and require additional testing:♙Pawn allowed moves (p or P) with extra checks on color target pieces♞Knight (n or n) allowed moves♗Bishop (b or B) allowed moves♜Rook (r or R) allowed moves♕Queen (q or Q) allowed moves♚King (k or K) allowed moves
Hackathon
StarkHack
2024
Contributors
- GaetanoMondelli
49 contributions