Components and supplies
2.8" TFT LCD Touch Screen Expansion Shield w/ Touch Pen for Arduino
Arduino Mega 2560
Resistor 100 ohm
Buzzer
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
ArduinoMega Chess 1.0
arduino
1//ArduinoMega Chess 1.0 2//Sergey Urusov, ususovsv@gmail.com 3 4#include <Adafruit_TFTLCD.h> 5#include <stdint.h> 6#include "TouchScreen.h" 7#include <avr/pgmspace.h> 8#include <EEPROM.h> 9 10 11uint16_t BLACK,BLUE,RED,GREEN,CYAN,MAGENTA,YELLOW,WHITE,GRAY,DARK,GRAY2,BLACKF,WHITEF; 12Adafruit_TFTLCD tft; 13#define YP A1 // must be an analog pin, use "An" notation! 14#define XM A2 // must be an analog pin, use "An" notation! 15#define YM 7 // can be a digital pin 16#define XP 6 // can be a digital pin 17TouchScreen ts = TouchScreen(XP, YP, XM, YM, 350); 18int touchx,touchy,touchx0,touchy0,ntouch; 19unsigned long lasttouch; 20#define MINPRESSURE 10 21#define MAXPRESSURE 1000 22const int PinBuzz = 44; 23int cycle=0; // 24const char fp=1; 25const char fn=2; 26const char fb=3; 27const char fr=4; 28const char fq=5; 29const char fk=6; 30const int fig_weight[]={0,100,320,330,500,900,0}; 31const char fig_symb[]=" NBRQK"; 32unsigned long count; 33boolean rotate=false; 34const char polezero[8][8] PROGMEM={ 35 { 0, 0, 0, 0, 0, 0, 0, 0}, 36 { 0, 0, 0, 0, 0, 0, 0, 0}, 37 { 0, 0, 0, 0, 0, 0, 0, 0}, 38 { 0, 0, 0, 0, 0, 0, 0, 0}, 39 { 0, 0, 0, 0, 0, 0, 0, 0}, 40 { 0, 0, 0, 0, 0, 0, 0, 0}, 41 { 0, 0, 0, 0, 0, 0, 0, 0}, 42 { 0, 0, 0, 0, 0, 0, 0, 0}, 43 }; 44const char polestart[8][8] PROGMEM={ 45 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 46 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 47 { 0, 0, 0, 0, 0, 0, 0, 0}, 48 { 0, 0, 0, 0, 0, 0, 0, 0}, 49 { 0, 0, 0, 0, 0, 0, 0, 0}, 50 { 0, 0, 0, 0, 0, 0, 0, 0}, 51 { fp, fp, fp, fp, fp, fp, fp, fp}, 52 { fr, fn, fb, fq, fk, fb, fn, fr}, 53 }; 54 55char pole[8][8]={ // 3 56 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 57 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 58 { 0, 0, 0, 0, 0, 0, 0, 0}, 59 { 0, 0, 0, 0, 0, 0, 0, 0}, 60 { 0, 0, 0, 0, 0, 0, 0, 0}, 61 { 0, 0, 0, 0, 0, 0, 0, 0}, 62 { fp, fp, fp, fp, fp, fp, fp, fp}, 63 { fr, fn, fb, fq, fk, fb, fn, fr}, 64 }; 65 66char pole0[8][8]; // 67char poledisp[8][8]; // 68char polechoice[7]; // 69boolean w00,w000,b00,b000; 70char blinkstep; 71typedef struct { 72 char fig1, fig2; // 73 char x1,y1,x2,y2; // 74 char check; // 75 char type; // 1- ,2- ,3- ,4-5-6-7- ,,, 76 int weight; // , 77} step_type; 78const int MAXSTEPS=500; //. 79//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 80int MINDEPTH; //. 81int MAXDEPTH; //. 82int LIMDEPTH; //. 83const char MAXCUTS=10; //.. - - (10- , 20) 84boolean TRACE=0; 85boolean checkmatesearch=0; // 86boolean solving=false; 87boolean choice=false; 88boolean sound=0; 89int cur_step=1; // , 1.... 90int limit=0; // , 0-6; 91//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 92const long limits[7]={10,30,60,180,600,1800,100000}; 93// 0 1 2 3 4 5 6 94step_type steps[MAXSTEPS]; // 95step_type cuts[MAXCUTS]; // - 96step_type lastbest; 97int lastscore; 98int minbeta,maxalpha; 99int startweight; 100int cur_level; // () 101int start_var; // , 1.... 102int cur_var; // , 1.... 103int cur_choice; // 104boolean check_on_table; // 105boolean isstatus; 106char WKJ=0,WKI=0,BKJ=0,BKI=0; // 107boolean endspiel=false; // 108char progress; // 0-100 109boolean only_action=false; // - 110unsigned long starttime,limittime,quitime=0; 111boolean drag; 112const uint8_t fig[6][32] PROGMEM={ 113{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 1140x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x0, 0x0}, // 115{0x0, 0x0, 0x3, 0x40, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x3F, 0xFC, 0x39, 0xFC, 0x33, 0xFC, 1160x7, 0xFC, 0xF, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x7, 0xE0, 0x1F, 0xF8, 0x0, 0x0}, // 117{0x1, 0x80, 0x3, 0xC0, 0x1, 0x80, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 1180x1F, 0xF8, 0x1F, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x31, 0x8C, 0x7F, 0xFE, 0x0, 0x0}, // 119{0x0, 0x0, 0x19, 0x98, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 1200xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0}, // 121{0x19, 0x98, 0xD9, 0x9B, 0xD9, 0x9B, 0xD9, 0x9B, 0x6D, 0xB6, 0x6D, 0xB6, 0x6D, 0xB6, 0x35, 0xAC, 1220x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0}, // 123{0x1, 0x80, 0x1, 0x80, 0x79, 0x9E, 0x7D, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1240x7F, 0xFE, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0} // 125}; 126const uint8_t fig_cont[6][32] PROGMEM={ 127{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1280x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8}, // 129{0x3, 0x40, 0x4, 0xA0, 0x8, 0x10, 0x12, 0x8, 0x20, 0x4, 0x40, 0x2, 0x46, 0x2, 0x4C, 0x2, 1300x38, 0x2, 0x10, 0x4, 0x10, 0x8, 0x8, 0x10, 0x4, 0x20, 0x18, 0x18, 0x20, 0x4, 0x3F, 0xFC}, // 131{0x2, 0x40, 0x4, 0x20, 0x6, 0x60, 0x10, 0x8, 0x21, 0x84, 0x21, 0x84, 0x21, 0x84, 0x27, 0xE4, 1320x21, 0x84, 0x21, 0x84, 0x11, 0x88, 0x8, 0x10, 0x34, 0x2C, 0x4E, 0x72, 0x80, 0x1, 0xFF, 0xFF}, // 133{0x39, 0x9C, 0x26, 0x64, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 1340x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC}, // 135{0x22, 0x24, 0x22, 0x24, 0x22, 0x24, 0x26, 0x64, 0x92, 0x49, 0x92, 0x49, 0x92, 0x49, 0x4A, 0x52, 1360x40, 0x2, 0x40, 0x2, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC}, // 137{0x2, 0x40, 0x7A, 0x5E, 0x86, 0x61, 0x82, 0x41, 0x2, 0x40, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 1380x80, 0x81, 0x41, 0x82, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC} // 139}; 140 141const uint8_t issound[2][32] PROGMEM={ 142{0x0, 0x0, 0x0, 0x0, 0x0, 0x32, 0x0, 0x54, 0x0, 0x98, 0x1F, 0x10, 0x10, 0x30, 0x10, 0x50, 1430x10, 0x90, 0x11, 0x10, 0x1F, 0x10, 0x4, 0x90, 0x8, 0x50, 0x10, 0x30, 0x0, 0x0, 0x0, 0x0}, 144{0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x50, 0x0, 0x90, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 1450x10, 0x10, 0x10, 0x10, 0x1F, 0x10, 0x0, 0x90, 0x0, 0x50, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0} 146}; 147const uint8_t iswb[2][32] PROGMEM={ 148{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1490x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x0, 0x0}, 150{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 1510xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0} 152}; 153const uint8_t iscm[2][32] PROGMEM={ 154{0x0, 0x0, 0x7, 0xE0, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0x0, 0x30, 0x0, 0x60, 1550x0, 0xC0, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0}, 156{0x0, 0x0, 0x3F, 0x0, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x1, 0x80, 0x3, 0x0, 1570x6, 0x24, 0xC, 0x24, 0xC, 0xFF, 0xC, 0x24, 0x0, 0xFF, 0xC, 0x24, 0xC, 0x24, 0x0, 0x0} 158}; 159const uint8_t islimit[7][32] PROGMEM={ 160{0x0, 0x0, 0x4, 0x70, 0xC, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 1610xE, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 162{0x0, 0x0, 0xC, 0x70, 0x12, 0x88, 0x2, 0x88, 0xC, 0x88, 0x2, 0x88, 0x2, 0x88, 0x12, 0x88, 1630xC, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 164{0x0, 0x0, 0x1, 0x80, 0x3, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 1650x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 166{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 1670x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 168{0x0, 0x0, 0xC, 0x78, 0x1C, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 1690x1E, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 170{0x0, 0x0, 0x1C, 0x78, 0x36, 0xCC, 0x6, 0xCC, 0x1C, 0xCC, 0x6, 0xCC, 0x36, 0xCC, 0x36, 0xCC, 1710x1C, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 172{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3C, 0x3C, 0x7E, 0x7E, 0x63, 0xC6, 0x61, 0x86, 1730x63, 0xC6, 0x66, 0x66, 0x3C, 0x3C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 174}; 175const uint8_t isdesc[2][32] PROGMEM={ 176{0x0, 0x0, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 1770xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E}, 178{0x0, 0x0, 0x0, 0x4, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x30, 1790x30, 0x70, 0x78, 0xE0, 0x3C, 0xC0, 0x1F, 0xC0, 0xF, 0x80, 0x7, 0x0, 0x2, 0x0, 0x0, 0x0} 180 181}; 182const uint8_t isback[32] PROGMEM={ 1830x0, 0x0, 0x0, 0x0, 0x20, 0xF0, 0x31, 0xF8, 0x3B, 0xFC, 0x3F, 0x8E, 0x3F, 0xE, 0x3F, 0xE, 1840x3F, 0x8E, 0x3F, 0xCE, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x70, 0x0, 0x0 185}; 186const uint8_t issave[32] PROGMEM={ 1870x1, 0x80, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 1880x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 189}; 190const uint8_t isload[32] PROGMEM={ 1910x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 1920x23, 0xC4, 0x27, 0xE4, 0x27, 0xE4, 0x23, 0xC4, 0x21, 0x84, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 193}; 194const uint8_t isempty[32] PROGMEM={ 1950xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 1960x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF 197}; 198const uint8_t isinit[32] PROGMEM={ 1990xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 2000x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 201}; 202const uint8_t isplay[32] PROGMEM={ 2030x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x1, 0x80, 0x23, 0xF8, 0x17, 0xC8, 0xB, 0xC8, 2040x3, 0xC0, 0x3, 0xC0, 0x2, 0x40, 0x4, 0x40, 0x4, 0x38, 0x4, 0x8, 0xC, 0x0, 0x0, 0x0 205}; 206const uint8_t isrotate[2][32] PROGMEM={ 207{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 2080x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0}, 209{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 2100x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0} 211}; 212 213const int pos[7][8][8] PROGMEM={ 214 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 215 {100,100,100,100,100,100,100,100}, //{ 50, 50, 50, 50, 50, 50, 50, 50}, 216 { 20, 30, 40, 50, 50, 40, 30, 20}, //{ 10, 10, 20, 30, 30, 20, 10, 10}, 217 { 5, 5, 10, 25, 25, 10, 5, 5}, 218 { 0, 0, 0, 20, 20, 0, 0, 0}, 219 { 5, -5,-10, 0, 0,-10, -5, 5}, 220 { 5, 10, 10,-20,-20, 10, 10, 5}, //{ 5, 10, 10,-20,-20, 10, 10, 5}, 221 { 0, 0, 0, 0, 0, 0, 0, 0}}, 222 223 {{-50,-40,-30,-30,-30,-30,-40,-50}, // 224 {-40,-20, 0, 0, 0, 0,-20,-40}, 225 {-30, 0, 10, 15, 15, 10, 0,-30}, 226 {-30, 5, 15, 20, 20, 15, 5,-30}, 227 {-30, 0, 15, 20, 20, 15, 0,-30}, 228 {-30, 5, 10, 15, 15, 10, 5,-30}, 229 {-40,-20, 0, 5, 5, 0,-20,-40}, 230 {-50,-40,-30,-30,-30,-30,-40,-50}}, 231 232 {{-20,-10,-10,-10,-10,-10,-10,-20}, // 233 {-10, 0, 0, 0, 0, 0, 0,-10}, 234 {-10, 0, 5, 10, 10, 5, 0,-10}, 235 {-10, 5, 5, 10, 10, 5, 5,-10}, 236 {-10, 0, 10, 10, 10, 10, 0,-10}, 237 {-10, 10, 10, 10, 10, 10, 10,-10}, 238 {-10, 5, 0, 0, 0, 0, 5,-10}, 239 {-20,-10,-10,-10,-10,-10,-10,-20}}, 240 241 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 242 { 5, 10, 10, 10, 10, 10, 10, 5}, 243 { -5, 0, 0, 0, 0, 0, 0, -5}, 244 { -5, 0, 0, 0, 0, 0, 0, -5}, 245 { -5, 0, 0, 0, 0, 0, 0, -5}, 246 { -5, 0, 0, 0, 0, 0, 0, -5}, 247 { -5, 0, 0, 0, 0, 0, 0, -5}, 248 { 0, 0, 0, 5, 5, 0, 0, 0}}, 249 250 {{-20,-10,-10, -5, -5,-10,-10,-20}, // 251 {-10, 0, 0, 0, 0, 0, 0,-10}, 252 {-10, 0, 5, 5, 5, 5, 0,-10}, 253 { -5, 0, 5, 5, 5, 5, 0, -5}, 254 { 0, 0, 5, 5, 5, 5, 0, -5}, 255 {-10, 5, 5, 5, 5, 5, 0,-10}, 256 {-10, 0, 5, 0, 0, 0, 0,-10}, 257 {-20,-10,-10, -5, -5,-10,-10,-20}}, 258 259 {{-30,-40,-40,-50,-50,-40,-40,-30}, // 260 {-30,-40,-40,-50,-50,-40,-40,-30}, 261 {-30,-40,-40,-50,-50,-40,-40,-30}, 262 {-30,-40,-40,-50,-50,-40,-40,-30}, 263 {-20,-30,-30,-40,-40,-30,-30,-20}, 264 {-10,-20,-20,-20,-20,-20,-20,-10}, 265 { 10, 10,-10,-10,-10,-10, 10, 10}, //{ 20, 20, 0, 0, 0, 0, 20, 20}, 266 { 10, 40, 30, 0, 0, 0, 50, 10}}, //{ 20, 30, 10, 0, 0, 10, 30, 20}}, 267 268 {{-50,-40,-30,-20,-20,-30,-40,-50}, // 269 {-30,-20,-10, 0, 0,-10,-20,-30}, 270 {-30,-10, 20, 30, 30, 20,-10,-30}, 271 {-30,-10, 30, 40, 40, 30,-10,-30}, 272 {-30,-10, 30, 40, 40, 30,-10,-30}, 273 {-30,-10, 20, 30, 30, 20,-10,-30}, 274 {-30,-30, 0, 0, 0, 0,-30,-30}, 275 {-50,-30,-30,-30,-30,-30,-30,-50}} 276}; 277 278//****** 279class Button { 280public: 281int bx,by,bw,bh,bshift; 282String bs; 283const uint8_t* bp; 284 Button(int x,int y,int w,int h,String s,const uint8_t* p, int shift); 285 void Show(int shift); 286 void Show(String s); 287 void Hide(); 288 boolean IsPressed(int xc, int yc); 289private: 290boolean active; 291 void ishow(uint16_t textcolor,uint16_t backcolor); 292}; 293Button::Button(int x,int y,int w,int h,String s,const uint8_t* p=NULL, int shift=0) { 294 bx=x; by=y; bw=w; bh=h; bs=s; bp=p; bshift=shift; 295}; 296void Button::Show(int shift=0) { 297 bshift=shift; 298 active=true; 299 ishow(WHITE,DARK); 300}; 301void Button::Show(String s) { 302 bs=s; 303 active=true; 304 ishow(WHITE,DARK); 305}; 306void Button::Hide() { 307 active=false; 308 ishow(GRAY,DARK); 309}; 310boolean Button::IsPressed(int xc, int yc) { 311 if (active&&abs(bx+bw/2-xc)<bw/2&&abs(by+bh/2-yc)<bh/2) { 312 ishow(WHITE,GRAY); 313 touchx=0; touchy=0; 314 return true; 315 } 316 return false; 317}; 318void Button::ishow(uint16_t textcolor,uint16_t backcolor) { 319 tft.fillRoundRect(bx+2,by+2,bw,bh,3,GRAY2); 320 tft.fillRoundRect(bx,by,bw,bh,3,backcolor); 321 tft.setTextColor(textcolor); 322 tft.setTextSize(1); 323 tft.setCursor(bx+(bw-bs.length()*6)/2,by+(bh-8)/2); 324 if (bp!=NULL) { 325 tft.drawBitmap(1+bx+(bw-16)/2, by+(bh-16)/2, bp+bshift*32, 16, 16,textcolor); 326 } 327 else tft.print(bs); 328} 329Button BAction(65, 216, 55, 20, "START"); 330Button BBW(125, 216, 22, 20, "",&iswb[0][0]); 331Button BBack(152, 216, 23, 20, "",&isback[0]); 332Button BInit(152, 216, 23, 20, "",&isinit[0]); 333Button BLimit(180, 216, 23, 20, "",&islimit[0][0]); 334Button BEmpty(180, 216, 23, 20, "",&isempty[0]); 335Button BRotate(208, 216, 23, 20, "",&isrotate[0][0]); 336Button BPlay(208, 216, 23, 20, "",&isplay[0]); 337Button BCM(236, 216, 23, 20, "",&iscm[0][0]); 338Button BSave(236, 216, 23, 20, "",&issave[0]); 339Button BDesc(264, 216, 23, 20, "",&isdesc[0][0]); 340Button BSound(292, 216, 23, 20, "",&issound[0][0]); 341Button BLoad(292, 216, 23, 20, "",&isload[0]); 342//****** 343//********************************** 344void drawBitmap(int16_t x, int16_t y, 345 const uint8_t *bitmap, int16_t w, int16_t h, 346 uint16_t color) { 347 int16_t i, j, byteWidth = (w + 7) / 8; 348 for(j=0; j<h; j++) { 349 for(i=0; i<w; i++ ) { 350 if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) { 351 tft.drawPixel(x+i, y+j, color); 352 } 353 } 354 } 355} 356//**************************** 357void deletebuttons() { 358 tft.fillRect(0,215,320,30,BLACK); 359} 360 361//**************************** 362void clearstatus() { 363 tft.fillRect(0,210,60,30,BLACK); 364 tft.drawFastHLine(5,210,200,BLACK); 365 tft.drawFastHLine(5,212,200,BLACK); 366} 367//**************************** 368void setup() { 369 Serial.begin(57600); 370 Serial.println(F("Start")); 371 pinMode(13, OUTPUT); 372 tft.reset(); 373 tft.begin(tft.readID()); 374 tft.setRotation(1); 375 definecolors(); 376 beep(100); 377 for (int i=0;i<MAXSTEPS;i++) { 378 steps[i].x1=0; steps[i].y1=0; 379 steps[i].x2=0; steps[i].y2=0; 380 steps[i].fig1=0; steps[i].fig2=0; 381 steps[i].check=0; 382 steps[i].type=0; 383 steps[i].weight=0; 384 } 385 initboard(); 386} 387//**************************** 388void initboard() { 389 for (int i=0;i<8;i++) 390 for (int j=0;j<8;j++) poledisp[j][i]=-100; // 391 tft.fillScreen(BLACK); 392 tft.setTextColor(GRAY); 393 tft.setTextSize(1); 394 for (int j=1;j<9;j++) { 395 tft.setCursor(2,j*24-15); 396 if (rotate) tft.print(j); else tft.print(9-j); 397 } 398 for (byte i=1;i<9;i++) { 399 tft.setCursor(i*24-1,198); 400 if (rotate) tft.print(char(96+9-i)); else tft.print(char(96+i)); 401 } 402 initscreen(); 403} 404//**************************** 405void initscreen() { 406 show_board(); 407 show_steps(); 408 BAction.Show("START"); 409 BBW.Show(cur_step%2); 410 if (cur_step>1) BBack.Show(); else BBack.Hide(); 411 BSave.Hide(); 412 BLoad.Hide(); 413 BInit.Hide(); 414 BBack.Show(); 415 BLimit.Show(); 416 BPlay.Hide(); 417 BRotate.Show(rotate); 418 BEmpty.Hide(); 419 BLimit.Show(limit); 420 BCM.Show(checkmatesearch); 421 BDesc.Show(choice); 422 BSound.Show(sound); 423} 424//**************************** 425void loop() { 426 gui(); 427 if (solving) { 428 int len=70; 429 if (!sound) len=2; 430 for (int i=1;i<4;i++) { tone(44,400+300*i,len); delay(70); } 431 lastscore=solve_step(); 432 float tim=float(millis()-starttime)/1000; 433 for (int i=3;i>=1;i--) { tone(44,400+300*i,len); delay(70); } 434 if (!checkmatesearch&&lastscore>-9000&&lastscore!=8999) { 435 movestep(cur_step); 436 cur_step++; steps[cur_step].fig1=0; 437 } 438 initscreen(); 439 animate_step(cur_step-1,false); 440 clearstatus(); 441 if (lastscore>9000) { 442 tft.setTextSize(1); 443 tft.setTextColor(GREEN); 444 tft.setCursor(0,210); 445 if (lastscore<9999) { 446 tft.print("# in "); 447 tft.print((9999-lastscore)/2+1); tft.print(F(" st")); 448 } else tft.print("Checkmate"); 449 } else if (lastscore<-9000) { 450 tft.setTextSize(1); 451 tft.setTextColor(RED); 452 tft.setCursor(0,210); 453 tft.print("GIVE UP!"); 454 show_steps(); 455 } else if (lastscore==8999) { //Draw 456 tft.setTextSize(1); 457 tft.setTextColor(YELLOW); 458 tft.setCursor(0,210); 459 tft.print("Draw"); 460 } 461 tft.setTextSize(1); 462 tft.setTextColor(GRAY); 463 tft.setCursor(0,220); 464 tft.print(tim,0); 465 tft.print("s "); 466 if (abs(lastscore)<5000) { 467 tft.print(lastscore); 468 } 469 tft.setCursor(0,230); 470 tft.print(count); 471 tft.print("pos"); 472 Serial.print(F("Positions estimated=")); Serial.println(count); 473 Serial.print(F("Time=")); Serial.print(tim,1); Serial.println("s"); 474 } 475 delay(10); 476 477 478} 479 480//********************************** 481void load_choice() { 482 for (int i=0;i<7;i++) 483 if (cur_step%2==1) polechoice[i]=i; else polechoice[i]=-i; 484} 485//********************************** 486void show_choice() { 487uint16_t color,color_cont; 488 for (int i=0;i<7;i++) { 489 color=BLACKF; 490 if (i%2==0) color=WHITEF; 491 tft.fillRect(260,i*25+35,25,25,color); 492 color=DARK; 493 if (i==cur_choice) color=WHITE; 494 tft.drawRect(260,i*25+35,25,25,color); 495 color=BLACK; color_cont=GRAY; 496 if (polechoice[i]>0) { color=WHITE; color_cont=BLACK; } 497 if (polechoice[i]!=0) { 498 drawBitmap(264, i*25+39,&fig[abs(polechoice[i])-1][0], 16, 16,color); 499 drawBitmap(264, i*25+39,&fig_cont[abs(polechoice[i])-1][0], 16, 16,color_cont); 500 } 501 } 502} 503//********************************** 504void animate_step(int nstep, boolean hide) { 505 if (!hide&&nstep-1>0&&steps[nstep-1].fig1!=0) animate_step(nstep-1,true); 506 if (nstep-1<1||steps[nstep].fig1==0) return; 507 int j=steps[nstep].x1; 508 int dj=steps[nstep].x2-steps[nstep].x1; 509 int i=steps[nstep].y1; 510 int di=steps[nstep].y2-steps[nstep].y1; 511 di=di/abs(di); dj=dj/abs(dj); 512 if (hide) show_fig(i,j); 513 while (j!=steps[nstep].x2||i!=steps[nstep].y2) { 514 show_fig(i,j); 515 if (!hide) 516 if (!rotate) tft.drawRect(i*24+13,j*24+1,23,23,WHITE); 517 else tft.drawRect((7-i)*24+13,(7-j)*24+1,23,23,WHITE); 518 int mj=0; 519 if (j!=steps[nstep].x2) { j+=dj; mj=1; } 520 if (abs(steps[nstep].fig1)==fn&&mj==1) continue; 521 if (i!=steps[nstep].y2) i+=di; 522 } 523 show_fig(i,j); 524 if (!hide) 525 if (!rotate) tft.drawRect(i*24+13,j*24+1,23,23,WHITE); 526 else tft.drawRect((7-i)*24+13,(7-j)*24+1,23,23,WHITE); 527} 528//********************************** 529boolean load_usb() { 530char s='x',i=0,j=0; boolean load=false; 531 Serial.println(F("Wait for FEN position")); 532 for (int i=0;i<8;i++) 533 for (int j=0;j<8;j++) { 534 pole0[j][i]=pole[j][i]; 535 pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 536 } 537 while (s!=' ') { 538 s=Serial.read(); 539 if (i>7) { i=0; j++; } 540 if (!getpole(j,i)) break; 541 switch (s) { 542 case '/': i=0; break; 543 case 'p': pole[j][i]=-fp; i++; break; 544 case 'P': pole[j][i]=fp; i++; break; 545 case 'n': pole[j][i]=-fn; i++; break; 546 case 'N': pole[j][i]=fn; i++; break; 547 case 'b': pole[j][i]=-fb; i++; break; 548 case 'B': pole[j][i]=fb; i++; break; 549 case 'r': pole[j][i]=-fr; i++; break; 550 case 'R': pole[j][i]=fr; i++; break; 551 case 'q': pole[j][i]=-fq; i++; break; 552 case 'Q': pole[j][i]=fq; i++; break; 553 case 'k': pole[j][i]=-fk; i++; break; 554 case 'K': pole[j][i]=fk; i++; break; 555 case '1': i++; break; 556 case '2': i+=2; break; 557 case '3': i+=3; break; 558 case '4': i+=4; break; 559 case '5': i+=5; break; 560 case '6': i+=6; break; 561 case '7': i+=7; break; 562 case '8': i=0; j++; break; 563 case ' ': break; 564 } 565 delay(20); 566 if (i+j>0&&Serial.available()==0) break; 567 } 568 s=0; 569 if (Serial.available()>0) s=Serial.read(); 570 while (Serial.available()>0) Serial.read(); 571 if (s=='w'||s==0) { cur_step=1; load=true; } 572 else if (s=='b') { cur_step=2; load=true; } 573 else load=false; 574 if (load) { 575 steps[1].fig1=0; steps[2].fig1=0; 576 Serial.println(F("Position loaded")); 577 } else { 578 for (int i=0;i<8;i++) 579 for (int j=0;j<8;j++) 580 pole[j][i]=pole0[j][i]; 581 } 582 return load; 583} 584//********************************** 585void play() { 586 BAction.Show("STOP"); 587 BSave.Hide(); 588 BBW.Hide(); 589 BLoad.Hide(); 590 BInit.Hide(); 591 BPlay.Hide(); 592 BEmpty.Hide(); 593 BDesc.Hide(); 594 clearstatus(); 595 for (int i=cur_step-1;i>0;i--) { 596 if (steps[i].fig1!=0) backstep(i); 597 } 598 show_board(); 599 show_steps(); 600 boolean st=false; 601 unsigned long tim=millis(); 602 int i=1; 603 while (i<cur_step) { 604 if (steps[i].fig1!=0) movestep(i); 605 digitalWrite(13, HIGH); 606 TSPoint p = ts.getPoint(); 607 digitalWrite(13, LOW); 608 pinMode(XM, OUTPUT); 609 pinMode(YP, OUTPUT); 610 int x=388-p.y/2.4; 611 int y=p.x/3.2-50; 612 if (p.z>MINPRESSURE&&p.z<MAXPRESSURE) 613 if (BAction.IsPressed(x,y)) { st=true; BAction.Show(); } 614 if (millis()-tim>2000||st) { 615 tim=millis(); 616 i++; 617 show_board(); 618 beep(20); 619 } 620 } 621 choice=0; 622 beep(200); 623 kingpositions(); 624 clearstatus(); 625 initscreen(); 626} 627//********************************** 628void gui() { 629 digitalWrite(13, HIGH); 630 TSPoint p = ts.getPoint(); 631 digitalWrite(13, LOW); 632 // if sharing pins, you'll need to fix the directions of the touchscreen pins 633 pinMode(XM, OUTPUT); 634 pinMode(YP, OUTPUT); 635 int x=388-p.y/2.4; 636 int y=p.x/3.2-50; 637 if (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&p.z>MINPRESSURE&&p.z<MAXPRESSURE) { 638 touchx=x; 639 touchy=y; 640 if (solving) { 641 if (BAction.IsPressed(x,y)) { // 642 beep(100); 643 solving=false; 644 BAction.Show("stopping"); 645 BAction.Hide(); 646 return; 647 } 648 } else { //not solve 649 if (BAction.IsPressed(x,y)) { // 650 if (!choice) { 651 delay(100); 652 solving=true; 653 BAction.Show(); 654 animate_step(cur_step-1,true); 655 return; 656 } else { // USB 657 delay(100); 658 BAction.Hide(); 659 BSave.Hide(); 660 BBW.Hide(); 661 BLoad.Hide(); 662 BInit.Hide(); 663 BPlay.Hide(); 664 BEmpty.Hide(); 665 BDesc.Hide(); 666 show_steps(); 667 choice=0; 668 if (!load_usb()) return; 669 beep(200); 670 kingpositions(); 671 clearstatus(); 672 initscreen(); 673 } 674 } else if (BBW.IsPressed(x,y)) { //- 675 beep(100); 676 if (cur_step==1) cur_step=2; 677 else if (cur_step==2) cur_step=1; 678 else if (cur_step%2==0) cur_step--; else cur_step++; 679 BBW.Show(cur_step%2); 680 if (choice) { load_choice(); show_choice(); } 681 return; 682 } else if (cur_step>1&&BBack.IsPressed(x,y)) { // 683 beep(100); 684 cur_step--; 685 animate_step(cur_step,true); 686 lastscore=0; 687 backstep(cur_step); 688 steps[cur_step].fig1=0; 689 show_board(); 690 show_steps(); 691 if (cur_step>1) BBack.Show(); else BBack.Hide(); 692 BBW.Show(cur_step%2); 693 clearstatus(); 694 return; 695 } else if (BLimit.IsPressed(x,y)) { // 696 beep(100); 697 limit++; if (limit>6) limit=0; 698 BLimit.Show(limit); 699 return; 700 } else if (BCM.IsPressed(x,y)) { // 701 beep(100); 702 checkmatesearch=!checkmatesearch; 703 BCM.Show(checkmatesearch); 704 if (!checkmatesearch) steps[cur_step].fig1=0; 705 lastscore=0; 706 show_steps(); 707 return; 708 } else if (BRotate.IsPressed(x,y)) { // 709 beep(100); 710 rotate=!rotate; 711 BRotate.Show(rotate); 712 initboard(); 713 return; 714 } else if (BSound.IsPressed(x,y)) { // 715 if (sound==1) { beep(100); sound=0; } else { sound=1; beep(200); } 716 BSound.Show(sound); 717 return; 718 } else if (BDesc.IsPressed(x,y)) { // 719 beep(100); 720 if (!choice) { 721 choice=1; 722 cur_choice=1; 723 load_choice(); 724 show_choice(); 725 BAction.Show("USB(FEN)"); 726 BBack.Hide(); 727 BInit.Show(); 728 BSave.Show(); 729 BLimit.Hide(); 730 BEmpty.Show(); 731 BRotate.Hide(); 732 BPlay.Show(); 733 BCM.Hide(); 734 BSound.Hide(); 735 BSave.Show(); 736 BLoad.Show(); 737 } else { 738 choice=0; 739 lastscore=0; 740 clearstatus(); 741 kingpositions(); 742 initscreen(); 743 } 744 BDesc.Show(choice); 745 return; 746 } else if (BInit.IsPressed(x,y)) { // 747 beep(200); 748 cur_step=1; 749 steps[1].fig1=0; 750 for (int i=0;i<8;i++) 751 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polestart[j][i]); 752 choice=0; 753 kingpositions(); 754 clearstatus(); 755 initscreen(); 756 } else if (BEmpty.IsPressed(x,y)) { // 757 beep(200); 758 cur_step=1; 759 clearstatus(); 760 steps[1].fig1=0; steps[2].fig1=0; 761 for (int i=0;i<8;i++) 762 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 763 BEmpty.Show(); 764 show_board(); 765 } else if (BSave.IsPressed(x,y)) { 766 beep(100); 767 for (int i=0;i<8;i++) 768 for (int j=0;j<8;j++) EEPROM.write(i*8+j,pole[j][i]); 769 EEPROM.put(64,cur_step); 770 EEPROM.put(66,limit); 771 for (int i=1;i<cur_step;i++) { 772 EEPROM.put(66+i*10,steps[i]); 773 } 774 BSave.Show(); 775 } else if (BLoad.IsPressed(x,y)) { 776 beep(200); 777 for (int i=0;i<8;i++) 778 for (int j=0;j<8;j++) pole[j][i]=EEPROM.read(i*8+j); 779 EEPROM.get(64,cur_step); 780 if (cur_step<0||cur_step>400) cur_step=1; 781 EEPROM.get(66,limit); 782 if (limit<0||limit>6) limit=2; 783 for (int i=1;i<cur_step;i++) { 784 EEPROM.get(66+i*10,steps[i]); 785 } 786 steps[0].fig1=0; 787 steps[cur_step].fig1=0; 788 choice=0; 789 kingpositions(); 790 clearstatus(); 791 initscreen(); 792 } else if (BPlay.IsPressed(x,y)) { // 793 beep(100); 794 BPlay.Show(); 795 play(); 796 return; 797 } 798 if (!choice&&ntouch==0) { 799 touchx0=touchx; 800 touchy0=touchy; 801 char x1=touchy0/24; 802 char y1=(touchx0-10)/24; 803 if (rotate) { x1=7-x1; y1=7-y1; } 804 if (getpole(y1,x1)&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 805 char p=pole[x1][y1]; 806 drag=true; 807 pole[x1][y1]=0; 808 show_fig(y1,x1); 809 pole[x1][y1]=p; 810 } else drag=false; 811 } 812 ntouch++; 813 if (drag&&ntouch>1) { 814 uint16_t buf[256]; 815 int x1=touchy0/24; 816 int y1=(touchx0-10)/24; 817 if (rotate) { x1=7-x1; y1=7-y1; } 818 if (abs(pole[x1][y1])>0&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 819 for (int i=0;i<16;i++) 820 for (int j=0;j<16;j++) buf[i*16+j]=tft.readPixel(touchx-8+i,touchy-8+j); 821 buf[0]=tft.readPixel(touchx-8,touchy-8); 822 uint16_t color,color_cont; 823 color=BLACK; color_cont=GRAY; 824 if (pole[x1][y1]>0) { color=WHITE; color_cont=BLACK; } 825 drawBitmap(touchx-8, touchy-8,&fig[abs(pole[x1][y1])-1][0], 16, 16,color); 826 drawBitmap(touchx-8, touchy-8,&fig_cont[abs(pole[x1][y1])-1][0], 16, 16,color_cont); 827 delay(30); 828 for (int i=0;i<16;i++) 829 for (int j=0;j<16;j++) tft.drawPixel(touchx-8+i,touchy-8+j,buf[i*16+j]); 830 } 831 } else if (choice) { 832 int x1=touchy/24; 833 int y1=(touchx-10)/24; 834 if (rotate) { x1=7-x1; y1=7-y1; } 835 if (touchy+touchx!=0&&choice&&getpole(y1,x1)) { 836 pole[x1][y1]=polechoice[cur_choice]; 837 show_board(); 838 } 839 if (touchx>=260&&touchx<=285&&touchy>=35&&touchy<=210) { 840 cur_choice=(touchy-35)/25; 841 show_choice(); 842 } 843 } 844 lasttouch=millis(); 845 } //not solve 846 if (!drag) { beep(10); ntouch=0; animate_step(cur_step-1,true); } 847 } else { //touching 848 if (!solving&&!choice&&drag!=0&&ntouch>0&&p.z<MINPRESSURE&&millis()-lasttouch>300) { //drag 849 char x1=touchy0/24; 850 char y1=(touchx0-10)/24; 851 char x2=touchy/24; 852 char y2=(touchx-10)/24; 853 if (rotate) { x1=7-x1; y1=7-y1; x2=7-x2; y2=7-y2; } 854 if (getpole(y1,x1)) show_fig(y1,x1); 855 if (getpole(y1,x1)&&getpole(y2,x2)) { 856 start_var=cur_step+1; 857 only_action=false; 858 load_variants(cur_step); 859 for (int i=cur_step+1;i<cur_step+cur_var+1;i++) 860 if (steps[i].x1==x1&&steps[i].y1==y1&&steps[i].x2==x2&&steps[i].y2==y2) { 861 steps[cur_step]=steps[i]; 862 movestep(cur_step); cur_step++; steps[cur_step].fig1=0; 863 int len=100; 864 if (!sound) len=2; 865 for (int i=2;i>=1;i--) { tone(44,400+300*i,len); delay(100); } 866 initscreen(); 867 animate_step(cur_step-1,false); 868 break; 869 } 870 } else { 871 beep(10); 872 } 873 ntouch=0; 874 drag=false; 875 } 876 } 877 if (millis()-quitime>1000) { 878 if (isstatus) show_status(); 879 quitime=millis(); 880 } 881 882 883} 884//********************************** 885void show_status() { 886 int tim=(millis()-starttime)/1000; 887 int cur=200000*tim/(limittime-starttime); 888 if (cur>200) { cur=200; solving=false; } 889 tft.drawFastHLine(5,210,cur,GRAY); 890 tft.drawFastHLine(5,212,progress*2,GRAY); 891 int m=tim/60; 892 int s=tim%60; 893 tft.setTextWrap(1); 894 tft.setTextColor(GRAY); 895 tft.setTextSize(1); 896 tft.fillRect(0,220,60,10,BLACK); 897 tft.setCursor(2,220); 898 if (m>0) { tft.print(m); tft.print(":"); } 899 if (s>0) { 900 if (s<10&&m>0) tft.print("0"); 901 tft.print(s); 902 } else if (m>0) tft.print("00"); else tft.print("0"); 903 tft.setCursor(36,220); 904 tft.print(cur_level); 905 906 tft.setCursor(2,230); 907 if (cur_step%2==1) tft.setTextColor(WHITE); else tft.setTextColor(GRAY); 908 if (lastbest.fig1!=steps[0].fig1||lastbest.x1!=steps[0].x1||lastbest.y1!=steps[0].y1|| 909 lastbest.x2!=steps[0].x2||lastbest.y2!=steps[0].y2) { 910 beep(10); 911 lastbest=steps[0]; 912 tft.fillRect(0,230,50,10,BLACK); 913 tft.setCursor(2,230); 914 tft.print(str_step(0)); 915 blinkstep=0; 916 } 917 if (steps[0].fig1!=0&&blinkstep==0&&tim>5) { 918 char poleb[8][8]; 919 for (int i=0;i<8;i++) 920 for (int j=0;j<8;j++) { poleb[j][i]=pole[j][i]; pole[j][i]=pole0[j][i]; } 921 movestep(0); 922 show_board(); 923 delay(100); 924 backstep(0); 925 show_board(); 926 for (int i=0;i<8;i++) 927 for (int j=0;j<8;j++) pole[j][i]=poleb[j][i]; 928 kingpositions(); 929 } 930 blinkstep++; 931 if (blinkstep>2) blinkstep=0; 932} 933//**************************** 934void beep(int leng) { 935 analogWrite(PinBuzz, 20); 936 delay(2); 937 if (!sound) analogWrite(PinBuzz, 0); 938 delay(leng); 939 analogWrite(PinBuzz, 0); 940} 941//**************************** 942void definecolors() { 943 BLACK =0x0000; 944 BLUE =0x07FF; //0x001F; 945 RED =0xF800; 946 GREEN =0x07E0; 947 CYAN =0x07FF; 948 MAGENTA=0xF81F; 949 YELLOW =0xFFE0; 950 WHITE =0xFFFF; 951 GRAY =0x7BEF; 952 DARK =getColor(32,32,32); 953 GRAY2 =getColor(16,16,16); 954 BLACKF =getColor(94,58,0); 955 WHITEF =getColor(180,114,0); 956} 957//**************************** 958uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) 959{ 960 red >>= 3; 961 green >>= 2; 962 blue >>= 3; 963 return (red << 11) | (green << 5) | blue; 964} 965//**************************** 966void show_fig(int i,int j) { 967uint16_t color,color_cont; 968 color=BLACKF; 969 if ((i+j+2)%2==0) color=WHITEF; 970 int jj=j, ii=i; 971 if (rotate) { jj=7-j; ii=7-i; } 972 tft.fillRect(ii*24+12,jj*24,25,25,color); 973 tft.drawRect(ii*24+12,jj*24,25,25,WHITE); 974 color=BLACK; color_cont=GRAY; 975 if (pole[j][i]>0) { color=WHITE; color_cont=BLACK; } 976 if (pole[j][i]!=0) { 977 drawBitmap(ii*24+17, jj*24+5,&fig[abs(pole[j][i])-1][0], 16, 16,color); 978 drawBitmap(ii*24+17, jj*24+5,&fig_cont[abs(pole[j][i])-1][0], 16, 16,color_cont); 979 } 980} 981//**************************** 982void show_board() { 983 for (int i=0;i<8;i++) 984 for (int j=0;j<8;j++) { 985 if (poledisp[j][i]!=pole[j][i]) show_fig(i,j); 986 poledisp[j][i]=pole[j][i]; 987 } 988} 989//**************************** 990String str_step(int i) { 991 String s=""; 992 if (steps[i].fig1==0) return s; 993 if (steps[i].type==2) s="0-0"; 994 else if (steps[i].type==3) s="0-0-0"; 995 else { 996 if (abs(steps[i].fig1)>1) s=fig_symb[abs(steps[i].fig1)]; 997 if (abs(steps[i].fig1<5)) { 998 s=s+char(97+steps[i].y1); 999 s=s+String(8-steps[i].x1); 1000 if (steps[i].fig2==0) s=s+"-"; 1001 } 1002 if (steps[i].fig2!=0) { 1003 s=s+"x"; 1004 if (abs(steps[i].fig2)>1) s=s+fig_symb[abs(steps[i].fig2)]; 1005 } 1006 s=s+char(97+steps[i].y2); 1007 s=s+String(8-steps[i].x2); 1008 } 1009 if (steps[i].type>3) s=s+fig_symb[steps[i].type-2]; 1010 if (steps[i].check==1) s=s+"+"; else 1011 if (steps[i].check==2) s=s+"#"; 1012 return s; 1013} 1014//**************************** 1015void show_steps() { 1016 tft.fillRect(206,0,114,214,getColor(8,8,8)); 1017 tft.fillRect(206,0,114,12,DARK); 1018 tft.setTextSize(1); 1019 tft.setTextColor(WHITE,DARK); 1020 tft.setCursor(206,2); 1021 tft.print(" ArduinoMega Chess "); 1022 int i=1; int y=14; 1023 tft.setTextColor(GRAY); 1024 int cur=(cur_step+1)/2; // 1025 int lim_step=cur_step; 1026 if (checkmatesearch&&lastscore>9000) { 1027 tft.setTextColor(GREEN); 1028 lim_step=cur_step+10000-lastscore; 1029 cur=lim_step/2; 1030 } 1031 if (cur>20) i=cur-20; 1032 while (i<=cur&&y<207) { // 1033 tft.setCursor(210,y); 1034 tft.print(i); 1035 tft.print("."); 1036 if (steps[i*2-1].fig1!=0) tft.print(str_step(i*2-1)); 1037 if (steps[i*2].fig1==0||i*2>lim_step) break; 1038 tft.print(","); 1039 tft.print(str_step(i*2)); 1040 i++; y+=10; 1041 } 1042 1043} 1044//**************************** 1045boolean getdiagrowcheckw(char dj,char di) { // 1046char d,j1,i1; 1047 j1=WKJ; i1=WKI; 1048 for (d=1;d<8;d++) { 1049 j1+=dj; i1+=di; 1050 if (getpole(j1,i1)) { 1051 if (pole[j1][i1]==-fq||pole[j1][i1]==-fb) return true; 1052 if (pole[j1][i1]!=0) break; 1053 } else break; 1054 } 1055 return false; 1056} 1057//**************************** 1058boolean getdiagrowcheckb(char dj,char di) { // 1059char d,j1,i1; 1060 j1=BKJ; i1=BKI; 1061 for (d=1;d<8;d++) { 1062 j1+=dj; i1+=di; 1063 if (getpole(j1,i1)) { 1064 if (pole[j1][i1]==fq||pole[j1][i1]==fb) return true; 1065 if (pole[j1][i1]!=0) break; 1066 } else break; 1067 } 1068 return false; 1069} 1070//**************************** 1071boolean getstreightrowcheckw(char dj,char di) { // - 1072char d,j1,i1; 1073 j1=WKJ; i1=WKI; 1074 for (d=1;d<8;d++) { 1075 j1+=dj; i1+=di; 1076 if (getpole(j1,i1)) { 1077 if (pole[j1][i1]==-fq||pole[j1][i1]==-fr) return true; 1078 if (pole[j1][i1]!=0) break; 1079 } else break; 1080 } 1081 return false; 1082} 1083//**************************** 1084boolean getstreightrowcheckb(char dj,char di) { // - 1085char d,j1,i1; 1086 j1=BKJ; i1=BKI; 1087 for (d=1;d<8;d++) { 1088 j1+=dj; i1+=di; 1089 if (getpole(j1,i1)) { 1090 if (pole[j1][i1]==fq||pole[j1][i1]==fr) return true; 1091 if (pole[j1][i1]!=0) break; 1092 } else break; 1093 } 1094 return false; 1095} 1096//**************************** 1097boolean get_check(char king) { // 1098 if (king==fk) { // 1099 if (getdiagrowcheckw(-1, 1)) return true; 1100 if (getdiagrowcheckw(-1,-1)) return true; 1101 if (getdiagrowcheckw( 1,-1)) return true; 1102 if (getdiagrowcheckw( 1, 1)) return true; 1103 if (getstreightrowcheckw(-1, 0)) return true; 1104 if (getstreightrowcheckw( 0, 1)) return true; 1105 if (getstreightrowcheckw( 0,-1)) return true; 1106 if (getstreightrowcheckw( 1, 0)) return true; 1107 if (getpole(WKJ-2,WKI-1)&&pole[WKJ-2][WKI-1]==-fn) return true; 1108 if (getpole(WKJ-2,WKI+1)&&pole[WKJ-2][WKI+1]==-fn) return true; 1109 if (getpole(WKJ-1,WKI-2)&&pole[WKJ-1][WKI-2]==-fn) return true; 1110 if (getpole(WKJ-1,WKI+2)&&pole[WKJ-1][WKI+2]==-fn) return true; 1111 if (getpole(WKJ+1,WKI-2)&&pole[WKJ+1][WKI-2]==-fn) return true; 1112 if (getpole(WKJ+1,WKI+2)&&pole[WKJ+1][WKI+2]==-fn) return true; 1113 if (getpole(WKJ+2,WKI-1)&&pole[WKJ+2][WKI-1]==-fn) return true; 1114 if (getpole(WKJ+2,WKI+1)&&pole[WKJ+2][WKI+1]==-fn) return true; 1115 if (getpole(WKJ-1,WKI-1)&&pole[WKJ-1][WKI-1]==-fp) return true; 1116 if (getpole(WKJ-1,WKI+1)&&pole[WKJ-1][WKI+1]==-fp) return true; 1117 } else { // 1118 if (getdiagrowcheckb( 1,-1)) return true; 1119 if (getdiagrowcheckb( 1, 1)) return true; 1120 if (getdiagrowcheckb(-1, 1)) return true; 1121 if (getdiagrowcheckb(-1,-1)) return true; 1122 if (getstreightrowcheckb( 1, 0)) return true; 1123 if (getstreightrowcheckb( 0, 1)) return true; 1124 if (getstreightrowcheckb( 0,-1)) return true; 1125 if (getstreightrowcheckb(-1, 0)) return true; 1126 if (getpole(BKJ+2,BKI-1)&&pole[BKJ+2][BKI-1]==fn) return true; 1127 if (getpole(BKJ+2,BKI+1)&&pole[BKJ+2][BKI+1]==fn) return true; 1128 if (getpole(BKJ+1,BKI-2)&&pole[BKJ+1][BKI-2]==fn) return true; 1129 if (getpole(BKJ+1,BKI+2)&&pole[BKJ+1][BKI+2]==fn) return true; 1130 if (getpole(BKJ-1,BKI-2)&&pole[BKJ-1][BKI-2]==fn) return true; 1131 if (getpole(BKJ-1,BKI+2)&&pole[BKJ-1][BKI+2]==fn) return true; 1132 if (getpole(BKJ-2,BKI-1)&&pole[BKJ-2][BKI-1]==fn) return true; 1133 if (getpole(BKJ-2,BKI+1)&&pole[BKJ-2][BKI+1]==fn) return true; 1134 if (getpole(BKJ+1,BKI-1)&&pole[BKJ+1][BKI-1]==fp) return true; 1135 if (getpole(BKJ+1,BKI+1)&&pole[BKJ+1][BKI+1]==fp) return true; 1136 } 1137 if (abs(BKJ-WKJ)<=1&&abs(BKI-WKI)<=1) return true; // 1138 return false; 1139} 1140//**************************** 1141boolean getpole(char j,char i) { // 1142 if (j>=0&&j<8&&i>=0&&i<8) return true; 1143 return false; 1144} 1145//**************************** 1146void addstep(char j1,char i1,char j2,char i2,char type) { 1147 int st=start_var+cur_var; 1148 steps[st].x1=j1; 1149 steps[st].x2=j2; 1150 steps[st].y1=i1; 1151 steps[st].y2=i2; 1152 steps[st].fig1=pole[j1][i1]; 1153 steps[st].fig2=pole[j2][i2]; 1154 if (type==1) { // 1155 steps[st].fig2=-steps[st].fig1; 1156 } 1157 steps[st].type=type; 1158 char ko=-fk; // 1159 if (steps[st].fig1>0) ko=fk; 1160 movestep(st); 1161 if (get_check(ko)) { backstep(st); return; } // - 1162 boolean che=get_check(-ko); // 1163 backstep(st); 1164 steps[st].weight=abs(steps[st].fig2)-abs(steps[st].fig1); 1165 if (type>3) steps[st].weight+=fig_weight[type-2]; 1166 if (endspiel&&steps[st].fig1==ko) steps[st].weight+=10; // - 1167 steps[st].check=che; 1168 if (che) steps[st].weight+=10; 1169 if (only_action) { 1170 if (steps[st].fig1==fp&&steps[st].x2==1||steps[st].fig1==-fp&&steps[st].x2==6) // 1171 { cur_var++; return; } 1172 if (steps[st].fig2==0&&steps[st].type<4&&!che&&!check_on_table) return; 1173 } 1174 1175 cur_var++; 1176} 1177//**************************** 1178void getrowstepsw(char j,char i,char dj,char di) { // 1179char d,j1,i1; 1180 j1=j; i1=i; 1181 for (d=1;d<8;d++) { 1182 j1+=dj; i1+=di; 1183 if (getpole(j1,i1)) { 1184 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1185 if (pole[j1][i1]!=0) break; 1186 } else break; 1187 } 1188} 1189//**************************** 1190void getrowstepsb(char j,char i,char dj,char di) { // 1191char d,j1,i1; 1192 j1=j; i1=i; 1193 for (d=1;d<8;d++) { 1194 j1+=dj; i1+=di; 1195 if (getpole(j1,i1)) { 1196 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1197 if (pole[j1][i1]!=0) break; 1198 } else break; 1199 } 1200} 1201//**************************** 1202void getonestepw(char j,char i,char dj,char di) { // 1203char j1,i1; 1204 j1=j+dj; i1=i+di; 1205 if (getpole(j1,i1)) 1206 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1207} 1208//**************************** 1209void getonestepb(char j,char i,char dj,char di) { // 1210char j1,i1; 1211 j1=j+dj; i1=i+di; 1212 if (getpole(j1,i1)) 1213 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1214} 1215//**************************** 1216void sort_variants(int from, int to) { // 1217 while (1) { 1218 int mov=0; 1219 for (int i=from;i<to;i++) // 1220 if (steps[i].weight<steps[i+1].weight) { 1221 mov++; 1222 step_type buf=steps[i]; 1223 steps[i]=steps[i+1]; 1224 steps[i+1]=buf; 1225 } 1226 if (mov==0) break; 1227 } 1228} 1229//**************************** 1230void load_variants(int nstep) { // nstep 1231 cur_var=0; 1232 if (pole[WKJ][WKI]!=fk||pole[BKJ][BKI]!=-fk) kingpositions(); 1233 if (nstep%2==1) check_on_table=get_check(fk); 1234 else check_on_table=get_check(-fk); // 1235 for (char i=0;i<8;i++) 1236 for (char j=0;j<8;j++) 1237 if (pole[j][i]>0&&nstep%2==1||pole[j][i]<0&&nstep%2==0) { 1238 switch (pole[j][i]) { 1239 case fp: // 1240 if (getpole(j-1,i)&&pole[j-1][i]==0) 1241 if (j!=1) addstep(j,i,j-1,i,0); else 1242 for (char t=4;t<8;t++) addstep(1,i,0,i,t); //-... 1243 if (j==6&&pole[j-1][i]==0&&pole[j-2][i]==0) addstep(j,i,j-2,i,0); 1244 if (getpole(j-1,i-1)&&pole[j-1][i-1]<0) 1245 if (j!=1) addstep(j,i,j-1,i-1,0); else 1246 for (char t=4;t<8;t++) addstep(j,i,j-1,i-1,t); //-... 1247 if (getpole(j-1,i+1)&&pole[j-1][i+1]<0) 1248 if (j!=1) addstep(j,i,j-1,i+1,0); else 1249 for (char t=4;t<8;t++) addstep(j,i,j-1,i+1,t); //-... 1250 if (j==3&&steps[nstep-1].fig1==-fp&&steps[nstep-1].x2==3&&steps[nstep-1].x1==1) { 1251 if (steps[nstep-1].y2-i==1) { // 1252 addstep(j,i,j-1,i+1,1); 1253 } else if (steps[nstep-1].y2-i==-1) { // 1254 addstep(j,i,j-1,i-1,1); 1255 } 1256 } 1257 break; 1258 case -fp: // 1259 if (getpole(j+1,i)&&pole[j+1][i]==0) 1260 if (j!=6) addstep(j,i,j+1,i,0); else 1261 for (char t=4;t<8;t++) addstep(j,i,j+1,i,t); //-... 1262 if (j==1&&pole[j+1][i]==0&&pole[j+2][i]==0) addstep(j,i,j+2,i,0); 1263 if (getpole(j+1,i-1)&&pole[j+1][i-1]>0) 1264 if (j!=6) addstep(j,i,j+1,i-1,0); else 1265 for (char t=4;t<8;t++) addstep(j,i,j+1,i-1,t); //-... 1266 if (getpole(j+1,i+1)&&pole[j+1][i+1]>0) 1267 if (j!=6) addstep(j,i,j+1,i+1,0); else 1268 for (char t=4;t<8;t++) addstep(j,i,j+1,i+1,t); //-... 1269 if (j==4&&steps[nstep-1].fig1==fp&&steps[nstep-1].x2==4&&steps[nstep-1].x1==6) { 1270 if (steps[nstep-1].y2-i==1) { // 1271 addstep(j,i,j+1,i+1,1); 1272 } else if (steps[nstep-1].y2-i==-1) { // 1273 addstep(j,i,j+1,i-1,1); 1274 } 1275 } 1276 break; 1277 case fn: // 1278 getonestepw(j,i,-2,-1); 1279 getonestepw(j,i,-2,1); 1280 getonestepw(j,i,-1,-2); 1281 getonestepw(j,i,-1,2); 1282 getonestepw(j,i,2,-1); 1283 getonestepw(j,i,2,1); 1284 getonestepw(j,i,1,-2); 1285 getonestepw(j,i,1,2); 1286 break; 1287 case -fn: // 1288 getonestepb(j,i,-2,-1); 1289 getonestepb(j,i,-2,1); 1290 getonestepb(j,i,-1,-2); 1291 getonestepb(j,i,-1,2); 1292 getonestepb(j,i,2,-1); 1293 getonestepb(j,i,2,1); 1294 getonestepb(j,i,1,-2); 1295 getonestepb(j,i,1,2); 1296 break; 1297 case fb: // 1298 getrowstepsw(j,i,1,1); 1299 getrowstepsw(j,i,-1,-1); 1300 getrowstepsw(j,i,1,-1); 1301 getrowstepsw(j,i,-1,1); 1302 break; 1303 case -fb: // 1304 getrowstepsb(j,i,1,1); 1305 getrowstepsb(j,i,-1,-1); 1306 getrowstepsb(j,i,1,-1); 1307 getrowstepsb(j,i,-1,1); 1308 break; 1309 case fr: // 1310 getrowstepsw(j,i,1,0); 1311 getrowstepsw(j,i,-1,0); 1312 getrowstepsw(j,i,0,1); 1313 getrowstepsw(j,i,0,-1); 1314 break; 1315 case -fr: // 1316 getrowstepsb(j,i,1,0); 1317 getrowstepsb(j,i,-1,0); 1318 getrowstepsb(j,i,0,1); 1319 getrowstepsb(j,i,0,-1); 1320 break; 1321 case fq: // 1322 getrowstepsw(j,i,1,1); 1323 getrowstepsw(j,i,-1,-1); 1324 getrowstepsw(j,i,1,-1); 1325 getrowstepsw(j,i,-1,1); 1326 getrowstepsw(j,i,1,0); 1327 getrowstepsw(j,i,-1,0); 1328 getrowstepsw(j,i,0,1); 1329 getrowstepsw(j,i,0,-1); 1330 break; 1331 case -fq: // 1332 getrowstepsb(j,i,1,1); 1333 getrowstepsb(j,i,-1,-1); 1334 getrowstepsb(j,i,1,-1); 1335 getrowstepsb(j,i,-1,1); 1336 getrowstepsb(j,i,1,0); 1337 getrowstepsb(j,i,-1,0); 1338 getrowstepsb(j,i,0,1); 1339 getrowstepsb(j,i,0,-1); 1340 break; 1341 case fk: // 1342 getonestepw(j,i, 1,-1); 1343 getonestepw(j,i, 1, 0); 1344 getonestepw(j,i, 1, 1); 1345 getonestepw(j,i, 0,-1); 1346 getonestepw(j,i, 0, 1); 1347 getonestepw(j,i,-1,-1); 1348 getonestepw(j,i,-1, 0); 1349 getonestepw(j,i,-1, 1); 1350 if (!check_on_table) add_rok(j,i,nstep); 1351 break; 1352 case -fk: // 1353 getonestepb(j,i, 1,-1); 1354 getonestepb(j,i, 1, 0); 1355 getonestepb(j,i, 1, 1); 1356 getonestepb(j,i, 0,-1); 1357 getonestepb(j,i, 0, 1); 1358 getonestepb(j,i,-1,-1); 1359 getonestepb(j,i,-1, 0); 1360 getonestepb(j,i,-1, 1); 1361 if (!check_on_table) add_rok(j,i,nstep); 1362 break; 1363 } //switch 1364 } //if 1365 1366 if (nstep>cur_step) { 1367 for (int i=start_var;i<start_var+cur_var;i++) { // 1368 for (char j=0;j<MAXCUTS;j++) 1369 if (cuts[j].fig1==steps[i].fig1&& 1370 cuts[j].x1==steps[i].x1&&cuts[j].y1==steps[i].y1&& 1371 cuts[j].x2==steps[i].x2&&cuts[j].y2==steps[i].y2) { 1372 steps[i].weight+=100+cuts[j].weight; 1373 break; 1374 } 1375 if (nstep>1&&steps[nstep-1].fig2!=0) // - 1376 if (steps[nstep-1].x2==steps[i].x2&&steps[nstep-1].y2==steps[i].y2) 1377 steps[i].weight+=1000; 1378 } 1379 } 1380 1381 sort_variants(start_var,start_var+cur_var-1); 1382 1383 } 1384//**************************** 1385void movestep(int nstep) { 1386 pole[steps[nstep].x1][steps[nstep].y1]=0; 1387 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig1; 1388 if (steps[nstep].fig1==fk) { 1389 WKJ=steps[nstep].x2; WKI=steps[nstep].y2; 1390 } else if (steps[nstep].fig1==-fk) { 1391 BKJ=steps[nstep].x2; BKI=steps[nstep].y2; 1392 } 1393 if (steps[nstep].type==0) return; 1394 if (steps[nstep].type==1) // 1395 if (steps[nstep].fig1>0) 1396 pole[steps[nstep].x2+1][steps[nstep].y2]=0; 1397 else 1398 pole[steps[nstep].x2-1][steps[nstep].y2]=0; 1399 else if (steps[nstep].type==2) // 1400 if (steps[nstep].fig1>0) { // 1401 pole[7][4]=0; pole[7][5]=fr; pole[7][6]=fk; pole[7][7]=0; //show_board(); delay(3000); 1402 } else { // 1403 pole[0][4]=0; pole[0][5]=-fr; pole[0][6]=-fk; pole[0][7]=0; //show_board(); delay(3000); 1404 } 1405 else if (steps[nstep].type==3) // 1406 if (steps[nstep].fig1>0) { // 1407 pole[7][0]=0; pole[7][1]=0; pole[7][2]=fk; pole[7][3]=fr; pole[7][4]=0; 1408 } else { // 1409 pole[0][0]=0; pole[0][1]=0; pole[0][2]=-fk; pole[0][3]=-fr; pole[0][4]=0; 1410 } 1411 else if (steps[nstep].type>3) //-.... 1412 if (steps[nstep].fig1>0) pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].type-2; 1413 else pole[steps[nstep].x2][steps[nstep].y2]=2-steps[nstep].type; 1414} 1415//**************************** 1416void backstep(int nstep) { 1417 pole[steps[nstep].x1][steps[nstep].y1]=steps[nstep].fig1; 1418 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig2; 1419 if (steps[nstep].fig1==fk) { 1420 WKJ=steps[nstep].x1; WKI=steps[nstep].y1; 1421 } else if (steps[nstep].fig1==-fk) { 1422 BKJ=steps[nstep].x1; BKI=steps[nstep].y1; 1423 } 1424 if (steps[nstep].type==0) return; 1425 if (steps[nstep].type==1) { // 1426 pole[steps[nstep].x2][steps[nstep].y2]=0; 1427 if (steps[nstep].fig1>0) 1428 pole[steps[nstep].x2+1][steps[nstep].y2]=-fp; 1429 else 1430 pole[steps[nstep].x2-1][steps[nstep].y2]=fp; 1431 } else if (steps[nstep].type==2) // 1432 if (steps[nstep].fig1>0) { // 1433 pole[7][4]=fk; pole[7][5]=0; pole[7][6]=0; pole[7][7]=fr; 1434 } else { // 1435 pole[0][4]=-fk; pole[0][5]=0; pole[0][6]=0; pole[0][7]=-fr; // show_board(); delay(3000); 1436 } 1437 else if (steps[nstep].type==3) // 1438 if (steps[nstep].fig1>0) { // 1439 pole[7][0]=fr; pole[7][1]=0; pole[7][2]=0; pole[7][3]=0; pole[7][4]=fk; 1440 } else { // 1441 pole[0][0]=-fr; pole[0][1]=0; pole[0][2]=0; pole[0][3]=0; pole[0][4]=-fk; 1442 } 1443 1444} 1445//**************************** 1446void get_wrocks(int nstep) { 1447 w00=true; w000=true; 1448 for (int i=1;i<nstep;i++) { 1449 if (steps[i].fig1==fk) { w00=false; w000=false; return; } 1450 if (steps[i].fig1==fr) 1451 if (steps[i].x1==7&&steps[i].y1==7) w00=false; 1452 else if (steps[i].x1==7&&steps[i].y1==0) w000=false; 1453 } 1454} 1455//**************************** 1456void get_brocks(int nstep) { 1457 b00=true; b000=true; 1458 for (int i=1;i<nstep;i++) { 1459 if (steps[i].fig1==-fk) { b00=false; b000=false; return; } 1460 if (steps[i].fig1==-fr) 1461 if (steps[i].x1==0&&steps[i].y1==7) b00=false; 1462 else if (steps[i].x1==0&&steps[i].y1==0) b000=false; 1463 } 1464} 1465//**************************** 1466void add_rok(char j,char i,int nstep) { // 1467boolean che1,che2; 1468 if (nstep%2==1) { // 1469 if (j!=7||i!=4) return; 1470 if (pole[7][5]==0&&pole[7][6]==0&&pole[7][7]==fr) { // 1471 pole[7][4]=0; 1472 WKI=5; che1=get_check(fk); 1473 WKI=6; che2=get_check(fk); 1474 WKI=4; pole[7][4]=fk; 1475 get_wrocks(nstep); 1476 if (!che1&&!che2&&w00) addstep(7,4,7,6,2); 1477 } 1478 if (pole[7][0]==fr&&pole[7][1]==0&&pole[7][2]==0&&pole[7][3]==0) { // 1479 pole[7][4]=0; 1480 WKI=2; che1=get_check(fk); 1481 WKI=3; che2=get_check(fk); 1482 WKI=4; pole[7][4]=fk; 1483 get_wrocks(nstep); 1484 if (!che1&&!che2&&w000) addstep(7,4,7,2,3); 1485 } 1486 } else { // 1487 if (j!=0||i!=4) return; 1488 if (pole[0][5]==0&&pole[0][6]==0&&pole[0][7]==-fr) { // 1489 pole[0][4]=0; 1490 BKI=5; che1=get_check(-fk); 1491 BKI=6; che2=get_check(-fk); 1492 BKI=4; pole[0][4]=-fk; 1493 get_brocks(nstep); 1494 if (!che1&&!che2&&b00) addstep(0,4,0,6,2); 1495 } 1496 if (pole[0][0]==-fr&&pole[0][1]==0&&pole[0][2]==0&&pole[0][3]==0) { // 1497 pole[0][4]=0; 1498 BKI=2; che1=get_check(-fk); 1499 BKI=3; che2=get_check(-fk); 1500 BKI=4; pole[0][4]=-fk; 1501 get_brocks(nstep); 1502 if (!che1&&!che2&&b000) addstep(0,4,0,2,3); 1503 } 1504 1505 } 1506} 1507//**************************** 1508int addrowstepsw(char j,char i,char dj,char di,char dc) { // 1509char d,j1,i1; int c=0; 1510 j1=j; i1=i; 1511 for (d=1;d<8;d++) { 1512 j1+=dj; i1+=di; 1513 if (getpole(j1,i1)) { 1514 if (pole[j1][i1]==0) c+=dc; 1515 else if (pole[j1][i1]>0) { // 1516 c+=1; break; 1517 } else { 1518 if (cur_step>6) c+=-pole[j1][i1]; // 1519 break; 1520 } 1521 } else break; 1522 } 1523 return c; 1524} 1525//**************************** 1526int addrowstepsb(char j,char i,char dj,char di,char dc) { // 1527char d,j1,i1; int c=0; 1528 j1=j; i1=i; 1529 for (d=1;d<8;d++) { 1530 j1+=dj; i1+=di; 1531 if (getpole(j1,i1)) { 1532 if (pole[j1][i1]==0) c-=dc; 1533 else if (pole[j1][i1]<0) { // 1534 c-=1; break; 1535 } else { 1536 if (cur_step>6) c-=pole[j1][i1]; // 1537 break; 1538 } 1539 } else break; 1540 } 1541 return c; 1542} 1543//**************************** 1544int addonestepw(char j,char i,char dj,char di) { // 1545char j1,i1; 1546 j1=j+dj; i1=i+di; 1547 if (getpole(j1,i1)) 1548 if (pole[j1][i1]==0) return 2; 1549 else if (pole[j1][i1]>0) return 1; 1550 else return -pole[j1][i1]; 1551 return 0; 1552} 1553//**************************** 1554int addonestepb(char j,char i,char dj,char di) { // 1555char j1,i1; 1556 j1=j+dj; i1=i+di; 1557 if (getpole(j1,i1)) 1558 if (pole[j1][i1]==0) return 2; 1559 else if (pole[j1][i1]<0) return 1; 1560 else return pole[j1][i1]; 1561 return 0; 1562} 1563//**************************** 1564int activity() { // - + 1565int c=0; 1566char pwj[8],pwi[8],pbj[8],pbi[8],ipw=0,ipb=0,nbw=0,nbb=0; 1567 if (checkmatesearch) return 0; // 1568 for (char i=0;i<8;i++) 1569 for (char j=0;j<8;j++) 1570 if (pole[j][i]!=0) { 1571 switch (pole[j][i]) { 1572 case fp: // 1573 if (getpole(j,i+1)&&pole[j][i+1]==fp) c+=2; // 1574 if (getpole(j-1,i)&&pole[j-1][i]==fp) c-=20; // 1575 pwj[ipw]=j; pwi[ipw]=i; ipw++; 1576 break; 1577 case -fp: // 1578 if (getpole(j,i-1)&&pole[j][i-1]==-fp) c-=2; // 1579 if (getpole(j+1,i)&&pole[j+1][i]==-fp) c+=20; // 1580 pbj[ipb]=j; pbi[ipb]=i; ipb++; 1581 break; 1582 case fn: // 1583 c+=addonestepw(j,i,-2,-1); 1584 c+=addonestepw(j,i,-2,1); 1585 c+=addonestepw(j,i,-1,-2); 1586 c+=addonestepw(j,i,-1,2); 1587 c+=addonestepw(j,i,2,-1); 1588 c+=addonestepw(j,i,2,1); 1589 c+=addonestepw(j,i,1,-2); 1590 c+=addonestepw(j,i,1,2); 1591 break; 1592 case -fn: // 1593 c+=addonestepb(j,i,-2,-1); 1594 c+=addonestepb(j,i,-2,1); 1595 c+=addonestepb(j,i,-1,-2); 1596 c+=addonestepb(j,i,-1,2); 1597 c+=addonestepb(j,i,2,-1); 1598 c+=addonestepb(j,i,2,1); 1599 c+=addonestepb(j,i,1,-2); 1600 c+=addonestepb(j,i,1,2); 1601 break; 1602 case fb: // 1603 c+=addrowstepsw(j,i,1,1,1); 1604 c+=addrowstepsw(j,i,-1,-1,2); 1605 c+=addrowstepsw(j,i,1,-1,1); 1606 c+=addrowstepsw(j,i,-1,1,2); 1607 nbw++; 1608 break; 1609 case -fb: // 1610 c+=addrowstepsb(j,i,1,1,2); 1611 c+=addrowstepsb(j,i,-1,-1,1); 1612 c+=addrowstepsb(j,i,1,-1,2); 1613 c+=addrowstepsb(j,i,-1,1,1); 1614 nbb++; 1615 break; 1616 case fr: // 1617 c+=addrowstepsw(j,i,1,0,2); // 1618 c+=addrowstepsw(j,i,-1,0,2); 1619 c+=addrowstepsw(j,i,0,1,1); 1620 c+=addrowstepsw(j,i,0,-1,1); 1621 break; 1622 case -fr: // 1623 c+=addrowstepsb(j,i,1,0,2); // 1624 c+=addrowstepsb(j,i,-1,0,2); 1625 c+=addrowstepsb(j,i,0,1,1); 1626 c+=addrowstepsb(j,i,0,-1,1); 1627 break; 1628 case fq: // 1629 if (cur_step>10) { 1630 c+=addrowstepsw(j,i,1,1,1); 1631 c+=addrowstepsw(j,i,-1,-1,2); 1632 c+=addrowstepsw(j,i,1,-1,1); 1633 c+=addrowstepsw(j,i,-1,1,2); 1634 c+=addrowstepsw(j,i,1,0,1); 1635 c+=addrowstepsw(j,i,-1,0,2); 1636 c+=addrowstepsw(j,i,0,1,1); 1637 c+=addrowstepsw(j,i,0,-1,1); 1638 } 1639 break; 1640 case -fq: // 1641 if (cur_step>10) { 1642 c+=addrowstepsb(j,i,1,1,2); 1643 c+=addrowstepsb(j,i,-1,-1,1); 1644 c+=addrowstepsb(j,i,1,-1,2); 1645 c+=addrowstepsb(j,i,-1,1,1); 1646 c+=addrowstepsb(j,i,1,0,2); 1647 c+=addrowstepsb(j,i,-1,0,1); 1648 c+=addrowstepsb(j,i,0,1,1); 1649 c+=addrowstepsb(j,i,0,-1,1); 1650 } 1651 break; 1652 } //switch 1653 if (cur_step>6) { 1654 if (pole[j][i]>0) { // 1655 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c+=3; 1656 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c+=3; 1657 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c+=2; 1658 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c+=2; 1659 } else { // 1660 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c-=3; 1661 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c-=3; 1662 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c-=2; 1663 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c-=2; 1664 } 1665 } 1666 } 1667 if (nbw>1) c+=30; // 1668 if (nbb>1) c-=30; 1669 for (char w=0;w<ipw;w++) { // 1670 boolean pass=1; 1671 for (char b=0;b<ipb;b++) { 1672 if (pwi[w]>0&&pbi[b]==pwi[w]-1&&pbj[b]<pwj[w]) pass=0; // 1673 if (pbi[b]==pwi[w]&&pbj[b]<pwj[w]) pass=0; // 1674 if ( pwi[w]<7&&pbi[b]==pwi[w]+1&&pbj[b]<pwj[w]) pass=0; // 1675 if (!pass) break; 1676 } 1677 if (pass) { 1678 c+=50; 1679 if (!endspiel) break; // 1680 } 1681 } 1682 for (char b=0;b<ipb;b++) { // 1683 boolean pass=1; 1684 for (char w=0;w<ipw;w++) { 1685 if (pbi[b]>0&&pwi[w]==pbi[b]-1&&pwj[w]>pbj[b]) pass=0; // 1686 if (pwi[w]==pbi[b]&&pwj[w]>pbj[b]) pass=0; // 1687 if (pbi[b]<7&&pwi[w]==pbi[b]+1&&pwj[w]>pbj[b]) pass=0; // 1688 if (!pass) break; 1689 } 1690 if (pass) { 1691 c-=50; 1692 if (!endspiel) break; // 1693 } 1694 } 1695 return c; 1696} 1697//**************************** 1698int evaluate(int nstep) { // 1699 long ww=0, wb=0; 1700 for (char i=0;i<8;i++) 1701 for (char j=0;j<8;j++) 1702 if (pole[j][i]<0) { 1703 wb+=fig_weight[-pole[j][i]]+(int)pgm_read_word(&pos[-pole[j][i]-1][7-j][i]); 1704 } 1705 else if (pole[j][i]>0) { 1706 ww+=fig_weight[pole[j][i]]+(int)pgm_read_word(&pos[pole[j][i]-1][j][i]); 1707 } 1708 count++; // 1709 long str=activity(); 1710 if (endspiel) { // 1711 wb+=(int)pgm_read_word(&pos[6][7-BKJ][BKI])-(int)pgm_read_word(&pos[5][7-BKJ][BKI]); 1712 ww+=(int)pgm_read_word(&pos[6][WKJ][WKI])-(int)pgm_read_word(&pos[5][WKJ][WKI]); 1713 if (wb<450&&ww>450) { // - - 1714 str-=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1715 } else if (ww<450&&wb>450) { // 1716 str+=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1717 } 1718 } 1719 if (nstep>8) { // 1720 if (steps[nstep-1].fig1==steps[nstep-5].fig1&&steps[nstep-2].fig1==steps[nstep-6].fig1&& 1721 steps[nstep-3].fig1==steps[nstep-7].fig1&&steps[nstep-4].fig1==steps[nstep-8].fig1) 1722 if (steps[nstep-1].x1==steps[nstep-5].x1&&steps[nstep-2].x1==steps[nstep-6].x1&& 1723 steps[nstep-3].x1==steps[nstep-7].x1&&steps[nstep-4].x1==steps[nstep-8].x1) 1724 if (steps[nstep-1].x2==steps[nstep-5].x2&&steps[nstep-2].x2==steps[nstep-6].x2&& 1725 steps[nstep-3].x2==steps[nstep-7].x2&&steps[nstep-4].x2==steps[nstep-8].x2) 1726 if (steps[nstep-1].y1==steps[nstep-5].y1&&steps[nstep-2].y1==steps[nstep-6].y1&& 1727 steps[nstep-3].y1==steps[nstep-7].y1&&steps[nstep-4].y1==steps[nstep-8].x1) 1728 if (steps[nstep-1].y2==steps[nstep-5].y2&&steps[nstep-2].y2==steps[nstep-6].y2&& 1729 steps[nstep-3].y2==steps[nstep-7].y2&&steps[nstep-4].y2==steps[nstep-8].y2) 1730 { Serial.println(F(" Draw - 3 repeat")); return 0; } 1731 } 1732 if (nstep%2==1) return 5000*(ww-wb)/(ww+wb+2000)+str; else return 5000*(wb-ww)/(ww+wb+2000)-str; 1733} 1734//**************************** 1735void add_cut(int ind) {// ind 1736 int minbeta=30000, minindex=MAXCUTS-1; 1737 for (char i=0;i<MAXCUTS;i++) { 1738 if (cuts[i].weight==0) { minindex=i; break; }// 1739 if (cuts[i].fig1==steps[ind].fig1&& 1740 cuts[i].x1==steps[ind].x1&&cuts[i].y1==steps[ind].y1&& 1741 cuts[i].x2==steps[ind].x2&&cuts[i].y2==steps[ind].y2) { 1742 cuts[i].weight++; return; // - 1 1743 } 1744 if (cuts[i].weight<minbeta) { // 1745 minbeta=cuts[i].weight; 1746 minindex=i; 1747 } 1748 } 1749 cuts[minindex]=steps[ind]; 1750 cuts[minindex].weight=1; //1 1751} 1752//**************************** 1753int quiescence(int start, int nstep, int alpha, int beta ) { 1754 if (nstep-cur_step>=LIMDEPTH||start>MAXSTEPS-70) return evaluate(nstep); 1755 if (!solving&&progress==0) return -5000; 1756 int score=-20000; 1757 start_var=start; 1758 only_action=true; 1759 load_variants(nstep); 1760 if (!check_on_table) { 1761 int stand_pat = evaluate(nstep); 1762 if (stand_pat >= score) score=stand_pat; 1763 if (score>alpha) alpha=score; 1764 if (alpha>=beta) return alpha; 1765 } 1766 if (cur_var==0) { 1767 if (check_on_table) { 1768 if (TRACE) Serial.println(F("checkmate?")); 1769 return -10000+nstep-cur_step; 1770 } else return evaluate(nstep); 1771 } 1772 int j=start+cur_var; 1773 for (int i=start;i<j;i++) { // 1774 if (TRACE) { //***** 1775 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" . ")); 1776 Serial.println(str_step(i)); 1777 } 1778 movestep(i); 1779 steps[nstep]=steps[i]; 1780 int tmp=-quiescence(j+1,nstep+1,-beta,-alpha); 1781 backstep(i); 1782 if (tmp>score) score=tmp; 1783 if (score>alpha) alpha=score; 1784 if (alpha>=beta ) { 1785 add_cut(nstep); 1786 return alpha; 1787 } 1788 if (checkmatesearch&&alpha>9000) break; 1789 gui(); 1790 } 1791 return score; 1792} 1793//**************************** 1794int alphaBeta(int start, int nstep, int alpha, int beta, int depthleft) { 1795//start - 1796//nstep - 1797int score=-20000,best; 1798 if( depthleft==0) return quiescence(start,nstep,alpha,beta) ; 1799 if (start>MAXSTEPS-70) return evaluate(nstep); 1800 start_var=start; 1801 only_action=false; 1802 if (nstep!=cur_step) load_variants(nstep); 1803 if (cur_var==0) { 1804 if (check_on_table) { 1805 if (TRACE) Serial.println(F("checkmate!")); 1806 return -10000+nstep-cur_step; 1807 } 1808 return 0; 1809 } 1810 int j=start+cur_var; 1811 best=start; 1812 for (int i=start;i<j;i++) { // 1813 if (nstep==cur_step) { 1814 Serial.print(str_step(i)); Serial.print(" "); 1815 Serial.print(i-start+1); Serial.print("/"); Serial.print(j-start); 1816 if (steps[i].weight<-9000) { Serial.println(F(" checkmate")); continue; } 1817 if (steps[i].fig2!=0||steps[i].check||alpha<-100) 1818 // , , -100 1819 { LIMDEPTH=MAXDEPTH+2; Serial.print(F("+2")); } else LIMDEPTH=MAXDEPTH; 1820 } else { 1821 if (TRACE) { //***** 1822 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" ")); 1823 Serial.println(str_step(i)); 1824 } 1825 } 1826 movestep(i); 1827 steps[nstep]=steps[i]; 1828 int tmp=-alphaBeta(j+1,nstep+1,-beta,-alpha,depthleft-1); 1829 backstep(i); 1830 steps[i].weight=tmp; 1831 if (tmp>score) score=tmp; 1832 if (score>alpha) { 1833 alpha=score; 1834 if (nstep>cur_step) add_cut(nstep); // 1835 if (TRACE) { Serial.print(F("ALPHA+:")); Serial.println(score); } 1836 best=i; 1837 if (nstep==cur_step) { 1838 steps[0]=steps[best]; 1839 Serial.print(F(" BEST")); 1840 } 1841 } 1842 if (alpha>=beta ) { 1843 if (nstep>cur_step) add_cut(nstep); // 1844 if (TRACE) { Serial.print(F("BETA CUT:")); Serial.println(score); } 1845 return alpha; 1846 } 1847 if (nstep==cur_step) { 1848 Serial.print(F(" ")); Serial.println(tmp); 1849 progress=100*(i-start+1)/(j-start); 1850 if (alpha==9999||alpha==-5000) break; 1851 if (checkmatesearch&&alpha>9000&&limit<3) break; 1852 if (!solving) { 1853 if (alpha>startweight&&cur_level>1) break; 1854 else { // - +50% 1855 if (BAction.bs==F("stopping")) break; 1856 if (cur_level>1&&100*(millis()-starttime)/(limittime-starttime)>240-limit*20) break; 1857 } 1858 } 1859 } 1860 if (nstep==cur_step+1&&!solving&&progress==0) break; 1861 gui(); 1862 } 1863 if (nstep==cur_step) { steps[nstep]=steps[best]; steps[0]=steps[best]; } 1864 return score; 1865} 1866//**************************** 1867void kingpositions() { 1868 for (char i=0;i<8;i++) // 1869 for (char j=0;j<8;j++) 1870 if (pole[j][i]==fk) { 1871 WKJ=j; WKI=i; 1872 } else if (pole[j][i]==-fk) { 1873 BKJ=j; BKI=i; 1874 } 1875} 1876//**************************** 1877int get_endspiel() { // 1878 int weight=0; 1879 for (char i=0;i<8;i++) 1880 for (char j=0;j<8;j++) 1881 if (pole[j][i]<0) 1882 weight+=fig_weight[-pole[j][i]]; 1883 else if (pole[j][i]>0) 1884 weight+=fig_weight[pole[j][i]]; // 8000 1885 if (weight<3500) endspiel=true; else endspiel=false; 1886 return weight; 1887} 1888//**************************** 1889boolean is_drawn() { // 1890 boolean drawn=false; 1891 int cn=0,cbw=0,cbb=0,co=0,cb=0,cw=0; 1892 for (char i=0;i<8;i++) 1893 for (char j=0;j<8;j++) { 1894 if (abs(pole[j][i])==1) co++; 1895 if (abs(pole[j][i])>3&&abs(pole[j][i])<6) co++; // , , 1896 if (abs(pole[j][i])==6) continue; // 1897 if (abs(pole[j][i])==2) cn++; // 1898 if (abs(pole[j][i])==3&&(i+j+2)%2==0) cbb++; // 1899 if (abs(pole[j][i])==3&&(i+j+2)%2==1) cbw++; // 1900 if (pole[j][i]==3) cw++; // 1901 if (pole[j][i]==-3) cb++; // 1902 } 1903 if (cn==1&&co+cbb+cbw==0) drawn=true; // 1904 if (cbb+cbw==1&&co+cn==0) drawn=true; // 1905 if (co+cn+cbb==0||co+cn+cbw==0) drawn=true; // 1906 if (co+cn==0&&cb==1&&cw==1) drawn=true; // 1907 if (drawn) return drawn; 1908 int rep=1; 1909 for (int s=cur_step-1; s>0; s--) { 1910 if (steps[s].fig1==0) continue; 1911 backstep(s); 1912 boolean eq=true; 1913 for (char i=0;i<8;i++) 1914 for (char j=0;j<8;j++) 1915 if (pole[j][i]!=pole0[j][i]) 1916 { eq=false; break; } 1917 if (eq) rep++; 1918 if (rep>2) break; 1919 } 1920 if (rep>1) { Serial.print(rep); Serial.println(" repetitions"); } 1921 for (char i=0;i<8;i++) 1922 for (char j=0;j<8;j++) pole[j][i]=pole0[j][i]; // 1923 if (rep>2) drawn=true; 1924 return drawn; 1925} 1926//**************************** 1927int solve_step() { 1928const int LMIN[6]={2,3,4,5,6,7}; 1929const int LMAX[6]={4,6,8,10,12,14}; 1930const int LCMIN[6]={2,4,6,8,10,12}; 1931const int LCMAX[6]={4,6,8,10,12,14}; 1932boolean check_on; 1933 tft.fillRect(0,210,60,10,BLACK); 1934 tft.drawFastHLine(5,210,200,DARK); 1935 starttime=millis(); 1936 isstatus=1; 1937 deletebuttons(); 1938 BAction.Show("STOP"); 1939 BBW.Hide(); 1940 BBack.Hide(); 1941 BLimit.Hide(); 1942 BCM.Hide(); 1943 BDesc.Hide(); 1944 BSound.Hide(); 1945 BRotate.Hide(); 1946 limittime=starttime+limits[limit]*1000; // 1947 for (char i=0;i<8;i++) 1948 for (char j=0;j<8;j++) pole0[j][i]=pole[j][i]; // 1949 lastbest.fig1=0; 1950 kingpositions(); 1951 int wei=get_endspiel(); 1952 count=0; 1953 startweight=evaluate(cur_step); 1954 Serial.println(""); 1955 Serial.println(F("---------------")); 1956 if (endspiel) { Serial.print(F("Endspiel: ")); Serial.println(wei); } 1957 if (cur_step%2==1) Serial.print(F("WHITE, ")); else Serial.print(F("BLACK, ")); 1958 Serial.print(F("start Score= ")); Serial.println(startweight); 1959 start_var=cur_step+21; 1960 only_action=false; 1961 lastbest.fig1=0; 1962 steps[0].fig1=0; 1963 load_variants(cur_step); 1964 check_on=check_on_table; 1965 if (cur_var==0||is_drawn()) { 1966 beep(500); 1967 tft.drawFastHLine(5,210,200,BLACK); 1968 tft.drawFastHLine(5,212,200,BLACK); 1969 BAction.Hide(); 1970 isstatus=0; 1971 solving=false; 1972 if (check_on_table) return -9999; else return 8999; 1973 } 1974 if (cur_var==1) { // 1 1975 tft.drawFastHLine(5,210,200,BLACK); 1976 tft.drawFastHLine(5,212,200,BLACK); 1977 BAction.Hide(); 1978 isstatus=0; 1979 solving=false; 1980 steps[cur_step]=steps[cur_step+21]; 1981 return startweight; 1982 } 1983 int vars=cur_var; 1984 int ALPHA=-20000; 1985 int BETA=20000; 1986 int score; 1987 solving=true; 1988 if (checkmatesearch) { 1989 Serial.println(F("Checkmate search")); 1990 steps[cur_step].fig1=0; lastscore=0; show_steps(); 1991 } 1992 int l=0; 1993 for (char i=0;i<MAXCUTS;i++) { cuts[i].weight=0; cuts[i].fig1=0; } // 1994 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // 1995 movestep(i); 1996 steps[i].weight=evaluate(cur_step); 1997 if (steps[i].fig2!=0) steps[i].weight-=steps[i].fig1; 1998 backstep(i); 1999 } 2000 while (l<6) { 2001 cur_level=l+1; 2002 progress=0; 2003 tft.drawFastHLine(5,212,200,DARK); 2004 MINDEPTH=LMIN[l]; 2005 MAXDEPTH=LMAX[l]; 2006 if (checkmatesearch) { 2007 ALPHA=9000; BETA=10000; 2008 MINDEPTH=LCMIN[l]; 2009 MAXDEPTH=LCMAX[l]; 2010 } 2011 if (l>0) Serial.println(""); Serial.print(F("******* LEVEL=")); Serial.print(l+1); 2012 Serial.print(" "); Serial.print(MINDEPTH); Serial.print("-"); Serial.print(MAXDEPTH); 2013 Serial.print(" "); Serial.print((millis()-starttime)/1000.,1); Serial.println("s"); 2014 if (checkmatesearch) { 2015 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // - 2016 movestep(i); 2017 start_var=cur_step+22+vars; 2018 only_action=false; 2019 load_variants(cur_step+1); 2020 backstep(i); 2021 steps[i].weight=-cur_var; 2022 } 2023 } 2024 sort_variants(cur_step+21,cur_step+20+vars); // 2025 cur_var=vars; 2026 check_on_table=check_on; 2027 score=alphaBeta(cur_step+21,cur_step,ALPHA,BETA,MINDEPTH); 2028 if (score>9996) break; 2029 if (score>9000&&limit<3) break; 2030 if (100*(millis()-starttime)/(limittime-starttime)>70-l*8) break; 2031 if (!solving) break; 2032 l++; 2033 } //while l 2034 if (score<-9000) { 2035 Serial.println(F("GIVE UP!")); 2036 } else { 2037 Serial.print(F("STEP=")); Serial.println(str_step(cur_step)); 2038 if (score>9000) { 2039 Serial.print(F("CHECKMATE ")); 2040 if (score<9999) { 2041 Serial.print(F("in ")); Serial.print((9999-score)/2+1); Serial.print(F(" steps")); 2042 } else steps[cur_step].check=2; 2043 Serial.println(""); 2044 if (checkmatesearch) { 2045 steps[cur_step+9999-score].check=2; 2046 steps[cur_step+10000-score].fig1=0; 2047 for (int i=cur_step;i<cur_step+10000-score;i++) Serial.println(str_step(i)); 2048 } 2049 } 2050 } 2051 tft.drawFastHLine(5,210,200,BLACK); 2052 tft.drawFastHLine(5,212,200,BLACK); 2053 progress=0; 2054 show_status(); 2055 BAction.Hide(); 2056 solving=0; 2057 isstatus=0; 2058 return score; 2059 2060 2061} 2062//**************************** 2063
ArduinoMega Chess 1.0 MCUFRIEND 320x480
arduino
Sketch updated to touchscreen MCUFRIEND 320x480 pixels.
1//ArduinoMega Chess 1.0 (MCUFRIEND 320x480 version) 2//Sergey Urusov, ususovsv@gmail.com 3 4#include <UTFTGLUE.h> 5#include <stdint.h> 6#include "TouchScreen.h" 7#include <avr/pgmspace.h> 8#include <EEPROM.h> 9 10uint16_t BLACK,BLUE,RED,GREEN,CYAN,MAGENTA,YELLOW,WHITE,GRAY,DARK,GRAY2,BLACKF,WHITEF; 11MCUFRIEND_kbv tft; 12 13int XP = 6, YP = A1, XM = A2, YM = 7; 14TouchScreen ts(XP, YP, XM, YM, 300); 15TSPoint tp; 16 17int touchx,touchy,touchx0,touchy0,ntouch; 18unsigned long lasttouch; 19#define MINPRESSURE 10 20#define MAXPRESSURE 1000 21const int PinBuzz = 44; 22int cycle=0; // 23const char fp=1; 24const char fn=2; 25const char fb=3; 26const char fr=4; 27const char fq=5; 28const char fk=6; 29const int fig_weight[]={0,100,320,330,500,900,0}; 30const char fig_symb[]=" NBRQK"; 31unsigned long count; 32boolean rotate=false; 33const char polezero[8][8] PROGMEM={ 34 { 0, 0, 0, 0, 0, 0, 0, 0}, 35 { 0, 0, 0, 0, 0, 0, 0, 0}, 36 { 0, 0, 0, 0, 0, 0, 0, 0}, 37 { 0, 0, 0, 0, 0, 0, 0, 0}, 38 { 0, 0, 0, 0, 0, 0, 0, 0}, 39 { 0, 0, 0, 0, 0, 0, 0, 0}, 40 { 0, 0, 0, 0, 0, 0, 0, 0}, 41 { 0, 0, 0, 0, 0, 0, 0, 0}, 42 }; 43const char polestart[8][8] PROGMEM={ 44 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 45 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 46 { 0, 0, 0, 0, 0, 0, 0, 0}, 47 { 0, 0, 0, 0, 0, 0, 0, 0}, 48 { 0, 0, 0, 0, 0, 0, 0, 0}, 49 { 0, 0, 0, 0, 0, 0, 0, 0}, 50 { fp, fp, fp, fp, fp, fp, fp, fp}, 51 { fr, fn, fb, fq, fk, fb, fn, fr}, 52 }; 53 54char pole[8][8]={ // 3 55 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 56 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 57 { 0, 0, 0, 0, 0, 0, 0, 0}, 58 { 0, 0, 0, 0, 0, 0, 0, 0}, 59 { 0, 0, 0, 0, 0, 0, 0, 0}, 60 { 0, 0, 0, 0, 0, 0, 0, 0}, 61 { fp, fp, fp, fp, fp, fp, fp, fp}, 62 { fr, fn, fb, fq, fk, fb, fn, fr}, 63 }; 64 65char pole0[8][8]; // 66char poledisp[8][8]; // 67char polechoice[7]; // 68boolean w00,w000,b00,b000; 69char blinkstep; 70typedef struct { 71 char fig1, fig2; // 72 char x1,y1,x2,y2; // 73 char check; // 74 char type; // 1- ,2- ,3- ,4-5-6-7- ,,, 75 int weight; // , 76} step_type; 77const int MAXSTEPS=500; //. 78//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 79int MINDEPTH; //. 80int MAXDEPTH; //. 81int LIMDEPTH; //. 82const char MAXCUTS=10; //.. - - (10- , 20) 83boolean TRACE=0; 84boolean checkmatesearch=0; // 85boolean solving=false; 86boolean choice=false; 87boolean sound=0; 88int cur_step=1; // , 1.... 89int limit=0; // , 0-6; 90//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 91const long limits[7]={10,30,60,180,600,1800,100000}; 92// 0 1 2 3 4 5 6 93step_type steps[MAXSTEPS]; // 94step_type cuts[MAXCUTS]; // - 95step_type lastbest; 96int lastscore; 97int minbeta,maxalpha; 98int startweight; 99int cur_level; // () 100int start_var; // , 1.... 101int cur_var; // , 1.... 102int cur_choice; // 103boolean check_on_table; // 104boolean isstatus; 105char WKJ=0,WKI=0,BKJ=0,BKI=0; // 106boolean endspiel=false; // 107char progress; // 0-100 108boolean only_action=false; // - 109unsigned long starttime,limittime,quitime=0; 110boolean drag; 111const uint8_t fig[6][32] PROGMEM={ 112{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 1130x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x0, 0x0}, // 114{0x0, 0x0, 0x3, 0x40, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x3F, 0xFC, 0x39, 0xFC, 0x33, 0xFC, 1150x7, 0xFC, 0xF, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x7, 0xE0, 0x1F, 0xF8, 0x0, 0x0}, // 116{0x1, 0x80, 0x3, 0xC0, 0x1, 0x80, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 1170x1F, 0xF8, 0x1F, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x31, 0x8C, 0x7F, 0xFE, 0x0, 0x0}, // 118{0x0, 0x0, 0x19, 0x98, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 1190xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0}, // 120{0x19, 0x98, 0xD9, 0x9B, 0xD9, 0x9B, 0xD9, 0x9B, 0x6D, 0xB6, 0x6D, 0xB6, 0x6D, 0xB6, 0x35, 0xAC, 1210x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0}, // 122{0x1, 0x80, 0x1, 0x80, 0x79, 0x9E, 0x7D, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1230x7F, 0xFE, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0} // 124}; 125const uint8_t fig_cont[6][32] PROGMEM={ 126{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1270x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8}, // 128{0x3, 0x40, 0x4, 0xA0, 0x8, 0x10, 0x12, 0x8, 0x20, 0x4, 0x40, 0x2, 0x46, 0x2, 0x4C, 0x2, 1290x38, 0x2, 0x10, 0x4, 0x10, 0x8, 0x8, 0x10, 0x4, 0x20, 0x18, 0x18, 0x20, 0x4, 0x3F, 0xFC}, // 130{0x2, 0x40, 0x4, 0x20, 0x6, 0x60, 0x10, 0x8, 0x21, 0x84, 0x21, 0x84, 0x21, 0x84, 0x27, 0xE4, 1310x21, 0x84, 0x21, 0x84, 0x11, 0x88, 0x8, 0x10, 0x34, 0x2C, 0x4E, 0x72, 0x80, 0x1, 0xFF, 0xFF}, // 132{0x39, 0x9C, 0x26, 0x64, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 1330x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC}, // 134{0x22, 0x24, 0x22, 0x24, 0x22, 0x24, 0x26, 0x64, 0x92, 0x49, 0x92, 0x49, 0x92, 0x49, 0x4A, 0x52, 1350x40, 0x2, 0x40, 0x2, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC}, // 136{0x2, 0x40, 0x7A, 0x5E, 0x86, 0x61, 0x82, 0x41, 0x2, 0x40, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 1370x80, 0x81, 0x41, 0x82, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC} // 138}; 139 140const uint8_t fig24[6][72] PROGMEM={ 141{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1E, 0x0, 0x0, 0x3F, 0x0, 0x80, 0x7F, 0x0, 0x80, 0x7F, 0x0, 1420x80, 0x7F, 0x0, 0x0, 0x3F, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x1C, 0x0, 0x80, 0xFF, 0x0, 0x80, 0xFF, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x1C, 0x0, 1430x0, 0x3E, 0x0, 0x0, 0x7F, 0x0, 0x80, 0xFF, 0x0, 0xC0, 0xFF, 0x1, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0x0, 0x0, 0x0 144}, //pawn 145{0x0, 0x0, 0x0, 0x0, 0x6E, 0x0, 0x0, 0xFF, 0x0, 0x80, 0xFF, 0x1, 0x80, 0xFF, 0x3, 0xC0, 0xFF, 0x7, 0xE0, 0xFF, 0xF, 0xE0, 0xF9, 0xF, 1460xF0, 0xF8, 0xF, 0xF0, 0xF8, 0xF, 0x60, 0xFC, 0xF, 0x0, 0xFE, 0xF, 0x0, 0xFF, 0xF, 0x0, 0xFF, 0x7, 0x0, 0xFF, 0x3, 0x0, 0xFF, 0x1, 1470x0, 0xFE, 0x0, 0x0, 0x7C, 0x0, 0x0, 0x38, 0x0, 0x0, 0x38, 0x0, 0x0, 0x7C, 0x0, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 148}, //knight 149{0x0, 0x0, 0x0, 0x0, 0x3C, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x3C, 0x0, 0x0, 0x18, 0x0, 0x80, 0xFF, 0x1, 0xC0, 0xFF, 0x3, 1500xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 1510xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0x80, 0xFF, 0x1, 0x0, 0xFF, 0x0, 0x8, 0x7E, 0x10, 0x1C, 0x3C, 0x38, 0xFE, 0xFF, 0x7F, 0x0, 0x0, 0x0 152}, //bishop 153{0x0, 0x0, 0x0, 0xF0, 0x3C, 0xF, 0xF0, 0x3C, 0xF, 0xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 1540xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 1550xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 156}, //rook 157{0x0, 0x0, 0x0, 0xEE, 0xBD, 0x77, 0xEE, 0xBD, 0x77, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 0xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 1580xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 1590x30, 0xFF, 0xC, 0xB0, 0xFF, 0xD, 0xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 160}, //queen 161{0x0, 0x0, 0x0, 0x0, 0x18, 0x0, 0xF0, 0x18, 0xF, 0xF8, 0x99, 0x1F, 0xFC, 0xDB, 0x3F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 1620xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x3F, 0xFC, 0xFF, 0x1F, 0xF8, 0xFF, 0x1F, 1630xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 164} //king 165}; 166 167const uint8_t fig24_cont[6][72] PROGMEM={ 168{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x22, 0x0, 0x0, 0x41, 0x0, 0x80, 0x80, 0x0, 0x80, 0x80, 0x0, 1690x80, 0x80, 0x0, 0x0, 0x41, 0x0, 0x0, 0x22, 0x0, 0xC0, 0xE3, 0x1, 0x40, 0x0, 0x1, 0x40, 0x0, 0x1, 0xC0, 0xE3, 0x1, 0x0, 0x22, 0x0, 1700x0, 0x41, 0x0, 0x80, 0x80, 0x0, 0x40, 0x0, 0x1, 0x20, 0x0, 0x2, 0x10, 0x0, 0x4, 0x10, 0x0, 0x4, 0x10, 0x0, 0x4, 0xF0, 0xFF, 0x7 171}, //pawn 172{0x0, 0x6E, 0x0, 0x0, 0x91, 0x0, 0x80, 0x0, 0x1, 0x40, 0x2, 0x2, 0x40, 0x0, 0x4, 0x20, 0x0, 0x8, 0x10, 0x0, 0x10, 0x10, 0x6, 0x10, 1730x8, 0x5, 0x10, 0x8, 0x5, 0x10, 0x90, 0x2, 0x10, 0x60, 0x1, 0x10, 0x80, 0x0, 0x10, 0x80, 0x0, 0x8, 0x80, 0x0, 0x4, 0x80, 0x0, 0x2, 1740x0, 0x1, 0x1, 0x0, 0x82, 0x0, 0x0, 0x44, 0x0, 0x0, 0x44, 0x0, 0xF8, 0x83, 0x1F, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 175}, //knight 176{0x0, 0x3C, 0x0, 0x0, 0x42, 0x0, 0x0, 0x81, 0x0, 0x0, 0x81, 0x0, 0x0, 0x42, 0x0, 0x80, 0xE7, 0x1, 0x40, 0x0, 0x2, 0x20, 0x18, 0x4, 1770x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x90, 0xFF, 0x9, 0x90, 0xFF, 0x9, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 1780x10, 0x18, 0x8, 0x20, 0x18, 0x4, 0x40, 0x18, 0x2, 0x88, 0x0, 0x11, 0x14, 0x81, 0x28, 0xE2, 0xC3, 0x47, 0x1, 0x0, 0x80, 0xFF, 0xFF, 0xFF 179}, //bishop 180{0xF8, 0x7E, 0x1F, 0x8, 0xC3, 0x10, 0x8, 0xC3, 0x10, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xE0, 0xFF, 0x7, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 1810x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 1820x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0xE0, 0xFF, 0x7, 0x10, 0x0, 0x8, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 183}, //rook 184{0xEF, 0xBD, 0xF7, 0x11, 0x42, 0x88, 0x11, 0x42, 0x88, 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 1850x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 1860xC8, 0x0, 0x13, 0x48, 0x0, 0x12, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 187}, //queen 188{0x0, 0x18, 0x0, 0xF0, 0x24, 0xF, 0x8, 0xA5, 0x10, 0x4, 0x66, 0x20, 0x2, 0x24, 0x40, 0x1, 0x24, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 1890x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x40, 0x2, 0x18, 0x20, 0x4, 0x18, 0x20, 1900xE8, 0xFF, 0x17, 0x10, 0x0, 0x8, 0x10, 0x0, 0x8, 0x20, 0x0, 0x4, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 191} //king 192}; 193 194 195const uint8_t issound[2][32] PROGMEM={ 196{0x0, 0x0, 0x0, 0x0, 0x0, 0x32, 0x0, 0x54, 0x0, 0x98, 0x1F, 0x10, 0x10, 0x30, 0x10, 0x50, 1970x10, 0x90, 0x11, 0x10, 0x1F, 0x10, 0x4, 0x90, 0x8, 0x50, 0x10, 0x30, 0x0, 0x0, 0x0, 0x0}, 198{0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x50, 0x0, 0x90, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 1990x10, 0x10, 0x10, 0x10, 0x1F, 0x10, 0x0, 0x90, 0x0, 0x50, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0} 200}; 201const uint8_t iswb[2][32] PROGMEM={ 202{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 2030x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x0, 0x0}, 204{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 2050xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0} 206}; 207const uint8_t iscm[2][32] PROGMEM={ 208{0x0, 0x0, 0x7, 0xE0, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0x0, 0x30, 0x0, 0x60, 2090x0, 0xC0, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0}, 210{0x0, 0x0, 0x3F, 0x0, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x1, 0x80, 0x3, 0x0, 2110x6, 0x24, 0xC, 0x24, 0xC, 0xFF, 0xC, 0x24, 0x0, 0xFF, 0xC, 0x24, 0xC, 0x24, 0x0, 0x0} 212}; 213const uint8_t islimit[7][32] PROGMEM={ 214{0x0, 0x0, 0x4, 0x70, 0xC, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 2150xE, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 216{0x0, 0x0, 0xC, 0x70, 0x12, 0x88, 0x2, 0x88, 0xC, 0x88, 0x2, 0x88, 0x2, 0x88, 0x12, 0x88, 2170xC, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 218{0x0, 0x0, 0x1, 0x80, 0x3, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 2190x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 220{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 2210x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 222{0x0, 0x0, 0xC, 0x78, 0x1C, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 2230x1E, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 224{0x0, 0x0, 0x1C, 0x78, 0x36, 0xCC, 0x6, 0xCC, 0x1C, 0xCC, 0x6, 0xCC, 0x36, 0xCC, 0x36, 0xCC, 2250x1C, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 226{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3C, 0x3C, 0x7E, 0x7E, 0x63, 0xC6, 0x61, 0x86, 2270x63, 0xC6, 0x66, 0x66, 0x3C, 0x3C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 228}; 229const uint8_t isdesc[2][32] PROGMEM={ 230{0x0, 0x0, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 2310xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E}, 232{0x0, 0x0, 0x0, 0x4, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x30, 2330x30, 0x70, 0x78, 0xE0, 0x3C, 0xC0, 0x1F, 0xC0, 0xF, 0x80, 0x7, 0x0, 0x2, 0x0, 0x0, 0x0} 234 235}; 236const uint8_t isback[32] PROGMEM={ 2370x0, 0x0, 0x0, 0x0, 0x20, 0xF0, 0x31, 0xF8, 0x3B, 0xFC, 0x3F, 0x8E, 0x3F, 0xE, 0x3F, 0xE, 2380x3F, 0x8E, 0x3F, 0xCE, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x70, 0x0, 0x0 239}; 240const uint8_t issave[32] PROGMEM={ 2410x1, 0x80, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 2420x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 243}; 244const uint8_t isload[32] PROGMEM={ 2450x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 2460x23, 0xC4, 0x27, 0xE4, 0x27, 0xE4, 0x23, 0xC4, 0x21, 0x84, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 247}; 248const uint8_t isempty[32] PROGMEM={ 2490xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 2500x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF 251}; 252const uint8_t isinit[32] PROGMEM={ 2530xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 2540x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 255}; 256const uint8_t isplay[32] PROGMEM={ 2570x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x1, 0x80, 0x23, 0xF8, 0x17, 0xC8, 0xB, 0xC8, 2580x3, 0xC0, 0x3, 0xC0, 0x2, 0x40, 0x4, 0x40, 0x4, 0x38, 0x4, 0x8, 0xC, 0x0, 0x0, 0x0 259}; 260const uint8_t isrotate[2][32] PROGMEM={ 261{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 2620x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0}, 263{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 2640x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0} 265}; 266 267const int pos[7][8][8] PROGMEM={ 268 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 269 {100,100,100,100,100,100,100,100}, //{ 50, 50, 50, 50, 50, 50, 50, 50}, 270 { 20, 30, 40, 50, 50, 40, 30, 20}, //{ 10, 10, 20, 30, 30, 20, 10, 10}, 271 { 5, 5, 10, 25, 25, 10, 5, 5}, 272 { 0, 0, 0, 20, 20, 0, 0, 0}, 273 { 5, -5,-10, 0, 0,-10, -5, 5}, 274 { 5, 10, 10,-20,-20, 10, 10, 5}, //{ 5, 10, 10,-20,-20, 10, 10, 5}, 275 { 0, 0, 0, 0, 0, 0, 0, 0}}, 276 277 {{-50,-40,-30,-30,-30,-30,-40,-50}, // 278 {-40,-20, 0, 0, 0, 0,-20,-40}, 279 {-30, 0, 10, 15, 15, 10, 0,-30}, 280 {-30, 5, 15, 20, 20, 15, 5,-30}, 281 {-30, 0, 15, 20, 20, 15, 0,-30}, 282 {-30, 5, 10, 15, 15, 10, 5,-30}, 283 {-40,-20, 0, 5, 5, 0,-20,-40}, 284 {-50,-40,-30,-30,-30,-30,-40,-50}}, 285 286 {{-20,-10,-10,-10,-10,-10,-10,-20}, // 287 {-10, 0, 0, 0, 0, 0, 0,-10}, 288 {-10, 0, 5, 10, 10, 5, 0,-10}, 289 {-10, 5, 5, 10, 10, 5, 5,-10}, 290 {-10, 0, 10, 10, 10, 10, 0,-10}, 291 {-10, 10, 10, 10, 10, 10, 10,-10}, 292 {-10, 5, 0, 0, 0, 0, 5,-10}, 293 {-20,-10,-10,-10,-10,-10,-10,-20}}, 294 295 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 296 { 5, 10, 10, 10, 10, 10, 10, 5}, 297 { -5, 0, 0, 0, 0, 0, 0, -5}, 298 { -5, 0, 0, 0, 0, 0, 0, -5}, 299 { -5, 0, 0, 0, 0, 0, 0, -5}, 300 { -5, 0, 0, 0, 0, 0, 0, -5}, 301 { -5, 0, 0, 0, 0, 0, 0, -5}, 302 { 0, 0, 0, 5, 5, 0, 0, 0}}, 303 304 {{-20,-10,-10, -5, -5,-10,-10,-20}, // 305 {-10, 0, 0, 0, 0, 0, 0,-10}, 306 {-10, 0, 5, 5, 5, 5, 0,-10}, 307 { -5, 0, 5, 5, 5, 5, 0, -5}, 308 { 0, 0, 5, 5, 5, 5, 0, -5}, 309 {-10, 5, 5, 5, 5, 5, 0,-10}, 310 {-10, 0, 5, 0, 0, 0, 0,-10}, 311 {-20,-10,-10, -5, -5,-10,-10,-20}}, 312 313 {{-30,-40,-40,-50,-50,-40,-40,-30}, // 314 {-30,-40,-40,-50,-50,-40,-40,-30}, 315 {-30,-40,-40,-50,-50,-40,-40,-30}, 316 {-30,-40,-40,-50,-50,-40,-40,-30}, 317 {-20,-30,-30,-40,-40,-30,-30,-20}, 318 {-10,-20,-20,-20,-20,-20,-20,-10}, 319 { 10, 10,-10,-10,-10,-10, 10, 10}, //{ 20, 20, 0, 0, 0, 0, 20, 20}, 320 { 10, 40, 30, 0, 0, 0, 50, 10}}, //{ 20, 30, 10, 0, 0, 10, 30, 20}}, 321 322 {{-50,-40,-30,-20,-20,-30,-40,-50}, // 323 {-30,-20,-10, 0, 0,-10,-20,-30}, 324 {-30,-10, 20, 30, 30, 20,-10,-30}, 325 {-30,-10, 30, 40, 40, 30,-10,-30}, 326 {-30,-10, 30, 40, 40, 30,-10,-30}, 327 {-30,-10, 20, 30, 30, 20,-10,-30}, 328 {-30,-30, 0, 0, 0, 0,-30,-30}, 329 {-50,-30,-30,-30,-30,-30,-30,-50}} 330}; 331 332//****** 333class Button { 334public: 335int bx,by,bw,bh,bshift; 336String bs; 337const uint8_t* bp; 338 Button(int x,int y,int w,int h,String s,const uint8_t* p, int shift); 339 void Show(int shift); 340 void Show(String s); 341 void Hide(); 342 boolean IsPressed(int xc, int yc); 343private: 344boolean active; 345 void ishow(uint16_t textcolor,uint16_t backcolor); 346}; 347Button::Button(int x,int y,int w,int h,String s,const uint8_t* p=NULL, int shift=0) { 348 bx=x; by=y; bw=w; bh=h; bs=s; bp=p; bshift=shift; 349}; 350void Button::Show(int shift=0) { 351 bshift=shift; 352 active=true; 353 ishow(WHITE,DARK); 354}; 355void Button::Show(String s) { 356 bs=s; 357 active=true; 358 ishow(WHITE,DARK); 359}; 360void Button::Hide() { 361 active=false; 362 ishow(GRAY,DARK); 363}; 364boolean Button::IsPressed(int xc, int yc) { 365 if (active&&abs(bx+bw/2-xc)<bw/2&&abs(by+bh/2-yc)<bh/2) { 366 ishow(WHITE,GRAY); 367 touchx=0; touchy=0; 368 return true; 369 } 370 return false; 371}; 372void Button::ishow(uint16_t textcolor,uint16_t backcolor) { 373 tft.fillRoundRect(bx+2,by+2,bw,bh,3,GRAY2); 374 tft.fillRoundRect(bx,by,bw,bh,3,backcolor); 375 tft.setTextColor(textcolor); 376 tft.setTextSize(1); 377 tft.setCursor(bx+(bw-bs.length()*6)/2,by+(bh-8)/2); 378 if (bp!=NULL) { 379 tft.drawBitmap(1+bx+(bw-16)/2, by+(bh-16)/2, bp+bshift*32, 16, 16,textcolor); 380 } 381 else tft.print(bs); 382} 383Button BAction(320, 285, 70, 30, "START"); 384Button BBW(402, 285, 30, 30, "",&iswb[0][0]); 385Button BBack(444, 285, 30, 30, "",&isback[0]); 386Button BInit(444, 285, 30, 30, "",&isinit[0]); 387Button BLimit(320, 245, 26, 30, "",&islimit[0][0]); 388Button BEmpty(320, 245, 26, 30, "",&isempty[0]); 389Button BRotate(352, 245, 26, 30, "",&isrotate[0][0]); 390Button BPlay(352, 245, 26, 30, "",&isplay[0]); 391Button BCM(384, 245, 26, 30, "",&iscm[0][0]); 392Button BSave(384, 245, 26, 30, "",&issave[0]); 393Button BDesc(416, 245, 26, 30, "",&isdesc[0][0]); 394Button BSound(448, 245, 26, 30, "",&issound[0][0]); 395Button BLoad(448, 245, 26, 30, "",&isload[0]); 396//********************************** 397void drawBitmap(int16_t x, int16_t y, 398 const uint8_t *bitmap, int16_t w, int16_t h, 399 uint16_t color) { 400 int16_t i, j, byteWidth = (w + 7) / 8; 401 for(j=0; j<h; j++) { 402 for(i=0; i<w; i++ ) { 403 if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (1 << (i % 8))) { 404 tft.drawPixel(x+i, y+j, color); 405 } 406 } 407 } 408} 409//**************************** 410void deletebuttons() { 411 tft.fillRect(320,245,160,75,BLACK); 412} 413 414//**************************** 415void clearstatus() { 416 tft.fillRect(320,225,160,20,BLACK); 417 tft.drawFastHLine(10,312,300,BLACK); 418 tft.drawFastHLine(10,315,300,BLACK); 419} 420//**************************** 421void setup() { 422 Serial.begin(57600); 423 Serial.println(F("Start")); 424 pinMode(13, OUTPUT); 425 tft.reset(); 426 tft.begin(tft.readID()); 427 tft.setRotation(1); 428 definecolors(); 429 beep(100); 430 for (int i=0;i<MAXSTEPS;i++) { 431 steps[i].x1=0; steps[i].y1=0; 432 steps[i].x2=0; steps[i].y2=0; 433 steps[i].fig1=0; steps[i].fig2=0; 434 steps[i].check=0; 435 steps[i].type=0; 436 steps[i].weight=0; 437 } 438 initboard(); 439} 440//**************************** 441void initboard() { 442 for (int i=0;i<8;i++) 443 for (int j=0;j<8;j++) poledisp[j][i]=-100; // 444 tft.fillScreen(BLACK); 445 tft.setTextColor(GRAY); 446 tft.setTextSize(1); 447 for (int j=1;j<9;j++) { 448 tft.setCursor(2,j*37-20); 449 if (rotate) tft.print(j); else tft.print(9-j); 450 } 451 for (byte i=1;i<9;i++) { 452 tft.setCursor(i*37-8,300); 453 if (rotate) tft.print(char(96+9-i)); else tft.print(char(96+i)); 454 } 455 initscreen(); 456} 457//**************************** 458void initscreen() { 459 show_board(); 460 show_steps(); 461 BAction.Show("START"); 462 BBW.Show(cur_step%2); 463 if (cur_step>1) BBack.Show(); else BBack.Hide(); 464 BSave.Hide(); 465 BLoad.Hide(); 466 BInit.Hide(); 467 BBack.Show(); 468 BLimit.Show(); 469 BPlay.Hide(); 470 BRotate.Show(rotate); 471 BEmpty.Hide(); 472 BLimit.Show(limit); 473 BCM.Show(checkmatesearch); 474 BDesc.Show(choice); 475 BSound.Show(sound); 476} 477//**************************** 478void loop() { 479 gui(); 480 if (solving) { 481 int len=70; 482 if (!sound) len=2; 483 for (int i=1;i<4;i++) { tone(44,400+300*i,len); delay(70); } 484 lastscore=solve_step(); 485 float tim=float(millis()-starttime)/1000; 486 for (int i=3;i>=1;i--) { tone(44,400+300*i,len); delay(70); } 487 if (!checkmatesearch&&lastscore>-9000&&lastscore!=8999) { 488 movestep(cur_step); 489 cur_step++; steps[cur_step].fig1=0; 490 } 491 initscreen(); 492 animate_step(cur_step-1,false); 493 clearstatus(); 494 if (lastscore>9000) { 495 tft.setTextSize(1); 496 tft.setTextColor(GREEN); 497 tft.setCursor(322,230); 498 if (lastscore<9999) { 499 tft.print("# in "); 500 tft.print((9999-lastscore)/2+1); tft.print(F(" st")); 501 } else tft.print("Checkmate"); 502 } else if (lastscore<-9000) { 503 tft.setTextSize(1); 504 tft.setTextColor(RED); 505 tft.setCursor(322,230); 506 tft.print("GIVE UP!"); 507 show_steps(); 508 } else if (lastscore==8999) { //Draw 509 tft.setTextSize(1); 510 tft.setTextColor(YELLOW); 511 tft.setCursor(322,230); 512 tft.print("Draw"); 513 } 514 tft.setTextSize(1); 515 tft.setTextColor(GRAY); 516 tft.setCursor(322,230); 517 tft.print(tim,0); 518 tft.print("s "); 519 if (abs(lastscore)<5000) { 520 tft.print("="); 521 tft.print(lastscore); 522 } 523 tft.setCursor(390,230); 524 tft.print(count); 525 tft.print(" pos"); 526 Serial.print(F("Positions estimated=")); Serial.println(count); 527 Serial.print(F("Time=")); Serial.print(tim,1); Serial.println("s"); 528 } 529 delay(10); 530 531 532} 533 534//********************************** 535void load_choice() { 536 for (int i=0;i<7;i++) 537 if (cur_step%2==1) polechoice[i]=i; else polechoice[i]=-i; 538} 539//********************************** 540void show_choice() { 541uint16_t color,color_cont; 542 for (int i=0;i<7;i++) { 543 color=BLACKF; 544 if (i%2==0) color=WHITEF; 545 tft.fillRect(416,i*25+35,25,25,color); 546 color=DARK; 547 if (i==cur_choice) color=WHITE; 548 tft.drawRect(416,i*25+35,25,25,color); 549 color=BLACK; color_cont=GRAY; 550 if (polechoice[i]>0) { color=WHITE; color_cont=BLACK; } 551 if (polechoice[i]!=0) { 552 tft.drawBitmap(420, i*25+39,&fig[abs(polechoice[i])-1][0], 16, 16,color); 553 tft.drawBitmap(420, i*25+39,&fig_cont[abs(polechoice[i])-1][0], 16, 16,color_cont); 554 } 555 } 556} 557//********************************** 558void animate_step(int nstep, boolean hide) { 559 if (!hide&&nstep-1>0&&steps[nstep-1].fig1!=0) animate_step(nstep-1,true); 560 if (nstep-1<1||steps[nstep].fig1==0) return; 561 int j=steps[nstep].x1; 562 int dj=steps[nstep].x2-steps[nstep].x1; 563 int i=steps[nstep].y1; 564 int di=steps[nstep].y2-steps[nstep].y1; 565 di=di/abs(di); dj=dj/abs(dj); 566 if (hide) show_fig(i,j); 567 while (j!=steps[nstep].x2||i!=steps[nstep].y2) { 568 show_fig(i,j); 569 if (!hide) 570 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 571 else tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 572 int mj=0; 573 if (j!=steps[nstep].x2) { j+=dj; mj=1; } 574 if (abs(steps[nstep].fig1)==fn&&mj==1) continue; 575 if (i!=steps[nstep].y2) i+=di; 576 } 577 show_fig(i,j); 578 if (!hide) 579 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 580 else tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 581} 582//********************************** 583boolean load_usb() { 584char s='x',i=0,j=0; boolean load=false; 585 Serial.println(F("Wait for FEN position")); 586 for (int i=0;i<8;i++) 587 for (int j=0;j<8;j++) { 588 pole0[j][i]=pole[j][i]; 589 pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 590 } 591 while (s!=' ') { 592 s=Serial.read(); 593 if (i>7) { i=0; j++; } 594 if (!getpole(j,i)) break; 595 switch (s) { 596 case '/': i=0; break; 597 case 'p': pole[j][i]=-fp; i++; break; 598 case 'P': pole[j][i]=fp; i++; break; 599 case 'n': pole[j][i]=-fn; i++; break; 600 case 'N': pole[j][i]=fn; i++; break; 601 case 'b': pole[j][i]=-fb; i++; break; 602 case 'B': pole[j][i]=fb; i++; break; 603 case 'r': pole[j][i]=-fr; i++; break; 604 case 'R': pole[j][i]=fr; i++; break; 605 case 'q': pole[j][i]=-fq; i++; break; 606 case 'Q': pole[j][i]=fq; i++; break; 607 case 'k': pole[j][i]=-fk; i++; break; 608 case 'K': pole[j][i]=fk; i++; break; 609 case '1': i++; break; 610 case '2': i+=2; break; 611 case '3': i+=3; break; 612 case '4': i+=4; break; 613 case '5': i+=5; break; 614 case '6': i+=6; break; 615 case '7': i+=7; break; 616 case '8': i=0; j++; break; 617 case ' ': break; 618 } 619 delay(20); 620 if (i+j>0&&Serial.available()==0) break; 621 } 622 s=0; 623 if (Serial.available()>0) s=Serial.read(); 624 while (Serial.available()>0) Serial.read(); 625 if (s=='w'||s==0) { cur_step=1; load=true; } 626 else if (s=='b') { cur_step=2; load=true; } 627 else load=false; 628 if (load) { 629 steps[1].fig1=0; steps[2].fig1=0; 630 Serial.println(F("Position loaded")); 631 } else { 632 for (int i=0;i<8;i++) 633 for (int j=0;j<8;j++) 634 pole[j][i]=pole0[j][i]; 635 } 636 return load; 637} 638//********************************** 639void play() { 640 BAction.Show("STOP"); 641 BSave.Hide(); 642 BBW.Hide(); 643 BLoad.Hide(); 644 BInit.Hide(); 645 BPlay.Hide(); 646 BEmpty.Hide(); 647 BDesc.Hide(); 648 clearstatus(); 649 for (int i=cur_step-1;i>0;i--) { 650 if (steps[i].fig1!=0) backstep(i); 651 } 652 show_board(); 653 show_steps(); 654 boolean st=false; 655 unsigned long tim=millis(); 656 int i=1; 657 while (i<cur_step) { 658 if (steps[i].fig1!=0) movestep(i); 659 digitalWrite(13, HIGH); 660 TSPoint p = ts.getPoint(); 661 digitalWrite(13, LOW); 662 pinMode(XM, OUTPUT); 663 pinMode(YP, OUTPUT); 664 int x=map(p.y,980,110,0,479); 665 int y=map(p.x,145,902,0,319); 666 if (p.z>MINPRESSURE&&p.z<MAXPRESSURE) 667 if (BAction.IsPressed(x,y)) { st=true; BAction.Show(); } 668 if (millis()-tim>2000||st) { 669 tim=millis(); 670 i++; 671 show_board(); 672 beep(20); 673 } 674 } 675 choice=0; 676 beep(200); 677 kingpositions(); 678 clearstatus(); 679 initscreen(); 680} 681//********************************** 682void gui() { 683 digitalWrite(13, HIGH); 684 TSPoint p = ts.getPoint(); 685 digitalWrite(13, LOW); 686 // if sharing pins, you'll need to fix the directions of the touchscreen pins 687 pinMode(XM, OUTPUT); 688 pinMode(YP, OUTPUT); 689 int x=map(p.y,980,110,0,479); 690 int y=map(p.x,145,902,0,319); 691 692 if (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&p.z>MINPRESSURE&&p.z<MAXPRESSURE) { 693 // tft.drawPixel(x,y,CYAN); 694 // Serial.println(x); 695 // Serial.println(y); 696 touchx=x; 697 touchy=y; 698 if (solving) { 699 if (BAction.IsPressed(x,y)) { // 700 beep(100); 701 solving=false; 702 BAction.Show("stopping"); 703 BAction.Hide(); 704 return; 705 } 706 } else { //not solve 707 if (BAction.IsPressed(x,y)) { // 708 if (!choice) { 709 delay(100); 710 solving=true; 711 BAction.Show(); 712 animate_step(cur_step-1,true); 713 return; 714 } else { // USB 715 delay(100); 716 BAction.Hide(); 717 BSave.Hide(); 718 BBW.Hide(); 719 BLoad.Hide(); 720 BInit.Hide(); 721 BPlay.Hide(); 722 BEmpty.Hide(); 723 BDesc.Hide(); 724 show_steps(); 725 choice=0; 726 if (!load_usb()) return; 727 beep(200); 728 kingpositions(); 729 clearstatus(); 730 initscreen(); 731 } 732 } else if (BBW.IsPressed(x,y)) { //- 733 beep(100); 734 if (cur_step==1) cur_step=2; 735 else if (cur_step==2) cur_step=1; 736 else if (cur_step%2==0) cur_step--; else cur_step++; 737 BBW.Show(cur_step%2); 738 if (choice) { load_choice(); show_choice(); } 739 return; 740 } else if (cur_step>1&&BBack.IsPressed(x,y)) { // 741 beep(100); 742 cur_step--; 743 animate_step(cur_step,true); 744 lastscore=0; 745 backstep(cur_step); 746 steps[cur_step].fig1=0; 747 show_board(); 748 show_steps(); 749 if (cur_step>1) BBack.Show(); else BBack.Hide(); 750 BBW.Show(cur_step%2); 751 clearstatus(); 752 return; 753 } else if (BLimit.IsPressed(x,y)) { // 754 beep(100); 755 limit++; if (limit>6) limit=0; 756 BLimit.Show(limit); 757 return; 758 } else if (BCM.IsPressed(x,y)) { // 759 beep(100); 760 checkmatesearch=!checkmatesearch; 761 BCM.Show(checkmatesearch); 762 if (!checkmatesearch) steps[cur_step].fig1=0; 763 lastscore=0; 764 show_steps(); 765 return; 766 } else if (BRotate.IsPressed(x,y)) { // 767 beep(100); 768 rotate=!rotate; 769 BRotate.Show(rotate); 770 initboard(); 771 return; 772 } else if (BSound.IsPressed(x,y)) { // 773 if (sound==1) { beep(100); sound=0; } else { sound=1; beep(200); } 774 BSound.Show(sound); 775 return; 776 } else if (BDesc.IsPressed(x,y)) { // 777 beep(100); 778 if (!choice) { 779 choice=1; 780 cur_choice=1; 781 load_choice(); 782 show_choice(); 783 BAction.Show("USB(FEN)"); 784 BBack.Hide(); 785 BInit.Show(); 786 BSave.Show(); 787 BLimit.Hide(); 788 BEmpty.Show(); 789 BRotate.Hide(); 790 BPlay.Show(); 791 BCM.Hide(); 792 BSound.Hide(); 793 BSave.Show(); 794 BLoad.Show(); 795 } else { 796 choice=0; 797 lastscore=0; 798 clearstatus(); 799 kingpositions(); 800 initscreen(); 801 } 802 BDesc.Show(choice); 803 return; 804 } else if (BInit.IsPressed(x,y)) { // 805 beep(200); 806 cur_step=1; 807 steps[1].fig1=0; 808 for (int i=0;i<8;i++) 809 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polestart[j][i]); 810 choice=0; 811 kingpositions(); 812 clearstatus(); 813 initscreen(); 814 } else if (BEmpty.IsPressed(x,y)) { // 815 beep(200); 816 cur_step=1; 817 clearstatus(); 818 steps[1].fig1=0; steps[2].fig1=0; 819 for (int i=0;i<8;i++) 820 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 821 BEmpty.Show(); 822 show_board(); 823 } else if (BSave.IsPressed(x,y)) { 824 beep(100); 825 for (int i=0;i<8;i++) 826 for (int j=0;j<8;j++) EEPROM.write(i*8+j,pole[j][i]); 827 EEPROM.put(64,cur_step); 828 EEPROM.put(66,limit); 829 for (int i=1;i<cur_step;i++) { 830 EEPROM.put(66+i*10,steps[i]); 831 } 832 BSave.Show(); 833 } else if (BLoad.IsPressed(x,y)) { 834 beep(200); 835 for (int i=0;i<8;i++) 836 for (int j=0;j<8;j++) pole[j][i]=EEPROM.read(i*8+j); 837 EEPROM.get(64,cur_step); 838 if (cur_step<0||cur_step>400) cur_step=1; 839 EEPROM.get(66,limit); 840 if (limit<0||limit>6) limit=2; 841 for (int i=1;i<cur_step;i++) { 842 EEPROM.get(66+i*10,steps[i]); 843 } 844 steps[0].fig1=0; 845 steps[cur_step].fig1=0; 846 choice=0; 847 kingpositions(); 848 clearstatus(); 849 initscreen(); 850 } else if (BPlay.IsPressed(x,y)) { // 851 beep(100); 852 BPlay.Show(); 853 play(); 854 return; 855 } 856 if (!choice&&ntouch==0) { 857 touchx0=touchx; 858 touchy0=touchy; 859 char x1=touchy0/37; 860 char y1=(touchx0-10)/37; 861 if (rotate) { x1=7-x1; y1=7-y1; } 862 if (getpole(y1,x1)&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 863 char p=pole[x1][y1]; 864 drag=true; 865 pole[x1][y1]=0; 866 show_fig(y1,x1); 867 pole[x1][y1]=p; 868 } else drag=false; 869 } 870 ntouch++; 871 if (drag&&ntouch>1) { 872 uint16_t buf[256]; 873 int x1=touchy0/37; 874 int y1=(touchx0-10)/37; 875 if (rotate) { x1=7-x1; y1=7-y1; } 876 if (abs(pole[x1][y1])>0&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 877 for (int i=0;i<16;i++) 878 for (int j=0;j<16;j++) buf[i*16+j]=tft.readPixel(touchx-8+i,touchy-8+j); 879 buf[0]=tft.readPixel(touchx-8,touchy-8); 880 uint16_t color,color_cont; 881 color=BLACK; color_cont=GRAY; 882 if (pole[x1][y1]>0) { color=WHITE; color_cont=BLACK; } 883 tft.drawBitmap(touchx-8, touchy-8,&fig[abs(pole[x1][y1])-1][0], 16, 16,color); 884 tft.drawBitmap(touchx-8, touchy-8,&fig_cont[abs(pole[x1][y1])-1][0], 16, 16,color_cont); 885 delay(30); 886 for (int i=0;i<16;i++) 887 for (int j=0;j<16;j++) tft.drawPixel(touchx-8+i,touchy-8+j,buf[i*16+j]); 888 } 889 } else if (choice) { 890 int x1=touchy/37; 891 int y1=(touchx-10)/37; 892 if (rotate) { x1=7-x1; y1=7-y1; } 893 if (touchy+touchx!=0&&choice&&getpole(y1,x1)) { 894 pole[x1][y1]=polechoice[cur_choice]; 895 show_board(); 896 } 897 if (touchx>=416&&touchx<=441&&touchy>=35&&touchy<=210) { 898 cur_choice=(touchy-35)/25; 899 show_choice(); 900 } 901 } 902 lasttouch=millis(); 903 } //not solve 904 if (!drag) { beep(10); ntouch=0; animate_step(cur_step-1,true); } 905 } else { //touching 906 if (!solving&&!choice&&drag!=0&&ntouch>0&&p.z<MINPRESSURE&&millis()-lasttouch>300) { //drag 907 char x1=touchy0/37; 908 char y1=(touchx0-10)/37; 909 char x2=touchy/37; 910 char y2=(touchx-10)/37; 911 if (rotate) { x1=7-x1; y1=7-y1; x2=7-x2; y2=7-y2; } 912 if (getpole(y1,x1)) show_fig(y1,x1); 913 if (getpole(y1,x1)&&getpole(y2,x2)) { 914 start_var=cur_step+1; 915 only_action=false; 916 load_variants(cur_step); 917 for (int i=cur_step+1;i<cur_step+cur_var+1;i++) 918 if (steps[i].x1==x1&&steps[i].y1==y1&&steps[i].x2==x2&&steps[i].y2==y2) { 919 steps[cur_step]=steps[i]; 920 movestep(cur_step); cur_step++; steps[cur_step].fig1=0; 921 int len=100; 922 if (!sound) len=2; 923 for (int i=2;i>=1;i--) { tone(44,400+300*i,len); delay(100); } 924 initscreen(); 925 animate_step(cur_step-1,false); 926 break; 927 } 928 } else { 929 beep(10); 930 } 931 ntouch=0; 932 drag=false; 933 } 934 } 935 if (millis()-quitime>1000) { 936 if (isstatus) show_status(); 937 quitime=millis(); 938 } 939 940 941} 942//********************************** 943void show_status() { 944 int tim=(millis()-starttime)/1000; 945 int cur=300000*tim/(limittime-starttime); 946 if (cur>300) { cur=300; solving=false; } 947 tft.drawFastHLine(10,312,cur,GRAY); 948 tft.drawFastHLine(10,315,progress*2,GRAY); 949 int m=tim/60; 950 int s=tim%60; 951 tft.setTextWrap(1); 952 tft.setTextColor(GRAY); 953 tft.setTextSize(1); 954 tft.fillRect(320,225,50,20,BLACK); 955 tft.setCursor(322,230); 956 if (m>0) { tft.print(m); tft.print(":"); } 957 if (s>0) { 958 if (s<10&&m>0) tft.print("0"); 959 tft.print(s); 960 } else if (m>0) tft.print("00"); else tft.print("0"); 961 tft.setCursor(350,230); 962 tft.print(cur_level); 963 964 tft.setCursor(370,230); 965 if (cur_step%2==1) tft.setTextColor(WHITE); else tft.setTextColor(GRAY); 966 if (lastbest.fig1!=steps[0].fig1||lastbest.x1!=steps[0].x1||lastbest.y1!=steps[0].y1|| 967 lastbest.x2!=steps[0].x2||lastbest.y2!=steps[0].y2) { 968 beep(10); 969 lastbest=steps[0]; 970 tft.fillRect(370,230,50,10,BLACK); 971 tft.setCursor(370,230); 972 tft.print(str_step(0)); 973 blinkstep=0; 974 } 975 if (steps[0].fig1!=0&&blinkstep==0&&tim>5) { 976 char poleb[8][8]; 977 for (int i=0;i<8;i++) 978 for (int j=0;j<8;j++) { poleb[j][i]=pole[j][i]; pole[j][i]=pole0[j][i]; } 979 movestep(0); 980 show_board(); 981 delay(100); 982 backstep(0); 983 show_board(); 984 for (int i=0;i<8;i++) 985 for (int j=0;j<8;j++) pole[j][i]=poleb[j][i]; 986 kingpositions(); 987 } 988 blinkstep++; 989 if (blinkstep>2) blinkstep=0; 990} 991//**************************** 992void beep(int leng) { 993 analogWrite(PinBuzz, 20); 994 delay(2); 995 if (!sound) analogWrite(PinBuzz, 0); 996 delay(leng); 997 analogWrite(PinBuzz, 0); 998} 999//**************************** 1000void definecolors() { 1001 BLACK =0x0000; 1002 BLUE =0x07FF; //0x001F; 1003 RED =0xF800; 1004 GREEN =0x07E0; 1005 CYAN =0x07FF; 1006 MAGENTA=0xF81F; 1007 YELLOW =0xFFE0; 1008 WHITE =0xFFFF; 1009 GRAY =0x7BEF; 1010 DARK =getColor(32,32,32); 1011 GRAY2 =getColor(16,16,16); 1012 BLACKF =getColor(94,58,0); 1013 WHITEF =getColor(180,114,0); 1014} 1015//**************************** 1016uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) 1017{ 1018 red >>= 3; 1019 green >>= 2; 1020 blue >>= 3; 1021 return (red << 11) | (green << 5) | blue; 1022} 1023//**************************** 1024void show_fig(int i,int j) { 1025uint16_t color,color_cont; 1026 color=BLACKF; 1027 if ((i+j+2)%2==0) color=WHITEF; 1028 int jj=j, ii=i; 1029 if (rotate) { jj=7-j; ii=7-i; } 1030 tft.fillRect(ii*37+12,jj*37,38,38,color); 1031 tft.drawRect(ii*37+12,jj*37,38,38,WHITE); 1032 color=BLACK; color_cont=GRAY; 1033 if (pole[j][i]>0) { color=WHITE; color_cont=BLACK; } 1034 if (pole[j][i]!=0) { 1035 drawBitmap(ii*37+19, jj*37+7,&fig24[abs(pole[j][i])-1][0], 24, 24,color); 1036 drawBitmap(ii*37+19, jj*37+7,&fig24_cont[abs(pole[j][i])-1][0], 24, 24,color_cont); 1037 //drawBitmap(ii*37+24, jj*37+12,&fig[abs(pole[j][i])-1][0], 16, 16,color); 1038 //drawBitmap(ii*37+24, jj*37+12,&fig_cont[abs(pole[j][i])-1][0], 16, 16,color_cont); 1039 } 1040} 1041//**************************** 1042void show_board() { 1043 for (int i=0;i<8;i++) 1044 for (int j=0;j<8;j++) { 1045 if (poledisp[j][i]!=pole[j][i]) show_fig(i,j); 1046 poledisp[j][i]=pole[j][i]; 1047 } 1048} 1049//**************************** 1050String str_step(int i) { 1051 String s=""; 1052 if (steps[i].fig1==0) return s; 1053 if (steps[i].type==2) s="0-0"; 1054 else if (steps[i].type==3) s="0-0-0"; 1055 else { 1056 if (abs(steps[i].fig1)>1) s=fig_symb[abs(steps[i].fig1)]; 1057 if (abs(steps[i].fig1<5)) { 1058 s=s+char(97+steps[i].y1); 1059 s=s+String(8-steps[i].x1); 1060 if (steps[i].fig2==0) s=s+"-"; 1061 } 1062 if (steps[i].fig2!=0) { 1063 s=s+"x"; 1064 if (abs(steps[i].fig2)>1) s=s+fig_symb[abs(steps[i].fig2)]; 1065 } 1066 s=s+char(97+steps[i].y2); 1067 s=s+String(8-steps[i].x2); 1068 } 1069 if (steps[i].type>3) s=s+fig_symb[steps[i].type-2]; 1070 if (steps[i].check==1) s=s+"+"; else 1071 if (steps[i].check==2) s=s+"#"; 1072 return s; 1073} 1074//**************************** 1075void show_steps() { 1076 tft.fillRect(320,0,160,222,getColor(8,8,8)); 1077 tft.fillRect(320,0,160,16,DARK); 1078 tft.setTextSize(1); 1079 tft.setTextColor(WHITE,DARK); 1080 tft.setCursor(350,4); 1081 tft.print("ArduinoMega Chess"); 1082 tft.setTextSize(1); 1083 int i=1; int y=20; 1084 tft.setTextColor(GRAY); 1085 int cur=(cur_step+1)/2; // 1086 int lim_step=cur_step; 1087 if (checkmatesearch&&lastscore>9000) { 1088 tft.setTextColor(GREEN); 1089 lim_step=cur_step+10000-lastscore; 1090 cur=lim_step/2; 1091 } 1092 if (cur>20) i=cur-20; 1093 while (i<=cur&&y<215) { // 1094 tft.setCursor(324,y); 1095 tft.print(i); 1096 tft.print(". "); 1097 if (steps[i*2-1].fig1!=0) tft.print(str_step(i*2-1)); 1098 if (steps[i*2].fig1==0||i*2>lim_step) break; 1099 tft.print(", "); 1100 tft.print(str_step(i*2)); 1101 i++; y+=10; 1102 } 1103 1104} 1105//**************************** 1106boolean getdiagrowcheckw(char dj,char di) { // 1107char d,j1,i1; 1108 j1=WKJ; i1=WKI; 1109 for (d=1;d<8;d++) { 1110 j1+=dj; i1+=di; 1111 if (getpole(j1,i1)) { 1112 if (pole[j1][i1]==-fq||pole[j1][i1]==-fb) return true; 1113 if (pole[j1][i1]!=0) break; 1114 } else break; 1115 } 1116 return false; 1117} 1118//**************************** 1119boolean getdiagrowcheckb(char dj,char di) { // 1120char d,j1,i1; 1121 j1=BKJ; i1=BKI; 1122 for (d=1;d<8;d++) { 1123 j1+=dj; i1+=di; 1124 if (getpole(j1,i1)) { 1125 if (pole[j1][i1]==fq||pole[j1][i1]==fb) return true; 1126 if (pole[j1][i1]!=0) break; 1127 } else break; 1128 } 1129 return false; 1130} 1131//**************************** 1132boolean getstreightrowcheckw(char dj,char di) { // - 1133char d,j1,i1; 1134 j1=WKJ; i1=WKI; 1135 for (d=1;d<8;d++) { 1136 j1+=dj; i1+=di; 1137 if (getpole(j1,i1)) { 1138 if (pole[j1][i1]==-fq||pole[j1][i1]==-fr) return true; 1139 if (pole[j1][i1]!=0) break; 1140 } else break; 1141 } 1142 return false; 1143} 1144//**************************** 1145boolean getstreightrowcheckb(char dj,char di) { // - 1146char d,j1,i1; 1147 j1=BKJ; i1=BKI; 1148 for (d=1;d<8;d++) { 1149 j1+=dj; i1+=di; 1150 if (getpole(j1,i1)) { 1151 if (pole[j1][i1]==fq||pole[j1][i1]==fr) return true; 1152 if (pole[j1][i1]!=0) break; 1153 } else break; 1154 } 1155 return false; 1156} 1157//**************************** 1158boolean get_check(char king) { // 1159 if (king==fk) { // 1160 if (getdiagrowcheckw(-1, 1)) return true; 1161 if (getdiagrowcheckw(-1,-1)) return true; 1162 if (getdiagrowcheckw( 1,-1)) return true; 1163 if (getdiagrowcheckw( 1, 1)) return true; 1164 if (getstreightrowcheckw(-1, 0)) return true; 1165 if (getstreightrowcheckw( 0, 1)) return true; 1166 if (getstreightrowcheckw( 0,-1)) return true; 1167 if (getstreightrowcheckw( 1, 0)) return true; 1168 if (getpole(WKJ-2,WKI-1)&&pole[WKJ-2][WKI-1]==-fn) return true; 1169 if (getpole(WKJ-2,WKI+1)&&pole[WKJ-2][WKI+1]==-fn) return true; 1170 if (getpole(WKJ-1,WKI-2)&&pole[WKJ-1][WKI-2]==-fn) return true; 1171 if (getpole(WKJ-1,WKI+2)&&pole[WKJ-1][WKI+2]==-fn) return true; 1172 if (getpole(WKJ+1,WKI-2)&&pole[WKJ+1][WKI-2]==-fn) return true; 1173 if (getpole(WKJ+1,WKI+2)&&pole[WKJ+1][WKI+2]==-fn) return true; 1174 if (getpole(WKJ+2,WKI-1)&&pole[WKJ+2][WKI-1]==-fn) return true; 1175 if (getpole(WKJ+2,WKI+1)&&pole[WKJ+2][WKI+1]==-fn) return true; 1176 if (getpole(WKJ-1,WKI-1)&&pole[WKJ-1][WKI-1]==-fp) return true; 1177 if (getpole(WKJ-1,WKI+1)&&pole[WKJ-1][WKI+1]==-fp) return true; 1178 } else { // 1179 if (getdiagrowcheckb( 1,-1)) return true; 1180 if (getdiagrowcheckb( 1, 1)) return true; 1181 if (getdiagrowcheckb(-1, 1)) return true; 1182 if (getdiagrowcheckb(-1,-1)) return true; 1183 if (getstreightrowcheckb( 1, 0)) return true; 1184 if (getstreightrowcheckb( 0, 1)) return true; 1185 if (getstreightrowcheckb( 0,-1)) return true; 1186 if (getstreightrowcheckb(-1, 0)) return true; 1187 if (getpole(BKJ+2,BKI-1)&&pole[BKJ+2][BKI-1]==fn) return true; 1188 if (getpole(BKJ+2,BKI+1)&&pole[BKJ+2][BKI+1]==fn) return true; 1189 if (getpole(BKJ+1,BKI-2)&&pole[BKJ+1][BKI-2]==fn) return true; 1190 if (getpole(BKJ+1,BKI+2)&&pole[BKJ+1][BKI+2]==fn) return true; 1191 if (getpole(BKJ-1,BKI-2)&&pole[BKJ-1][BKI-2]==fn) return true; 1192 if (getpole(BKJ-1,BKI+2)&&pole[BKJ-1][BKI+2]==fn) return true; 1193 if (getpole(BKJ-2,BKI-1)&&pole[BKJ-2][BKI-1]==fn) return true; 1194 if (getpole(BKJ-2,BKI+1)&&pole[BKJ-2][BKI+1]==fn) return true; 1195 if (getpole(BKJ+1,BKI-1)&&pole[BKJ+1][BKI-1]==fp) return true; 1196 if (getpole(BKJ+1,BKI+1)&&pole[BKJ+1][BKI+1]==fp) return true; 1197 } 1198 if (abs(BKJ-WKJ)<=1&&abs(BKI-WKI)<=1) return true; // 1199 return false; 1200} 1201//**************************** 1202boolean getpole(char j,char i) { // 1203 if (j>=0&&j<8&&i>=0&&i<8) return true; 1204 return false; 1205} 1206//**************************** 1207void addstep(char j1,char i1,char j2,char i2,char type) { 1208 int st=start_var+cur_var; 1209 steps[st].x1=j1; 1210 steps[st].x2=j2; 1211 steps[st].y1=i1; 1212 steps[st].y2=i2; 1213 steps[st].fig1=pole[j1][i1]; 1214 steps[st].fig2=pole[j2][i2]; 1215 if (type==1) { // 1216 steps[st].fig2=-steps[st].fig1; 1217 } 1218 steps[st].type=type; 1219 char ko=-fk; // 1220 if (steps[st].fig1>0) ko=fk; 1221 movestep(st); 1222 if (get_check(ko)) { backstep(st); return; } // - 1223 boolean che=get_check(-ko); // 1224 backstep(st); 1225 steps[st].weight=abs(steps[st].fig2)-abs(steps[st].fig1); 1226 if (type>3) steps[st].weight+=fig_weight[type-2]; 1227 if (endspiel&&steps[st].fig1==ko) steps[st].weight+=10; // - 1228 steps[st].check=che; 1229 if (che) steps[st].weight+=10; 1230 if (only_action) { 1231 if (steps[st].fig1==fp&&steps[st].x2==1||steps[st].fig1==-fp&&steps[st].x2==6) // 1232 { cur_var++; return; } 1233 if (steps[st].fig2==0&&steps[st].type<4&&!che&&!check_on_table) return; 1234 } 1235 1236 cur_var++; 1237} 1238//**************************** 1239void getrowstepsw(char j,char i,char dj,char di) { // 1240char d,j1,i1; 1241 j1=j; i1=i; 1242 for (d=1;d<8;d++) { 1243 j1+=dj; i1+=di; 1244 if (getpole(j1,i1)) { 1245 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1246 if (pole[j1][i1]!=0) break; 1247 } else break; 1248 } 1249} 1250//**************************** 1251void getrowstepsb(char j,char i,char dj,char di) { // 1252char d,j1,i1; 1253 j1=j; i1=i; 1254 for (d=1;d<8;d++) { 1255 j1+=dj; i1+=di; 1256 if (getpole(j1,i1)) { 1257 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1258 if (pole[j1][i1]!=0) break; 1259 } else break; 1260 } 1261} 1262//**************************** 1263void getonestepw(char j,char i,char dj,char di) { // 1264char j1,i1; 1265 j1=j+dj; i1=i+di; 1266 if (getpole(j1,i1)) 1267 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1268} 1269//**************************** 1270void getonestepb(char j,char i,char dj,char di) { // 1271char j1,i1; 1272 j1=j+dj; i1=i+di; 1273 if (getpole(j1,i1)) 1274 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1275} 1276//**************************** 1277void sort_variants(int from, int to) { // 1278 while (1) { 1279 int mov=0; 1280 for (int i=from;i<to;i++) // 1281 if (steps[i].weight<steps[i+1].weight) { 1282 mov++; 1283 step_type buf=steps[i]; 1284 steps[i]=steps[i+1]; 1285 steps[i+1]=buf; 1286 } 1287 if (mov==0) break; 1288 } 1289} 1290//**************************** 1291void load_variants(int nstep) { // nstep 1292 cur_var=0; 1293 if (pole[WKJ][WKI]!=fk||pole[BKJ][BKI]!=-fk) kingpositions(); 1294 if (nstep%2==1) check_on_table=get_check(fk); 1295 else check_on_table=get_check(-fk); // 1296 for (char i=0;i<8;i++) 1297 for (char j=0;j<8;j++) 1298 if (pole[j][i]>0&&nstep%2==1||pole[j][i]<0&&nstep%2==0) { 1299 switch (pole[j][i]) { 1300 case fp: // 1301 if (getpole(j-1,i)&&pole[j-1][i]==0) 1302 if (j!=1) addstep(j,i,j-1,i,0); else 1303 for (char t=4;t<8;t++) addstep(1,i,0,i,t); //-... 1304 if (j==6&&pole[j-1][i]==0&&pole[j-2][i]==0) addstep(j,i,j-2,i,0); 1305 if (getpole(j-1,i-1)&&pole[j-1][i-1]<0) 1306 if (j!=1) addstep(j,i,j-1,i-1,0); else 1307 for (char t=4;t<8;t++) addstep(j,i,j-1,i-1,t); //-... 1308 if (getpole(j-1,i+1)&&pole[j-1][i+1]<0) 1309 if (j!=1) addstep(j,i,j-1,i+1,0); else 1310 for (char t=4;t<8;t++) addstep(j,i,j-1,i+1,t); //-... 1311 if (j==3&&steps[nstep-1].fig1==-fp&&steps[nstep-1].x2==3&&steps[nstep-1].x1==1) { 1312 if (steps[nstep-1].y2-i==1) { // 1313 addstep(j,i,j-1,i+1,1); 1314 } else if (steps[nstep-1].y2-i==-1) { // 1315 addstep(j,i,j-1,i-1,1); 1316 } 1317 } 1318 break; 1319 case -fp: // 1320 if (getpole(j+1,i)&&pole[j+1][i]==0) 1321 if (j!=6) addstep(j,i,j+1,i,0); else 1322 for (char t=4;t<8;t++) addstep(j,i,j+1,i,t); //-... 1323 if (j==1&&pole[j+1][i]==0&&pole[j+2][i]==0) addstep(j,i,j+2,i,0); 1324 if (getpole(j+1,i-1)&&pole[j+1][i-1]>0) 1325 if (j!=6) addstep(j,i,j+1,i-1,0); else 1326 for (char t=4;t<8;t++) addstep(j,i,j+1,i-1,t); //-... 1327 if (getpole(j+1,i+1)&&pole[j+1][i+1]>0) 1328 if (j!=6) addstep(j,i,j+1,i+1,0); else 1329 for (char t=4;t<8;t++) addstep(j,i,j+1,i+1,t); //-... 1330 if (j==4&&steps[nstep-1].fig1==fp&&steps[nstep-1].x2==4&&steps[nstep-1].x1==6) { 1331 if (steps[nstep-1].y2-i==1) { // 1332 addstep(j,i,j+1,i+1,1); 1333 } else if (steps[nstep-1].y2-i==-1) { // 1334 addstep(j,i,j+1,i-1,1); 1335 } 1336 } 1337 break; 1338 case fn: // 1339 getonestepw(j,i,-2,-1); 1340 getonestepw(j,i,-2,1); 1341 getonestepw(j,i,-1,-2); 1342 getonestepw(j,i,-1,2); 1343 getonestepw(j,i,2,-1); 1344 getonestepw(j,i,2,1); 1345 getonestepw(j,i,1,-2); 1346 getonestepw(j,i,1,2); 1347 break; 1348 case -fn: // 1349 getonestepb(j,i,-2,-1); 1350 getonestepb(j,i,-2,1); 1351 getonestepb(j,i,-1,-2); 1352 getonestepb(j,i,-1,2); 1353 getonestepb(j,i,2,-1); 1354 getonestepb(j,i,2,1); 1355 getonestepb(j,i,1,-2); 1356 getonestepb(j,i,1,2); 1357 break; 1358 case fb: // 1359 getrowstepsw(j,i,1,1); 1360 getrowstepsw(j,i,-1,-1); 1361 getrowstepsw(j,i,1,-1); 1362 getrowstepsw(j,i,-1,1); 1363 break; 1364 case -fb: // 1365 getrowstepsb(j,i,1,1); 1366 getrowstepsb(j,i,-1,-1); 1367 getrowstepsb(j,i,1,-1); 1368 getrowstepsb(j,i,-1,1); 1369 break; 1370 case fr: // 1371 getrowstepsw(j,i,1,0); 1372 getrowstepsw(j,i,-1,0); 1373 getrowstepsw(j,i,0,1); 1374 getrowstepsw(j,i,0,-1); 1375 break; 1376 case -fr: // 1377 getrowstepsb(j,i,1,0); 1378 getrowstepsb(j,i,-1,0); 1379 getrowstepsb(j,i,0,1); 1380 getrowstepsb(j,i,0,-1); 1381 break; 1382 case fq: // 1383 getrowstepsw(j,i,1,1); 1384 getrowstepsw(j,i,-1,-1); 1385 getrowstepsw(j,i,1,-1); 1386 getrowstepsw(j,i,-1,1); 1387 getrowstepsw(j,i,1,0); 1388 getrowstepsw(j,i,-1,0); 1389 getrowstepsw(j,i,0,1); 1390 getrowstepsw(j,i,0,-1); 1391 break; 1392 case -fq: // 1393 getrowstepsb(j,i,1,1); 1394 getrowstepsb(j,i,-1,-1); 1395 getrowstepsb(j,i,1,-1); 1396 getrowstepsb(j,i,-1,1); 1397 getrowstepsb(j,i,1,0); 1398 getrowstepsb(j,i,-1,0); 1399 getrowstepsb(j,i,0,1); 1400 getrowstepsb(j,i,0,-1); 1401 break; 1402 case fk: // 1403 getonestepw(j,i, 1,-1); 1404 getonestepw(j,i, 1, 0); 1405 getonestepw(j,i, 1, 1); 1406 getonestepw(j,i, 0,-1); 1407 getonestepw(j,i, 0, 1); 1408 getonestepw(j,i,-1,-1); 1409 getonestepw(j,i,-1, 0); 1410 getonestepw(j,i,-1, 1); 1411 if (!check_on_table) add_rok(j,i,nstep); 1412 break; 1413 case -fk: // 1414 getonestepb(j,i, 1,-1); 1415 getonestepb(j,i, 1, 0); 1416 getonestepb(j,i, 1, 1); 1417 getonestepb(j,i, 0,-1); 1418 getonestepb(j,i, 0, 1); 1419 getonestepb(j,i,-1,-1); 1420 getonestepb(j,i,-1, 0); 1421 getonestepb(j,i,-1, 1); 1422 if (!check_on_table) add_rok(j,i,nstep); 1423 break; 1424 } //switch 1425 } //if 1426 1427 if (nstep>cur_step) { 1428 for (int i=start_var;i<start_var+cur_var;i++) { // 1429 for (char j=0;j<MAXCUTS;j++) 1430 if (cuts[j].fig1==steps[i].fig1&& 1431 cuts[j].x1==steps[i].x1&&cuts[j].y1==steps[i].y1&& 1432 cuts[j].x2==steps[i].x2&&cuts[j].y2==steps[i].y2) { 1433 steps[i].weight+=100+cuts[j].weight; 1434 break; 1435 } 1436 if (nstep>1&&steps[nstep-1].fig2!=0) // - 1437 if (steps[nstep-1].x2==steps[i].x2&&steps[nstep-1].y2==steps[i].y2) 1438 steps[i].weight+=1000; 1439 } 1440 } 1441 1442 sort_variants(start_var,start_var+cur_var-1); 1443 1444 } 1445//**************************** 1446void movestep(int nstep) { 1447 pole[steps[nstep].x1][steps[nstep].y1]=0; 1448 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig1; 1449 if (steps[nstep].fig1==fk) { 1450 WKJ=steps[nstep].x2; WKI=steps[nstep].y2; 1451 } else if (steps[nstep].fig1==-fk) { 1452 BKJ=steps[nstep].x2; BKI=steps[nstep].y2; 1453 } 1454 if (steps[nstep].type==0) return; 1455 if (steps[nstep].type==1) // 1456 if (steps[nstep].fig1>0) 1457 pole[steps[nstep].x2+1][steps[nstep].y2]=0; 1458 else 1459 pole[steps[nstep].x2-1][steps[nstep].y2]=0; 1460 else if (steps[nstep].type==2) // 1461 if (steps[nstep].fig1>0) { // 1462 pole[7][4]=0; pole[7][5]=fr; pole[7][6]=fk; pole[7][7]=0; //show_board(); delay(3000); 1463 } else { // 1464 pole[0][4]=0; pole[0][5]=-fr; pole[0][6]=-fk; pole[0][7]=0; //show_board(); delay(3000); 1465 } 1466 else if (steps[nstep].type==3) // 1467 if (steps[nstep].fig1>0) { // 1468 pole[7][0]=0; pole[7][1]=0; pole[7][2]=fk; pole[7][3]=fr; pole[7][4]=0; 1469 } else { // 1470 pole[0][0]=0; pole[0][1]=0; pole[0][2]=-fk; pole[0][3]=-fr; pole[0][4]=0; 1471 } 1472 else if (steps[nstep].type>3) //-.... 1473 if (steps[nstep].fig1>0) pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].type-2; 1474 else pole[steps[nstep].x2][steps[nstep].y2]=2-steps[nstep].type; 1475} 1476//**************************** 1477void backstep(int nstep) { 1478 pole[steps[nstep].x1][steps[nstep].y1]=steps[nstep].fig1; 1479 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig2; 1480 if (steps[nstep].fig1==fk) { 1481 WKJ=steps[nstep].x1; WKI=steps[nstep].y1; 1482 } else if (steps[nstep].fig1==-fk) { 1483 BKJ=steps[nstep].x1; BKI=steps[nstep].y1; 1484 } 1485 if (steps[nstep].type==0) return; 1486 if (steps[nstep].type==1) { // 1487 pole[steps[nstep].x2][steps[nstep].y2]=0; 1488 if (steps[nstep].fig1>0) 1489 pole[steps[nstep].x2+1][steps[nstep].y2]=-fp; 1490 else 1491 pole[steps[nstep].x2-1][steps[nstep].y2]=fp; 1492 } else if (steps[nstep].type==2) // 1493 if (steps[nstep].fig1>0) { // 1494 pole[7][4]=fk; pole[7][5]=0; pole[7][6]=0; pole[7][7]=fr; 1495 } else { // 1496 pole[0][4]=-fk; pole[0][5]=0; pole[0][6]=0; pole[0][7]=-fr; // show_board(); delay(3000); 1497 } 1498 else if (steps[nstep].type==3) // 1499 if (steps[nstep].fig1>0) { // 1500 pole[7][0]=fr; pole[7][1]=0; pole[7][2]=0; pole[7][3]=0; pole[7][4]=fk; 1501 } else { // 1502 pole[0][0]=-fr; pole[0][1]=0; pole[0][2]=0; pole[0][3]=0; pole[0][4]=-fk; 1503 } 1504 1505} 1506//**************************** 1507void get_wrocks(int nstep) { 1508 w00=true; w000=true; 1509 for (int i=1;i<nstep;i++) { 1510 if (steps[i].fig1==fk) { w00=false; w000=false; return; } 1511 if (steps[i].fig1==fr) 1512 if (steps[i].x1==7&&steps[i].y1==7) w00=false; 1513 else if (steps[i].x1==7&&steps[i].y1==0) w000=false; 1514 } 1515} 1516//**************************** 1517void get_brocks(int nstep) { 1518 b00=true; b000=true; 1519 for (int i=1;i<nstep;i++) { 1520 if (steps[i].fig1==-fk) { b00=false; b000=false; return; } 1521 if (steps[i].fig1==-fr) 1522 if (steps[i].x1==0&&steps[i].y1==7) b00=false; 1523 else if (steps[i].x1==0&&steps[i].y1==0) b000=false; 1524 } 1525} 1526//**************************** 1527void add_rok(char j,char i,int nstep) { // 1528boolean che1,che2; 1529 if (nstep%2==1) { // 1530 if (j!=7||i!=4) return; 1531 if (pole[7][5]==0&&pole[7][6]==0&&pole[7][7]==fr) { // 1532 pole[7][4]=0; 1533 WKI=5; che1=get_check(fk); 1534 WKI=6; che2=get_check(fk); 1535 WKI=4; pole[7][4]=fk; 1536 get_wrocks(nstep); 1537 if (!che1&&!che2&&w00) addstep(7,4,7,6,2); 1538 } 1539 if (pole[7][0]==fr&&pole[7][1]==0&&pole[7][2]==0&&pole[7][3]==0) { // 1540 pole[7][4]=0; 1541 WKI=2; che1=get_check(fk); 1542 WKI=3; che2=get_check(fk); 1543 WKI=4; pole[7][4]=fk; 1544 get_wrocks(nstep); 1545 if (!che1&&!che2&&w000) addstep(7,4,7,2,3); 1546 } 1547 } else { // 1548 if (j!=0||i!=4) return; 1549 if (pole[0][5]==0&&pole[0][6]==0&&pole[0][7]==-fr) { // 1550 pole[0][4]=0; 1551 BKI=5; che1=get_check(-fk); 1552 BKI=6; che2=get_check(-fk); 1553 BKI=4; pole[0][4]=-fk; 1554 get_brocks(nstep); 1555 if (!che1&&!che2&&b00) addstep(0,4,0,6,2); 1556 } 1557 if (pole[0][0]==-fr&&pole[0][1]==0&&pole[0][2]==0&&pole[0][3]==0) { // 1558 pole[0][4]=0; 1559 BKI=2; che1=get_check(-fk); 1560 BKI=3; che2=get_check(-fk); 1561 BKI=4; pole[0][4]=-fk; 1562 get_brocks(nstep); 1563 if (!che1&&!che2&&b000) addstep(0,4,0,2,3); 1564 } 1565 1566 } 1567} 1568//**************************** 1569int addrowstepsw(char j,char i,char dj,char di,char dc) { // 1570char d,j1,i1; int c=0; 1571 j1=j; i1=i; 1572 for (d=1;d<8;d++) { 1573 j1+=dj; i1+=di; 1574 if (getpole(j1,i1)) { 1575 if (pole[j1][i1]==0) c+=dc; 1576 else if (pole[j1][i1]>0) { // 1577 c+=1; break; 1578 } else { 1579 if (cur_step>6) c+=-pole[j1][i1]; // 1580 break; 1581 } 1582 } else break; 1583 } 1584 return c; 1585} 1586//**************************** 1587int addrowstepsb(char j,char i,char dj,char di,char dc) { // 1588char d,j1,i1; int c=0; 1589 j1=j; i1=i; 1590 for (d=1;d<8;d++) { 1591 j1+=dj; i1+=di; 1592 if (getpole(j1,i1)) { 1593 if (pole[j1][i1]==0) c-=dc; 1594 else if (pole[j1][i1]<0) { // 1595 c-=1; break; 1596 } else { 1597 if (cur_step>6) c-=pole[j1][i1]; // 1598 break; 1599 } 1600 } else break; 1601 } 1602 return c; 1603} 1604//**************************** 1605int addonestepw(char j,char i,char dj,char di) { // 1606char j1,i1; 1607 j1=j+dj; i1=i+di; 1608 if (getpole(j1,i1)) 1609 if (pole[j1][i1]==0) return 2; 1610 else if (pole[j1][i1]>0) return 1; 1611 else return -pole[j1][i1]; 1612 return 0; 1613} 1614//**************************** 1615int addonestepb(char j,char i,char dj,char di) { // 1616char j1,i1; 1617 j1=j+dj; i1=i+di; 1618 if (getpole(j1,i1)) 1619 if (pole[j1][i1]==0) return 2; 1620 else if (pole[j1][i1]<0) return 1; 1621 else return pole[j1][i1]; 1622 return 0; 1623} 1624//**************************** 1625int activity() { // - + 1626int c=0; 1627char pwj[8],pwi[8],pbj[8],pbi[8],ipw=0,ipb=0,nbw=0,nbb=0; 1628 if (checkmatesearch) return 0; // 1629 for (char i=0;i<8;i++) 1630 for (char j=0;j<8;j++) 1631 if (pole[j][i]!=0) { 1632 switch (pole[j][i]) { 1633 case fp: // 1634 if (getpole(j,i+1)&&pole[j][i+1]==fp) c+=2; // 1635 if (getpole(j-1,i)&&pole[j-1][i]==fp) c-=20; // 1636 pwj[ipw]=j; pwi[ipw]=i; ipw++; 1637 break; 1638 case -fp: // 1639 if (getpole(j,i-1)&&pole[j][i-1]==-fp) c-=2; // 1640 if (getpole(j+1,i)&&pole[j+1][i]==-fp) c+=20; // 1641 pbj[ipb]=j; pbi[ipb]=i; ipb++; 1642 break; 1643 case fn: // 1644 c+=addonestepw(j,i,-2,-1); 1645 c+=addonestepw(j,i,-2,1); 1646 c+=addonestepw(j,i,-1,-2); 1647 c+=addonestepw(j,i,-1,2); 1648 c+=addonestepw(j,i,2,-1); 1649 c+=addonestepw(j,i,2,1); 1650 c+=addonestepw(j,i,1,-2); 1651 c+=addonestepw(j,i,1,2); 1652 break; 1653 case -fn: // 1654 c+=addonestepb(j,i,-2,-1); 1655 c+=addonestepb(j,i,-2,1); 1656 c+=addonestepb(j,i,-1,-2); 1657 c+=addonestepb(j,i,-1,2); 1658 c+=addonestepb(j,i,2,-1); 1659 c+=addonestepb(j,i,2,1); 1660 c+=addonestepb(j,i,1,-2); 1661 c+=addonestepb(j,i,1,2); 1662 break; 1663 case fb: // 1664 c+=addrowstepsw(j,i,1,1,1); 1665 c+=addrowstepsw(j,i,-1,-1,2); 1666 c+=addrowstepsw(j,i,1,-1,1); 1667 c+=addrowstepsw(j,i,-1,1,2); 1668 nbw++; 1669 break; 1670 case -fb: // 1671 c+=addrowstepsb(j,i,1,1,2); 1672 c+=addrowstepsb(j,i,-1,-1,1); 1673 c+=addrowstepsb(j,i,1,-1,2); 1674 c+=addrowstepsb(j,i,-1,1,1); 1675 nbb++; 1676 break; 1677 case fr: // 1678 c+=addrowstepsw(j,i,1,0,2); // 1679 c+=addrowstepsw(j,i,-1,0,2); 1680 c+=addrowstepsw(j,i,0,1,1); 1681 c+=addrowstepsw(j,i,0,-1,1); 1682 break; 1683 case -fr: // 1684 c+=addrowstepsb(j,i,1,0,2); // 1685 c+=addrowstepsb(j,i,-1,0,2); 1686 c+=addrowstepsb(j,i,0,1,1); 1687 c+=addrowstepsb(j,i,0,-1,1); 1688 break; 1689 case fq: // 1690 if (cur_step>10) { 1691 c+=addrowstepsw(j,i,1,1,1); 1692 c+=addrowstepsw(j,i,-1,-1,2); 1693 c+=addrowstepsw(j,i,1,-1,1); 1694 c+=addrowstepsw(j,i,-1,1,2); 1695 c+=addrowstepsw(j,i,1,0,1); 1696 c+=addrowstepsw(j,i,-1,0,2); 1697 c+=addrowstepsw(j,i,0,1,1); 1698 c+=addrowstepsw(j,i,0,-1,1); 1699 } 1700 break; 1701 case -fq: // 1702 if (cur_step>10) { 1703 c+=addrowstepsb(j,i,1,1,2); 1704 c+=addrowstepsb(j,i,-1,-1,1); 1705 c+=addrowstepsb(j,i,1,-1,2); 1706 c+=addrowstepsb(j,i,-1,1,1); 1707 c+=addrowstepsb(j,i,1,0,2); 1708 c+=addrowstepsb(j,i,-1,0,1); 1709 c+=addrowstepsb(j,i,0,1,1); 1710 c+=addrowstepsb(j,i,0,-1,1); 1711 } 1712 break; 1713 } //switch 1714 if (cur_step>6) { 1715 if (pole[j][i]>0) { // 1716 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c+=3; 1717 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c+=3; 1718 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c+=2; 1719 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c+=2; 1720 } else { // 1721 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c-=3; 1722 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c-=3; 1723 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c-=2; 1724 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c-=2; 1725 } 1726 } 1727 } 1728 if (nbw>1) c+=30; // 1729 if (nbb>1) c-=30; 1730 for (char w=0;w<ipw;w++) { // 1731 boolean pass=1; 1732 for (char b=0;b<ipb;b++) { 1733 if (pwi[w]>0&&pbi[b]==pwi[w]-1&&pbj[b]<pwj[w]) pass=0; // 1734 if (pbi[b]==pwi[w]&&pbj[b]<pwj[w]) pass=0; // 1735 if ( pwi[w]<7&&pbi[b]==pwi[w]+1&&pbj[b]<pwj[w]) pass=0; // 1736 if (!pass) break; 1737 } 1738 if (pass) { 1739 c+=50; 1740 if (!endspiel) break; // 1741 } 1742 } 1743 for (char b=0;b<ipb;b++) { // 1744 boolean pass=1; 1745 for (char w=0;w<ipw;w++) { 1746 if (pbi[b]>0&&pwi[w]==pbi[b]-1&&pwj[w]>pbj[b]) pass=0; // 1747 if (pwi[w]==pbi[b]&&pwj[w]>pbj[b]) pass=0; // 1748 if (pbi[b]<7&&pwi[w]==pbi[b]+1&&pwj[w]>pbj[b]) pass=0; // 1749 if (!pass) break; 1750 } 1751 if (pass) { 1752 c-=50; 1753 if (!endspiel) break; // 1754 } 1755 } 1756 return c; 1757} 1758//**************************** 1759int evaluate(int nstep) { // 1760 long ww=0, wb=0; 1761 for (char i=0;i<8;i++) 1762 for (char j=0;j<8;j++) 1763 if (pole[j][i]<0) { 1764 wb+=fig_weight[-pole[j][i]]+(int)pgm_read_word(&pos[-pole[j][i]-1][7-j][i]); 1765 } 1766 else if (pole[j][i]>0) { 1767 ww+=fig_weight[pole[j][i]]+(int)pgm_read_word(&pos[pole[j][i]-1][j][i]); 1768 } 1769 count++; // 1770 long str=activity(); 1771 if (endspiel) { // 1772 wb+=(int)pgm_read_word(&pos[6][7-BKJ][BKI])-(int)pgm_read_word(&pos[5][7-BKJ][BKI]); 1773 ww+=(int)pgm_read_word(&pos[6][WKJ][WKI])-(int)pgm_read_word(&pos[5][WKJ][WKI]); 1774 if (wb<450&&ww>450) { // - - 1775 str-=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1776 } else if (ww<450&&wb>450) { // 1777 str+=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1778 } 1779 } 1780 if (nstep>8) { // 1781 if (steps[nstep-1].fig1==steps[nstep-5].fig1&&steps[nstep-2].fig1==steps[nstep-6].fig1&& 1782 steps[nstep-3].fig1==steps[nstep-7].fig1&&steps[nstep-4].fig1==steps[nstep-8].fig1) 1783 if (steps[nstep-1].x1==steps[nstep-5].x1&&steps[nstep-2].x1==steps[nstep-6].x1&& 1784 steps[nstep-3].x1==steps[nstep-7].x1&&steps[nstep-4].x1==steps[nstep-8].x1) 1785 if (steps[nstep-1].x2==steps[nstep-5].x2&&steps[nstep-2].x2==steps[nstep-6].x2&& 1786 steps[nstep-3].x2==steps[nstep-7].x2&&steps[nstep-4].x2==steps[nstep-8].x2) 1787 if (steps[nstep-1].y1==steps[nstep-5].y1&&steps[nstep-2].y1==steps[nstep-6].y1&& 1788 steps[nstep-3].y1==steps[nstep-7].y1&&steps[nstep-4].y1==steps[nstep-8].x1) 1789 if (steps[nstep-1].y2==steps[nstep-5].y2&&steps[nstep-2].y2==steps[nstep-6].y2&& 1790 steps[nstep-3].y2==steps[nstep-7].y2&&steps[nstep-4].y2==steps[nstep-8].y2) 1791 { Serial.println(F(" Draw - 3 repeat")); return 0; } 1792 } 1793 if (nstep%2==1) return 5000*(ww-wb)/(ww+wb+2000)+str; else return 5000*(wb-ww)/(ww+wb+2000)-str; 1794} 1795//**************************** 1796void add_cut(int ind) {// ind 1797 int minbeta=30000, minindex=MAXCUTS-1; 1798 for (char i=0;i<MAXCUTS;i++) { 1799 if (cuts[i].weight==0) { minindex=i; break; }// 1800 if (cuts[i].fig1==steps[ind].fig1&& 1801 cuts[i].x1==steps[ind].x1&&cuts[i].y1==steps[ind].y1&& 1802 cuts[i].x2==steps[ind].x2&&cuts[i].y2==steps[ind].y2) { 1803 cuts[i].weight++; return; // - 1 1804 } 1805 if (cuts[i].weight<minbeta) { // 1806 minbeta=cuts[i].weight; 1807 minindex=i; 1808 } 1809 } 1810 cuts[minindex]=steps[ind]; 1811 cuts[minindex].weight=1; //1 1812} 1813//**************************** 1814int quiescence(int start, int nstep, int alpha, int beta ) { 1815 if (nstep-cur_step>=LIMDEPTH||start>MAXSTEPS-70) return evaluate(nstep); 1816 if (!solving&&progress==0) return -5000; 1817 int score=-20000; 1818 start_var=start; 1819 only_action=true; 1820 load_variants(nstep); 1821 if (!check_on_table) { 1822 int stand_pat = evaluate(nstep); 1823 if (stand_pat >= score) score=stand_pat; 1824 if (score>alpha) alpha=score; 1825 if (alpha>=beta) return alpha; 1826 } 1827 if (cur_var==0) { 1828 if (check_on_table) { 1829 if (TRACE) Serial.println(F("checkmate?")); 1830 return -10000+nstep-cur_step; 1831 } else return evaluate(nstep); 1832 } 1833 int j=start+cur_var; 1834 for (int i=start;i<j;i++) { // 1835 if (TRACE) { //***** 1836 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" . ")); 1837 Serial.println(str_step(i)); 1838 } 1839 movestep(i); 1840 steps[nstep]=steps[i]; 1841 int tmp=-quiescence(j+1,nstep+1,-beta,-alpha); 1842 backstep(i); 1843 if (tmp>score) score=tmp; 1844 if (score>alpha) alpha=score; 1845 if (alpha>=beta ) { 1846 add_cut(nstep); 1847 return alpha; 1848 } 1849 if (checkmatesearch&&alpha>9000) break; 1850 gui(); 1851 } 1852 return score; 1853} 1854//**************************** 1855int alphaBeta(int start, int nstep, int alpha, int beta, int depthleft) { 1856//start - 1857//nstep - 1858int score=-20000,best; 1859 if( depthleft==0) return quiescence(start,nstep,alpha,beta) ; 1860 if (start>MAXSTEPS-70) return evaluate(nstep); 1861 start_var=start; 1862 only_action=false; 1863 if (nstep!=cur_step) load_variants(nstep); 1864 if (cur_var==0) { 1865 if (check_on_table) { 1866 if (TRACE) Serial.println(F("checkmate!")); 1867 return -10000+nstep-cur_step; 1868 } 1869 return 0; 1870 } 1871 int j=start+cur_var; 1872 best=start; 1873 for (int i=start;i<j;i++) { // 1874 if (nstep==cur_step) { 1875 Serial.print(str_step(i)); Serial.print(" "); 1876 Serial.print(i-start+1); Serial.print("/"); Serial.print(j-start); 1877 if (steps[i].weight<-9000) { Serial.println(F(" checkmate")); continue; } 1878 if (steps[i].fig2!=0||steps[i].check||alpha<-100) 1879 // , , -100 1880 { LIMDEPTH=MAXDEPTH+2; Serial.print(F("+2")); } else LIMDEPTH=MAXDEPTH; 1881 } else { 1882 if (TRACE) { //***** 1883 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" ")); 1884 Serial.println(str_step(i)); 1885 } 1886 } 1887 movestep(i); 1888 steps[nstep]=steps[i]; 1889 int tmp=-alphaBeta(j+1,nstep+1,-beta,-alpha,depthleft-1); 1890 backstep(i); 1891 steps[i].weight=tmp; 1892 if (tmp>score) score=tmp; 1893 if (score>alpha) { 1894 alpha=score; 1895 if (nstep>cur_step) add_cut(nstep); // 1896 if (TRACE) { Serial.print(F("ALPHA+:")); Serial.println(score); } 1897 best=i; 1898 if (nstep==cur_step) { 1899 steps[0]=steps[best]; 1900 Serial.print(F(" BEST")); 1901 } 1902 } 1903 if (alpha>=beta ) { 1904 if (nstep>cur_step) add_cut(nstep); // 1905 if (TRACE) { Serial.print(F("BETA CUT:")); Serial.println(score); } 1906 return alpha; 1907 } 1908 if (nstep==cur_step) { 1909 Serial.print(F(" ")); Serial.println(tmp); 1910 progress=100*(i-start+1)/(j-start); 1911 if (alpha==9999||alpha==-5000) break; 1912 if (checkmatesearch&&alpha>9000&&limit<3) break; 1913 if (!solving) { 1914 if (alpha>startweight&&cur_level>1) break; 1915 else { // - +50% 1916 if (BAction.bs==F("stopping")) break; 1917 if (cur_level>1&&100*(millis()-starttime)/(limittime-starttime)>240-limit*20) break; 1918 } 1919 } 1920 } 1921 if (nstep==cur_step+1&&!solving&&progress==0) break; 1922 gui(); 1923 } 1924 if (nstep==cur_step) { steps[nstep]=steps[best]; steps[0]=steps[best]; } 1925 return score; 1926} 1927//**************************** 1928void kingpositions() { 1929 for (char i=0;i<8;i++) // 1930 for (char j=0;j<8;j++) 1931 if (pole[j][i]==fk) { 1932 WKJ=j; WKI=i; 1933 } else if (pole[j][i]==-fk) { 1934 BKJ=j; BKI=i; 1935 } 1936} 1937//**************************** 1938int get_endspiel() { // 1939 int weight=0; 1940 for (char i=0;i<8;i++) 1941 for (char j=0;j<8;j++) 1942 if (pole[j][i]<0) 1943 weight+=fig_weight[-pole[j][i]]; 1944 else if (pole[j][i]>0) 1945 weight+=fig_weight[pole[j][i]]; // 8000 1946 if (weight<3500) endspiel=true; else endspiel=false; 1947 return weight; 1948} 1949//**************************** 1950boolean is_drawn() { // 1951 boolean drawn=false; 1952 int cn=0,cbw=0,cbb=0,co=0,cb=0,cw=0; 1953 for (char i=0;i<8;i++) 1954 for (char j=0;j<8;j++) { 1955 if (abs(pole[j][i])==1) co++; 1956 if (abs(pole[j][i])>3&&abs(pole[j][i])<6) co++; // , , 1957 if (abs(pole[j][i])==6) continue; // 1958 if (abs(pole[j][i])==2) cn++; // 1959 if (abs(pole[j][i])==3&&(i+j+2)%2==0) cbb++; // 1960 if (abs(pole[j][i])==3&&(i+j+2)%2==1) cbw++; // 1961 if (pole[j][i]==3) cw++; // 1962 if (pole[j][i]==-3) cb++; // 1963 } 1964 if (cn==1&&co+cbb+cbw==0) drawn=true; // 1965 if (cbb+cbw==1&&co+cn==0) drawn=true; // 1966 if (co+cn+cbb==0||co+cn+cbw==0) drawn=true; // 1967 if (co+cn==0&&cb==1&&cw==1) drawn=true; // 1968 if (drawn) return drawn; 1969 int rep=1; 1970 for (int s=cur_step-1; s>0; s--) { 1971 if (steps[s].fig1==0) continue; 1972 backstep(s); 1973 boolean eq=true; 1974 for (char i=0;i<8;i++) 1975 for (char j=0;j<8;j++) 1976 if (pole[j][i]!=pole0[j][i]) 1977 { eq=false; break; } 1978 if (eq) rep++; 1979 if (rep>2) break; 1980 } 1981 if (rep>1) { Serial.print(rep); Serial.println(" repetitions"); } 1982 for (char i=0;i<8;i++) 1983 for (char j=0;j<8;j++) pole[j][i]=pole0[j][i]; // 1984 if (rep>2) drawn=true; 1985 return drawn; 1986} 1987//**************************** 1988int solve_step() { 1989const int LMIN[6]={2,3,4,5,6,7}; 1990const int LMAX[6]={4,6,8,10,12,14}; 1991const int LCMIN[6]={2,4,6,8,10,12}; 1992const int LCMAX[6]={4,6,8,10,12,14}; 1993boolean check_on; 1994 starttime=millis(); 1995 isstatus=1; 1996 deletebuttons(); 1997 clearstatus(); 1998 tft.drawFastHLine(10,312,300,DARK); 1999 tft.drawFastHLine(10,315,300,DARK); 2000 BAction.Show("STOP"); 2001 BBW.Hide(); 2002 BBack.Hide(); 2003 BLimit.Hide(); 2004 BCM.Hide(); 2005 BDesc.Hide(); 2006 BSound.Hide(); 2007 BRotate.Hide(); 2008 limittime=starttime+limits[limit]*1000; // 2009 for (char i=0;i<8;i++) 2010 for (char j=0;j<8;j++) pole0[j][i]=pole[j][i]; // 2011 lastbest.fig1=0; 2012 kingpositions(); 2013 int wei=get_endspiel(); 2014 count=0; 2015 startweight=evaluate(cur_step); 2016 Serial.println(""); 2017 Serial.println(F("---------------")); 2018 if (endspiel) { Serial.print(F("Endspiel: ")); Serial.println(wei); } 2019 if (cur_step%2==1) Serial.print(F("WHITE, ")); else Serial.print(F("BLACK, ")); 2020 Serial.print(F("start Score= ")); Serial.println(startweight); 2021 start_var=cur_step+21; 2022 only_action=false; 2023 lastbest.fig1=0; 2024 steps[0].fig1=0; 2025 load_variants(cur_step); 2026 check_on=check_on_table; 2027 if (cur_var==0||is_drawn()) { 2028 beep(500); 2029 tft.drawFastHLine(10,312,300,BLACK); 2030 tft.drawFastHLine(10,315,300,BLACK); 2031 BAction.Hide(); 2032 isstatus=0; 2033 solving=false; 2034 if (check_on_table) return -9999; else return 8999; 2035 } 2036 if (cur_var==1) { // 1 2037 tft.drawFastHLine(10,312,300,BLACK); 2038 tft.drawFastHLine(10,315,300,BLACK); 2039 BAction.Hide(); 2040 isstatus=0; 2041 solving=false; 2042 steps[cur_step]=steps[cur_step+21]; 2043 return startweight; 2044 } 2045 int vars=cur_var; 2046 int ALPHA=-20000; 2047 int BETA=20000; 2048 int score; 2049 solving=true; 2050 if (checkmatesearch) { 2051 Serial.println(F("Checkmate search")); 2052 steps[cur_step].fig1=0; lastscore=0; show_steps(); 2053 } 2054 int l=0; 2055 for (char i=0;i<MAXCUTS;i++) { cuts[i].weight=0; cuts[i].fig1=0; } // 2056 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // 2057 movestep(i); 2058 steps[i].weight=evaluate(cur_step); 2059 if (steps[i].fig2!=0) steps[i].weight-=steps[i].fig1; 2060 backstep(i); 2061 } 2062 while (l<6) { 2063 cur_level=l+1; 2064 progress=0; 2065 tft.drawFastHLine(10,315,300,DARK); 2066 MINDEPTH=LMIN[l]; 2067 MAXDEPTH=LMAX[l]; 2068 if (checkmatesearch) { 2069 ALPHA=9000; BETA=10000; 2070 MINDEPTH=LCMIN[l]; 2071 MAXDEPTH=LCMAX[l]; 2072 } 2073 if (l>0) Serial.println(""); Serial.print(F("******* LEVEL=")); Serial.print(l+1); 2074 Serial.print(" "); Serial.print(MINDEPTH); Serial.print("-"); Serial.print(MAXDEPTH); 2075 Serial.print(" "); Serial.print((millis()-starttime)/1000.,1); Serial.println("s"); 2076 if (checkmatesearch) { 2077 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // - 2078 movestep(i); 2079 start_var=cur_step+22+vars; 2080 only_action=false; 2081 load_variants(cur_step+1); 2082 backstep(i); 2083 steps[i].weight=-cur_var; 2084 } 2085 } 2086 sort_variants(cur_step+21,cur_step+20+vars); // 2087 cur_var=vars; 2088 check_on_table=check_on; 2089 score=alphaBeta(cur_step+21,cur_step,ALPHA,BETA,MINDEPTH); 2090 if (score>9996) break; 2091 if (score>9000&&limit<3) break; 2092 if (100*(millis()-starttime)/(limittime-starttime)>70-l*8) break; 2093 if (!solving) break; 2094 l++; 2095 } //while l 2096 if (score<-9000) { 2097 Serial.println(F("GIVE UP!")); 2098 } else { 2099 Serial.print(F("STEP=")); Serial.println(str_step(cur_step)); 2100 if (score>9000) { 2101 Serial.print(F("CHECKMATE ")); 2102 if (score<9999) { 2103 Serial.print(F("in ")); Serial.print((9999-score)/2+1); Serial.print(F(" steps")); 2104 } else steps[cur_step].check=2; 2105 Serial.println(""); 2106 if (checkmatesearch) { 2107 steps[cur_step+9999-score].check=2; 2108 steps[cur_step+10000-score].fig1=0; 2109 for (int i=cur_step;i<cur_step+10000-score;i++) Serial.println(str_step(i)); 2110 } 2111 } 2112 } 2113 tft.drawFastHLine(10,312,300,BLACK); 2114 tft.drawFastHLine(10,315,300,BLACK); 2115 progress=0; 2116 show_status(); 2117 BAction.Hide(); 2118 solving=0; 2119 isstatus=0; 2120 return score; 2121 2122 2123} 2124//**************************** 2125
ArduinoMegaChess_DUE
arduino
Sketch updated to Arduino DUE with ILI9486/XPT2046 320x480 pixels touchscreen.
1//ArduinoMega Chess 1.0 (DUE 320x480 version) 2//Sergey Urusov, ususovsv@gmail.com 3 4#include <TFT_HX8357_Due.h> // Hardware-specific library 5#include <stdint.h> 6#include <URTouch.h> 7#include <avr/pgmspace.h> 8#include <Streaming.h> 9#include <DueFlashStorage.h> 10 11uint16_t BLACK,BLUE,RED,GREEN,CYAN,MAGENTA,YELLOW,WHITE,GRAY,DARK,GRAY2,BLACKF,WHITEF; 12 13TFT_HX8357_Due tft = TFT_HX8357_Due(); 14 15URTouch myTouch( 6, 5, 4, 3, 2); 16 17DueFlashStorage dueFlashStorage; 18 19int touchx,touchy,touchx0,touchy0,ntouch; 20unsigned long lasttouch; 21#define MINPRESSURE 10 22#define MAXPRESSURE 1000 23const int PinBuzz = 12; 24int cycle=0; // 25const signed char fp=1; 26const signed char fn=2; 27const signed char fb=3; 28const signed char fr=4; 29const signed char fq=5; 30const signed char fk=6; 31const int fig_weight[]={0,100,320,330,500,900,0}; 32const char fig_symb[]=" NBRQK"; 33unsigned long count; 34boolean rotate=false; 35const signed char polezero[8][8] PROGMEM={ 36 { 0, 0, 0, 0, 0, 0, 0, 0}, 37 { 0, 0, 0, 0, 0, 0, 0, 0}, 38 { 0, 0, 0, 0, 0, 0, 0, 0}, 39 { 0, 0, 0, 0, 0, 0, 0, 0}, 40 { 0, 0, 0, 0, 0, 0, 0, 0}, 41 { 0, 0, 0, 0, 0, 0, 0, 0}, 42 { 0, 0, 0, 0, 0, 0, 0, 0}, 43 { 0, 0, 0, 0, 0, 0, 0, 0}, 44 }; 45const signed char polestart[8][8] PROGMEM={ 46 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 47 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 48 { 0, 0, 0, 0, 0, 0, 0, 0}, 49 { 0, 0, 0, 0, 0, 0, 0, 0}, 50 { 0, 0, 0, 0, 0, 0, 0, 0}, 51 { 0, 0, 0, 0, 0, 0, 0, 0}, 52 { fp, fp, fp, fp, fp, fp, fp, fp}, 53 { fr, fn, fb, fq, fk, fb, fn, fr}, 54 }; 55 56signed char pole[8][8]={ // 3 57 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 58 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 59 { 0, 0, 0, 0, 0, 0, 0, 0}, 60 { 0, 0, 0, 0, 0, 0, 0, 0}, 61 { 0, 0, 0, 0, 0, 0, 0, 0}, 62 { 0, 0, 0, 0, 0, 0, 0, 0}, 63 { fp, fp, fp, fp, fp, fp, fp, fp}, 64 { fr, fn, fb, fq, fk, fb, fn, fr}, 65 }; 66 67signed char pole0[8][8]; // 68signed char poledisp[8][8]; // 69signed char polechoice[7]; // 70boolean w00,w000,b00,b000; 71signed char blinkstep; 72typedef struct { 73 signed char fig1, fig2; // 74 signed char x1,y1,x2,y2; // 75 signed char check; // 76 signed char type; // 1- ,2- ,3- ,4-5-6-7- ,,, 77 int weight; // , 78} step_type; 79const int MAXSTEPS=1000; //. 80//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 81int MINDEPTH; //. 82int MAXDEPTH; //. 83int LIMDEPTH; //. 84const signed char MAXCUTS=10; //.. - - (10- , 20) 85boolean TRACE=0; 86boolean checkmatesearch=0; // 87boolean solving=false; 88boolean choice=false; 89boolean sound=1; 90short cur_step=1; // , 1.... 91short limit=0; // , 0-7; 92//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 93const int limits[8]={3,10,30,60,180,600,1800,100000}; 94// 0 1 2 3 4 5 6 7 95step_type steps[MAXSTEPS]; // 96step_type cuts[MAXCUTS]; // - 97step_type lastbest; 98int lastscore; 99int minbeta,maxalpha; 100int startweight; 101int cur_level; // () 102int start_var; // , 1.... 103int cur_var; // , 1.... 104int cur_choice; // 105boolean check_on_table; // 106boolean isstatus; 107signed char WKJ=0,WKI=0,BKJ=0,BKI=0; // 108boolean endspiel=false; // 109signed char progress; // 0-100 110boolean only_action=false; // - 111unsigned long starttime,limittime,quitime=0; 112boolean drag; 113signed char movedfig=0; 114const uint8_t fig[6][32] PROGMEM={ 115{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 1160x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x0, 0x0}, // 117{0x0, 0x0, 0x3, 0x40, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x3F, 0xFC, 0x39, 0xFC, 0x33, 0xFC, 1180x7, 0xFC, 0xF, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x7, 0xE0, 0x1F, 0xF8, 0x0, 0x0}, // 119{0x1, 0x80, 0x3, 0xC0, 0x1, 0x80, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 1200x1F, 0xF8, 0x1F, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x31, 0x8C, 0x7F, 0xFE, 0x0, 0x0}, // 121{0x0, 0x0, 0x19, 0x98, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 1220xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0}, // 123{0x19, 0x98, 0xD9, 0x9B, 0xD9, 0x9B, 0xD9, 0x9B, 0x6D, 0xB6, 0x6D, 0xB6, 0x6D, 0xB6, 0x35, 0xAC, 1240x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0}, // 125{0x1, 0x80, 0x1, 0x80, 0x79, 0x9E, 0x7D, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1260x7F, 0xFE, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0} // 127}; 128const uint8_t fig_cont[6][32] PROGMEM={ 129{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1300x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8}, // 131{0x3, 0x40, 0x4, 0xA0, 0x8, 0x10, 0x12, 0x8, 0x20, 0x4, 0x40, 0x2, 0x46, 0x2, 0x4C, 0x2, 1320x38, 0x2, 0x10, 0x4, 0x10, 0x8, 0x8, 0x10, 0x4, 0x20, 0x18, 0x18, 0x20, 0x4, 0x3F, 0xFC}, // 133{0x2, 0x40, 0x4, 0x20, 0x6, 0x60, 0x10, 0x8, 0x21, 0x84, 0x21, 0x84, 0x21, 0x84, 0x27, 0xE4, 1340x21, 0x84, 0x21, 0x84, 0x11, 0x88, 0x8, 0x10, 0x34, 0x2C, 0x4E, 0x72, 0x80, 0x1, 0xFF, 0xFF}, // 135{0x39, 0x9C, 0x26, 0x64, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 1360x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC}, // 137{0x22, 0x24, 0x22, 0x24, 0x22, 0x24, 0x26, 0x64, 0x92, 0x49, 0x92, 0x49, 0x92, 0x49, 0x4A, 0x52, 1380x40, 0x2, 0x40, 0x2, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC}, // 139{0x2, 0x40, 0x7A, 0x5E, 0x86, 0x61, 0x82, 0x41, 0x2, 0x40, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 1400x80, 0x81, 0x41, 0x82, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC} // 141}; 142 143const uint8_t fig24[6][72] PROGMEM={ 144{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1E, 0x0, 0x0, 0x3F, 0x0, 0x80, 0x7F, 0x0, 0x80, 0x7F, 0x0, 1450x80, 0x7F, 0x0, 0x0, 0x3F, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x1C, 0x0, 0x80, 0xFF, 0x0, 0x80, 0xFF, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x1C, 0x0, 1460x0, 0x3E, 0x0, 0x0, 0x7F, 0x0, 0x80, 0xFF, 0x0, 0xC0, 0xFF, 0x1, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0x0, 0x0, 0x0 147}, //pawn 148{0x0, 0x0, 0x0, 0x0, 0x6E, 0x0, 0x0, 0xFF, 0x0, 0x80, 0xFF, 0x1, 0x80, 0xFF, 0x3, 0xC0, 0xFF, 0x7, 0xE0, 0xFF, 0xF, 0xE0, 0xF9, 0xF, 1490xF0, 0xF8, 0xF, 0xF0, 0xF8, 0xF, 0x60, 0xFC, 0xF, 0x0, 0xFE, 0xF, 0x0, 0xFF, 0xF, 0x0, 0xFF, 0x7, 0x0, 0xFF, 0x3, 0x0, 0xFF, 0x1, 1500x0, 0xFE, 0x0, 0x0, 0x7C, 0x0, 0x0, 0x38, 0x0, 0x0, 0x38, 0x0, 0x0, 0x7C, 0x0, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 151}, //knight 152{0x0, 0x0, 0x0, 0x0, 0x3C, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x3C, 0x0, 0x0, 0x18, 0x0, 0x80, 0xFF, 0x1, 0xC0, 0xFF, 0x3, 1530xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 1540xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0x80, 0xFF, 0x1, 0x0, 0xFF, 0x0, 0x8, 0x7E, 0x10, 0x1C, 0x3C, 0x38, 0xFE, 0xFF, 0x7F, 0x0, 0x0, 0x0 155}, //bishop 156{0x0, 0x0, 0x0, 0xF0, 0x3C, 0xF, 0xF0, 0x3C, 0xF, 0xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 1570xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 1580xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 159}, //rook 160{0x0, 0x0, 0x0, 0xEE, 0xBD, 0x77, 0xEE, 0xBD, 0x77, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 0xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 1610xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 1620x30, 0xFF, 0xC, 0xB0, 0xFF, 0xD, 0xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 163}, //queen 164{0x0, 0x0, 0x0, 0x0, 0x18, 0x0, 0xF0, 0x18, 0xF, 0xF8, 0x99, 0x1F, 0xFC, 0xDB, 0x3F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 1650xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x3F, 0xFC, 0xFF, 0x1F, 0xF8, 0xFF, 0x1F, 1660xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 167} //king 168}; 169 170const uint8_t fig24_cont[6][72] PROGMEM={ 171{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x22, 0x0, 0x0, 0x41, 0x0, 0x80, 0x80, 0x0, 0x80, 0x80, 0x0, 1720x80, 0x80, 0x0, 0x0, 0x41, 0x0, 0x0, 0x22, 0x0, 0xC0, 0xE3, 0x1, 0x40, 0x0, 0x1, 0x40, 0x0, 0x1, 0xC0, 0xE3, 0x1, 0x0, 0x22, 0x0, 1730x0, 0x41, 0x0, 0x80, 0x80, 0x0, 0x40, 0x0, 0x1, 0x20, 0x0, 0x2, 0x10, 0x0, 0x4, 0x10, 0x0, 0x4, 0x10, 0x0, 0x4, 0xF0, 0xFF, 0x7 174}, //pawn 175{0x0, 0x6E, 0x0, 0x0, 0x91, 0x0, 0x80, 0x0, 0x1, 0x40, 0x2, 0x2, 0x40, 0x0, 0x4, 0x20, 0x0, 0x8, 0x10, 0x0, 0x10, 0x10, 0x6, 0x10, 1760x8, 0x5, 0x10, 0x8, 0x5, 0x10, 0x90, 0x2, 0x10, 0x60, 0x1, 0x10, 0x80, 0x0, 0x10, 0x80, 0x0, 0x8, 0x80, 0x0, 0x4, 0x80, 0x0, 0x2, 1770x0, 0x1, 0x1, 0x0, 0x82, 0x0, 0x0, 0x44, 0x0, 0x0, 0x44, 0x0, 0xF8, 0x83, 0x1F, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 178}, //knight 179{0x0, 0x3C, 0x0, 0x0, 0x42, 0x0, 0x0, 0x81, 0x0, 0x0, 0x81, 0x0, 0x0, 0x42, 0x0, 0x80, 0xE7, 0x1, 0x40, 0x0, 0x2, 0x20, 0x18, 0x4, 1800x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x90, 0xFF, 0x9, 0x90, 0xFF, 0x9, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 1810x10, 0x18, 0x8, 0x20, 0x18, 0x4, 0x40, 0x18, 0x2, 0x88, 0x0, 0x11, 0x14, 0x81, 0x28, 0xE2, 0xC3, 0x47, 0x1, 0x0, 0x80, 0xFF, 0xFF, 0xFF 182}, //bishop 183{0xF8, 0x7E, 0x1F, 0x8, 0xC3, 0x10, 0x8, 0xC3, 0x10, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xE0, 0xFF, 0x7, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 1840x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 1850x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0xE0, 0xFF, 0x7, 0x10, 0x0, 0x8, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 186}, //rook 187{0xEF, 0xBD, 0xF7, 0x11, 0x42, 0x88, 0x11, 0x42, 0x88, 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 1880x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 1890xC8, 0x0, 0x13, 0x48, 0x0, 0x12, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 190}, //queen 191{0x0, 0x18, 0x0, 0xF0, 0x24, 0xF, 0x8, 0xA5, 0x10, 0x4, 0x66, 0x20, 0x2, 0x24, 0x40, 0x1, 0x24, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 1920x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x40, 0x2, 0x18, 0x20, 0x4, 0x18, 0x20, 1930xE8, 0xFF, 0x17, 0x10, 0x0, 0x8, 0x10, 0x0, 0x8, 0x20, 0x0, 0x4, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 194} //king 195}; 196 197 198const uint8_t issound[2][32] PROGMEM={ 199{0x0, 0x0, 0x0, 0x0, 0x0, 0x32, 0x0, 0x54, 0x0, 0x98, 0x1F, 0x10, 0x10, 0x30, 0x10, 0x50, 2000x10, 0x90, 0x11, 0x10, 0x1F, 0x10, 0x4, 0x90, 0x8, 0x50, 0x10, 0x30, 0x0, 0x0, 0x0, 0x0}, 201{0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x50, 0x0, 0x90, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 2020x10, 0x10, 0x10, 0x10, 0x1F, 0x10, 0x0, 0x90, 0x0, 0x50, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0} 203}; 204const uint8_t iswb[2][32] PROGMEM={ 205{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 2060x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x0, 0x0}, 207{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 2080xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0} 209}; 210const uint8_t iscm[2][32] PROGMEM={ 211{0x0, 0x0, 0x7, 0xE0, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0x0, 0x30, 0x0, 0x60, 2120x0, 0xC0, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0}, 213{0x0, 0x0, 0x3F, 0x0, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x1, 0x80, 0x3, 0x0, 2140x6, 0x24, 0xC, 0x24, 0xC, 0xFF, 0xC, 0x24, 0x0, 0xFF, 0xC, 0x24, 0xC, 0x24, 0x0, 0x0} 215}; 216const uint8_t islimit[8][32] PROGMEM={ 217{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 2180x3, 0xC0, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 219{0x0, 0x0, 0x4, 0x70, 0xC, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 2200xE, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 221{0x0, 0x0, 0xC, 0x70, 0x12, 0x88, 0x2, 0x88, 0xC, 0x88, 0x2, 0x88, 0x2, 0x88, 0x12, 0x88, 2220xC, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 223{0x0, 0x0, 0x1, 0x80, 0x3, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 2240x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 225{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 2260x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 227{0x0, 0x0, 0xC, 0x78, 0x1C, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 2280x1E, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 229{0x0, 0x0, 0x1C, 0x78, 0x36, 0xCC, 0x6, 0xCC, 0x1C, 0xCC, 0x6, 0xCC, 0x36, 0xCC, 0x36, 0xCC, 2300x1C, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 231{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3C, 0x3C, 0x7E, 0x7E, 0x63, 0xC6, 0x61, 0x86, 2320x63, 0xC6, 0x66, 0x66, 0x3C, 0x3C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 233}; 234const uint8_t isdesc[2][32] PROGMEM={ 235{0x0, 0x0, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 2360xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E}, 237{0x0, 0x0, 0x0, 0x4, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x30, 2380x30, 0x70, 0x78, 0xE0, 0x3C, 0xC0, 0x1F, 0xC0, 0xF, 0x80, 0x7, 0x0, 0x2, 0x0, 0x0, 0x0} 239 240}; 241const uint8_t isback[32] PROGMEM={ 2420x0, 0x0, 0x0, 0x0, 0x20, 0xF0, 0x31, 0xF8, 0x3B, 0xFC, 0x3F, 0x8E, 0x3F, 0xE, 0x3F, 0xE, 2430x3F, 0x8E, 0x3F, 0xCE, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x70, 0x0, 0x0 244}; 245const uint8_t issave[32] PROGMEM={ 2460x1, 0x80, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 2470x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 248}; 249const uint8_t isload[32] PROGMEM={ 2500x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 2510x23, 0xC4, 0x27, 0xE4, 0x27, 0xE4, 0x23, 0xC4, 0x21, 0x84, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 252}; 253const uint8_t isempty[32] PROGMEM={ 2540xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 2550x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF 256}; 257const uint8_t isinit[32] PROGMEM={ 2580xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 2590x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 260}; 261const uint8_t isplay[32] PROGMEM={ 2620x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x1, 0x80, 0x23, 0xF8, 0x17, 0xC8, 0xB, 0xC8, 2630x3, 0xC0, 0x3, 0xC0, 0x2, 0x40, 0x4, 0x40, 0x4, 0x38, 0x4, 0x8, 0xC, 0x0, 0x0, 0x0 264}; 265const uint8_t isrotate[2][32] PROGMEM={ 266{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 2670x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0}, 268{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 2690x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0} 270}; 271 272const int pos[7][8][8] PROGMEM={ 273 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 274 {100,100,100,100,100,100,100,100}, //{ 50, 50, 50, 50, 50, 50, 50, 50}, 275 { 20, 30, 40, 50, 50, 40, 30, 20}, //{ 10, 10, 20, 30, 30, 20, 10, 10}, 276 { 5, 5, 10, 25, 25, 10, 5, 5}, 277 { 0, 0, 0, 20, 20, 0, 0, 0}, 278 { 5, -5,-10, 0, 0,-10, -5, 5}, 279 { 5, 10, 10,-20,-20, 10, 10, 5}, //{ 5, 10, 10,-20,-20, 10, 10, 5}, 280 { 0, 0, 0, 0, 0, 0, 0, 0}}, 281 282 {{-50,-40,-30,-30,-30,-30,-40,-50}, // 283 {-40,-20, 0, 0, 0, 0,-20,-40}, 284 {-30, 0, 10, 15, 15, 10, 0,-30}, 285 {-30, 5, 15, 20, 20, 15, 5,-30}, 286 {-30, 0, 15, 20, 20, 15, 0,-30}, 287 {-30, 5, 10, 15, 15, 10, 5,-30}, 288 {-40,-20, 0, 5, 5, 0,-20,-40}, 289 {-50,-40,-30,-30,-30,-30,-40,-50}}, 290 291 {{-20,-10,-10,-10,-10,-10,-10,-20}, // 292 {-10, 0, 0, 0, 0, 0, 0,-10}, 293 {-10, 0, 5, 10, 10, 5, 0,-10}, 294 {-10, 5, 5, 10, 10, 5, 5,-10}, 295 {-10, 0, 10, 10, 10, 10, 0,-10}, 296 {-10, 10, 10, 10, 10, 10, 10,-10}, 297 {-10, 5, 0, 0, 0, 0, 5,-10}, 298 {-20,-10,-10,-10,-10,-10,-10,-20}}, 299 300 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 301 { 5, 10, 10, 10, 10, 10, 10, 5}, 302 { -5, 0, 0, 0, 0, 0, 0, -5}, 303 { -5, 0, 0, 0, 0, 0, 0, -5}, 304 { -5, 0, 0, 0, 0, 0, 0, -5}, 305 { -5, 0, 0, 0, 0, 0, 0, -5}, 306 { -5, 0, 0, 0, 0, 0, 0, -5}, 307 { 0, 0, 0, 5, 5, 0, 0, 0}}, 308 309 {{-20,-10,-10, -5, -5,-10,-10,-20}, // 310 {-10, 0, 0, 0, 0, 0, 0,-10}, 311 {-10, 0, 5, 5, 5, 5, 0,-10}, 312 { -5, 0, 5, 5, 5, 5, 0, -5}, 313 { 0, 0, 5, 5, 5, 5, 0, -5}, 314 {-10, 5, 5, 5, 5, 5, 0,-10}, 315 {-10, 0, 5, 0, 0, 0, 0,-10}, 316 {-20,-10,-10, -5, -5,-10,-10,-20}}, 317 318 {{-30,-40,-40,-50,-50,-40,-40,-30}, // 319 {-30,-40,-40,-50,-50,-40,-40,-30}, 320 {-30,-40,-40,-50,-50,-40,-40,-30}, 321 {-30,-40,-40,-50,-50,-40,-40,-30}, 322 {-20,-30,-30,-40,-40,-30,-30,-20}, 323 {-10,-20,-20,-20,-20,-20,-20,-10}, 324 { 10, 10,-10,-10,-10,-10, 10, 10}, //{ 20, 20, 0, 0, 0, 0, 20, 20}, 325 { 10, 40, 30, 0, 0, 0, 50, 10}}, //{ 20, 30, 10, 0, 0, 10, 30, 20}}, 326 327 {{-50,-40,-30,-20,-20,-30,-40,-50}, // 328 {-30,-20,-10, 0, 0,-10,-20,-30}, 329 {-30,-10, 20, 30, 30, 20,-10,-30}, 330 {-30,-10, 30, 40, 40, 30,-10,-30}, 331 {-30,-10, 30, 40, 40, 30,-10,-30}, 332 {-30,-10, 20, 30, 30, 20,-10,-30}, 333 {-30,-30, 0, 0, 0, 0,-30,-30}, 334 {-50,-30,-30,-30,-30,-30,-30,-50}} 335}; 336 337volatile static int32_t toggles; // number of ups/downs in a tone burst 338 339void tone(int toneFreq,int32_t duration){ // duration in ms 340 const uint32_t rcVal = VARIANT_MCK/256/toneFreq; // target value for counter, before it resets (= 82 for 4kHz) 341 toggles = 2*toneFreq*duration/1000; // calculate no of waveform edges (rises/falls) for the tone burst 342 setupTC(TC1,0,TC3_IRQn,toneFreq); // Start Timer/Counter 1, channel 0, interrupt, frequency 343 } 344 345void setupTC(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t freq){ 346 pmc_set_writeprotect(false); // disable write protection of timer registers 347 pmc_enable_periph_clk((uint32_t) irq); // enable clock / interrupt 348 TC_Configure(tc, channel, 349 TC_CMR_TCCLKS_TIMER_CLOCK4 | // TIMER_CLOCK4: MCK/128=656,250Hz. 16 bits so 656,250/65,536=~10Hz/bit 350 TC_CMR_WAVE | // Waveform mode 351 TC_CMR_WAVSEL_UP_RC ); // Counter running up and reset when = Register C value (rcVal) 352 const uint32_t rcVal = VARIANT_MCK/256/freq; // target value for counter, before it resets 353 TC_SetRC(tc, channel, rcVal); 354 TC_Start(tc, channel); 355 (*tc).TC_CHANNEL[channel].TC_IER = TC_IER_CPCS; // IER: CPCS bit enables RC compare interrupt when set 356 (*tc).TC_CHANNEL[channel].TC_IDR = ~TC_IER_CPCS; // IDR: clear CPCS bit = don't disable RC compare interrupt 357 NVIC_EnableIRQ(irq); // Enable TC3_IRQn in the Nested Vector Interrupt Controller) 358 } 359 360void TC3_Handler(void){ // timer ISR TC1 ch 0 361 TC_GetStatus(TC1,0); 362 if (toggles != 0){ 363 digitalWrite(PinBuzz,!digitalRead(PinBuzz)); // invert the pin state (i.e toggle) 364 if (toggles > 0) toggles--; 365 } 366 } 367 368 void noTone(){ 369 TC_Stop(TC1,0); // stop timer 370 digitalWrite(PinBuzz,LOW); // no signal on pin 371 } 372 373//****** 374class Button { 375public: 376int bx,by,bw,bh,bshift; 377String bs; 378const uint8_t* bp; 379 Button(int x,int y,int w,int h,String s,const uint8_t* p, int shift); 380 void Show(int shift); 381 void Show(String s); 382 void Hide(); 383 boolean IsPressed(int xc, int yc); 384private: 385boolean active; 386 void ishow(uint16_t textcolor,uint16_t backcolor); 387}; 388Button::Button(int x,int y,int w,int h,String s,const uint8_t* p=NULL, int shift=0) { 389 bx=x; by=y; bw=w; bh=h; bs=s; bp=p; bshift=shift; 390}; 391void Button::Show(int shift=0) { 392 bshift=shift; 393 active=true; 394 ishow(WHITE,DARK); 395}; 396void Button::Show(String s) { 397 bs=s; 398 active=true; 399 ishow(WHITE,DARK); 400}; 401void Button::Hide() { 402 active=false; 403 ishow(GRAY,DARK); 404}; 405boolean Button::IsPressed(int xc, int yc) { 406 if (active&&abs(bx+bw/2-xc)<bw/2&&abs(by+bh/2-yc)<bh/2) { 407 ishow(WHITE,GRAY); 408 touchx=0; touchy=0; 409 return true; 410 } 411 return false; 412}; 413void Button::ishow(uint16_t textcolor,uint16_t backcolor) { 414 tft.fillRoundRect(bx+2,by+2,bw,bh,3,GRAY2); 415 tft.fillRoundRect(bx,by,bw,bh,3,backcolor); 416 tft.setTextColor(textcolor); 417 tft.setTextSize(1); 418 tft.setCursor(bx+(bw-bs.length()*6)/2,by+(bh-8)/2); 419 if (bp!=NULL) { 420 tft.drawBitmap(1+bx+(bw-16)/2, by+(bh-16)/2, bp+bshift*32, 16, 16,textcolor); 421 } 422 else tft.print(bs); 423} 424Button BAction(320, 285, 70, 30, "START"); 425Button BBW(402, 285, 30, 30, "",&iswb[0][0]); 426Button BBack(444, 285, 30, 30, "",&isback[0]); 427Button BInit(444, 285, 30, 30, "",&isinit[0]); 428Button BLimit(320, 245, 26, 30, "",&islimit[0][0]); 429Button BEmpty(320, 245, 26, 30, "",&isempty[0]); 430Button BRotate(352, 245, 26, 30, "",&isrotate[0][0]); 431Button BPlay(352, 245, 26, 30, "",&isplay[0]); 432Button BCM(384, 245, 26, 30, "",&iscm[0][0]); 433Button BSave(384, 245, 26, 30, "",&issave[0]); 434Button BDesc(416, 245, 26, 30, "",&isdesc[0][0]); 435Button BSound(448, 245, 26, 30, "",&issound[0][0]); 436Button BLoad(448, 245, 26, 30, "",&isload[0]); 437 438union byte2_t { 439 uint8_t b[2]; 440 struct { 441 short s; 442 }; 443 }; 444 445byte2_t byte2; 446//********************************** 447void drawBitmap(int16_t x, int16_t y, 448 const uint8_t *bitmap, int16_t w, int16_t h, 449 uint16_t color) { 450 int16_t i, j, byteWidth = (w + 7) / 8; 451 //if (color!=pixelfg) tft.fgColor(color); 452 for(j=0; j<h; j++) { 453 for(i=0; i<w; i++ ) { 454 if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (1 << (i % 8))) { 455 tft.drawPixel(x+i, y+j, color); 456 } 457 } 458 } 459} 460//**************************** 461void deletebuttons() { 462 tft.fillRect(320,245,160,75,BLACK); 463} 464 465//**************************** 466void clearstatus() { 467 tft.fillRect(320,225,160,20,BLACK); 468 tft.drawFastHLine(10,312,300,BLACK); 469 tft.drawFastHLine(10,315,300,BLACK); 470} 471//**************************** 472void setup() { 473 Serial.begin(57600); 474 Serial.println(F("Start")); 475 pinMode(13, OUTPUT); 476 tft.init(); 477 tft.setRotation(1); 478 myTouch.InitTouch(); 479 definecolors(); 480 beep(100); 481 for (int i=0;i<MAXSTEPS;i++) { 482 steps[i].x1=0; steps[i].y1=0; 483 steps[i].x2=0; steps[i].y2=0; 484 steps[i].fig1=0; steps[i].fig2=0; 485 steps[i].check=0; 486 steps[i].type=0; 487 steps[i].weight=0; 488 } 489 initboard(); 490} 491//**************************** 492void initboard() { 493 for (int i=0;i<8;i++) 494 for (int j=0;j<8;j++) poledisp[j][i]=-100; // 495 tft.fillScreen(BLACK); 496 tft.setTextColor(GRAY); 497 tft.setTextSize(1); 498 for (int j=1;j<9;j++) { 499 tft.setCursor(2,j*37-20); 500 if (rotate) tft.print(j); else tft.print(9-j); 501 } 502 for (byte i=1;i<9;i++) { 503 tft.setCursor(i*37-8,300); 504 if (rotate) tft.print(char(96+9-i)); else tft.print(char(96+i)); 505 } 506 initscreen(); 507} 508//**************************** 509void initscreen() { 510 show_board(); 511 show_steps(); 512 BAction.Show("START"); 513 BBW.Show(cur_step%2); 514 if (cur_step>1) BBack.Show(); else BBack.Hide(); 515 BSave.Hide(); 516 BLoad.Hide(); 517 BInit.Hide(); 518 BBack.Show(); 519 BLimit.Show(); 520 BPlay.Hide(); 521 BRotate.Show(rotate); 522 BEmpty.Hide(); 523 BLimit.Show(limit); 524 BCM.Show(checkmatesearch); 525 BDesc.Show(choice); 526 BSound.Show(sound); 527} 528//**************************** 529void loop() { 530 gui(); 531 if (solving) { 532 int len=70; 533 if (!sound) len=2; 534 for (int i=1;i<4;i++) { 535 tone(400+300*i,len); 536 delay(70); } 537 lastscore=solve_step(); 538 float tim=float(millis()-starttime)/1000; 539 for (int i=3;i>=1;i--) { 540 tone(400+300*i,len); 541 delay(70); } 542 if (!checkmatesearch&&lastscore>-9000&&lastscore!=8999) { 543 movestep(cur_step); 544 cur_step++; steps[cur_step].fig1=0; 545 } 546 initscreen(); 547 animate_step(cur_step-1,false); 548 clearstatus(); 549 if (lastscore>9000) { 550 tft.setTextSize(1); 551 tft.setTextColor(GREEN); 552 tft.setCursor(322,230); 553 if (lastscore<9999) { 554 tft.print("# in "); 555 tft.print((9999-lastscore)/2+1); tft.print(F(" st")); 556 } else tft.print("Checkmate"); 557 } else if (lastscore<-9000) { 558 tft.setTextSize(1); 559 tft.setTextColor(RED); 560 tft.setCursor(322,230); 561 tft.print("GIVE UP!"); 562 show_steps(); 563 } else if (lastscore==8999) { //Draw 564 tft.setTextSize(1); 565 tft.setTextColor(YELLOW); 566 tft.setCursor(322,230); 567 tft.print("Draw"); 568 } 569 tft.setTextSize(1); 570 tft.setTextColor(GRAY); 571 tft.setCursor(322,230); 572 tft.print(tim,0); 573 tft.print("s "); 574 if (abs(lastscore)<5000) { 575 tft.print("="); 576 tft.print(lastscore); 577 } 578 tft.setCursor(390,230); 579 tft.print(count); 580 tft.print(" pos"); 581 Serial.print(F("Positions estimated=")); Serial.println(count); 582 Serial.print(F("Time=")); Serial.print(tim,1); Serial.println("s"); 583 } 584 delay(10); 585 586 587} 588 589//********************************** 590void load_choice() { 591 for (int i=0;i<7;i++) 592 if (cur_step%2==1) polechoice[i]=i; else polechoice[i]=-i; 593} 594//********************************** 595void show_choice() { 596uint16_t color,color_cont; 597 for (int i=0;i<7;i++) { 598 color=BLACKF; 599 if (i%2==0) color=WHITEF; 600 tft.fillRect(416,i*25+35,25,25,color); 601 color=DARK; 602 if (i==cur_choice) color=WHITE; 603 tft.drawRect(416,i*25+35,25,25,color); 604 color=BLACK; color_cont=GRAY; 605 if (polechoice[i]>0) { color=WHITE; color_cont=BLACK; } 606 if (polechoice[i]!=0) { 607 tft.drawBitmap(420, i*25+39,&fig[abs(polechoice[i])-1][0], 16, 16,color); 608 tft.drawBitmap(420, i*25+39,&fig_cont[abs(polechoice[i])-1][0], 16, 16,color_cont); 609 } 610 } 611} 612//********************************** 613void animate_step(int nstep, boolean hide) { 614 if (!hide&&nstep-1>0&&steps[nstep-1].fig1!=0) animate_step(nstep-1,true); 615 if (nstep-1<1||steps[nstep].fig1==0) return; 616 int j=steps[nstep].x1; 617 int dj=steps[nstep].x2-steps[nstep].x1; 618 int i=steps[nstep].y1; 619 int di=steps[nstep].y2-steps[nstep].y1; 620 di=di/abs(di); dj=dj/abs(dj); 621 if (hide) show_fig(i,j); 622 while (j!=steps[nstep].x2||i!=steps[nstep].y2) { 623 show_fig(i,j); 624 if (!hide) 625 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 626 else tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 627 int mj=0; 628 if (j!=steps[nstep].x2) { j+=dj; mj=1; } 629 if (abs(steps[nstep].fig1)==fn&&mj==1) continue; 630 if (i!=steps[nstep].y2) i+=di; 631 } 632 show_fig(i,j); 633 if (!hide) 634 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 635 else tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 636} 637//********************************** 638boolean load_usb() { 639signed char s='x',i=0,j=0; boolean load=false; 640 Serial.println(F("Wait for FEN position")); 641 for (int i=0;i<8;i++) 642 for (int j=0;j<8;j++) { 643 pole0[j][i]=pole[j][i]; 644 pole[j][i]=(signed char)pgm_read_byte(&polezero[j][i]); 645 } 646 while (s!=' ') { 647 s=Serial.read(); 648 if (i>7) { i=0; j++; } 649 if (!getpole(j,i)) break; 650 switch (s) { 651 case '/': i=0; break; 652 case 'p': pole[j][i]=-fp; i++; break; 653 case 'P': pole[j][i]=fp; i++; break; 654 case 'n': pole[j][i]=-fn; i++; break; 655 case 'N': pole[j][i]=fn; i++; break; 656 case 'b': pole[j][i]=-fb; i++; break; 657 case 'B': pole[j][i]=fb; i++; break; 658 case 'r': pole[j][i]=-fr; i++; break; 659 case 'R': pole[j][i]=fr; i++; break; 660 case 'q': pole[j][i]=-fq; i++; break; 661 case 'Q': pole[j][i]=fq; i++; break; 662 case 'k': pole[j][i]=-fk; i++; break; 663 case 'K': pole[j][i]=fk; i++; break; 664 case '1': i++; break; 665 case '2': i+=2; break; 666 case '3': i+=3; break; 667 case '4': i+=4; break; 668 case '5': i+=5; break; 669 case '6': i+=6; break; 670 case '7': i+=7; break; 671 case '8': i=0; j++; break; 672 case ' ': break; 673 } 674 delay(20); 675 if (i+j>0&&Serial.available()==0) break; 676 } 677 s=0; 678 if (Serial.available()>0) s=Serial.read(); 679 while (Serial.available()>0) Serial.read(); 680 if (s=='w'||s==0) { cur_step=1; load=true; } 681 else if (s=='b') { cur_step=2; load=true; } 682 else load=false; 683 if (load) { 684 steps[1].fig1=0; steps[2].fig1=0; 685 Serial.println(F("Position loaded")); 686 } else { 687 for (int i=0;i<8;i++) 688 for (int j=0;j<8;j++) 689 pole[j][i]=pole0[j][i]; 690 } 691 return load; 692} 693//********************************** 694void play() { 695 BAction.Show("STOP"); 696 BSave.Hide(); 697 BBW.Hide(); 698 BLoad.Hide(); 699 BInit.Hide(); 700 BPlay.Hide(); 701 BEmpty.Hide(); 702 BDesc.Hide(); 703 clearstatus(); 704 for (int i=cur_step-1;i>0;i--) { 705 if (steps[i].fig1!=0) backstep(i); 706 } 707 show_board(); 708 show_steps(); 709 boolean st=false; 710 unsigned long tim=millis(); 711 int i=1; 712 while (i<cur_step) { 713 if (steps[i].fig1!=0) movestep(i); 714 myTouch.calibrateRead(); 715 int x=map(myTouch.TP_X,3850,250,0,479); 716 int y=map(myTouch.TP_Y,280,3850,0,319); 717 if (myTouch.TP_X!=0&&myTouch.TP_Y!=4095) 718 if (BAction.IsPressed(x,y)) { st=true; BAction.Show(); } 719 if (millis()-tim>2000||st) { 720 tim=millis(); 721 i++; 722 show_board(); 723 beep(20); 724 } 725 } 726 choice=0; 727 beep(200); 728 kingpositions(); 729 clearstatus(); 730 initscreen(); 731} 732//********************************** 733void gui() { 734 myTouch.calibrateRead(); 735 int x=map(myTouch.TP_X,3850,250,0,479); 736 int y=map(myTouch.TP_Y,280,3850,0,319); 737 if (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&myTouch.TP_X!=0&&myTouch.TP_Y!=4095) { 738 // tft.drawPixel(x,y,CYAN); 739 // Serial.println(x); 740 // Serial.println(y); 741 touchx=x; 742 touchy=y; 743 if (solving) { 744 if (BAction.IsPressed(x,y)) { // 745 beep(100); 746 solving=false; 747 BAction.Show("stopping"); 748 BAction.Hide(); 749 return; 750 } 751 } else { //not solve 752 if (BAction.IsPressed(x,y)) { // 753 if (!choice) { 754 delay(100); 755 solving=true; 756 BAction.Show(); 757 animate_step(cur_step-1,true); 758 return; 759 } else { // USB 760 delay(100); 761 BAction.Hide(); 762 BSave.Hide(); 763 BBW.Hide(); 764 BLoad.Hide(); 765 BInit.Hide(); 766 BPlay.Hide(); 767 BEmpty.Hide(); 768 BDesc.Hide(); 769 show_steps(); 770 choice=0; 771 if (!load_usb()) return; 772 beep(200); 773 kingpositions(); 774 clearstatus(); 775 initscreen(); 776 } 777 } else if (BBW.IsPressed(x,y)) { //- 778 beep(100); 779 if (cur_step==1) cur_step=2; 780 else if (cur_step==2) cur_step=1; 781 else if (cur_step%2==0) cur_step--; else cur_step++; 782 BBW.Show(cur_step%2); 783 if (choice) { load_choice(); show_choice(); } 784 return; 785 } else if (cur_step>1&&BBack.IsPressed(x,y)) { // 786 beep(100); 787 cur_step--; 788 animate_step(cur_step,true); 789 lastscore=0; 790 backstep(cur_step); 791 steps[cur_step].fig1=0; 792 show_board(); 793 show_steps(); 794 if (cur_step>1) BBack.Show(); else BBack.Hide(); 795 BBW.Show(cur_step%2); 796 clearstatus(); 797 return; 798 } else if (BLimit.IsPressed(x,y)) { // 799 beep(100); 800 limit++; if (limit>7) limit=0; 801 BLimit.Show(limit); 802 return; 803 } else if (BCM.IsPressed(x,y)) { // 804 beep(100); 805 checkmatesearch=!checkmatesearch; 806 BCM.Show(checkmatesearch); 807 if (!checkmatesearch) steps[cur_step].fig1=0; 808 lastscore=0; 809 show_steps(); 810 return; 811 } else if (BRotate.IsPressed(x,y)) { // 812 beep(100); 813 rotate=!rotate; 814 BRotate.Show(rotate); 815 initboard(); 816 return; 817 } else if (BSound.IsPressed(x,y)) { // 818 if (sound==1) { beep(100); sound=0; } else { sound=1; beep(200); } 819 BSound.Show(sound); 820 return; 821 } else if (BDesc.IsPressed(x,y)) { // 822 beep(100); 823 if (!choice) { 824 choice=1; 825 cur_choice=1; 826 load_choice(); 827 show_choice(); 828 BAction.Show("USB(FEN)"); 829 BBack.Hide(); 830 BInit.Show(); 831 BSave.Show(); 832 BLimit.Hide(); 833 BEmpty.Show(); 834 BRotate.Hide(); 835 BPlay.Show(); 836 BCM.Hide(); 837 BSound.Hide(); 838 BSave.Show(); 839 BLoad.Show(); 840 } else { 841 choice=0; 842 lastscore=0; 843 clearstatus(); 844 kingpositions(); 845 initscreen(); 846 } 847 BDesc.Show(choice); 848 return; 849 } else if (BInit.IsPressed(x,y)) { // 850 beep(200); 851 cur_step=1; 852 steps[1].fig1=0; 853 for (int i=0;i<8;i++) 854 for (int j=0;j<8;j++) pole[j][i]=(signed char)pgm_read_byte(&polestart[j][i]); 855 choice=0; 856 kingpositions(); 857 clearstatus(); 858 initscreen(); 859 } else if (BEmpty.IsPressed(x,y)) { // 860 beep(200); 861 cur_step=1; 862 clearstatus(); 863 steps[1].fig1=0; steps[2].fig1=0; 864 for (int i=0;i<8;i++) 865 for (int j=0;j<8;j++) pole[j][i]=(signed char)pgm_read_byte(&polezero[j][i]); 866 BEmpty.Show(); 867 show_board(); 868 } else if (BSave.IsPressed(x,y)) { 869 beep(100); 870 for (int i=0;i<8;i++) 871 for (int j=0;j<8;j++) dueFlashStorage.write(i*8+j,pole[j][i]); 872 byte2.s=cur_step; 873 dueFlashStorage.write(65,byte2.b[0]); 874 dueFlashStorage.write(64,byte2.b[1]); 875 byte2.s=limit; 876 dueFlashStorage.write(67,byte2.b[0]); 877 dueFlashStorage.write(66,byte2.b[1]); 878 step_type st; 879 for (int i=1;i<cur_step;i++) { 880 st=steps[i]; 881 dueFlashStorage.write(68+i*sizeof(step_type), (byte *)&st, sizeof(step_type)); 882 } 883 BSave.Show(); 884 } else if (BLoad.IsPressed(x,y)) { 885 beep(200); 886 for (int i=0;i<8;i++) 887 for (int j=0;j<8;j++) pole[j][i]=dueFlashStorage.read(i*8+j); 888 byte2.b[0]=dueFlashStorage.read(65); 889 byte2.b[1]=dueFlashStorage.read(64); 890 cur_step=byte2.s; 891 if (cur_step<0||cur_step>400) cur_step=1; 892 byte2.b[0]=dueFlashStorage.read(67); 893 byte2.b[1]=dueFlashStorage.read(66); 894 limit=byte2.s; 895 if (limit<0||limit>7) limit=0; 896 step_type st; 897 for (int i=1;i<cur_step;i++) { 898 memcpy(&st, dueFlashStorage.readAddress(68+i*sizeof(step_type)), sizeof(step_type)); 899 steps[i]=st; 900 } 901 steps[0].fig1=0; 902 steps[cur_step].fig1=0; 903 choice=0; 904 kingpositions(); 905 clearstatus(); 906 initscreen(); 907 } else if (BPlay.IsPressed(x,y)) { // 908 beep(100); 909 BPlay.Show(); 910 play(); 911 return; 912 } 913 if (!choice&&ntouch==0) { 914 touchx0=touchx; 915 touchy0=touchy; 916 signed char x1=touchy0/37; 917 signed char y1=(touchx0-10)/37; 918 if (rotate) { x1=7-x1; y1=7-y1; } 919 if (getpole(y1,x1)&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 920 signed char p=pole[x1][y1]; 921 movedfig=p; 922 drag=true; 923 pole[x1][y1]=0; 924 show_fig(y1,x1); 925 pole[x1][y1]=p; 926 } else drag=false; 927 } 928 ntouch++; 929 if (drag&&ntouch>1) { 930 uint16_t buf[256]; 931 int x1=touchy0/37; 932 int y1=(touchx0-10)/37; 933 if (rotate) { x1=7-x1; y1=7-y1; } 934 if (abs(pole[x1][y1])>0&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 935 int x2=touchy/37; 936 int y2=(touchx-10)/37; 937 if (getpole(x2,y2)) { 938 signed char p=pole[x2][y2]; 939 pole[x2][y2]=movedfig; 940 show_fig(y2,x2); 941 delay(30); 942 pole[x2][y2]=p; 943 show_fig(y2,x2); 944 if (x2==x1&&y2==y1) { 945 pole[x2][y2]=0; 946 show_fig(y2,x2); 947 pole[x2][y2]=p; 948 } 949 } 950 // for (int i=0;i<16;i++) 951 // for (int j=0;j<16;j++) buf[i*16+j]=tft.readPixel(touchx-8+i,touchy-8+j); 952 //buf[0]=tft.readPixel(touchx-8,touchy-8); 953 // uint16_t color,color_cont; 954 // color=BLACK; color_cont=GRAY; 955 // if (pole[x1][y1]>0) { color=WHITE; color_cont=BLACK; } 956 // tft.drawBitmap(touchx-8, touchy-8,&fig[abs(pole[x1][y1])-1][0], 16, 16,color); 957 // tft.drawBitmap(touchx-8, touchy-8,&fig_cont[abs(pole[x1][y1])-1][0], 16, 16,color_cont); 958 // delay(30); 959 // for (int i=0;i<16;i++) 960 // for (int j=0;j<16;j++) tft.drawPixel(touchx-8+i,touchy-8+j,buf[i*16+j]); 961 } 962 } else if (choice) { 963 int x1=touchy/37; 964 int y1=(touchx-10)/37; 965 if (rotate) { x1=7-x1; y1=7-y1; } 966 if (touchy+touchx!=0&&choice&&getpole(y1,x1)) { 967 pole[x1][y1]=polechoice[cur_choice]; 968 show_board(); 969 } 970 if (touchx>=416&&touchx<=441&&touchy>=35&&touchy<=210) { 971 cur_choice=(touchy-35)/25; 972 show_choice(); 973 } 974 } 975 lasttouch=millis(); 976 } //not solve 977 if (!drag) { beep(10); ntouch=0; animate_step(cur_step-1,true); } 978 } else { //touching 979 if (!solving&&!choice&&drag!=0&&ntouch>0&&(myTouch.TP_X==0||myTouch.TP_Y==4095)&&millis()-lasttouch>300) { //drag 980 signed char x1=touchy0/37; 981 signed char y1=(touchx0-10)/37; 982 signed char x2=touchy/37; 983 signed char y2=(touchx-10)/37; 984 if (rotate) { x1=7-x1; y1=7-y1; x2=7-x2; y2=7-y2; } 985 if (getpole(y1,x1)) show_fig(y1,x1); 986 if (getpole(y1,x1)&&getpole(y2,x2)) { 987 start_var=cur_step+1; 988 only_action=false; 989 load_variants(cur_step); 990 for (int i=cur_step+1;i<cur_step+cur_var+1;i++) 991 if (steps[i].x1==x1&&steps[i].y1==y1&&steps[i].x2==x2&&steps[i].y2==y2) { 992 steps[cur_step]=steps[i]; 993 movestep(cur_step); cur_step++; steps[cur_step].fig1=0; 994 int len=100; 995 if (!sound) len=2; 996 for (int i=2;i>=1;i--) { 997 tone(400+300*i,len); 998 delay(100); } 999 initscreen(); 1000 animate_step(cur_step-1,false); 1001 break; 1002 } 1003 } else { 1004 beep(10); 1005 } 1006 ntouch=0; 1007 drag=false; 1008 } 1009 } 1010 if (millis()-quitime>1000) { 1011 if (isstatus) show_status(); 1012 quitime=millis(); 1013 } 1014 1015 1016} 1017//********************************** 1018void show_status() { 1019 int tim=(millis()-starttime)/1000; 1020 int cur=300000*tim/(limittime-starttime); 1021 if (cur>300) { cur=300; solving=false; } 1022 tft.drawFastHLine(10,312,cur,GRAY); 1023 tft.drawFastHLine(10,315,progress*2,GRAY); 1024 int m=tim/60; 1025 int s=tim%60; 1026 tft.setTextWrap(1); 1027 tft.setTextColor(GRAY); 1028 tft.setTextSize(1); 1029 tft.fillRect(320,225,50,20,BLACK); 1030 tft.setCursor(322,230); 1031 if (m>0) { tft.print(m); tft.print(":"); } 1032 if (s>0) { 1033 if (s<10&&m>0) tft.print("0"); 1034 tft.print(s); 1035 } else if (m>0) tft.print("00"); else tft.print("0"); 1036 tft.setCursor(350,230); 1037 tft.print(cur_level); 1038 1039 tft.setCursor(370,230); 1040 if (cur_step%2==1) tft.setTextColor(WHITE); else tft.setTextColor(GRAY); 1041 if (lastbest.fig1!=steps[0].fig1||lastbest.x1!=steps[0].x1||lastbest.y1!=steps[0].y1|| 1042 lastbest.x2!=steps[0].x2||lastbest.y2!=steps[0].y2) { 1043 beep(10); 1044 lastbest=steps[0]; 1045 tft.fillRect(370,230,50,10,BLACK); 1046 tft.setCursor(370,230); 1047 tft.print(str_step(0)); 1048 blinkstep=0; 1049 } 1050 if (steps[0].fig1!=0&&blinkstep==0&&tim>5) { 1051 signed char poleb[8][8]; 1052 for (int i=0;i<8;i++) 1053 for (int j=0;j<8;j++) { poleb[j][i]=pole[j][i]; pole[j][i]=pole0[j][i]; } 1054 movestep(0); 1055 show_board(); 1056 delay(100); 1057 backstep(0); 1058 show_board(); 1059 for (int i=0;i<8;i++) 1060 for (int j=0;j<8;j++) pole[j][i]=poleb[j][i]; 1061 kingpositions(); 1062 } 1063 blinkstep++; 1064 if (blinkstep>2) blinkstep=0; 1065} 1066//**************************** 1067void beep(int leng) { 1068 analogWrite(PinBuzz, 20); 1069 delay(2); 1070 if (!sound) analogWrite(PinBuzz, 0); 1071 delay(leng); 1072 analogWrite(PinBuzz, 0); 1073} 1074//**************************** 1075void definecolors() { 1076 BLACK =0x0000; 1077 BLUE =0x07FF; //0x001F; 1078 RED =0xF800; 1079 GREEN =0x07E0; 1080 CYAN =0x07FF; 1081 MAGENTA=0xF81F; 1082 YELLOW =0xFFE0; 1083 WHITE =0xFFFF; 1084 GRAY =0x7BEF; 1085 DARK =getColor(32,32,32); 1086 GRAY2 =getColor(16,16,16); 1087 BLACKF =getColor(94,58,0); 1088 WHITEF =getColor(180,114,0); 1089} 1090//**************************** 1091uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) 1092{ 1093 red >>= 3; 1094 green >>= 2; 1095 blue >>= 3; 1096 return (red << 11) | (green << 5) | blue; 1097} 1098//**************************** 1099void show_fig(int i,int j) { 1100uint16_t color,color_cont; 1101 color=BLACKF; 1102 if ((i+j+2)%2==0) color=WHITEF; 1103 int jj=j, ii=i; 1104 if (rotate) { jj=7-j; ii=7-i; } 1105 tft.fillRect(ii*37+12,jj*37,38,38,color); 1106 tft.drawRect(ii*37+12,jj*37,38,38,WHITE); 1107 color=BLACK; color_cont=GRAY; 1108 if (pole[j][i]>0) { color=WHITE; color_cont=BLACK; } 1109 if (pole[j][i]!=0) { 1110 drawBitmap(ii*37+19, jj*37+7,&fig24[abs(pole[j][i])-1][0], 24, 24,color); 1111 drawBitmap(ii*37+19, jj*37+7,&fig24_cont[abs(pole[j][i])-1][0], 24, 24,color_cont); 1112 //drawBitmap(ii*37+24, jj*37+12,&fig[abs(pole[j][i])-1][0], 16, 16,color); 1113 //drawBitmap(ii*37+24, jj*37+12,&fig_cont[abs(pole[j][i])-1][0], 16, 16,color_cont); 1114 } 1115} 1116//**************************** 1117void show_board() { 1118 for (int i=0;i<8;i++) 1119 for (int j=0;j<8;j++) { 1120 if (poledisp[j][i]!=pole[j][i]) show_fig(i,j); 1121 poledisp[j][i]=pole[j][i]; 1122 } 1123} 1124//**************************** 1125String str_step(int i) { 1126 String s=""; 1127 if (steps[i].fig1==0) return s; 1128 if (steps[i].type==2) s="0-0"; 1129 else if (steps[i].type==3) s="0-0-0"; 1130 else { 1131 if (abs(steps[i].fig1)>1) s=fig_symb[abs(steps[i].fig1)]; 1132 if (abs(steps[i].fig1<5)) { 1133 s=s+char(97+steps[i].y1); 1134 s=s+String(8-steps[i].x1); 1135 if (steps[i].fig2==0) s=s+"-"; 1136 } 1137 if (steps[i].fig2!=0) { 1138 s=s+"x"; 1139 if (abs(steps[i].fig2)>1) s=s+fig_symb[abs(steps[i].fig2)]; 1140 } 1141 s=s+char(97+steps[i].y2); 1142 s=s+String(8-steps[i].x2); 1143 } 1144 if (steps[i].type>3) s=s+fig_symb[steps[i].type-2]; 1145 if (steps[i].check==1) s=s+"+"; else 1146 if (steps[i].check==2) s=s+"#"; 1147 return s; 1148} 1149//**************************** 1150void show_steps() { 1151 tft.fillRect(320,0,160,222,getColor(8,8,8)); 1152 tft.fillRect(320,0,160,16,DARK); 1153 tft.setTextSize(1); 1154 tft.setTextColor(WHITE,DARK); 1155 tft.setCursor(332,4); 1156 tft.print("ArduinoMega Chess (DUE)"); 1157 tft.setTextSize(1); 1158 int i=1; int y=20; 1159 tft.setTextColor(GRAY); 1160 int cur=(cur_step+1)/2; // 1161 int lim_step=cur_step; 1162 if (checkmatesearch&&lastscore>9000) { 1163 tft.setTextColor(GREEN); 1164 lim_step=cur_step+10000-lastscore; 1165 cur=lim_step/2; 1166 } 1167 if (cur>19) i=cur-19; 1168 while (i<=cur&&y<215) { // 1169 tft.setCursor(324,y); 1170 tft.print(i); 1171 tft.print(". "); 1172 if (steps[i*2-1].fig1!=0) tft.print(str_step(i*2-1)); 1173 if (steps[i*2].fig1==0||i*2>lim_step) break; 1174 tft.print(", "); 1175 tft.print(str_step(i*2)); 1176 i++; y+=10; 1177 } 1178 1179} 1180//**************************** 1181boolean getdiagrowcheckw(signed char dj,signed char di) { // 1182signed char d,j1,i1; 1183 j1=WKJ; i1=WKI; 1184 for (d=1;d<8;d++) { 1185 j1+=dj; i1+=di; 1186 if (getpole(j1,i1)) { 1187 if (pole[j1][i1]==-fq||pole[j1][i1]==-fb) return true; 1188 if (pole[j1][i1]!=0) break; 1189 } else break; 1190 } 1191 return false; 1192} 1193//**************************** 1194boolean getdiagrowcheckb(signed char dj,signed char di) { // 1195signed char d,j1,i1; 1196 j1=BKJ; i1=BKI; 1197 for (d=1;d<8;d++) { 1198 j1+=dj; i1+=di; 1199 if (getpole(j1,i1)) { 1200 if (pole[j1][i1]==fq||pole[j1][i1]==fb) return true; 1201 if (pole[j1][i1]!=0) break; 1202 } else break; 1203 } 1204 return false; 1205} 1206//**************************** 1207boolean getstreightrowcheckw(signed char dj,signed char di) { // - 1208signed char d,j1,i1; 1209 j1=WKJ; i1=WKI; 1210 for (d=1;d<8;d++) { 1211 j1+=dj; i1+=di; 1212 if (getpole(j1,i1)) { 1213 if (pole[j1][i1]==-fq||pole[j1][i1]==-fr) return true; 1214 if (pole[j1][i1]!=0) break; 1215 } else break; 1216 } 1217 return false; 1218} 1219//**************************** 1220boolean getstreightrowcheckb(signed char dj,signed char di) { // - 1221signed char d,j1,i1; 1222 j1=BKJ; i1=BKI; 1223 for (d=1;d<8;d++) { 1224 j1+=dj; i1+=di; 1225 if (getpole(j1,i1)) { 1226 if (pole[j1][i1]==fq||pole[j1][i1]==fr) return true; 1227 if (pole[j1][i1]!=0) break; 1228 } else break; 1229 } 1230 return false; 1231} 1232//**************************** 1233boolean get_check(signed char king) { // 1234 if (king==fk) { // 1235 if (getdiagrowcheckw(-1, 1)) return true; 1236 if (getdiagrowcheckw(-1,-1)) return true; 1237 if (getdiagrowcheckw( 1,-1)) return true; 1238 if (getdiagrowcheckw( 1, 1)) return true; 1239 if (getstreightrowcheckw(-1, 0)) return true; 1240 if (getstreightrowcheckw( 0, 1)) return true; 1241 if (getstreightrowcheckw( 0,-1)) return true; 1242 if (getstreightrowcheckw( 1, 0)) return true; 1243 if (getpole(WKJ-2,WKI-1)&&pole[WKJ-2][WKI-1]==-fn) return true; 1244 if (getpole(WKJ-2,WKI+1)&&pole[WKJ-2][WKI+1]==-fn) return true; 1245 if (getpole(WKJ-1,WKI-2)&&pole[WKJ-1][WKI-2]==-fn) return true; 1246 if (getpole(WKJ-1,WKI+2)&&pole[WKJ-1][WKI+2]==-fn) return true; 1247 if (getpole(WKJ+1,WKI-2)&&pole[WKJ+1][WKI-2]==-fn) return true; 1248 if (getpole(WKJ+1,WKI+2)&&pole[WKJ+1][WKI+2]==-fn) return true; 1249 if (getpole(WKJ+2,WKI-1)&&pole[WKJ+2][WKI-1]==-fn) return true; 1250 if (getpole(WKJ+2,WKI+1)&&pole[WKJ+2][WKI+1]==-fn) return true; 1251 if (getpole(WKJ-1,WKI-1)&&pole[WKJ-1][WKI-1]==-fp) return true; 1252 if (getpole(WKJ-1,WKI+1)&&pole[WKJ-1][WKI+1]==-fp) return true; 1253 } else { // 1254 if (getdiagrowcheckb( 1,-1)) return true; 1255 if (getdiagrowcheckb( 1, 1)) return true; 1256 if (getdiagrowcheckb(-1, 1)) return true; 1257 if (getdiagrowcheckb(-1,-1)) return true; 1258 if (getstreightrowcheckb( 1, 0)) return true; 1259 if (getstreightrowcheckb( 0, 1)) return true; 1260 if (getstreightrowcheckb( 0,-1)) return true; 1261 if (getstreightrowcheckb(-1, 0)) return true; 1262 if (getpole(BKJ+2,BKI-1)&&pole[BKJ+2][BKI-1]==fn) return true; 1263 if (getpole(BKJ+2,BKI+1)&&pole[BKJ+2][BKI+1]==fn) return true; 1264 if (getpole(BKJ+1,BKI-2)&&pole[BKJ+1][BKI-2]==fn) return true; 1265 if (getpole(BKJ+1,BKI+2)&&pole[BKJ+1][BKI+2]==fn) return true; 1266 if (getpole(BKJ-1,BKI-2)&&pole[BKJ-1][BKI-2]==fn) return true; 1267 if (getpole(BKJ-1,BKI+2)&&pole[BKJ-1][BKI+2]==fn) return true; 1268 if (getpole(BKJ-2,BKI-1)&&pole[BKJ-2][BKI-1]==fn) return true; 1269 if (getpole(BKJ-2,BKI+1)&&pole[BKJ-2][BKI+1]==fn) return true; 1270 if (getpole(BKJ+1,BKI-1)&&pole[BKJ+1][BKI-1]==fp) return true; 1271 if (getpole(BKJ+1,BKI+1)&&pole[BKJ+1][BKI+1]==fp) return true; 1272 } 1273 if (abs(BKJ-WKJ)<=1&&abs(BKI-WKI)<=1) return true; // 1274 return false; 1275} 1276//**************************** 1277boolean getpole(signed char j,signed char i) { // 1278 if (j>=0&&j<8&&i>=0&&i<8) return true; 1279 return false; 1280} 1281//**************************** 1282void addstep(signed char j1,signed char i1,signed char j2,signed char i2,signed char type) { 1283 int st=start_var+cur_var; 1284 steps[st].x1=j1; 1285 steps[st].x2=j2; 1286 steps[st].y1=i1; 1287 steps[st].y2=i2; 1288 steps[st].fig1=pole[j1][i1]; 1289 steps[st].fig2=pole[j2][i2]; 1290 if (type==1) { // 1291 steps[st].fig2=-steps[st].fig1; 1292 } 1293 steps[st].type=type; 1294 signed char ko=-fk; // 1295 if (steps[st].fig1>0) ko=fk; 1296 movestep(st); 1297 if (get_check(ko)) { backstep(st); return; } // - 1298 boolean che=get_check(-ko); // 1299 backstep(st); 1300 steps[st].weight=abs(steps[st].fig2)-abs(steps[st].fig1); 1301 if (type>3) steps[st].weight+=fig_weight[type-2]; 1302 if (endspiel&&steps[st].fig1==ko) steps[st].weight+=10; // - 1303 steps[st].check=che; 1304 if (che) steps[st].weight+=10; 1305 if (only_action) { 1306 if (steps[st].fig1==fp&&steps[st].x2==1||steps[st].fig1==-fp&&steps[st].x2==6) // 1307 { cur_var++; return; } 1308 if (steps[st].fig2==0&&steps[st].type<4&&!che&&!check_on_table) return; 1309 } 1310 1311 cur_var++; 1312} 1313//**************************** 1314void getrowstepsw(signed char j,signed char i,signed char dj,signed char di) { // 1315signed char d,j1,i1; 1316 j1=j; i1=i; 1317 for (d=1;d<8;d++) { 1318 j1+=dj; i1+=di; 1319 if (getpole(j1,i1)) { 1320 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1321 if (pole[j1][i1]!=0) break; 1322 } else break; 1323 } 1324} 1325//**************************** 1326void getrowstepsb(signed char j,signed char i,signed char dj,signed char di) { // 1327signed char d,j1,i1; 1328 j1=j; i1=i; 1329 for (d=1;d<8;d++) { 1330 j1+=dj; i1+=di; 1331 if (getpole(j1,i1)) { 1332 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1333 if (pole[j1][i1]!=0) break; 1334 } else break; 1335 } 1336} 1337//**************************** 1338void getonestepw(signed char j,signed char i,signed char dj,signed char di) { // 1339signed char j1,i1; 1340 j1=j+dj; i1=i+di; 1341 if (getpole(j1,i1)) 1342 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1343} 1344//**************************** 1345void getonestepb(signed char j,signed char i,signed char dj,signed char di) { // 1346signed char j1,i1; 1347 j1=j+dj; i1=i+di; 1348 if (getpole(j1,i1)) 1349 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1350} 1351//**************************** 1352void sort_variants(int from, int to) { // 1353 while (1) { 1354 int mov=0; 1355 for (int i=from;i<to;i++) // 1356 if (steps[i].weight<steps[i+1].weight) { 1357 mov++; 1358 step_type buf=steps[i]; 1359 steps[i]=steps[i+1]; 1360 steps[i+1]=buf; 1361 } 1362 if (mov==0) break; 1363 } 1364} 1365//**************************** 1366void load_variants(int nstep) { // nstep 1367 cur_var=0; 1368 if (pole[WKJ][WKI]!=fk||pole[BKJ][BKI]!=-fk) kingpositions(); 1369 if (nstep%2==1) check_on_table=get_check(fk); 1370 else check_on_table=get_check(-fk); // 1371 for (char i=0;i<8;i++) 1372 for (char j=0;j<8;j++) 1373 if (pole[j][i]>0&&nstep%2==1||pole[j][i]<0&&nstep%2==0) { 1374 switch (pole[j][i]) { 1375 case fp: // 1376 if (getpole(j-1,i)&&pole[j-1][i]==0) 1377 if (j!=1) addstep(j,i,j-1,i,0); else 1378 for (signed char t=4;t<8;t++) addstep(1,i,0,i,t); //-... 1379 if (j==6&&pole[j-1][i]==0&&pole[j-2][i]==0) addstep(j,i,j-2,i,0); 1380 if (getpole(j-1,i-1)&&pole[j-1][i-1]<0) 1381 if (j!=1) addstep(j,i,j-1,i-1,0); else 1382 for (signed char t=4;t<8;t++) addstep(j,i,j-1,i-1,t); //-... 1383 if (getpole(j-1,i+1)&&pole[j-1][i+1]<0) 1384 if (j!=1) addstep(j,i,j-1,i+1,0); else 1385 for (signed char t=4;t<8;t++) addstep(j,i,j-1,i+1,t); //-... 1386 if (j==3&&steps[nstep-1].fig1==-fp&&steps[nstep-1].x2==3&&steps[nstep-1].x1==1) { 1387 if (steps[nstep-1].y2-i==1) { // 1388 addstep(j,i,j-1,i+1,1); 1389 } else if (steps[nstep-1].y2-i==-1) { // 1390 addstep(j,i,j-1,i-1,1); 1391 } 1392 } 1393 break; 1394 case -fp: // 1395 if (getpole(j+1,i)&&pole[j+1][i]==0) 1396 if (j!=6) addstep(j,i,j+1,i,0); else 1397 for (signed char t=4;t<8;t++) addstep(j,i,j+1,i,t); //-... 1398 if (j==1&&pole[j+1][i]==0&&pole[j+2][i]==0) addstep(j,i,j+2,i,0); 1399 if (getpole(j+1,i-1)&&pole[j+1][i-1]>0) 1400 if (j!=6) addstep(j,i,j+1,i-1,0); else 1401 for (signed char t=4;t<8;t++) addstep(j,i,j+1,i-1,t); //-... 1402 if (getpole(j+1,i+1)&&pole[j+1][i+1]>0) 1403 if (j!=6) addstep(j,i,j+1,i+1,0); else 1404 for (signed char t=4;t<8;t++) addstep(j,i,j+1,i+1,t); //-... 1405 if (j==4&&steps[nstep-1].fig1==fp&&steps[nstep-1].x2==4&&steps[nstep-1].x1==6) { 1406 if (steps[nstep-1].y2-i==1) { // 1407 addstep(j,i,j+1,i+1,1); 1408 } else if (steps[nstep-1].y2-i==-1) { // 1409 addstep(j,i,j+1,i-1,1); 1410 } 1411 } 1412 break; 1413 case fn: // 1414 getonestepw(j,i,-2,-1); 1415 getonestepw(j,i,-2,1); 1416 getonestepw(j,i,-1,-2); 1417 getonestepw(j,i,-1,2); 1418 getonestepw(j,i,2,-1); 1419 getonestepw(j,i,2,1); 1420 getonestepw(j,i,1,-2); 1421 getonestepw(j,i,1,2); 1422 break; 1423 case -fn: // 1424 getonestepb(j,i,-2,-1); 1425 getonestepb(j,i,-2,1); 1426 getonestepb(j,i,-1,-2); 1427 getonestepb(j,i,-1,2); 1428 getonestepb(j,i,2,-1); 1429 getonestepb(j,i,2,1); 1430 getonestepb(j,i,1,-2); 1431 getonestepb(j,i,1,2); 1432 break; 1433 case fb: // 1434 getrowstepsw(j,i,1,1); 1435 getrowstepsw(j,i,-1,-1); 1436 getrowstepsw(j,i,1,-1); 1437 getrowstepsw(j,i,-1,1); 1438 break; 1439 case -fb: // 1440 getrowstepsb(j,i,1,1); 1441 getrowstepsb(j,i,-1,-1); 1442 getrowstepsb(j,i,1,-1); 1443 getrowstepsb(j,i,-1,1); 1444 break; 1445 case fr: // 1446 getrowstepsw(j,i,1,0); 1447 getrowstepsw(j,i,-1,0); 1448 getrowstepsw(j,i,0,1); 1449 getrowstepsw(j,i,0,-1); 1450 break; 1451 case -fr: // 1452 getrowstepsb(j,i,1,0); 1453 getrowstepsb(j,i,-1,0); 1454 getrowstepsb(j,i,0,1); 1455 getrowstepsb(j,i,0,-1); 1456 break; 1457 case fq: // 1458 getrowstepsw(j,i,1,1); 1459 getrowstepsw(j,i,-1,-1); 1460 getrowstepsw(j,i,1,-1); 1461 getrowstepsw(j,i,-1,1); 1462 getrowstepsw(j,i,1,0); 1463 getrowstepsw(j,i,-1,0); 1464 getrowstepsw(j,i,0,1); 1465 getrowstepsw(j,i,0,-1); 1466 break; 1467 case -fq: // 1468 getrowstepsb(j,i,1,1); 1469 getrowstepsb(j,i,-1,-1); 1470 getrowstepsb(j,i,1,-1); 1471 getrowstepsb(j,i,-1,1); 1472 getrowstepsb(j,i,1,0); 1473 getrowstepsb(j,i,-1,0); 1474 getrowstepsb(j,i,0,1); 1475 getrowstepsb(j,i,0,-1); 1476 break; 1477 case fk: // 1478 getonestepw(j,i, 1,-1); 1479 getonestepw(j,i, 1, 0); 1480 getonestepw(j,i, 1, 1); 1481 getonestepw(j,i, 0,-1); 1482 getonestepw(j,i, 0, 1); 1483 getonestepw(j,i,-1,-1); 1484 getonestepw(j,i,-1, 0); 1485 getonestepw(j,i,-1, 1); 1486 if (!check_on_table) add_rok(j,i,nstep); 1487 break; 1488 case -fk: // 1489 getonestepb(j,i, 1,-1); 1490 getonestepb(j,i, 1, 0); 1491 getonestepb(j,i, 1, 1); 1492 getonestepb(j,i, 0,-1); 1493 getonestepb(j,i, 0, 1); 1494 getonestepb(j,i,-1,-1); 1495 getonestepb(j,i,-1, 0); 1496 getonestepb(j,i,-1, 1); 1497 if (!check_on_table) add_rok(j,i,nstep); 1498 break; 1499 } //switch 1500 } //if 1501 1502 if (nstep>cur_step) { 1503 for (int i=start_var;i<start_var+cur_var;i++) { // 1504 for (signed char j=0;j<MAXCUTS;j++) 1505 if (cuts[j].fig1==steps[i].fig1&& 1506 cuts[j].x1==steps[i].x1&&cuts[j].y1==steps[i].y1&& 1507 cuts[j].x2==steps[i].x2&&cuts[j].y2==steps[i].y2) { 1508 steps[i].weight+=100+cuts[j].weight; 1509 break; 1510 } 1511 if (nstep>1&&steps[nstep-1].fig2!=0) // - 1512 if (steps[nstep-1].x2==steps[i].x2&&steps[nstep-1].y2==steps[i].y2) 1513 steps[i].weight+=1000; 1514 } 1515 } 1516 1517 sort_variants(start_var,start_var+cur_var-1); 1518 1519 } 1520//**************************** 1521void movestep(int nstep) { 1522 pole[steps[nstep].x1][steps[nstep].y1]=0; 1523 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig1; 1524 if (steps[nstep].fig1==fk) { 1525 WKJ=steps[nstep].x2; WKI=steps[nstep].y2; 1526 } else if (steps[nstep].fig1==-fk) { 1527 BKJ=steps[nstep].x2; BKI=steps[nstep].y2; 1528 } 1529 if (steps[nstep].type==0) return; 1530 if (steps[nstep].type==1) // 1531 if (steps[nstep].fig1>0) 1532 pole[steps[nstep].x2+1][steps[nstep].y2]=0; 1533 else 1534 pole[steps[nstep].x2-1][steps[nstep].y2]=0; 1535 else if (steps[nstep].type==2) // 1536 if (steps[nstep].fig1>0) { // 1537 pole[7][4]=0; pole[7][5]=fr; pole[7][6]=fk; pole[7][7]=0; //show_board(); delay(3000); 1538 } else { // 1539 pole[0][4]=0; pole[0][5]=-fr; pole[0][6]=-fk; pole[0][7]=0; //show_board(); delay(3000); 1540 } 1541 else if (steps[nstep].type==3) // 1542 if (steps[nstep].fig1>0) { // 1543 pole[7][0]=0; pole[7][1]=0; pole[7][2]=fk; pole[7][3]=fr; pole[7][4]=0; 1544 } else { // 1545 pole[0][0]=0; pole[0][1]=0; pole[0][2]=-fk; pole[0][3]=-fr; pole[0][4]=0; 1546 } 1547 else if (steps[nstep].type>3) //-.... 1548 if (steps[nstep].fig1>0) pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].type-2; 1549 else pole[steps[nstep].x2][steps[nstep].y2]=2-steps[nstep].type; 1550} 1551//**************************** 1552void backstep(int nstep) { 1553 pole[steps[nstep].x1][steps[nstep].y1]=steps[nstep].fig1; 1554 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig2; 1555 if (steps[nstep].fig1==fk) { 1556 WKJ=steps[nstep].x1; WKI=steps[nstep].y1; 1557 } else if (steps[nstep].fig1==-fk) { 1558 BKJ=steps[nstep].x1; BKI=steps[nstep].y1; 1559 } 1560 if (steps[nstep].type==0) return; 1561 if (steps[nstep].type==1) { // 1562 pole[steps[nstep].x2][steps[nstep].y2]=0; 1563 if (steps[nstep].fig1>0) 1564 pole[steps[nstep].x2+1][steps[nstep].y2]=-fp; 1565 else 1566 pole[steps[nstep].x2-1][steps[nstep].y2]=fp; 1567 } else if (steps[nstep].type==2) // 1568 if (steps[nstep].fig1>0) { // 1569 pole[7][4]=fk; pole[7][5]=0; pole[7][6]=0; pole[7][7]=fr; 1570 } else { // 1571 pole[0][4]=-fk; pole[0][5]=0; pole[0][6]=0; pole[0][7]=-fr; // show_board(); delay(3000); 1572 } 1573 else if (steps[nstep].type==3) // 1574 if (steps[nstep].fig1>0) { // 1575 pole[7][0]=fr; pole[7][1]=0; pole[7][2]=0; pole[7][3]=0; pole[7][4]=fk; 1576 } else { // 1577 pole[0][0]=-fr; pole[0][1]=0; pole[0][2]=0; pole[0][3]=0; pole[0][4]=-fk; 1578 } 1579 1580} 1581//**************************** 1582void get_wrocks(int nstep) { 1583 w00=true; w000=true; 1584 for (int i=1;i<nstep;i++) { 1585 if (steps[i].fig1==fk) { w00=false; w000=false; return; } 1586 if (steps[i].fig1==fr) 1587 if (steps[i].x1==7&&steps[i].y1==7) w00=false; 1588 else if (steps[i].x1==7&&steps[i].y1==0) w000=false; 1589 } 1590} 1591//**************************** 1592void get_brocks(int nstep) { 1593 b00=true; b000=true; 1594 for (int i=1;i<nstep;i++) { 1595 if (steps[i].fig1==-fk) { b00=false; b000=false; return; } 1596 if (steps[i].fig1==-fr) 1597 if (steps[i].x1==0&&steps[i].y1==7) b00=false; 1598 else if (steps[i].x1==0&&steps[i].y1==0) b000=false; 1599 } 1600} 1601//**************************** 1602void add_rok(signed char j,signed char i,int nstep) { // 1603boolean che1,che2; 1604 if (nstep%2==1) { // 1605 if (j!=7||i!=4) return; 1606 if (pole[7][5]==0&&pole[7][6]==0&&pole[7][7]==fr) { // 1607 pole[7][4]=0; 1608 WKI=5; che1=get_check(fk); 1609 WKI=6; che2=get_check(fk); 1610 WKI=4; pole[7][4]=fk; 1611 get_wrocks(nstep); 1612 if (!che1&&!che2&&w00) addstep(7,4,7,6,2); 1613 } 1614 if (pole[7][0]==fr&&pole[7][1]==0&&pole[7][2]==0&&pole[7][3]==0) { // 1615 pole[7][4]=0; 1616 WKI=2; che1=get_check(fk); 1617 WKI=3; che2=get_check(fk); 1618 WKI=4; pole[7][4]=fk; 1619 get_wrocks(nstep); 1620 if (!che1&&!che2&&w000) addstep(7,4,7,2,3); 1621 } 1622 } else { // 1623 if (j!=0||i!=4) return; 1624 if (pole[0][5]==0&&pole[0][6]==0&&pole[0][7]==-fr) { // 1625 pole[0][4]=0; 1626 BKI=5; che1=get_check(-fk); 1627 BKI=6; che2=get_check(-fk); 1628 BKI=4; pole[0][4]=-fk; 1629 get_brocks(nstep); 1630 if (!che1&&!che2&&b00) addstep(0,4,0,6,2); 1631 } 1632 if (pole[0][0]==-fr&&pole[0][1]==0&&pole[0][2]==0&&pole[0][3]==0) { // 1633 pole[0][4]=0; 1634 BKI=2; che1=get_check(-fk); 1635 BKI=3; che2=get_check(-fk); 1636 BKI=4; pole[0][4]=-fk; 1637 get_brocks(nstep); 1638 if (!che1&&!che2&&b000) addstep(0,4,0,2,3); 1639 } 1640 1641 } 1642} 1643//**************************** 1644int addrowstepsw(signed char j,signed char i,signed char dj,signed char di,signed char dc) { // 1645signed char d,j1,i1; int c=0; 1646 j1=j; i1=i; 1647 for (d=1;d<8;d++) { 1648 j1+=dj; i1+=di; 1649 if (getpole(j1,i1)) { 1650 if (pole[j1][i1]==0) c+=dc; 1651 else if (pole[j1][i1]>0) { // 1652 c+=1; break; 1653 } else { 1654 if (cur_step>6) c+=-pole[j1][i1]; // 1655 break; 1656 } 1657 } else break; 1658 } 1659 return c; 1660} 1661//**************************** 1662int addrowstepsb(signed char j,signed char i,signed char dj,signed char di,signed char dc) { // 1663signed char d,j1,i1; int c=0; 1664 j1=j; i1=i; 1665 for (d=1;d<8;d++) { 1666 j1+=dj; i1+=di; 1667 if (getpole(j1,i1)) { 1668 if (pole[j1][i1]==0) c-=dc; 1669 else if (pole[j1][i1]<0) { // 1670 c-=1; break; 1671 } else { 1672 if (cur_step>6) c-=pole[j1][i1]; // 1673 break; 1674 } 1675 } else break; 1676 } 1677 return c; 1678} 1679//**************************** 1680int addonestepw(signed char j,signed char i,signed char dj,signed char di) { // 1681signed char j1,i1; 1682 j1=j+dj; i1=i+di; 1683 if (getpole(j1,i1)) 1684 if (pole[j1][i1]==0) return 2; 1685 else if (pole[j1][i1]>0) return 1; 1686 else return -pole[j1][i1]; 1687 return 0; 1688} 1689//**************************** 1690int addonestepb(signed char j,signed char i,signed char dj,signed char di) { // 1691signed char j1,i1; 1692 j1=j+dj; i1=i+di; 1693 if (getpole(j1,i1)) 1694 if (pole[j1][i1]==0) return 2; 1695 else if (pole[j1][i1]<0) return 1; 1696 else return pole[j1][i1]; 1697 return 0; 1698} 1699//**************************** 1700int activity() { // - + 1701int c=0; 1702signed char pwj[8],pwi[8],pbj[8],pbi[8],ipw=0,ipb=0,nbw=0,nbb=0; 1703 if (checkmatesearch) return 0; // 1704 for (char i=0;i<8;i++) 1705 for (char j=0;j<8;j++) 1706 if (pole[j][i]!=0) { 1707 switch (pole[j][i]) { 1708 case fp: // 1709 if (getpole(j,i+1)&&pole[j][i+1]==fp) c+=2; // 1710 if (getpole(j-1,i)&&pole[j-1][i]==fp) c-=20; // 1711 pwj[ipw]=j; pwi[ipw]=i; ipw++; 1712 break; 1713 case -fp: // 1714 if (getpole(j,i-1)&&pole[j][i-1]==-fp) c-=2; // 1715 if (getpole(j+1,i)&&pole[j+1][i]==-fp) c+=20; // 1716 pbj[ipb]=j; pbi[ipb]=i; ipb++; 1717 break; 1718 case fn: // 1719 c+=addonestepw(j,i,-2,-1); 1720 c+=addonestepw(j,i,-2,1); 1721 c+=addonestepw(j,i,-1,-2); 1722 c+=addonestepw(j,i,-1,2); 1723 c+=addonestepw(j,i,2,-1); 1724 c+=addonestepw(j,i,2,1); 1725 c+=addonestepw(j,i,1,-2); 1726 c+=addonestepw(j,i,1,2); 1727 break; 1728 case -fn: // 1729 c+=addonestepb(j,i,-2,-1); 1730 c+=addonestepb(j,i,-2,1); 1731 c+=addonestepb(j,i,-1,-2); 1732 c+=addonestepb(j,i,-1,2); 1733 c+=addonestepb(j,i,2,-1); 1734 c+=addonestepb(j,i,2,1); 1735 c+=addonestepb(j,i,1,-2); 1736 c+=addonestepb(j,i,1,2); 1737 break; 1738 case fb: // 1739 c+=addrowstepsw(j,i,1,1,1); 1740 c+=addrowstepsw(j,i,-1,-1,2); 1741 c+=addrowstepsw(j,i,1,-1,1); 1742 c+=addrowstepsw(j,i,-1,1,2); 1743 nbw++; 1744 break; 1745 case -fb: // 1746 c+=addrowstepsb(j,i,1,1,2); 1747 c+=addrowstepsb(j,i,-1,-1,1); 1748 c+=addrowstepsb(j,i,1,-1,2); 1749 c+=addrowstepsb(j,i,-1,1,1); 1750 nbb++; 1751 break; 1752 case fr: // 1753 c+=addrowstepsw(j,i,1,0,2); // 1754 c+=addrowstepsw(j,i,-1,0,2); 1755 c+=addrowstepsw(j,i,0,1,1); 1756 c+=addrowstepsw(j,i,0,-1,1); 1757 break; 1758 case -fr: // 1759 c+=addrowstepsb(j,i,1,0,2); // 1760 c+=addrowstepsb(j,i,-1,0,2); 1761 c+=addrowstepsb(j,i,0,1,1); 1762 c+=addrowstepsb(j,i,0,-1,1); 1763 break; 1764 case fq: // 1765 if (cur_step>10) { 1766 c+=addrowstepsw(j,i,1,1,1); 1767 c+=addrowstepsw(j,i,-1,-1,2); 1768 c+=addrowstepsw(j,i,1,-1,1); 1769 c+=addrowstepsw(j,i,-1,1,2); 1770 c+=addrowstepsw(j,i,1,0,1); 1771 c+=addrowstepsw(j,i,-1,0,2); 1772 c+=addrowstepsw(j,i,0,1,1); 1773 c+=addrowstepsw(j,i,0,-1,1); 1774 } 1775 break; 1776 case -fq: // 1777 if (cur_step>10) { 1778 c+=addrowstepsb(j,i,1,1,2); 1779 c+=addrowstepsb(j,i,-1,-1,1); 1780 c+=addrowstepsb(j,i,1,-1,2); 1781 c+=addrowstepsb(j,i,-1,1,1); 1782 c+=addrowstepsb(j,i,1,0,2); 1783 c+=addrowstepsb(j,i,-1,0,1); 1784 c+=addrowstepsb(j,i,0,1,1); 1785 c+=addrowstepsb(j,i,0,-1,1); 1786 } 1787 break; 1788 } //switch 1789 if (cur_step>6) { 1790 if (pole[j][i]>0) { // 1791 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c+=3; 1792 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c+=3; 1793 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c+=2; 1794 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c+=2; 1795 } else { // 1796 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c-=3; 1797 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c-=3; 1798 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c-=2; 1799 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c-=2; 1800 } 1801 } 1802 } 1803 if (nbw>1) c+=30; // 1804 if (nbb>1) c-=30; 1805 for (signed char w=0;w<ipw;w++) { // 1806 boolean pass=1; 1807 for (signed char b=0;b<ipb;b++) { 1808 if (pwi[w]>0&&pbi[b]==pwi[w]-1&&pbj[b]<pwj[w]) pass=0; // 1809 if (pbi[b]==pwi[w]&&pbj[b]<pwj[w]) pass=0; // 1810 if ( pwi[w]<7&&pbi[b]==pwi[w]+1&&pbj[b]<pwj[w]) pass=0; // 1811 if (!pass) break; 1812 } 1813 if (pass) { 1814 c+=50; 1815 if (!endspiel) break; // 1816 } 1817 } 1818 for (signed char b=0;b<ipb;b++) { // 1819 boolean pass=1; 1820 for (signed char w=0;w<ipw;w++) { 1821 if (pbi[b]>0&&pwi[w]==pbi[b]-1&&pwj[w]>pbj[b]) pass=0; // 1822 if (pwi[w]==pbi[b]&&pwj[w]>pbj[b]) pass=0; // 1823 if (pbi[b]<7&&pwi[w]==pbi[b]+1&&pwj[w]>pbj[b]) pass=0; // 1824 if (!pass) break; 1825 } 1826 if (pass) { 1827 c-=50; 1828 if (!endspiel) break; // 1829 } 1830 } 1831 return c; 1832} 1833//**************************** 1834int evaluate(int nstep) { // 1835 int ww=0, wb=0; 1836 for (char i=0;i<8;i++) 1837 for (char j=0;j<8;j++) 1838 if (pole[j][i]<0) { 1839 wb+=fig_weight[-pole[j][i]]+(short)pgm_read_word(&pos[-pole[j][i]-1][7-j][i]); 1840 } 1841 else if (pole[j][i]>0) { 1842 ww+=fig_weight[pole[j][i]]+(short)pgm_read_word(&pos[pole[j][i]-1][j][i]); 1843 } 1844 count++; // 1845 int str=activity(); 1846 if (endspiel) { // 1847 wb+=(short)pgm_read_word(&pos[6][7-BKJ][BKI])-(short)pgm_read_word(&pos[5][7-BKJ][BKI]); 1848 ww+=(short)pgm_read_word(&pos[6][WKJ][WKI])-(short)pgm_read_word(&pos[5][WKJ][WKI]); 1849 if (wb<450&&ww>450) { // - - 1850 str-=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1851 } else if (ww<450&&wb>450) { // 1852 str+=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1853 } 1854 } 1855 if (nstep>8) { // 1856 if (steps[nstep-1].fig1==steps[nstep-5].fig1&&steps[nstep-2].fig1==steps[nstep-6].fig1&& 1857 steps[nstep-3].fig1==steps[nstep-7].fig1&&steps[nstep-4].fig1==steps[nstep-8].fig1) 1858 if (steps[nstep-1].x1==steps[nstep-5].x1&&steps[nstep-2].x1==steps[nstep-6].x1&& 1859 steps[nstep-3].x1==steps[nstep-7].x1&&steps[nstep-4].x1==steps[nstep-8].x1) 1860 if (steps[nstep-1].x2==steps[nstep-5].x2&&steps[nstep-2].x2==steps[nstep-6].x2&& 1861 steps[nstep-3].x2==steps[nstep-7].x2&&steps[nstep-4].x2==steps[nstep-8].x2) 1862 if (steps[nstep-1].y1==steps[nstep-5].y1&&steps[nstep-2].y1==steps[nstep-6].y1&& 1863 steps[nstep-3].y1==steps[nstep-7].y1&&steps[nstep-4].y1==steps[nstep-8].x1) 1864 if (steps[nstep-1].y2==steps[nstep-5].y2&&steps[nstep-2].y2==steps[nstep-6].y2&& 1865 steps[nstep-3].y2==steps[nstep-7].y2&&steps[nstep-4].y2==steps[nstep-8].y2) 1866 { Serial.println(F(" Draw - 3 repeat")); return 0; } 1867 } 1868 if (nstep%2==1) return 5000*(ww-wb)/(ww+wb+2000)+str; else return 5000*(wb-ww)/(ww+wb+2000)-str; 1869} 1870//**************************** 1871void add_cut(int ind) {// ind 1872 int minbeta=30000, minindex=MAXCUTS-1; 1873 for (char i=0;i<MAXCUTS;i++) { 1874 if (cuts[i].weight==0) { minindex=i; break; }// 1875 if (cuts[i].fig1==steps[ind].fig1&& 1876 cuts[i].x1==steps[ind].x1&&cuts[i].y1==steps[ind].y1&& 1877 cuts[i].x2==steps[ind].x2&&cuts[i].y2==steps[ind].y2) { 1878 cuts[i].weight++; return; // - 1 1879 } 1880 if (cuts[i].weight<minbeta) { // 1881 minbeta=cuts[i].weight; 1882 minindex=i; 1883 } 1884 } 1885 cuts[minindex]=steps[ind]; 1886 cuts[minindex].weight=1; //1 1887} 1888//**************************** 1889int quiescence(int start, int nstep, int alpha, int beta ) { 1890 if (nstep-cur_step>=LIMDEPTH||start>MAXSTEPS-70) return evaluate(nstep); 1891 if (!solving&&progress==0) return -5000; 1892 int score=-20000; 1893 start_var=start; 1894 only_action=true; 1895 load_variants(nstep); 1896 if (!check_on_table) { 1897 int stand_pat = evaluate(nstep); 1898 if (stand_pat >= score) score=stand_pat; 1899 if (score>alpha) alpha=score; 1900 if (alpha>=beta) return alpha; 1901 } 1902 if (cur_var==0) { 1903 if (check_on_table) { 1904 if (TRACE) Serial.println(F("checkmate?")); 1905 return -10000+nstep-cur_step; 1906 } else return evaluate(nstep); 1907 } 1908 int j=start+cur_var; 1909 for (int i=start;i<j;i++) { // 1910 if (TRACE) { //***** 1911 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" . ")); 1912 Serial.println(str_step(i)); 1913 } 1914 movestep(i); 1915 steps[nstep]=steps[i]; 1916 int tmp=-quiescence(j+1,nstep+1,-beta,-alpha); 1917 backstep(i); 1918 if (tmp>score) score=tmp; 1919 if (score>alpha) alpha=score; 1920 if (alpha>=beta ) { 1921 add_cut(nstep); 1922 return alpha; 1923 } 1924 if (checkmatesearch&&alpha>9000) break; 1925 gui(); 1926 } 1927 return score; 1928} 1929//**************************** 1930int alphaBeta(int start, int nstep, int alpha, int beta, int depthleft) { 1931//start - 1932//nstep - 1933int score=-20000,best; 1934 if( depthleft==0) return quiescence(start,nstep,alpha,beta) ; 1935 if (start>MAXSTEPS-70) return evaluate(nstep); 1936 start_var=start; 1937 only_action=false; 1938 if (nstep!=cur_step) load_variants(nstep); 1939 if (cur_var==0) { 1940 if (check_on_table) { 1941 if (TRACE) Serial.println(F("checkmate!")); 1942 return -10000+nstep-cur_step; 1943 } 1944 return 0; 1945 } 1946 int j=start+cur_var; 1947 best=start; 1948 for (int i=start;i<j;i++) { // 1949 if (nstep==cur_step) { 1950 Serial.print(str_step(i)); Serial.print(" "); 1951 Serial.print(i-start+1); Serial.print("/"); Serial.print(j-start); 1952 if (steps[i].weight<-9000) { Serial.println(F(" checkmate")); continue; } 1953 if (steps[i].fig2!=0||steps[i].check||alpha<-100) 1954 // , , -100 1955 { LIMDEPTH=MAXDEPTH+2; Serial.print(F("+2")); } else LIMDEPTH=MAXDEPTH; 1956 } else { 1957 if (TRACE) { //***** 1958 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" ")); 1959 Serial.println(str_step(i)); 1960 } 1961 } 1962 movestep(i); 1963 steps[nstep]=steps[i]; 1964 int tmp=-alphaBeta(j+1,nstep+1,-beta,-alpha,depthleft-1); 1965 backstep(i); 1966 steps[i].weight=tmp; 1967 if (tmp>score) score=tmp; 1968 if (score>alpha) { 1969 alpha=score; 1970 if (nstep>cur_step) add_cut(nstep); // 1971 if (TRACE) { Serial.print(F("ALPHA+:")); Serial.println(score); } 1972 best=i; 1973 if (nstep==cur_step) { 1974 steps[0]=steps[best]; 1975 Serial.print(F(" BEST")); 1976 } 1977 } 1978 if (alpha>=beta ) { 1979 if (nstep>cur_step) add_cut(nstep); // 1980 if (TRACE) { Serial.print(F("BETA CUT:")); Serial.println(score); } 1981 return alpha; 1982 } 1983 if (nstep==cur_step) { 1984 Serial.print(F(" ")); Serial.println(tmp); 1985 progress=100*(i-start+1)/(j-start); 1986 if (alpha==9999||alpha==-5000) break; 1987 if (checkmatesearch&&alpha>9000&&limit<3) break; 1988 if (!solving) { 1989 if (alpha>startweight&&cur_level>1) break; 1990 else { // - +50% 1991 if (BAction.bs==F("stopping")) break; 1992 if (cur_level>1&&100*(millis()-starttime)/(limittime-starttime)>240-limit*20) break; 1993 } 1994 } 1995 } 1996 if (nstep==cur_step+1&&!solving&&progress==0) break; 1997 gui(); 1998 } 1999 if (nstep==cur_step) { steps[nstep]=steps[best]; steps[0]=steps[best]; } 2000 return score; 2001} 2002//**************************** 2003void kingpositions() { 2004 for (char i=0;i<8;i++) // 2005 for (char j=0;j<8;j++) 2006 if (pole[j][i]==fk) { 2007 WKJ=j; WKI=i; 2008 } else if (pole[j][i]==-fk) { 2009 BKJ=j; BKI=i; 2010 } 2011} 2012//**************************** 2013int get_endspiel() { // 2014 int weight=0; 2015 for (char i=0;i<8;i++) 2016 for (char j=0;j<8;j++) 2017 if (pole[j][i]<0) 2018 weight+=fig_weight[-pole[j][i]]; 2019 else if (pole[j][i]>0) 2020 weight+=fig_weight[pole[j][i]]; // 8000 2021 if (weight<3500) endspiel=true; else endspiel=false; 2022 return weight; 2023} 2024//**************************** 2025boolean is_drawn() { // 2026 boolean drawn=false; 2027 int cn=0,cbw=0,cbb=0,co=0,cb=0,cw=0; 2028 for (char i=0;i<8;i++) 2029 for (char j=0;j<8;j++) { 2030 if (abs(pole[j][i])==1) co++; 2031 if (abs(pole[j][i])>3&&abs(pole[j][i])<6) co++; // , , 2032 if (abs(pole[j][i])==6) continue; // 2033 if (abs(pole[j][i])==2) cn++; // 2034 if (abs(pole[j][i])==3&&(i+j+2)%2==0) cbb++; // 2035 if (abs(pole[j][i])==3&&(i+j+2)%2==1) cbw++; // 2036 if (pole[j][i]==3) cw++; // 2037 if (pole[j][i]==-3) cb++; // 2038 } 2039 if (cn==1&&co+cbb+cbw==0) drawn=true; // 2040 if (cbb+cbw==1&&co+cn==0) drawn=true; // 2041 if (co+cn+cbb==0||co+cn+cbw==0) drawn=true; // 2042 if (co+cn==0&&cb==1&&cw==1) drawn=true; // 2043 if (drawn) return drawn; 2044 int rep=1; 2045 for (int s=cur_step-1; s>0; s--) { 2046 if (steps[s].fig1==0) continue; 2047 backstep(s); 2048 boolean eq=true; 2049 for (char i=0;i<8;i++) 2050 for (char j=0;j<8;j++) 2051 if (pole[j][i]!=pole0[j][i]) 2052 { eq=false; break; } 2053 if (eq) rep++; 2054 if (rep>2) break; 2055 } 2056 if (rep>1) { Serial.print(rep); Serial.println(" repetitions"); } 2057 for (char i=0;i<8;i++) 2058 for (char j=0;j<8;j++) pole[j][i]=pole0[j][i]; // 2059 if (rep>2) drawn=true; 2060 return drawn; 2061} 2062//**************************** 2063int solve_step() { 2064const int LMIN[7]={2,3,4,5,6,7,8}; 2065const int LMAX[7]={4,6,8,10,12,14,16}; 2066const int LCMIN[7]={2,4,6,8,10,12,14}; 2067const int LCMAX[7]={4,6,8,10,12,14,16}; 2068boolean check_on; 2069 starttime=millis(); 2070 isstatus=1; 2071 deletebuttons(); 2072 clearstatus(); 2073 tft.drawFastHLine(10,312,300,DARK); 2074 tft.drawFastHLine(10,315,300,DARK); 2075 BAction.Show("STOP"); 2076 BBW.Hide(); 2077 BBack.Hide(); 2078 BLimit.Hide(); 2079 BCM.Hide(); 2080 BDesc.Hide(); 2081 BSound.Hide(); 2082 BRotate.Hide(); 2083 limittime=starttime+limits[limit]*1000; // 2084 for (char i=0;i<8;i++) 2085 for (char j=0;j<8;j++) pole0[j][i]=pole[j][i]; // 2086 lastbest.fig1=0; 2087 kingpositions(); 2088 int wei=get_endspiel(); 2089 count=0; 2090 startweight=evaluate(cur_step); 2091 Serial.println(""); 2092 Serial.println(F("---------------")); 2093 if (endspiel) { Serial.print(F("Endspiel: ")); Serial.println(wei); } 2094 if (cur_step%2==1) Serial.print(F("WHITE, ")); else Serial.print(F("BLACK, ")); 2095 Serial.print(F("start Score= ")); Serial.println(startweight); 2096 start_var=cur_step+21; 2097 only_action=false; 2098 lastbest.fig1=0; 2099 steps[0].fig1=0; 2100 load_variants(cur_step); 2101 check_on=check_on_table; 2102 if (cur_var==0||is_drawn()) { 2103 beep(500); 2104 tft.drawFastHLine(10,312,300,BLACK); 2105 tft.drawFastHLine(10,315,300,BLACK); 2106 BAction.Hide(); 2107 isstatus=0; 2108 solving=false; 2109 if (check_on_table) return -9999; else return 8999; 2110 } 2111 if (cur_var==1) { // 1 2112 tft.drawFastHLine(10,312,300,BLACK); 2113 tft.drawFastHLine(10,315,300,BLACK); 2114 BAction.Hide(); 2115 isstatus=0; 2116 solving=false; 2117 steps[cur_step]=steps[cur_step+21]; 2118 return startweight; 2119 } 2120 int vars=cur_var; 2121 int ALPHA=-20000; 2122 int BETA=20000; 2123 int score; 2124 solving=true; 2125 if (checkmatesearch) { 2126 Serial.println(F("Checkmate search")); 2127 steps[cur_step].fig1=0; lastscore=0; show_steps(); 2128 } 2129 int l=0; 2130 for (char i=0;i<MAXCUTS;i++) { cuts[i].weight=0; cuts[i].fig1=0; } // 2131 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // 2132 movestep(i); 2133 steps[i].weight=evaluate(cur_step); 2134 if (steps[i].fig2!=0) steps[i].weight-=steps[i].fig1; 2135 backstep(i); 2136 } 2137 while (l<7) { 2138 cur_level=l+1; 2139 progress=0; 2140 tft.drawFastHLine(10,315,300,DARK); 2141 MINDEPTH=LMIN[l]; 2142 MAXDEPTH=LMAX[l]; 2143 if (checkmatesearch) { 2144 ALPHA=9000; BETA=10000; 2145 MINDEPTH=LCMIN[l]; 2146 MAXDEPTH=LCMAX[l]; 2147 } 2148 if (l>0) Serial.println(""); Serial.print(F("******* LEVEL=")); Serial.print(l+1); 2149 Serial.print(" "); Serial.print(MINDEPTH); Serial.print("-"); Serial.print(MAXDEPTH); 2150 Serial.print(" "); Serial.print((millis()-starttime)/1000.,1); Serial.println("s"); 2151 if (checkmatesearch) { 2152 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // - 2153 movestep(i); 2154 start_var=cur_step+22+vars; 2155 only_action=false; 2156 load_variants(cur_step+1); 2157 backstep(i); 2158 steps[i].weight=-cur_var; 2159 } 2160 } 2161 sort_variants(cur_step+21,cur_step+20+vars); // 2162 cur_var=vars; 2163 check_on_table=check_on; 2164 score=alphaBeta(cur_step+21,cur_step,ALPHA,BETA,MINDEPTH); 2165 if (score>9996) break; 2166 if (score>9000&&limit<3) break; 2167 if (100*(millis()-starttime)/(limittime-starttime)>80-l*8) break; 2168 if (!solving) break; 2169 l++; 2170 } //while l 2171 if (score<-9000) { 2172 Serial.println(F("GIVE UP!")); 2173 } else { 2174 Serial.print(F("STEP=")); Serial.println(str_step(cur_step)); 2175 if (score>9000) { 2176 Serial.print(F("CHECKMATE ")); 2177 if (score<9999) { 2178 Serial.print(F("in ")); Serial.print((9999-score)/2+1); Serial.print(F(" steps")); 2179 } else steps[cur_step].check=2; 2180 Serial.println(""); 2181 if (checkmatesearch) { 2182 steps[cur_step+9999-score].check=2; 2183 steps[cur_step+10000-score].fig1=0; 2184 for (int i=cur_step;i<cur_step+10000-score;i++) Serial.println(str_step(i)); 2185 } 2186 } 2187 } 2188 tft.drawFastHLine(10,312,300,BLACK); 2189 tft.drawFastHLine(10,315,300,BLACK); 2190 progress=0; 2191 show_status(); 2192 BAction.Hide(); 2193 solving=0; 2194 isstatus=0; 2195 return score; 2196 2197 2198} 2199//**************************** 2200
ArduinoMega Chess 1.0 MCUFRIEND 320x480
arduino
Sketch updated to touchscreen MCUFRIEND 320x480 pixels.
1//ArduinoMega Chess 1.0 (MCUFRIEND 320x480 version) 2//Sergey Urusov, 3 ususovsv@gmail.com 4 5#include <UTFTGLUE.h> 6#include <stdint.h> 7#include 8 "TouchScreen.h" 9#include <avr/pgmspace.h> 10#include <EEPROM.h> 11 12uint16_t 13 BLACK,BLUE,RED,GREEN,CYAN,MAGENTA,YELLOW,WHITE,GRAY,DARK,GRAY2,BLACKF,WHITEF; 14MCUFRIEND_kbv 15 tft; 16 17int XP = 6, YP = A1, XM = A2, YM = 7; 18TouchScreen ts(XP, YP, XM, 19 YM, 300); 20TSPoint tp; 21 22int touchx,touchy,touchx0,touchy0,ntouch; 23 24unsigned long lasttouch; 25#define MINPRESSURE 10 26#define MAXPRESSURE 27 1000 28const int PinBuzz = 44; 29int cycle=0; // 30const char fp=1; 31const 32 char fn=2; 33const char fb=3; 34const char fr=4; 35const char fq=5; 36const 37 char fk=6; 38const int fig_weight[]={0,100,320,330,500,900,0}; 39const char fig_symb[]=" 40 NBRQK"; 41unsigned long count; 42boolean rotate=false; 43const char polezero[8][8] 44 PROGMEM={ 45 { 0, 0, 0, 0, 0, 0, 0, 0}, 46 { 0, 47 0, 0, 0, 0, 0, 0, 0}, 48 { 0, 0, 0, 0, 0, 0, 0, 0}, 49 50 { 0, 0, 0, 0, 0, 0, 0, 0}, 51 { 0, 0, 0, 0, 52 0, 0, 0, 0}, 53 { 0, 0, 0, 0, 0, 0, 0, 0}, 54 { 55 0, 0, 0, 0, 0, 0, 0, 0}, 56 { 0, 0, 0, 0, 0, 0, 0, 57 0}, 58 }; 59const char polestart[8][8] PROGMEM={ 60 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 61 62 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 63 { 0, 0, 0, 0, 64 0, 0, 0, 0}, 65 { 0, 0, 0, 0, 0, 0, 0, 0}, 66 { 67 0, 0, 0, 0, 0, 0, 0, 0}, 68 { 0, 0, 0, 0, 0, 0, 0, 69 0}, 70 { fp, fp, fp, fp, fp, fp, fp, fp}, 71 { fr, fn, 72 fb, fq, fk, fb, fn, fr}, 73 }; 74 75char pole[8][8]={ 76 // 3 77 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 78 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 79 80 { 0, 0, 0, 0, 0, 0, 0, 0}, 81 { 0, 0, 0, 0, 82 0, 0, 0, 0}, 83 { 0, 0, 0, 0, 0, 0, 0, 0}, 84 { 85 0, 0, 0, 0, 0, 0, 0, 0}, 86 { fp, fp, fp, fp, fp, fp, fp, 87 fp}, 88 { fr, fn, fb, fq, fk, fb, fn, fr}, 89 }; 90 91 92char pole0[8][8]; // 93char poledisp[8][8]; // 94char polechoice[7]; 95 // 96boolean w00,w000,b00,b000; 97char blinkstep; 98typedef struct 99 { 100 char fig1, fig2; // 101 char x1,y1,x2,y2; // 102 char check; // 103 104 char type; // 1- ,2- ,3- ,4-5-6-7- ,,, 105 int weight; // , 106} step_type; 107const 108 int MAXSTEPS=500; //. 109//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 110int MINDEPTH; 111 //. 112int MAXDEPTH; //. 113int LIMDEPTH; //. 114const char MAXCUTS=10; 115 //.. - - (10- , 20) 116boolean TRACE=0; 117boolean checkmatesearch=0; // 118boolean 119 solving=false; 120boolean choice=false; 121boolean sound=0; 122int cur_step=1; // 123 , 1.... 124int limit=0; // , 0-6; 125//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 126const 127 long limits[7]={10,30,60,180,600,1800,100000}; 128// 0 1 2 129 3 4 5 6 130step_type steps[MAXSTEPS]; // 131step_type cuts[MAXCUTS]; 132 // - 133step_type lastbest; 134int lastscore; 135int minbeta,maxalpha; 136int startweight; 137int 138 cur_level; // () 139int start_var; // , 1.... 140int cur_var; // , 1.... 141int 142 cur_choice; // 143boolean check_on_table; // 144boolean isstatus; 145char WKJ=0,WKI=0,BKJ=0,BKI=0; 146 // 147boolean endspiel=false; // 148char progress; // 0-100 149boolean only_action=false; 150 // - 151unsigned long starttime,limittime,quitime=0; 152boolean drag; 153const 154 uint8_t fig[6][32] PROGMEM={ 155{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 156 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 1570x7, 0xE0, 0x3, 0xC0, 0x3, 158 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x0, 0x0}, // 159{0x0, 160 0x0, 0x3, 0x40, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x3F, 0xFC, 0x39, 0xFC, 0x33, 161 0xFC, 1620x7, 0xFC, 0xF, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x7, 0xE0, 163 0x1F, 0xF8, 0x0, 0x0}, // 164{0x1, 0x80, 0x3, 0xC0, 0x1, 0x80, 0xF, 0xF0, 0x1F, 165 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 1660x1F, 0xF8, 0x1F, 0xF8, 0xF, 0xF0, 167 0x7, 0xE0, 0x3, 0xC0, 0x31, 0x8C, 0x7F, 0xFE, 0x0, 0x0}, // 168{0x0, 0x0, 0x19, 169 0x98, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 1700xF, 171 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 172 0x0, 0x0}, // 173{0x19, 0x98, 0xD9, 0x9B, 0xD9, 0x9B, 0xD9, 0x9B, 0x6D, 0xB6, 0x6D, 174 0xB6, 0x6D, 0xB6, 0x35, 0xAC, 1750x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 176 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0}, // 177{0x1, 0x80, 0x1, 0x80, 0x79, 178 0x9E, 0x7D, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1790x7F, 0xFE, 180 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0} 181 // 182}; 183const uint8_t fig_cont[6][32] PROGMEM={ 184{0x0, 0x0, 0x0, 0x0, 185 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1860x8, 187 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 188 0xF8}, // 189{0x3, 0x40, 0x4, 0xA0, 0x8, 0x10, 0x12, 0x8, 0x20, 0x4, 0x40, 0x2, 190 0x46, 0x2, 0x4C, 0x2, 1910x38, 0x2, 0x10, 0x4, 0x10, 0x8, 0x8, 0x10, 0x4, 192 0x20, 0x18, 0x18, 0x20, 0x4, 0x3F, 0xFC}, // 193{0x2, 0x40, 0x4, 0x20, 0x6, 194 0x60, 0x10, 0x8, 0x21, 0x84, 0x21, 0x84, 0x21, 0x84, 0x27, 0xE4, 1950x21, 0x84, 196 0x21, 0x84, 0x11, 0x88, 0x8, 0x10, 0x34, 0x2C, 0x4E, 0x72, 0x80, 0x1, 0xFF, 0xFF}, 197 // 198{0x39, 0x9C, 0x26, 0x64, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x10, 0x8, 0x10, 199 0x8, 0x10, 0x8, 2000x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 201 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC}, // 202{0x22, 0x24, 0x22, 0x24, 0x22, 0x24, 203 0x26, 0x64, 0x92, 0x49, 0x92, 0x49, 0x92, 0x49, 0x4A, 0x52, 2040x40, 0x2, 0x40, 205 0x2, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC}, // 206{0x2, 207 0x40, 0x7A, 0x5E, 0x86, 0x61, 0x82, 0x41, 0x2, 0x40, 0x1, 0x80, 0x1, 0x80, 0x1, 208 0x80, 2090x80, 0x81, 0x41, 0x82, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 210 0x20, 0x4, 0x3F, 0xFC} // 211}; 212 213const uint8_t fig24[6][72] PROGMEM={ 214{0x0, 215 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1E, 0x0, 216 0x0, 0x3F, 0x0, 0x80, 0x7F, 0x0, 0x80, 0x7F, 0x0, 2170x80, 0x7F, 0x0, 0x0, 218 0x3F, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x1C, 0x0, 0x80, 0xFF, 0x0, 0x80, 0xFF, 219 0x0, 0x0, 0x1C, 0x0, 0x0, 0x1C, 0x0, 2200x0, 0x3E, 0x0, 0x0, 0x7F, 0x0, 0x80, 221 0xFF, 0x0, 0xC0, 0xFF, 0x1, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 222 0x0, 0x0, 0x0 223}, //pawn 224{0x0, 0x0, 0x0, 0x0, 0x6E, 0x0, 0x0, 0xFF, 225 0x0, 0x80, 0xFF, 0x1, 0x80, 0xFF, 0x3, 0xC0, 0xFF, 0x7, 0xE0, 0xFF, 0xF, 0xE0, 226 0xF9, 0xF, 2270xF0, 0xF8, 0xF, 0xF0, 0xF8, 0xF, 0x60, 0xFC, 0xF, 0x0, 0xFE, 228 0xF, 0x0, 0xFF, 0xF, 0x0, 0xFF, 0x7, 0x0, 0xFF, 0x3, 0x0, 0xFF, 0x1, 2290x0, 230 0xFE, 0x0, 0x0, 0x7C, 0x0, 0x0, 0x38, 0x0, 0x0, 0x38, 0x0, 0x0, 0x7C, 231 0x0, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 232}, //knight 233{0x0, 234 0x0, 0x0, 0x0, 0x3C, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x3C, 0x0, 235 0x0, 0x18, 0x0, 0x80, 0xFF, 0x1, 0xC0, 0xFF, 0x3, 2360xE0, 0xFF, 0x7, 0xE0, 237 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 238 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 2390xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0x80, 240 0xFF, 0x1, 0x0, 0xFF, 0x0, 0x8, 0x7E, 0x10, 0x1C, 0x3C, 0x38, 0xFE, 0xFF, 0x7F, 241 0x0, 0x0, 0x0 242}, //bishop 243{0x0, 0x0, 0x0, 0xF0, 0x3C, 0xF, 0xF0, 0x3C, 244 0xF, 0xF0, 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 245 0xFF, 0x3, 2460xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 247 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 2480xC0, 249 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 250 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 251}, //rook 252{0x0, 0x0, 253 0x0, 0xEE, 0xBD, 0x77, 0xEE, 0xBD, 0x77, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 254 0xC6, 0x18, 0x63, 0xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 2550xCC, 0x18, 0x33, 0xCC, 256 0x18, 0x33, 0xCC, 0x18, 0x33, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 257 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 2580x30, 0xFF, 0xC, 0xB0, 0xFF, 0xD, 0xF0, 259 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 260 0x0, 0x0, 0x0 261}, //queen 262{0x0, 0x0, 0x0, 0x0, 0x18, 0x0, 0xF0, 0x18, 263 0xF, 0xF8, 0x99, 0x1F, 0xFC, 0xDB, 0x3F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 264 0xFF, 0x7F, 2650xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 266 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x3F, 0xFC, 0xFF, 0x1F, 0xF8, 0xFF, 0x1F, 2670xF0, 268 0xFF, 0xF, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 269 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 270} //king 271}; 272 273const 274 uint8_t fig24_cont[6][72] PROGMEM={ 275{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 276 0x0, 0x0, 0x0, 0x1C, 0x0, 0x0, 0x22, 0x0, 0x0, 0x41, 0x0, 0x80, 0x80, 277 0x0, 0x80, 0x80, 0x0, 2780x80, 0x80, 0x0, 0x0, 0x41, 0x0, 0x0, 0x22, 0x0, 0xC0, 279 0xE3, 0x1, 0x40, 0x0, 0x1, 0x40, 0x0, 0x1, 0xC0, 0xE3, 0x1, 0x0, 0x22, 0x0, 2800x0, 281 0x41, 0x0, 0x80, 0x80, 0x0, 0x40, 0x0, 0x1, 0x20, 0x0, 0x2, 0x10, 0x0, 0x4, 282 0x10, 0x0, 0x4, 0x10, 0x0, 0x4, 0xF0, 0xFF, 0x7 283}, //pawn 284{0x0, 0x6E, 285 0x0, 0x0, 0x91, 0x0, 0x80, 0x0, 0x1, 0x40, 0x2, 0x2, 0x40, 0x0, 0x4, 0x20, 286 0x0, 0x8, 0x10, 0x0, 0x10, 0x10, 0x6, 0x10, 2870x8, 0x5, 0x10, 0x8, 0x5, 288 0x10, 0x90, 0x2, 0x10, 0x60, 0x1, 0x10, 0x80, 0x0, 0x10, 0x80, 0x0, 0x8, 0x80, 289 0x0, 0x4, 0x80, 0x0, 0x2, 2900x0, 0x1, 0x1, 0x0, 0x82, 0x0, 0x0, 0x44, 291 0x0, 0x0, 0x44, 0x0, 0xF8, 0x83, 0x1F, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 292 0xFF, 0x1F 293}, //knight 294{0x0, 0x3C, 0x0, 0x0, 0x42, 0x0, 0x0, 0x81, 0x0, 295 0x0, 0x81, 0x0, 0x0, 0x42, 0x0, 0x80, 0xE7, 0x1, 0x40, 0x0, 0x2, 0x20, 296 0x18, 0x4, 2970x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x90, 0xFF, 298 0x9, 0x90, 0xFF, 0x9, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 2990x10, 300 0x18, 0x8, 0x20, 0x18, 0x4, 0x40, 0x18, 0x2, 0x88, 0x0, 0x11, 0x14, 0x81, 0x28, 301 0xE2, 0xC3, 0x47, 0x1, 0x0, 0x80, 0xFF, 0xFF, 0xFF 302}, //bishop 303{0xF8, 0x7E, 304 0x1F, 0x8, 0xC3, 0x10, 0x8, 0xC3, 0x10, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xE0, 305 0xFF, 0x7, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 3060x20, 0x0, 0x4, 0x20, 0x0, 0x4, 307 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 308 0x0, 0x4, 0x20, 0x0, 0x4, 3090x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 310 0xE0, 0xFF, 0x7, 0x10, 0x0, 0x8, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 311 0xFF, 0x1F 312}, //rook 313{0xEF, 0xBD, 0xF7, 0x11, 0x42, 0x88, 0x11, 0x42, 0x88, 314 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 315 0x4C, 3160x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x64, 0x66, 0x26, 317 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 3180xC8, 319 0x0, 0x13, 0x48, 0x0, 0x12, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xF0, 0xFF, 0xF, 320 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 321}, //queen 322{0x0, 0x18, 323 0x0, 0xF0, 0x24, 0xF, 0x8, 0xA5, 0x10, 0x4, 0x66, 0x20, 0x2, 0x24, 0x40, 0x1, 324 0x24, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 3250x1, 0x18, 0x80, 0x1, 0x18, 326 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x40, 0x2, 327 0x18, 0x20, 0x4, 0x18, 0x20, 3280xE8, 0xFF, 0x17, 0x10, 0x0, 0x8, 0x10, 0x0, 329 0x8, 0x20, 0x0, 0x4, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 330 0xF8, 0xFF, 0x1F 331} //king 332}; 333 334 335const uint8_t issound[2][32] PROGMEM={ 336{0x0, 337 0x0, 0x0, 0x0, 0x0, 0x32, 0x0, 0x54, 0x0, 0x98, 0x1F, 0x10, 0x10, 0x30, 0x10, 338 0x50, 3390x10, 0x90, 0x11, 0x10, 0x1F, 0x10, 0x4, 0x90, 0x8, 0x50, 0x10, 0x30, 340 0x0, 0x0, 0x0, 0x0}, 341{0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x50, 0x0, 342 0x90, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 3430x10, 0x10, 0x10, 0x10, 0x1F, 0x10, 344 0x0, 0x90, 0x0, 0x50, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0} 345}; 346const uint8_t 347 iswb[2][32] PROGMEM={ 348{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 349 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 3500x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 351 0x10, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x0, 0x0}, 352{0x0, 0x0, 0x0, 0x0, 353 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 3540xF, 355 0xF0, 0x7, 0xE0, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 356 0x0, 0x0} 357}; 358const uint8_t iscm[2][32] PROGMEM={ 359{0x0, 0x0, 0x7, 0xE0, 360 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0x0, 0x30, 0x0, 0x60, 3610x0, 362 0xC0, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0, 0x1, 0x80, 0x1, 0x80, 363 0x0, 0x0}, 364{0x0, 0x0, 0x3F, 0x0, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x61, 365 0x80, 0x1, 0x80, 0x3, 0x0, 3660x6, 0x24, 0xC, 0x24, 0xC, 0xFF, 0xC, 0x24, 367 0x0, 0xFF, 0xC, 0x24, 0xC, 0x24, 0x0, 0x0} 368}; 369const uint8_t islimit[7][32] 370 PROGMEM={ 371{0x0, 0x0, 0x4, 0x70, 0xC, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 372 0x4, 0x88, 0x4, 0x88, 3730xE, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 374 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 375{0x0, 0x0, 0xC, 0x70, 0x12, 0x88, 376 0x2, 0x88, 0xC, 0x88, 0x2, 0x88, 0x2, 0x88, 0x12, 0x88, 3770xC, 0x70, 0x0, 378 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 379{0x0, 380 0x0, 0x1, 0x80, 0x3, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 381 0x80, 3820x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 383 0x22, 0xA4, 0x0, 0x0}, 384{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 385 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 3860x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 387 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 388{0x0, 0x0, 0xC, 389 0x78, 0x1C, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 3900x1E, 391 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 392 0x0}, 393{0x0, 0x0, 0x1C, 0x78, 0x36, 0xCC, 0x6, 0xCC, 0x1C, 0xCC, 0x6, 0xCC, 394 0x36, 0xCC, 0x36, 0xCC, 3950x1C, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 396 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 397{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 398 0x0, 0x0, 0x3C, 0x3C, 0x7E, 0x7E, 0x63, 0xC6, 0x61, 0x86, 3990x63, 0xC6, 0x66, 400 0x66, 0x3C, 0x3C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 401}; 402const 403 uint8_t isdesc[2][32] PROGMEM={ 404{0x0, 0x0, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E, 405 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 4060xE3, 0x8E, 0xE3, 0x8E, 0x1C, 407 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E}, 408{0x0, 0x0, 409 0x0, 0x4, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x30, 4100x30, 411 0x70, 0x78, 0xE0, 0x3C, 0xC0, 0x1F, 0xC0, 0xF, 0x80, 0x7, 0x0, 0x2, 0x0, 0x0, 412 0x0} 413 414}; 415const uint8_t isback[32] PROGMEM={ 4160x0, 0x0, 0x0, 0x0, 417 0x20, 0xF0, 0x31, 0xF8, 0x3B, 0xFC, 0x3F, 0x8E, 0x3F, 0xE, 0x3F, 0xE, 4180x3F, 419 0x8E, 0x3F, 0xCE, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x70, 0x0, 420 0x0 421}; 422const uint8_t issave[32] PROGMEM={ 4230x1, 0x80, 0x3, 0xC0, 0x7, 424 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 4250x23, 0xC4, 426 0x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 427}; 428const 429 uint8_t isload[32] PROGMEM={ 4300x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 431 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 4320x23, 0xC4, 0x27, 0xE4, 0x27, 433 0xE4, 0x23, 0xC4, 0x21, 0x84, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 434}; 435const uint8_t 436 isempty[32] PROGMEM={ 4370xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 438 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 4390x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 440 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF 441}; 442const uint8_t isinit[32] 443 PROGMEM={ 4440xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0x80, 0x1, 0x80, 445 0x1, 0x80, 0x1, 0x80, 0x1, 4460x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 447 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 448}; 449const uint8_t isplay[32] PROGMEM={ 4500x0, 451 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x1, 0x80, 0x23, 0xF8, 0x17, 0xC8, 452 0xB, 0xC8, 4530x3, 0xC0, 0x3, 0xC0, 0x2, 0x40, 0x4, 0x40, 0x4, 0x38, 0x4, 454 0x8, 0xC, 0x0, 0x0, 0x0 455}; 456const uint8_t isrotate[2][32] PROGMEM={ 457{0x0, 458 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 459 0x4, 4600x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 461 0x0, 0x0, 0x0, 0x0}, 462{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 463 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 4640x20, 0x4, 0x20, 0x4, 0x20, 0x4, 465 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0} 466}; 467 468const 469 int pos[7][8][8] PROGMEM={ 470 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 471 {100,100,100,100,100,100,100,100}, 472 //{ 50, 50, 50, 50, 50, 50, 50, 50}, 473 { 20, 30, 40, 50, 50, 40, 30, 20}, //{ 474 10, 10, 20, 30, 30, 20, 10, 10}, 475 { 5, 5, 10, 25, 25, 10, 5, 5}, 476 { 477 0, 0, 0, 20, 20, 0, 0, 0}, 478 { 5, -5,-10, 0, 0,-10, -5, 5}, 479 { 480 5, 10, 10,-20,-20, 10, 10, 5}, //{ 5, 10, 10,-20,-20, 10, 10, 5}, 481 { 0, 482 0, 0, 0, 0, 0, 0, 0}}, 483 484 {{-50,-40,-30,-30,-30,-30,-40,-50}, // 485 486 {-40,-20, 0, 0, 0, 0,-20,-40}, 487 {-30, 0, 10, 15, 15, 10, 0,-30}, 488 489 {-30, 5, 15, 20, 20, 15, 5,-30}, 490 {-30, 0, 15, 20, 20, 15, 0,-30}, 491 492 {-30, 5, 10, 15, 15, 10, 5,-30}, 493 {-40,-20, 0, 5, 5, 0,-20,-40}, 494 495 {-50,-40,-30,-30,-30,-30,-40,-50}}, 496 497 {{-20,-10,-10,-10,-10,-10,-10,-20}, 498 // 499 {-10, 0, 0, 0, 0, 0, 0,-10}, 500 {-10, 0, 5, 10, 10, 5, 0,-10}, 501 502 {-10, 5, 5, 10, 10, 5, 5,-10}, 503 {-10, 0, 10, 10, 10, 10, 0,-10}, 504 505 {-10, 10, 10, 10, 10, 10, 10,-10}, 506 {-10, 5, 0, 0, 0, 0, 5,-10}, 507 508 {-20,-10,-10,-10,-10,-10,-10,-20}}, 509 510 {{ 0, 0, 0, 0, 0, 0, 0, 0}, 511 // 512 { 5, 10, 10, 10, 10, 10, 10, 5}, 513 { -5, 0, 0, 0, 0, 0, 0, -5}, 514 515 { -5, 0, 0, 0, 0, 0, 0, -5}, 516 { -5, 0, 0, 0, 0, 0, 0, -5}, 517 518 { -5, 0, 0, 0, 0, 0, 0, -5}, 519 { -5, 0, 0, 0, 0, 0, 0, -5}, 520 521 { 0, 0, 0, 5, 5, 0, 0, 0}}, 522 523 {{-20,-10,-10, -5, -5,-10,-10,-20}, 524 // 525 {-10, 0, 0, 0, 0, 0, 0,-10}, 526 {-10, 0, 5, 5, 5, 5, 0,-10}, 527 528 { -5, 0, 5, 5, 5, 5, 0, -5}, 529 { 0, 0, 5, 5, 5, 5, 0, -5}, 530 531 {-10, 5, 5, 5, 5, 5, 0,-10}, 532 {-10, 0, 5, 0, 0, 0, 0,-10}, 533 534 {-20,-10,-10, -5, -5,-10,-10,-20}}, 535 536 {{-30,-40,-40,-50,-50,-40,-40,-30}, 537 // 538 {-30,-40,-40,-50,-50,-40,-40,-30}, 539 {-30,-40,-40,-50,-50,-40,-40,-30}, 540 541 {-30,-40,-40,-50,-50,-40,-40,-30}, 542 {-20,-30,-30,-40,-40,-30,-30,-20}, 543 544 {-10,-20,-20,-20,-20,-20,-20,-10}, 545 { 10, 10,-10,-10,-10,-10, 10, 10}, //{ 546 20, 20, 0, 0, 0, 0, 20, 20}, 547 { 10, 40, 30, 0, 0, 0, 50, 10}}, //{ 20, 548 30, 10, 0, 0, 10, 30, 20}}, 549 550 {{-50,-40,-30,-20,-20,-30,-40,-50}, // 551 552 {-30,-20,-10, 0, 0,-10,-20,-30}, 553 {-30,-10, 20, 30, 30, 20,-10,-30}, 554 555 {-30,-10, 30, 40, 40, 30,-10,-30}, 556 {-30,-10, 30, 40, 40, 30,-10,-30}, 557 558 {-30,-10, 20, 30, 30, 20,-10,-30}, 559 {-30,-30, 0, 0, 0, 0,-30,-30}, 560 561 {-50,-30,-30,-30,-30,-30,-30,-50}} 562}; 563 564//****** 565class Button { 566public: 567int 568 bx,by,bw,bh,bshift; 569String bs; 570const uint8_t* bp; 571 Button(int x,int y,int 572 w,int h,String s,const uint8_t* p, int shift); 573 void Show(int shift); 574 void 575 Show(String s); 576 void Hide(); 577 boolean IsPressed(int xc, int yc); 578private: 579boolean 580 active; 581 void ishow(uint16_t textcolor,uint16_t backcolor); 582}; 583Button::Button(int 584 x,int y,int w,int h,String s,const uint8_t* p=NULL, int shift=0) { 585 bx=x; by=y; 586 bw=w; bh=h; bs=s; bp=p; bshift=shift; 587}; 588void Button::Show(int shift=0) { 589 590 bshift=shift; 591 active=true; 592 ishow(WHITE,DARK); 593}; 594void Button::Show(String 595 s) { 596 bs=s; 597 active=true; 598 ishow(WHITE,DARK); 599}; 600void Button::Hide() 601 { 602 active=false; 603 ishow(GRAY,DARK); 604}; 605boolean Button::IsPressed(int 606 xc, int yc) { 607 if (active&&abs(bx+bw/2-xc)<bw/2&&abs(by+bh/2-yc)<bh/2) { 608 609 ishow(WHITE,GRAY); 610 touchx=0; touchy=0; 611 return true; 612 613 } 614 return false; 615}; 616void Button::ishow(uint16_t textcolor,uint16_t 617 backcolor) { 618 tft.fillRoundRect(bx+2,by+2,bw,bh,3,GRAY2); 619 620 tft.fillRoundRect(bx,by,bw,bh,3,backcolor); 621 tft.setTextColor(textcolor); 622 623 tft.setTextSize(1); 624 tft.setCursor(bx+(bw-bs.length()*6)/2,by+(bh-8)/2); 625 626 if (bp!=NULL) { 627 tft.drawBitmap(1+bx+(bw-16)/2, by+(bh-16)/2, 628 bp+bshift*32, 16, 16,textcolor); 629 } 630 else tft.print(bs); 631} 632Button 633 BAction(320, 285, 70, 30, "START"); 634Button BBW(402, 285, 30, 30, "",&iswb[0][0]); 635Button 636 BBack(444, 285, 30, 30, "",&isback[0]); 637Button BInit(444, 285, 30, 30, "",&isinit[0]); 638Button 639 BLimit(320, 245, 26, 30, "",&islimit[0][0]); 640Button BEmpty(320, 245, 26, 30, 641 "",&isempty[0]); 642Button BRotate(352, 245, 26, 30, "",&isrotate[0][0]); 643Button 644 BPlay(352, 245, 26, 30, "",&isplay[0]); 645Button BCM(384, 245, 26, 30, "",&iscm[0][0]); 646Button 647 BSave(384, 245, 26, 30, "",&issave[0]); 648Button BDesc(416, 245, 26, 30, "",&isdesc[0][0]); 649Button 650 BSound(448, 245, 26, 30, "",&issound[0][0]); 651Button BLoad(448, 245, 26, 30, 652 "",&isload[0]); 653//********************************** 654void drawBitmap(int16_t 655 x, int16_t y, 656 const uint8_t *bitmap, int16_t w, int16_t h, 657 uint16_t 658 color) { 659 int16_t i, j, byteWidth = (w + 7) / 8; 660 for(j=0; j<h; j++) { 661 662 for(i=0; i<w; i++ ) { 663 if(pgm_read_byte(bitmap + j * byteWidth 664 + i / 8) & (1 << (i % 8))) { 665 tft.drawPixel(x+i, y+j, color); 666 667 } 668 } 669 } 670} 671//**************************** 672void deletebuttons() 673 { 674 tft.fillRect(320,245,160,75,BLACK); 675} 676 677//**************************** 678void 679 clearstatus() { 680 tft.fillRect(320,225,160,20,BLACK); 681 tft.drawFastHLine(10,312,300,BLACK); 682 683 tft.drawFastHLine(10,315,300,BLACK); 684} 685//**************************** 686void 687 setup() { 688 Serial.begin(57600); 689 Serial.println(F("Start")); 690 691 pinMode(13, OUTPUT); 692 tft.reset(); 693 tft.begin(tft.readID()); 694 tft.setRotation(1); 695 696 definecolors(); 697 beep(100); 698 for (int i=0;i<MAXSTEPS;i++) 699 { 700 steps[i].x1=0; steps[i].y1=0; 701 steps[i].x2=0; steps[i].y2=0; 702 703 steps[i].fig1=0; steps[i].fig2=0; 704 steps[i].check=0; 705 steps[i].type=0; 706 707 steps[i].weight=0; 708 } 709 initboard(); 710} 711//**************************** 712void 713 initboard() { 714 for (int i=0;i<8;i++) 715 for (int j=0;j<8;j++) poledisp[j][i]=-100; 716 // 717 tft.fillScreen(BLACK); 718 tft.setTextColor(GRAY); 719 720 tft.setTextSize(1); 721 for (int j=1;j<9;j++) { 722 tft.setCursor(2,j*37-20); 723 724 if (rotate) tft.print(j); else tft.print(9-j); 725 } 726 for (byte i=1;i<9;i++) 727 { 728 tft.setCursor(i*37-8,300); 729 if (rotate) tft.print(char(96+9-i)); 730 else tft.print(char(96+i)); 731 } 732 initscreen(); 733} 734//**************************** 735void 736 initscreen() { 737 show_board(); 738 show_steps(); 739 BAction.Show("START"); 740 741 BBW.Show(cur_step%2); 742 if (cur_step>1) BBack.Show(); else BBack.Hide(); 743 744 BSave.Hide(); 745 BLoad.Hide(); 746 BInit.Hide(); 747 BBack.Show(); 748 BLimit.Show(); 749 750 BPlay.Hide(); 751 BRotate.Show(rotate); 752 753 BEmpty.Hide(); 754 BLimit.Show(limit); 755 BCM.Show(checkmatesearch); 756 BDesc.Show(choice); 757 758 BSound.Show(sound); 759} 760//**************************** 761void loop() 762 { 763 gui(); 764 if (solving) { 765 int len=70; 766 if 767 (!sound) len=2; 768 for (int i=1;i<4;i++) { tone(44,400+300*i,len); delay(70); 769 } 770 lastscore=solve_step(); 771 float tim=float(millis()-starttime)/1000; 772 773 for (int i=3;i>=1;i--) { tone(44,400+300*i,len); delay(70); } 774 if (!checkmatesearch&&lastscore>-9000&&lastscore!=8999) 775 { 776 movestep(cur_step); 777 cur_step++; steps[cur_step].fig1=0; 778 779 } 780 initscreen(); 781 animate_step(cur_step-1,false); 782 783 clearstatus(); 784 if (lastscore>9000) { 785 tft.setTextSize(1); 786 787 tft.setTextColor(GREEN); 788 tft.setCursor(322,230); 789 790 if (lastscore<9999) { 791 tft.print("# in "); 792 tft.print((9999-lastscore)/2+1); 793 tft.print(F(" st")); 794 } else tft.print("Checkmate"); 795 } 796 else if (lastscore<-9000) { 797 tft.setTextSize(1); 798 tft.setTextColor(RED); 799 800 tft.setCursor(322,230); 801 tft.print("GIVE UP!"); 802 803 show_steps(); 804 } else if (lastscore==8999) { //Draw 805 tft.setTextSize(1); 806 807 tft.setTextColor(YELLOW); 808 tft.setCursor(322,230); 809 810 tft.print("Draw"); 811 } 812 tft.setTextSize(1); 813 814 tft.setTextColor(GRAY); 815 tft.setCursor(322,230); 816 tft.print(tim,0); 817 818 tft.print("s "); 819 if (abs(lastscore)<5000) { 820 tft.print("="); 821 822 tft.print(lastscore); 823 } 824 tft.setCursor(390,230); 825 826 tft.print(count); 827 tft.print(" pos"); 828 Serial.print(F("Positions 829 estimated=")); Serial.println(count); 830 Serial.print(F("Time=")); Serial.print(tim,1); 831 Serial.println("s"); 832 } 833 delay(10); 834 835 836} 837 838//********************************** 839 840void load_choice() { 841 for (int i=0;i<7;i++) 842 if (cur_step%2==1) polechoice[i]=i; 843 else polechoice[i]=-i; 844} 845//********************************** 846void show_choice() 847 { 848uint16_t color,color_cont; 849 for (int i=0;i<7;i++) { 850 color=BLACKF; 851 852 if (i%2==0) color=WHITEF; 853 tft.fillRect(416,i*25+35,25,25,color); 854 855 color=DARK; 856 if (i==cur_choice) color=WHITE; 857 tft.drawRect(416,i*25+35,25,25,color); 858 859 color=BLACK; color_cont=GRAY; 860 if (polechoice[i]>0) { color=WHITE; color_cont=BLACK; 861 } 862 if (polechoice[i]!=0) { 863 tft.drawBitmap(420, i*25+39,&fig[abs(polechoice[i])-1][0], 864 16, 16,color); 865 tft.drawBitmap(420, i*25+39,&fig_cont[abs(polechoice[i])-1][0], 866 16, 16,color_cont); 867 } 868 } 869} 870//********************************** 871 872void animate_step(int nstep, boolean hide) { 873 if (!hide&&nstep-1>0&&steps[nstep-1].fig1!=0) 874 animate_step(nstep-1,true); 875 if (nstep-1<1||steps[nstep].fig1==0) return; 876 877 int j=steps[nstep].x1; 878 int dj=steps[nstep].x2-steps[nstep].x1; 879 int i=steps[nstep].y1; 880 881 int di=steps[nstep].y2-steps[nstep].y1; 882 di=di/abs(di); dj=dj/abs(dj); 883 884 if (hide) show_fig(i,j); 885 while (j!=steps[nstep].x2||i!=steps[nstep].y2) { 886 887 show_fig(i,j); 888 if (!hide) 889 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 890 891 else tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 892 int 893 mj=0; 894 if (j!=steps[nstep].x2) { j+=dj; mj=1; } 895 if (abs(steps[nstep].fig1)==fn&&mj==1) 896 continue; 897 if (i!=steps[nstep].y2) i+=di; 898 } 899 show_fig(i,j); 900 if 901 (!hide) 902 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 903 else 904 tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 905} 906//********************************** 907 908boolean load_usb() { 909char s='x',i=0,j=0; boolean load=false; 910 Serial.println(F("Wait 911 for FEN position")); 912 for (int i=0;i<8;i++) 913 for (int j=0;j<8;j++) 914 { 915 pole0[j][i]=pole[j][i]; 916 pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 917 918 } 919 while (s!=' ') { 920 s=Serial.read(); 921 922 if (i>7) { i=0; j++; } 923 if (!getpole(j,i)) break; 924 switch 925 (s) { 926 case '/': i=0; break; 927 case 'p': pole[j][i]=-fp; i++; 928 break; 929 case 'P': pole[j][i]=fp; i++; break; 930 case 'n': pole[j][i]=-fn; 931 i++; break; 932 case 'N': pole[j][i]=fn; i++; break; 933 case 'b': pole[j][i]=-fb; 934 i++; break; 935 case 'B': pole[j][i]=fb; i++; break; 936 case 'r': pole[j][i]=-fr; 937 i++; break; 938 case 'R': pole[j][i]=fr; i++; break; 939 case 'q': pole[j][i]=-fq; 940 i++; break; 941 case 'Q': pole[j][i]=fq; i++; break; 942 case 'k': pole[j][i]=-fk; 943 i++; break; 944 case 'K': pole[j][i]=fk; i++; break; 945 case '1': i++; break; 946 947 case '2': i+=2; break; 948 case '3': i+=3; break; 949 case '4': i+=4; 950 break; 951 case '5': i+=5; break; 952 case '6': i+=6; break; 953 case '7': 954 i+=7; break; 955 case '8': i=0; j++; break; 956 case ' ': break; 957 958 } 959 delay(20); 960 if (i+j>0&&Serial.available()==0) break; 961 962 } 963 s=0; 964 if (Serial.available()>0) s=Serial.read(); 965 while (Serial.available()>0) 966 Serial.read(); 967 if (s=='w'||s==0) { cur_step=1; load=true; } 968 else if (s=='b') 969 { cur_step=2; load=true; } 970 else load=false; 971 if (load) { 972 steps[1].fig1=0; 973 steps[2].fig1=0; 974 Serial.println(F("Position loaded")); 975 } else { 976 977 for (int i=0;i<8;i++) 978 for (int j=0;j<8;j++) 979 pole[j][i]=pole0[j][i]; 980 981 } 982 return load; 983} 984//********************************** 985 986void play() { 987 BAction.Show("STOP"); 988 BSave.Hide(); 989 BBW.Hide(); 990 991 BLoad.Hide(); 992 BInit.Hide(); 993 BPlay.Hide(); 994 BEmpty.Hide(); 995 BDesc.Hide(); 996 997 clearstatus(); 998 for (int i=cur_step-1;i>0;i--) { 999 if (steps[i].fig1!=0) 1000 backstep(i); 1001 } 1002 show_board(); 1003 show_steps(); 1004 boolean st=false; 1005 1006 unsigned long tim=millis(); 1007 int i=1; 1008 while (i<cur_step) { 1009 if (steps[i].fig1!=0) 1010 movestep(i); 1011 digitalWrite(13, HIGH); 1012 TSPoint p = ts.getPoint(); 1013 digitalWrite(13, 1014 LOW); 1015 pinMode(XM, OUTPUT); 1016 pinMode(YP, OUTPUT); 1017 int x=map(p.y,980,110,0,479); 1018 1019 int y=map(p.x,145,902,0,319); 1020 if (p.z>MINPRESSURE&&p.z<MAXPRESSURE) 1021 1022 if (BAction.IsPressed(x,y)) { st=true; BAction.Show(); } 1023 if (millis()-tim>2000||st) 1024 { 1025 tim=millis(); 1026 i++; 1027 show_board(); 1028 beep(20); 1029 1030 } 1031 } 1032 choice=0; 1033 beep(200); 1034 kingpositions(); 1035 1036 clearstatus(); 1037 initscreen(); 1038} 1039//********************************** 1040 1041void gui() { 1042 digitalWrite(13, HIGH); 1043 TSPoint p = ts.getPoint(); 1044 1045 digitalWrite(13, LOW); 1046 // if sharing pins, you'll need to fix the directions 1047 of the touchscreen pins 1048 pinMode(XM, OUTPUT); 1049 pinMode(YP, OUTPUT); 1050 1051 int x=map(p.y,980,110,0,479); 1052 int y=map(p.x,145,902,0,319); 1053 1054 if 1055 (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&p.z>MINPRESSURE&&p.z<MAXPRESSURE) { 1056 1057 // tft.drawPixel(x,y,CYAN); 1058 // Serial.println(x); 1059 // Serial.println(y); 1060 1061 touchx=x; 1062 touchy=y; 1063 if (solving) { 1064 if 1065 (BAction.IsPressed(x,y)) { // 1066 beep(100); 1067 solving=false; 1068 1069 BAction.Show("stopping"); 1070 BAction.Hide(); 1071 1072 return; 1073 } 1074 } else { //not solve 1075 if (BAction.IsPressed(x,y)) 1076 { // 1077 if (!choice) { 1078 delay(100); 1079 solving=true; 1080 1081 BAction.Show(); 1082 animate_step(cur_step-1,true); 1083 1084 return; 1085 } else { // USB 1086 delay(100); 1087 BAction.Hide(); 1088 1089 BSave.Hide(); 1090 BBW.Hide(); 1091 BLoad.Hide(); 1092 BInit.Hide(); 1093 1094 BPlay.Hide(); 1095 BEmpty.Hide(); 1096 BDesc.Hide(); 1097 1098 show_steps(); 1099 choice=0; 1100 if (!load_usb()) 1101 return; 1102 beep(200); 1103 kingpositions(); 1104 1105 clearstatus(); 1106 initscreen(); 1107 1108 } 1109 } else if (BBW.IsPressed(x,y)) { //- 1110 beep(100); 1111 1112 if (cur_step==1) cur_step=2; 1113 else if (cur_step==2) 1114 cur_step=1; 1115 else if (cur_step%2==0) cur_step--; else cur_step++; 1116 1117 BBW.Show(cur_step%2); 1118 if (choice) { load_choice(); show_choice(); 1119 } 1120 return; 1121 } else if (cur_step>1&&BBack.IsPressed(x,y)) { // 1122 1123 beep(100); 1124 cur_step--; 1125 animate_step(cur_step,true); 1126 1127 lastscore=0; 1128 backstep(cur_step); 1129 1130 steps[cur_step].fig1=0; 1131 show_board(); 1132 show_steps(); 1133 1134 if (cur_step>1) BBack.Show(); else BBack.Hide(); 1135 BBW.Show(cur_step%2); 1136 1137 clearstatus(); 1138 return; 1139 } else 1140 if (BLimit.IsPressed(x,y)) { // 1141 beep(100); 1142 limit++; 1143 if (limit>6) limit=0; 1144 BLimit.Show(limit); 1145 return; 1146 1147 } else if (BCM.IsPressed(x,y)) { // 1148 beep(100); 1149 1150 checkmatesearch=!checkmatesearch; 1151 BCM.Show(checkmatesearch); 1152 1153 if (!checkmatesearch) steps[cur_step].fig1=0; 1154 lastscore=0; 1155 1156 show_steps(); 1157 return; 1158 } else if (BRotate.IsPressed(x,y)) 1159 { // 1160 beep(100); 1161 rotate=!rotate; 1162 BRotate.Show(rotate); 1163 1164 initboard(); 1165 return; 1166 } else 1167 if (BSound.IsPressed(x,y)) { // 1168 if (sound==1) { beep(100); 1169 sound=0; } else { sound=1; beep(200); } 1170 BSound.Show(sound); 1171 1172 return; 1173 } else if (BDesc.IsPressed(x,y)) { // 1174 beep(100); 1175 1176 if (!choice) { 1177 choice=1; 1178 cur_choice=1; 1179 load_choice(); 1180 1181 show_choice(); 1182 BAction.Show("USB(FEN)"); 1183 1184 BBack.Hide(); 1185 BInit.Show(); 1186 BSave.Show(); 1187 1188 BLimit.Hide(); 1189 BEmpty.Show(); 1190 BRotate.Hide(); 1191 1192 BPlay.Show(); 1193 BCM.Hide(); 1194 BSound.Hide(); 1195 1196 BSave.Show(); 1197 BLoad.Show(); 1198 } else { 1199 1200 choice=0; 1201 lastscore=0; 1202 clearstatus(); 1203 kingpositions(); 1204 1205 initscreen(); 1206 } 1207 BDesc.Show(choice); 1208 1209 return; 1210 } else if (BInit.IsPressed(x,y)) 1211 { // 1212 beep(200); 1213 cur_step=1; 1214 steps[1].fig1=0; 1215 1216 for (int i=0;i<8;i++) 1217 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polestart[j][i]); 1218 1219 choice=0; 1220 kingpositions(); 1221 1222 clearstatus(); 1223 initscreen(); 1224 } 1225 else if (BEmpty.IsPressed(x,y)) { // 1226 beep(200); 1227 1228 cur_step=1; 1229 clearstatus(); 1230 steps[1].fig1=0; 1231 steps[2].fig1=0; 1232 for (int i=0;i<8;i++) 1233 for (int j=0;j<8;j++) 1234 pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 1235 BEmpty.Show(); 1236 1237 show_board(); 1238 } else if (BSave.IsPressed(x,y)) 1239 { 1240 beep(100); 1241 for (int i=0;i<8;i++) 1242 for (int 1243 j=0;j<8;j++) EEPROM.write(i*8+j,pole[j][i]); 1244 EEPROM.put(64,cur_step); 1245 1246 EEPROM.put(66,limit); 1247 for (int i=1;i<cur_step;i++) { 1248 EEPROM.put(66+i*10,steps[i]); 1249 1250 } 1251 BSave.Show(); 1252 } else if (BLoad.IsPressed(x,y)) { 1253 1254 beep(200); 1255 for (int i=0;i<8;i++) 1256 for (int j=0;j<8;j++) 1257 pole[j][i]=EEPROM.read(i*8+j); 1258 EEPROM.get(64,cur_step); 1259 1260 if (cur_step<0||cur_step>400) cur_step=1; 1261 EEPROM.get(66,limit); 1262 1263 if (limit<0||limit>6) limit=2; 1264 for 1265 (int i=1;i<cur_step;i++) { 1266 EEPROM.get(66+i*10,steps[i]); 1267 } 1268 1269 steps[0].fig1=0; 1270 steps[cur_step].fig1=0; 1271 1272 choice=0; 1273 kingpositions(); 1274 1275 clearstatus(); 1276 initscreen(); 1277 1278 } else if (BPlay.IsPressed(x,y)) { // 1279 beep(100); 1280 BPlay.Show(); 1281 1282 play(); 1283 return; 1284 } 1285 if (!choice&&ntouch==0) 1286 { 1287 touchx0=touchx; 1288 touchy0=touchy; 1289 char x1=touchy0/37; 1290 1291 char y1=(touchx0-10)/37; 1292 if (rotate) { x1=7-x1; y1=7-y1; 1293 } 1294 if (getpole(y1,x1)&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) 1295 { 1296 char p=pole[x1][y1]; 1297 drag=true; 1298 1299 pole[x1][y1]=0; 1300 show_fig(y1,x1); 1301 pole[x1][y1]=p; 1302 1303 } else drag=false; 1304 } 1305 ntouch++; 1306 if (drag&&ntouch>1) 1307 { 1308 uint16_t buf[256]; 1309 int x1=touchy0/37; 1310 1311 int y1=(touchx0-10)/37; 1312 if (rotate) { x1=7-x1; y1=7-y1; 1313 } 1314 if (abs(pole[x1][y1])>0&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) 1315 { 1316 for (int i=0;i<16;i++) 1317 for (int j=0;j<16;j++) buf[i*16+j]=tft.readPixel(touchx-8+i,touchy-8+j); 1318 1319 buf[0]=tft.readPixel(touchx-8,touchy-8); 1320 uint16_t color,color_cont; 1321 1322 color=BLACK; color_cont=GRAY; 1323 if (pole[x1][y1]>0) 1324 { color=WHITE; color_cont=BLACK; } 1325 tft.drawBitmap(touchx-8, 1326 touchy-8,&fig[abs(pole[x1][y1])-1][0], 16, 16,color); 1327 tft.drawBitmap(touchx-8, 1328 touchy-8,&fig_cont[abs(pole[x1][y1])-1][0], 16, 16,color_cont); 1329 delay(30); 1330 1331 for (int i=0;i<16;i++) 1332 for (int j=0;j<16;j++) tft.drawPixel(touchx-8+i,touchy-8+j,buf[i*16+j]); 1333 1334 } 1335 } else if (choice) { 1336 int x1=touchy/37; 1337 1338 int y1=(touchx-10)/37; 1339 if (rotate) { x1=7-x1; 1340 y1=7-y1; } 1341 if (touchy+touchx!=0&&choice&&getpole(y1,x1)) { 1342 1343 pole[x1][y1]=polechoice[cur_choice]; 1344 show_board(); 1345 } 1346 1347 if (touchx>=416&&touchx<=441&&touchy>=35&&touchy<=210) { 1348 1349 cur_choice=(touchy-35)/25; 1350 show_choice(); 1351 1352 } 1353 } 1354 lasttouch=millis(); 1355 } //not solve 1356 1357 if (!drag) { beep(10); ntouch=0; animate_step(cur_step-1,true); } 1358 } 1359 else { //touching 1360 if (!solving&&!choice&&drag!=0&&ntouch>0&&p.z<MINPRESSURE&&millis()-lasttouch>300) 1361 { //drag 1362 char x1=touchy0/37; 1363 char y1=(touchx0-10)/37; 1364 1365 char x2=touchy/37; 1366 char y2=(touchx-10)/37; 1367 if (rotate) { 1368 x1=7-x1; y1=7-y1; x2=7-x2; y2=7-y2; } 1369 if (getpole(y1,x1)) show_fig(y1,x1); 1370 1371 if (getpole(y1,x1)&&getpole(y2,x2)) { 1372 start_var=cur_step+1; 1373 1374 only_action=false; 1375 load_variants(cur_step); 1376 for 1377 (int i=cur_step+1;i<cur_step+cur_var+1;i++) 1378 if (steps[i].x1==x1&&steps[i].y1==y1&&steps[i].x2==x2&&steps[i].y2==y2) 1379 { 1380 steps[cur_step]=steps[i]; 1381 movestep(cur_step); 1382 cur_step++; steps[cur_step].fig1=0; 1383 int len=100; 1384 if (!sound) 1385 len=2; 1386 for (int i=2;i>=1;i--) { tone(44,400+300*i,len); delay(100); 1387 } 1388 initscreen(); 1389 animate_step(cur_step-1,false); 1390 break; 1391 1392 } 1393 } else { 1394 1395 beep(10); 1396 } 1397 ntouch=0; 1398 drag=false; 1399 1400 } 1401 } 1402 if (millis()-quitime>1000) { 1403 if (isstatus) show_status(); 1404 1405 quitime=millis(); 1406 } 1407 1408 1409} 1410//********************************** 1411 1412void show_status() { 1413 int tim=(millis()-starttime)/1000; 1414 int 1415 cur=300000*tim/(limittime-starttime); 1416 if (cur>300) { cur=300; solving=false; 1417 } 1418 tft.drawFastHLine(10,312,cur,GRAY); 1419 tft.drawFastHLine(10,315,progress*2,GRAY); 1420 1421 int m=tim/60; 1422 int s=tim%60; 1423 tft.setTextWrap(1); 1424 tft.setTextColor(GRAY); 1425 1426 tft.setTextSize(1); 1427 tft.fillRect(320,225,50,20,BLACK); 1428 1429 tft.setCursor(322,230); 1430 if (m>0) { tft.print(m); tft.print(":"); 1431 } 1432 if (s>0) { 1433 if (s<10&&m>0) tft.print("0"); 1434 tft.print(s); 1435 1436 } else if (m>0) tft.print("00"); else tft.print("0"); 1437 tft.setCursor(350,230); 1438 1439 tft.print(cur_level); 1440 1441 tft.setCursor(370,230); 1442 if 1443 (cur_step%2==1) tft.setTextColor(WHITE); else tft.setTextColor(GRAY); 1444 if 1445 (lastbest.fig1!=steps[0].fig1||lastbest.x1!=steps[0].x1||lastbest.y1!=steps[0].y1|| 1446 1447 lastbest.x2!=steps[0].x2||lastbest.y2!=steps[0].y2) { 1448 beep(10); 1449 1450 lastbest=steps[0]; 1451 tft.fillRect(370,230,50,10,BLACK); 1452 1453 tft.setCursor(370,230); 1454 tft.print(str_step(0)); 1455 blinkstep=0; 1456 1457 } 1458 if (steps[0].fig1!=0&&blinkstep==0&&tim>5) { 1459 char poleb[8][8]; 1460 1461 for (int i=0;i<8;i++) 1462 for (int j=0;j<8;j++) { poleb[j][i]=pole[j][i]; 1463 pole[j][i]=pole0[j][i]; } 1464 movestep(0); 1465 show_board(); 1466 1467 delay(100); 1468 backstep(0); 1469 show_board(); 1470 for 1471 (int i=0;i<8;i++) 1472 for (int j=0;j<8;j++) pole[j][i]=poleb[j][i]; 1473 1474 kingpositions(); 1475 } 1476 blinkstep++; 1477 if (blinkstep>2) blinkstep=0; 1478 1479} 1480//**************************** 1481void beep(int leng) { 1482 1483 analogWrite(PinBuzz, 20); 1484 delay(2); 1485 if (!sound) analogWrite(PinBuzz, 1486 0); 1487 delay(leng); 1488 analogWrite(PinBuzz, 0); 1489} 1490//**************************** 1491void 1492 definecolors() { 1493 BLACK =0x0000; 1494 BLUE =0x07FF; //0x001F; 1495 RED =0xF800; 1496 1497 GREEN =0x07E0; 1498 CYAN =0x07FF; 1499 MAGENTA=0xF81F; 1500 YELLOW =0xFFE0; 1501 1502 WHITE =0xFFFF; 1503 GRAY =0x7BEF; 1504 DARK =getColor(32,32,32); 1505 GRAY2 =getColor(16,16,16); 1506 1507 BLACKF =getColor(94,58,0); 1508 WHITEF =getColor(180,114,0); 1509} 1510//**************************** 1511uint16_t 1512 getColor(uint8_t red, uint8_t green, uint8_t blue) 1513{ 1514 red >>= 3; 1515 green 1516 >>= 2; 1517 blue >>= 3; 1518 return (red << 11) | (green << 5) | blue; 1519} 1520//**************************** 1521void 1522 show_fig(int i,int j) { 1523uint16_t color,color_cont; 1524 color=BLACKF; 1525 1526 if ((i+j+2)%2==0) color=WHITEF; 1527 int jj=j, ii=i; 1528 if (rotate) 1529 { jj=7-j; ii=7-i; } 1530 tft.fillRect(ii*37+12,jj*37,38,38,color); 1531 tft.drawRect(ii*37+12,jj*37,38,38,WHITE); 1532 1533 color=BLACK; color_cont=GRAY; 1534 if (pole[j][i]>0) { color=WHITE; color_cont=BLACK; 1535 } 1536 if (pole[j][i]!=0) { 1537 drawBitmap(ii*37+19, jj*37+7,&fig24[abs(pole[j][i])-1][0], 1538 24, 24,color); 1539 drawBitmap(ii*37+19, jj*37+7,&fig24_cont[abs(pole[j][i])-1][0], 1540 24, 24,color_cont); 1541 //drawBitmap(ii*37+24, jj*37+12,&fig[abs(pole[j][i])-1][0], 1542 16, 16,color); 1543 //drawBitmap(ii*37+24, jj*37+12,&fig_cont[abs(pole[j][i])-1][0], 1544 16, 16,color_cont); 1545 } 1546} 1547//**************************** 1548void 1549 show_board() { 1550 for (int i=0;i<8;i++) 1551 for (int j=0;j<8;j++) { 1552 if 1553 (poledisp[j][i]!=pole[j][i]) show_fig(i,j); 1554 poledisp[j][i]=pole[j][i]; 1555 1556 } 1557} 1558//**************************** 1559String str_step(int i) { 1560 String 1561 s=""; 1562 if (steps[i].fig1==0) return s; 1563 if (steps[i].type==2) s="0-0"; 1564 1565 else if (steps[i].type==3) s="0-0-0"; 1566 else { 1567 if (abs(steps[i].fig1)>1) 1568 s=fig_symb[abs(steps[i].fig1)]; 1569 if (abs(steps[i].fig1<5)) { 1570 s=s+char(97+steps[i].y1); 1571 1572 s=s+String(8-steps[i].x1); 1573 if (steps[i].fig2==0) s=s+"-"; 1574 1575 } 1576 if (steps[i].fig2!=0) { 1577 s=s+"x"; 1578 if 1579 (abs(steps[i].fig2)>1) s=s+fig_symb[abs(steps[i].fig2)]; 1580 } 1581 s=s+char(97+steps[i].y2); 1582 1583 s=s+String(8-steps[i].x2); 1584 } 1585 if (steps[i].type>3) s=s+fig_symb[steps[i].type-2]; 1586 1587 if (steps[i].check==1) s=s+"+"; else 1588 if (steps[i].check==2) s=s+"#"; 1589 1590 return s; 1591} 1592//**************************** 1593void show_steps() { 1594 tft.fillRect(320,0,160,222,getColor(8,8,8)); 1595 1596 tft.fillRect(320,0,160,16,DARK); 1597 tft.setTextSize(1); 1598 tft.setTextColor(WHITE,DARK); 1599 1600 tft.setCursor(350,4); 1601 tft.print("ArduinoMega Chess"); 1602 1603 tft.setTextSize(1); 1604 int i=1; int y=20; 1605 tft.setTextColor(GRAY); 1606 1607 int cur=(cur_step+1)/2; // 1608 int lim_step=cur_step; 1609 if (checkmatesearch&&lastscore>9000) 1610 { 1611 tft.setTextColor(GREEN); 1612 lim_step=cur_step+10000-lastscore; 1613 1614 cur=lim_step/2; 1615 } 1616 if (cur>20) i=cur-20; 1617 while (i<=cur&&y<215) 1618 { // 1619 tft.setCursor(324,y); 1620 tft.print(i); 1621 tft.print(". 1622 "); 1623 if (steps[i*2-1].fig1!=0) tft.print(str_step(i*2-1)); 1624 if (steps[i*2].fig1==0||i*2>lim_step) 1625 break; 1626 tft.print(", "); 1627 tft.print(str_step(i*2)); 1628 1629 i++; y+=10; 1630 } 1631 1632} 1633//**************************** 1634boolean 1635 getdiagrowcheckw(char dj,char di) { // 1636char d,j1,i1; 1637 j1=WKJ; 1638 i1=WKI; 1639 for (d=1;d<8;d++) { 1640 j1+=dj; i1+=di; 1641 if (getpole(j1,i1)) 1642 { 1643 if (pole[j1][i1]==-fq||pole[j1][i1]==-fb) return true; 1644 1645 if (pole[j1][i1]!=0) break; 1646 } else break; 1647 } 1648 return 1649 false; 1650} 1651//**************************** 1652boolean getdiagrowcheckb(char dj,char 1653 di) { // 1654char d,j1,i1; 1655 j1=BKJ; i1=BKI; 1656 for (d=1;d<8;d++) 1657 { 1658 j1+=dj; i1+=di; 1659 if (getpole(j1,i1)) { 1660 if (pole[j1][i1]==fq||pole[j1][i1]==fb) 1661 return true; 1662 if (pole[j1][i1]!=0) break; 1663 } else break; 1664 1665 } 1666 return false; 1667} 1668//**************************** 1669boolean getstreightrowcheckw(char 1670 dj,char di) { // - 1671char d,j1,i1; 1672 j1=WKJ; i1=WKI; 1673 for (d=1;d<8;d++) 1674 { 1675 j1+=dj; i1+=di; 1676 if (getpole(j1,i1)) { 1677 if 1678 (pole[j1][i1]==-fq||pole[j1][i1]==-fr) return true; 1679 if (pole[j1][i1]!=0) 1680 break; 1681 } else break; 1682 } 1683 return false; 1684} 1685//**************************** 1686boolean 1687 getstreightrowcheckb(char dj,char di) { // - 1688char d,j1,i1; 1689 j1=BKJ; 1690 i1=BKI; 1691 for (d=1;d<8;d++) { 1692 j1+=dj; i1+=di; 1693 if (getpole(j1,i1)) 1694 { 1695 if (pole[j1][i1]==fq||pole[j1][i1]==fr) return true; 1696 1697 if (pole[j1][i1]!=0) break; 1698 } else break; 1699 } 1700 return 1701 false; 1702} 1703//**************************** 1704boolean get_check(char king) { 1705 // 1706 if (king==fk) { // 1707 if (getdiagrowcheckw(-1, 1)) 1708 return true; 1709 if (getdiagrowcheckw(-1,-1)) return true; 1710 1711 if (getdiagrowcheckw( 1,-1)) return true; 1712 if (getdiagrowcheckw( 1, 1)) 1713 return true; 1714 if (getstreightrowcheckw(-1, 0)) return true; 1715 1716 if (getstreightrowcheckw( 0, 1)) return true; 1717 if (getstreightrowcheckw( 1718 0,-1)) return true; 1719 if (getstreightrowcheckw( 1, 0)) return true; 1720 1721 if (getpole(WKJ-2,WKI-1)&&pole[WKJ-2][WKI-1]==-fn) return true; 1722 1723 if (getpole(WKJ-2,WKI+1)&&pole[WKJ-2][WKI+1]==-fn) return true; 1724 1725 if (getpole(WKJ-1,WKI-2)&&pole[WKJ-1][WKI-2]==-fn) return true; 1726 1727 if (getpole(WKJ-1,WKI+2)&&pole[WKJ-1][WKI+2]==-fn) return true; 1728 1729 if (getpole(WKJ+1,WKI-2)&&pole[WKJ+1][WKI-2]==-fn) return true; 1730 1731 if (getpole(WKJ+1,WKI+2)&&pole[WKJ+1][WKI+2]==-fn) return true; 1732 1733 if (getpole(WKJ+2,WKI-1)&&pole[WKJ+2][WKI-1]==-fn) return true; 1734 1735 if (getpole(WKJ+2,WKI+1)&&pole[WKJ+2][WKI+1]==-fn) return true; 1736 1737 if (getpole(WKJ-1,WKI-1)&&pole[WKJ-1][WKI-1]==-fp) return true; 1738 if 1739 (getpole(WKJ-1,WKI+1)&&pole[WKJ-1][WKI+1]==-fp) return true; 1740 1741 } else { // 1742 if (getdiagrowcheckb( 1,-1)) return true; 1743 1744 if (getdiagrowcheckb( 1, 1)) return true; 1745 if (getdiagrowcheckb(-1, 1)) 1746 return true; 1747 if (getdiagrowcheckb(-1,-1)) return true; 1748 if (getstreightrowcheckb( 1749 1, 0)) return true; 1750 if (getstreightrowcheckb( 0, 1)) return true; 1751 1752 if (getstreightrowcheckb( 0,-1)) return true; 1753 if (getstreightrowcheckb(-1, 1754 0)) return true; 1755 if (getpole(BKJ+2,BKI-1)&&pole[BKJ+2][BKI-1]==fn) 1756 return true; 1757 if (getpole(BKJ+2,BKI+1)&&pole[BKJ+2][BKI+1]==fn) return 1758 true; 1759 if (getpole(BKJ+1,BKI-2)&&pole[BKJ+1][BKI-2]==fn) return 1760 true; 1761 if (getpole(BKJ+1,BKI+2)&&pole[BKJ+1][BKI+2]==fn) return true; 1762 1763 if (getpole(BKJ-1,BKI-2)&&pole[BKJ-1][BKI-2]==fn) return 1764 true; 1765 if (getpole(BKJ-1,BKI+2)&&pole[BKJ-1][BKI+2]==fn) return true; 1766 1767 if (getpole(BKJ-2,BKI-1)&&pole[BKJ-2][BKI-1]==fn) return true; 1768 1769 if (getpole(BKJ-2,BKI+1)&&pole[BKJ-2][BKI+1]==fn) return true; 1770 1771 if (getpole(BKJ+1,BKI-1)&&pole[BKJ+1][BKI-1]==fp) return true; 1772 if (getpole(BKJ+1,BKI+1)&&pole[BKJ+1][BKI+1]==fp) 1773 return true; 1774 } 1775 if (abs(BKJ-WKJ)<=1&&abs(BKI-WKI)<=1) return 1776 true; // 1777 return false; 1778} 1779//**************************** 1780boolean 1781 getpole(char j,char i) { // 1782 if (j>=0&&j<8&&i>=0&&i<8) return true; 1783 1784 return false; 1785} 1786//**************************** 1787void addstep(char 1788 j1,char i1,char j2,char i2,char type) { 1789 int st=start_var+cur_var; 1790 1791 steps[st].x1=j1; 1792 steps[st].x2=j2; 1793 steps[st].y1=i1; 1794 steps[st].y2=i2; 1795 1796 steps[st].fig1=pole[j1][i1]; 1797 steps[st].fig2=pole[j2][i2]; 1798 if (type==1) 1799 { // 1800 steps[st].fig2=-steps[st].fig1; 1801 } 1802 steps[st].type=type; 1803 1804 char ko=-fk; // 1805 if (steps[st].fig1>0) ko=fk; 1806 movestep(st); 1807 1808 if (get_check(ko)) { backstep(st); return; } // - 1809 boolean che=get_check(-ko); 1810 // 1811 backstep(st); 1812 steps[st].weight=abs(steps[st].fig2)-abs(steps[st].fig1); 1813 1814 if (type>3) steps[st].weight+=fig_weight[type-2]; 1815 if (endspiel&&steps[st].fig1==ko) 1816 steps[st].weight+=10; // - 1817 steps[st].check=che; 1818 if (che) steps[st].weight+=10; 1819 1820 if (only_action) { 1821 if (steps[st].fig1==fp&&steps[st].x2==1||steps[st].fig1==-fp&&steps[st].x2==6) 1822 // 1823 { cur_var++; return; } 1824 if (steps[st].fig2==0&&steps[st].type<4&&!che&&!check_on_table) 1825 return; 1826 } 1827 1828 cur_var++; 1829} 1830//**************************** 1831void 1832 getrowstepsw(char j,char i,char dj,char di) { // 1833char d,j1,i1; 1834 1835 j1=j; i1=i; 1836 for (d=1;d<8;d++) { 1837 j1+=dj; i1+=di; 1838 if (getpole(j1,i1)) 1839 { 1840 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1841 if (pole[j1][i1]!=0) 1842 break; 1843 } else break; 1844 } 1845} 1846//**************************** 1847void 1848 getrowstepsb(char j,char i,char dj,char di) { // 1849char d,j1,i1; 1850 j1=j; 1851 i1=i; 1852 for (d=1;d<8;d++) { 1853 j1+=dj; i1+=di; 1854 if (getpole(j1,i1)) 1855 { 1856 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1857 if (pole[j1][i1]!=0) 1858 break; 1859 } else break; 1860 } 1861} 1862//**************************** 1863void 1864 getonestepw(char j,char i,char dj,char di) { // 1865char j1,i1; 1866 j1=j+dj; 1867 i1=i+di; 1868 if (getpole(j1,i1)) 1869 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1870 1871} 1872//**************************** 1873void getonestepb(char j,char i,char 1874 dj,char di) { // 1875char j1,i1; 1876 j1=j+dj; i1=i+di; 1877 if (getpole(j1,i1)) 1878 1879 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1880} 1881//**************************** 1882void 1883 sort_variants(int from, int to) { // 1884 while (1) { 1885 int mov=0; 1886 1887 for (int i=from;i<to;i++) // 1888 if (steps[i].weight<steps[i+1].weight) 1889 { 1890 mov++; 1891 step_type buf=steps[i]; 1892 steps[i]=steps[i+1]; 1893 1894 steps[i+1]=buf; 1895 } 1896 if (mov==0) break; 1897 1898 } 1899} 1900//**************************** 1901void load_variants(int nstep) 1902 { // nstep 1903 cur_var=0; 1904 if (pole[WKJ][WKI]!=fk||pole[BKJ][BKI]!=-fk) 1905 kingpositions(); 1906 if (nstep%2==1) check_on_table=get_check(fk); 1907 else 1908 check_on_table=get_check(-fk); // 1909 for (char i=0;i<8;i++) 1910 for (char 1911 j=0;j<8;j++) 1912 if (pole[j][i]>0&&nstep%2==1||pole[j][i]<0&&nstep%2==0) { 1913 1914 switch (pole[j][i]) { 1915 case fp: // 1916 if (getpole(j-1,i)&&pole[j-1][i]==0) 1917 1918 if (j!=1) addstep(j,i,j-1,i,0); else 1919 for (char t=4;t<8;t++) 1920 addstep(1,i,0,i,t); //-... 1921 if (j==6&&pole[j-1][i]==0&&pole[j-2][i]==0) 1922 addstep(j,i,j-2,i,0); 1923 if (getpole(j-1,i-1)&&pole[j-1][i-1]<0) 1924 if 1925 (j!=1) addstep(j,i,j-1,i-1,0); else 1926 for (char t=4;t<8;t++) addstep(j,i,j-1,i-1,t); 1927 //-... 1928 if (getpole(j-1,i+1)&&pole[j-1][i+1]<0) 1929 1930 if (j!=1) addstep(j,i,j-1,i+1,0); else 1931 for (char t=4;t<8;t++) 1932 addstep(j,i,j-1,i+1,t); //-... 1933 if 1934 (j==3&&steps[nstep-1].fig1==-fp&&steps[nstep-1].x2==3&&steps[nstep-1].x1==1) { 1935 1936 if (steps[nstep-1].y2-i==1) { // 1937 addstep(j,i,j-1,i+1,1); 1938 1939 } else if (steps[nstep-1].y2-i==-1) { // 1940 addstep(j,i,j-1,i-1,1); 1941 1942 } 1943 } 1944 break; 1945 case -fp: // 1946 1947 if (getpole(j+1,i)&&pole[j+1][i]==0) 1948 if (j!=6) addstep(j,i,j+1,i,0); 1949 else 1950 for (char t=4;t<8;t++) addstep(j,i,j+1,i,t); //-... 1951 1952 if (j==1&&pole[j+1][i]==0&&pole[j+2][i]==0) addstep(j,i,j+2,i,0); 1953 if 1954 (getpole(j+1,i-1)&&pole[j+1][i-1]>0) 1955 if (j!=6) addstep(j,i,j+1,i-1,0); 1956 else 1957 for (char t=4;t<8;t++) addstep(j,i,j+1,i-1,t); //-... 1958 1959 if (getpole(j+1,i+1)&&pole[j+1][i+1]>0) 1960 if (j!=6) addstep(j,i,j+1,i+1,0); 1961 else 1962 for (char t=4;t<8;t++) addstep(j,i,j+1,i+1,t); //-... 1963 1964 if (j==4&&steps[nstep-1].fig1==fp&&steps[nstep-1].x2==4&&steps[nstep-1].x1==6) 1965 { 1966 if (steps[nstep-1].y2-i==1) 1967 { // 1968 addstep(j,i,j+1,i+1,1); 1969 } 1970 else if (steps[nstep-1].y2-i==-1) { // 1971 addstep(j,i,j+1,i-1,1); 1972 1973 } 1974 } 1975 break; 1976 case fn: // 1977 getonestepw(j,i,-2,-1); 1978 1979 getonestepw(j,i,-2,1); 1980 getonestepw(j,i,-1,-2); 1981 getonestepw(j,i,-1,2); 1982 1983 getonestepw(j,i,2,-1); 1984 getonestepw(j,i,2,1); 1985 getonestepw(j,i,1,-2); 1986 1987 getonestepw(j,i,1,2); 1988 break; 1989 case -fn: // 1990 1991 getonestepb(j,i,-2,-1); 1992 getonestepb(j,i,-2,1); 1993 getonestepb(j,i,-1,-2); 1994 1995 getonestepb(j,i,-1,2); 1996 getonestepb(j,i,2,-1); 1997 getonestepb(j,i,2,1); 1998 1999 getonestepb(j,i,1,-2); 2000 getonestepb(j,i,1,2); 2001 break; 2002 2003 case fb: // 2004 getrowstepsw(j,i,1,1); 2005 getrowstepsw(j,i,-1,-1); 2006 2007 getrowstepsw(j,i,1,-1); 2008 getrowstepsw(j,i,-1,1); 2009 break; 2010 2011 case -fb: // 2012 getrowstepsb(j,i,1,1); 2013 getrowstepsb(j,i,-1,-1); 2014 2015 getrowstepsb(j,i,1,-1); 2016 getrowstepsb(j,i,-1,1); 2017 break; 2018 2019 case fr: // 2020 getrowstepsw(j,i,1,0); 2021 getrowstepsw(j,i,-1,0); 2022 2023 getrowstepsw(j,i,0,1); 2024 getrowstepsw(j,i,0,-1); 2025 break; 2026 2027 case -fr: // 2028 getrowstepsb(j,i,1,0); 2029 getrowstepsb(j,i,-1,0); 2030 2031 getrowstepsb(j,i,0,1); 2032 getrowstepsb(j,i,0,-1); 2033 break; 2034 2035 case fq: // 2036 getrowstepsw(j,i,1,1); 2037 getrowstepsw(j,i,-1,-1); 2038 2039 getrowstepsw(j,i,1,-1); 2040 getrowstepsw(j,i,-1,1); 2041 getrowstepsw(j,i,1,0); 2042 2043 getrowstepsw(j,i,-1,0); 2044 getrowstepsw(j,i,0,1); 2045 getrowstepsw(j,i,0,-1); 2046 2047 break; 2048 case -fq: // 2049 getrowstepsb(j,i,1,1); 2050 2051 getrowstepsb(j,i,-1,-1); 2052 getrowstepsb(j,i,1,-1); 2053 getrowstepsb(j,i,-1,1); 2054 2055 getrowstepsb(j,i,1,0); 2056 getrowstepsb(j,i,-1,0); 2057 getrowstepsb(j,i,0,1); 2058 2059 getrowstepsb(j,i,0,-1); 2060 break; 2061 case fk: // 2062 getonestepw(j,i, 2063 1,-1); 2064 getonestepw(j,i, 1, 0); 2065 getonestepw(j,i, 1, 1); 2066 2067 getonestepw(j,i, 0,-1); 2068 getonestepw(j,i, 2069 0, 1); 2070 getonestepw(j,i,-1,-1); 2071 getonestepw(j,i,-1, 0); 2072 2073 getonestepw(j,i,-1, 1); 2074 if (!check_on_table) add_rok(j,i,nstep); 2075 2076 break; 2077 case -fk: // 2078 getonestepb(j,i, 1,-1); 2079 2080 getonestepb(j,i, 1, 0); 2081 getonestepb(j,i, 1, 1); 2082 getonestepb(j,i, 2083 0,-1); 2084 getonestepb(j,i, 0, 1); 2085 getonestepb(j,i,-1,-1); 2086 2087 getonestepb(j,i,-1, 0); 2088 getonestepb(j,i,-1, 1); 2089 2090 if (!check_on_table) add_rok(j,i,nstep); 2091 break; 2092 } //switch 2093 2094 } //if 2095 2096 if (nstep>cur_step) { 2097 for (int i=start_var;i<start_var+cur_var;i++) 2098 { // 2099 for (char j=0;j<MAXCUTS;j++) 2100 if (cuts[j].fig1==steps[i].fig1&& 2101 2102 cuts[j].x1==steps[i].x1&&cuts[j].y1==steps[i].y1&& 2103 cuts[j].x2==steps[i].x2&&cuts[j].y2==steps[i].y2) 2104 { 2105 steps[i].weight+=100+cuts[j].weight; 2106 break; 2107 2108 } 2109 if (nstep>1&&steps[nstep-1].fig2!=0) // - 2110 if 2111 (steps[nstep-1].x2==steps[i].x2&&steps[nstep-1].y2==steps[i].y2) 2112 steps[i].weight+=1000; 2113 2114 } 2115 } 2116 2117 sort_variants(start_var,start_var+cur_var-1); 2118 2119 2120 } 2121//**************************** 2122void movestep(int nstep) 2123 { 2124 pole[steps[nstep].x1][steps[nstep].y1]=0; 2125 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig1; 2126 2127 if (steps[nstep].fig1==fk) { 2128 WKJ=steps[nstep].x2; WKI=steps[nstep].y2; 2129 2130 } else if (steps[nstep].fig1==-fk) { 2131 BKJ=steps[nstep].x2; BKI=steps[nstep].y2; 2132 2133 } 2134 if (steps[nstep].type==0) return; 2135 if (steps[nstep].type==1) // 2136 2137 if (steps[nstep].fig1>0) 2138 pole[steps[nstep].x2+1][steps[nstep].y2]=0; 2139 2140 else 2141 pole[steps[nstep].x2-1][steps[nstep].y2]=0; 2142 else if (steps[nstep].type==2) 2143 // 2144 if (steps[nstep].fig1>0) { // 2145 pole[7][4]=0; pole[7][5]=fr; 2146 pole[7][6]=fk; pole[7][7]=0; //show_board(); delay(3000); 2147 } else { // 2148 2149 pole[0][4]=0; pole[0][5]=-fr; pole[0][6]=-fk; pole[0][7]=0; //show_board(); 2150 delay(3000); 2151 } 2152 else if (steps[nstep].type==3) // 2153 if (steps[nstep].fig1>0) 2154 { // 2155 pole[7][0]=0; pole[7][1]=0; pole[7][2]=fk; pole[7][3]=fr; pole[7][4]=0; 2156 2157 } else { // 2158 pole[0][0]=0; pole[0][1]=0; pole[0][2]=-fk; pole[0][3]=-fr; 2159 pole[0][4]=0; 2160 } 2161 else if (steps[nstep].type>3) //-.... 2162 if 2163 (steps[nstep].fig1>0) pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].type-2; 2164 2165 else pole[steps[nstep].x2][steps[nstep].y2]=2-steps[nstep].type; 2166} 2167//**************************** 2168 2169void backstep(int nstep) { 2170 pole[steps[nstep].x1][steps[nstep].y1]=steps[nstep].fig1; 2171 2172 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig2; 2173 if (steps[nstep].fig1==fk) 2174 { 2175 WKJ=steps[nstep].x1; WKI=steps[nstep].y1; 2176 } else if (steps[nstep].fig1==-fk) 2177 { 2178 BKJ=steps[nstep].x1; BKI=steps[nstep].y1; 2179 } 2180 if (steps[nstep].type==0) 2181 return; 2182 if (steps[nstep].type==1) { // 2183 pole[steps[nstep].x2][steps[nstep].y2]=0; 2184 2185 if (steps[nstep].fig1>0) 2186 pole[steps[nstep].x2+1][steps[nstep].y2]=-fp; 2187 2188 else 2189 pole[steps[nstep].x2-1][steps[nstep].y2]=fp; 2190 } else 2191 if (steps[nstep].type==2) // 2192 if (steps[nstep].fig1>0) { // 2193 pole[7][4]=fk; 2194 pole[7][5]=0; pole[7][6]=0; pole[7][7]=fr; 2195 } else { // 2196 pole[0][4]=-fk; 2197 pole[0][5]=0; pole[0][6]=0; pole[0][7]=-fr; // show_board(); delay(3000); 2198 } 2199 2200 else if (steps[nstep].type==3) // 2201 if (steps[nstep].fig1>0) { // 2202 pole[7][0]=fr; 2203 pole[7][1]=0; pole[7][2]=0; pole[7][3]=0; pole[7][4]=fk; 2204 } else { // 2205 2206 pole[0][0]=-fr; pole[0][1]=0; pole[0][2]=0; pole[0][3]=0; pole[0][4]=-fk; 2207 2208 } 2209 2210} 2211//**************************** 2212void get_wrocks(int nstep) 2213 { 2214 w00=true; w000=true; 2215 for (int i=1;i<nstep;i++) { 2216 if (steps[i].fig1==fk) 2217 { w00=false; w000=false; return; } 2218 if (steps[i].fig1==fr) 2219 if (steps[i].x1==7&&steps[i].y1==7) 2220 w00=false; 2221 else if (steps[i].x1==7&&steps[i].y1==0) w000=false; 2222 } 2223} 2224//**************************** 2225void 2226 get_brocks(int nstep) { 2227 b00=true; b000=true; 2228 for (int i=1;i<nstep;i++) 2229 { 2230 if (steps[i].fig1==-fk) { b00=false; b000=false; return; } 2231 if (steps[i].fig1==-fr) 2232 2233 if (steps[i].x1==0&&steps[i].y1==7) b00=false; 2234 else if (steps[i].x1==0&&steps[i].y1==0) 2235 b000=false; 2236 } 2237} 2238//**************************** 2239void add_rok(char 2240 j,char i,int nstep) { // 2241boolean che1,che2; 2242 if (nstep%2==1) { // 2243 2244 if (j!=7||i!=4) return; 2245 if (pole[7][5]==0&&pole[7][6]==0&&pole[7][7]==fr) 2246 { // 2247 pole[7][4]=0; 2248 WKI=5; che1=get_check(fk); 2249 WKI=6; che2=get_check(fk); 2250 2251 WKI=4; pole[7][4]=fk; 2252 get_wrocks(nstep); 2253 if (!che1&&!che2&&w00) 2254 addstep(7,4,7,6,2); 2255 } 2256 if (pole[7][0]==fr&&pole[7][1]==0&&pole[7][2]==0&&pole[7][3]==0) 2257 { // 2258 pole[7][4]=0; 2259 WKI=2; che1=get_check(fk); 2260 WKI=3; che2=get_check(fk); 2261 2262 WKI=4; pole[7][4]=fk; 2263 get_wrocks(nstep); 2264 if (!che1&&!che2&&w000) 2265 addstep(7,4,7,2,3); 2266 } 2267 } else { // 2268 if (j!=0||i!=4) return; 2269 if 2270 (pole[0][5]==0&&pole[0][6]==0&&pole[0][7]==-fr) { // 2271 pole[0][4]=0; 2272 2273 BKI=5; che1=get_check(-fk); 2274 BKI=6; che2=get_check(-fk); 2275 BKI=4; 2276 pole[0][4]=-fk; 2277 get_brocks(nstep); 2278 if (!che1&&!che2&&b00) addstep(0,4,0,6,2); 2279 2280 } 2281 if (pole[0][0]==-fr&&pole[0][1]==0&&pole[0][2]==0&&pole[0][3]==0) { // 2282 2283 pole[0][4]=0; 2284 BKI=2; che1=get_check(-fk); 2285 BKI=3; che2=get_check(-fk); 2286 2287 BKI=4; pole[0][4]=-fk; 2288 get_brocks(nstep); 2289 if (!che1&&!che2&&b000) 2290 addstep(0,4,0,2,3); 2291 } 2292 2293 } 2294} 2295//**************************** 2296int 2297 addrowstepsw(char j,char i,char dj,char di,char dc) { // 2298char d,j1,i1; 2299 int c=0; 2300 j1=j; i1=i; 2301 for (d=1;d<8;d++) { 2302 j1+=dj; 2303 i1+=di; 2304 if (getpole(j1,i1)) { 2305 if (pole[j1][i1]==0) c+=dc; 2306 2307 else if (pole[j1][i1]>0) { // 2308 c+=1; break; 2309 } 2310 else { 2311 if (cur_step>6) c+=-pole[j1][i1]; // 2312 2313 break; 2314 } 2315 } else break; 2316 } 2317 return c; 2318} 2319//**************************** 2320int 2321 addrowstepsb(char j,char i,char dj,char di,char dc) { // 2322char d,j1,i1; int 2323 c=0; 2324 j1=j; i1=i; 2325 for (d=1;d<8;d++) { 2326 j1+=dj; i1+=di; 2327 2328 if (getpole(j1,i1)) { 2329 if (pole[j1][i1]==0) c-=dc; 2330 else 2331 if (pole[j1][i1]<0) { // 2332 c-=1; break; 2333 } else { 2334 2335 if (cur_step>6) c-=pole[j1][i1]; // 2336 break; 2337 2338 } 2339 } else break; 2340 } 2341 return c; 2342} 2343//**************************** 2344int 2345 addonestepw(char j,char i,char dj,char di) { // 2346char j1,i1; 2347 j1=j+dj; 2348 i1=i+di; 2349 if (getpole(j1,i1)) 2350 if (pole[j1][i1]==0) return 2; 2351 2352 else if (pole[j1][i1]>0) return 1; 2353 else return -pole[j1][i1]; 2354 2355 return 0; 2356} 2357//**************************** 2358int addonestepb(char j,char 2359 i,char dj,char di) { // 2360char j1,i1; 2361 j1=j+dj; i1=i+di; 2362 if (getpole(j1,i1)) 2363 2364 if (pole[j1][i1]==0) return 2; 2365 else if (pole[j1][i1]<0) return 2366 1; 2367 else return pole[j1][i1]; 2368 return 0; 2369} 2370//**************************** 2371int 2372 activity() { // - + 2373int c=0; 2374char pwj[8],pwi[8],pbj[8],pbi[8],ipw=0,ipb=0,nbw=0,nbb=0; 2375 2376 if (checkmatesearch) return 0; // 2377 for (char i=0;i<8;i++) 2378 for 2379 (char j=0;j<8;j++) 2380 if (pole[j][i]!=0) { 2381 switch (pole[j][i]) { 2382 2383 case fp: // 2384 if (getpole(j,i+1)&&pole[j][i+1]==fp) c+=2; // 2385 2386 if (getpole(j-1,i)&&pole[j-1][i]==fp) c-=20; // 2387 pwj[ipw]=j; pwi[ipw]=i; 2388 ipw++; 2389 break; 2390 case -fp: // 2391 if (getpole(j,i-1)&&pole[j][i-1]==-fp) 2392 c-=2; // 2393 if (getpole(j+1,i)&&pole[j+1][i]==-fp) c+=20; // 2394 pbj[ipb]=j; 2395 pbi[ipb]=i; ipb++; 2396 break; 2397 case fn: // 2398 c+=addonestepw(j,i,-2,-1); 2399 2400 c+=addonestepw(j,i,-2,1); 2401 c+=addonestepw(j,i,-1,-2); 2402 c+=addonestepw(j,i,-1,2); 2403 2404 c+=addonestepw(j,i,2,-1); 2405 c+=addonestepw(j,i,2,1); 2406 c+=addonestepw(j,i,1,-2); 2407 2408 c+=addonestepw(j,i,1,2); 2409 break; 2410 case -fn: // 2411 2412 c+=addonestepb(j,i,-2,-1); 2413 c+=addonestepb(j,i,-2,1); 2414 c+=addonestepb(j,i,-1,-2); 2415 2416 c+=addonestepb(j,i,-1,2); 2417 c+=addonestepb(j,i,2,-1); 2418 c+=addonestepb(j,i,2,1); 2419 2420 c+=addonestepb(j,i,1,-2); 2421 c+=addonestepb(j,i,1,2); 2422 break; 2423 2424 case fb: // 2425 c+=addrowstepsw(j,i,1,1,1); 2426 c+=addrowstepsw(j,i,-1,-1,2); 2427 2428 c+=addrowstepsw(j,i,1,-1,1); 2429 c+=addrowstepsw(j,i,-1,1,2); 2430 2431 nbw++; 2432 break; 2433 case -fb: // 2434 c+=addrowstepsb(j,i,1,1,2); 2435 2436 c+=addrowstepsb(j,i,-1,-1,1); 2437 c+=addrowstepsb(j,i,1,-1,2); 2438 c+=addrowstepsb(j,i,-1,1,1); 2439 2440 nbb++; 2441 break; 2442 case fr: // 2443 c+=addrowstepsw(j,i,1,0,2); 2444 // 2445 c+=addrowstepsw(j,i,-1,0,2); 2446 c+=addrowstepsw(j,i,0,1,1); 2447 2448 c+=addrowstepsw(j,i,0,-1,1); 2449 break; 2450 case -fr: // 2451 2452 c+=addrowstepsb(j,i,1,0,2); // 2453 c+=addrowstepsb(j,i,-1,0,2); 2454 c+=addrowstepsb(j,i,0,1,1); 2455 2456 c+=addrowstepsb(j,i,0,-1,1); 2457 break; 2458 case fq: // 2459 if 2460 (cur_step>10) { 2461 c+=addrowstepsw(j,i,1,1,1); 2462 c+=addrowstepsw(j,i,-1,-1,2); 2463 2464 c+=addrowstepsw(j,i,1,-1,1); 2465 c+=addrowstepsw(j,i,-1,1,2); 2466 2467 c+=addrowstepsw(j,i,1,0,1); 2468 c+=addrowstepsw(j,i,-1,0,2); 2469 c+=addrowstepsw(j,i,0,1,1); 2470 2471 c+=addrowstepsw(j,i,0,-1,1); 2472 } 2473 break; 2474 case 2475 -fq: // 2476 if (cur_step>10) { 2477 c+=addrowstepsb(j,i,1,1,2); 2478 2479 c+=addrowstepsb(j,i,-1,-1,1); 2480 c+=addrowstepsb(j,i,1,-1,2); 2481 2482 c+=addrowstepsb(j,i,-1,1,1); 2483 c+=addrowstepsb(j,i,1,0,2); 2484 2485 c+=addrowstepsb(j,i,-1,0,1); 2486 c+=addrowstepsb(j,i,0,1,1); 2487 c+=addrowstepsb(j,i,0,-1,1); 2488 2489 } 2490 break; 2491 } //switch 2492 if (cur_step>6) { 2493 2494 if (pole[j][i]>0) { // 2495 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c+=3; 2496 2497 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c+=3; 2498 if (abs(WKJ-j)<3&&abs(WKI-i)<3) 2499 c+=2; 2500 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c+=2; 2501 } else { // 2502 2503 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c-=3; 2504 if (abs(BKJ-j)<2&&abs(BKI-i)<2) 2505 c-=3; 2506 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c-=2; 2507 if (abs(BKJ-j)<3&&abs(BKI-i)<3) 2508 c-=2; 2509 } 2510 } 2511 } 2512 if (nbw>1) c+=30; // 2513 if 2514 (nbb>1) c-=30; 2515 for (char w=0;w<ipw;w++) { // 2516 boolean pass=1; 2517 2518 for (char b=0;b<ipb;b++) { 2519 if (pwi[w]>0&&pbi[b]==pwi[w]-1&&pbj[b]<pwj[w]) 2520 pass=0; // 2521 if (pbi[b]==pwi[w]&&pbj[b]<pwj[w]) pass=0; // 2522 if 2523 ( pwi[w]<7&&pbi[b]==pwi[w]+1&&pbj[b]<pwj[w]) pass=0; // 2524 if (!pass) 2525 break; 2526 } 2527 if (pass) { 2528 c+=50; 2529 if (!endspiel) 2530 break; // 2531 } 2532 } 2533 for (char b=0;b<ipb;b++) { // 2534 boolean 2535 pass=1; 2536 for (char w=0;w<ipw;w++) { 2537 if (pbi[b]>0&&pwi[w]==pbi[b]-1&&pwj[w]>pbj[b]) 2538 pass=0; // 2539 if (pwi[w]==pbi[b]&&pwj[w]>pbj[b]) pass=0; // 2540 if 2541 (pbi[b]<7&&pwi[w]==pbi[b]+1&&pwj[w]>pbj[b]) pass=0; // 2542 if (!pass) 2543 break; 2544 } 2545 if (pass) { 2546 c-=50; 2547 if (!endspiel) 2548 break; // 2549 } 2550 } 2551 return c; 2552} 2553//**************************** 2554int 2555 evaluate(int nstep) { // 2556 long ww=0, wb=0; 2557 for (char i=0;i<8;i++) 2558 2559 for (char j=0;j<8;j++) 2560 if (pole[j][i]<0) { 2561 wb+=fig_weight[-pole[j][i]]+(int)pgm_read_word(&pos[-pole[j][i]-1][7-j][i]); 2562 2563 } 2564 else if (pole[j][i]>0) { 2565 ww+=fig_weight[pole[j][i]]+(int)pgm_read_word(&pos[pole[j][i]-1][j][i]); 2566 2567 } 2568 count++; // 2569 2570 long str=activity(); 2571 if (endspiel) { // 2572 wb+=(int)pgm_read_word(&pos[6][7-BKJ][BKI])-(int)pgm_read_word(&pos[5][7-BKJ][BKI]); 2573 2574 ww+=(int)pgm_read_word(&pos[6][WKJ][WKI])-(int)pgm_read_word(&pos[5][WKJ][WKI]); 2575 2576 if (wb<450&&ww>450) { // - - 2577 2578 str-=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 2579 } else if (ww<450&&wb>450) { // 2580 2581 str+=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 2582 } 2583 } 2584 if (nstep>8) { // 2585 2586 if (steps[nstep-1].fig1==steps[nstep-5].fig1&&steps[nstep-2].fig1==steps[nstep-6].fig1&& 2587 2588 steps[nstep-3].fig1==steps[nstep-7].fig1&&steps[nstep-4].fig1==steps[nstep-8].fig1) 2589 2590 if (steps[nstep-1].x1==steps[nstep-5].x1&&steps[nstep-2].x1==steps[nstep-6].x1&& 2591 2592 steps[nstep-3].x1==steps[nstep-7].x1&&steps[nstep-4].x1==steps[nstep-8].x1) 2593 2594 if (steps[nstep-1].x2==steps[nstep-5].x2&&steps[nstep-2].x2==steps[nstep-6].x2&& 2595 2596 steps[nstep-3].x2==steps[nstep-7].x2&&steps[nstep-4].x2==steps[nstep-8].x2) 2597 2598 if (steps[nstep-1].y1==steps[nstep-5].y1&&steps[nstep-2].y1==steps[nstep-6].y1&& 2599 2600 steps[nstep-3].y1==steps[nstep-7].y1&&steps[nstep-4].y1==steps[nstep-8].x1) 2601 2602 if (steps[nstep-1].y2==steps[nstep-5].y2&&steps[nstep-2].y2==steps[nstep-6].y2&& 2603 2604 steps[nstep-3].y2==steps[nstep-7].y2&&steps[nstep-4].y2==steps[nstep-8].y2) 2605 2606 { Serial.println(F(" Draw - 3 repeat")); return 0; } 2607 } 2608 2609 if (nstep%2==1) return 5000*(ww-wb)/(ww+wb+2000)+str; else return 5000*(wb-ww)/(ww+wb+2000)-str; 2610 2611} 2612//**************************** 2613void add_cut(int ind) {// ind 2614 2615 int minbeta=30000, minindex=MAXCUTS-1; 2616 for (char i=0;i<MAXCUTS;i++) { 2617 2618 if (cuts[i].weight==0) { minindex=i; break; }// 2619 if (cuts[i].fig1==steps[ind].fig1&& 2620 2621 cuts[i].x1==steps[ind].x1&&cuts[i].y1==steps[ind].y1&& 2622 cuts[i].x2==steps[ind].x2&&cuts[i].y2==steps[ind].y2) 2623 { 2624 cuts[i].weight++; return; // - 1 2625 } 2626 if (cuts[i].weight<minbeta) 2627 { // 2628 minbeta=cuts[i].weight; 2629 minindex=i; 2630 } 2631 } 2632 2633 cuts[minindex]=steps[ind]; 2634 cuts[minindex].weight=1; //1 2635} 2636//**************************** 2637 2638int quiescence(int start, int nstep, int alpha, int beta ) { 2639 if 2640 (nstep-cur_step>=LIMDEPTH||start>MAXSTEPS-70) return evaluate(nstep); 2641 2642 if (!solving&&progress==0) return -5000; 2643 int score=-20000; 2644 2645 start_var=start; 2646 only_action=true; 2647 load_variants(nstep); 2648 2649 if (!check_on_table) { 2650 int stand_pat = evaluate(nstep); 2651 2652 if (stand_pat >= score) score=stand_pat; 2653 if (score>alpha) alpha=score; 2654 2655 if (alpha>=beta) return alpha; 2656 } 2657 if 2658 (cur_var==0) { 2659 if (check_on_table) { 2660 if (TRACE) Serial.println(F("checkmate?")); 2661 2662 return -10000+nstep-cur_step; 2663 2664 } else return evaluate(nstep); 2665 } 2666 int j=start+cur_var; 2667 2668 for (int i=start;i<j;i++) { // 2669 if (TRACE) { //***** 2670 2671 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" . ")); 2672 2673 Serial.println(str_step(i)); 2674 } 2675 movestep(i); 2676 2677 steps[nstep]=steps[i]; 2678 int tmp=-quiescence(j+1,nstep+1,-beta,-alpha); 2679 2680 backstep(i); 2681 if (tmp>score) score=tmp; 2682 2683 if (score>alpha) alpha=score; 2684 if (alpha>=beta ) { 2685 2686 add_cut(nstep); 2687 return alpha; 2688 } 2689 2690 if (checkmatesearch&&alpha>9000) break; 2691 gui(); 2692 2693 } 2694 return score; 2695} 2696//**************************** 2697int alphaBeta(int 2698 start, int nstep, int alpha, int beta, int depthleft) { 2699//start - 2700//nstep 2701 - 2702int score=-20000,best; 2703 if( depthleft==0) return quiescence(start,nstep,alpha,beta) 2704 ; 2705 if (start>MAXSTEPS-70) return evaluate(nstep); 2706 start_var=start; 2707 2708 only_action=false; 2709 if (nstep!=cur_step) load_variants(nstep); 2710 2711 if (cur_var==0) { 2712 if (check_on_table) { 2713 if (TRACE) 2714 Serial.println(F("checkmate!")); 2715 return -10000+nstep-cur_step; 2716 2717 } 2718 return 0; 2719 } 2720 int j=start+cur_var; 2721 2722 best=start; 2723 for (int i=start;i<j;i++) { // 2724 2725 if (nstep==cur_step) { 2726 Serial.print(str_step(i)); 2727 Serial.print(" "); 2728 Serial.print(i-start+1); Serial.print("/"); Serial.print(j-start); 2729 2730 if (steps[i].weight<-9000) { Serial.println(F(" 2731 checkmate")); continue; } 2732 if (steps[i].fig2!=0||steps[i].check||alpha<-100) 2733 2734 // , , -100 2735 { LIMDEPTH=MAXDEPTH+2; Serial.print(F("+2")); 2736 } else LIMDEPTH=MAXDEPTH; 2737 } else { 2738 if (TRACE) { //***** 2739 2740 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" ")); 2741 2742 Serial.println(str_step(i)); 2743 } 2744 } 2745 2746 movestep(i); 2747 steps[nstep]=steps[i]; 2748 int tmp=-alphaBeta(j+1,nstep+1,-beta,-alpha,depthleft-1); 2749 2750 backstep(i); 2751 steps[i].weight=tmp; 2752 if (tmp>score) 2753 score=tmp; 2754 if (score>alpha) { 2755 alpha=score; 2756 if (nstep>cur_step) 2757 add_cut(nstep); // 2758 if (TRACE) { Serial.print(F("ALPHA+:")); 2759 Serial.println(score); } 2760 best=i; 2761 if (nstep==cur_step) 2762 { 2763 steps[0]=steps[best]; 2764 Serial.print(F(" BEST")); 2765 2766 } 2767 } 2768 if (alpha>=beta ) { 2769 if (nstep>cur_step) 2770 add_cut(nstep); // 2771 if (TRACE) { Serial.print(F("BETA 2772 CUT:")); Serial.println(score); } 2773 return alpha; 2774 } 2775 2776 if (nstep==cur_step) { 2777 Serial.print(F(" ")); 2778 Serial.println(tmp); 2779 progress=100*(i-start+1)/(j-start); 2780 2781 if (alpha==9999||alpha==-5000) break; 2782 if 2783 (checkmatesearch&&alpha>9000&&limit<3) break; 2784 if (!solving) { 2785 2786 if (alpha>startweight&&cur_level>1) break; 2787 else { // - 2788 +50% 2789 if (BAction.bs==F("stopping")) break; 2790 2791 if (cur_level>1&&100*(millis()-starttime)/(limittime-starttime)>240-limit*20) 2792 break; 2793 } 2794 } 2795 } 2796 2797 if (nstep==cur_step+1&&!solving&&progress==0) break; 2798 gui(); 2799 2800 } 2801 if (nstep==cur_step) { steps[nstep]=steps[best]; 2802 steps[0]=steps[best]; } 2803 return score; 2804} 2805//**************************** 2806 2807void kingpositions() { 2808 for (char i=0;i<8;i++) // 2809 for (char j=0;j<8;j++) 2810 2811 if (pole[j][i]==fk) { 2812 WKJ=j; WKI=i; 2813 } else if (pole[j][i]==-fk) 2814 { 2815 BKJ=j; BKI=i; 2816 } 2817} 2818//**************************** 2819int 2820 get_endspiel() { // 2821 int weight=0; 2822 for (char i=0;i<8;i++) 2823 for 2824 (char j=0;j<8;j++) 2825 if (pole[j][i]<0) 2826 weight+=fig_weight[-pole[j][i]]; 2827 2828 else if (pole[j][i]>0) 2829 weight+=fig_weight[pole[j][i]]; 2830 // 8000 2831 if (weight<3500) endspiel=true; else endspiel=false; 2832 return 2833 weight; 2834} 2835//**************************** 2836boolean is_drawn() { // 2837 2838 boolean drawn=false; 2839 int cn=0,cbw=0,cbb=0,co=0,cb=0,cw=0; 2840 for (char 2841 i=0;i<8;i++) 2842 for (char j=0;j<8;j++) { 2843 if (abs(pole[j][i])==1) co++; 2844 2845 if (abs(pole[j][i])>3&&abs(pole[j][i])<6) co++; // , , 2846 if (abs(pole[j][i])==6) 2847 continue; // 2848 if (abs(pole[j][i])==2) cn++; // 2849 if (abs(pole[j][i])==3&&(i+j+2)%2==0) 2850 cbb++; // 2851 if (abs(pole[j][i])==3&&(i+j+2)%2==1) cbw++; // 2852 if 2853 (pole[j][i]==3) cw++; // 2854 if (pole[j][i]==-3) cb++; // 2855 } 2856 2857 if (cn==1&&co+cbb+cbw==0) drawn=true; // 2858 if (cbb+cbw==1&&co+cn==0) drawn=true; 2859 // 2860 if (co+cn+cbb==0||co+cn+cbw==0) drawn=true; // 2861 if (co+cn==0&&cb==1&&cw==1) 2862 drawn=true; // 2863 if (drawn) return drawn; 2864 int rep=1; 2865 for (int s=cur_step-1; 2866 s>0; s--) { 2867 if (steps[s].fig1==0) continue; 2868 backstep(s); 2869 boolean 2870 eq=true; 2871 for (char i=0;i<8;i++) 2872 for (char j=0;j<8;j++) 2873 if 2874 (pole[j][i]!=pole0[j][i]) 2875 { eq=false; break; } 2876 if (eq) rep++; 2877 2878 if (rep>2) break; 2879 } 2880 if (rep>1) { Serial.print(rep); Serial.println(" 2881 repetitions"); } 2882 for (char i=0;i<8;i++) 2883 for (char j=0;j<8;j++) pole[j][i]=pole0[j][i]; 2884 // 2885 if (rep>2) drawn=true; 2886 return drawn; 2887} 2888//**************************** 2889 2890int solve_step() { 2891const int LMIN[6]={2,3,4,5,6,7}; 2892const int LMAX[6]={4,6,8,10,12,14}; 2893const 2894 int LCMIN[6]={2,4,6,8,10,12}; 2895const int LCMAX[6]={4,6,8,10,12,14}; 2896boolean 2897 check_on; 2898 starttime=millis(); 2899 isstatus=1; 2900 deletebuttons(); 2901 2902 clearstatus(); 2903 tft.drawFastHLine(10,312,300,DARK); 2904 tft.drawFastHLine(10,315,300,DARK); 2905 2906 BAction.Show("STOP"); 2907 BBW.Hide(); 2908 BBack.Hide(); 2909 BLimit.Hide(); 2910 2911 BCM.Hide(); 2912 BDesc.Hide(); 2913 BSound.Hide(); 2914 BRotate.Hide(); 2915 2916 limittime=starttime+limits[limit]*1000; // 2917 for (char i=0;i<8;i++) 2918 2919 for (char j=0;j<8;j++) pole0[j][i]=pole[j][i]; // 2920 lastbest.fig1=0; 2921 2922 kingpositions(); 2923 int wei=get_endspiel(); 2924 count=0; 2925 startweight=evaluate(cur_step); 2926 2927 Serial.println(""); 2928 Serial.println(F("---------------")); 2929 if (endspiel) 2930 { Serial.print(F("Endspiel: ")); Serial.println(wei); } 2931 if (cur_step%2==1) 2932 Serial.print(F("WHITE, ")); else Serial.print(F("BLACK, ")); 2933 Serial.print(F("start 2934 Score= ")); Serial.println(startweight); 2935 start_var=cur_step+21; 2936 only_action=false; 2937 2938 lastbest.fig1=0; 2939 steps[0].fig1=0; 2940 load_variants(cur_step); 2941 2942 check_on=check_on_table; 2943 if (cur_var==0||is_drawn()) { 2944 beep(500); 2945 2946 tft.drawFastHLine(10,312,300,BLACK); 2947 tft.drawFastHLine(10,315,300,BLACK); 2948 2949 BAction.Hide(); 2950 isstatus=0; 2951 solving=false; 2952 if (check_on_table) 2953 return -9999; else return 8999; 2954 } 2955 if (cur_var==1) { // 1 2956 tft.drawFastHLine(10,312,300,BLACK); 2957 2958 tft.drawFastHLine(10,315,300,BLACK); 2959 BAction.Hide(); 2960 isstatus=0; 2961 2962 solving=false; 2963 steps[cur_step]=steps[cur_step+21]; 2964 return startweight; 2965 2966 } 2967 int vars=cur_var; 2968 int ALPHA=-20000; 2969 int BETA=20000; 2970 2971 int score; 2972 solving=true; 2973 if (checkmatesearch) { 2974 Serial.println(F("Checkmate 2975 search")); 2976 steps[cur_step].fig1=0; lastscore=0; show_steps(); 2977 } 2978 int 2979 l=0; 2980 for (char i=0;i<MAXCUTS;i++) { cuts[i].weight=0; cuts[i].fig1=0; } // 2981 2982 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // 2983 movestep(i); 2984 steps[i].weight=evaluate(cur_step); 2985 2986 if (steps[i].fig2!=0) steps[i].weight-=steps[i].fig1; 2987 backstep(i); 2988 } 2989 2990 while (l<6) { 2991 cur_level=l+1; 2992 progress=0; 2993 tft.drawFastHLine(10,315,300,DARK); 2994 2995 MINDEPTH=LMIN[l]; 2996 MAXDEPTH=LMAX[l]; 2997 if (checkmatesearch) 2998 { 2999 ALPHA=9000; BETA=10000; 3000 MINDEPTH=LCMIN[l]; 3001 MAXDEPTH=LCMAX[l]; 3002 3003 } 3004 if (l>0) Serial.println(""); Serial.print(F("******* LEVEL=")); 3005 Serial.print(l+1); 3006 Serial.print(" "); Serial.print(MINDEPTH); Serial.print("-"); 3007 Serial.print(MAXDEPTH); 3008 Serial.print(" "); Serial.print((millis()-starttime)/1000.,1); 3009 Serial.println("s"); 3010 if (checkmatesearch) 3011 { 3012 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // - 3013 movestep(i); 3014 3015 start_var=cur_step+22+vars; 3016 only_action=false; 3017 load_variants(cur_step+1); 3018 3019 backstep(i); 3020 steps[i].weight=-cur_var; 3021 3022 } 3023 } 3024 sort_variants(cur_step+21,cur_step+20+vars); // 3025 3026 cur_var=vars; 3027 check_on_table=check_on; 3028 score=alphaBeta(cur_step+21,cur_step,ALPHA,BETA,MINDEPTH); 3029 3030 if (score>9996) break; 3031 if (score>9000&&limit<3) break; 3032 if 3033 (100*(millis()-starttime)/(limittime-starttime)>70-l*8) break; 3034 if 3035 (!solving) break; 3036 l++; 3037 } //while l 3038 if (score<-9000) 3039 { 3040 Serial.println(F("GIVE UP!")); 3041 } else { 3042 Serial.print(F("STEP=")); 3043 Serial.println(str_step(cur_step)); 3044 if (score>9000) { 3045 Serial.print(F("CHECKMATE 3046 ")); 3047 if (score<9999) { 3048 Serial.print(F("in ")); Serial.print((9999-score)/2+1); 3049 Serial.print(F(" steps")); 3050 } else steps[cur_step].check=2; 3051 Serial.println(""); 3052 3053 if (checkmatesearch) { 3054 steps[cur_step+9999-score].check=2; 3055 3056 steps[cur_step+10000-score].fig1=0; 3057 for (int i=cur_step;i<cur_step+10000-score;i++) 3058 Serial.println(str_step(i)); 3059 } 3060 } 3061 } 3062 tft.drawFastHLine(10,312,300,BLACK); 3063 3064 tft.drawFastHLine(10,315,300,BLACK); 3065 progress=0; 3066 show_status(); 3067 3068 BAction.Hide(); 3069 solving=0; 3070 isstatus=0; 3071 return score; 3072 3073 3074} 3075//**************************** 3076 3077
ArduinoMega Chess 1.0
arduino
1//ArduinoMega Chess 1.0 2//Sergey Urusov, ususovsv@gmail.com 3 4#include <Adafruit_TFTLCD.h> 5#include <stdint.h> 6#include "TouchScreen.h" 7#include <avr/pgmspace.h> 8#include <EEPROM.h> 9 10 11uint16_t BLACK,BLUE,RED,GREEN,CYAN,MAGENTA,YELLOW,WHITE,GRAY,DARK,GRAY2,BLACKF,WHITEF; 12Adafruit_TFTLCD tft; 13#define YP A1 // must be an analog pin, use "An" notation! 14#define XM A2 // must be an analog pin, use "An" notation! 15#define YM 7 // can be a digital pin 16#define XP 6 // can be a digital pin 17TouchScreen ts = TouchScreen(XP, YP, XM, YM, 350); 18int touchx,touchy,touchx0,touchy0,ntouch; 19unsigned long lasttouch; 20#define MINPRESSURE 10 21#define MAXPRESSURE 1000 22const int PinBuzz = 44; 23int cycle=0; // 24const char fp=1; 25const char fn=2; 26const char fb=3; 27const char fr=4; 28const char fq=5; 29const char fk=6; 30const int fig_weight[]={0,100,320,330,500,900,0}; 31const char fig_symb[]=" NBRQK"; 32unsigned long count; 33boolean rotate=false; 34const char polezero[8][8] PROGMEM={ 35 { 0, 0, 0, 0, 0, 0, 0, 0}, 36 { 0, 0, 0, 0, 0, 0, 0, 0}, 37 { 0, 0, 0, 0, 0, 0, 0, 0}, 38 { 0, 0, 0, 0, 0, 0, 0, 0}, 39 { 0, 0, 0, 0, 0, 0, 0, 0}, 40 { 0, 0, 0, 0, 0, 0, 0, 0}, 41 { 0, 0, 0, 0, 0, 0, 0, 0}, 42 { 0, 0, 0, 0, 0, 0, 0, 0}, 43 }; 44const char polestart[8][8] PROGMEM={ 45 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 46 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 47 { 0, 0, 0, 0, 0, 0, 0, 0}, 48 { 0, 0, 0, 0, 0, 0, 0, 0}, 49 { 0, 0, 0, 0, 0, 0, 0, 0}, 50 { 0, 0, 0, 0, 0, 0, 0, 0}, 51 { fp, fp, fp, fp, fp, fp, fp, fp}, 52 { fr, fn, fb, fq, fk, fb, fn, fr}, 53 }; 54 55char pole[8][8]={ // 3 56 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 57 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 58 { 0, 0, 0, 0, 0, 0, 0, 0}, 59 { 0, 0, 0, 0, 0, 0, 0, 0}, 60 { 0, 0, 0, 0, 0, 0, 0, 0}, 61 { 0, 0, 0, 0, 0, 0, 0, 0}, 62 { fp, fp, fp, fp, fp, fp, fp, fp}, 63 { fr, fn, fb, fq, fk, fb, fn, fr}, 64 }; 65 66char pole0[8][8]; // 67char poledisp[8][8]; // 68char polechoice[7]; // 69boolean w00,w000,b00,b000; 70char blinkstep; 71typedef struct { 72 char fig1, fig2; // 73 char x1,y1,x2,y2; // 74 char check; // 75 char type; // 1- ,2- ,3- ,4-5-6-7- ,,, 76 int weight; // , 77} step_type; 78const int MAXSTEPS=500; //. 79//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 80int MINDEPTH; //. 81int MAXDEPTH; //. 82int LIMDEPTH; //. 83const char MAXCUTS=10; //.. - - (10- , 20) 84boolean TRACE=0; 85boolean checkmatesearch=0; // 86boolean solving=false; 87boolean choice=false; 88boolean sound=0; 89int cur_step=1; // , 1.... 90int limit=0; // , 0-6; 91//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 92const long limits[7]={10,30,60,180,600,1800,100000}; 93// 0 1 2 3 4 5 6 94step_type steps[MAXSTEPS]; // 95step_type cuts[MAXCUTS]; // - 96step_type lastbest; 97int lastscore; 98int minbeta,maxalpha; 99int startweight; 100int cur_level; // () 101int start_var; // , 1.... 102int cur_var; // , 1.... 103int cur_choice; // 104boolean check_on_table; // 105boolean isstatus; 106char WKJ=0,WKI=0,BKJ=0,BKI=0; // 107boolean endspiel=false; // 108char progress; // 0-100 109boolean only_action=false; // - 110unsigned long starttime,limittime,quitime=0; 111boolean drag; 112const uint8_t fig[6][32] PROGMEM={ 113{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 1140x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x0, 0x0}, // 115{0x0, 0x0, 0x3, 0x40, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x3F, 0xFC, 0x39, 0xFC, 0x33, 0xFC, 1160x7, 0xFC, 0xF, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x7, 0xE0, 0x1F, 0xF8, 0x0, 0x0}, // 117{0x1, 0x80, 0x3, 0xC0, 0x1, 0x80, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 1180x1F, 0xF8, 0x1F, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x31, 0x8C, 0x7F, 0xFE, 0x0, 0x0}, // 119{0x0, 0x0, 0x19, 0x98, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 1200xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0}, // 121{0x19, 0x98, 0xD9, 0x9B, 0xD9, 0x9B, 0xD9, 0x9B, 0x6D, 0xB6, 0x6D, 0xB6, 0x6D, 0xB6, 0x35, 0xAC, 1220x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0}, // 123{0x1, 0x80, 0x1, 0x80, 0x79, 0x9E, 0x7D, 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1240x7F, 0xFE, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0} // 125}; 126const uint8_t fig_cont[6][32] PROGMEM={ 127{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1280x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8}, // 129{0x3, 0x40, 0x4, 0xA0, 0x8, 0x10, 0x12, 0x8, 0x20, 0x4, 0x40, 0x2, 0x46, 0x2, 0x4C, 0x2, 1300x38, 0x2, 0x10, 0x4, 0x10, 0x8, 0x8, 0x10, 0x4, 0x20, 0x18, 0x18, 0x20, 0x4, 0x3F, 0xFC}, // 131{0x2, 0x40, 0x4, 0x20, 0x6, 0x60, 0x10, 0x8, 0x21, 0x84, 0x21, 0x84, 0x21, 0x84, 0x27, 0xE4, 1320x21, 0x84, 0x21, 0x84, 0x11, 0x88, 0x8, 0x10, 0x34, 0x2C, 0x4E, 0x72, 0x80, 0x1, 0xFF, 0xFF}, // 133{0x39, 0x9C, 0x26, 0x64, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 1340x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC}, // 135{0x22, 0x24, 0x22, 0x24, 0x22, 0x24, 0x26, 0x64, 0x92, 0x49, 0x92, 0x49, 0x92, 0x49, 0x4A, 0x52, 1360x40, 0x2, 0x40, 0x2, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC}, // 137{0x2, 0x40, 0x7A, 0x5E, 0x86, 0x61, 0x82, 0x41, 0x2, 0x40, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 1380x80, 0x81, 0x41, 0x82, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC} // 139}; 140 141const uint8_t issound[2][32] PROGMEM={ 142{0x0, 0x0, 0x0, 0x0, 0x0, 0x32, 0x0, 0x54, 0x0, 0x98, 0x1F, 0x10, 0x10, 0x30, 0x10, 0x50, 1430x10, 0x90, 0x11, 0x10, 0x1F, 0x10, 0x4, 0x90, 0x8, 0x50, 0x10, 0x30, 0x0, 0x0, 0x0, 0x0}, 144{0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x50, 0x0, 0x90, 0x1F, 0x10, 0x10, 0x10, 0x10, 0x10, 1450x10, 0x10, 0x10, 0x10, 0x1F, 0x10, 0x0, 0x90, 0x0, 0x50, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0} 146}; 147const uint8_t iswb[2][32] PROGMEM={ 148{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1490x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x0, 0x0}, 150{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 0xF0, 0xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 1510xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0} 152}; 153const uint8_t iscm[2][32] PROGMEM={ 154{0x0, 0x0, 0x7, 0xE0, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0xC, 0x30, 0x0, 0x30, 0x0, 0x60, 1550x0, 0xC0, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0}, 156{0x0, 0x0, 0x3F, 0x0, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x1, 0x80, 0x3, 0x0, 1570x6, 0x24, 0xC, 0x24, 0xC, 0xFF, 0xC, 0x24, 0x0, 0xFF, 0xC, 0x24, 0xC, 0x24, 0x0, 0x0} 158}; 159const uint8_t islimit[7][32] PROGMEM={ 160{0x0, 0x0, 0x4, 0x70, 0xC, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 0x88, 1610xE, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 162{0x0, 0x0, 0xC, 0x70, 0x12, 0x88, 0x2, 0x88, 0xC, 0x88, 0x2, 0x88, 0x2, 0x88, 0x12, 0x88, 1630xC, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 164{0x0, 0x0, 0x1, 0x80, 0x3, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 1650x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 166{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 1670x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 168{0x0, 0x0, 0xC, 0x78, 0x1C, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 1690x1E, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 170{0x0, 0x0, 0x1C, 0x78, 0x36, 0xCC, 0x6, 0xCC, 0x1C, 0xCC, 0x6, 0xCC, 0x36, 0xCC, 0x36, 0xCC, 1710x1C, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 172{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3C, 0x3C, 0x7E, 0x7E, 0x63, 0xC6, 0x61, 0x86, 1730x63, 0xC6, 0x66, 0x66, 0x3C, 0x3C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 174}; 175const uint8_t isdesc[2][32] PROGMEM={ 176{0x0, 0x0, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 1770xE3, 0x8E, 0xE3, 0x8E, 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E}, 178{0x0, 0x0, 0x0, 0x4, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x30, 1790x30, 0x70, 0x78, 0xE0, 0x3C, 0xC0, 0x1F, 0xC0, 0xF, 0x80, 0x7, 0x0, 0x2, 0x0, 0x0, 0x0} 180 181}; 182const uint8_t isback[32] PROGMEM={ 1830x0, 0x0, 0x0, 0x0, 0x20, 0xF0, 0x31, 0xF8, 0x3B, 0xFC, 0x3F, 0x8E, 0x3F, 0xE, 0x3F, 0xE, 1840x3F, 0x8E, 0x3F, 0xCE, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x70, 0x0, 0x0 185}; 186const uint8_t issave[32] PROGMEM={ 1870x1, 0x80, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 1880x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 189}; 190const uint8_t isload[32] PROGMEM={ 1910x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 1920x23, 0xC4, 0x27, 0xE4, 0x27, 0xE4, 0x23, 0xC4, 0x21, 0x84, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 193}; 194const uint8_t isempty[32] PROGMEM={ 1950xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 1960x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF 197}; 198const uint8_t isinit[32] PROGMEM={ 1990xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 2000x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 201}; 202const uint8_t isplay[32] PROGMEM={ 2030x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x1, 0x80, 0x23, 0xF8, 0x17, 0xC8, 0xB, 0xC8, 2040x3, 0xC0, 0x3, 0xC0, 0x2, 0x40, 0x4, 0x40, 0x4, 0x38, 0x4, 0x8, 0xC, 0x0, 0x0, 0x0 205}; 206const uint8_t isrotate[2][32] PROGMEM={ 207{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 2080x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0}, 209{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 2100x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0} 211}; 212 213const int pos[7][8][8] PROGMEM={ 214 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 215 {100,100,100,100,100,100,100,100}, //{ 50, 50, 50, 50, 50, 50, 50, 50}, 216 { 20, 30, 40, 50, 50, 40, 30, 20}, //{ 10, 10, 20, 30, 30, 20, 10, 10}, 217 { 5, 5, 10, 25, 25, 10, 5, 5}, 218 { 0, 0, 0, 20, 20, 0, 0, 0}, 219 { 5, -5,-10, 0, 0,-10, -5, 5}, 220 { 5, 10, 10,-20,-20, 10, 10, 5}, //{ 5, 10, 10,-20,-20, 10, 10, 5}, 221 { 0, 0, 0, 0, 0, 0, 0, 0}}, 222 223 {{-50,-40,-30,-30,-30,-30,-40,-50}, // 224 {-40,-20, 0, 0, 0, 0,-20,-40}, 225 {-30, 0, 10, 15, 15, 10, 0,-30}, 226 {-30, 5, 15, 20, 20, 15, 5,-30}, 227 {-30, 0, 15, 20, 20, 15, 0,-30}, 228 {-30, 5, 10, 15, 15, 10, 5,-30}, 229 {-40,-20, 0, 5, 5, 0,-20,-40}, 230 {-50,-40,-30,-30,-30,-30,-40,-50}}, 231 232 {{-20,-10,-10,-10,-10,-10,-10,-20}, // 233 {-10, 0, 0, 0, 0, 0, 0,-10}, 234 {-10, 0, 5, 10, 10, 5, 0,-10}, 235 {-10, 5, 5, 10, 10, 5, 5,-10}, 236 {-10, 0, 10, 10, 10, 10, 0,-10}, 237 {-10, 10, 10, 10, 10, 10, 10,-10}, 238 {-10, 5, 0, 0, 0, 0, 5,-10}, 239 {-20,-10,-10,-10,-10,-10,-10,-20}}, 240 241 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 242 { 5, 10, 10, 10, 10, 10, 10, 5}, 243 { -5, 0, 0, 0, 0, 0, 0, -5}, 244 { -5, 0, 0, 0, 0, 0, 0, -5}, 245 { -5, 0, 0, 0, 0, 0, 0, -5}, 246 { -5, 0, 0, 0, 0, 0, 0, -5}, 247 { -5, 0, 0, 0, 0, 0, 0, -5}, 248 { 0, 0, 0, 5, 5, 0, 0, 0}}, 249 250 {{-20,-10,-10, -5, -5,-10,-10,-20}, // 251 {-10, 0, 0, 0, 0, 0, 0,-10}, 252 {-10, 0, 5, 5, 5, 5, 0,-10}, 253 { -5, 0, 5, 5, 5, 5, 0, -5}, 254 { 0, 0, 5, 5, 5, 5, 0, -5}, 255 {-10, 5, 5, 5, 5, 5, 0,-10}, 256 {-10, 0, 5, 0, 0, 0, 0,-10}, 257 {-20,-10,-10, -5, -5,-10,-10,-20}}, 258 259 {{-30,-40,-40,-50,-50,-40,-40,-30}, // 260 {-30,-40,-40,-50,-50,-40,-40,-30}, 261 {-30,-40,-40,-50,-50,-40,-40,-30}, 262 {-30,-40,-40,-50,-50,-40,-40,-30}, 263 {-20,-30,-30,-40,-40,-30,-30,-20}, 264 {-10,-20,-20,-20,-20,-20,-20,-10}, 265 { 10, 10,-10,-10,-10,-10, 10, 10}, //{ 20, 20, 0, 0, 0, 0, 20, 20}, 266 { 10, 40, 30, 0, 0, 0, 50, 10}}, //{ 20, 30, 10, 0, 0, 10, 30, 20}}, 267 268 {{-50,-40,-30,-20,-20,-30,-40,-50}, // 269 {-30,-20,-10, 0, 0,-10,-20,-30}, 270 {-30,-10, 20, 30, 30, 20,-10,-30}, 271 {-30,-10, 30, 40, 40, 30,-10,-30}, 272 {-30,-10, 30, 40, 40, 30,-10,-30}, 273 {-30,-10, 20, 30, 30, 20,-10,-30}, 274 {-30,-30, 0, 0, 0, 0,-30,-30}, 275 {-50,-30,-30,-30,-30,-30,-30,-50}} 276}; 277 278//****** 279class Button { 280public: 281int bx,by,bw,bh,bshift; 282String bs; 283const uint8_t* bp; 284 Button(int x,int y,int w,int h,String s,const uint8_t* p, int shift); 285 void Show(int shift); 286 void Show(String s); 287 void Hide(); 288 boolean IsPressed(int xc, int yc); 289private: 290boolean active; 291 void ishow(uint16_t textcolor,uint16_t backcolor); 292}; 293Button::Button(int x,int y,int w,int h,String s,const uint8_t* p=NULL, int shift=0) { 294 bx=x; by=y; bw=w; bh=h; bs=s; bp=p; bshift=shift; 295}; 296void Button::Show(int shift=0) { 297 bshift=shift; 298 active=true; 299 ishow(WHITE,DARK); 300}; 301void Button::Show(String s) { 302 bs=s; 303 active=true; 304 ishow(WHITE,DARK); 305}; 306void Button::Hide() { 307 active=false; 308 ishow(GRAY,DARK); 309}; 310boolean Button::IsPressed(int xc, int yc) { 311 if (active&&abs(bx+bw/2-xc)<bw/2&&abs(by+bh/2-yc)<bh/2) { 312 ishow(WHITE,GRAY); 313 touchx=0; touchy=0; 314 return true; 315 } 316 return false; 317}; 318void Button::ishow(uint16_t textcolor,uint16_t backcolor) { 319 tft.fillRoundRect(bx+2,by+2,bw,bh,3,GRAY2); 320 tft.fillRoundRect(bx,by,bw,bh,3,backcolor); 321 tft.setTextColor(textcolor); 322 tft.setTextSize(1); 323 tft.setCursor(bx+(bw-bs.length()*6)/2,by+(bh-8)/2); 324 if (bp!=NULL) { 325 tft.drawBitmap(1+bx+(bw-16)/2, by+(bh-16)/2, bp+bshift*32, 16, 16,textcolor); 326 } 327 else tft.print(bs); 328} 329Button BAction(65, 216, 55, 20, "START"); 330Button BBW(125, 216, 22, 20, "",&iswb[0][0]); 331Button BBack(152, 216, 23, 20, "",&isback[0]); 332Button BInit(152, 216, 23, 20, "",&isinit[0]); 333Button BLimit(180, 216, 23, 20, "",&islimit[0][0]); 334Button BEmpty(180, 216, 23, 20, "",&isempty[0]); 335Button BRotate(208, 216, 23, 20, "",&isrotate[0][0]); 336Button BPlay(208, 216, 23, 20, "",&isplay[0]); 337Button BCM(236, 216, 23, 20, "",&iscm[0][0]); 338Button BSave(236, 216, 23, 20, "",&issave[0]); 339Button BDesc(264, 216, 23, 20, "",&isdesc[0][0]); 340Button BSound(292, 216, 23, 20, "",&issound[0][0]); 341Button BLoad(292, 216, 23, 20, "",&isload[0]); 342//****** 343//********************************** 344void drawBitmap(int16_t x, int16_t y, 345 const uint8_t *bitmap, int16_t w, int16_t h, 346 uint16_t color) { 347 int16_t i, j, byteWidth = (w + 7) / 8; 348 for(j=0; j<h; j++) { 349 for(i=0; i<w; i++ ) { 350 if(pgm_read_byte(bitmap + j * byteWidth + i / 8) & (128 >> (i & 7))) { 351 tft.drawPixel(x+i, y+j, color); 352 } 353 } 354 } 355} 356//**************************** 357void deletebuttons() { 358 tft.fillRect(0,215,320,30,BLACK); 359} 360 361//**************************** 362void clearstatus() { 363 tft.fillRect(0,210,60,30,BLACK); 364 tft.drawFastHLine(5,210,200,BLACK); 365 tft.drawFastHLine(5,212,200,BLACK); 366} 367//**************************** 368void setup() { 369 Serial.begin(57600); 370 Serial.println(F("Start")); 371 pinMode(13, OUTPUT); 372 tft.reset(); 373 tft.begin(tft.readID()); 374 tft.setRotation(1); 375 definecolors(); 376 beep(100); 377 for (int i=0;i<MAXSTEPS;i++) { 378 steps[i].x1=0; steps[i].y1=0; 379 steps[i].x2=0; steps[i].y2=0; 380 steps[i].fig1=0; steps[i].fig2=0; 381 steps[i].check=0; 382 steps[i].type=0; 383 steps[i].weight=0; 384 } 385 initboard(); 386} 387//**************************** 388void initboard() { 389 for (int i=0;i<8;i++) 390 for (int j=0;j<8;j++) poledisp[j][i]=-100; // 391 tft.fillScreen(BLACK); 392 tft.setTextColor(GRAY); 393 tft.setTextSize(1); 394 for (int j=1;j<9;j++) { 395 tft.setCursor(2,j*24-15); 396 if (rotate) tft.print(j); else tft.print(9-j); 397 } 398 for (byte i=1;i<9;i++) { 399 tft.setCursor(i*24-1,198); 400 if (rotate) tft.print(char(96+9-i)); else tft.print(char(96+i)); 401 } 402 initscreen(); 403} 404//**************************** 405void initscreen() { 406 show_board(); 407 show_steps(); 408 BAction.Show("START"); 409 BBW.Show(cur_step%2); 410 if (cur_step>1) BBack.Show(); else BBack.Hide(); 411 BSave.Hide(); 412 BLoad.Hide(); 413 BInit.Hide(); 414 BBack.Show(); 415 BLimit.Show(); 416 BPlay.Hide(); 417 BRotate.Show(rotate); 418 BEmpty.Hide(); 419 BLimit.Show(limit); 420 BCM.Show(checkmatesearch); 421 BDesc.Show(choice); 422 BSound.Show(sound); 423} 424//**************************** 425void loop() { 426 gui(); 427 if (solving) { 428 int len=70; 429 if (!sound) len=2; 430 for (int i=1;i<4;i++) { tone(44,400+300*i,len); delay(70); } 431 lastscore=solve_step(); 432 float tim=float(millis()-starttime)/1000; 433 for (int i=3;i>=1;i--) { tone(44,400+300*i,len); delay(70); } 434 if (!checkmatesearch&&lastscore>-9000&&lastscore!=8999) { 435 movestep(cur_step); 436 cur_step++; steps[cur_step].fig1=0; 437 } 438 initscreen(); 439 animate_step(cur_step-1,false); 440 clearstatus(); 441 if (lastscore>9000) { 442 tft.setTextSize(1); 443 tft.setTextColor(GREEN); 444 tft.setCursor(0,210); 445 if (lastscore<9999) { 446 tft.print("# in "); 447 tft.print((9999-lastscore)/2+1); tft.print(F(" st")); 448 } else tft.print("Checkmate"); 449 } else if (lastscore<-9000) { 450 tft.setTextSize(1); 451 tft.setTextColor(RED); 452 tft.setCursor(0,210); 453 tft.print("GIVE UP!"); 454 show_steps(); 455 } else if (lastscore==8999) { //Draw 456 tft.setTextSize(1); 457 tft.setTextColor(YELLOW); 458 tft.setCursor(0,210); 459 tft.print("Draw"); 460 } 461 tft.setTextSize(1); 462 tft.setTextColor(GRAY); 463 tft.setCursor(0,220); 464 tft.print(tim,0); 465 tft.print("s "); 466 if (abs(lastscore)<5000) { 467 tft.print(lastscore); 468 } 469 tft.setCursor(0,230); 470 tft.print(count); 471 tft.print("pos"); 472 Serial.print(F("Positions estimated=")); Serial.println(count); 473 Serial.print(F("Time=")); Serial.print(tim,1); Serial.println("s"); 474 } 475 delay(10); 476 477 478} 479 480//********************************** 481void load_choice() { 482 for (int i=0;i<7;i++) 483 if (cur_step%2==1) polechoice[i]=i; else polechoice[i]=-i; 484} 485//********************************** 486void show_choice() { 487uint16_t color,color_cont; 488 for (int i=0;i<7;i++) { 489 color=BLACKF; 490 if (i%2==0) color=WHITEF; 491 tft.fillRect(260,i*25+35,25,25,color); 492 color=DARK; 493 if (i==cur_choice) color=WHITE; 494 tft.drawRect(260,i*25+35,25,25,color); 495 color=BLACK; color_cont=GRAY; 496 if (polechoice[i]>0) { color=WHITE; color_cont=BLACK; } 497 if (polechoice[i]!=0) { 498 drawBitmap(264, i*25+39,&fig[abs(polechoice[i])-1][0], 16, 16,color); 499 drawBitmap(264, i*25+39,&fig_cont[abs(polechoice[i])-1][0], 16, 16,color_cont); 500 } 501 } 502} 503//********************************** 504void animate_step(int nstep, boolean hide) { 505 if (!hide&&nstep-1>0&&steps[nstep-1].fig1!=0) animate_step(nstep-1,true); 506 if (nstep-1<1||steps[nstep].fig1==0) return; 507 int j=steps[nstep].x1; 508 int dj=steps[nstep].x2-steps[nstep].x1; 509 int i=steps[nstep].y1; 510 int di=steps[nstep].y2-steps[nstep].y1; 511 di=di/abs(di); dj=dj/abs(dj); 512 if (hide) show_fig(i,j); 513 while (j!=steps[nstep].x2||i!=steps[nstep].y2) { 514 show_fig(i,j); 515 if (!hide) 516 if (!rotate) tft.drawRect(i*24+13,j*24+1,23,23,WHITE); 517 else tft.drawRect((7-i)*24+13,(7-j)*24+1,23,23,WHITE); 518 int mj=0; 519 if (j!=steps[nstep].x2) { j+=dj; mj=1; } 520 if (abs(steps[nstep].fig1)==fn&&mj==1) continue; 521 if (i!=steps[nstep].y2) i+=di; 522 } 523 show_fig(i,j); 524 if (!hide) 525 if (!rotate) tft.drawRect(i*24+13,j*24+1,23,23,WHITE); 526 else tft.drawRect((7-i)*24+13,(7-j)*24+1,23,23,WHITE); 527} 528//********************************** 529boolean load_usb() { 530char s='x',i=0,j=0; boolean load=false; 531 Serial.println(F("Wait for FEN position")); 532 for (int i=0;i<8;i++) 533 for (int j=0;j<8;j++) { 534 pole0[j][i]=pole[j][i]; 535 pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 536 } 537 while (s!=' ') { 538 s=Serial.read(); 539 if (i>7) { i=0; j++; } 540 if (!getpole(j,i)) break; 541 switch (s) { 542 case '/': i=0; break; 543 case 'p': pole[j][i]=-fp; i++; break; 544 case 'P': pole[j][i]=fp; i++; break; 545 case 'n': pole[j][i]=-fn; i++; break; 546 case 'N': pole[j][i]=fn; i++; break; 547 case 'b': pole[j][i]=-fb; i++; break; 548 case 'B': pole[j][i]=fb; i++; break; 549 case 'r': pole[j][i]=-fr; i++; break; 550 case 'R': pole[j][i]=fr; i++; break; 551 case 'q': pole[j][i]=-fq; i++; break; 552 case 'Q': pole[j][i]=fq; i++; break; 553 case 'k': pole[j][i]=-fk; i++; break; 554 case 'K': pole[j][i]=fk; i++; break; 555 case '1': i++; break; 556 case '2': i+=2; break; 557 case '3': i+=3; break; 558 case '4': i+=4; break; 559 case '5': i+=5; break; 560 case '6': i+=6; break; 561 case '7': i+=7; break; 562 case '8': i=0; j++; break; 563 case ' ': break; 564 } 565 delay(20); 566 if (i+j>0&&Serial.available()==0) break; 567 } 568 s=0; 569 if (Serial.available()>0) s=Serial.read(); 570 while (Serial.available()>0) Serial.read(); 571 if (s=='w'||s==0) { cur_step=1; load=true; } 572 else if (s=='b') { cur_step=2; load=true; } 573 else load=false; 574 if (load) { 575 steps[1].fig1=0; steps[2].fig1=0; 576 Serial.println(F("Position loaded")); 577 } else { 578 for (int i=0;i<8;i++) 579 for (int j=0;j<8;j++) 580 pole[j][i]=pole0[j][i]; 581 } 582 return load; 583} 584//********************************** 585void play() { 586 BAction.Show("STOP"); 587 BSave.Hide(); 588 BBW.Hide(); 589 BLoad.Hide(); 590 BInit.Hide(); 591 BPlay.Hide(); 592 BEmpty.Hide(); 593 BDesc.Hide(); 594 clearstatus(); 595 for (int i=cur_step-1;i>0;i--) { 596 if (steps[i].fig1!=0) backstep(i); 597 } 598 show_board(); 599 show_steps(); 600 boolean st=false; 601 unsigned long tim=millis(); 602 int i=1; 603 while (i<cur_step) { 604 if (steps[i].fig1!=0) movestep(i); 605 digitalWrite(13, HIGH); 606 TSPoint p = ts.getPoint(); 607 digitalWrite(13, LOW); 608 pinMode(XM, OUTPUT); 609 pinMode(YP, OUTPUT); 610 int x=388-p.y/2.4; 611 int y=p.x/3.2-50; 612 if (p.z>MINPRESSURE&&p.z<MAXPRESSURE) 613 if (BAction.IsPressed(x,y)) { st=true; BAction.Show(); } 614 if (millis()-tim>2000||st) { 615 tim=millis(); 616 i++; 617 show_board(); 618 beep(20); 619 } 620 } 621 choice=0; 622 beep(200); 623 kingpositions(); 624 clearstatus(); 625 initscreen(); 626} 627//********************************** 628void gui() { 629 digitalWrite(13, HIGH); 630 TSPoint p = ts.getPoint(); 631 digitalWrite(13, LOW); 632 // if sharing pins, you'll need to fix the directions of the touchscreen pins 633 pinMode(XM, OUTPUT); 634 pinMode(YP, OUTPUT); 635 int x=388-p.y/2.4; 636 int y=p.x/3.2-50; 637 if (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&p.z>MINPRESSURE&&p.z<MAXPRESSURE) { 638 touchx=x; 639 touchy=y; 640 if (solving) { 641 if (BAction.IsPressed(x,y)) { // 642 beep(100); 643 solving=false; 644 BAction.Show("stopping"); 645 BAction.Hide(); 646 return; 647 } 648 } else { //not solve 649 if (BAction.IsPressed(x,y)) { // 650 if (!choice) { 651 delay(100); 652 solving=true; 653 BAction.Show(); 654 animate_step(cur_step-1,true); 655 return; 656 } else { // USB 657 delay(100); 658 BAction.Hide(); 659 BSave.Hide(); 660 BBW.Hide(); 661 BLoad.Hide(); 662 BInit.Hide(); 663 BPlay.Hide(); 664 BEmpty.Hide(); 665 BDesc.Hide(); 666 show_steps(); 667 choice=0; 668 if (!load_usb()) return; 669 beep(200); 670 kingpositions(); 671 clearstatus(); 672 initscreen(); 673 } 674 } else if (BBW.IsPressed(x,y)) { //- 675 beep(100); 676 if (cur_step==1) cur_step=2; 677 else if (cur_step==2) cur_step=1; 678 else if (cur_step%2==0) cur_step--; else cur_step++; 679 BBW.Show(cur_step%2); 680 if (choice) { load_choice(); show_choice(); } 681 return; 682 } else if (cur_step>1&&BBack.IsPressed(x,y)) { // 683 beep(100); 684 cur_step--; 685 animate_step(cur_step,true); 686 lastscore=0; 687 backstep(cur_step); 688 steps[cur_step].fig1=0; 689 show_board(); 690 show_steps(); 691 if (cur_step>1) BBack.Show(); else BBack.Hide(); 692 BBW.Show(cur_step%2); 693 clearstatus(); 694 return; 695 } else if (BLimit.IsPressed(x,y)) { // 696 beep(100); 697 limit++; if (limit>6) limit=0; 698 BLimit.Show(limit); 699 return; 700 } else if (BCM.IsPressed(x,y)) { // 701 beep(100); 702 checkmatesearch=!checkmatesearch; 703 BCM.Show(checkmatesearch); 704 if (!checkmatesearch) steps[cur_step].fig1=0; 705 lastscore=0; 706 show_steps(); 707 return; 708 } else if (BRotate.IsPressed(x,y)) { // 709 beep(100); 710 rotate=!rotate; 711 BRotate.Show(rotate); 712 initboard(); 713 return; 714 } else if (BSound.IsPressed(x,y)) { // 715 if (sound==1) { beep(100); sound=0; } else { sound=1; beep(200); } 716 BSound.Show(sound); 717 return; 718 } else if (BDesc.IsPressed(x,y)) { // 719 beep(100); 720 if (!choice) { 721 choice=1; 722 cur_choice=1; 723 load_choice(); 724 show_choice(); 725 BAction.Show("USB(FEN)"); 726 BBack.Hide(); 727 BInit.Show(); 728 BSave.Show(); 729 BLimit.Hide(); 730 BEmpty.Show(); 731 BRotate.Hide(); 732 BPlay.Show(); 733 BCM.Hide(); 734 BSound.Hide(); 735 BSave.Show(); 736 BLoad.Show(); 737 } else { 738 choice=0; 739 lastscore=0; 740 clearstatus(); 741 kingpositions(); 742 initscreen(); 743 } 744 BDesc.Show(choice); 745 return; 746 } else if (BInit.IsPressed(x,y)) { // 747 beep(200); 748 cur_step=1; 749 steps[1].fig1=0; 750 for (int i=0;i<8;i++) 751 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polestart[j][i]); 752 choice=0; 753 kingpositions(); 754 clearstatus(); 755 initscreen(); 756 } else if (BEmpty.IsPressed(x,y)) { // 757 beep(200); 758 cur_step=1; 759 clearstatus(); 760 steps[1].fig1=0; steps[2].fig1=0; 761 for (int i=0;i<8;i++) 762 for (int j=0;j<8;j++) pole[j][i]=(char)pgm_read_byte(&polezero[j][i]); 763 BEmpty.Show(); 764 show_board(); 765 } else if (BSave.IsPressed(x,y)) { 766 beep(100); 767 for (int i=0;i<8;i++) 768 for (int j=0;j<8;j++) EEPROM.write(i*8+j,pole[j][i]); 769 EEPROM.put(64,cur_step); 770 EEPROM.put(66,limit); 771 for (int i=1;i<cur_step;i++) { 772 EEPROM.put(66+i*10,steps[i]); 773 } 774 BSave.Show(); 775 } else if (BLoad.IsPressed(x,y)) { 776 beep(200); 777 for (int i=0;i<8;i++) 778 for (int j=0;j<8;j++) pole[j][i]=EEPROM.read(i*8+j); 779 EEPROM.get(64,cur_step); 780 if (cur_step<0||cur_step>400) cur_step=1; 781 EEPROM.get(66,limit); 782 if (limit<0||limit>6) limit=2; 783 for (int i=1;i<cur_step;i++) { 784 EEPROM.get(66+i*10,steps[i]); 785 } 786 steps[0].fig1=0; 787 steps[cur_step].fig1=0; 788 choice=0; 789 kingpositions(); 790 clearstatus(); 791 initscreen(); 792 } else if (BPlay.IsPressed(x,y)) { // 793 beep(100); 794 BPlay.Show(); 795 play(); 796 return; 797 } 798 if (!choice&&ntouch==0) { 799 touchx0=touchx; 800 touchy0=touchy; 801 char x1=touchy0/24; 802 char y1=(touchx0-10)/24; 803 if (rotate) { x1=7-x1; y1=7-y1; } 804 if (getpole(y1,x1)&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 805 char p=pole[x1][y1]; 806 drag=true; 807 pole[x1][y1]=0; 808 show_fig(y1,x1); 809 pole[x1][y1]=p; 810 } else drag=false; 811 } 812 ntouch++; 813 if (drag&&ntouch>1) { 814 uint16_t buf[256]; 815 int x1=touchy0/24; 816 int y1=(touchx0-10)/24; 817 if (rotate) { x1=7-x1; y1=7-y1; } 818 if (abs(pole[x1][y1])>0&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) { 819 for (int i=0;i<16;i++) 820 for (int j=0;j<16;j++) buf[i*16+j]=tft.readPixel(touchx-8+i,touchy-8+j); 821 buf[0]=tft.readPixel(touchx-8,touchy-8); 822 uint16_t color,color_cont; 823 color=BLACK; color_cont=GRAY; 824 if (pole[x1][y1]>0) { color=WHITE; color_cont=BLACK; } 825 drawBitmap(touchx-8, touchy-8,&fig[abs(pole[x1][y1])-1][0], 16, 16,color); 826 drawBitmap(touchx-8, touchy-8,&fig_cont[abs(pole[x1][y1])-1][0], 16, 16,color_cont); 827 delay(30); 828 for (int i=0;i<16;i++) 829 for (int j=0;j<16;j++) tft.drawPixel(touchx-8+i,touchy-8+j,buf[i*16+j]); 830 } 831 } else if (choice) { 832 int x1=touchy/24; 833 int y1=(touchx-10)/24; 834 if (rotate) { x1=7-x1; y1=7-y1; } 835 if (touchy+touchx!=0&&choice&&getpole(y1,x1)) { 836 pole[x1][y1]=polechoice[cur_choice]; 837 show_board(); 838 } 839 if (touchx>=260&&touchx<=285&&touchy>=35&&touchy<=210) { 840 cur_choice=(touchy-35)/25; 841 show_choice(); 842 } 843 } 844 lasttouch=millis(); 845 } //not solve 846 if (!drag) { beep(10); ntouch=0; animate_step(cur_step-1,true); } 847 } else { //touching 848 if (!solving&&!choice&&drag!=0&&ntouch>0&&p.z<MINPRESSURE&&millis()-lasttouch>300) { //drag 849 char x1=touchy0/24; 850 char y1=(touchx0-10)/24; 851 char x2=touchy/24; 852 char y2=(touchx-10)/24; 853 if (rotate) { x1=7-x1; y1=7-y1; x2=7-x2; y2=7-y2; } 854 if (getpole(y1,x1)) show_fig(y1,x1); 855 if (getpole(y1,x1)&&getpole(y2,x2)) { 856 start_var=cur_step+1; 857 only_action=false; 858 load_variants(cur_step); 859 for (int i=cur_step+1;i<cur_step+cur_var+1;i++) 860 if (steps[i].x1==x1&&steps[i].y1==y1&&steps[i].x2==x2&&steps[i].y2==y2) { 861 steps[cur_step]=steps[i]; 862 movestep(cur_step); cur_step++; steps[cur_step].fig1=0; 863 int len=100; 864 if (!sound) len=2; 865 for (int i=2;i>=1;i--) { tone(44,400+300*i,len); delay(100); } 866 initscreen(); 867 animate_step(cur_step-1,false); 868 break; 869 } 870 } else { 871 beep(10); 872 } 873 ntouch=0; 874 drag=false; 875 } 876 } 877 if (millis()-quitime>1000) { 878 if (isstatus) show_status(); 879 quitime=millis(); 880 } 881 882 883} 884//********************************** 885void show_status() { 886 int tim=(millis()-starttime)/1000; 887 int cur=200000*tim/(limittime-starttime); 888 if (cur>200) { cur=200; solving=false; } 889 tft.drawFastHLine(5,210,cur,GRAY); 890 tft.drawFastHLine(5,212,progress*2,GRAY); 891 int m=tim/60; 892 int s=tim%60; 893 tft.setTextWrap(1); 894 tft.setTextColor(GRAY); 895 tft.setTextSize(1); 896 tft.fillRect(0,220,60,10,BLACK); 897 tft.setCursor(2,220); 898 if (m>0) { tft.print(m); tft.print(":"); } 899 if (s>0) { 900 if (s<10&&m>0) tft.print("0"); 901 tft.print(s); 902 } else if (m>0) tft.print("00"); else tft.print("0"); 903 tft.setCursor(36,220); 904 tft.print(cur_level); 905 906 tft.setCursor(2,230); 907 if (cur_step%2==1) tft.setTextColor(WHITE); else tft.setTextColor(GRAY); 908 if (lastbest.fig1!=steps[0].fig1||lastbest.x1!=steps[0].x1||lastbest.y1!=steps[0].y1|| 909 lastbest.x2!=steps[0].x2||lastbest.y2!=steps[0].y2) { 910 beep(10); 911 lastbest=steps[0]; 912 tft.fillRect(0,230,50,10,BLACK); 913 tft.setCursor(2,230); 914 tft.print(str_step(0)); 915 blinkstep=0; 916 } 917 if (steps[0].fig1!=0&&blinkstep==0&&tim>5) { 918 char poleb[8][8]; 919 for (int i=0;i<8;i++) 920 for (int j=0;j<8;j++) { poleb[j][i]=pole[j][i]; pole[j][i]=pole0[j][i]; } 921 movestep(0); 922 show_board(); 923 delay(100); 924 backstep(0); 925 show_board(); 926 for (int i=0;i<8;i++) 927 for (int j=0;j<8;j++) pole[j][i]=poleb[j][i]; 928 kingpositions(); 929 } 930 blinkstep++; 931 if (blinkstep>2) blinkstep=0; 932} 933//**************************** 934void beep(int leng) { 935 analogWrite(PinBuzz, 20); 936 delay(2); 937 if (!sound) analogWrite(PinBuzz, 0); 938 delay(leng); 939 analogWrite(PinBuzz, 0); 940} 941//**************************** 942void definecolors() { 943 BLACK =0x0000; 944 BLUE =0x07FF; //0x001F; 945 RED =0xF800; 946 GREEN =0x07E0; 947 CYAN =0x07FF; 948 MAGENTA=0xF81F; 949 YELLOW =0xFFE0; 950 WHITE =0xFFFF; 951 GRAY =0x7BEF; 952 DARK =getColor(32,32,32); 953 GRAY2 =getColor(16,16,16); 954 BLACKF =getColor(94,58,0); 955 WHITEF =getColor(180,114,0); 956} 957//**************************** 958uint16_t getColor(uint8_t red, uint8_t green, uint8_t blue) 959{ 960 red >>= 3; 961 green >>= 2; 962 blue >>= 3; 963 return (red << 11) | (green << 5) | blue; 964} 965//**************************** 966void show_fig(int i,int j) { 967uint16_t color,color_cont; 968 color=BLACKF; 969 if ((i+j+2)%2==0) color=WHITEF; 970 int jj=j, ii=i; 971 if (rotate) { jj=7-j; ii=7-i; } 972 tft.fillRect(ii*24+12,jj*24,25,25,color); 973 tft.drawRect(ii*24+12,jj*24,25,25,WHITE); 974 color=BLACK; color_cont=GRAY; 975 if (pole[j][i]>0) { color=WHITE; color_cont=BLACK; } 976 if (pole[j][i]!=0) { 977 drawBitmap(ii*24+17, jj*24+5,&fig[abs(pole[j][i])-1][0], 16, 16,color); 978 drawBitmap(ii*24+17, jj*24+5,&fig_cont[abs(pole[j][i])-1][0], 16, 16,color_cont); 979 } 980} 981//**************************** 982void show_board() { 983 for (int i=0;i<8;i++) 984 for (int j=0;j<8;j++) { 985 if (poledisp[j][i]!=pole[j][i]) show_fig(i,j); 986 poledisp[j][i]=pole[j][i]; 987 } 988} 989//**************************** 990String str_step(int i) { 991 String s=""; 992 if (steps[i].fig1==0) return s; 993 if (steps[i].type==2) s="0-0"; 994 else if (steps[i].type==3) s="0-0-0"; 995 else { 996 if (abs(steps[i].fig1)>1) s=fig_symb[abs(steps[i].fig1)]; 997 if (abs(steps[i].fig1<5)) { 998 s=s+char(97+steps[i].y1); 999 s=s+String(8-steps[i].x1); 1000 if (steps[i].fig2==0) s=s+"-"; 1001 } 1002 if (steps[i].fig2!=0) { 1003 s=s+"x"; 1004 if (abs(steps[i].fig2)>1) s=s+fig_symb[abs(steps[i].fig2)]; 1005 } 1006 s=s+char(97+steps[i].y2); 1007 s=s+String(8-steps[i].x2); 1008 } 1009 if (steps[i].type>3) s=s+fig_symb[steps[i].type-2]; 1010 if (steps[i].check==1) s=s+"+"; else 1011 if (steps[i].check==2) s=s+"#"; 1012 return s; 1013} 1014//**************************** 1015void show_steps() { 1016 tft.fillRect(206,0,114,214,getColor(8,8,8)); 1017 tft.fillRect(206,0,114,12,DARK); 1018 tft.setTextSize(1); 1019 tft.setTextColor(WHITE,DARK); 1020 tft.setCursor(206,2); 1021 tft.print(" ArduinoMega Chess "); 1022 int i=1; int y=14; 1023 tft.setTextColor(GRAY); 1024 int cur=(cur_step+1)/2; // 1025 int lim_step=cur_step; 1026 if (checkmatesearch&&lastscore>9000) { 1027 tft.setTextColor(GREEN); 1028 lim_step=cur_step+10000-lastscore; 1029 cur=lim_step/2; 1030 } 1031 if (cur>20) i=cur-20; 1032 while (i<=cur&&y<207) { // 1033 tft.setCursor(210,y); 1034 tft.print(i); 1035 tft.print("."); 1036 if (steps[i*2-1].fig1!=0) tft.print(str_step(i*2-1)); 1037 if (steps[i*2].fig1==0||i*2>lim_step) break; 1038 tft.print(","); 1039 tft.print(str_step(i*2)); 1040 i++; y+=10; 1041 } 1042 1043} 1044//**************************** 1045boolean getdiagrowcheckw(char dj,char di) { // 1046char d,j1,i1; 1047 j1=WKJ; i1=WKI; 1048 for (d=1;d<8;d++) { 1049 j1+=dj; i1+=di; 1050 if (getpole(j1,i1)) { 1051 if (pole[j1][i1]==-fq||pole[j1][i1]==-fb) return true; 1052 if (pole[j1][i1]!=0) break; 1053 } else break; 1054 } 1055 return false; 1056} 1057//**************************** 1058boolean getdiagrowcheckb(char dj,char di) { // 1059char d,j1,i1; 1060 j1=BKJ; i1=BKI; 1061 for (d=1;d<8;d++) { 1062 j1+=dj; i1+=di; 1063 if (getpole(j1,i1)) { 1064 if (pole[j1][i1]==fq||pole[j1][i1]==fb) return true; 1065 if (pole[j1][i1]!=0) break; 1066 } else break; 1067 } 1068 return false; 1069} 1070//**************************** 1071boolean getstreightrowcheckw(char dj,char di) { // - 1072char d,j1,i1; 1073 j1=WKJ; i1=WKI; 1074 for (d=1;d<8;d++) { 1075 j1+=dj; i1+=di; 1076 if (getpole(j1,i1)) { 1077 if (pole[j1][i1]==-fq||pole[j1][i1]==-fr) return true; 1078 if (pole[j1][i1]!=0) break; 1079 } else break; 1080 } 1081 return false; 1082} 1083//**************************** 1084boolean getstreightrowcheckb(char dj,char di) { // - 1085char d,j1,i1; 1086 j1=BKJ; i1=BKI; 1087 for (d=1;d<8;d++) { 1088 j1+=dj; i1+=di; 1089 if (getpole(j1,i1)) { 1090 if (pole[j1][i1]==fq||pole[j1][i1]==fr) return true; 1091 if (pole[j1][i1]!=0) break; 1092 } else break; 1093 } 1094 return false; 1095} 1096//**************************** 1097boolean get_check(char king) { // 1098 if (king==fk) { // 1099 if (getdiagrowcheckw(-1, 1)) return true; 1100 if (getdiagrowcheckw(-1,-1)) return true; 1101 if (getdiagrowcheckw( 1,-1)) return true; 1102 if (getdiagrowcheckw( 1, 1)) return true; 1103 if (getstreightrowcheckw(-1, 0)) return true; 1104 if (getstreightrowcheckw( 0, 1)) return true; 1105 if (getstreightrowcheckw( 0,-1)) return true; 1106 if (getstreightrowcheckw( 1, 0)) return true; 1107 if (getpole(WKJ-2,WKI-1)&&pole[WKJ-2][WKI-1]==-fn) return true; 1108 if (getpole(WKJ-2,WKI+1)&&pole[WKJ-2][WKI+1]==-fn) return true; 1109 if (getpole(WKJ-1,WKI-2)&&pole[WKJ-1][WKI-2]==-fn) return true; 1110 if (getpole(WKJ-1,WKI+2)&&pole[WKJ-1][WKI+2]==-fn) return true; 1111 if (getpole(WKJ+1,WKI-2)&&pole[WKJ+1][WKI-2]==-fn) return true; 1112 if (getpole(WKJ+1,WKI+2)&&pole[WKJ+1][WKI+2]==-fn) return true; 1113 if (getpole(WKJ+2,WKI-1)&&pole[WKJ+2][WKI-1]==-fn) return true; 1114 if (getpole(WKJ+2,WKI+1)&&pole[WKJ+2][WKI+1]==-fn) return true; 1115 if (getpole(WKJ-1,WKI-1)&&pole[WKJ-1][WKI-1]==-fp) return true; 1116 if (getpole(WKJ-1,WKI+1)&&pole[WKJ-1][WKI+1]==-fp) return true; 1117 } else { // 1118 if (getdiagrowcheckb( 1,-1)) return true; 1119 if (getdiagrowcheckb( 1, 1)) return true; 1120 if (getdiagrowcheckb(-1, 1)) return true; 1121 if (getdiagrowcheckb(-1,-1)) return true; 1122 if (getstreightrowcheckb( 1, 0)) return true; 1123 if (getstreightrowcheckb( 0, 1)) return true; 1124 if (getstreightrowcheckb( 0,-1)) return true; 1125 if (getstreightrowcheckb(-1, 0)) return true; 1126 if (getpole(BKJ+2,BKI-1)&&pole[BKJ+2][BKI-1]==fn) return true; 1127 if (getpole(BKJ+2,BKI+1)&&pole[BKJ+2][BKI+1]==fn) return true; 1128 if (getpole(BKJ+1,BKI-2)&&pole[BKJ+1][BKI-2]==fn) return true; 1129 if (getpole(BKJ+1,BKI+2)&&pole[BKJ+1][BKI+2]==fn) return true; 1130 if (getpole(BKJ-1,BKI-2)&&pole[BKJ-1][BKI-2]==fn) return true; 1131 if (getpole(BKJ-1,BKI+2)&&pole[BKJ-1][BKI+2]==fn) return true; 1132 if (getpole(BKJ-2,BKI-1)&&pole[BKJ-2][BKI-1]==fn) return true; 1133 if (getpole(BKJ-2,BKI+1)&&pole[BKJ-2][BKI+1]==fn) return true; 1134 if (getpole(BKJ+1,BKI-1)&&pole[BKJ+1][BKI-1]==fp) return true; 1135 if (getpole(BKJ+1,BKI+1)&&pole[BKJ+1][BKI+1]==fp) return true; 1136 } 1137 if (abs(BKJ-WKJ)<=1&&abs(BKI-WKI)<=1) return true; // 1138 return false; 1139} 1140//**************************** 1141boolean getpole(char j,char i) { // 1142 if (j>=0&&j<8&&i>=0&&i<8) return true; 1143 return false; 1144} 1145//**************************** 1146void addstep(char j1,char i1,char j2,char i2,char type) { 1147 int st=start_var+cur_var; 1148 steps[st].x1=j1; 1149 steps[st].x2=j2; 1150 steps[st].y1=i1; 1151 steps[st].y2=i2; 1152 steps[st].fig1=pole[j1][i1]; 1153 steps[st].fig2=pole[j2][i2]; 1154 if (type==1) { // 1155 steps[st].fig2=-steps[st].fig1; 1156 } 1157 steps[st].type=type; 1158 char ko=-fk; // 1159 if (steps[st].fig1>0) ko=fk; 1160 movestep(st); 1161 if (get_check(ko)) { backstep(st); return; } // - 1162 boolean che=get_check(-ko); // 1163 backstep(st); 1164 steps[st].weight=abs(steps[st].fig2)-abs(steps[st].fig1); 1165 if (type>3) steps[st].weight+=fig_weight[type-2]; 1166 if (endspiel&&steps[st].fig1==ko) steps[st].weight+=10; // - 1167 steps[st].check=che; 1168 if (che) steps[st].weight+=10; 1169 if (only_action) { 1170 if (steps[st].fig1==fp&&steps[st].x2==1||steps[st].fig1==-fp&&steps[st].x2==6) // 1171 { cur_var++; return; } 1172 if (steps[st].fig2==0&&steps[st].type<4&&!che&&!check_on_table) return; 1173 } 1174 1175 cur_var++; 1176} 1177//**************************** 1178void getrowstepsw(char j,char i,char dj,char di) { // 1179char d,j1,i1; 1180 j1=j; i1=i; 1181 for (d=1;d<8;d++) { 1182 j1+=dj; i1+=di; 1183 if (getpole(j1,i1)) { 1184 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1185 if (pole[j1][i1]!=0) break; 1186 } else break; 1187 } 1188} 1189//**************************** 1190void getrowstepsb(char j,char i,char dj,char di) { // 1191char d,j1,i1; 1192 j1=j; i1=i; 1193 for (d=1;d<8;d++) { 1194 j1+=dj; i1+=di; 1195 if (getpole(j1,i1)) { 1196 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1197 if (pole[j1][i1]!=0) break; 1198 } else break; 1199 } 1200} 1201//**************************** 1202void getonestepw(char j,char i,char dj,char di) { // 1203char j1,i1; 1204 j1=j+dj; i1=i+di; 1205 if (getpole(j1,i1)) 1206 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1207} 1208//**************************** 1209void getonestepb(char j,char i,char dj,char di) { // 1210char j1,i1; 1211 j1=j+dj; i1=i+di; 1212 if (getpole(j1,i1)) 1213 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1214} 1215//**************************** 1216void sort_variants(int from, int to) { // 1217 while (1) { 1218 int mov=0; 1219 for (int i=from;i<to;i++) // 1220 if (steps[i].weight<steps[i+1].weight) { 1221 mov++; 1222 step_type buf=steps[i]; 1223 steps[i]=steps[i+1]; 1224 steps[i+1]=buf; 1225 } 1226 if (mov==0) break; 1227 } 1228} 1229//**************************** 1230void load_variants(int nstep) { // nstep 1231 cur_var=0; 1232 if (pole[WKJ][WKI]!=fk||pole[BKJ][BKI]!=-fk) kingpositions(); 1233 if (nstep%2==1) check_on_table=get_check(fk); 1234 else check_on_table=get_check(-fk); // 1235 for (char i=0;i<8;i++) 1236 for (char j=0;j<8;j++) 1237 if (pole[j][i]>0&&nstep%2==1||pole[j][i]<0&&nstep%2==0) { 1238 switch (pole[j][i]) { 1239 case fp: // 1240 if (getpole(j-1,i)&&pole[j-1][i]==0) 1241 if (j!=1) addstep(j,i,j-1,i,0); else 1242 for (char t=4;t<8;t++) addstep(1,i,0,i,t); //-... 1243 if (j==6&&pole[j-1][i]==0&&pole[j-2][i]==0) addstep(j,i,j-2,i,0); 1244 if (getpole(j-1,i-1)&&pole[j-1][i-1]<0) 1245 if (j!=1) addstep(j,i,j-1,i-1,0); else 1246 for (char t=4;t<8;t++) addstep(j,i,j-1,i-1,t); //-... 1247 if (getpole(j-1,i+1)&&pole[j-1][i+1]<0) 1248 if (j!=1) addstep(j,i,j-1,i+1,0); else 1249 for (char t=4;t<8;t++) addstep(j,i,j-1,i+1,t); //-... 1250 if (j==3&&steps[nstep-1].fig1==-fp&&steps[nstep-1].x2==3&&steps[nstep-1].x1==1) { 1251 if (steps[nstep-1].y2-i==1) { // 1252 addstep(j,i,j-1,i+1,1); 1253 } else if (steps[nstep-1].y2-i==-1) { // 1254 addstep(j,i,j-1,i-1,1); 1255 } 1256 } 1257 break; 1258 case -fp: // 1259 if (getpole(j+1,i)&&pole[j+1][i]==0) 1260 if (j!=6) addstep(j,i,j+1,i,0); else 1261 for (char t=4;t<8;t++) addstep(j,i,j+1,i,t); //-... 1262 if (j==1&&pole[j+1][i]==0&&pole[j+2][i]==0) addstep(j,i,j+2,i,0); 1263 if (getpole(j+1,i-1)&&pole[j+1][i-1]>0) 1264 if (j!=6) addstep(j,i,j+1,i-1,0); else 1265 for (char t=4;t<8;t++) addstep(j,i,j+1,i-1,t); //-... 1266 if (getpole(j+1,i+1)&&pole[j+1][i+1]>0) 1267 if (j!=6) addstep(j,i,j+1,i+1,0); else 1268 for (char t=4;t<8;t++) addstep(j,i,j+1,i+1,t); //-... 1269 if (j==4&&steps[nstep-1].fig1==fp&&steps[nstep-1].x2==4&&steps[nstep-1].x1==6) { 1270 if (steps[nstep-1].y2-i==1) { // 1271 addstep(j,i,j+1,i+1,1); 1272 } else if (steps[nstep-1].y2-i==-1) { // 1273 addstep(j,i,j+1,i-1,1); 1274 } 1275 } 1276 break; 1277 case fn: // 1278 getonestepw(j,i,-2,-1); 1279 getonestepw(j,i,-2,1); 1280 getonestepw(j,i,-1,-2); 1281 getonestepw(j,i,-1,2); 1282 getonestepw(j,i,2,-1); 1283 getonestepw(j,i,2,1); 1284 getonestepw(j,i,1,-2); 1285 getonestepw(j,i,1,2); 1286 break; 1287 case -fn: // 1288 getonestepb(j,i,-2,-1); 1289 getonestepb(j,i,-2,1); 1290 getonestepb(j,i,-1,-2); 1291 getonestepb(j,i,-1,2); 1292 getonestepb(j,i,2,-1); 1293 getonestepb(j,i,2,1); 1294 getonestepb(j,i,1,-2); 1295 getonestepb(j,i,1,2); 1296 break; 1297 case fb: // 1298 getrowstepsw(j,i,1,1); 1299 getrowstepsw(j,i,-1,-1); 1300 getrowstepsw(j,i,1,-1); 1301 getrowstepsw(j,i,-1,1); 1302 break; 1303 case -fb: // 1304 getrowstepsb(j,i,1,1); 1305 getrowstepsb(j,i,-1,-1); 1306 getrowstepsb(j,i,1,-1); 1307 getrowstepsb(j,i,-1,1); 1308 break; 1309 case fr: // 1310 getrowstepsw(j,i,1,0); 1311 getrowstepsw(j,i,-1,0); 1312 getrowstepsw(j,i,0,1); 1313 getrowstepsw(j,i,0,-1); 1314 break; 1315 case -fr: // 1316 getrowstepsb(j,i,1,0); 1317 getrowstepsb(j,i,-1,0); 1318 getrowstepsb(j,i,0,1); 1319 getrowstepsb(j,i,0,-1); 1320 break; 1321 case fq: // 1322 getrowstepsw(j,i,1,1); 1323 getrowstepsw(j,i,-1,-1); 1324 getrowstepsw(j,i,1,-1); 1325 getrowstepsw(j,i,-1,1); 1326 getrowstepsw(j,i,1,0); 1327 getrowstepsw(j,i,-1,0); 1328 getrowstepsw(j,i,0,1); 1329 getrowstepsw(j,i,0,-1); 1330 break; 1331 case -fq: // 1332 getrowstepsb(j,i,1,1); 1333 getrowstepsb(j,i,-1,-1); 1334 getrowstepsb(j,i,1,-1); 1335 getrowstepsb(j,i,-1,1); 1336 getrowstepsb(j,i,1,0); 1337 getrowstepsb(j,i,-1,0); 1338 getrowstepsb(j,i,0,1); 1339 getrowstepsb(j,i,0,-1); 1340 break; 1341 case fk: // 1342 getonestepw(j,i, 1,-1); 1343 getonestepw(j,i, 1, 0); 1344 getonestepw(j,i, 1, 1); 1345 getonestepw(j,i, 0,-1); 1346 getonestepw(j,i, 0, 1); 1347 getonestepw(j,i,-1,-1); 1348 getonestepw(j,i,-1, 0); 1349 getonestepw(j,i,-1, 1); 1350 if (!check_on_table) add_rok(j,i,nstep); 1351 break; 1352 case -fk: // 1353 getonestepb(j,i, 1,-1); 1354 getonestepb(j,i, 1, 0); 1355 getonestepb(j,i, 1, 1); 1356 getonestepb(j,i, 0,-1); 1357 getonestepb(j,i, 0, 1); 1358 getonestepb(j,i,-1,-1); 1359 getonestepb(j,i,-1, 0); 1360 getonestepb(j,i,-1, 1); 1361 if (!check_on_table) add_rok(j,i,nstep); 1362 break; 1363 } //switch 1364 } //if 1365 1366 if (nstep>cur_step) { 1367 for (int i=start_var;i<start_var+cur_var;i++) { // 1368 for (char j=0;j<MAXCUTS;j++) 1369 if (cuts[j].fig1==steps[i].fig1&& 1370 cuts[j].x1==steps[i].x1&&cuts[j].y1==steps[i].y1&& 1371 cuts[j].x2==steps[i].x2&&cuts[j].y2==steps[i].y2) { 1372 steps[i].weight+=100+cuts[j].weight; 1373 break; 1374 } 1375 if (nstep>1&&steps[nstep-1].fig2!=0) // - 1376 if (steps[nstep-1].x2==steps[i].x2&&steps[nstep-1].y2==steps[i].y2) 1377 steps[i].weight+=1000; 1378 } 1379 } 1380 1381 sort_variants(start_var,start_var+cur_var-1); 1382 1383 } 1384//**************************** 1385void movestep(int nstep) { 1386 pole[steps[nstep].x1][steps[nstep].y1]=0; 1387 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig1; 1388 if (steps[nstep].fig1==fk) { 1389 WKJ=steps[nstep].x2; WKI=steps[nstep].y2; 1390 } else if (steps[nstep].fig1==-fk) { 1391 BKJ=steps[nstep].x2; BKI=steps[nstep].y2; 1392 } 1393 if (steps[nstep].type==0) return; 1394 if (steps[nstep].type==1) // 1395 if (steps[nstep].fig1>0) 1396 pole[steps[nstep].x2+1][steps[nstep].y2]=0; 1397 else 1398 pole[steps[nstep].x2-1][steps[nstep].y2]=0; 1399 else if (steps[nstep].type==2) // 1400 if (steps[nstep].fig1>0) { // 1401 pole[7][4]=0; pole[7][5]=fr; pole[7][6]=fk; pole[7][7]=0; //show_board(); delay(3000); 1402 } else { // 1403 pole[0][4]=0; pole[0][5]=-fr; pole[0][6]=-fk; pole[0][7]=0; //show_board(); delay(3000); 1404 } 1405 else if (steps[nstep].type==3) // 1406 if (steps[nstep].fig1>0) { // 1407 pole[7][0]=0; pole[7][1]=0; pole[7][2]=fk; pole[7][3]=fr; pole[7][4]=0; 1408 } else { // 1409 pole[0][0]=0; pole[0][1]=0; pole[0][2]=-fk; pole[0][3]=-fr; pole[0][4]=0; 1410 } 1411 else if (steps[nstep].type>3) //-.... 1412 if (steps[nstep].fig1>0) pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].type-2; 1413 else pole[steps[nstep].x2][steps[nstep].y2]=2-steps[nstep].type; 1414} 1415//**************************** 1416void backstep(int nstep) { 1417 pole[steps[nstep].x1][steps[nstep].y1]=steps[nstep].fig1; 1418 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig2; 1419 if (steps[nstep].fig1==fk) { 1420 WKJ=steps[nstep].x1; WKI=steps[nstep].y1; 1421 } else if (steps[nstep].fig1==-fk) { 1422 BKJ=steps[nstep].x1; BKI=steps[nstep].y1; 1423 } 1424 if (steps[nstep].type==0) return; 1425 if (steps[nstep].type==1) { // 1426 pole[steps[nstep].x2][steps[nstep].y2]=0; 1427 if (steps[nstep].fig1>0) 1428 pole[steps[nstep].x2+1][steps[nstep].y2]=-fp; 1429 else 1430 pole[steps[nstep].x2-1][steps[nstep].y2]=fp; 1431 } else if (steps[nstep].type==2) // 1432 if (steps[nstep].fig1>0) { // 1433 pole[7][4]=fk; pole[7][5]=0; pole[7][6]=0; pole[7][7]=fr; 1434 } else { // 1435 pole[0][4]=-fk; pole[0][5]=0; pole[0][6]=0; pole[0][7]=-fr; // show_board(); delay(3000); 1436 } 1437 else if (steps[nstep].type==3) // 1438 if (steps[nstep].fig1>0) { // 1439 pole[7][0]=fr; pole[7][1]=0; pole[7][2]=0; pole[7][3]=0; pole[7][4]=fk; 1440 } else { // 1441 pole[0][0]=-fr; pole[0][1]=0; pole[0][2]=0; pole[0][3]=0; pole[0][4]=-fk; 1442 } 1443 1444} 1445//**************************** 1446void get_wrocks(int nstep) { 1447 w00=true; w000=true; 1448 for (int i=1;i<nstep;i++) { 1449 if (steps[i].fig1==fk) { w00=false; w000=false; return; } 1450 if (steps[i].fig1==fr) 1451 if (steps[i].x1==7&&steps[i].y1==7) w00=false; 1452 else if (steps[i].x1==7&&steps[i].y1==0) w000=false; 1453 } 1454} 1455//**************************** 1456void get_brocks(int nstep) { 1457 b00=true; b000=true; 1458 for (int i=1;i<nstep;i++) { 1459 if (steps[i].fig1==-fk) { b00=false; b000=false; return; } 1460 if (steps[i].fig1==-fr) 1461 if (steps[i].x1==0&&steps[i].y1==7) b00=false; 1462 else if (steps[i].x1==0&&steps[i].y1==0) b000=false; 1463 } 1464} 1465//**************************** 1466void add_rok(char j,char i,int nstep) { // 1467boolean che1,che2; 1468 if (nstep%2==1) { // 1469 if (j!=7||i!=4) return; 1470 if (pole[7][5]==0&&pole[7][6]==0&&pole[7][7]==fr) { // 1471 pole[7][4]=0; 1472 WKI=5; che1=get_check(fk); 1473 WKI=6; che2=get_check(fk); 1474 WKI=4; pole[7][4]=fk; 1475 get_wrocks(nstep); 1476 if (!che1&&!che2&&w00) addstep(7,4,7,6,2); 1477 } 1478 if (pole[7][0]==fr&&pole[7][1]==0&&pole[7][2]==0&&pole[7][3]==0) { // 1479 pole[7][4]=0; 1480 WKI=2; che1=get_check(fk); 1481 WKI=3; che2=get_check(fk); 1482 WKI=4; pole[7][4]=fk; 1483 get_wrocks(nstep); 1484 if (!che1&&!che2&&w000) addstep(7,4,7,2,3); 1485 } 1486 } else { // 1487 if (j!=0||i!=4) return; 1488 if (pole[0][5]==0&&pole[0][6]==0&&pole[0][7]==-fr) { // 1489 pole[0][4]=0; 1490 BKI=5; che1=get_check(-fk); 1491 BKI=6; che2=get_check(-fk); 1492 BKI=4; pole[0][4]=-fk; 1493 get_brocks(nstep); 1494 if (!che1&&!che2&&b00) addstep(0,4,0,6,2); 1495 } 1496 if (pole[0][0]==-fr&&pole[0][1]==0&&pole[0][2]==0&&pole[0][3]==0) { // 1497 pole[0][4]=0; 1498 BKI=2; che1=get_check(-fk); 1499 BKI=3; che2=get_check(-fk); 1500 BKI=4; pole[0][4]=-fk; 1501 get_brocks(nstep); 1502 if (!che1&&!che2&&b000) addstep(0,4,0,2,3); 1503 } 1504 1505 } 1506} 1507//**************************** 1508int addrowstepsw(char j,char i,char dj,char di,char dc) { // 1509char d,j1,i1; int c=0; 1510 j1=j; i1=i; 1511 for (d=1;d<8;d++) { 1512 j1+=dj; i1+=di; 1513 if (getpole(j1,i1)) { 1514 if (pole[j1][i1]==0) c+=dc; 1515 else if (pole[j1][i1]>0) { // 1516 c+=1; break; 1517 } else { 1518 if (cur_step>6) c+=-pole[j1][i1]; // 1519 break; 1520 } 1521 } else break; 1522 } 1523 return c; 1524} 1525//**************************** 1526int addrowstepsb(char j,char i,char dj,char di,char dc) { // 1527char d,j1,i1; int c=0; 1528 j1=j; i1=i; 1529 for (d=1;d<8;d++) { 1530 j1+=dj; i1+=di; 1531 if (getpole(j1,i1)) { 1532 if (pole[j1][i1]==0) c-=dc; 1533 else if (pole[j1][i1]<0) { // 1534 c-=1; break; 1535 } else { 1536 if (cur_step>6) c-=pole[j1][i1]; // 1537 break; 1538 } 1539 } else break; 1540 } 1541 return c; 1542} 1543//**************************** 1544int addonestepw(char j,char i,char dj,char di) { // 1545char j1,i1; 1546 j1=j+dj; i1=i+di; 1547 if (getpole(j1,i1)) 1548 if (pole[j1][i1]==0) return 2; 1549 else if (pole[j1][i1]>0) return 1; 1550 else return -pole[j1][i1]; 1551 return 0; 1552} 1553//**************************** 1554int addonestepb(char j,char i,char dj,char di) { // 1555char j1,i1; 1556 j1=j+dj; i1=i+di; 1557 if (getpole(j1,i1)) 1558 if (pole[j1][i1]==0) return 2; 1559 else if (pole[j1][i1]<0) return 1; 1560 else return pole[j1][i1]; 1561 return 0; 1562} 1563//**************************** 1564int activity() { // - + 1565int c=0; 1566char pwj[8],pwi[8],pbj[8],pbi[8],ipw=0,ipb=0,nbw=0,nbb=0; 1567 if (checkmatesearch) return 0; // 1568 for (char i=0;i<8;i++) 1569 for (char j=0;j<8;j++) 1570 if (pole[j][i]!=0) { 1571 switch (pole[j][i]) { 1572 case fp: // 1573 if (getpole(j,i+1)&&pole[j][i+1]==fp) c+=2; // 1574 if (getpole(j-1,i)&&pole[j-1][i]==fp) c-=20; // 1575 pwj[ipw]=j; pwi[ipw]=i; ipw++; 1576 break; 1577 case -fp: // 1578 if (getpole(j,i-1)&&pole[j][i-1]==-fp) c-=2; // 1579 if (getpole(j+1,i)&&pole[j+1][i]==-fp) c+=20; // 1580 pbj[ipb]=j; pbi[ipb]=i; ipb++; 1581 break; 1582 case fn: // 1583 c+=addonestepw(j,i,-2,-1); 1584 c+=addonestepw(j,i,-2,1); 1585 c+=addonestepw(j,i,-1,-2); 1586 c+=addonestepw(j,i,-1,2); 1587 c+=addonestepw(j,i,2,-1); 1588 c+=addonestepw(j,i,2,1); 1589 c+=addonestepw(j,i,1,-2); 1590 c+=addonestepw(j,i,1,2); 1591 break; 1592 case -fn: // 1593 c+=addonestepb(j,i,-2,-1); 1594 c+=addonestepb(j,i,-2,1); 1595 c+=addonestepb(j,i,-1,-2); 1596 c+=addonestepb(j,i,-1,2); 1597 c+=addonestepb(j,i,2,-1); 1598 c+=addonestepb(j,i,2,1); 1599 c+=addonestepb(j,i,1,-2); 1600 c+=addonestepb(j,i,1,2); 1601 break; 1602 case fb: // 1603 c+=addrowstepsw(j,i,1,1,1); 1604 c+=addrowstepsw(j,i,-1,-1,2); 1605 c+=addrowstepsw(j,i,1,-1,1); 1606 c+=addrowstepsw(j,i,-1,1,2); 1607 nbw++; 1608 break; 1609 case -fb: // 1610 c+=addrowstepsb(j,i,1,1,2); 1611 c+=addrowstepsb(j,i,-1,-1,1); 1612 c+=addrowstepsb(j,i,1,-1,2); 1613 c+=addrowstepsb(j,i,-1,1,1); 1614 nbb++; 1615 break; 1616 case fr: // 1617 c+=addrowstepsw(j,i,1,0,2); // 1618 c+=addrowstepsw(j,i,-1,0,2); 1619 c+=addrowstepsw(j,i,0,1,1); 1620 c+=addrowstepsw(j,i,0,-1,1); 1621 break; 1622 case -fr: // 1623 c+=addrowstepsb(j,i,1,0,2); // 1624 c+=addrowstepsb(j,i,-1,0,2); 1625 c+=addrowstepsb(j,i,0,1,1); 1626 c+=addrowstepsb(j,i,0,-1,1); 1627 break; 1628 case fq: // 1629 if (cur_step>10) { 1630 c+=addrowstepsw(j,i,1,1,1); 1631 c+=addrowstepsw(j,i,-1,-1,2); 1632 c+=addrowstepsw(j,i,1,-1,1); 1633 c+=addrowstepsw(j,i,-1,1,2); 1634 c+=addrowstepsw(j,i,1,0,1); 1635 c+=addrowstepsw(j,i,-1,0,2); 1636 c+=addrowstepsw(j,i,0,1,1); 1637 c+=addrowstepsw(j,i,0,-1,1); 1638 } 1639 break; 1640 case -fq: // 1641 if (cur_step>10) { 1642 c+=addrowstepsb(j,i,1,1,2); 1643 c+=addrowstepsb(j,i,-1,-1,1); 1644 c+=addrowstepsb(j,i,1,-1,2); 1645 c+=addrowstepsb(j,i,-1,1,1); 1646 c+=addrowstepsb(j,i,1,0,2); 1647 c+=addrowstepsb(j,i,-1,0,1); 1648 c+=addrowstepsb(j,i,0,1,1); 1649 c+=addrowstepsb(j,i,0,-1,1); 1650 } 1651 break; 1652 } //switch 1653 if (cur_step>6) { 1654 if (pole[j][i]>0) { // 1655 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c+=3; 1656 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c+=3; 1657 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c+=2; 1658 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c+=2; 1659 } else { // 1660 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c-=3; 1661 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c-=3; 1662 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c-=2; 1663 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c-=2; 1664 } 1665 } 1666 } 1667 if (nbw>1) c+=30; // 1668 if (nbb>1) c-=30; 1669 for (char w=0;w<ipw;w++) { // 1670 boolean pass=1; 1671 for (char b=0;b<ipb;b++) { 1672 if (pwi[w]>0&&pbi[b]==pwi[w]-1&&pbj[b]<pwj[w]) pass=0; // 1673 if (pbi[b]==pwi[w]&&pbj[b]<pwj[w]) pass=0; // 1674 if ( pwi[w]<7&&pbi[b]==pwi[w]+1&&pbj[b]<pwj[w]) pass=0; // 1675 if (!pass) break; 1676 } 1677 if (pass) { 1678 c+=50; 1679 if (!endspiel) break; // 1680 } 1681 } 1682 for (char b=0;b<ipb;b++) { // 1683 boolean pass=1; 1684 for (char w=0;w<ipw;w++) { 1685 if (pbi[b]>0&&pwi[w]==pbi[b]-1&&pwj[w]>pbj[b]) pass=0; // 1686 if (pwi[w]==pbi[b]&&pwj[w]>pbj[b]) pass=0; // 1687 if (pbi[b]<7&&pwi[w]==pbi[b]+1&&pwj[w]>pbj[b]) pass=0; // 1688 if (!pass) break; 1689 } 1690 if (pass) { 1691 c-=50; 1692 if (!endspiel) break; // 1693 } 1694 } 1695 return c; 1696} 1697//**************************** 1698int evaluate(int nstep) { // 1699 long ww=0, wb=0; 1700 for (char i=0;i<8;i++) 1701 for (char j=0;j<8;j++) 1702 if (pole[j][i]<0) { 1703 wb+=fig_weight[-pole[j][i]]+(int)pgm_read_word(&pos[-pole[j][i]-1][7-j][i]); 1704 } 1705 else if (pole[j][i]>0) { 1706 ww+=fig_weight[pole[j][i]]+(int)pgm_read_word(&pos[pole[j][i]-1][j][i]); 1707 } 1708 count++; // 1709 long str=activity(); 1710 if (endspiel) { // 1711 wb+=(int)pgm_read_word(&pos[6][7-BKJ][BKI])-(int)pgm_read_word(&pos[5][7-BKJ][BKI]); 1712 ww+=(int)pgm_read_word(&pos[6][WKJ][WKI])-(int)pgm_read_word(&pos[5][WKJ][WKI]); 1713 if (wb<450&&ww>450) { // - - 1714 str-=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1715 } else if (ww<450&&wb>450) { // 1716 str+=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 1717 } 1718 } 1719 if (nstep>8) { // 1720 if (steps[nstep-1].fig1==steps[nstep-5].fig1&&steps[nstep-2].fig1==steps[nstep-6].fig1&& 1721 steps[nstep-3].fig1==steps[nstep-7].fig1&&steps[nstep-4].fig1==steps[nstep-8].fig1) 1722 if (steps[nstep-1].x1==steps[nstep-5].x1&&steps[nstep-2].x1==steps[nstep-6].x1&& 1723 steps[nstep-3].x1==steps[nstep-7].x1&&steps[nstep-4].x1==steps[nstep-8].x1) 1724 if (steps[nstep-1].x2==steps[nstep-5].x2&&steps[nstep-2].x2==steps[nstep-6].x2&& 1725 steps[nstep-3].x2==steps[nstep-7].x2&&steps[nstep-4].x2==steps[nstep-8].x2) 1726 if (steps[nstep-1].y1==steps[nstep-5].y1&&steps[nstep-2].y1==steps[nstep-6].y1&& 1727 steps[nstep-3].y1==steps[nstep-7].y1&&steps[nstep-4].y1==steps[nstep-8].x1) 1728 if (steps[nstep-1].y2==steps[nstep-5].y2&&steps[nstep-2].y2==steps[nstep-6].y2&& 1729 steps[nstep-3].y2==steps[nstep-7].y2&&steps[nstep-4].y2==steps[nstep-8].y2) 1730 { Serial.println(F(" Draw - 3 repeat")); return 0; } 1731 } 1732 if (nstep%2==1) return 5000*(ww-wb)/(ww+wb+2000)+str; else return 5000*(wb-ww)/(ww+wb+2000)-str; 1733} 1734//**************************** 1735void add_cut(int ind) {// ind 1736 int minbeta=30000, minindex=MAXCUTS-1; 1737 for (char i=0;i<MAXCUTS;i++) { 1738 if (cuts[i].weight==0) { minindex=i; break; }// 1739 if (cuts[i].fig1==steps[ind].fig1&& 1740 cuts[i].x1==steps[ind].x1&&cuts[i].y1==steps[ind].y1&& 1741 cuts[i].x2==steps[ind].x2&&cuts[i].y2==steps[ind].y2) { 1742 cuts[i].weight++; return; // - 1 1743 } 1744 if (cuts[i].weight<minbeta) { // 1745 minbeta=cuts[i].weight; 1746 minindex=i; 1747 } 1748 } 1749 cuts[minindex]=steps[ind]; 1750 cuts[minindex].weight=1; //1 1751} 1752//**************************** 1753int quiescence(int start, int nstep, int alpha, int beta ) { 1754 if (nstep-cur_step>=LIMDEPTH||start>MAXSTEPS-70) return evaluate(nstep); 1755 if (!solving&&progress==0) return -5000; 1756 int score=-20000; 1757 start_var=start; 1758 only_action=true; 1759 load_variants(nstep); 1760 if (!check_on_table) { 1761 int stand_pat = evaluate(nstep); 1762 if (stand_pat >= score) score=stand_pat; 1763 if (score>alpha) alpha=score; 1764 if (alpha>=beta) return alpha; 1765 } 1766 if (cur_var==0) { 1767 if (check_on_table) { 1768 if (TRACE) Serial.println(F("checkmate?")); 1769 return -10000+nstep-cur_step; 1770 } else return evaluate(nstep); 1771 } 1772 int j=start+cur_var; 1773 for (int i=start;i<j;i++) { // 1774 if (TRACE) { //***** 1775 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" . ")); 1776 Serial.println(str_step(i)); 1777 } 1778 movestep(i); 1779 steps[nstep]=steps[i]; 1780 int tmp=-quiescence(j+1,nstep+1,-beta,-alpha); 1781 backstep(i); 1782 if (tmp>score) score=tmp; 1783 if (score>alpha) alpha=score; 1784 if (alpha>=beta ) { 1785 add_cut(nstep); 1786 return alpha; 1787 } 1788 if (checkmatesearch&&alpha>9000) break; 1789 gui(); 1790 } 1791 return score; 1792} 1793//**************************** 1794int alphaBeta(int start, int nstep, int alpha, int beta, int depthleft) { 1795//start - 1796//nstep - 1797int score=-20000,best; 1798 if( depthleft==0) return quiescence(start,nstep,alpha,beta) ; 1799 if (start>MAXSTEPS-70) return evaluate(nstep); 1800 start_var=start; 1801 only_action=false; 1802 if (nstep!=cur_step) load_variants(nstep); 1803 if (cur_var==0) { 1804 if (check_on_table) { 1805 if (TRACE) Serial.println(F("checkmate!")); 1806 return -10000+nstep-cur_step; 1807 } 1808 return 0; 1809 } 1810 int j=start+cur_var; 1811 best=start; 1812 for (int i=start;i<j;i++) { // 1813 if (nstep==cur_step) { 1814 Serial.print(str_step(i)); Serial.print(" "); 1815 Serial.print(i-start+1); Serial.print("/"); Serial.print(j-start); 1816 if (steps[i].weight<-9000) { Serial.println(F(" checkmate")); continue; } 1817 if (steps[i].fig2!=0||steps[i].check||alpha<-100) 1818 // , , -100 1819 { LIMDEPTH=MAXDEPTH+2; Serial.print(F("+2")); } else LIMDEPTH=MAXDEPTH; 1820 } else { 1821 if (TRACE) { //***** 1822 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" ")); 1823 Serial.println(str_step(i)); 1824 } 1825 } 1826 movestep(i); 1827 steps[nstep]=steps[i]; 1828 int tmp=-alphaBeta(j+1,nstep+1,-beta,-alpha,depthleft-1); 1829 backstep(i); 1830 steps[i].weight=tmp; 1831 if (tmp>score) score=tmp; 1832 if (score>alpha) { 1833 alpha=score; 1834 if (nstep>cur_step) add_cut(nstep); // 1835 if (TRACE) { Serial.print(F("ALPHA+:")); Serial.println(score); } 1836 best=i; 1837 if (nstep==cur_step) { 1838 steps[0]=steps[best]; 1839 Serial.print(F(" BEST")); 1840 } 1841 } 1842 if (alpha>=beta ) { 1843 if (nstep>cur_step) add_cut(nstep); // 1844 if (TRACE) { Serial.print(F("BETA CUT:")); Serial.println(score); } 1845 return alpha; 1846 } 1847 if (nstep==cur_step) { 1848 Serial.print(F(" ")); Serial.println(tmp); 1849 progress=100*(i-start+1)/(j-start); 1850 if (alpha==9999||alpha==-5000) break; 1851 if (checkmatesearch&&alpha>9000&&limit<3) break; 1852 if (!solving) { 1853 if (alpha>startweight&&cur_level>1) break; 1854 else { // - +50% 1855 if (BAction.bs==F("stopping")) break; 1856 if (cur_level>1&&100*(millis()-starttime)/(limittime-starttime)>240-limit*20) break; 1857 } 1858 } 1859 } 1860 if (nstep==cur_step+1&&!solving&&progress==0) break; 1861 gui(); 1862 } 1863 if (nstep==cur_step) { steps[nstep]=steps[best]; steps[0]=steps[best]; } 1864 return score; 1865} 1866//**************************** 1867void kingpositions() { 1868 for (char i=0;i<8;i++) // 1869 for (char j=0;j<8;j++) 1870 if (pole[j][i]==fk) { 1871 WKJ=j; WKI=i; 1872 } else if (pole[j][i]==-fk) { 1873 BKJ=j; BKI=i; 1874 } 1875} 1876//**************************** 1877int get_endspiel() { // 1878 int weight=0; 1879 for (char i=0;i<8;i++) 1880 for (char j=0;j<8;j++) 1881 if (pole[j][i]<0) 1882 weight+=fig_weight[-pole[j][i]]; 1883 else if (pole[j][i]>0) 1884 weight+=fig_weight[pole[j][i]]; // 8000 1885 if (weight<3500) endspiel=true; else endspiel=false; 1886 return weight; 1887} 1888//**************************** 1889boolean is_drawn() { // 1890 boolean drawn=false; 1891 int cn=0,cbw=0,cbb=0,co=0,cb=0,cw=0; 1892 for (char i=0;i<8;i++) 1893 for (char j=0;j<8;j++) { 1894 if (abs(pole[j][i])==1) co++; 1895 if (abs(pole[j][i])>3&&abs(pole[j][i])<6) co++; // , , 1896 if (abs(pole[j][i])==6) continue; // 1897 if (abs(pole[j][i])==2) cn++; // 1898 if (abs(pole[j][i])==3&&(i+j+2)%2==0) cbb++; // 1899 if (abs(pole[j][i])==3&&(i+j+2)%2==1) cbw++; // 1900 if (pole[j][i]==3) cw++; // 1901 if (pole[j][i]==-3) cb++; // 1902 } 1903 if (cn==1&&co+cbb+cbw==0) drawn=true; // 1904 if (cbb+cbw==1&&co+cn==0) drawn=true; // 1905 if (co+cn+cbb==0||co+cn+cbw==0) drawn=true; // 1906 if (co+cn==0&&cb==1&&cw==1) drawn=true; // 1907 if (drawn) return drawn; 1908 int rep=1; 1909 for (int s=cur_step-1; s>0; s--) { 1910 if (steps[s].fig1==0) continue; 1911 backstep(s); 1912 boolean eq=true; 1913 for (char i=0;i<8;i++) 1914 for (char j=0;j<8;j++) 1915 if (pole[j][i]!=pole0[j][i]) 1916 { eq=false; break; } 1917 if (eq) rep++; 1918 if (rep>2) break; 1919 } 1920 if (rep>1) { Serial.print(rep); Serial.println(" repetitions"); } 1921 for (char i=0;i<8;i++) 1922 for (char j=0;j<8;j++) pole[j][i]=pole0[j][i]; // 1923 if (rep>2) drawn=true; 1924 return drawn; 1925} 1926//**************************** 1927int solve_step() { 1928const int LMIN[6]={2,3,4,5,6,7}; 1929const int LMAX[6]={4,6,8,10,12,14}; 1930const int LCMIN[6]={2,4,6,8,10,12}; 1931const int LCMAX[6]={4,6,8,10,12,14}; 1932boolean check_on; 1933 tft.fillRect(0,210,60,10,BLACK); 1934 tft.drawFastHLine(5,210,200,DARK); 1935 starttime=millis(); 1936 isstatus=1; 1937 deletebuttons(); 1938 BAction.Show("STOP"); 1939 BBW.Hide(); 1940 BBack.Hide(); 1941 BLimit.Hide(); 1942 BCM.Hide(); 1943 BDesc.Hide(); 1944 BSound.Hide(); 1945 BRotate.Hide(); 1946 limittime=starttime+limits[limit]*1000; // 1947 for (char i=0;i<8;i++) 1948 for (char j=0;j<8;j++) pole0[j][i]=pole[j][i]; // 1949 lastbest.fig1=0; 1950 kingpositions(); 1951 int wei=get_endspiel(); 1952 count=0; 1953 startweight=evaluate(cur_step); 1954 Serial.println(""); 1955 Serial.println(F("---------------")); 1956 if (endspiel) { Serial.print(F("Endspiel: ")); Serial.println(wei); } 1957 if (cur_step%2==1) Serial.print(F("WHITE, ")); else Serial.print(F("BLACK, ")); 1958 Serial.print(F("start Score= ")); Serial.println(startweight); 1959 start_var=cur_step+21; 1960 only_action=false; 1961 lastbest.fig1=0; 1962 steps[0].fig1=0; 1963 load_variants(cur_step); 1964 check_on=check_on_table; 1965 if (cur_var==0||is_drawn()) { 1966 beep(500); 1967 tft.drawFastHLine(5,210,200,BLACK); 1968 tft.drawFastHLine(5,212,200,BLACK); 1969 BAction.Hide(); 1970 isstatus=0; 1971 solving=false; 1972 if (check_on_table) return -9999; else return 8999; 1973 } 1974 if (cur_var==1) { // 1 1975 tft.drawFastHLine(5,210,200,BLACK); 1976 tft.drawFastHLine(5,212,200,BLACK); 1977 BAction.Hide(); 1978 isstatus=0; 1979 solving=false; 1980 steps[cur_step]=steps[cur_step+21]; 1981 return startweight; 1982 } 1983 int vars=cur_var; 1984 int ALPHA=-20000; 1985 int BETA=20000; 1986 int score; 1987 solving=true; 1988 if (checkmatesearch) { 1989 Serial.println(F("Checkmate search")); 1990 steps[cur_step].fig1=0; lastscore=0; show_steps(); 1991 } 1992 int l=0; 1993 for (char i=0;i<MAXCUTS;i++) { cuts[i].weight=0; cuts[i].fig1=0; } // 1994 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // 1995 movestep(i); 1996 steps[i].weight=evaluate(cur_step); 1997 if (steps[i].fig2!=0) steps[i].weight-=steps[i].fig1; 1998 backstep(i); 1999 } 2000 while (l<6) { 2001 cur_level=l+1; 2002 progress=0; 2003 tft.drawFastHLine(5,212,200,DARK); 2004 MINDEPTH=LMIN[l]; 2005 MAXDEPTH=LMAX[l]; 2006 if (checkmatesearch) { 2007 ALPHA=9000; BETA=10000; 2008 MINDEPTH=LCMIN[l]; 2009 MAXDEPTH=LCMAX[l]; 2010 } 2011 if (l>0) Serial.println(""); Serial.print(F("******* LEVEL=")); Serial.print(l+1); 2012 Serial.print(" "); Serial.print(MINDEPTH); Serial.print("-"); Serial.print(MAXDEPTH); 2013 Serial.print(" "); Serial.print((millis()-starttime)/1000.,1); Serial.println("s"); 2014 if (checkmatesearch) { 2015 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // - 2016 movestep(i); 2017 start_var=cur_step+22+vars; 2018 only_action=false; 2019 load_variants(cur_step+1); 2020 backstep(i); 2021 steps[i].weight=-cur_var; 2022 } 2023 } 2024 sort_variants(cur_step+21,cur_step+20+vars); // 2025 cur_var=vars; 2026 check_on_table=check_on; 2027 score=alphaBeta(cur_step+21,cur_step,ALPHA,BETA,MINDEPTH); 2028 if (score>9996) break; 2029 if (score>9000&&limit<3) break; 2030 if (100*(millis()-starttime)/(limittime-starttime)>70-l*8) break; 2031 if (!solving) break; 2032 l++; 2033 } //while l 2034 if (score<-9000) { 2035 Serial.println(F("GIVE UP!")); 2036 } else { 2037 Serial.print(F("STEP=")); Serial.println(str_step(cur_step)); 2038 if (score>9000) { 2039 Serial.print(F("CHECKMATE ")); 2040 if (score<9999) { 2041 Serial.print(F("in ")); Serial.print((9999-score)/2+1); Serial.print(F(" steps")); 2042 } else steps[cur_step].check=2; 2043 Serial.println(""); 2044 if (checkmatesearch) { 2045 steps[cur_step+9999-score].check=2; 2046 steps[cur_step+10000-score].fig1=0; 2047 for (int i=cur_step;i<cur_step+10000-score;i++) Serial.println(str_step(i)); 2048 } 2049 } 2050 } 2051 tft.drawFastHLine(5,210,200,BLACK); 2052 tft.drawFastHLine(5,212,200,BLACK); 2053 progress=0; 2054 show_status(); 2055 BAction.Hide(); 2056 solving=0; 2057 isstatus=0; 2058 return score; 2059 2060 2061} 2062//**************************** 2063
ArduinoMegaChess_DUE
arduino
Sketch updated to Arduino DUE with ILI9486/XPT2046 320x480 pixels touchscreen.
1//ArduinoMega Chess 1.0 (DUE 320x480 version) 2//Sergey Urusov, ususovsv@gmail.com 3 4#include 5 <TFT_HX8357_Due.h> // Hardware-specific library 6#include <stdint.h> 7#include 8 <URTouch.h> 9#include <avr/pgmspace.h> 10#include <Streaming.h> 11#include <DueFlashStorage.h> 12 13uint16_t 14 BLACK,BLUE,RED,GREEN,CYAN,MAGENTA,YELLOW,WHITE,GRAY,DARK,GRAY2,BLACKF,WHITEF; 15 16TFT_HX8357_Due 17 tft = TFT_HX8357_Due(); 18 19URTouch myTouch( 6, 5, 4, 3, 2); 20 21DueFlashStorage 22 dueFlashStorage; 23 24int touchx,touchy,touchx0,touchy0,ntouch; 25unsigned 26 long lasttouch; 27#define MINPRESSURE 10 28#define MAXPRESSURE 1000 29const int 30 PinBuzz = 12; 31int cycle=0; // 32const signed char fp=1; 33const signed char 34 fn=2; 35const signed char fb=3; 36const signed char fr=4; 37const signed char 38 fq=5; 39const signed char fk=6; 40const int fig_weight[]={0,100,320,330,500,900,0}; 41const 42 char fig_symb[]=" NBRQK"; 43unsigned long count; 44boolean rotate=false; 45const 46 signed char polezero[8][8] PROGMEM={ 47 { 0, 0, 0, 0, 0, 0, 0, 48 0}, 49 { 0, 0, 0, 0, 0, 0, 0, 0}, 50 { 0, 0, 51 0, 0, 0, 0, 0, 0}, 52 { 0, 0, 0, 0, 0, 0, 0, 0}, 53 54 { 0, 0, 0, 0, 0, 0, 0, 0}, 55 { 0, 0, 0, 0, 56 0, 0, 0, 0}, 57 { 0, 0, 0, 0, 0, 0, 0, 0}, 58 { 59 0, 0, 0, 0, 0, 0, 0, 0}, 60 }; 61const signed char polestart[8][8] 62 PROGMEM={ 63 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 64 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 65 66 { 0, 0, 0, 0, 0, 0, 0, 0}, 67 { 0, 0, 0, 0, 68 0, 0, 0, 0}, 69 { 0, 0, 0, 0, 0, 0, 0, 0}, 70 { 71 0, 0, 0, 0, 0, 0, 0, 0}, 72 { fp, fp, fp, fp, fp, fp, fp, 73 fp}, 74 { fr, fn, fb, fq, fk, fb, fn, fr}, 75 }; 76 77 78signed char pole[8][8]={ // 3 79 {-fr,-fn,-fb,-fq,-fk,-fb,-fn,-fr}, 80 81 {-fp,-fp,-fp,-fp,-fp,-fp,-fp,-fp}, 82 { 0, 0, 0, 0, 83 0, 0, 0, 0}, 84 { 0, 0, 0, 0, 0, 0, 0, 0}, 85 { 86 0, 0, 0, 0, 0, 0, 0, 0}, 87 { 0, 0, 0, 0, 0, 0, 0, 88 0}, 89 { fp, fp, fp, fp, fp, fp, fp, fp}, 90 { fr, fn, 91 fb, fq, fk, fb, fn, fr}, 92 }; 93 94signed char pole0[8][8]; 95 // 96signed char poledisp[8][8]; // 97signed char polechoice[7]; // 98boolean 99 w00,w000,b00,b000; 100signed char blinkstep; 101typedef struct { 102 103 signed char fig1, fig2; // 104 signed char x1,y1,x2,y2; // 105 signed 106 char check; // 107 signed char type; // 1- ,2- ,3- ,4-5-6-7- ,,, 108 int 109 weight; // , 110} step_type; 111const int MAXSTEPS=1000; //. 112//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 113int 114 MINDEPTH; //. 115int MAXDEPTH; //. 116int LIMDEPTH; //. 117const signed 118 char MAXCUTS=10; //.. - - (10- , 20) 119boolean TRACE=0; 120boolean checkmatesearch=0; 121 // 122boolean solving=false; 123boolean choice=false; 124boolean sound=1; 125short 126 cur_step=1; // , 1.... 127short limit=0; // , 0-7; 128//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 129const 130 int limits[8]={3,10,30,60,180,600,1800,100000}; 131// 0 1 2 132 3 4 5 6 7 133step_type steps[MAXSTEPS]; // 134step_type cuts[MAXCUTS]; 135 // - 136step_type lastbest; 137int lastscore; 138int minbeta,maxalpha; 139int startweight; 140int 141 cur_level; // () 142int start_var; // , 1.... 143int cur_var; // , 1.... 144int 145 cur_choice; // 146boolean check_on_table; // 147boolean isstatus; 148signed 149 char WKJ=0,WKI=0,BKJ=0,BKI=0; // 150boolean endspiel=false; // 151signed char 152 progress; // 0-100 153boolean only_action=false; // - 154unsigned long starttime,limittime,quitime=0; 155 156boolean drag; 157signed char movedfig=0; 158const uint8_t fig[6][32] 159 PROGMEM={ 160{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 0x7, 0xE0, 161 0x3, 0xC0, 0x3, 0xC0, 1620x7, 0xE0, 0x3, 0xC0, 0x3, 0xC0, 0x7, 0xE0, 0xF, 163 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x0, 0x0}, // 164{0x0, 0x0, 0x3, 0x40, 0x7, 0xE0, 165 0xF, 0xF0, 0x1F, 0xF8, 0x3F, 0xFC, 0x39, 0xFC, 0x33, 0xFC, 1660x7, 0xFC, 0xF, 167 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 0xC0, 0x7, 0xE0, 0x1F, 0xF8, 0x0, 0x0}, 168 // 169{0x1, 0x80, 0x3, 0xC0, 0x1, 0x80, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 170 0x1F, 0xF8, 0x1F, 0xF8, 1710x1F, 0xF8, 0x1F, 0xF8, 0xF, 0xF0, 0x7, 0xE0, 0x3, 172 0xC0, 0x31, 0x8C, 0x7F, 0xFE, 0x0, 0x0}, // 173{0x0, 0x0, 0x19, 0x98, 0x1F, 174 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 1750xF, 0xF0, 176 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0}, 177 // 178{0x19, 0x98, 0xD9, 0x9B, 0xD9, 0x9B, 0xD9, 0x9B, 0x6D, 0xB6, 0x6D, 0xB6, 0x6D, 179 0xB6, 0x35, 0xAC, 1800x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 181 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0}, // 182{0x1, 0x80, 0x1, 0x80, 0x79, 0x9E, 0x7D, 183 0xBE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 1840x7F, 0xFE, 0x3F, 0xFC, 185 0x3F, 0xFC, 0x1F, 0xF8, 0xF, 0xF0, 0xF, 0xF0, 0x1F, 0xF8, 0x0, 0x0} // 186}; 187const 188 uint8_t fig_cont[6][32] PROGMEM={ 189{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 190 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 0x4, 0x20, 1910x8, 0x10, 0x4, 0x20, 0x4, 192 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8}, // 193{0x3, 194 0x40, 0x4, 0xA0, 0x8, 0x10, 0x12, 0x8, 0x20, 0x4, 0x40, 0x2, 0x46, 0x2, 0x4C, 195 0x2, 1960x38, 0x2, 0x10, 0x4, 0x10, 0x8, 0x8, 0x10, 0x4, 0x20, 0x18, 0x18, 197 0x20, 0x4, 0x3F, 0xFC}, // 198{0x2, 0x40, 0x4, 0x20, 0x6, 0x60, 0x10, 0x8, 0x21, 199 0x84, 0x21, 0x84, 0x21, 0x84, 0x27, 0xE4, 2000x21, 0x84, 0x21, 0x84, 0x11, 0x88, 201 0x8, 0x10, 0x34, 0x2C, 0x4E, 0x72, 0x80, 0x1, 0xFF, 0xFF}, // 202{0x39, 0x9C, 203 0x26, 0x64, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 2040x10, 205 0x8, 0x10, 0x8, 0x10, 0x8, 0x10, 0x8, 0x1F, 0xF8, 0x20, 0x4, 0x20, 0x4, 0x3F, 206 0xFC}, // 207{0x22, 0x24, 0x22, 0x24, 0x22, 0x24, 0x26, 0x64, 0x92, 0x49, 0x92, 208 0x49, 0x92, 0x49, 0x4A, 0x52, 2090x40, 0x2, 0x40, 0x2, 0x5F, 0xFA, 0x20, 0x4, 210 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 0xFC}, // 211{0x2, 0x40, 0x7A, 0x5E, 212 0x86, 0x61, 0x82, 0x41, 0x2, 0x40, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 2130x80, 214 0x81, 0x41, 0x82, 0x5F, 0xFA, 0x20, 0x4, 0x1F, 0xF8, 0x10, 0x8, 0x20, 0x4, 0x3F, 215 0xFC} // 216}; 217 218const uint8_t fig24[6][72] PROGMEM={ 219{0x0, 0x0, 0x0, 220 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1E, 0x0, 0x0, 0x3F, 221 0x0, 0x80, 0x7F, 0x0, 0x80, 0x7F, 0x0, 2220x80, 0x7F, 0x0, 0x0, 0x3F, 0x0, 0x0, 223 0x1C, 0x0, 0x0, 0x1C, 0x0, 0x80, 0xFF, 0x0, 0x80, 0xFF, 0x0, 0x0, 0x1C, 224 0x0, 0x0, 0x1C, 0x0, 2250x0, 0x3E, 0x0, 0x0, 0x7F, 0x0, 0x80, 0xFF, 0x0, 0xC0, 226 0xFF, 0x1, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0xE0, 0xFF, 0x3, 0x0, 0x0, 0x0 227}, 228 //pawn 229{0x0, 0x0, 0x0, 0x0, 0x6E, 0x0, 0x0, 0xFF, 0x0, 0x80, 0xFF, 0x1, 230 0x80, 0xFF, 0x3, 0xC0, 0xFF, 0x7, 0xE0, 0xFF, 0xF, 0xE0, 0xF9, 0xF, 2310xF0, 232 0xF8, 0xF, 0xF0, 0xF8, 0xF, 0x60, 0xFC, 0xF, 0x0, 0xFE, 0xF, 0x0, 0xFF, 0xF, 233 0x0, 0xFF, 0x7, 0x0, 0xFF, 0x3, 0x0, 0xFF, 0x1, 2340x0, 0xFE, 0x0, 0x0, 235 0x7C, 0x0, 0x0, 0x38, 0x0, 0x0, 0x38, 0x0, 0x0, 0x7C, 0x0, 0xF0, 0xFF, 236 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 237}, //knight 238{0x0, 0x0, 0x0, 0x0, 239 0x3C, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x7E, 0x0, 0x0, 0x3C, 0x0, 0x0, 0x18, 240 0x0, 0x80, 0xFF, 0x1, 0xC0, 0xFF, 0x3, 2410xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 242 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xE0, 0xFF, 0x7, 243 0xE0, 0xFF, 0x7, 2440xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0x80, 0xFF, 0x1, 0x0, 245 0xFF, 0x0, 0x8, 0x7E, 0x10, 0x1C, 0x3C, 0x38, 0xFE, 0xFF, 0x7F, 0x0, 0x0, 0x0 246}, 247 //bishop 248{0x0, 0x0, 0x0, 0xF0, 0x3C, 0xF, 0xF0, 0x3C, 0xF, 0xF0, 0xFF, 0xF, 249 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 2500xC0, 251 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 252 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 2530xC0, 0xFF, 0x3, 0xC0, 254 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 255 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 256}, //rook 257{0x0, 0x0, 0x0, 0xEE, 0xBD, 258 0x77, 0xEE, 0xBD, 0x77, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 0xC6, 0x18, 0x63, 0xCC, 259 0x18, 0x33, 0xCC, 0x18, 0x33, 2600xCC, 0x18, 0x33, 0xCC, 0x18, 0x33, 0xCC, 0x18, 261 0x33, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 0x99, 0x19, 0x98, 262 0x99, 0x19, 2630x30, 0xFF, 0xC, 0xB0, 0xFF, 0xD, 0xF0, 0xFF, 0xF, 0xE0, 0xFF, 264 0x7, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 265}, 266 //queen 267{0x0, 0x0, 0x0, 0x0, 0x18, 0x0, 0xF0, 0x18, 0xF, 0xF8, 0x99, 0x1F, 268 0xFC, 0xDB, 0x3F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 2690xFE, 270 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 0xFE, 0xFF, 0x7F, 271 0xFE, 0xFF, 0x3F, 0xFC, 0xFF, 0x1F, 0xF8, 0xFF, 0x1F, 2720xF0, 0xFF, 0xF, 0xE0, 273 0xFF, 0x7, 0xE0, 0xFF, 0x7, 0xC0, 0xFF, 0x3, 0xE0, 0xFF, 0x7, 0xF0, 0xFF, 0xF, 274 0xF0, 0xFF, 0xF, 0x0, 0x0, 0x0 275} //king 276}; 277 278const uint8_t fig24_cont[6][72] 279 PROGMEM={ 280{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1C, 0x0, 281 0x0, 0x22, 0x0, 0x0, 0x41, 0x0, 0x80, 0x80, 0x0, 0x80, 0x80, 0x0, 2820x80, 283 0x80, 0x0, 0x0, 0x41, 0x0, 0x0, 0x22, 0x0, 0xC0, 0xE3, 0x1, 0x40, 0x0, 0x1, 284 0x40, 0x0, 0x1, 0xC0, 0xE3, 0x1, 0x0, 0x22, 0x0, 2850x0, 0x41, 0x0, 0x80, 286 0x80, 0x0, 0x40, 0x0, 0x1, 0x20, 0x0, 0x2, 0x10, 0x0, 0x4, 0x10, 0x0, 0x4, 287 0x10, 0x0, 0x4, 0xF0, 0xFF, 0x7 288}, //pawn 289{0x0, 0x6E, 0x0, 0x0, 0x91, 290 0x0, 0x80, 0x0, 0x1, 0x40, 0x2, 0x2, 0x40, 0x0, 0x4, 0x20, 0x0, 0x8, 0x10, 291 0x0, 0x10, 0x10, 0x6, 0x10, 2920x8, 0x5, 0x10, 0x8, 0x5, 0x10, 0x90, 0x2, 293 0x10, 0x60, 0x1, 0x10, 0x80, 0x0, 0x10, 0x80, 0x0, 0x8, 0x80, 0x0, 0x4, 0x80, 294 0x0, 0x2, 2950x0, 0x1, 0x1, 0x0, 0x82, 0x0, 0x0, 0x44, 0x0, 0x0, 0x44, 296 0x0, 0xF8, 0x83, 0x1F, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 297}, 298 //knight 299{0x0, 0x3C, 0x0, 0x0, 0x42, 0x0, 0x0, 0x81, 0x0, 0x0, 0x81, 0x0, 300 0x0, 0x42, 0x0, 0x80, 0xE7, 0x1, 0x40, 0x0, 0x2, 0x20, 0x18, 0x4, 3010x10, 302 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x90, 0xFF, 0x9, 0x90, 0xFF, 0x9, 303 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 0x10, 0x18, 0x8, 3040x10, 0x18, 0x8, 0x20, 305 0x18, 0x4, 0x40, 0x18, 0x2, 0x88, 0x0, 0x11, 0x14, 0x81, 0x28, 0xE2, 0xC3, 0x47, 306 0x1, 0x0, 0x80, 0xFF, 0xFF, 0xFF 307}, //bishop 308{0xF8, 0x7E, 0x1F, 0x8, 0xC3, 309 0x10, 0x8, 0xC3, 0x10, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xE0, 0xFF, 0x7, 0x20, 310 0x0, 0x4, 0x20, 0x0, 0x4, 3110x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 312 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 313 0x0, 0x4, 3140x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0x20, 0x0, 0x4, 0xE0, 0xFF, 315 0x7, 0x10, 0x0, 0x8, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 316}, 317 //rook 318{0xEF, 0xBD, 0xF7, 0x11, 0x42, 0x88, 0x11, 0x42, 0x88, 0x29, 0xA5, 0x94, 319 0x29, 0xA5, 0x94, 0x29, 0xA5, 0x94, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 3200x32, 321 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x32, 0xA5, 0x4C, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 322 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 0x64, 0x66, 0x26, 3230xC8, 0x0, 0x13, 0x48, 324 0x0, 0x12, 0x8, 0x0, 0x10, 0x10, 0x0, 0x8, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 325 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 326}, //queen 327{0x0, 0x18, 0x0, 0xF0, 0x24, 328 0xF, 0x8, 0xA5, 0x10, 0x4, 0x66, 0x20, 0x2, 0x24, 0x40, 0x1, 0x24, 0x80, 0x1, 329 0x18, 0x80, 0x1, 0x18, 0x80, 3300x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 331 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x80, 0x1, 0x18, 0x40, 0x2, 0x18, 0x20, 0x4, 332 0x18, 0x20, 3330xE8, 0xFF, 0x17, 0x10, 0x0, 0x8, 0x10, 0x0, 0x8, 0x20, 0x0, 334 0x4, 0xF0, 0xFF, 0xF, 0x8, 0x0, 0x10, 0x8, 0x0, 0x10, 0xF8, 0xFF, 0x1F 335} 336 //king 337}; 338 339 340const uint8_t issound[2][32] PROGMEM={ 341{0x0, 0x0, 0x0, 342 0x0, 0x0, 0x32, 0x0, 0x54, 0x0, 0x98, 0x1F, 0x10, 0x10, 0x30, 0x10, 0x50, 3430x10, 344 0x90, 0x11, 0x10, 0x1F, 0x10, 0x4, 0x90, 0x8, 0x50, 0x10, 0x30, 0x0, 0x0, 0x0, 345 0x0}, 346{0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x50, 0x0, 0x90, 0x1F, 0x10, 347 0x10, 0x10, 0x10, 0x10, 3480x10, 0x10, 0x10, 0x10, 0x1F, 0x10, 0x0, 0x90, 0x0, 349 0x50, 0x0, 0x30, 0x0, 0x0, 0x0, 0x0} 350}; 351const uint8_t iswb[2][32] PROGMEM={ 352{0x0, 353 0x0, 0x0, 0x0, 0x3, 0xC0, 0x4, 0x20, 0x8, 0x10, 0x8, 0x10, 0x4, 0x20, 354 0x4, 0x20, 3550x8, 0x10, 0x4, 0x20, 0x4, 0x20, 0x8, 0x10, 0x10, 0x8, 0x10, 356 0x8, 0x1F, 0xF8, 0x0, 0x0}, 357{0x0, 0x0, 0x0, 0x0, 0x3, 0xC0, 0x7, 0xE0, 358 0xF, 0xF0, 0xF, 0xF0, 0x7, 0xE0, 0x7, 0xE0, 3590xF, 0xF0, 0x7, 0xE0, 0x7, 360 0xE0, 0xF, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x0, 0x0} 361}; 362const 363 uint8_t iscm[2][32] PROGMEM={ 364{0x0, 0x0, 0x7, 0xE0, 0xC, 0x30, 0xC, 0x30, 365 0xC, 0x30, 0xC, 0x30, 0x0, 0x30, 0x0, 0x60, 3660x0, 0xC0, 0x1, 0x80, 0x1, 367 0x80, 0x1, 0x80, 0x0, 0x0, 0x1, 0x80, 0x1, 0x80, 0x0, 0x0}, 368{0x0, 0x0, 369 0x3F, 0x0, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x61, 0x80, 0x1, 0x80, 0x3, 0x0, 3700x6, 371 0x24, 0xC, 0x24, 0xC, 0xFF, 0xC, 0x24, 0x0, 0xFF, 0xC, 0x24, 0xC, 0x24, 372 0x0, 0x0} 373}; 374const uint8_t islimit[8][32] PROGMEM={ 375{0x0, 0x0, 0x3, 0xC0, 376 0x6, 0x60, 0x0, 0x60, 0x1, 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 3770x3, 378 0xC0, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 379 0x0, 0x0}, 380{0x0, 0x0, 0x4, 0x70, 0xC, 0x88, 0x4, 0x88, 0x4, 0x88, 0x4, 381 0x88, 0x4, 0x88, 0x4, 0x88, 3820xE, 0x70, 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 383 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 384{0x0, 0x0, 0xC, 0x70, 0x12, 385 0x88, 0x2, 0x88, 0xC, 0x88, 0x2, 0x88, 0x2, 0x88, 0x12, 0x88, 3860xC, 0x70, 387 0x0, 0x0, 0x1D, 0xDC, 0x11, 0x10, 0x1D, 0xD0, 0x5, 0x10, 0x1D, 0xDC, 0x0, 0x0}, 388{0x0, 389 0x0, 0x1, 0x80, 0x3, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 390 0x80, 3910x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 392 0x22, 0xA4, 0x0, 0x0}, 393{0x0, 0x0, 0x3, 0xC0, 0x6, 0x60, 0x0, 0x60, 0x1, 394 0xC0, 0x0, 0x60, 0x6, 0x60, 0x6, 0x60, 3950x3, 0xC0, 0x0, 0x0, 0x22, 0xA4, 396 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 397{0x0, 0x0, 0xC, 398 0x78, 0x1C, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 0xC, 0xCC, 3990x1E, 400 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 401 0x0}, 402{0x0, 0x0, 0x1C, 0x78, 0x36, 0xCC, 0x6, 0xCC, 0x1C, 0xCC, 0x6, 0xCC, 403 0x36, 0xCC, 0x36, 0xCC, 4040x1C, 0x78, 0x0, 0x0, 0x22, 0xA4, 0x36, 0xA4, 0x2A, 405 0xB4, 0x22, 0xAC, 0x22, 0xA4, 0x0, 0x0}, 406{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 407 0x0, 0x0, 0x3C, 0x3C, 0x7E, 0x7E, 0x63, 0xC6, 0x61, 0x86, 4080x63, 0xC6, 0x66, 409 0x66, 0x3C, 0x3C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} 410}; 411const 412 uint8_t isdesc[2][32] PROGMEM={ 413{0x0, 0x0, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E, 414 0x1C, 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 4150xE3, 0x8E, 0xE3, 0x8E, 0x1C, 416 0x70, 0x1C, 0x70, 0x1C, 0x70, 0xE3, 0x8E, 0xE3, 0x8E, 0xE3, 0x8E}, 417{0x0, 0x0, 418 0x0, 0x4, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x30, 4190x30, 420 0x70, 0x78, 0xE0, 0x3C, 0xC0, 0x1F, 0xC0, 0xF, 0x80, 0x7, 0x0, 0x2, 0x0, 0x0, 421 0x0} 422 423}; 424const uint8_t isback[32] PROGMEM={ 4250x0, 0x0, 0x0, 0x0, 426 0x20, 0xF0, 0x31, 0xF8, 0x3B, 0xFC, 0x3F, 0x8E, 0x3F, 0xE, 0x3F, 0xE, 4270x3F, 428 0x8E, 0x3F, 0xCE, 0x0, 0xE, 0x0, 0xE, 0x0, 0x1C, 0x0, 0x38, 0x0, 0x70, 0x0, 429 0x0 430}; 431const uint8_t issave[32] PROGMEM={ 4320x1, 0x80, 0x3, 0xC0, 0x7, 433 0xE0, 0x7, 0xE0, 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 4340x23, 0xC4, 435 0x23, 0xC4, 0x23, 0xC4, 0x23, 0xC4, 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 436}; 437const 438 uint8_t isload[32] PROGMEM={ 4390x0, 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 440 0x3, 0xC0, 0x3F, 0xFC, 0x23, 0xC4, 0x23, 0xC4, 4410x23, 0xC4, 0x27, 0xE4, 0x27, 442 0xE4, 0x23, 0xC4, 0x21, 0x84, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0 443}; 444const uint8_t 445 isempty[32] PROGMEM={ 4460xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 447 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 4480x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 449 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF 450}; 451const uint8_t isinit[32] 452 PROGMEM={ 4530xFF, 0xFF, 0x80, 0x1, 0x80, 0x1, 0xFF, 0xFF, 0x80, 0x1, 0x80, 454 0x1, 0x80, 0x1, 0x80, 0x1, 4550x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0x80, 0x1, 0xFF, 456 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF 457}; 458const uint8_t isplay[32] PROGMEM={ 4590x0, 460 0x0, 0x3, 0xC0, 0x3, 0xC0, 0x3, 0xC0, 0x1, 0x80, 0x23, 0xF8, 0x17, 0xC8, 461 0xB, 0xC8, 4620x3, 0xC0, 0x3, 0xC0, 0x2, 0x40, 0x4, 0x40, 0x4, 0x38, 0x4, 463 0x8, 0xC, 0x0, 0x0, 0x0 464}; 465const uint8_t isrotate[2][32] PROGMEM={ 466{0x0, 467 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 0x4, 0x20, 468 0x4, 4690x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 470 0x0, 0x0, 0x0, 0x0}, 471{0x0, 0x0, 0x0, 0x0, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 472 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 0x3F, 0xFC, 4730x20, 0x4, 0x20, 0x4, 0x20, 0x4, 474 0x20, 0x4, 0x20, 0x4, 0x3F, 0xFC, 0x0, 0x0, 0x0, 0x0} 475}; 476 477const 478 int pos[7][8][8] PROGMEM={ 479 {{ 0, 0, 0, 0, 0, 0, 0, 0}, // 480 {100,100,100,100,100,100,100,100}, 481 //{ 50, 50, 50, 50, 50, 50, 50, 50}, 482 { 20, 30, 40, 50, 50, 40, 30, 20}, //{ 483 10, 10, 20, 30, 30, 20, 10, 10}, 484 { 5, 5, 10, 25, 25, 10, 5, 5}, 485 { 486 0, 0, 0, 20, 20, 0, 0, 0}, 487 { 5, -5,-10, 0, 0,-10, -5, 5}, 488 { 489 5, 10, 10,-20,-20, 10, 10, 5}, //{ 5, 10, 10,-20,-20, 10, 10, 5}, 490 { 0, 491 0, 0, 0, 0, 0, 0, 0}}, 492 493 {{-50,-40,-30,-30,-30,-30,-40,-50}, // 494 495 {-40,-20, 0, 0, 0, 0,-20,-40}, 496 {-30, 0, 10, 15, 15, 10, 0,-30}, 497 498 {-30, 5, 15, 20, 20, 15, 5,-30}, 499 {-30, 0, 15, 20, 20, 15, 0,-30}, 500 501 {-30, 5, 10, 15, 15, 10, 5,-30}, 502 {-40,-20, 0, 5, 5, 0,-20,-40}, 503 504 {-50,-40,-30,-30,-30,-30,-40,-50}}, 505 506 {{-20,-10,-10,-10,-10,-10,-10,-20}, 507 // 508 {-10, 0, 0, 0, 0, 0, 0,-10}, 509 {-10, 0, 5, 10, 10, 5, 0,-10}, 510 511 {-10, 5, 5, 10, 10, 5, 5,-10}, 512 {-10, 0, 10, 10, 10, 10, 0,-10}, 513 514 {-10, 10, 10, 10, 10, 10, 10,-10}, 515 {-10, 5, 0, 0, 0, 0, 5,-10}, 516 517 {-20,-10,-10,-10,-10,-10,-10,-20}}, 518 519 {{ 0, 0, 0, 0, 0, 0, 0, 0}, 520 // 521 { 5, 10, 10, 10, 10, 10, 10, 5}, 522 { -5, 0, 0, 0, 0, 0, 0, -5}, 523 524 { -5, 0, 0, 0, 0, 0, 0, -5}, 525 { -5, 0, 0, 0, 0, 0, 0, -5}, 526 527 { -5, 0, 0, 0, 0, 0, 0, -5}, 528 { -5, 0, 0, 0, 0, 0, 0, -5}, 529 530 { 0, 0, 0, 5, 5, 0, 0, 0}}, 531 532 {{-20,-10,-10, -5, -5,-10,-10,-20}, 533 // 534 {-10, 0, 0, 0, 0, 0, 0,-10}, 535 {-10, 0, 5, 5, 5, 5, 0,-10}, 536 537 { -5, 0, 5, 5, 5, 5, 0, -5}, 538 { 0, 0, 5, 5, 5, 5, 0, -5}, 539 540 {-10, 5, 5, 5, 5, 5, 0,-10}, 541 {-10, 0, 5, 0, 0, 0, 0,-10}, 542 543 {-20,-10,-10, -5, -5,-10,-10,-20}}, 544 545 {{-30,-40,-40,-50,-50,-40,-40,-30}, 546 // 547 {-30,-40,-40,-50,-50,-40,-40,-30}, 548 {-30,-40,-40,-50,-50,-40,-40,-30}, 549 550 {-30,-40,-40,-50,-50,-40,-40,-30}, 551 {-20,-30,-30,-40,-40,-30,-30,-20}, 552 553 {-10,-20,-20,-20,-20,-20,-20,-10}, 554 { 10, 10,-10,-10,-10,-10, 10, 10}, //{ 555 20, 20, 0, 0, 0, 0, 20, 20}, 556 { 10, 40, 30, 0, 0, 0, 50, 10}}, //{ 20, 557 30, 10, 0, 0, 10, 30, 20}}, 558 559 {{-50,-40,-30,-20,-20,-30,-40,-50}, // 560 561 {-30,-20,-10, 0, 0,-10,-20,-30}, 562 {-30,-10, 20, 30, 30, 20,-10,-30}, 563 564 {-30,-10, 30, 40, 40, 30,-10,-30}, 565 {-30,-10, 30, 40, 40, 30,-10,-30}, 566 567 {-30,-10, 20, 30, 30, 20,-10,-30}, 568 {-30,-30, 0, 0, 0, 0,-30,-30}, 569 570 {-50,-30,-30,-30,-30,-30,-30,-50}} 571}; 572 573volatile static int32_t toggles; 574 // number of ups/downs in a tone burst 575 576void tone(int 577 toneFreq,int32_t duration){ // duration in ms 578 const 579 uint32_t rcVal = VARIANT_MCK/256/toneFreq; // target value for counter, before 580 it resets (= 82 for 4kHz) 581 toggles = 2*toneFreq*duration/1000; // 582 calculate no of waveform edges (rises/falls) for the tone burst 583 setupTC(TC1,0,TC3_IRQn,toneFreq); 584 // Start Timer/Counter 1, channel 0, interrupt, frequency 585 } 586 587 588void setupTC(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t freq){ 589 590 pmc_set_writeprotect(false); // disable write protection 591 of timer registers 592 pmc_enable_periph_clk((uint32_t) irq); // enable 593 clock / interrupt 594 TC_Configure(tc, channel, 595 TC_CMR_TCCLKS_TIMER_CLOCK4 596 | // TIMER_CLOCK4: MCK/128=656,250Hz. 16 bits so 656,250/65,536=~10Hz/bit 597 598 TC_CMR_WAVE | // Waveform mode 599 TC_CMR_WAVSEL_UP_RC 600 ); // Counter running up and reset when = Register C value (rcVal) 601 602 const uint32_t rcVal = VARIANT_MCK/256/freq; // target value for counter, 603 before it resets 604 TC_SetRC(tc, channel, rcVal); 605 TC_Start(tc, channel); 606 607 (*tc).TC_CHANNEL[channel].TC_IER = TC_IER_CPCS; // IER: CPCS bit enables RC 608 compare interrupt when set 609 (*tc).TC_CHANNEL[channel].TC_IDR = ~TC_IER_CPCS; 610 // IDR: clear CPCS bit = don't disable RC compare interrupt 611 NVIC_EnableIRQ(irq); 612 // Enable TC3_IRQn in the Nested Vector Interrupt 613 Controller) 614 } 615 616void TC3_Handler(void){ // 617 timer ISR TC1 ch 0 618 TC_GetStatus(TC1,0); 619 if (toggles != 0){ 620 digitalWrite(PinBuzz,!digitalRead(PinBuzz)); 621 // invert the pin state (i.e toggle) 622 if (toggles > 0) toggles--; 623 624 } 625 } 626 627 void noTone(){ 628 TC_Stop(TC1,0); // 629 stop timer 630 digitalWrite(PinBuzz,LOW); // no signal 631 on pin 632 } 633 634//****** 635class Button { 636public: 637int bx,by,bw,bh,bshift; 638String 639 bs; 640const uint8_t* bp; 641 Button(int x,int y,int w,int h,String s,const uint8_t* 642 p, int shift); 643 void Show(int shift); 644 void Show(String s); 645 void Hide(); 646 647 boolean IsPressed(int xc, int yc); 648private: 649boolean active; 650 void ishow(uint16_t 651 textcolor,uint16_t backcolor); 652}; 653Button::Button(int x,int y,int w,int h,String 654 s,const uint8_t* p=NULL, int shift=0) { 655 bx=x; by=y; bw=w; bh=h; bs=s; bp=p; 656 bshift=shift; 657}; 658void Button::Show(int shift=0) { 659 bshift=shift; 660 active=true; 661 662 ishow(WHITE,DARK); 663}; 664void Button::Show(String s) { 665 bs=s; 666 active=true; 667 668 ishow(WHITE,DARK); 669}; 670void Button::Hide() { 671 active=false; 672 ishow(GRAY,DARK); 673 674}; 675boolean Button::IsPressed(int xc, int yc) { 676 if (active&&abs(bx+bw/2-xc)<bw/2&&abs(by+bh/2-yc)<bh/2) 677 { 678 ishow(WHITE,GRAY); 679 touchx=0; touchy=0; 680 return true; 681 682 } 683 return false; 684}; 685void Button::ishow(uint16_t textcolor,uint16_t 686 backcolor) { 687 tft.fillRoundRect(bx+2,by+2,bw,bh,3,GRAY2); 688 689 tft.fillRoundRect(bx,by,bw,bh,3,backcolor); 690 tft.setTextColor(textcolor); 691 692 tft.setTextSize(1); 693 tft.setCursor(bx+(bw-bs.length()*6)/2,by+(bh-8)/2); 694 695 if (bp!=NULL) { 696 tft.drawBitmap(1+bx+(bw-16)/2, by+(bh-16)/2, 697 bp+bshift*32, 16, 16,textcolor); 698 } 699 else tft.print(bs); 700} 701Button 702 BAction(320, 285, 70, 30, "START"); 703Button BBW(402, 285, 30, 30, "",&iswb[0][0]); 704Button 705 BBack(444, 285, 30, 30, "",&isback[0]); 706Button BInit(444, 285, 30, 30, "",&isinit[0]); 707Button 708 BLimit(320, 245, 26, 30, "",&islimit[0][0]); 709Button BEmpty(320, 245, 26, 30, 710 "",&isempty[0]); 711Button BRotate(352, 245, 26, 30, "",&isrotate[0][0]); 712Button 713 BPlay(352, 245, 26, 30, "",&isplay[0]); 714Button BCM(384, 245, 26, 30, "",&iscm[0][0]); 715Button 716 BSave(384, 245, 26, 30, "",&issave[0]); 717Button BDesc(416, 245, 26, 30, "",&isdesc[0][0]); 718Button 719 BSound(448, 245, 26, 30, "",&issound[0][0]); 720Button BLoad(448, 245, 26, 30, 721 "",&isload[0]); 722 723union byte2_t { 724 uint8_t b[2]; 725 struct 726 { 727 short s; 728 }; 729 }; 730 731byte2_t byte2; 732//********************************** 733 734void drawBitmap(int16_t x, int16_t y, 735 const uint8_t *bitmap, 736 int16_t w, int16_t h, 737 uint16_t color) { 738 int16_t i, j, byteWidth 739 = (w + 7) / 8; 740 //if (color!=pixelfg) tft.fgColor(color); 741 for(j=0; j<h; 742 j++) { 743 for(i=0; i<w; i++ ) { 744 if(pgm_read_byte(bitmap + j 745 * byteWidth + i / 8) & (1 << (i % 8))) { 746 tft.drawPixel(x+i, 747 y+j, color); 748 } 749 } 750 } 751} 752//**************************** 753void 754 deletebuttons() { 755 tft.fillRect(320,245,160,75,BLACK); 756} 757 758//**************************** 759void 760 clearstatus() { 761 tft.fillRect(320,225,160,20,BLACK); 762 tft.drawFastHLine(10,312,300,BLACK); 763 764 tft.drawFastHLine(10,315,300,BLACK); 765} 766//**************************** 767void 768 setup() { 769 Serial.begin(57600); 770 Serial.println(F("Start")); 771 772 pinMode(13, OUTPUT); 773 tft.init(); 774 tft.setRotation(1); 775 myTouch.InitTouch(); 776 777 definecolors(); 778 beep(100); 779 for (int i=0;i<MAXSTEPS;i++) { 780 781 steps[i].x1=0; steps[i].y1=0; 782 steps[i].x2=0; steps[i].y2=0; 783 steps[i].fig1=0; 784 steps[i].fig2=0; 785 steps[i].check=0; 786 steps[i].type=0; 787 steps[i].weight=0; 788 789 } 790 initboard(); 791} 792//**************************** 793void initboard() 794 { 795 for (int i=0;i<8;i++) 796 for (int j=0;j<8;j++) poledisp[j][i]=-100; 797 // 798 tft.fillScreen(BLACK); 799 tft.setTextColor(GRAY); 800 801 tft.setTextSize(1); 802 for (int j=1;j<9;j++) { 803 tft.setCursor(2,j*37-20); 804 805 if (rotate) tft.print(j); else tft.print(9-j); 806 } 807 for (byte i=1;i<9;i++) 808 { 809 tft.setCursor(i*37-8,300); 810 if (rotate) tft.print(char(96+9-i)); 811 else tft.print(char(96+i)); 812 } 813 initscreen(); 814} 815//**************************** 816void 817 initscreen() { 818 show_board(); 819 show_steps(); 820 BAction.Show("START"); 821 822 BBW.Show(cur_step%2); 823 if (cur_step>1) BBack.Show(); else BBack.Hide(); 824 825 BSave.Hide(); 826 BLoad.Hide(); 827 BInit.Hide(); 828 BBack.Show(); 829 BLimit.Show(); 830 831 BPlay.Hide(); 832 BRotate.Show(rotate); 833 834 BEmpty.Hide(); 835 BLimit.Show(limit); 836 BCM.Show(checkmatesearch); 837 BDesc.Show(choice); 838 839 BSound.Show(sound); 840} 841//**************************** 842void loop() 843 { 844 gui(); 845 if (solving) { 846 int len=70; 847 if 848 (!sound) len=2; 849 for (int i=1;i<4;i++) { 850 tone(400+300*i,len); 851 852 delay(70); } 853 lastscore=solve_step(); 854 float 855 tim=float(millis()-starttime)/1000; 856 for (int i=3;i>=1;i--) { 857 tone(400+300*i,len); 858 859 delay(70); } 860 if (!checkmatesearch&&lastscore>-9000&&lastscore!=8999) 861 { 862 movestep(cur_step); 863 cur_step++; steps[cur_step].fig1=0; 864 865 } 866 initscreen(); 867 animate_step(cur_step-1,false); 868 869 clearstatus(); 870 if (lastscore>9000) { 871 tft.setTextSize(1); 872 873 tft.setTextColor(GREEN); 874 tft.setCursor(322,230); 875 876 if (lastscore<9999) { 877 tft.print("# in "); 878 tft.print((9999-lastscore)/2+1); 879 tft.print(F(" st")); 880 } else tft.print("Checkmate"); 881 } 882 else if (lastscore<-9000) { 883 tft.setTextSize(1); 884 tft.setTextColor(RED); 885 886 tft.setCursor(322,230); 887 tft.print("GIVE UP!"); 888 889 show_steps(); 890 } else if (lastscore==8999) { //Draw 891 tft.setTextSize(1); 892 893 tft.setTextColor(YELLOW); 894 tft.setCursor(322,230); 895 896 tft.print("Draw"); 897 } 898 tft.setTextSize(1); 899 900 tft.setTextColor(GRAY); 901 tft.setCursor(322,230); 902 tft.print(tim,0); 903 904 tft.print("s "); 905 if (abs(lastscore)<5000) { 906 tft.print("="); 907 908 tft.print(lastscore); 909 } 910 tft.setCursor(390,230); 911 912 tft.print(count); 913 tft.print(" pos"); 914 Serial.print(F("Positions 915 estimated=")); Serial.println(count); 916 Serial.print(F("Time=")); Serial.print(tim,1); 917 Serial.println("s"); 918 } 919 delay(10); 920 921 922} 923 924//********************************** 925 926void load_choice() { 927 for (int i=0;i<7;i++) 928 if (cur_step%2==1) polechoice[i]=i; 929 else polechoice[i]=-i; 930} 931//********************************** 932void show_choice() 933 { 934uint16_t color,color_cont; 935 for (int i=0;i<7;i++) { 936 color=BLACKF; 937 938 if (i%2==0) color=WHITEF; 939 tft.fillRect(416,i*25+35,25,25,color); 940 941 color=DARK; 942 if (i==cur_choice) color=WHITE; 943 tft.drawRect(416,i*25+35,25,25,color); 944 945 color=BLACK; color_cont=GRAY; 946 if (polechoice[i]>0) { color=WHITE; color_cont=BLACK; 947 } 948 if (polechoice[i]!=0) { 949 tft.drawBitmap(420, i*25+39,&fig[abs(polechoice[i])-1][0], 950 16, 16,color); 951 tft.drawBitmap(420, i*25+39,&fig_cont[abs(polechoice[i])-1][0], 952 16, 16,color_cont); 953 } 954 } 955} 956//********************************** 957 958void animate_step(int nstep, boolean hide) { 959 if (!hide&&nstep-1>0&&steps[nstep-1].fig1!=0) 960 animate_step(nstep-1,true); 961 if (nstep-1<1||steps[nstep].fig1==0) return; 962 963 int j=steps[nstep].x1; 964 int dj=steps[nstep].x2-steps[nstep].x1; 965 int i=steps[nstep].y1; 966 967 int di=steps[nstep].y2-steps[nstep].y1; 968 di=di/abs(di); dj=dj/abs(dj); 969 970 if (hide) show_fig(i,j); 971 while (j!=steps[nstep].x2||i!=steps[nstep].y2) { 972 973 show_fig(i,j); 974 if (!hide) 975 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 976 977 else tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 978 int 979 mj=0; 980 if (j!=steps[nstep].x2) { j+=dj; mj=1; } 981 if (abs(steps[nstep].fig1)==fn&&mj==1) 982 continue; 983 if (i!=steps[nstep].y2) i+=di; 984 } 985 show_fig(i,j); 986 if 987 (!hide) 988 if (!rotate) tft.drawRect(i*37+13,j*37+1,36,36,WHITE); 989 else 990 tft.drawRect((7-i)*37+13,(7-j)*37+1,36,36,WHITE); 991} 992//********************************** 993 994boolean load_usb() { 995signed char s='x',i=0,j=0; boolean load=false; 996 997 Serial.println(F("Wait for FEN position")); 998 for (int i=0;i<8;i++) 999 for 1000 (int j=0;j<8;j++) { 1001 pole0[j][i]=pole[j][i]; 1002 pole[j][i]=(signed 1003 char)pgm_read_byte(&polezero[j][i]); 1004 } 1005 while 1006 (s!=' ') { 1007 s=Serial.read(); 1008 if (i>7) { i=0; j++; } 1009 if 1010 (!getpole(j,i)) break; 1011 switch (s) { 1012 case '/': i=0; break; 1013 1014 case 'p': pole[j][i]=-fp; i++; break; 1015 case 'P': pole[j][i]=fp; i++; break; 1016 1017 case 'n': pole[j][i]=-fn; i++; break; 1018 case 'N': pole[j][i]=fn; i++; break; 1019 1020 case 'b': pole[j][i]=-fb; i++; break; 1021 case 'B': pole[j][i]=fb; i++; break; 1022 1023 case 'r': pole[j][i]=-fr; i++; break; 1024 case 'R': pole[j][i]=fr; i++; break; 1025 1026 case 'q': pole[j][i]=-fq; i++; break; 1027 case 'Q': pole[j][i]=fq; i++; break; 1028 1029 case 'k': pole[j][i]=-fk; i++; break; 1030 case 'K': pole[j][i]=fk; i++; break; 1031 1032 case '1': i++; break; 1033 case '2': i+=2; break; 1034 case '3': i+=3; break; 1035 1036 case '4': i+=4; break; 1037 case '5': i+=5; break; 1038 case '6': i+=6; 1039 break; 1040 case '7': i+=7; break; 1041 case '8': i=0; j++; break; 1042 case 1043 ' ': break; 1044 } 1045 delay(20); 1046 if (i+j>0&&Serial.available()==0) 1047 break; 1048 } 1049 s=0; 1050 if (Serial.available()>0) s=Serial.read(); 1051 while 1052 (Serial.available()>0) Serial.read(); 1053 if (s=='w'||s==0) { cur_step=1; load=true; 1054 } 1055 else if (s=='b') { cur_step=2; load=true; } 1056 else load=false; 1057 1058 if (load) { 1059 steps[1].fig1=0; steps[2].fig1=0; 1060 Serial.println(F("Position 1061 loaded")); 1062 } else { 1063 for (int i=0;i<8;i++) 1064 for (int j=0;j<8;j++) 1065 1066 pole[j][i]=pole0[j][i]; 1067 } 1068 return load; 1069} 1070//********************************** 1071 1072void play() { 1073 BAction.Show("STOP"); 1074 BSave.Hide(); 1075 BBW.Hide(); 1076 1077 BLoad.Hide(); 1078 BInit.Hide(); 1079 BPlay.Hide(); 1080 BEmpty.Hide(); 1081 BDesc.Hide(); 1082 1083 clearstatus(); 1084 for (int i=cur_step-1;i>0;i--) { 1085 if (steps[i].fig1!=0) 1086 backstep(i); 1087 } 1088 show_board(); 1089 show_steps(); 1090 boolean st=false; 1091 1092 unsigned long tim=millis(); 1093 int i=1; 1094 while (i<cur_step) { 1095 if (steps[i].fig1!=0) 1096 movestep(i); 1097 myTouch.calibrateRead(); 1098 int x=map(myTouch.TP_X,3850,250,0,479); 1099 1100 int y=map(myTouch.TP_Y,280,3850,0,319); 1101 if (myTouch.TP_X!=0&&myTouch.TP_Y!=4095) 1102 1103 if (BAction.IsPressed(x,y)) { st=true; BAction.Show(); } 1104 if (millis()-tim>2000||st) 1105 { 1106 tim=millis(); 1107 i++; 1108 show_board(); 1109 beep(20); 1110 1111 } 1112 } 1113 choice=0; 1114 beep(200); 1115 kingpositions(); 1116 1117 clearstatus(); 1118 initscreen(); 1119} 1120//********************************** 1121 1122void gui() { 1123 myTouch.calibrateRead(); 1124 int x=map(myTouch.TP_X,3850,250,0,479); 1125 1126 int y=map(myTouch.TP_Y,280,3850,0,319); 1127 if (((abs(x-touchx)>5||abs(y-touchy)>5)||drag)&&myTouch.TP_X!=0&&myTouch.TP_Y!=4095) 1128 { 1129 // tft.drawPixel(x,y,CYAN); 1130 // Serial.println(x); 1131 // 1132 Serial.println(y); 1133 touchx=x; 1134 touchy=y; 1135 if 1136 (solving) { 1137 if (BAction.IsPressed(x,y)) { // 1138 beep(100); 1139 1140 solving=false; 1141 BAction.Show("stopping"); 1142 1143 BAction.Hide(); 1144 return; 1145 } 1146 } else 1147 { //not solve 1148 if (BAction.IsPressed(x,y)) { // 1149 if (!choice) 1150 { 1151 delay(100); 1152 solving=true; 1153 BAction.Show(); 1154 1155 animate_step(cur_step-1,true); 1156 return; 1157 } 1158 else { // USB 1159 delay(100); 1160 BAction.Hide(); 1161 BSave.Hide(); 1162 1163 BBW.Hide(); 1164 BLoad.Hide(); 1165 BInit.Hide(); 1166 BPlay.Hide(); 1167 1168 BEmpty.Hide(); 1169 BDesc.Hide(); 1170 show_steps(); 1171 1172 choice=0; 1173 if (!load_usb()) return; 1174 beep(200); 1175 1176 kingpositions(); 1177 clearstatus(); 1178 1179 initscreen(); 1180 } 1181 } else 1182 if (BBW.IsPressed(x,y)) { //- 1183 beep(100); 1184 if (cur_step==1) 1185 cur_step=2; 1186 else if (cur_step==2) cur_step=1; 1187 else if (cur_step%2==0) 1188 cur_step--; else cur_step++; 1189 BBW.Show(cur_step%2); 1190 1191 if (choice) { load_choice(); show_choice(); } 1192 return; 1193 } 1194 else if (cur_step>1&&BBack.IsPressed(x,y)) { // 1195 beep(100); 1196 1197 cur_step--; 1198 animate_step(cur_step,true); 1199 lastscore=0; 1200 1201 backstep(cur_step); 1202 steps[cur_step].fig1=0; 1203 1204 show_board(); 1205 show_steps(); 1206 if (cur_step>1) 1207 BBack.Show(); else BBack.Hide(); 1208 BBW.Show(cur_step%2); 1209 1210 clearstatus(); 1211 return; 1212 } else if (BLimit.IsPressed(x,y)) 1213 { // 1214 beep(100); 1215 limit++; if (limit>7) limit=0; 1216 1217 BLimit.Show(limit); 1218 return; 1219 } else if (BCM.IsPressed(x,y)) 1220 { // 1221 beep(100); 1222 checkmatesearch=!checkmatesearch; 1223 1224 BCM.Show(checkmatesearch); 1225 if (!checkmatesearch) steps[cur_step].fig1=0; 1226 1227 lastscore=0; 1228 show_steps(); 1229 return; 1230 } else if 1231 (BRotate.IsPressed(x,y)) { // 1232 beep(100); 1233 rotate=!rotate; 1234 1235 BRotate.Show(rotate); 1236 initboard(); 1237 1238 return; 1239 } else if (BSound.IsPressed(x,y)) { // 1240 1241 if (sound==1) { beep(100); sound=0; } else { sound=1; beep(200); } 1242 1243 BSound.Show(sound); 1244 return; 1245 } else if (BDesc.IsPressed(x,y)) 1246 { // 1247 beep(100); 1248 if (!choice) { 1249 choice=1; 1250 cur_choice=1; 1251 1252 load_choice(); 1253 show_choice(); 1254 BAction.Show("USB(FEN)"); 1255 1256 BBack.Hide(); 1257 BInit.Show(); 1258 BSave.Show(); 1259 1260 BLimit.Hide(); 1261 BEmpty.Show(); 1262 BRotate.Hide(); 1263 1264 BPlay.Show(); 1265 BCM.Hide(); 1266 BSound.Hide(); 1267 1268 BSave.Show(); 1269 BLoad.Show(); 1270 } else { 1271 1272 choice=0; 1273 lastscore=0; 1274 clearstatus(); 1275 kingpositions(); 1276 1277 initscreen(); 1278 } 1279 BDesc.Show(choice); 1280 1281 return; 1282 } else if (BInit.IsPressed(x,y)) 1283 { // 1284 beep(200); 1285 cur_step=1; 1286 steps[1].fig1=0; 1287 1288 for (int i=0;i<8;i++) 1289 for (int j=0;j<8;j++) pole[j][i]=(signed 1290 char)pgm_read_byte(&polestart[j][i]); 1291 choice=0; 1292 1293 kingpositions(); 1294 clearstatus(); 1295 initscreen(); 1296 1297 } else if (BEmpty.IsPressed(x,y)) { // 1298 1299 beep(200); 1300 cur_step=1; 1301 clearstatus(); 1302 1303 steps[1].fig1=0; steps[2].fig1=0; 1304 for (int i=0;i<8;i++) 1305 1306 for (int j=0;j<8;j++) pole[j][i]=(signed char)pgm_read_byte(&polezero[j][i]); 1307 1308 BEmpty.Show(); 1309 show_board(); 1310 1311 } else if (BSave.IsPressed(x,y)) { 1312 beep(100); 1313 for 1314 (int i=0;i<8;i++) 1315 for (int j=0;j<8;j++) dueFlashStorage.write(i*8+j,pole[j][i]); 1316 1317 byte2.s=cur_step; 1318 dueFlashStorage.write(65,byte2.b[0]); 1319 1320 dueFlashStorage.write(64,byte2.b[1]); 1321 byte2.s=limit; 1322 1323 dueFlashStorage.write(67,byte2.b[0]); 1324 dueFlashStorage.write(66,byte2.b[1]); 1325 1326 step_type st; 1327 for (int i=1;i<cur_step;i++) 1328 { 1329 st=steps[i]; 1330 dueFlashStorage.write(68+i*sizeof(step_type), 1331 (byte *)&st, sizeof(step_type)); 1332 } 1333 BSave.Show(); 1334 1335 } else if (BLoad.IsPressed(x,y)) { 1336 beep(200); 1337 for 1338 (int i=0;i<8;i++) 1339 for (int j=0;j<8;j++) pole[j][i]=dueFlashStorage.read(i*8+j); 1340 1341 byte2.b[0]=dueFlashStorage.read(65); 1342 byte2.b[1]=dueFlashStorage.read(64); 1343 1344 cur_step=byte2.s; 1345 if (cur_step<0||cur_step>400) 1346 cur_step=1; 1347 byte2.b[0]=dueFlashStorage.read(67); 1348 1349 byte2.b[1]=dueFlashStorage.read(66); 1350 limit=byte2.s; 1351 1352 if (limit<0||limit>7) limit=0; 1353 step_type st; 1354 for 1355 (int i=1;i<cur_step;i++) { 1356 memcpy(&st, dueFlashStorage.readAddress(68+i*sizeof(step_type)), 1357 sizeof(step_type)); 1358 steps[i]=st; 1359 } 1360 1361 steps[0].fig1=0; 1362 steps[cur_step].fig1=0; 1363 1364 choice=0; 1365 kingpositions(); 1366 clearstatus(); 1367 1368 initscreen(); 1369 } else if (BPlay.IsPressed(x,y)) 1370 { // 1371 beep(100); 1372 BPlay.Show(); 1373 play(); 1374 1375 return; 1376 } 1377 if (!choice&&ntouch==0) { 1378 touchx0=touchx; 1379 1380 touchy0=touchy; 1381 signed char x1=touchy0/37; 1382 signed char 1383 y1=(touchx0-10)/37; 1384 if (rotate) { x1=7-x1; y1=7-y1; } 1385 if 1386 (getpole(y1,x1)&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) 1387 { 1388 signed char p=pole[x1][y1]; 1389 movedfig=p; 1390 1391 drag=true; 1392 pole[x1][y1]=0; 1393 show_fig(y1,x1); 1394 1395 pole[x1][y1]=p; 1396 } else drag=false; 1397 } 1398 1399 ntouch++; 1400 if (drag&&ntouch>1) { 1401 uint16_t 1402 buf[256]; 1403 int x1=touchy0/37; 1404 int y1=(touchx0-10)/37; 1405 1406 if (rotate) { x1=7-x1; y1=7-y1; } 1407 if (abs(pole[x1][y1])>0&&(cur_step%2==1&&pole[x1][y1]>0||cur_step%2==0&&pole[x1][y1]<0)) 1408 { 1409 int x2=touchy/37; 1410 int y2=(touchx-10)/37; 1411 1412 if (getpole(x2,y2)) { 1413 signed 1414 char p=pole[x2][y2]; 1415 pole[x2][y2]=movedfig; 1416 1417 show_fig(y2,x2); 1418 delay(30); 1419 pole[x2][y2]=p; 1420 1421 show_fig(y2,x2); 1422 if (x2==x1&&y2==y1) 1423 { 1424 pole[x2][y2]=0; 1425 show_fig(y2,x2); 1426 1427 pole[x2][y2]=p; 1428 } 1429 } 1430 // 1431 for (int i=0;i<16;i++) 1432 // for (int j=0;j<16;j++) buf[i*16+j]=tft.readPixel(touchx-8+i,touchy-8+j); 1433 1434 //buf[0]=tft.readPixel(touchx-8,touchy-8); 1435 // uint16_t color,color_cont; 1436 1437 // color=BLACK; color_cont=GRAY; 1438 // if (pole[x1][y1]>0) 1439 { color=WHITE; color_cont=BLACK; } 1440 // tft.drawBitmap(touchx-8, 1441 touchy-8,&fig[abs(pole[x1][y1])-1][0], 16, 16,color); 1442 // tft.drawBitmap(touchx-8, 1443 touchy-8,&fig_cont[abs(pole[x1][y1])-1][0], 16, 16,color_cont); 1444 // 1445 delay(30); 1446 // for (int i=0;i<16;i++) 1447 // for (int j=0;j<16;j++) 1448 tft.drawPixel(touchx-8+i,touchy-8+j,buf[i*16+j]); 1449 } 1450 1451 } else if (choice) { 1452 int x1=touchy/37; 1453 int y1=(touchx-10)/37; 1454 1455 if (rotate) { x1=7-x1; y1=7-y1; } 1456 if 1457 (touchy+touchx!=0&&choice&&getpole(y1,x1)) { 1458 pole[x1][y1]=polechoice[cur_choice]; 1459 1460 show_board(); 1461 } 1462 if (touchx>=416&&touchx<=441&&touchy>=35&&touchy<=210) 1463 { 1464 cur_choice=(touchy-35)/25; 1465 show_choice(); 1466 1467 } 1468 } 1469 lasttouch=millis(); 1470 } //not solve 1471 1472 if (!drag) { beep(10); ntouch=0; animate_step(cur_step-1,true); } 1473 } 1474 else { //touching 1475 if (!solving&&!choice&&drag!=0&&ntouch>0&&(myTouch.TP_X==0||myTouch.TP_Y==4095)&&millis()-lasttouch>300) 1476 { //drag 1477 signed char x1=touchy0/37; 1478 signed char y1=(touchx0-10)/37; 1479 1480 signed char x2=touchy/37; 1481 signed char y2=(touchx-10)/37; 1482 1483 if (rotate) { x1=7-x1; y1=7-y1; x2=7-x2; y2=7-y2; } 1484 if (getpole(y1,x1)) 1485 show_fig(y1,x1); 1486 if (getpole(y1,x1)&&getpole(y2,x2)) { 1487 start_var=cur_step+1; 1488 1489 only_action=false; 1490 load_variants(cur_step); 1491 for 1492 (int i=cur_step+1;i<cur_step+cur_var+1;i++) 1493 if (steps[i].x1==x1&&steps[i].y1==y1&&steps[i].x2==x2&&steps[i].y2==y2) 1494 { 1495 steps[cur_step]=steps[i]; 1496 movestep(cur_step); 1497 cur_step++; steps[cur_step].fig1=0; 1498 int len=100; 1499 if (!sound) 1500 len=2; 1501 for (int i=2;i>=1;i--) { 1502 tone(400+300*i,len); 1503 1504 delay(100); } 1505 initscreen(); 1506 animate_step(cur_step-1,false); 1507 1508 break; 1509 } 1510 } 1511 else { 1512 beep(10); 1513 } 1514 ntouch=0; 1515 drag=false; 1516 1517 } 1518 } 1519 if (millis()-quitime>1000) { 1520 if (isstatus) show_status(); 1521 1522 quitime=millis(); 1523 } 1524 1525 1526} 1527//********************************** 1528 1529void show_status() { 1530 int tim=(millis()-starttime)/1000; 1531 int 1532 cur=300000*tim/(limittime-starttime); 1533 if (cur>300) { cur=300; solving=false; 1534 } 1535 tft.drawFastHLine(10,312,cur,GRAY); 1536 tft.drawFastHLine(10,315,progress*2,GRAY); 1537 1538 int m=tim/60; 1539 int s=tim%60; 1540 tft.setTextWrap(1); 1541 tft.setTextColor(GRAY); 1542 1543 tft.setTextSize(1); 1544 tft.fillRect(320,225,50,20,BLACK); 1545 1546 tft.setCursor(322,230); 1547 if (m>0) { tft.print(m); tft.print(":"); 1548 } 1549 if (s>0) { 1550 if (s<10&&m>0) tft.print("0"); 1551 tft.print(s); 1552 1553 } else if (m>0) tft.print("00"); else tft.print("0"); 1554 tft.setCursor(350,230); 1555 1556 tft.print(cur_level); 1557 1558 tft.setCursor(370,230); 1559 if 1560 (cur_step%2==1) tft.setTextColor(WHITE); else tft.setTextColor(GRAY); 1561 if 1562 (lastbest.fig1!=steps[0].fig1||lastbest.x1!=steps[0].x1||lastbest.y1!=steps[0].y1|| 1563 1564 lastbest.x2!=steps[0].x2||lastbest.y2!=steps[0].y2) { 1565 beep(10); 1566 1567 lastbest=steps[0]; 1568 tft.fillRect(370,230,50,10,BLACK); 1569 1570 tft.setCursor(370,230); 1571 tft.print(str_step(0)); 1572 blinkstep=0; 1573 1574 } 1575 if (steps[0].fig1!=0&&blinkstep==0&&tim>5) { 1576 signed char poleb[8][8]; 1577 1578 for (int i=0;i<8;i++) 1579 for (int j=0;j<8;j++) { poleb[j][i]=pole[j][i]; 1580 pole[j][i]=pole0[j][i]; } 1581 movestep(0); 1582 show_board(); 1583 1584 delay(100); 1585 backstep(0); 1586 show_board(); 1587 for 1588 (int i=0;i<8;i++) 1589 for (int j=0;j<8;j++) pole[j][i]=poleb[j][i]; 1590 1591 kingpositions(); 1592 } 1593 blinkstep++; 1594 if (blinkstep>2) blinkstep=0; 1595 1596} 1597//**************************** 1598void beep(int leng) { 1599 1600 analogWrite(PinBuzz, 20); 1601 delay(2); 1602 if (!sound) analogWrite(PinBuzz, 1603 0); 1604 delay(leng); 1605 analogWrite(PinBuzz, 0); 1606} 1607//**************************** 1608void 1609 definecolors() { 1610 BLACK =0x0000; 1611 BLUE =0x07FF; //0x001F; 1612 RED =0xF800; 1613 1614 GREEN =0x07E0; 1615 CYAN =0x07FF; 1616 MAGENTA=0xF81F; 1617 YELLOW =0xFFE0; 1618 1619 WHITE =0xFFFF; 1620 GRAY =0x7BEF; 1621 DARK =getColor(32,32,32); 1622 GRAY2 =getColor(16,16,16); 1623 1624 BLACKF =getColor(94,58,0); 1625 WHITEF =getColor(180,114,0); 1626} 1627//**************************** 1628uint16_t 1629 getColor(uint8_t red, uint8_t green, uint8_t blue) 1630{ 1631 red >>= 3; 1632 green 1633 >>= 2; 1634 blue >>= 3; 1635 return (red << 11) | (green << 5) | blue; 1636} 1637//**************************** 1638void 1639 show_fig(int i,int j) { 1640uint16_t color,color_cont; 1641 color=BLACKF; 1642 1643 if ((i+j+2)%2==0) color=WHITEF; 1644 int jj=j, ii=i; 1645 if (rotate) 1646 { jj=7-j; ii=7-i; } 1647 tft.fillRect(ii*37+12,jj*37,38,38,color); 1648 tft.drawRect(ii*37+12,jj*37,38,38,WHITE); 1649 1650 color=BLACK; color_cont=GRAY; 1651 if (pole[j][i]>0) { color=WHITE; color_cont=BLACK; 1652 } 1653 if (pole[j][i]!=0) { 1654 drawBitmap(ii*37+19, jj*37+7,&fig24[abs(pole[j][i])-1][0], 1655 24, 24,color); 1656 drawBitmap(ii*37+19, jj*37+7,&fig24_cont[abs(pole[j][i])-1][0], 1657 24, 24,color_cont); 1658 //drawBitmap(ii*37+24, jj*37+12,&fig[abs(pole[j][i])-1][0], 1659 16, 16,color); 1660 //drawBitmap(ii*37+24, jj*37+12,&fig_cont[abs(pole[j][i])-1][0], 1661 16, 16,color_cont); 1662 } 1663} 1664//**************************** 1665void 1666 show_board() { 1667 for (int i=0;i<8;i++) 1668 for (int j=0;j<8;j++) { 1669 if 1670 (poledisp[j][i]!=pole[j][i]) show_fig(i,j); 1671 poledisp[j][i]=pole[j][i]; 1672 1673 } 1674} 1675//**************************** 1676String str_step(int i) { 1677 String 1678 s=""; 1679 if (steps[i].fig1==0) return s; 1680 if (steps[i].type==2) s="0-0"; 1681 1682 else if (steps[i].type==3) s="0-0-0"; 1683 else { 1684 if (abs(steps[i].fig1)>1) 1685 s=fig_symb[abs(steps[i].fig1)]; 1686 if (abs(steps[i].fig1<5)) { 1687 s=s+char(97+steps[i].y1); 1688 1689 s=s+String(8-steps[i].x1); 1690 if (steps[i].fig2==0) s=s+"-"; 1691 1692 } 1693 if (steps[i].fig2!=0) { 1694 s=s+"x"; 1695 if 1696 (abs(steps[i].fig2)>1) s=s+fig_symb[abs(steps[i].fig2)]; 1697 } 1698 s=s+char(97+steps[i].y2); 1699 1700 s=s+String(8-steps[i].x2); 1701 } 1702 if (steps[i].type>3) s=s+fig_symb[steps[i].type-2]; 1703 1704 if (steps[i].check==1) s=s+"+"; else 1705 if (steps[i].check==2) s=s+"#"; 1706 1707 return s; 1708} 1709//**************************** 1710void show_steps() { 1711 tft.fillRect(320,0,160,222,getColor(8,8,8)); 1712 1713 tft.fillRect(320,0,160,16,DARK); 1714 tft.setTextSize(1); 1715 tft.setTextColor(WHITE,DARK); 1716 1717 tft.setCursor(332,4); 1718 tft.print("ArduinoMega Chess (DUE)"); 1719 1720 tft.setTextSize(1); 1721 int i=1; int y=20; 1722 tft.setTextColor(GRAY); 1723 1724 int cur=(cur_step+1)/2; // 1725 int lim_step=cur_step; 1726 1727 if (checkmatesearch&&lastscore>9000) { 1728 tft.setTextColor(GREEN); 1729 1730 lim_step=cur_step+10000-lastscore; 1731 cur=lim_step/2; 1732 } 1733 if (cur>19) 1734 i=cur-19; 1735 while (i<=cur&&y<215) { // 1736 tft.setCursor(324,y); 1737 1738 tft.print(i); 1739 tft.print(". "); 1740 if (steps[i*2-1].fig1!=0) tft.print(str_step(i*2-1)); 1741 1742 if (steps[i*2].fig1==0||i*2>lim_step) break; 1743 tft.print(", "); 1744 1745 tft.print(str_step(i*2)); 1746 i++; y+=10; 1747 } 1748 1749} 1750//**************************** 1751boolean 1752 getdiagrowcheckw(signed char dj,signed char di) { // 1753signed char d,j1,i1; 1754 1755 j1=WKJ; i1=WKI; 1756 for (d=1;d<8;d++) { 1757 j1+=dj; i1+=di; 1758 1759 if (getpole(j1,i1)) { 1760 if (pole[j1][i1]==-fq||pole[j1][i1]==-fb) 1761 return true; 1762 if (pole[j1][i1]!=0) break; 1763 } else break; 1764 1765 } 1766 return false; 1767} 1768//**************************** 1769boolean getdiagrowcheckb(signed 1770 char dj,signed char di) { // 1771signed char d,j1,i1; 1772 j1=BKJ; 1773 i1=BKI; 1774 for (d=1;d<8;d++) { 1775 j1+=dj; i1+=di; 1776 if (getpole(j1,i1)) 1777 { 1778 if (pole[j1][i1]==fq||pole[j1][i1]==fb) return true; 1779 1780 if (pole[j1][i1]!=0) break; 1781 } else break; 1782 } 1783 return 1784 false; 1785} 1786//**************************** 1787boolean getstreightrowcheckw(signed 1788 char dj,signed char di) { // - 1789signed char d,j1,i1; 1790 j1=WKJ; i1=WKI; 1791 1792 for (d=1;d<8;d++) { 1793 j1+=dj; i1+=di; 1794 if (getpole(j1,i1)) 1795 { 1796 if (pole[j1][i1]==-fq||pole[j1][i1]==-fr) return true; 1797 1798 if (pole[j1][i1]!=0) break; 1799 } else break; 1800 } 1801 return 1802 false; 1803} 1804//**************************** 1805boolean getstreightrowcheckb(signed 1806 char dj,signed char di) { // - 1807signed char d,j1,i1; 1808 j1=BKJ; i1=BKI; 1809 1810 for (d=1;d<8;d++) { 1811 j1+=dj; i1+=di; 1812 if (getpole(j1,i1)) 1813 { 1814 if (pole[j1][i1]==fq||pole[j1][i1]==fr) return true; 1815 1816 if (pole[j1][i1]!=0) break; 1817 } else break; 1818 } 1819 return 1820 false; 1821} 1822//**************************** 1823boolean get_check(signed char king) 1824 { // 1825 if (king==fk) { // 1826 if (getdiagrowcheckw(-1, 1827 1)) return true; 1828 if (getdiagrowcheckw(-1,-1)) return true; 1829 1830 if (getdiagrowcheckw( 1,-1)) return true; 1831 if (getdiagrowcheckw( 1, 1)) 1832 return true; 1833 if (getstreightrowcheckw(-1, 0)) return true; 1834 1835 if (getstreightrowcheckw( 0, 1)) return true; 1836 if (getstreightrowcheckw( 1837 0,-1)) return true; 1838 if (getstreightrowcheckw( 1, 0)) return true; 1839 1840 if (getpole(WKJ-2,WKI-1)&&pole[WKJ-2][WKI-1]==-fn) return true; 1841 1842 if (getpole(WKJ-2,WKI+1)&&pole[WKJ-2][WKI+1]==-fn) return true; 1843 1844 if (getpole(WKJ-1,WKI-2)&&pole[WKJ-1][WKI-2]==-fn) return true; 1845 1846 if (getpole(WKJ-1,WKI+2)&&pole[WKJ-1][WKI+2]==-fn) return true; 1847 1848 if (getpole(WKJ+1,WKI-2)&&pole[WKJ+1][WKI-2]==-fn) return true; 1849 1850 if (getpole(WKJ+1,WKI+2)&&pole[WKJ+1][WKI+2]==-fn) return true; 1851 1852 if (getpole(WKJ+2,WKI-1)&&pole[WKJ+2][WKI-1]==-fn) return true; 1853 1854 if (getpole(WKJ+2,WKI+1)&&pole[WKJ+2][WKI+1]==-fn) return true; 1855 1856 if (getpole(WKJ-1,WKI-1)&&pole[WKJ-1][WKI-1]==-fp) return true; 1857 if 1858 (getpole(WKJ-1,WKI+1)&&pole[WKJ-1][WKI+1]==-fp) return true; 1859 1860 } else { // 1861 if (getdiagrowcheckb( 1,-1)) return true; 1862 1863 if (getdiagrowcheckb( 1, 1)) return true; 1864 if (getdiagrowcheckb(-1, 1)) 1865 return true; 1866 if (getdiagrowcheckb(-1,-1)) return true; 1867 if (getstreightrowcheckb( 1868 1, 0)) return true; 1869 if (getstreightrowcheckb( 0, 1)) return true; 1870 1871 if (getstreightrowcheckb( 0,-1)) return true; 1872 if (getstreightrowcheckb(-1, 1873 0)) return true; 1874 if (getpole(BKJ+2,BKI-1)&&pole[BKJ+2][BKI-1]==fn) 1875 return true; 1876 if (getpole(BKJ+2,BKI+1)&&pole[BKJ+2][BKI+1]==fn) return 1877 true; 1878 if (getpole(BKJ+1,BKI-2)&&pole[BKJ+1][BKI-2]==fn) return 1879 true; 1880 if (getpole(BKJ+1,BKI+2)&&pole[BKJ+1][BKI+2]==fn) return true; 1881 1882 if (getpole(BKJ-1,BKI-2)&&pole[BKJ-1][BKI-2]==fn) return 1883 true; 1884 if (getpole(BKJ-1,BKI+2)&&pole[BKJ-1][BKI+2]==fn) return true; 1885 1886 if (getpole(BKJ-2,BKI-1)&&pole[BKJ-2][BKI-1]==fn) return true; 1887 1888 if (getpole(BKJ-2,BKI+1)&&pole[BKJ-2][BKI+1]==fn) return true; 1889 1890 if (getpole(BKJ+1,BKI-1)&&pole[BKJ+1][BKI-1]==fp) return true; 1891 if (getpole(BKJ+1,BKI+1)&&pole[BKJ+1][BKI+1]==fp) 1892 return true; 1893 } 1894 if (abs(BKJ-WKJ)<=1&&abs(BKI-WKI)<=1) return 1895 true; // 1896 return false; 1897} 1898//**************************** 1899boolean 1900 getpole(signed char j,signed char i) { // 1901 if (j>=0&&j<8&&i>=0&&i<8) 1902 return true; 1903 return false; 1904} 1905//**************************** 1906void 1907 addstep(signed char j1,signed char i1,signed char j2,signed char i2,signed char 1908 type) { 1909 int st=start_var+cur_var; 1910 steps[st].x1=j1; 1911 steps[st].x2=j2; 1912 1913 steps[st].y1=i1; 1914 steps[st].y2=i2; 1915 steps[st].fig1=pole[j1][i1]; 1916 steps[st].fig2=pole[j2][i2]; 1917 1918 if (type==1) { // 1919 steps[st].fig2=-steps[st].fig1; 1920 1921 } 1922 steps[st].type=type; 1923 signed char ko=-fk; // 1924 if (steps[st].fig1>0) 1925 ko=fk; 1926 movestep(st); 1927 if (get_check(ko)) { backstep(st); return; } // 1928 - 1929 boolean che=get_check(-ko); // 1930 backstep(st); 1931 steps[st].weight=abs(steps[st].fig2)-abs(steps[st].fig1); 1932 1933 if (type>3) steps[st].weight+=fig_weight[type-2]; 1934 if (endspiel&&steps[st].fig1==ko) 1935 steps[st].weight+=10; // - 1936 steps[st].check=che; 1937 if (che) steps[st].weight+=10; 1938 1939 if (only_action) { 1940 if (steps[st].fig1==fp&&steps[st].x2==1||steps[st].fig1==-fp&&steps[st].x2==6) 1941 // 1942 { cur_var++; return; } 1943 if (steps[st].fig2==0&&steps[st].type<4&&!che&&!check_on_table) 1944 return; 1945 } 1946 1947 cur_var++; 1948} 1949//**************************** 1950void 1951 getrowstepsw(signed char j,signed char i,signed char dj,signed char di) { // 1952signed 1953 char d,j1,i1; 1954 j1=j; i1=i; 1955 for (d=1;d<8;d++) { 1956 j1+=dj; 1957 i1+=di; 1958 if (getpole(j1,i1)) { 1959 if (pole[j1][i1]<=0) addstep(j,i,j1,i1,0); 1960 1961 if (pole[j1][i1]!=0) break; 1962 } else break; 1963 } 1964} 1965//**************************** 1966void 1967 getrowstepsb(signed char j,signed char i,signed char dj,signed char di) { // 1968signed 1969 char d,j1,i1; 1970 j1=j; i1=i; 1971 for (d=1;d<8;d++) { 1972 j1+=dj; 1973 i1+=di; 1974 if (getpole(j1,i1)) { 1975 if (pole[j1][i1]>=0) addstep(j,i,j1,i1,0); 1976 1977 if (pole[j1][i1]!=0) break; 1978 } else break; 1979 } 1980} 1981//**************************** 1982void 1983 getonestepw(signed char j,signed char i,signed char dj,signed char di) { // 1984signed 1985 char j1,i1; 1986 j1=j+dj; i1=i+di; 1987 if (getpole(j1,i1)) 1988 if (pole[j1][i1]<=0) 1989 addstep(j,i,j1,i1,0); 1990} 1991//**************************** 1992void getonestepb(signed 1993 char j,signed char i,signed char dj,signed char di) { // 1994signed char j1,i1; 1995 1996 j1=j+dj; i1=i+di; 1997 if (getpole(j1,i1)) 1998 if (pole[j1][i1]>=0) 1999 addstep(j,i,j1,i1,0); 2000} 2001//**************************** 2002void sort_variants(int 2003 from, int to) { // 2004 while (1) { 2005 int mov=0; 2006 for (int 2007 i=from;i<to;i++) // 2008 if (steps[i].weight<steps[i+1].weight) { 2009 2010 mov++; 2011 step_type buf=steps[i]; 2012 steps[i]=steps[i+1]; 2013 2014 steps[i+1]=buf; 2015 } 2016 if (mov==0) break; 2017 2018 } 2019} 2020//**************************** 2021void load_variants(int nstep) 2022 { // nstep 2023 cur_var=0; 2024 if (pole[WKJ][WKI]!=fk||pole[BKJ][BKI]!=-fk) 2025 kingpositions(); 2026 if (nstep%2==1) check_on_table=get_check(fk); 2027 else 2028 check_on_table=get_check(-fk); // 2029 for (char i=0;i<8;i++) 2030 for (char 2031 j=0;j<8;j++) 2032 if (pole[j][i]>0&&nstep%2==1||pole[j][i]<0&&nstep%2==0) { 2033 2034 switch (pole[j][i]) { 2035 case fp: // 2036 if (getpole(j-1,i)&&pole[j-1][i]==0) 2037 2038 if (j!=1) addstep(j,i,j-1,i,0); else 2039 for (signed char 2040 t=4;t<8;t++) addstep(1,i,0,i,t); //-... 2041 if (j==6&&pole[j-1][i]==0&&pole[j-2][i]==0) 2042 addstep(j,i,j-2,i,0); 2043 if (getpole(j-1,i-1)&&pole[j-1][i-1]<0) 2044 if 2045 (j!=1) addstep(j,i,j-1,i-1,0); else 2046 for (signed char t=4;t<8;t++) 2047 addstep(j,i,j-1,i-1,t); //-... 2048 if 2049 (getpole(j-1,i+1)&&pole[j-1][i+1]<0) 2050 if (j!=1) addstep(j,i,j-1,i+1,0); 2051 else 2052 for (signed char t=4;t<8;t++) addstep(j,i,j-1,i+1,t); //-... 2053 2054 if (j==3&&steps[nstep-1].fig1==-fp&&steps[nstep-1].x2==3&&steps[nstep-1].x1==1) 2055 { 2056 if (steps[nstep-1].y2-i==1) { // 2057 addstep(j,i,j-1,i+1,1); 2058 2059 } else if (steps[nstep-1].y2-i==-1) { // 2060 addstep(j,i,j-1,i-1,1); 2061 2062 } 2063 } 2064 break; 2065 case -fp: // 2066 2067 if (getpole(j+1,i)&&pole[j+1][i]==0) 2068 if (j!=6) addstep(j,i,j+1,i,0); 2069 else 2070 for (signed char t=4;t<8;t++) addstep(j,i,j+1,i,t); //-... 2071 2072 if (j==1&&pole[j+1][i]==0&&pole[j+2][i]==0) addstep(j,i,j+2,i,0); 2073 if 2074 (getpole(j+1,i-1)&&pole[j+1][i-1]>0) 2075 if (j!=6) addstep(j,i,j+1,i-1,0); 2076 else 2077 for (signed char t=4;t<8;t++) addstep(j,i,j+1,i-1,t); //-... 2078 2079 if (getpole(j+1,i+1)&&pole[j+1][i+1]>0) 2080 if (j!=6) addstep(j,i,j+1,i+1,0); 2081 else 2082 for (signed char t=4;t<8;t++) addstep(j,i,j+1,i+1,t); //-... 2083 2084 if (j==4&&steps[nstep-1].fig1==fp&&steps[nstep-1].x2==4&&steps[nstep-1].x1==6) 2085 { 2086 if (steps[nstep-1].y2-i==1) 2087 { // 2088 addstep(j,i,j+1,i+1,1); 2089 } 2090 else if (steps[nstep-1].y2-i==-1) { // 2091 addstep(j,i,j+1,i-1,1); 2092 2093 } 2094 } 2095 break; 2096 case fn: // 2097 getonestepw(j,i,-2,-1); 2098 2099 getonestepw(j,i,-2,1); 2100 getonestepw(j,i,-1,-2); 2101 getonestepw(j,i,-1,2); 2102 2103 getonestepw(j,i,2,-1); 2104 getonestepw(j,i,2,1); 2105 getonestepw(j,i,1,-2); 2106 2107 getonestepw(j,i,1,2); 2108 break; 2109 case -fn: // 2110 2111 getonestepb(j,i,-2,-1); 2112 getonestepb(j,i,-2,1); 2113 getonestepb(j,i,-1,-2); 2114 2115 getonestepb(j,i,-1,2); 2116 getonestepb(j,i,2,-1); 2117 getonestepb(j,i,2,1); 2118 2119 getonestepb(j,i,1,-2); 2120 getonestepb(j,i,1,2); 2121 break; 2122 2123 case fb: // 2124 getrowstepsw(j,i,1,1); 2125 getrowstepsw(j,i,-1,-1); 2126 2127 getrowstepsw(j,i,1,-1); 2128 getrowstepsw(j,i,-1,1); 2129 break; 2130 2131 case -fb: // 2132 getrowstepsb(j,i,1,1); 2133 getrowstepsb(j,i,-1,-1); 2134 2135 getrowstepsb(j,i,1,-1); 2136 getrowstepsb(j,i,-1,1); 2137 break; 2138 2139 case fr: // 2140 getrowstepsw(j,i,1,0); 2141 getrowstepsw(j,i,-1,0); 2142 2143 getrowstepsw(j,i,0,1); 2144 getrowstepsw(j,i,0,-1); 2145 break; 2146 2147 case -fr: // 2148 getrowstepsb(j,i,1,0); 2149 getrowstepsb(j,i,-1,0); 2150 2151 getrowstepsb(j,i,0,1); 2152 getrowstepsb(j,i,0,-1); 2153 break; 2154 2155 case fq: // 2156 getrowstepsw(j,i,1,1); 2157 getrowstepsw(j,i,-1,-1); 2158 2159 getrowstepsw(j,i,1,-1); 2160 getrowstepsw(j,i,-1,1); 2161 getrowstepsw(j,i,1,0); 2162 2163 getrowstepsw(j,i,-1,0); 2164 getrowstepsw(j,i,0,1); 2165 getrowstepsw(j,i,0,-1); 2166 2167 break; 2168 case -fq: // 2169 getrowstepsb(j,i,1,1); 2170 2171 getrowstepsb(j,i,-1,-1); 2172 getrowstepsb(j,i,1,-1); 2173 getrowstepsb(j,i,-1,1); 2174 2175 getrowstepsb(j,i,1,0); 2176 getrowstepsb(j,i,-1,0); 2177 getrowstepsb(j,i,0,1); 2178 2179 getrowstepsb(j,i,0,-1); 2180 break; 2181 case fk: // 2182 getonestepw(j,i, 2183 1,-1); 2184 getonestepw(j,i, 1, 0); 2185 getonestepw(j,i, 1, 1); 2186 2187 getonestepw(j,i, 0,-1); 2188 getonestepw(j,i, 2189 0, 1); 2190 getonestepw(j,i,-1,-1); 2191 getonestepw(j,i,-1, 0); 2192 2193 getonestepw(j,i,-1, 1); 2194 if (!check_on_table) add_rok(j,i,nstep); 2195 2196 break; 2197 case -fk: // 2198 getonestepb(j,i, 1,-1); 2199 2200 getonestepb(j,i, 1, 0); 2201 getonestepb(j,i, 1, 1); 2202 getonestepb(j,i, 2203 0,-1); 2204 getonestepb(j,i, 0, 1); 2205 getonestepb(j,i,-1,-1); 2206 2207 getonestepb(j,i,-1, 0); 2208 getonestepb(j,i,-1, 1); 2209 2210 if (!check_on_table) add_rok(j,i,nstep); 2211 break; 2212 } //switch 2213 2214 } //if 2215 2216 if (nstep>cur_step) { 2217 for (int i=start_var;i<start_var+cur_var;i++) 2218 { // 2219 for (signed char j=0;j<MAXCUTS;j++) 2220 if 2221 (cuts[j].fig1==steps[i].fig1&& 2222 cuts[j].x1==steps[i].x1&&cuts[j].y1==steps[i].y1&& 2223 2224 cuts[j].x2==steps[i].x2&&cuts[j].y2==steps[i].y2) { 2225 steps[i].weight+=100+cuts[j].weight; 2226 2227 break; 2228 } 2229 if (nstep>1&&steps[nstep-1].fig2!=0) 2230 // - 2231 if (steps[nstep-1].x2==steps[i].x2&&steps[nstep-1].y2==steps[i].y2) 2232 2233 steps[i].weight+=1000; 2234 } 2235 } 2236 2237 2238 sort_variants(start_var,start_var+cur_var-1); 2239 2240 } 2241//**************************** 2242void 2243 movestep(int nstep) { 2244 pole[steps[nstep].x1][steps[nstep].y1]=0; 2245 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig1; 2246 2247 if (steps[nstep].fig1==fk) { 2248 WKJ=steps[nstep].x2; WKI=steps[nstep].y2; 2249 2250 } else if (steps[nstep].fig1==-fk) { 2251 BKJ=steps[nstep].x2; BKI=steps[nstep].y2; 2252 2253 } 2254 if (steps[nstep].type==0) return; 2255 if (steps[nstep].type==1) // 2256 2257 if (steps[nstep].fig1>0) 2258 pole[steps[nstep].x2+1][steps[nstep].y2]=0; 2259 2260 else 2261 pole[steps[nstep].x2-1][steps[nstep].y2]=0; 2262 else if (steps[nstep].type==2) 2263 // 2264 if (steps[nstep].fig1>0) { // 2265 pole[7][4]=0; pole[7][5]=fr; 2266 pole[7][6]=fk; pole[7][7]=0; //show_board(); delay(3000); 2267 } else { // 2268 2269 pole[0][4]=0; pole[0][5]=-fr; pole[0][6]=-fk; pole[0][7]=0; //show_board(); 2270 delay(3000); 2271 } 2272 else if (steps[nstep].type==3) // 2273 if (steps[nstep].fig1>0) 2274 { // 2275 pole[7][0]=0; pole[7][1]=0; pole[7][2]=fk; pole[7][3]=fr; pole[7][4]=0; 2276 2277 } else { // 2278 pole[0][0]=0; pole[0][1]=0; pole[0][2]=-fk; pole[0][3]=-fr; 2279 pole[0][4]=0; 2280 } 2281 else if (steps[nstep].type>3) //-.... 2282 if 2283 (steps[nstep].fig1>0) pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].type-2; 2284 2285 else pole[steps[nstep].x2][steps[nstep].y2]=2-steps[nstep].type; 2286} 2287//**************************** 2288 2289void backstep(int nstep) { 2290 pole[steps[nstep].x1][steps[nstep].y1]=steps[nstep].fig1; 2291 2292 pole[steps[nstep].x2][steps[nstep].y2]=steps[nstep].fig2; 2293 if (steps[nstep].fig1==fk) 2294 { 2295 WKJ=steps[nstep].x1; WKI=steps[nstep].y1; 2296 } else if (steps[nstep].fig1==-fk) 2297 { 2298 BKJ=steps[nstep].x1; BKI=steps[nstep].y1; 2299 } 2300 if (steps[nstep].type==0) 2301 return; 2302 if (steps[nstep].type==1) { // 2303 pole[steps[nstep].x2][steps[nstep].y2]=0; 2304 2305 if (steps[nstep].fig1>0) 2306 pole[steps[nstep].x2+1][steps[nstep].y2]=-fp; 2307 2308 else 2309 pole[steps[nstep].x2-1][steps[nstep].y2]=fp; 2310 } else 2311 if (steps[nstep].type==2) // 2312 if (steps[nstep].fig1>0) { // 2313 pole[7][4]=fk; 2314 pole[7][5]=0; pole[7][6]=0; pole[7][7]=fr; 2315 } else { // 2316 pole[0][4]=-fk; 2317 pole[0][5]=0; pole[0][6]=0; pole[0][7]=-fr; // show_board(); delay(3000); 2318 } 2319 2320 else if (steps[nstep].type==3) // 2321 if (steps[nstep].fig1>0) { // 2322 pole[7][0]=fr; 2323 pole[7][1]=0; pole[7][2]=0; pole[7][3]=0; pole[7][4]=fk; 2324 } else { // 2325 2326 pole[0][0]=-fr; pole[0][1]=0; pole[0][2]=0; pole[0][3]=0; pole[0][4]=-fk; 2327 2328 } 2329 2330} 2331//**************************** 2332void get_wrocks(int nstep) 2333 { 2334 w00=true; w000=true; 2335 for (int i=1;i<nstep;i++) { 2336 if (steps[i].fig1==fk) 2337 { w00=false; w000=false; return; } 2338 if (steps[i].fig1==fr) 2339 if (steps[i].x1==7&&steps[i].y1==7) 2340 w00=false; 2341 else if (steps[i].x1==7&&steps[i].y1==0) w000=false; 2342 } 2343} 2344//**************************** 2345void 2346 get_brocks(int nstep) { 2347 b00=true; b000=true; 2348 for (int i=1;i<nstep;i++) 2349 { 2350 if (steps[i].fig1==-fk) { b00=false; b000=false; return; } 2351 if (steps[i].fig1==-fr) 2352 2353 if (steps[i].x1==0&&steps[i].y1==7) b00=false; 2354 else if (steps[i].x1==0&&steps[i].y1==0) 2355 b000=false; 2356 } 2357} 2358//**************************** 2359void add_rok(signed 2360 char j,signed char i,int nstep) { // 2361boolean che1,che2; 2362 if (nstep%2==1) 2363 { // 2364 if (j!=7||i!=4) return; 2365 if (pole[7][5]==0&&pole[7][6]==0&&pole[7][7]==fr) 2366 { // 2367 pole[7][4]=0; 2368 WKI=5; che1=get_check(fk); 2369 WKI=6; che2=get_check(fk); 2370 2371 WKI=4; pole[7][4]=fk; 2372 get_wrocks(nstep); 2373 if (!che1&&!che2&&w00) 2374 addstep(7,4,7,6,2); 2375 } 2376 if (pole[7][0]==fr&&pole[7][1]==0&&pole[7][2]==0&&pole[7][3]==0) 2377 { // 2378 pole[7][4]=0; 2379 WKI=2; che1=get_check(fk); 2380 WKI=3; che2=get_check(fk); 2381 2382 WKI=4; pole[7][4]=fk; 2383 get_wrocks(nstep); 2384 if (!che1&&!che2&&w000) 2385 addstep(7,4,7,2,3); 2386 } 2387 } else { // 2388 if (j!=0||i!=4) return; 2389 if 2390 (pole[0][5]==0&&pole[0][6]==0&&pole[0][7]==-fr) { // 2391 pole[0][4]=0; 2392 2393 BKI=5; che1=get_check(-fk); 2394 BKI=6; che2=get_check(-fk); 2395 BKI=4; 2396 pole[0][4]=-fk; 2397 get_brocks(nstep); 2398 if (!che1&&!che2&&b00) addstep(0,4,0,6,2); 2399 2400 } 2401 if (pole[0][0]==-fr&&pole[0][1]==0&&pole[0][2]==0&&pole[0][3]==0) { // 2402 2403 pole[0][4]=0; 2404 BKI=2; che1=get_check(-fk); 2405 BKI=3; che2=get_check(-fk); 2406 2407 BKI=4; pole[0][4]=-fk; 2408 get_brocks(nstep); 2409 if (!che1&&!che2&&b000) 2410 addstep(0,4,0,2,3); 2411 } 2412 2413 } 2414} 2415//**************************** 2416int 2417 addrowstepsw(signed char j,signed char i,signed char dj,signed char di,signed char 2418 dc) { // 2419signed char d,j1,i1; int c=0; 2420 j1=j; i1=i; 2421 2422 for (d=1;d<8;d++) { 2423 j1+=dj; i1+=di; 2424 if (getpole(j1,i1)) { 2425 2426 if (pole[j1][i1]==0) c+=dc; 2427 else if (pole[j1][i1]>0) { // 2428 2429 c+=1; break; 2430 } else { 2431 if (cur_step>6) c+=-pole[j1][i1]; 2432 // 2433 break; 2434 } 2435 } else break; 2436 } 2437 2438 return c; 2439} 2440//**************************** 2441int addrowstepsb(signed char 2442 j,signed char i,signed char dj,signed char di,signed char dc) { // 2443signed 2444 char d,j1,i1; int c=0; 2445 j1=j; i1=i; 2446 for (d=1;d<8;d++) { 2447 2448 j1+=dj; i1+=di; 2449 if (getpole(j1,i1)) { 2450 if (pole[j1][i1]==0) 2451 c-=dc; 2452 else if (pole[j1][i1]<0) { // 2453 c-=1; break; 2454 2455 } else { 2456 if (cur_step>6) c-=pole[j1][i1]; // 2457 2458 break; 2459 } 2460 } else break; 2461 } 2462 return c; 2463} 2464//**************************** 2465int 2466 addonestepw(signed char j,signed char i,signed char dj,signed char di) { // 2467signed 2468 char j1,i1; 2469 j1=j+dj; i1=i+di; 2470 if (getpole(j1,i1)) 2471 if (pole[j1][i1]==0) 2472 return 2; 2473 else if (pole[j1][i1]>0) return 1; 2474 else return -pole[j1][i1]; 2475 2476 return 0; 2477} 2478//**************************** 2479int addonestepb(signed 2480 char j,signed char i,signed char dj,signed char di) { // 2481signed char j1,i1; 2482 2483 j1=j+dj; i1=i+di; 2484 if (getpole(j1,i1)) 2485 if (pole[j1][i1]==0) return 2486 2; 2487 else if (pole[j1][i1]<0) return 1; 2488 else return pole[j1][i1]; 2489 2490 return 0; 2491} 2492//**************************** 2493int activity() { 2494 // - + 2495int c=0; 2496signed char pwj[8],pwi[8],pbj[8],pbi[8],ipw=0,ipb=0,nbw=0,nbb=0; 2497 2498 if (checkmatesearch) return 0; // 2499 for (char i=0;i<8;i++) 2500 for 2501 (char j=0;j<8;j++) 2502 if (pole[j][i]!=0) { 2503 switch (pole[j][i]) { 2504 2505 case fp: // 2506 if (getpole(j,i+1)&&pole[j][i+1]==fp) c+=2; // 2507 2508 if (getpole(j-1,i)&&pole[j-1][i]==fp) c-=20; // 2509 pwj[ipw]=j; pwi[ipw]=i; 2510 ipw++; 2511 break; 2512 case -fp: // 2513 if (getpole(j,i-1)&&pole[j][i-1]==-fp) 2514 c-=2; // 2515 if (getpole(j+1,i)&&pole[j+1][i]==-fp) c+=20; // 2516 pbj[ipb]=j; 2517 pbi[ipb]=i; ipb++; 2518 break; 2519 case fn: // 2520 c+=addonestepw(j,i,-2,-1); 2521 2522 c+=addonestepw(j,i,-2,1); 2523 c+=addonestepw(j,i,-1,-2); 2524 c+=addonestepw(j,i,-1,2); 2525 2526 c+=addonestepw(j,i,2,-1); 2527 c+=addonestepw(j,i,2,1); 2528 c+=addonestepw(j,i,1,-2); 2529 2530 c+=addonestepw(j,i,1,2); 2531 break; 2532 case -fn: // 2533 2534 c+=addonestepb(j,i,-2,-1); 2535 c+=addonestepb(j,i,-2,1); 2536 c+=addonestepb(j,i,-1,-2); 2537 2538 c+=addonestepb(j,i,-1,2); 2539 c+=addonestepb(j,i,2,-1); 2540 c+=addonestepb(j,i,2,1); 2541 2542 c+=addonestepb(j,i,1,-2); 2543 c+=addonestepb(j,i,1,2); 2544 break; 2545 2546 case fb: // 2547 c+=addrowstepsw(j,i,1,1,1); 2548 c+=addrowstepsw(j,i,-1,-1,2); 2549 2550 c+=addrowstepsw(j,i,1,-1,1); 2551 c+=addrowstepsw(j,i,-1,1,2); 2552 2553 nbw++; 2554 break; 2555 case -fb: // 2556 c+=addrowstepsb(j,i,1,1,2); 2557 2558 c+=addrowstepsb(j,i,-1,-1,1); 2559 c+=addrowstepsb(j,i,1,-1,2); 2560 c+=addrowstepsb(j,i,-1,1,1); 2561 2562 nbb++; 2563 break; 2564 case fr: // 2565 c+=addrowstepsw(j,i,1,0,2); 2566 // 2567 c+=addrowstepsw(j,i,-1,0,2); 2568 c+=addrowstepsw(j,i,0,1,1); 2569 2570 c+=addrowstepsw(j,i,0,-1,1); 2571 break; 2572 case -fr: // 2573 2574 c+=addrowstepsb(j,i,1,0,2); // 2575 c+=addrowstepsb(j,i,-1,0,2); 2576 c+=addrowstepsb(j,i,0,1,1); 2577 2578 c+=addrowstepsb(j,i,0,-1,1); 2579 break; 2580 case fq: // 2581 if 2582 (cur_step>10) { 2583 c+=addrowstepsw(j,i,1,1,1); 2584 c+=addrowstepsw(j,i,-1,-1,2); 2585 2586 c+=addrowstepsw(j,i,1,-1,1); 2587 c+=addrowstepsw(j,i,-1,1,2); 2588 2589 c+=addrowstepsw(j,i,1,0,1); 2590 c+=addrowstepsw(j,i,-1,0,2); 2591 c+=addrowstepsw(j,i,0,1,1); 2592 2593 c+=addrowstepsw(j,i,0,-1,1); 2594 } 2595 break; 2596 case 2597 -fq: // 2598 if (cur_step>10) { 2599 c+=addrowstepsb(j,i,1,1,2); 2600 2601 c+=addrowstepsb(j,i,-1,-1,1); 2602 c+=addrowstepsb(j,i,1,-1,2); 2603 2604 c+=addrowstepsb(j,i,-1,1,1); 2605 c+=addrowstepsb(j,i,1,0,2); 2606 2607 c+=addrowstepsb(j,i,-1,0,1); 2608 c+=addrowstepsb(j,i,0,1,1); 2609 c+=addrowstepsb(j,i,0,-1,1); 2610 2611 } 2612 break; 2613 } //switch 2614 if (cur_step>6) { 2615 2616 if (pole[j][i]>0) { // 2617 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c+=3; 2618 2619 if (abs(BKJ-j)<2&&abs(BKI-i)<2) c+=3; 2620 if (abs(WKJ-j)<3&&abs(WKI-i)<3) 2621 c+=2; 2622 if (abs(BKJ-j)<3&&abs(BKI-i)<3) c+=2; 2623 } else { // 2624 2625 if (abs(WKJ-j)<2&&abs(WKI-i)<2) c-=3; 2626 if (abs(BKJ-j)<2&&abs(BKI-i)<2) 2627 c-=3; 2628 if (abs(WKJ-j)<3&&abs(WKI-i)<3) c-=2; 2629 if (abs(BKJ-j)<3&&abs(BKI-i)<3) 2630 c-=2; 2631 } 2632 } 2633 } 2634 if (nbw>1) c+=30; // 2635 if 2636 (nbb>1) c-=30; 2637 for (signed char w=0;w<ipw;w++) { // 2638 boolean pass=1; 2639 2640 for (signed char b=0;b<ipb;b++) { 2641 if (pwi[w]>0&&pbi[b]==pwi[w]-1&&pbj[b]<pwj[w]) 2642 pass=0; // 2643 if (pbi[b]==pwi[w]&&pbj[b]<pwj[w]) pass=0; // 2644 if 2645 ( pwi[w]<7&&pbi[b]==pwi[w]+1&&pbj[b]<pwj[w]) pass=0; // 2646 if (!pass) 2647 break; 2648 } 2649 if (pass) { 2650 c+=50; 2651 if (!endspiel) 2652 break; // 2653 } 2654 } 2655 for (signed char b=0;b<ipb;b++) { // 2656 2657 boolean pass=1; 2658 for (signed char w=0;w<ipw;w++) { 2659 if (pbi[b]>0&&pwi[w]==pbi[b]-1&&pwj[w]>pbj[b]) 2660 pass=0; // 2661 if (pwi[w]==pbi[b]&&pwj[w]>pbj[b]) pass=0; // 2662 if 2663 (pbi[b]<7&&pwi[w]==pbi[b]+1&&pwj[w]>pbj[b]) pass=0; // 2664 if (!pass) 2665 break; 2666 } 2667 if (pass) { 2668 c-=50; 2669 if (!endspiel) 2670 break; // 2671 } 2672 } 2673 return c; 2674} 2675//**************************** 2676int 2677 evaluate(int nstep) { // 2678 int ww=0, wb=0; 2679 for (char i=0;i<8;i++) 2680 2681 for (char j=0;j<8;j++) 2682 if (pole[j][i]<0) { 2683 wb+=fig_weight[-pole[j][i]]+(short)pgm_read_word(&pos[-pole[j][i]-1][7-j][i]); 2684 2685 } 2686 else if (pole[j][i]>0) { 2687 ww+=fig_weight[pole[j][i]]+(short)pgm_read_word(&pos[pole[j][i]-1][j][i]); 2688 2689 } 2690 count++; // 2691 2692 int str=activity(); 2693 if (endspiel) { // 2694 wb+=(short)pgm_read_word(&pos[6][7-BKJ][BKI])-(short)pgm_read_word(&pos[5][7-BKJ][BKI]); 2695 2696 ww+=(short)pgm_read_word(&pos[6][WKJ][WKI])-(short)pgm_read_word(&pos[5][WKJ][WKI]); 2697 2698 if (wb<450&&ww>450) { // - - 2699 2700 str-=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 2701 } else if (ww<450&&wb>450) { // 2702 2703 str+=(abs(WKJ-BKJ)+abs(WKI-BKI))*30; 2704 } 2705 } 2706 if (nstep>8) { // 2707 2708 if (steps[nstep-1].fig1==steps[nstep-5].fig1&&steps[nstep-2].fig1==steps[nstep-6].fig1&& 2709 2710 steps[nstep-3].fig1==steps[nstep-7].fig1&&steps[nstep-4].fig1==steps[nstep-8].fig1) 2711 2712 if (steps[nstep-1].x1==steps[nstep-5].x1&&steps[nstep-2].x1==steps[nstep-6].x1&& 2713 2714 steps[nstep-3].x1==steps[nstep-7].x1&&steps[nstep-4].x1==steps[nstep-8].x1) 2715 2716 if (steps[nstep-1].x2==steps[nstep-5].x2&&steps[nstep-2].x2==steps[nstep-6].x2&& 2717 2718 steps[nstep-3].x2==steps[nstep-7].x2&&steps[nstep-4].x2==steps[nstep-8].x2) 2719 2720 if (steps[nstep-1].y1==steps[nstep-5].y1&&steps[nstep-2].y1==steps[nstep-6].y1&& 2721 2722 steps[nstep-3].y1==steps[nstep-7].y1&&steps[nstep-4].y1==steps[nstep-8].x1) 2723 2724 if (steps[nstep-1].y2==steps[nstep-5].y2&&steps[nstep-2].y2==steps[nstep-6].y2&& 2725 2726 steps[nstep-3].y2==steps[nstep-7].y2&&steps[nstep-4].y2==steps[nstep-8].y2) 2727 2728 { Serial.println(F(" Draw - 3 repeat")); return 0; } 2729 } 2730 2731 if (nstep%2==1) return 5000*(ww-wb)/(ww+wb+2000)+str; else return 5000*(wb-ww)/(ww+wb+2000)-str; 2732 2733} 2734//**************************** 2735void add_cut(int ind) {// ind 2736 2737 int minbeta=30000, minindex=MAXCUTS-1; 2738 for (char i=0;i<MAXCUTS;i++) { 2739 2740 if (cuts[i].weight==0) { minindex=i; break; }// 2741 if (cuts[i].fig1==steps[ind].fig1&& 2742 2743 cuts[i].x1==steps[ind].x1&&cuts[i].y1==steps[ind].y1&& 2744 cuts[i].x2==steps[ind].x2&&cuts[i].y2==steps[ind].y2) 2745 { 2746 cuts[i].weight++; return; // - 1 2747 } 2748 if (cuts[i].weight<minbeta) 2749 { // 2750 minbeta=cuts[i].weight; 2751 minindex=i; 2752 } 2753 } 2754 2755 cuts[minindex]=steps[ind]; 2756 cuts[minindex].weight=1; //1 2757} 2758//**************************** 2759 2760int quiescence(int start, int nstep, int alpha, int beta ) { 2761 if 2762 (nstep-cur_step>=LIMDEPTH||start>MAXSTEPS-70) return evaluate(nstep); 2763 2764 if (!solving&&progress==0) return -5000; 2765 int score=-20000; 2766 2767 start_var=start; 2768 only_action=true; 2769 load_variants(nstep); 2770 2771 if (!check_on_table) { 2772 int stand_pat = evaluate(nstep); 2773 2774 if (stand_pat >= score) score=stand_pat; 2775 if (score>alpha) alpha=score; 2776 2777 if (alpha>=beta) return alpha; 2778 } 2779 if 2780 (cur_var==0) { 2781 if (check_on_table) { 2782 if (TRACE) Serial.println(F("checkmate?")); 2783 2784 return -10000+nstep-cur_step; 2785 2786 } else return evaluate(nstep); 2787 } 2788 int j=start+cur_var; 2789 2790 for (int i=start;i<j;i++) { // 2791 if (TRACE) { //***** 2792 2793 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" . ")); 2794 2795 Serial.println(str_step(i)); 2796 } 2797 movestep(i); 2798 2799 steps[nstep]=steps[i]; 2800 int tmp=-quiescence(j+1,nstep+1,-beta,-alpha); 2801 2802 backstep(i); 2803 if (tmp>score) score=tmp; 2804 2805 if (score>alpha) alpha=score; 2806 if (alpha>=beta ) { 2807 2808 add_cut(nstep); 2809 return alpha; 2810 } 2811 2812 if (checkmatesearch&&alpha>9000) break; 2813 gui(); 2814 2815 } 2816 return score; 2817} 2818//**************************** 2819int alphaBeta(int 2820 start, int nstep, int alpha, int beta, int depthleft) { 2821//start - 2822//nstep 2823 - 2824int score=-20000,best; 2825 if( depthleft==0) return quiescence(start,nstep,alpha,beta) 2826 ; 2827 if (start>MAXSTEPS-70) return evaluate(nstep); 2828 start_var=start; 2829 2830 only_action=false; 2831 if (nstep!=cur_step) load_variants(nstep); 2832 2833 if (cur_var==0) { 2834 if (check_on_table) { 2835 if (TRACE) 2836 Serial.println(F("checkmate!")); 2837 return -10000+nstep-cur_step; 2838 2839 } 2840 return 0; 2841 } 2842 int j=start+cur_var; 2843 2844 best=start; 2845 for (int i=start;i<j;i++) { // 2846 2847 if (nstep==cur_step) { 2848 Serial.print(str_step(i)); 2849 Serial.print(" "); 2850 Serial.print(i-start+1); Serial.print("/"); Serial.print(j-start); 2851 2852 if (steps[i].weight<-9000) { Serial.println(F(" 2853 checkmate")); continue; } 2854 if (steps[i].fig2!=0||steps[i].check||alpha<-100) 2855 2856 // , , -100 2857 { LIMDEPTH=MAXDEPTH+2; Serial.print(F("+2")); 2858 } else LIMDEPTH=MAXDEPTH; 2859 } else { 2860 if (TRACE) { //***** 2861 2862 for (int u=0;u<nstep-cur_step;u++) Serial.print(F(" ")); 2863 2864 Serial.println(str_step(i)); 2865 } 2866 } 2867 2868 movestep(i); 2869 steps[nstep]=steps[i]; 2870 int tmp=-alphaBeta(j+1,nstep+1,-beta,-alpha,depthleft-1); 2871 2872 backstep(i); 2873 steps[i].weight=tmp; 2874 if (tmp>score) 2875 score=tmp; 2876 if (score>alpha) { 2877 alpha=score; 2878 if (nstep>cur_step) 2879 add_cut(nstep); // 2880 if (TRACE) { Serial.print(F("ALPHA+:")); 2881 Serial.println(score); } 2882 best=i; 2883 if (nstep==cur_step) 2884 { 2885 steps[0]=steps[best]; 2886 Serial.print(F(" BEST")); 2887 2888 } 2889 } 2890 if (alpha>=beta ) { 2891 if (nstep>cur_step) 2892 add_cut(nstep); // 2893 if (TRACE) { Serial.print(F("BETA 2894 CUT:")); Serial.println(score); } 2895 return alpha; 2896 } 2897 2898 if (nstep==cur_step) { 2899 Serial.print(F(" ")); 2900 Serial.println(tmp); 2901 progress=100*(i-start+1)/(j-start); 2902 2903 if (alpha==9999||alpha==-5000) break; 2904 if 2905 (checkmatesearch&&alpha>9000&&limit<3) break; 2906 if (!solving) { 2907 2908 if (alpha>startweight&&cur_level>1) break; 2909 else { // - 2910 +50% 2911 if (BAction.bs==F("stopping")) break; 2912 2913 if (cur_level>1&&100*(millis()-starttime)/(limittime-starttime)>240-limit*20) 2914 break; 2915 } 2916 } 2917 } 2918 2919 if (nstep==cur_step+1&&!solving&&progress==0) break; 2920 gui(); 2921 2922 } 2923 if (nstep==cur_step) { steps[nstep]=steps[best]; 2924 steps[0]=steps[best]; } 2925 return score; 2926} 2927//**************************** 2928 2929void kingpositions() { 2930 for (char i=0;i<8;i++) // 2931 for (char j=0;j<8;j++) 2932 2933 if (pole[j][i]==fk) { 2934 WKJ=j; WKI=i; 2935 } else if (pole[j][i]==-fk) 2936 { 2937 BKJ=j; BKI=i; 2938 } 2939} 2940//**************************** 2941int 2942 get_endspiel() { // 2943 int weight=0; 2944 for (char i=0;i<8;i++) 2945 for 2946 (char j=0;j<8;j++) 2947 if (pole[j][i]<0) 2948 weight+=fig_weight[-pole[j][i]]; 2949 2950 else if (pole[j][i]>0) 2951 weight+=fig_weight[pole[j][i]]; 2952 // 8000 2953 if (weight<3500) endspiel=true; else endspiel=false; 2954 return 2955 weight; 2956} 2957//**************************** 2958boolean is_drawn() { // 2959 2960 boolean drawn=false; 2961 int cn=0,cbw=0,cbb=0,co=0,cb=0,cw=0; 2962 for (char 2963 i=0;i<8;i++) 2964 for (char j=0;j<8;j++) { 2965 if (abs(pole[j][i])==1) co++; 2966 2967 if (abs(pole[j][i])>3&&abs(pole[j][i])<6) co++; // , , 2968 if (abs(pole[j][i])==6) 2969 continue; // 2970 if (abs(pole[j][i])==2) cn++; // 2971 if (abs(pole[j][i])==3&&(i+j+2)%2==0) 2972 cbb++; // 2973 if (abs(pole[j][i])==3&&(i+j+2)%2==1) cbw++; // 2974 if 2975 (pole[j][i]==3) cw++; // 2976 if (pole[j][i]==-3) cb++; // 2977 } 2978 2979 if (cn==1&&co+cbb+cbw==0) drawn=true; // 2980 if (cbb+cbw==1&&co+cn==0) drawn=true; 2981 // 2982 if (co+cn+cbb==0||co+cn+cbw==0) drawn=true; // 2983 if (co+cn==0&&cb==1&&cw==1) 2984 drawn=true; // 2985 if (drawn) return drawn; 2986 int rep=1; 2987 for (int s=cur_step-1; 2988 s>0; s--) { 2989 if (steps[s].fig1==0) continue; 2990 backstep(s); 2991 boolean 2992 eq=true; 2993 for (char i=0;i<8;i++) 2994 for (char j=0;j<8;j++) 2995 if 2996 (pole[j][i]!=pole0[j][i]) 2997 { eq=false; break; } 2998 if (eq) rep++; 2999 3000 if (rep>2) break; 3001 } 3002 if (rep>1) { Serial.print(rep); Serial.println(" 3003 repetitions"); } 3004 for (char i=0;i<8;i++) 3005 for (char j=0;j<8;j++) pole[j][i]=pole0[j][i]; 3006 // 3007 if (rep>2) drawn=true; 3008 return drawn; 3009} 3010//**************************** 3011 3012int solve_step() { 3013const int LMIN[7]={2,3,4,5,6,7,8}; 3014const int LMAX[7]={4,6,8,10,12,14,16}; 3015const 3016 int LCMIN[7]={2,4,6,8,10,12,14}; 3017const int LCMAX[7]={4,6,8,10,12,14,16}; 3018boolean 3019 check_on; 3020 starttime=millis(); 3021 isstatus=1; 3022 deletebuttons(); 3023 3024 clearstatus(); 3025 tft.drawFastHLine(10,312,300,DARK); 3026 tft.drawFastHLine(10,315,300,DARK); 3027 3028 BAction.Show("STOP"); 3029 BBW.Hide(); 3030 BBack.Hide(); 3031 BLimit.Hide(); 3032 3033 BCM.Hide(); 3034 BDesc.Hide(); 3035 BSound.Hide(); 3036 BRotate.Hide(); 3037 3038 limittime=starttime+limits[limit]*1000; // 3039 for (char i=0;i<8;i++) 3040 3041 for (char j=0;j<8;j++) pole0[j][i]=pole[j][i]; // 3042 lastbest.fig1=0; 3043 3044 kingpositions(); 3045 int wei=get_endspiel(); 3046 count=0; 3047 startweight=evaluate(cur_step); 3048 3049 Serial.println(""); 3050 Serial.println(F("---------------")); 3051 if (endspiel) 3052 { Serial.print(F("Endspiel: ")); Serial.println(wei); } 3053 if (cur_step%2==1) 3054 Serial.print(F("WHITE, ")); else Serial.print(F("BLACK, ")); 3055 Serial.print(F("start 3056 Score= ")); Serial.println(startweight); 3057 start_var=cur_step+21; 3058 only_action=false; 3059 3060 lastbest.fig1=0; 3061 steps[0].fig1=0; 3062 load_variants(cur_step); 3063 3064 check_on=check_on_table; 3065 if (cur_var==0||is_drawn()) { 3066 beep(500); 3067 3068 tft.drawFastHLine(10,312,300,BLACK); 3069 tft.drawFastHLine(10,315,300,BLACK); 3070 3071 BAction.Hide(); 3072 isstatus=0; 3073 solving=false; 3074 if (check_on_table) 3075 return -9999; else return 8999; 3076 } 3077 if (cur_var==1) { // 1 3078 tft.drawFastHLine(10,312,300,BLACK); 3079 3080 tft.drawFastHLine(10,315,300,BLACK); 3081 BAction.Hide(); 3082 isstatus=0; 3083 3084 solving=false; 3085 steps[cur_step]=steps[cur_step+21]; 3086 return startweight; 3087 3088 } 3089 int vars=cur_var; 3090 int ALPHA=-20000; 3091 int BETA=20000; 3092 3093 int score; 3094 solving=true; 3095 if (checkmatesearch) { 3096 Serial.println(F("Checkmate 3097 search")); 3098 steps[cur_step].fig1=0; lastscore=0; show_steps(); 3099 } 3100 int 3101 l=0; 3102 for (char i=0;i<MAXCUTS;i++) { cuts[i].weight=0; cuts[i].fig1=0; } // 3103 3104 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // 3105 movestep(i); 3106 steps[i].weight=evaluate(cur_step); 3107 3108 if (steps[i].fig2!=0) steps[i].weight-=steps[i].fig1; 3109 backstep(i); 3110 } 3111 3112 while (l<7) { 3113 cur_level=l+1; 3114 progress=0; 3115 tft.drawFastHLine(10,315,300,DARK); 3116 3117 MINDEPTH=LMIN[l]; 3118 MAXDEPTH=LMAX[l]; 3119 if (checkmatesearch) 3120 { 3121 ALPHA=9000; BETA=10000; 3122 MINDEPTH=LCMIN[l]; 3123 MAXDEPTH=LCMAX[l]; 3124 3125 } 3126 if (l>0) Serial.println(""); Serial.print(F("******* LEVEL=")); 3127 Serial.print(l+1); 3128 Serial.print(" "); Serial.print(MINDEPTH); Serial.print("-"); 3129 Serial.print(MAXDEPTH); 3130 Serial.print(" "); Serial.print((millis()-starttime)/1000.,1); 3131 Serial.println("s"); 3132 if (checkmatesearch) 3133 { 3134 for (int i=cur_step+21;i<cur_step+21+vars;i++) { // - 3135 movestep(i); 3136 3137 start_var=cur_step+22+vars; 3138 only_action=false; 3139 load_variants(cur_step+1); 3140 3141 backstep(i); 3142 steps[i].weight=-cur_var; 3143 3144 } 3145 } 3146 sort_variants(cur_step+21,cur_step+20+vars); // 3147 3148 cur_var=vars; 3149 check_on_table=check_on; 3150 score=alphaBeta(cur_step+21,cur_step,ALPHA,BETA,MINDEPTH); 3151 3152 if (score>9996) break; 3153 if (score>9000&&limit<3) break; 3154 if 3155 (100*(millis()-starttime)/(limittime-starttime)>80-l*8) break; 3156 if 3157 (!solving) break; 3158 l++; 3159 } //while l 3160 if (score<-9000) 3161 { 3162 Serial.println(F("GIVE UP!")); 3163 } else { 3164 Serial.print(F("STEP=")); 3165 Serial.println(str_step(cur_step)); 3166 if (score>9000) { 3167 Serial.print(F("CHECKMATE 3168 ")); 3169 if (score<9999) { 3170 Serial.print(F("in ")); Serial.print((9999-score)/2+1); 3171 Serial.print(F(" steps")); 3172 } else steps[cur_step].check=2; 3173 Serial.println(""); 3174 3175 if (checkmatesearch) { 3176 steps[cur_step+9999-score].check=2; 3177 3178 steps[cur_step+10000-score].fig1=0; 3179 for (int i=cur_step;i<cur_step+10000-score;i++) 3180 Serial.println(str_step(i)); 3181 } 3182 } 3183 } 3184 tft.drawFastHLine(10,312,300,BLACK); 3185 3186 tft.drawFastHLine(10,315,300,BLACK); 3187 progress=0; 3188 show_status(); 3189 3190 BAction.Hide(); 3191 solving=0; 3192 isstatus=0; 3193 return score; 3194 3195 3196} 3197//**************************** 3198 3199
Downloadable files
Buzzer connection scheme
About schematic - display with touchscreen just plugged into arduino slot. Buzzer connected to the digital pin d44 with its plus clemme, and opposite to the ground through 100 Om resistor.
Buzzer connection scheme
Comments
Only logged in users can leave comments