flipchess_scene_1.c 17 KB

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