[Library] nChess - Control (Windows x86 & x64)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

[Library] nChess - Control (Windows x86 & x64)

Post by Mijikai »

nChess is basically a chess Gagdet 8)

Image

It is easy to use, sizeable and looks ok thanks to the great chess set by Cburnett.
You can create a board of any size (square) flip it and decide which player has the move.
Or make a board that only a computer can move (block user input).
Pieces and the active Player can be set freely at any time there is also a freemode where all pieces can be moved anywhere.
The representation of the whole board is stored in a buffer that can be accessed.
The board is handled completely automatically and all chess moves (unless in freemode) are checked - no invalid moves are possible.
All special moves are handled like promotion (with piece selection) or casteling.

I coded this a few years back mainly to watch multiple chess games or to play on one computer.
Its meant to be straight forward and accessible so some things are not included like a clock, move log, field notations or a evaluation scale.

Let me know what you think :)

Code Example

Include:

Code: Select all

EnableExplicit

;NCHESS CONTROL
;Author:  Mijikai
;Version: Alpha
;License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)

;Uses the Wikipedia Chess Pieces
;Link:    https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces#/media/File:Chess_Pieces_Sprite.svg
;Author:  Cburnett
;License: Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)

Enumeration CHESS_PIECE
  #CHESS_PIECE_NONE
  #CHESS_PIECE_PAWN
  #CHESS_PIECE_KNIGHT
  #CHESS_PIECE_BISHOP
  #CHESS_PIECE_ROOK
  #CHESS_PIECE_QUEEN
  #CHESS_PIECE_KING
EndEnumeration

Enumeration CHESS_PLAYER
  #CHESS_PLAYER_NONE
  #CHESS_PLAYER_WHITE
  #CHESS_PLAYER_BLACK
EndEnumeration

#CHESS_VERSION = $0001

Structure CHESS_PIECE_STRUCT
  player.a
  piece.a
EndStructure

Structure CHESS_BOARD_STRUCT
  piece.CHESS_PIECE_STRUCT[64]
EndStructure

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
  Import "nchess32.lib"
    nChessCreate.i(hWnd.i,X.i,Y.i,Size.i,FlipBoard.i = #False,ActivePlayer.i = #CHESS_PLAYER_WHITE);create the chessboard
    nChessReset(nChess.i,FlipBoard.i = #False,ActivePlayer.i = #CHESS_PLAYER_WHITE)                ;resets the chess board (flip -> white or black on bottom)
    nChessRedraw.i(nChess.i)                                                                       ;redraw the board (only needed if there is some kind of overlay)
    nChessSetPlayer.i(nChess.i,ActivePlayer.i)                                                     ;set active player
    nChessSetPiece.i(nChess.i,X.i,Y.i,Player.i,Piece.i)                                            ;set a piece
    nChessFreeMode.i(nChess.i,FreeMode.i = #False)                                                 ;move all pieces freely
    nChessError.i(nChess.i)                                                                        ;has an error occured ? (internal rendering routines - should never happen)
    nChessBoard.i(nChess.i)                                                                        ;returns a pointer to a buffer that represents the current board in memory
    nChessDestroy.i(nChess.i)                                                                      ;removes the chessboard
    nChessVersion.i()
  EndImport
CompilerElse
  Import "nchess64.lib"
    nChessCreate.i(hWnd.i,X.i,Y.i,Size.i,FlipBoard.i = #False,ActivePlayer.i = #CHESS_PLAYER_WHITE);create the chessboard
    nChessReset(nChess.i,FlipBoard.i = #False,ActivePlayer.i = #CHESS_PLAYER_WHITE)                ;resets the chess board (flip -> white or black on bottom)
    nChessRedraw.i(nChess.i)                                                                       ;redraw the board (only needed if there is some kind of overlay)
    nChessSetPlayer.i(nChess.i,ActivePlayer.i)                                                     ;set active player
    nChessSetPiece.i(nChess.i,X.i,Y.i,Player.i,Piece.i)                                            ;set a piece
    nChessFreeMode.i(nChess.i,FreeMode.i = #False)                                                 ;move all pieces freely
    nChessError.i(nChess.i)                                                                        ;has an error occured ? (internal rendering routines - should never happen)
    nChessBoard.i(nChess.i)                                                                        ;returns a pointer to a buffer that represents the current board in memory
    nChessDestroy.i(nChess.i)                                                                      ;removes the chessboard
    nChessVersion.i()
  EndImport
CompilerEndIf
Example (same as screenshot):

Code: Select all


EnableExplicit

XIncludeFile "nchess.pbi"

Procedure.i Main()
  Protected b1.i
  Protected b2.i
  Protected b3.i
  Protected *board.CHESS_BOARD_STRUCT
  If OpenWindow(0,0,0,640,440,"nChess Example",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
    If nChessVersion() = #CHESS_VERSION
      b1 = nChessCreate(WindowID(0),10,10,410);<- chess board normal
      b2 = nChessCreate(WindowID(0),430,10,200,#False,#CHESS_PLAYER_NONE);<- chess board not playable
      b3 = nChessCreate(WindowID(0),430,220,200,#True);<- chess board flipped
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Break
        EndSelect
      ForEver
    EndIf
    CloseWindow(0)  
  EndIf  
  ProcedureReturn #Null
EndProcedure

Main()

End
Download nChess Alpha:
https://www.dropbox.com/s/kyvug8bh6zvz8 ... a.zip?dl=0