flipchess_scene_1.c 17 KB

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