Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #define _WIN32_WINNT 0x0500
  4. #include<windows.h>
  5. using namespace std;
  6. bool OX = 1;
  7. char grid[105][105];
  8. void Show_ascii_map(char grid[105][105])
  9. {
  10. COORD coord;
  11. coord.X = 0;
  12. coord.Y = 0;
  13. SetConsoleCursorPosition(GetStdHandle( STD_OUTPUT_HANDLE ), coord);
  14. Sleep(20);
  15. for(int i = 40; i <= 60; i++){
  16. for(int j = 40; j <= 60; j++)
  17. cout << grid[i][j] << " ";
  18. cout << "\n";
  19. }
  20. }
  21. void Create_ascii_map(char grid[][105])
  22. {
  23. for(int i = 0; i <= 100; i++)
  24. for(int j = 0; j <= 100; j++)
  25. grid[i][j] = '_';
  26. }
  27. POINT PointGridTransform(POINT Cursor)
  28. {
  29. POINT v = Cursor;
  30. v.x = Cursor.x / 16;
  31. v.y = Cursor.y / 14;
  32. return v;
  33. }
  34. bool isValid(POINT Cursor)
  35. {
  36. if((Cursor.y < 0 || Cursor.x < 0) || (Cursor.y > 20 || Cursor.x > 20))
  37. return 0;
  38. return 1;
  39. }
  40. void CheckWinCondition(int posX, int posY, char grid[105][105])
  41. {
  42. int valX[] = {0, -1, 0, 1, 1, -1, 1, -1};
  43. int valY[] = {1, 0, -1, 0, 1, -1, -1, 1};
  44. for(int i = 0; i < 8; i++)
  45. {
  46. int lineVerifier = 0;
  47. int cx = posX, cy = posY;
  48. if(OX == 1){
  49. for(int j = 0; j < 4; j++)
  50. {
  51.  
  52. cx += valX[i];
  53. cy += valY[i];
  54. if(grid[cy][cx] != 'X'){
  55. lineVerifier = 1;
  56. break;
  57. }
  58. }
  59. if(lineVerifier == 0)
  60. {
  61. system("CLS");
  62. cout << "X Wins";
  63. Sleep(2000);
  64. exit(1);
  65. }
  66. int cx = posX, cy = posY;
  67. lineVerifier = 0;
  68. if(grid[posY][posX] == '?'){
  69. for(int j = 0; j < 4; j++)
  70. {
  71. cx += valX[i];
  72. cy += valY[i];
  73. if(grid[cy][cx] != '?'){
  74. lineVerifier = 1;
  75. break;
  76. }
  77. }
  78. if(lineVerifier == 0)
  79. {
  80. system("CLS");
  81. cout << "X Wins";
  82. Sleep(2000);
  83. exit(1);
  84. }
  85. }
  86. }
  87. else
  88. {
  89. lineVerifier = 0;
  90. for(int j = 0; j < 4; j++)
  91. {
  92. cx += valX[i];
  93. cy += valY[i];
  94. if(grid[cy][cx] != 'O'){
  95. lineVerifier = 1;
  96. break;
  97. }
  98. }
  99. if(lineVerifier == 0)
  100. {
  101. system("CLS");
  102. cout << "O Wins";
  103. Sleep(2000);
  104. exit(1);
  105. }
  106. int cx = posX, cy = posY;
  107. lineVerifier = 0;
  108. if(grid[posY][posX] == '?'){
  109. for(int j = 0; j < 4; j++)
  110. {
  111. cx += valX[i];
  112. cy += valY[i];
  113. if(grid[cy][cx] != '?'){
  114. lineVerifier = 1;
  115. break;
  116. }
  117. }
  118. if(lineVerifier == 0)
  119. {
  120. system("CLS");
  121. cout << "O Wins";
  122. Sleep(2000);
  123. exit(1);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. void PlayerMove(POINT gridCoord, bool is1Pressed, bool is2Pressed)
  130. {
  131. POINT Coords = PointGridTransform(gridCoord);
  132. if(is1Pressed == true && grid[Coords.y + 40][Coords.x + 40] == '_')
  133. {
  134. if(OX == 0){
  135. grid[Coords.y + 40][Coords.x + 40] = 'O';
  136. }
  137. else{
  138. grid[Coords.y + 40][Coords.x + 40] = 'X';
  139. }
  140. CheckWinCondition(Coords.x + 40, Coords.y + 40, grid);
  141. OX = 1 - OX;
  142. }
  143. if(is2Pressed == true && grid[Coords.y + 40][Coords.x + 40] == '_')
  144. {
  145. grid[Coords.y + 40][Coords.x + 40] = '?';
  146. CheckWinCondition(Coords.x + 40, Coords.y + 40, grid);
  147. OX = 1 - OX;
  148. }
  149. }
  150.  
  151. bool isMouse1Pressed()
  152. {
  153. if((GetKeyState(VK_LBUTTON) & 0x100) != 0)
  154. {
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. bool isMouse2Pressed()
  160. {
  161. if((GetKeyState(VK_RBUTTON) & 0x100) != 0)
  162. {
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. void EnableGame(char grid[][105])
  168. {
  169. POINT p;
  170. HWND windowConsole = GetConsoleWindow();
  171. RECT windowApiDimensions;
  172. GetWindowRect(windowConsole, &windowApiDimensions);
  173. GetCursorPos(&p);
  174. ScreenToClient(windowConsole, &p);
  175. PlayerMove(p, isMouse1Pressed(), isMouse2Pressed());
  176. }
  177. int main()
  178. {
  179. Create_ascii_map(grid);
  180. while(1)
  181. {
  182. EnableGame(grid);
  183. Show_ascii_map(grid);
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement