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, 123