flipchess_scene_1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. //for(int i = 0; i < 40; ++i) putchar('\n');
  127. //putchar('\n');
  128. // if(model->game.ply > 0) {
  129. // model->msg = (SCL_boardWhitesTurn(model->game.board) ? "black played" : "white played");
  130. // // printf(" played ");
  131. // uint8_t s0, s1;
  132. // char p;
  133. // SCL_recordGetMove(model->game.record, model->game.ply - 1, &s0, &s1, &p);
  134. // SCL_moveToString(model->game.board, s0, s1, p, model->moveString);
  135. // model->msg = model->moveString;
  136. // //printf("%s\n", moveString);
  137. // }
  138. model->msg = (SCL_boardWhitesTurn(model->game.board) ? "white to move" : "black to move");
  139. //printf(" to move\n");
  140. // if(paramInfo) {
  141. // //putchar('\n');
  142. // if(random960PosNumber >= 0)
  143. // printf("960 random position number: %d\n", random960PosNumber);
  144. // printf("ply number: %d\n", game.ply);
  145. // //SCL_boardToFEN(game.board, string);
  146. // //printf("FEN: %s\n", string);
  147. // int16_t eval = SCL_boardEvaluateStatic(game.board);
  148. // printf(
  149. // "board static evaluation: %lf (%d)\n",
  150. // ((double)eval) / ((double)SCL_VALUE_PAWN),
  151. // eval);
  152. // printf("board hash: %u\n", SCL_boardHash32(game.board));
  153. // printf("phase: ");
  154. // switch(SCL_boardEstimatePhase(game.board)) {
  155. // case SCL_PHASE_OPENING:
  156. // puts("opening");
  157. // break;
  158. // case SCL_PHASE_ENDGAME:
  159. // puts("endgame");
  160. // break;
  161. // default:
  162. // puts("midgame");
  163. // break;
  164. // }
  165. // printf(
  166. // "en passant: %d\n",
  167. // ((game.board[SCL_BOARD_ENPASSANT_CASTLE_BYTE] & 0x0f) + 1) % 16);
  168. // printf(
  169. // "50 move rule count: %d\n", game.board[SCL_BOARD_MOVE_COUNT_BYTE]);
  170. // // if(paramFEN == NULL && paramPGN == NULL) {
  171. // // //printf("PGN: ");
  172. // // //SCL_printPGN(game.record, putCharacter, startState);
  173. // // //putchar('\n');
  174. // // }
  175. // }
  176. if(model->game.state != SCL_GAME_STATE_PLAYING || model->paramExit)
  177. return FlipChessStatusReturn;
  178. //uint8_t squareFrom = 0;
  179. //uint8_t squareTo = 0;
  180. char movePromote = 'q';
  181. if((SCL_boardWhitesTurn(model->game.board) && model->paramPlayerW == 0) ||
  182. (!SCL_boardWhitesTurn(model->game.board) && model->paramPlayerB == 0)) {
  183. // printf("\nmove: ");
  184. // scanf("%s", string);
  185. // char string[256];
  186. // if(stringsEqual(string, "undo", 5))
  187. // moveType = 3;
  188. // else if(stringsEqual(string, "quit", 5))
  189. // break;
  190. // else {
  191. //squareFrom = squareSelected; //SCL_stringToSquare(string);
  192. //squareTo = squareSelected; //SCL_stringToSquare(string + 2);
  193. //uint8_t r =
  194. // SCL_stringToMove(string, &squareFrom, &squareTo, &movePromote);
  195. if (model->turnState == 0 && model->squareSelected != 255) {
  196. model->squareFrom = model->squareSelected;
  197. model->turnState = 1;
  198. } else if (model->turnState == 1 && model->squareSelected != 255) {
  199. model->squareTo = model->squareSelected;
  200. model->turnState = 2;
  201. model->squareSelectedLast = model->squareSelected;
  202. model->squareSelected = 255;
  203. }
  204. if(model->turnState == 1 && model->squareFrom != 255) {
  205. if((model->game.board[model->squareFrom] != '.') &&
  206. (SCL_pieceIsWhite(model->game.board[model->squareFrom]) ==
  207. SCL_boardWhitesTurn(model->game.board))) {
  208. SCL_boardGetMoves(model->game.board, model->squareFrom, model->moveHighlight);
  209. }
  210. } else if (model->turnState == 2) {
  211. if(SCL_squareSetContains(model->moveHighlight, model->squareTo)) {
  212. moveType = 1;
  213. }
  214. model->turnState = 0;
  215. }
  216. // }
  217. } else {
  218. flipchess_makeAIMove(
  219. model->game.board, &(model->squareFrom), &(model->squareTo), &movePromote, model);
  220. moveType = 2;
  221. model->turnState = 0;
  222. }
  223. if(moveType == 1 || moveType == 2) {
  224. SCL_moveToString(
  225. model->game.board, model->squareFrom, model->squareTo, movePromote, model->moveString);
  226. SCL_gameMakeMove(&(model->game), model->squareFrom, model->squareTo, movePromote);
  227. SCL_squareSetClear(model->moveHighlight);
  228. SCL_squareSetAdd(model->moveHighlight, model->squareFrom);
  229. SCL_squareSetAdd(model->moveHighlight, model->squareTo);
  230. } else if(moveType == 3) {
  231. if(model->paramPlayerW != 0 || model->paramPlayerB != 0) SCL_gameUndoMove(&(model->game));
  232. SCL_gameUndoMove(&(model->game));
  233. SCL_squareSetClear(model->moveHighlight);
  234. }
  235. //putchar('\n');
  236. switch(model->game.state) {
  237. case SCL_GAME_STATE_WHITE_WIN:
  238. model->msg = "white wins";
  239. break;
  240. case SCL_GAME_STATE_BLACK_WIN:
  241. model->msg = "black wins";
  242. break;
  243. case SCL_GAME_STATE_DRAW_STALEMATE:
  244. model->msg = "draw (stalemate)";
  245. break;
  246. case SCL_GAME_STATE_DRAW_REPETITION:
  247. model->msg = "draw (repeated position)";
  248. break;
  249. case SCL_GAME_STATE_DRAW_DEAD:
  250. model->msg = "draw (dead position)";
  251. break;
  252. case SCL_GAME_STATE_DRAW:
  253. model->msg = "draw";
  254. break;
  255. case SCL_GAME_STATE_DRAW_50:
  256. model->msg = "draw (50 move rule)";
  257. break;
  258. default:
  259. if(model->game.ply > 0) {
  260. model->msg = (SCL_boardWhitesTurn(model->game.board) ? "black played" : "white played");
  261. // printf(" played ");
  262. uint8_t s0, s1;
  263. char p;
  264. SCL_recordGetMove(model->game.record, model->game.ply - 1, &s0, &s1, &p);
  265. SCL_moveToString(model->game.board, s0, s1, p, model->moveString);
  266. model->msg = model->moveString;
  267. //printf("%s\n", moveString);
  268. }
  269. //model->msg = "game over";
  270. break;
  271. }
  272. return moveType;
  273. }
  274. void flipchess_scene_1_set_callback(
  275. FlipChessScene1* instance,
  276. FlipChessScene1Callback callback,
  277. void* context) {
  278. furi_assert(instance);
  279. furi_assert(callback);
  280. instance->callback = callback;
  281. instance->context = context;
  282. }
  283. void flipchess_scene_1_draw(Canvas* canvas, FlipChessScene1Model* model) {
  284. //UNUSED(model);
  285. canvas_clear(canvas);
  286. canvas_set_color(canvas, ColorBlack);
  287. // Frame
  288. canvas_draw_frame(canvas, 0, 0, 128, 64);
  289. // Message
  290. canvas_set_font(canvas, FontSecondary);
  291. canvas_draw_str(canvas, 66, 10, model->msg);
  292. // Board
  293. for(uint16_t y = 0; y < SCL_BOARD_PICTURE_WIDTH; y++) {
  294. for(uint16_t x = 0; x < SCL_BOARD_PICTURE_WIDTH; x++) {
  295. if(!picture[x + (y * SCL_BOARD_PICTURE_WIDTH)]) {
  296. canvas_draw_dot(canvas, x, y);
  297. }
  298. }
  299. }
  300. }
  301. static int flipchess_scene_1_model_init(
  302. FlipChessScene1Model* const model,
  303. const int white_mode,
  304. const int black_mode) {
  305. model->paramPlayerW = white_mode;
  306. model->paramPlayerB = black_mode;
  307. model->paramAnalyze = 255; // depth of analysis
  308. model->paramMoves = 0;
  309. model->paramInfo = 1;
  310. model->paramFlipBoard = 0;
  311. model->paramExit = 0;
  312. model->paramStep = 0;
  313. model->paramFEN = NULL;
  314. model->paramPGN = NULL;
  315. model->clockSeconds = -1;
  316. SCL_Board emptyStartState = SCL_BOARD_START_STATE;
  317. memcpy(model->startState, &emptyStartState, sizeof(SCL_Board));
  318. model->random960PosNumber = -1;
  319. model->squareSelected = 255;
  320. model->squareSelectedLast = 255;
  321. model->msg = "init";
  322. SCL_SquareSet emptySquareSet = SCL_SQUARE_SET_EMPTY;
  323. memcpy(model->moveHighlight, &emptySquareSet, sizeof(SCL_SquareSet));
  324. model->moveString[0] = '\0';
  325. model->squareFrom = 255;
  326. model->squareTo = 255;
  327. model->turnState = 0;
  328. furi_hal_random_init();
  329. SCL_randomBetterSeed(furi_hal_random_get());
  330. #if SCL_960_CASTLING
  331. if(model->random960PosNumber < 0) model->random960PosNumber = SCL_randomBetter();
  332. #endif
  333. if(model->random960PosNumber >= 0) model->random960PosNumber %= 960;
  334. if(model->paramFEN != NULL)
  335. SCL_boardFromFEN(model->startState, model->paramFEN);
  336. else if(model->paramPGN != NULL) {
  337. SCL_Record record;
  338. SCL_recordFromPGN(record, model->paramPGN);
  339. SCL_boardInit(model->startState);
  340. SCL_recordApply(record, model->startState, model->paramStep);
  341. }
  342. #if SCL_960_CASTLING
  343. else
  344. SCL_boardInit960(model->startState, model->random960PosNumber);
  345. #endif
  346. SCL_gameInit(&(model->game), model->startState);
  347. if(model->paramAnalyze != 255) {
  348. char p;
  349. uint8_t move[] = {0, 0};
  350. model->paramPlayerW = model->paramAnalyze;
  351. model->paramPlayerB = model->paramAnalyze;
  352. int16_t evaluation =
  353. flipchess_makeAIMove(model->game.board, &(move[0]), &(move[1]), &p, model);
  354. if(model->paramAnalyze == 0) evaluation = SCL_boardEvaluateStatic(model->game.board);
  355. char moveStr[5];
  356. moveStr[4] = 0;
  357. SCL_squareToString(move[0], moveStr);
  358. SCL_squareToString(move[1], moveStr + 2);
  359. //printf("%lf (%d)\n", ((double)evaluation) / ((double)SCL_VALUE_PAWN), evaluation);
  360. //puts(moveStr);
  361. return evaluation;
  362. }
  363. if(model->paramMoves) {
  364. char string[256];
  365. for(int i = 0; i < 64; ++i)
  366. if(model->game.board[i] != '.' &&
  367. SCL_pieceIsWhite(model->game.board[i]) == SCL_boardWhitesTurn(model->game.board)) {
  368. SCL_SquareSet possibleMoves = SCL_SQUARE_SET_EMPTY;
  369. SCL_boardGetMoves(model->game.board, i, possibleMoves);
  370. SCL_SQUARE_SET_ITERATE_BEGIN(possibleMoves)
  371. SCL_moveToString(model->game.board, i, iteratedSquare, 'q', string);
  372. //printf("%s ", string);
  373. SCL_SQUARE_SET_ITERATE_END
  374. }
  375. return FlipChessStatusReturn;
  376. }
  377. model->msg = (SCL_boardWhitesTurn(model->game.board) ? "white to move" : "black to move");
  378. // 0 = success
  379. return FlipChessStatusSuccess;
  380. }
  381. bool flipchess_scene_1_input(InputEvent* event, void* context) {
  382. furi_assert(context);
  383. FlipChessScene1* instance = context;
  384. if(event->type == InputTypeRelease) {
  385. switch(event->key) {
  386. case InputKeyBack:
  387. with_view_model(
  388. instance->view,
  389. FlipChessScene1Model * model,
  390. {
  391. UNUSED(model);
  392. instance->callback(FlipChessCustomEventScene1Back, instance->context);
  393. },
  394. true);
  395. break;
  396. case InputKeyRight:
  397. with_view_model(
  398. instance->view,
  399. FlipChessScene1Model * model,
  400. {
  401. if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  402. model->squareSelected = model->squareSelectedLast;
  403. } else {
  404. model->squareSelected = (model->squareSelected + 1) % 64;
  405. }
  406. flipchess_drawBoard(model);
  407. },
  408. true);
  409. break;
  410. case InputKeyDown:
  411. with_view_model(
  412. instance->view,
  413. FlipChessScene1Model * model,
  414. {
  415. if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  416. model->squareSelected = model->squareSelectedLast;
  417. } else {
  418. model->squareSelected = (model->squareSelected + 56) % 64;
  419. }
  420. flipchess_drawBoard(model);
  421. },
  422. true);
  423. break;
  424. case InputKeyLeft:
  425. with_view_model(
  426. instance->view,
  427. FlipChessScene1Model * model,
  428. {
  429. if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  430. model->squareSelected = model->squareSelectedLast;
  431. } else {
  432. model->squareSelected = (model->squareSelected + 63) % 64;
  433. }
  434. flipchess_drawBoard(model);
  435. },
  436. true);
  437. break;
  438. case InputKeyUp:
  439. with_view_model(
  440. instance->view,
  441. FlipChessScene1Model * model,
  442. {if (model->squareSelectedLast != 255 && model->squareSelected == 255) {
  443. model->squareSelected = model->squareSelectedLast;
  444. } else {
  445. model->squareSelected = (model->squareSelected + 8) % 64;
  446. }
  447. flipchess_drawBoard(model);
  448. },
  449. true);
  450. break;
  451. case InputKeyOk:
  452. with_view_model(
  453. instance->view, FlipChessScene1Model * model,
  454. {
  455. uint8_t status = flipchess_turn(model);
  456. if(status == FlipChessStatusReturn) {
  457. instance->callback(FlipChessCustomEventScene1Back, instance->context);
  458. } else {
  459. flipchess_drawBoard(model);
  460. }
  461. },
  462. true);
  463. break;
  464. case InputKeyMAX:
  465. break;
  466. }
  467. }
  468. return true;
  469. }
  470. void flipchess_scene_1_exit(void* context) {
  471. furi_assert(context);
  472. FlipChessScene1* instance = (FlipChessScene1*)context;
  473. with_view_model(
  474. instance->view, FlipChessScene1Model * model, { model->squareSelected = 255; }, true);
  475. }
  476. void flipchess_scene_1_enter(void* context) {
  477. furi_assert(context);
  478. FlipChessScene1* instance = (FlipChessScene1*)context;
  479. FlipChess* app = instance->context;
  480. flipchess_play_happy_bump(app);
  481. with_view_model(
  482. instance->view,
  483. FlipChessScene1Model * model,
  484. {
  485. const int status =
  486. flipchess_scene_1_model_init(model, app->white_mode, app->black_mode);
  487. // nonzero status
  488. if(status == FlipChessStatusSuccess) {
  489. flipchess_drawBoard(model);
  490. } else {
  491. }
  492. // if return status, return from scene immediately
  493. if(status == FlipChessStatusReturn) {
  494. instance->callback(FlipChessCustomEventScene1Back, instance->context);
  495. }
  496. },
  497. true);
  498. }
  499. FlipChessScene1* flipchess_scene_1_alloc() {
  500. FlipChessScene1* instance = malloc(sizeof(FlipChessScene1));
  501. instance->view = view_alloc();
  502. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipChessScene1Model));
  503. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  504. view_set_draw_callback(instance->view, (ViewDrawCallback)flipchess_scene_1_draw);
  505. view_set_input_callback(instance->view, flipchess_scene_1_input);
  506. view_set_enter_callback(instance->view, flipchess_scene_1_enter);
  507. view_set_exit_callback(instance->view, flipchess_scene_1_exit);
  508. return instance;
  509. }
  510. void flipchess_scene_1_free(FlipChessScene1* instance) {
  511. furi_assert(instance);
  512. with_view_model(
  513. instance->view, FlipChessScene1Model * model, { UNUSED(model); }, true);
  514. view_free(instance->view);
  515. free(instance);
  516. }
  517. View* flipchess_scene_1_get_view(FlipChessScene1* instance) {
  518. furi_assert(instance);
  519. return instance->view;
  520. }