Skip to content

Commit

Permalink
Add Chess console example, i write it
Browse files Browse the repository at this point in the history
  • Loading branch information
zadium committed Nov 10, 2023
1 parent 82d23ef commit daa02d4
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 5 deletions.
11 changes: 6 additions & 5 deletions ChessBoard.lsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@author: Zai Dium
@update: 2022-02-16
@version: 1.19
@revision: 803
@revision: 983
@localfile: ?defaultpath\Chess\[email protected]
@license: MIT
Expand Down Expand Up @@ -112,8 +112,9 @@ printBoard()
if (x != 0)
s += " ";
name = getBoardPlot(x, y);
if (name =="")
name = "-";
if (name =="" || name == " ")
name = ".";
name = llChar(0xFF21+llOrd(name, 0)-65);
s += name;
x++;
}
Expand Down Expand Up @@ -285,13 +286,13 @@ integer text_move(string msg)
if ((c == 4) || ((c == 5) && (llGetSubString(msg, 2, 2) == " ")))
{
integer i = 0;
integer x1 = llOrd(msg, i)-98;
integer x1 = llOrd(msg, i)-97;
i++;
integer y1 = llOrd(msg, i)-49;
if (c == 5)
i++;
i++;
integer x2 = llOrd(msg, i)-98;
integer x2 = llOrd(msg, i)-97;
i++;
integer y2 = llOrd(msg, i)-49;
if ((x1>=0) && (y1>=0) && (x2>=0) && (y2>=0))
Expand Down
127 changes: 127 additions & 0 deletions engine/chess_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
This Python script is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
You should have received a copy of the license along with this work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
License information:
- Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made.
- NonCommercial: You may not use this work for commercial purposes.
- ShareAlike: If you remix, transform, or build upon this work, you must distribute your contributions under the same license as the original.
"""
from Chessnut import Game
from Chessnut.game import InvalidMove
from colorama import init, Fore, Back, Style
from pystockfish import *

#init_fen = "rnbqkbnr/pp2pppp/8/3p4/8/5N2/PPPP1PPP/RNBQKB1R w KQkq - 0 1"
init_fen = "r1bqkbnr/p1pp1ppp/1pn5/4p3/2B1P3/5Q2/PPPP1PPP/RNB1K1NR w KQkq - 0 1"
## f3f7
deep = Engine(depth=6)

init(autoreset=True)

game = Game(init_fen)
#game.apply_move(move)

def print_fen():
global game
if game != None:
print(game.board)

def print_board():
global game

if game != None:
print("\n a b c d e f g h")
print(" –––––––––––––––––")
row = 8
print(str(row)+"|", end="")
for index in range(64):
piece = game.board.get_piece(index)
if index > 0 and index % 8 == 0:
print(" |"+str(row))
row = row-1
print(str(row)+"|", end="")
print(" "+piece, end='')
print(" |"+str(row))
print(" –––––––––––––––––")
print(" a b c d e f g h\n")
print_state()

def new_game():
global game
game = Game()
print(Fore.GREEN + "New game created")

def try_move(move):
global game, deep
if game == None:
new_game()
try:
game.apply_move(move)
except InvalidMove as err:
print("\n"+Fore.RED+"Invalid Move " + move+"\n")
return False
except Exception as err:
print("\n"+Fore.RED+"error:" + type(err).__name__ + ' message: ' + str(err).strip()+"\n")
return False

print(Fore.GREEN+"move: " + move)
return True

def print_state():
status = None
if game.status == 1:
status = Fore.RED+"Check"
elif game.status == 2:
status = Fore.RED+"Checkmate"
elif game.status == 3:
status = Fore.RED+"Stalemate"
if(game.status != 0):
print(status+"\n")

#print('\u2654')

while True:

if game != None:
if game.state.player == "b":
player = "Black "
else:
player = "White "
else:
player = ""
line = input(player + "Command » ");
params = line.split(" ")
cmd = params[0]
del params[0]
if cmd == "move":
try_move(params[0])
print_board()
elif cmd == "print":
if len(params)>0 and params[0] == "fen":
print(game)
print("")
else:
print_board()
elif cmd == "new":
new_game()
print_board()
elif cmd == "q" or cmd == "exit":
break
elif cmd == "":
pass
else:
if game.status < Game.CHECKMATE:
if try_move(cmd):
print_board ()
if game.status < Game.CHECKMATE:
fen = str(game)
deep.setfenposition(fen)
#print(deep.bestmove())
respond = deep.bestmove()
move = respond["move"]
try_move(move)

print_board()

0 comments on commit daa02d4

Please sign in to comment.