Jump to content

Chess sketch


ValeryVal
 Share

Recommended Posts

This is sketch of chess game script. Very beta - 1 hour developing, only...

AChessSketch.au3:

;==========================================
; AChessSketch (c) Valery Ivanov, 14 December 2007
;
; Chess desktop & figures to move

Global $DButton = 8

#include <GuiConstants.au3>
Global $Width, $Height, $h, $A = 40
Global $hWnd
Global $Current_Id, $Current_I, $Current_J
Global $Del_I, $Del_J
Global $Count

Global $Width = $DButton*$A, $Height = $Width
Global $ColorPassive = 0x843E28
Global $ColorActive = 0xDDBE1E

$Style = $WS_THICKFRAME + $WS_POPUP
$StyleEx = $WS_EX_OVERLAPPEDWINDOW

$hWnd = GuiCreate("AChessSketch (c) Valery Ivanov, 14 December 2007", $Width, $Height, (@DesktopWidth-$Width)/2, (@DesktopHeight-$Height)/2, $Style, $StyleEx)

GUISetFont(32)
GUISetBkColor (0xFFFFFF)
SetFigures()

GuiSetState()

While 1
 $Msg = GUIGetMsg(1)
 If $Msg[0] = $GUI_EVENT_CLOSE then exit 
 if $Msg[0] = $GUI_EVENT_MOUSEMOVE then MouseMoveHandler($Msg)
WEnd

EXIT

;--------------------------------
Func SetFigures()
local $i, $j, $X, $Y


For $i = 1 to $DButton
 $X = ($i-1)*$A
 For $j = 1 to $DButton
  $Y = ($j-1)*$A
   ;chess desktop
  $Label = ''
  if Mod($i+$j,2) then $Label = '+'
  GuiCtrlCreateLabel($Label, $X, $Y, $A, $A, $SS_CENTER, 2)
    GUICtrlSetColor(-1,$ColorPassive)
    GUICtrlSetFont (-1, 30, 400, 4, 'Chess Regular')
    if $j = 2 then 
        if Mod($i+$j,2) then 
          $Label = 'O'
        else 
     $Label = 'o'
        endif
    endif
  if $j = 7 then 
    if Mod($i+$j,2) then 
      $Label = 'P'
    else 
      $Label = 'p'
    endif
  endif

   ;Black figures
  if $j = 1 then 
      if $i = 1 then $Label = 't'
   if $i = 8 then $Label = 'T'
   if $i = 2 then $Label = 'J'
   if $i = 7 then $Label = 'j'
   if $i = 3 then $Label = 'n'
   if $i = 6 then $Label = 'N'
   if $i = 4 then $Label = 'W'
   if $i = 5 then $Label = 'l'
  endif

 ;White figures
  if $j = 8 then 
   if $i = 1 then $Label = 'R'
   if $i = 8 then $Label = 'r'
   if $i = 2 then $Label = 'h'
   if $i = 7 then $Label = 'H'
   if $i = 3 then $Label = 'B'
   if $i = 6 then $Label = 'b'
   if $i = 4 then $Label = 'q'
   if $i = 5 then $Label = 'K'
  endif
    Assign('Figure_' &  $i & $j, '',2)
    if $Label <> '' and $Label <> '+' then Assign('Figure_' &  $i & $j, StringUpper($Label),2)
  Assign('Ctrl_' & $i & $j, GuiCtrlCreateInput($Label, $X, $Y, $A, $A, $ES_CENTER+$ES_READONLY, 2), 2)
    GUICtrlSetFont (-1, 30, 400, 4, 'Chess Regular')
    GUICtrlSetColor(-1,$ColorPassive)
  GUICtrlSetBkColor(-1, 0xFFFFFF)
 Next
Next

EndFunc

;=====================
func MouseMoveHandler($Msg)
local $X, $Y, $CursorInfo
local $Drag = 0

if Not IsFigure($Msg[3], $Msg[4]) then return
$CursorInfo = GUIGetCursorInfo ($hWnd)
if $CursorInfo[2] then GUICtrlSetColor($Current_Id,$ColorActive)
While $CursorInfo[2] = 1 
 $Drag = 1
 $CursorInfo = GUIGetCursorInfo ($hWnd)
 $X = $CursorInfo[0]
 $Y = $CursorInfo[1]
 GUICtrlSetPos($Current_Id, $X, $Y, $A, $A)
WEnd
if $Drag = 1 then 
  $res = PutFigure($Current_Id, $X+$A/2, $Y+$A/2)
  if $res then
;--------------------------------------
  else
   MsgBox(0,'','Invalid Move!')
  endif
endif
endfunc

;=====================
func IsFigure($X, $Y)
local $i, $j
$i = Int($X/$A)+1
$j = Int($Y/$A)+1
if Eval('Figure_' & $i & $j) <> '' then 
  $Current_Id = Eval('Ctrl_' & $i & $j)
  $Current_I = $i
  $Current_J = $j
  return 1
endif
return 0
endfunc

;=====================
func PutFigure($Current_Id, $X, $Y)
local $i, $j, $Label
$i = Int($X/$A)+1
$j = Int($Y/$A)+1

GUICtrlSetPos($Current_Id, ($Current_I-1)*$A, ($Current_J-1)*$A, $A, $A)
GUICtrlSetColor($Current_Id,$ColorPassive)
if IsMove($i,$j) then 

  $Label = GUICtrlRead($Current_Id)
  if Mod($i+$j,2) <> Mod($Current_I+$Current_J,2) then 
    if StringIsUpper($Label) then 
      $Label = StringLower($Label)
    else
      $Label = StringUpper($Label)
   endif
  endif
  GUICtrlSetData(Eval('Ctrl_' & $i & $j),$Label)
  Assign('Figure_' &  $i & $j, StringUpper($Label),2)
  
  $Label = ''
  if Mod($Current_I+$Current_J,2) then $Label = '+'
 GUICtrlSetData($Current_Id,$Label)
 Assign('Figure_' & $Current_I & $Current_J, '', 2)
 return 1
endif
return 0
endfunc

;=====================
; Any move is valid 
func IsMove($i,$j)
 if $Current_I = $i and $Current_J = $j then return 0
 return 1
endfunc

It uses font CHEQ_TT.TTF:

However I haven't any plan to continue it.

The point of world view

Link to comment
Share on other sites

Note: You have to install this font to see it. This file is on this page, too.

It would be a good project for someone to combine this with some remote control software so people could play each other over the internet.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I would be very impressed to see a working, and challenging, AI for this.

I'm currently working on a clone of "Cities and Knights of Catan" and... well...

let's just say I've purposefully distracted myself with other things for now.

I don't think we realize the possibilities of such games until we have complete control over every possibility.

Good Luck!

Link to comment
Share on other sites

You want AI.

- -* it very very hard.

It not gona happen soon

I agree it wouldn't be a "sooner" than "later" accomplishment. But, I'm sure an AI is possible. The problem is creating a strategical intelligence. To write thousands of lines of possibilities... to then have the AI decide "if this is the situation..." (loop through the possibilities, make comparisons) "... then this is my best move"... is very difficult to grasp.

Coding a brain could be easy... if only we could settle on it being spontaneous and unreasonable.

I like the idea of your graphical chess. Maybe, rather than have it play chess, you could use it to help a "chess player" to document moves or to plan their own strategies. Kind of like a "notepad" for chess. Anyway... have fun, at least!

Edit: By the way, I think if this were a program where "people could play each other", even coding rules, limitations, and certain scenarios would be difficult... but not impossible. It would just take some time.

Edited by The Ape
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...