GridPositionTool.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // GridPositionTool.cpp
  3. // auto_fill_jewel_v3
  4. //
  5. // Created by Red on 2024/12/4.
  6. //
  7. #include "GridPositionTool.hpp"
  8. #include <utility>
  9. #include <iostream>
  10. using std::pair ;
  11. #include <cassert>
  12. using std::cout;
  13. using std::endl;
  14. #define GRIDPOSITIONTOOL_BIG_GRID_NCOLS 3
  15. #define GRIDPOSITIONTOOL_BIG_GRID_NROWS 3
  16. #define GRIDPOSITIONTOOL_GAME_WIDTH 640
  17. #define GRIDPOSITIONTOOL_GAME_HEIGHT 720
  18. #define GRIDPOSITIONTOOL_LG_W 210
  19. #define GRIDPOSITIONTOOL_LG_H 270
  20. #define GRIDPOSITIONTOOL_MD_W 210
  21. #define GRIDPOSITIONTOOL_MD_H 135
  22. #define GRIDPOSITIONTOOL_SM_W 105
  23. #define GRIDPOSITIONTOOL_SM_H 135
  24. void GridPositionTool::solve(const bool movable,
  25. const string firstMoveTypeCode,
  26. const string secondMoveTypeCode,
  27. const vector<int>& plateTypeIdArr ,
  28. const vector<int>& moveIdArr ,
  29. vector<vector<int>>& resultPositions )
  30. {
  31. resultPositions.resize(plateTypeIdArr.size()) ;
  32. string moveTypeCode = "" ;//no movement.
  33. if( movable ==true ) {
  34. moveTypeCode = firstMoveTypeCode + secondMoveTypeCode ;// A B C BA CB CA
  35. }
  36. auto fgc = FillGlobalConfig::getInstance() ;
  37. //先安排第一行移动大盘子
  38. for(int iplate = 0 ; iplate < plateTypeIdArr.size();++ iplate ) {
  39. int plateTypeId = plateTypeIdArr[iplate] ;
  40. FillGlobalConfig::PlateItem* platePtr = fgc->getPlateItemById(plateTypeId) ;
  41. int moveid = moveIdArr[iplate] ;// 0-nomove, 1-firstmove , 2-secondmove
  42. int plateSize = platePtr->_size ;
  43. if( moveid==1 && plateSize == FILLGLOBALCONFIG_PLATESIZE_LG) {
  44. ContourData::Point lowerLeftPosition ;
  45. FillGlobalConfig::GridBoxCell* gbcPtr = fgc->getLowestUnfilledGridBox(moveTypeCode, moveid, plateSize, lowerLeftPosition);
  46. assert( gbcPtr != nullptr ) ;
  47. fgc->setFilled(gbcPtr, plateSize) ;
  48. resultPositions[iplate] = {(int)lowerLeftPosition.x, (int)lowerLeftPosition.y} ;
  49. }
  50. }
  51. //安排第一行移动中盘子
  52. for(int iplate = 0 ; iplate < plateTypeIdArr.size();++ iplate ) {
  53. int plateTypeId = plateTypeIdArr[iplate] ;
  54. FillGlobalConfig::PlateItem* platePtr = fgc->getPlateItemById(plateTypeId) ;
  55. int moveid = moveIdArr[iplate] ;// 0-nomove, 1-firstmove , 2-secondmove
  56. int plateSize = platePtr->_size ;
  57. if( moveid==1 && plateSize == FILLGLOBALCONFIG_PLATESIZE_MD) {
  58. ContourData::Point lowerLeftPosition ;
  59. FillGlobalConfig::GridBoxCell* gbcPtr = fgc->getLowestUnfilledGridBox(moveTypeCode, moveid, plateSize, lowerLeftPosition);
  60. assert( gbcPtr != nullptr ) ;
  61. fgc->setFilled(gbcPtr, plateSize) ;
  62. resultPositions[iplate] = {(int)lowerLeftPosition.x, (int)lowerLeftPosition.y} ;
  63. }
  64. }
  65. //先安排第二行移动大盘子
  66. for(int iplate = 0 ; iplate < plateTypeIdArr.size();++ iplate ) {
  67. int plateTypeId = plateTypeIdArr[iplate] ;
  68. FillGlobalConfig::PlateItem* platePtr = fgc->getPlateItemById(plateTypeId) ;
  69. int moveid = moveIdArr[iplate] ;// 0-nomove, 1-firstmove , 2-secondmove
  70. int plateSize = platePtr->_size ;
  71. if( moveid==2 && plateSize == FILLGLOBALCONFIG_PLATESIZE_LG) {
  72. ContourData::Point lowerLeftPosition ;
  73. FillGlobalConfig::GridBoxCell* gbcPtr = fgc->getLowestUnfilledGridBox(moveTypeCode, moveid, plateSize, lowerLeftPosition);
  74. assert( gbcPtr != nullptr ) ;
  75. fgc->setFilled(gbcPtr, plateSize) ;
  76. resultPositions[iplate] = {(int)lowerLeftPosition.x, (int)lowerLeftPosition.y} ;
  77. }
  78. }
  79. //安排第二行移动中盘子
  80. for(int iplate = 0 ; iplate < plateTypeIdArr.size();++ iplate ) {
  81. int plateTypeId = plateTypeIdArr[iplate] ;
  82. FillGlobalConfig::PlateItem* platePtr = fgc->getPlateItemById(plateTypeId) ;
  83. int moveid = moveIdArr[iplate] ;// 0-nomove, 1-firstmove , 2-secondmove
  84. int plateSize = platePtr->_size ;
  85. if( moveid==2 && plateSize == FILLGLOBALCONFIG_PLATESIZE_MD) {
  86. ContourData::Point lowerLeftPosition ;
  87. FillGlobalConfig::GridBoxCell* gbcPtr = fgc->getLowestUnfilledGridBox(moveTypeCode, moveid, plateSize, lowerLeftPosition);
  88. assert( gbcPtr != nullptr ) ;
  89. fgc->setFilled(gbcPtr, plateSize) ;
  90. resultPositions[iplate] = {(int)lowerLeftPosition.x, (int)lowerLeftPosition.y} ;
  91. }
  92. }
  93. //安排不移动大盘子
  94. for(int iplate = 0 ; iplate < plateTypeIdArr.size();++ iplate ) {
  95. int plateTypeId = plateTypeIdArr[iplate] ;
  96. FillGlobalConfig::PlateItem* platePtr = fgc->getPlateItemById(plateTypeId) ;
  97. int moveid = moveIdArr[iplate] ;// 0-nomove, 1-firstmove , 2-secondmove
  98. int plateSize = platePtr->_size ;
  99. if( moveid==0 && plateSize == FILLGLOBALCONFIG_PLATESIZE_LG) {
  100. ContourData::Point lowerLeftPosition ;
  101. FillGlobalConfig::GridBoxCell* gbcPtr = fgc->getLowestUnfilledGridBox(moveTypeCode, moveid, plateSize, lowerLeftPosition);
  102. assert( gbcPtr != nullptr ) ;
  103. fgc->setFilled(gbcPtr, plateSize) ;
  104. resultPositions[iplate] = {(int)lowerLeftPosition.x, (int)lowerLeftPosition.y} ;
  105. }
  106. }
  107. //安排不移动中盘子
  108. for(int iplate = 0 ; iplate < plateTypeIdArr.size();++ iplate ) {
  109. int plateTypeId = plateTypeIdArr[iplate] ;
  110. FillGlobalConfig::PlateItem* platePtr = fgc->getPlateItemById(plateTypeId) ;
  111. int moveid = moveIdArr[iplate] ;// 0-nomove, 1-firstmove , 2-secondmove
  112. int plateSize = platePtr->_size ;
  113. if( moveid==0 && plateSize == FILLGLOBALCONFIG_PLATESIZE_MD) {
  114. ContourData::Point lowerLeftPosition ;
  115. FillGlobalConfig::GridBoxCell* gbcPtr = fgc->getLowestUnfilledGridBox(moveTypeCode, moveid, plateSize, lowerLeftPosition);
  116. assert( gbcPtr != nullptr ) ;
  117. fgc->setFilled(gbcPtr, plateSize) ;
  118. resultPositions[iplate] = {(int)lowerLeftPosition.x, (int)lowerLeftPosition.y} ;
  119. }
  120. }
  121. }
  122. bool GridPositionTool::getIndexPositionInGrid( const vector<vector<int>>& gridRows, const int index, int& col,int& row)
  123. {
  124. col = -1 ;
  125. row = -1 ;
  126. for(int irow = 0 ; irow < gridRows.size();++irow ) {
  127. for(int icol=0; icol < gridRows[irow].size(); ++ icol ) {
  128. if( gridRows[irow][icol] == index ) {
  129. col = icol ;
  130. row = irow ;
  131. return true ;
  132. }
  133. }
  134. }
  135. std::cout<<"error: could not found index from grid. "<<index<<std::endl;
  136. return false;
  137. }
  138. vector<int> GridPositionTool::computePositionByGridColRow(int plateSize, int col, int row )
  139. {
  140. if( plateSize == FILLGLOBALCONFIG_PLATESIZE_LG ) {
  141. //将左上角原点的网格坐标换算成左下角原点网格坐标
  142. row = GRIDPOSITIONTOOL_BIG_GRID_NROWS - 1 - row ;
  143. int x0 = 0 ;
  144. int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS*GRIDPOSITIONTOOL_LG_H/2;
  145. int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / GRIDPOSITIONTOOL_BIG_GRID_NCOLS ;
  146. return {
  147. (int)(x0+(col+0.5)*widthAndMargin ) ,
  148. (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_LG_H) } ;
  149. }else if( plateSize==FILLGLOBALCONFIG_PLATESIZE_MD ) {
  150. //将左上角原点的网格坐标换算成左下角原点网格坐标
  151. row = GRIDPOSITIONTOOL_BIG_GRID_NROWS*2 - 1 - row ;
  152. int x0 = 0 ;
  153. int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS * 2 * GRIDPOSITIONTOOL_MD_H/2;
  154. int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / GRIDPOSITIONTOOL_BIG_GRID_NCOLS ;
  155. return {
  156. (int)(x0+(col+0.5)*widthAndMargin ) ,
  157. (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_MD_H) } ;
  158. }else { // FILLGLOBALCONFIG_JEWELSIZE_SM
  159. //将左上角原点的网格坐标换算成左下角原点网格坐标
  160. row = GRIDPOSITIONTOOL_BIG_GRID_NROWS*2 - 1 - row ;
  161. int x0 = 0 ;
  162. int y0 = GRIDPOSITIONTOOL_GAME_HEIGHT/2 - GRIDPOSITIONTOOL_BIG_GRID_NROWS * 2 * GRIDPOSITIONTOOL_SM_H/2;
  163. int widthAndMargin = GRIDPOSITIONTOOL_GAME_WIDTH / (GRIDPOSITIONTOOL_BIG_GRID_NCOLS*2) ;
  164. return {
  165. (int)(x0+(col+0.5)*widthAndMargin ) ,
  166. (int)(y0+(row+0.5)*GRIDPOSITIONTOOL_SM_H) } ;
  167. }
  168. }