Code Monkey home page Code Monkey logo

blackjack-sui's Introduction

BlackJack with Sui and Move

This repo contains code for the BlackJack on Sui Blockchain game, built with Move.

Sui Blackjack Modules

single_player_blackjack.move

Defines the game object and provides methods to create and play a game. The overall flow of invocation is:

  • Admin (owner of the HouseCap object) invokes the initialize_house_data function that can be called only once (Capability is destroyed).
  • Player invokes the place_bet_and_create_game Move function that initializes the game randomness, receives the bet and creates a new game.
  • Dealer invokes the first_deal Move function that performs the initial deal of the game.
  • Player invokes the do_hit Move function that records the player's intent to hit.
  • Dealer invokes the hit Move function that performs the hit action.
  • Player invokes the do_stand Move function that records the player's intent to stand.
  • Dealer invokes the stand Move function that performs the stand action.

counter_nft.move

This module defines the Counter NFT object and provides methods to create and increment it.
The Counter NFT is used as the VRF input for every game that a player plays.
The count always increases after use, ensuring a unique input for every game.
A player is required to create a Counter NFT before playing their first game.
The UI can seemlessly create the Counter NFT for the user by including the counter creation along with the game creation function in the same PTB.

Gameplay

  • This is a 1-1 version of the game, where the player plays against the dealer (machine).
  • Dealer has a public BLS key.
  • Player creates randomness by moving their mouse over screen, places bet and starts game.
  • Dealer backend listens to the start transaction, signs and distributes 2 cards for player and 1 for dealer.
  • Player can Hit or Stand
    • If Stand is selected, then the Dealer draws cards until reaching a sum >= 17.
      • If Dealer reaches a number >= 17, he Stops. Then the Smart Contract compares sums and declares the winner.
    • If Hit is selected, then Dealer draws a card for the player.
  • For Frontend-Backend communication, a custom websocket server is used.
  • Every action (Deal, Hit, Stand) is comprised from 2 distinct transactions. The first one is initiated from the player in order to capture the intent of the user to perform an action. The second one is initiated from the dealer backend, as a response action to the first one and performs the actual business logic.

More details are depicted on the Game Flow section below.

Stake is fixed at 0.2 SUI

Game Flow

The overall game flow is presented in the following sequence diagram:

Sequence Diagram

Source Code Directories structure

  • move:

    • Contains the Move code of the smart contracts
    • Contains a move package named blackjack which contains the Move code of the smart contracts.
  • app

    • Contains The frontend code of the app.
      • React Framework
      • Next.js
      • Tailwind CSS
  • setup

    • A Typescript project, with a ready-to-use:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.