Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. # include "Board.h"
  2. using namespace std;
  3.  
  4. Board::Board()
  5. {
  6. width = 7;
  7. length = 6;
  8.  
  9. p_board = new char* [width];
  10. for (int i = 0; i < width; i++)
  11. {
  12. p_board[i] = new char[length];
  13. }
  14.  
  15. for (int i = 0; i < length; i++)
  16. {
  17. for (int j = 0; j <width ; j++)
  18. {
  19. p_board[i][j] = ' ';
  20. }
  21. }
  22.  
  23. }
  24.  
  25. Board::Board(int length_, int width_)
  26. {
  27. if (length_ < 7 || width_ < 6) //check if length < 7 and height < 6
  28. {
  29. width = 7;
  30. length = 6;
  31.  
  32. p_board = new char*[width];
  33. for (int i = 0; i < width; i++)
  34. {
  35. p_board[i] = new char[length];
  36. }
  37.  
  38. for (int i = 0; i <length ; i++)
  39. {
  40. for (int j = 0; j <width; j++)
  41. {
  42. p_board[i][j] = ' ';
  43. }
  44. }
  45. }
  46. else
  47. {
  48. length = length_;
  49. width = width_;
  50.  
  51. p_board = new char*[width];
  52. for (int i = 0; i < width; i++)
  53. {
  54. p_board[i] = new char[length];
  55. }
  56.  
  57. for (int i = 0; i <length; i++)
  58. {
  59. for (int j = 0; j <width ; j++)
  60. {
  61. p_board[i][j] = ' ';
  62. }
  63. }
  64. }
  65. }
  66.  
  67. Board::~Board()
  68. {
  69. for (int i = 0; i < width; i++)
  70. {
  71. delete[] p_board[i];
  72. }
  73. delete [] p_board;
  74. }
  75.  
  76. /*void Board::print() const
  77. {
  78. int j = 1;
  79. for (int i = 0; i < width; i++)
  80. {
  81. cout << j<<" |";
  82. for (int j = 0; j < length ; j++)
  83. {
  84. //cout << "____";
  85. cout << p_board[i][j];
  86. }
  87. j++;
  88. }
  89.  
  90. }
  91. */
  92. void Board::PlacePlayer(const int col, int actor)
  93. {
  94. for (int i = 0; i < length; i--)
  95. {
  96. if (p_board[col][i] == ' ')
  97. {
  98. p_board[col][i] = actor;
  99. break;
  100. }
  101. }
  102.  
  103.  
  104. }
  105.  
  106. int Board::Winner(const int col, int row, char actor)
  107. {
  108. int i = col, j = row, count=1;
  109.  
  110. for (int k = 0; k < 3; k++) //cheak row +
  111. {
  112. if (p_board[i][j++] == actor)
  113. {
  114. count++;
  115. j++;
  116. }
  117. else
  118. break;
  119. }
  120.  
  121. if (count == 4) //cheak if count =4
  122. {
  123. return 0; // "win" ==0
  124. }
  125.  
  126. i = col, j = row;
  127.  
  128. for (int k = 0; k < 3; k++)//cheak row --
  129. {
  130. if (p_board[i][j--] == actor)
  131. {
  132. count++;
  133. j--;
  134. }
  135. else
  136. break;
  137. }
  138.  
  139. if (count == 4)//cheak if count =4
  140. {
  141. return 0; // "win" == 0
  142. }
  143.  
  144. //////////////////////////////////////////////col
  145.  
  146. i = col, j = row, count = 1;
  147.  
  148. for (int k = 0; k < 3; k++) //cheak col +
  149. {
  150. if (p_board[i++][j] == actor)
  151. {
  152. count++;
  153. i++;
  154. }
  155. else
  156. break;
  157. }
  158.  
  159. if (count == 4) //cheak if count =4
  160. {
  161. return 0; // "win" ==0
  162. }
  163.  
  164. i = col, j = row;
  165.  
  166. for (int k = 0; k < 3; k++)//cheak col --
  167. {
  168. if (p_board[i--][j] == actor)
  169. {
  170. count++;
  171. i--;
  172. }
  173. else
  174. break;
  175. }
  176. if (count == 4)//cheak if count =4
  177. {
  178. return 0; // "win" == 0
  179. }
  180.  
  181. ////////////////////////////////////////////// ----->>>>
  182.  
  183. i = col, j = row, count = 1;
  184.  
  185. for (int k = 0; k < 3; k++) //cheak
  186. {
  187. if (p_board[i--][j++] == actor)
  188. {
  189. count++;
  190. i--;
  191. j++;
  192. }
  193. else
  194. break;
  195. }
  196.  
  197. if (count == 4) //cheak if count =4
  198. {
  199. return 0; // "win" ==0
  200. }
  201.  
  202. i = col, j = row;
  203.  
  204. for (int k = 0; k < 3; k++)//cheak row --
  205. {
  206. if (p_board[i++][j--] == actor)
  207. {
  208. count++;
  209. i++;
  210. j--;
  211. }
  212. else
  213. break;
  214. }
  215. if (count == 4)//cheak if count =4
  216. {
  217. return 0; // "win" == 0
  218. }
  219.  
  220. /////////////////////////////////////////////cheak <<<-------
  221.  
  222. i = col, j = row, count = 1;
  223.  
  224. for (int k = 0; k < 3; k++) //cheak
  225. {
  226. if (p_board[i++][j++] == actor)
  227. {
  228. count++;
  229. i++;
  230. j++;
  231. }
  232. else
  233. break;
  234. }
  235.  
  236. if (count == 4) //cheak if count =4
  237. {
  238. return 0; // "win" ==0
  239. }
  240.  
  241. i = col, j = row;
  242.  
  243. for (int k = 0; k < 3; k++)//cheak row --
  244. {
  245. if (p_board[i--][j--] == actor)
  246. {
  247. count++;
  248. i--;
  249. j--;
  250. }
  251. else
  252. break;
  253. }
  254. if (count == 4)//cheak if count =4
  255. {
  256. return 0; // "win" == 0
  257. }
  258.  
  259. if (count != 4)//check if actor not win
  260. {
  261. return 1; // lozer
  262. }
  263.  
  264. }
  265.  
  266. void Board::print() const
  267. {
  268. int k = 1;
  269. cout << "|";
  270. for (int i = 0; i < width; i++)//print |
  271. {
  272. cout << k << " |";
  273. k++;
  274. }
  275.  
  276. cout << endl;
  277. cout << "_";
  278. for (int i = 0; i < width; i++)
  279. {
  280. cout << "___";
  281. }
  282. cout << endl;
  283.  
  284. for (int i = 0; i <length; i++)
  285. {
  286. cout <<"|";
  287. for (int j = 0; j <width ; j++)
  288. {
  289. cout<<p_board[i][j]<<" |";
  290. }
  291. cout << endl;
  292. cout << "_";
  293. for (int i = 0; i < width; i++)
  294. {
  295. cout << "___";
  296. }
  297. cout << endl;
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement