juinda

Word game

Jun 14th, 2017
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. //Project : Word game
  2. //Create : ZZY
  3. //Date : 17.5.2
  4. //E-mail : [email protected]
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. #include <map>
  9. #include <string>
  10.  
  11. #define GAMEBOARDSIZE 4
  12.  
  13. using namespace std;
  14.  
  15. class WordGame {
  16. public:
  17. WordGame() :gameBoard(nullptr) {}
  18. ~WordGame();
  19. void init();
  20. void start();
  21. private:
  22. char** gameBoard;
  23. fstream words;
  24. map<char, int> gameBoardCount;
  25. bool canBeFormed(string word);
  26. bool canBeFormed(int row, int col, string word, map<pair<int, int>, bool> used);
  27. };
  28.  
  29. int main(void)
  30. {
  31. WordGame game;
  32. game.init();
  33. game.start();
  34. return 0;
  35. }
  36.  
  37. //destory the object
  38. WordGame::~WordGame()
  39. {
  40. if (words.is_open())
  41. words.close();
  42. if (gameBoard != nullptr)
  43. {
  44. for (int index = 0; index < GAMEBOARDSIZE; ++index)
  45. {
  46. if (gameBoard[index] != nullptr)
  47. {
  48. delete[] gameBoard[index];
  49. gameBoard[index] = nullptr;
  50. }
  51. }
  52. delete[] gameBoard;
  53. gameBoard = nullptr;
  54. }
  55. }
  56.  
  57. void WordGame::init()
  58. {
  59. //init
  60. gameBoard = new char*[GAMEBOARDSIZE];
  61. for (int init = 0; init < GAMEBOARDSIZE; ++init)
  62. gameBoard[init] = new char[GAMEBOARDSIZE];
  63. for (char word = 'a'; word <= 'z'; word++)
  64. gameBoardCount[word] = 0;
  65.  
  66. //open words.txt
  67. words.open("words.txt", ios::in);
  68.  
  69. //Get the gameBoard infomation
  70. char temp;
  71. for (int row = 0; row < GAMEBOARDSIZE; ++row)
  72. {
  73. for (int col = 0; col < GAMEBOARDSIZE;)
  74. {
  75. cin >> temp;
  76. if (temp >= 'a' && temp <= 'z')
  77. {
  78. gameBoard[row][col++] = temp;
  79. gameBoardCount[temp]++;
  80. }
  81. }
  82. }
  83. }
  84.  
  85. //start to find the word
  86. void WordGame::start()
  87. {
  88. string word;
  89. while (words >> word)
  90. {
  91. bool needCheck = true;
  92. map<char, int> count;
  93. for (char& check : word)
  94. {
  95. count[check]++;
  96. if (count[check] > gameBoardCount[check])
  97. {
  98. needCheck = false;
  99. break;
  100. }
  101. }
  102. if (needCheck)
  103. {
  104. if (canBeFormed(word))
  105. cout << word << endl;
  106. }
  107. }
  108. }
  109.  
  110. //init for check the word
  111. bool WordGame::canBeFormed(string word)
  112. {
  113. if (word.size() > 0)
  114. {
  115. map<pair<int, int>, bool> used;
  116. for (int row = 0; row < GAMEBOARDSIZE; ++row)
  117. {
  118. for (int col = 00; col < GAMEBOARDSIZE; ++col)
  119. {
  120. if (word[0] == gameBoard[row][col])
  121. {
  122. if (canBeFormed(row, col, word, used))
  123. return true;
  124. }
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130.  
  131. //recursive check the word
  132. bool WordGame::canBeFormed(int row, int col, string word, map<pair<int, int>, bool> used)
  133. {
  134. static int rowMove[8] = { 0,1,1,1,0,-1,-1,-1 };
  135. static int colMove[8] = { 1,1,0,-1,-1,-1,0,1 };
  136. if (word.size() > 0)
  137. {
  138. if (row < 0 || row >= GAMEBOARDSIZE || col < 0 || col >= GAMEBOARDSIZE)
  139. return false;
  140. if (used[pair<int, int>(row, col)] || word[0] != gameBoard[row][col])
  141. return false;
  142. used[pair<int, int>(row, col)] = true;
  143. string temp = word.substr(1, word.size());
  144. for (int check = 0; check < 8; check++)
  145. {
  146. if (canBeFormed(row + rowMove[check], col + colMove[check], temp, used))
  147. return true;
  148. }
  149. return false;
  150. }
  151. return true;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment