Code Monkey home page Code Monkey logo

react-native-chessboard's Introduction

πŸ‘‰πŸΌ React Native Chessboard

A lightweight, simple, and high-performing chessboard for React Native.

Deeply inspired by the Chess Youtube Episode from the series "Can it be done in React Native?" made by William Candillon.

Disclaimer

If you want this package in production, use it with caution.

Installation

You need to have already installed the following packages:

Open a Terminal in your project's folder and install the library using yarn:

yarn add react-native-chessboard

or with npm:

npm install react-native-chessboard

Usage

import Chessboard from 'react-native-chessboard';

const App = () => (
  <View
    style={{
        flex: 1, 
        alignItems: 'center',
        justifyContent: 'center'
    }}
  >
    <Chessboard/>
  </View>
);

Properties

gestureEnabled?: boolean

Enables gestures for chess pieces.

Default: true


fen?: string

Indicates the initial fen position of the chessboard.


withLetters?: boolean

Decides whether or not to show the letters on the bottom horizontal axis of the chessboard.

Default: true


withNumbers?: boolean

Decides whether or not to show the numbers on the left vertical axis of the chessboard.

Default: true


boardSize?: number

Indicates the chessboard width and height.

Default: Math.floor(SCREEN_WIDTH / 8) * 8


renderPiece?: (piece: PieceType) => React.ReactElement | null

It gives the possibility to customise the chessboard pieces.

In detail, it takes a PieceType as input, which is constructed as follows:

type Player = 'b' | 'w';
type Type = 'q' | 'r' | 'n' | 'b' | 'k' | 'p';
type PieceType = `${Player}${Type}`;

onMove?: (info: ChessMoveInfo) => void;

It's a particularly useful callback if you want to execute an instruction after a move.

import Chessboard from 'react-native-chessboard';

const App = () => (
  <View
    style={{
        flex: 1, 
        alignItems: 'center',
        justifyContent: 'center'
    }}
  >
    <Chessboard
        onMove={({ state }) => {
          if (state.in_checkmate) {
            console.log('Life goes on.');
          }
        }}
      />
  </View>
);

In detail, you can access these parameters:

  • in_check: boolean
  • in_checkmate: boolean
  • in_draw: boolean
  • in_stalemate: boolean
  • in_threefold_repetition: boolean
  • insufficient_material: boolean
  • game_over: boolean
  • fen: boolean

P.S: These parameters are the outputs given by the respective functions used by chess.js (on which the package is built).


colors?: ChessboardColorsType

Useful if you want to customise the default colors used in the chessboard.

Default:

  • black: '#62B1A8'
  • white: '#D9FDF8'
  • lastMoveHighlight: 'rgba(255,255,0, 0.5)'
  • checkmateHighlight: '#E84855'
  • promotionPieceButton: '#FF9B71'

durations?: { move?: number }

Useful if you want to customise the default durations used in the chessboard (in milliseconds).

Default:

  • move: 150

What if I want to move pieces around without gestures?

Fortunately, the package provides the possibility of passing a React Ref to the component.

The operations granted are:

move: ({ from: Square; to: Square; }) => Promise<Move | undefined> | undefined;

Very useful if you want to move the pieces on the chessboard programmatically.

import Chessboard, { ChessboardRef } from 'react-native-chessboard';

const App = () => {
  const chessboardRef = useRef<ChessboardRef>(null);

  useEffect(() => {
    (async () => {
      await chessboardRef.current?.move({ from: 'e2', to: 'e4' });
      await chessboardRef.current?.move({ from: 'e7', to: 'e5' });
      await chessboardRef.current?.move({ from: 'd1', to: 'f3' });
      await chessboardRef.current?.move({ from: 'a7', to: 'a6' });
      await chessboardRef.current?.move({ from: 'f1', to: 'c4' });
      await chessboardRef.current?.move({ from: 'a6', to: 'a5' });
      await chessboardRef.current?.move({ from: 'f3', to: 'f7' });
    })();
  }, []);

  return (
      <View
        style={{
            flex: 1, 
            alignItems: 'center',
            justifyContent: 'center'
        }}
    >
        <Chessboard
            ref={chessboardRef}
            durations={{ move: 1000 }}
        />
    </View>
  )
};

undo: () => void;

Undoes the last made move on the board. Can be done multiple times in a row as long as resetBoard is not called.


highlight: (_: { square: Square; color?: string }) => void;

Highlight a square on the chessboard. The default color is 'rgba(255,255,0, 0.5)'.


resetAllHighlightedSquares: () => void;


resetBoard: (fen?: string) => void;

Resets the chessboard from a fen position.


getState: () => ChessboardState;

Returns the current state of the chessboard (which can also be retrieved using the onMove callback).


Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

react-native-chessboard's People

Contributors

enzomanuelmangano avatar fireplank avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

react-native-chessboard's Issues

Chessboard unresponsive, most black pieces rendered incorrectly

Screenshot_2023-02-12-04-02-59-794_host exp exponent

App.js file below:

import React from 'react';
import { View } from 'react-native';
import Chessboard from 'react-native-chessboard';

export default function App() {
  return (
    <View
      style={{
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
      }}
    >
      <Chessboard />
    </View>
  )
};

allow same .move() interface as Chess.js provides (allow SAN)

Hi

first thanks for the great work.

As you're using Chess.js bts could you please enable the full .move() api (including SAN moves)

.move('e4') as a first move equals .move({ from: 'e2', to: 'e4' })

SAN is just more common for chess players, and the way I (and many) store the games/moves in the db.
Conversion also requires to always keep track of the board (e.g. 'e4' could be from 'e2' or from 'e3') so it feels like an additional redundant work, as Chess.js already provides all the possibilities. (Imagine to programmatically play down a PGN file, every move has to be converted while keeping track of the board at all time)

Thank you!

The player's position start with the black pieces

I have read about the documentation but I see that the current board does not allow players to start with black pieces, it would be great if you consider developing that functionality further.
Thank you.

I realized it can be done with fen, however if you could develop a simpler function to allow players to start with black position that would be awesome, btw thanks for the chessboard, and sorry for my bad english.

Way to flip the board depending on player color?

Is there a way to flip the board orientation depending if the player is 'white' or 'black'? I have been trying to use this library but I cannot find anything inbuilt for it, although it is so feature rich otherwise. I would love if someone could point me in the right direction in implementing it from the black POV.

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.