flipchess_scene_1.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #include "../flipchess.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <furi_hal_random.h>
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. //#include <dolphin/dolphin.h>
  8. #include <string.h>
  9. //#include "flipchess_icons.h"
  10. #include "../helpers/flipchess_haptic.h"
  11. #define SCL_960_CASTLING 0 // setting to 1 compiles a 960 version of smolchess
  12. #define XBOARD_DEBUG 0 // will create files with xboard communication
  13. #define SCL_EVALUATION_FUNCTION SCL_boardEvaluateStatic
  14. #define SCL_DEBUG_AI 0
  15. #include "../chess/smallchesslib.h"
  16. #define MAX_TEXT_LEN 30 // 30 = max length of text
  17. #define MAX_TEXT_BUF (MAX_TEXT_LEN + 1) // max length of text + null terminator
  18. struct FlipChessScene1 {
  19. View* view;
  20. FlipChessScene1Callback callback;
  21. void* context;
  22. };
  23. typedef struct {
  24. uint8_t paramPlayerW;
  25. uint8_t paramPlayerB;
  26. // uint8_t paramBoard = 1;
  27. uint8_t paramAnalyze; // depth of analysis
  28. uint8_t paramMoves;
  29. //uint8_t paramXboard = 0;
  30. uint8_t paramInfo;
  31. //uint8_t paramDraw = 1;
  32. uint8_t paramFlipBoard;
  33. //uint8_t paramHelp = 0;
  34. uint8_t paramExit;
  35. uint16_t paramStep;
  36. char* paramFEN;
  37. char* paramPGN;
  38. //uint16_t paramRandom = 0;
  39. //uint8_t paramBlind = 0;
  40. int clockSeconds;
  41. SCL_Game game;
  42. SCL_Board startState;
  43. int16_t random960PosNumber;
  44. //uint8_t picture[SCL_BOARD_PICTURE_WIDTH * SCL_BOARD_PICTURE_WIDTH];
  45. uint8_t squareSelected;
  46. uint8_t squareSelectedLast;
  47. char* msg;
  48. SCL_SquareSet squareSet;
  49. char moveString[16];
  50. SCL_SquareSet moveHighlight;
  51. uint8_t squareFrom;
  52. uint8_t squareTo;
  53. uint8_t turnState;
  54. } FlipChessScene1Model;
  55. uint8_t picture[SCL_BOARD_PICTURE_WIDTH * SCL_BOARD_PICTURE_WIDTH];
  56. void flipchess_putImagePixel(uint8_t pixel, uint16_t index) {
  57. picture[index] = pixel;
  58. }
  59. uint8_t flipchess_stringsEqual(const char* s1, const char* s2, int max) {
  60. for(int i = 0; i < max; ++i) {
  61. if(*s1 != *s2) return 0;
  62. if(*s1 == 0) return 1;
  63. s1++;
  64. s2++;
  65. }
  66. return 1;
  67. }
  68. int16_t flipchess_makeAIMove(
  69. SCL_Board board,
  70. uint8_t* s0,
  71. uint8_t* s1,
  72. char* prom,
  73. FlipChessScene1Model* model) {
  74. uint8_t level = SCL_boardWhitesTurn(board) ? model->paramPlayerW : model->paramPlayerB;
  75. uint8_t depth = (level > 0) ? level : 1;
  76. uint8_t extraDepth = 3;
  77. uint8_t endgameDepth = 1;
  78. uint8_t randomness =
  79. model->game.ply < 2 ? 1 : 0; /* in first moves increase randomness for different
  80. openings */
  81. uint8_t rs0, rs1;
  82. SCL_gameGetRepetiotionMove(&(model->game), &rs0, &rs1);
  83. if(model->clockSeconds >= 0) // when using clock, choose AI params accordingly
  84. {
  85. if(model->clockSeconds <= 5) {
  86. depth = 1;
  87. extraDepth = 2;
  88. endgameDepth = 0;
  89. } else if(model->clockSeconds < 15) {
  90. depth = 2;
  91. extraDepth = 2;
  92. } else if(model->clockSeconds < 100) {
  93. depth = 2;
  94. } else if(model->clockSeconds < 5 * 60) {
  95. depth = 3;
  96. } else {
  97. depth = 3;
  98. extraDepth = 4;
  99. }
  100. }
  101. return SCL_getAIMove(
  102. board,
  103. depth,
  104. extraDepth,
  105. endgameDepth,
  106. SCL_boardEvaluateStatic,
  107. SCL_randomBetter,
  108. randomness,
  109. rs0,
  110. rs1,
  111. s0,
  112. s1,
  113. prom);
  114. }
  115. void flipchess_drawBoard(FlipChessScene1Model* model) {
  116. SCL_drawBoard(
  117. model->game.board,
  118. flipchess_putImagePixel,
  119. model->squareSelected,
  120. model->moveHighlight,
  121. model->paramFlipBoard);
  122. }
  123. uint8_t flipchess_turn(FlipChessScene1Model* model) {
  124. // 0: none, 1: player, 2: AI, 3: undo
  125. uint8_t moveType = 0;
  126. model->msg = (SCL_boardWhitesTurn(model->game.board) ? "white to move" : "black to move");
  127. // if(paramInfo) {
  128. // //putchar('\n');
  129. // if(random960PosNumber >= 0)
  130. // printf("960 random position number: %d\n", random960PosNumber);
  131. // printf("ply number: %d\n", game.ply);
  132. // //SCL_boardToFEN(game.board, string);
  133. // //printf("FEN: %s\n", string);
  134. // int16_t eval = SCL_boardEvaluateStatic(game.board);
  135. // printf(
  136. // "board static evaluation: %lf (%d)\n",
  137. // ((double)eval) / ((double)SCL_VALUE_PAWN),
  138. // eval);
  139. // printf("board hash: %u\n", SCL_boardHash32(game.board));
  140. // printf("phase: ");
  141. // switch(SCL_boardEstimatePhase(game.board)) {
  142. // case SCL_PHASE_OPENING:
  143. // puts("opening");
  144. // break;
  145. // case SCL_PHASE_ENDGAME:
  146. // puts("endgame");
  147. // break;
  148. // default:
  149. // puts("midgame");
  150. // break;
  151. // }
  152. // printf(
  153. // "en passant: %d\n",
  154. // ((game.board[SCL_BOARD_ENPASSANT_CASTLE_BYTE] & 0x0f) + 1) % 16);
  155. // printf(
  156. // "50 move rule count: %d\n", game.board[SCL_BOARD_MOVE_COUNT_BYTE]);
  157. // // if(paramFEN == NULL && paramPGN == NULL) {
  158. // // //printf("PGN: ");
  159. // // //SCL_printPGN(game.record, putCharacter, startState);
  160. // // //putchar('\n');
  161. // // }
  162. // }
  163. if(model->game.state != SCL_GAME_STATE_PLAYING || model->paramExit)
  164. return FlipChessStatusReturn;
  165. char movePromote = 'q';
  166. if((SCL_boardWhitesTurn(model->game.board) && model->paramPlayerW == 0) ||
  167. (!SCL_boardWhitesTurn(model->game.board) && model->paramPlayerB == 0)) {
  168. // if(stringsEqual(string, "undo", 5))
  169. // moveType = 3;
  170. // else if(stringsEqual(string, "quit", 5))
  171. // break;
  172. if (model->turnState == 0 && model->squareSelected != 255) {
  173. model->squareFrom = model->squareSelected;
  174. model->turnState = 1;
  175. } else if (model->turnState == 1 && model->squareSelected != 255) {
  176. model->squareTo = model->squareSelected;
  177. model->turnState = 2;
  178. model->squareSelectedLast = model->squareSelected;
  179. model->squareSelected = 255;
  180. }
  181. if(model->turnState == 1 && model->squareFrom != 255) {
  182. if((model->game.board[model->squareFrom] != '.') &&
  183. (SCL_pieceIsWhite(model->game.board[model->squareFrom]) ==
  184. SCL_boardWhitesTurn(model->game.board))) {
  185. SCL_boardGetMoves(model->game.board, model->squareFrom, model->moveHighlight);
  186. }
  187. } else if (model->turnState == 2) {
  188. if(SCL_squareSetContains(model->moveHighlight, model->squareTo)) {
  189. moveType = 1;
  190. }
  191. model->turnState = 0;
  192. SCL_squareSetClear(model->moveHighlight);
  193. }
  194. // }
  195. } else {
  196. flipchess_makeAIMove(
  197. model->game.board, &(model->squareFrom), &(model->squareTo), &movePromote, model);
  198. moveType = 2;
  199. model->turnState = 0;
  200. }
  201. if(moveType == 1 || moveType == 2) {
  202. SCL_moveToString(
  203. model->game.board, model->squareFrom, model->squareTo, movePromote, model->moveString);
  204. SCL_gameMakeMove(&(model->game), model->squareFrom, model->squareTo, movePromote);
  205. SCL_squareSetClear(model->moveHighlight);
  206. SCL_squareSetAdd(model->moveHighlight, model->squareFrom);
  207. SCL_squareSetAdd(model->moveHighlight, model->squareTo);
  208. } else if(moveType == 3) {
  209. if(model->paramPlayerW != 0 || model->paramPlayerB != 0) SCL_gameUndoMove(&(model->game));
  210. SCL_gameUndoMove(&(model->game));
  211. SCL_squareSetClear(model->moveHighlight);
  212. }
  213. switch(model->game.state) {
  214. case SCL_GAME_STATE_WHITE_WIN:
  215. model->msg = "white wins";
  216. break;
  217. case SCL_GAME_STATE_BLACK_WIN:
  218. model->msg = "black wins";
  219. break;
  220. case SCL_GAME_STATE_DRAW_STALEMATE:
  221. model->msg = "draw (stalemate)";
  222. break;
  223. case SCL_GAME_STATE_DRAW_REPETITION:
  224. model->msg = "draw (repetition)";
  225. break;
  226. case SCL_GAME_STATE_DRAW_DEAD:
  227. model->msg = "draw (dead pos.)";
  228. break;
  229. case SCL_GAME_STATE_DRAW:
  230. model->msg = "draw";
  231. break;
  232. case SCL_GAME_STATE_DRAW_50:
  233. model->msg = "draw (50 moves)";
  234. break;
  235. default:
  236. if(model->game.ply > 0) {
  237. model->msg = (SCL_boardWhitesTurn(model->game.board) ? "black played" : "white played");
  238. // printf(" played ");
  239. uint8_t s0, s1;
  240. char p;
  241. SCL_recordGetMove(model->game.record, model->game.ply - 1, &s0, &s1, &p);
  242. SCL_moveToString(model->game.board, s0, s1, p, model->moveString);
  243. //model->msg = model->moveString;
  244. }
  245. break;
  246. }
  247. return moveType;
  248. }
  249. void flipchess_scene_1_set_callback(
  250. FlipChessScene1* instance,
  251. FlipChessScene1Callback callback,
  252. void* context) {
  253. furi_assert(instance);
  254. furi_assert(callback);
  255. instance->callback = callback;
  256. instance->context = context;
  257. }
  258. void flipchess_scene_1_draw(Canvas* canvas, FlipChessScene1Model* model) {
  259. //UNUSED(model);
  260. canvas_clear(canvas);
  261. canvas_set_color(canvas, ColorBlack);
  262. // Frame
  263. canvas_draw_frame(canvas, 0, 0, 128, 64);
  264. // Message
  265. canvas_set_font(canvas, FontSecondary);
  266. canvas_draw_str(canvas, 66, 10, model->msg);
  267. canvas_draw_str(canvas, 66, 20, model->moveString);
  268. // Board
  269. for(uint16_t y = 0; y < SCL_BOARD_PICTURE_WIDTH; y++) {
  270. for(uint16_t x = 0; x < SCL_BOARD_PICTURE_WIDTH; x++) {
  271. if(!picture[x + (y * SCL_BOARD_PICTURE_WIDTH)]) {
  272. canvas_draw_dot(canvas, x, y);
  273. }
  274. }
  275. }
  276. }
  277. static int flipchess_scene_1_model_init(
  278. FlipChessScene1Model* const model,
  279. const int white_mode,
  280. const int black_mode) {
  281. model->paramPlayerW = white_mode;
  282. model->paramPlayerB = black_mode;
  283. model->paramAnalyze = 255; // depth of analysis
  284. model->paramMoves = 0;
  285. model->paramInfo = 1;
  286. model->paramFlipBoard = 0;
  287. model->paramExit = 0;
  288. model->paramStep = 0;
  289. model->paramFEN = NULL;
  290. model->paramPGN = NULL;
  291. model->clockSeconds = -1;
  292. SCL_Board emptyStartState = SCL_BOARD_START_STATE;
  293. memcpy(model->startState, &emptyStartState, sizeof(SCL_Board));
  294. model->random960PosNumber = -1;
  295. model->squareSelected = 255;
  296. model->squareSelectedLast = 255;
  297. model->msg = "init";
  298. SCL_SquareSet emptySquareSet = SCL_SQUARE_SET_EMPTY;
  299. memcpy(model->moveHighlight, &emptySquareSet, sizeof(SCL_SquareSet));
  300. model->moveString[0] = '\0';
  301. model->squareFrom = 255;
  302. model->squareTo = 255;
  303. model->turnState = 0;
  304. furi_hal_random_init();
  305. SCL_randomBetterSeed(furi_hal_random_get());
  306. #if SCL_960_CASTLING
  307. if(model->random960PosNumber < 0) model->random960PosNumber = SCL_randomBetter();
  308. #endif
  309. if(model->random960PosNumber >= 0) model->random960PosNumber %= 960;
  310. if(model->paramFEN != NULL)
  311. SCL_boardFromFEN(model->startState, model->paramFEN);
  312. else if(model->paramPGN != NULL) {
  313. SCL_Record record;
  314. SCL_recordFromPGN(record, model->paramPGN);
  315. SCL_boardInit(model->startState);
  316. SCL_recordApply(record, model->startState, model->paramStep);
  317. }
  318. #if SCL_960_CASTLING
  319. else
  320. SCL_boardInit960(model->startState, model->random960PosNumber);
  321. #endif
  322. SCL_gameInit(&(model->game), model->startState);
  323. if(model->paramAnalyze != 255) {
  324. char p;
  325. uint8_t move[] = {0, 0};
  326. model->paramPlayerW = model->paramAnalyze;
  327. model->paramPlayerB = model->paramAnalyze;
  328. int16_t evaluation =
  329. flipchess_makeAIMove(model->game.board, &(move[0]), &(move[1]), &p, model);
  330. if(model->paramAnalyze == 0) evaluation = SCL_boardEvaluateStatic(model->game.board);
  331. char moveStr[5];
  332. moveStr[4] = 0;
  333. SCL_squareToString(move[0], moveStr);
  334. SCL_squareToString(move[1], moveStr + 2);
  335. //printf("%lf (%d)\n", ((double)evaluation) / ((double)SCL_VALUE_PAWN), evaluation);
  336. //puts(moveStr);
  337. return evaluation;
  338. }
  339. if(model->paramMoves) {
  340. char string[256];
  341. for(int i = 0; i < 64; ++i)
  342. if(model->game.board[i] != '.' &&
  343. SCL_pieceIsWhite(model->game.board[i]) == SCL_boardWhitesTurn(model->game.board)) {
  344. SCL_SquareSet possibleMoves = SCL_SQUARE_SET_EMPTY;
  345. SCL_boardGetMoves(model->game.board, i, possibleMoves);
  346. SCL_SQUARE_SET_ITERATE_BEGIN(possibleMoves)
  347. SCL_moveToString(model->game.board, i, iteratedSquare, 'q', string);
  348. //printf("%s ", string);
  349. SCL_SQUARE_SET_ITERATE_END
  350. }
  351. return FlipChessStatusReturn;
  352. }
  353. // model->msg = (SCL_boardWhitesTurn(model->game.board) ? "white to move" : "black to move");
  354. // 0 = success
  355. return FlipChessStatusSuccess;
  356. }
  357. bool flipchess_scene_1_input(InputEvent* event, void* context) {
  358. furi_assert(context);
  359. FlipChessScene1* instance = context;
  360. if(event->type == InputTypeRelease) {
  361. switch(event->key) {
  362. case InputKeyBack:
  363. with_view_model(
  364. instance->view,
  365. FlipChessScene1Model * model,
  366. {
  367. UNUSED(model);
  368. instance->callback(FlipChessCustomEventScene1Back, instance->context);
  369. },
  370. true);
  371. break;
  372. case InputKeyRight:
  373. with_view_model(
  374. instance->view,
  375. FlipChessScene1Model * model,
  376. {
  377. if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  378. model->squareSelected = model->squareSelectedLast;
  379. } else {
  380. model->squareSelected = (model->squareSelected + 1) % 64;
  381. }
  382. flipchess_drawBoard(model);
  383. },
  384. true);
  385. break;
  386. case InputKeyDown:
  387. with_view_model(
  388. instance->view,
  389. FlipChessScene1Model * model,
  390. {
  391. if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  392. model->squareSelected = model->squareSelectedLast;
  393. } else {
  394. model->squareSelected = (model->squareSelected + 56) % 64;
  395. }
  396. flipchess_drawBoard(model);
  397. },
  398. true);
  399. break;
  400. case InputKeyLeft:
  401. with_view_model(
  402. instance->view,
  403. FlipChessScene1Model * model,
  404. {
  405. if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  406. model->squareSelected = model->squareSelectedLast;
  407. } else {
  408. model->squareSelected = (model->squareSelected + 63) % 64;
  409. }
  410. flipchess_drawBoard(model);
  411. },
  412. true);
  413. break;
  414. case InputKeyUp:
  415. with_view_model(
  416. instance->view,
  417. FlipChessScene1Model * model,
  418. {if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  419. model->squareSelected = model->squareSelectedLast;
  420. } else {
  421. model->squareSelected = (model->squareSelected + 8) % 64;
  422. }
  423. flipchess_drawBoard(model);
  424. },
  425. true);
  426. break;
  427. case InputKeyOk:
  428. with_view_model(
  429. instance->view, FlipChessScene1Model * model,
  430. {
  431. const uint8_t turn = flipchess_turn(model);
  432. if(turn == FlipChessStatusReturn) {
  433. instance->callback(FlipChessCustomEventScene1Back, instance->context);
  434. } else {
  435. flipchess_drawBoard(model);
  436. }
  437. },
  438. true);
  439. break;
  440. case InputKeyMAX:
  441. break;
  442. }
  443. }
  444. return true;
  445. }
  446. void flipchess_scene_1_exit(void* context) {
  447. furi_assert(context);
  448. FlipChessScene1* instance = (FlipChessScene1*)context;
  449. with_view_model(
  450. instance->view, FlipChessScene1Model * model, { model->squareSelected = 255; }, true);
  451. }
  452. void flipchess_scene_1_enter(void* context) {
  453. furi_assert(context);
  454. FlipChessScene1* instance = (FlipChessScene1*)context;
  455. FlipChess* app = instance->context;
  456. flipchess_play_happy_bump(app);
  457. with_view_model(
  458. instance->view,
  459. FlipChessScene1Model * model,
  460. {
  461. int init =
  462. flipchess_scene_1_model_init(model, app->white_mode, app->black_mode);
  463. // nonzero status
  464. if(init == FlipChessStatusSuccess) {
  465. // perform initial turn
  466. const uint8_t turn = flipchess_turn(model);
  467. if(turn == FlipChessStatusReturn) {
  468. init = turn;
  469. } else {
  470. flipchess_drawBoard(model);
  471. }
  472. }
  473. // if return status, return from scene immediately
  474. if(init == FlipChessStatusReturn) {
  475. instance->callback(FlipChessCustomEventScene1Back, instance->context);
  476. }
  477. },
  478. true);
  479. }
  480. FlipChessScene1* flipchess_scene_1_alloc() {
  481. FlipChessScene1* instance = malloc(sizeof(FlipChessScene1));
  482. instance->view = view_alloc();
  483. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipChessScene1Model));
  484. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  485. view_set_draw_callback(instance->view, (ViewDrawCallback)flipchess_scene_1_draw);
  486. view_set_input_callback(instance->view, flipchess_scene_1_input);
  487. view_set_enter_callback(instance->view, flipchess_scene_1_enter);
  488. view_set_exit_callback(instance->view, flipchess_scene_1_exit);
  489. return instance;
  490. }
  491. void flipchess_scene_1_free(FlipChessScene1* instance) {
  492. furi_assert(instance);
  493. with_view_model(
  494. instance->view, FlipChessScene1Model * model, { UNUSED(model); }, true);
  495. view_free(instance->view);
  496. free(instance);
  497. }
  498. View* flipchess_scene_1_get_view(FlipChessScene1* instance) {
  499. furi_assert(instance);
  500. return instance->view;
  501. }