Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define NMAX 1000
  5. char castigator(char matrice[][NMAX], int p, int q, int n_lin_col) {
  6. int i,j,n,ok,k;
  7. char x;
  8. n=n_lin_col;
  9. // varificare pe linii
  10. for (i=p; i<p+n; i++) {
  11. k=0;
  12. for (j=q; j<q+n-1; j++) {
  13. if (matrice[i][q]=='X' || matrice[i][q]=='0')
  14. x=matrice[i][q];
  15. else
  16. x = 'H';
  17. if (matrice[i][j+1]==x)
  18. k++;
  19. if (k==(n-1))
  20. return x;
  21. }
  22. }
  23. // verificare pe coloane
  24. for (j=q; j<q+n; j++) {
  25. k=0;
  26. for (i=p; i<p+n-1; i++) {
  27. if (matrice[p][j]=='X' || matrice[p][j]=='0')
  28. x=matrice[p][j];
  29. else
  30. x='H';
  31. if (matrice[i+1][j]==x)
  32. k++;
  33. if (k==(n-1))
  34. return x;
  35. }
  36. }
  37. // verificare diagonala principala
  38. k=0;
  39. if (matrice[p][q]=='X' || matrice[p][q]=='0') {
  40. x = matrice[p][q];
  41. }
  42. else {
  43. x='H';
  44. }
  45. for (i=0; i<n; i++) {
  46. if (matrice[p+i][q+i]==x)
  47. k++;
  48. if (k==n)
  49. return x;
  50. }
  51. /*k=0;
  52. if (matrice[p][q]=='X' || matrice[p][q]=='0') {
  53. x = matrice[p][q];
  54. }
  55. else {
  56. x='H';
  57. }
  58. for (i=p; i< p+n; i++) {
  59. for (j = q; j < q + n; j++) {
  60. if (i==j) {
  61. if (x == matrice[i][j]) {
  62. k++;
  63. }
  64. }
  65. if (k == n) {
  66. return x;
  67. }
  68. }
  69. } */ // incercare nereuista
  70. // verificare diagonala secundara
  71. k=0;
  72. if (matrice[p][q+n-1]=='X' || matrice[p][q+n-1]=='0')
  73. x=matrice[p][q+n-1];
  74. else
  75. x='H';
  76. for (i=0; i<n; i++) {
  77. if (x==matrice[p+i][q+n-1-i])
  78. {
  79. k++;
  80. }
  81. if (k==n)
  82. return x;
  83. }
  84. /* k=0;
  85. if (matrice[p][q+n-1]=='X' || matrice[p][q+n-1]=='0')
  86. x=matrice[p][q+n-1];
  87. else
  88. x='H';
  89. for (i=p; i<p+n; i++) {
  90. for(j=q; j<q+n; j++) {
  91. if (j==(q+n)-1-i) {
  92. if (matrice[i][j]==x)
  93. k++;
  94. }
  95. if (k==n)
  96. return x;
  97. }
  98. } */ //incercare esuata
  99. return '-';
  100.  
  101. }
  102.  
  103. void afisare_mini_matrice(char matrice[][NMAX], int p, int q, int n_lin_col) {
  104. int i,j,n;
  105. n=n_lin_col;
  106. for (i=p; i<p+n; i++) {
  107. for (j = q; j < q + n; j++) {
  108. printf(" %c", matrice[i][j]);
  109. }
  110. printf("\n");
  111. }
  112. }
  113.  
  114. void minimatrice(char matrice[][NMAX], int n_lin_col, char v[NMAX]) {
  115. int p,q,n,k;
  116. // char x;
  117. p=0;
  118. q=0;
  119. n=n_lin_col;
  120. k=0;
  121. while (p < (n * n - n + 1)) {
  122. v[k]=castigator(matrice,p,q,n);
  123. k++;
  124. // printf("############ \n"); // TODO de facut contor pt vectorul cu castigatori
  125. //x=castigator(matrice,p,q,n);
  126. // printf(" %c", x);
  127. q = q + n;
  128. if (q > (n * n - n + 1)) {
  129. q = 0;
  130. p = p + n;
  131. }
  132. }
  133.  
  134. }
  135.  
  136. void verificare(char matrice[][NMAX], int n_lin_col) {
  137. int i,j,p=0,q=0,n, eror=0;
  138. int v[NMAX];
  139. n=n_lin_col;
  140.  
  141. for (i=p; i < (p+n); i++) {
  142. for (j = q; j < (q + n); j++) {
  143. // functie castig
  144. printf("%c ", matrice[i][j]);
  145. // v[p+1/n][(q+1/n)] = functie castig;
  146. if (j == n * n - 1)
  147. eror = 1;
  148. }
  149. printf("\n");
  150. if (i == (p + n - 1) && eror == 0) {
  151. q = q + n;
  152. i = p;
  153. }
  154. if (i == (p + n - 1) && eror == 1) {
  155. p = p + n;
  156. i = p;
  157. q=0;
  158. eror = 0;
  159. }
  160. if (i == (n * n - 1) && eror == 1)
  161. break;
  162.  
  163. // if (i==n*n-1)
  164. // break;
  165. // if (i==p+n-1)
  166. // p=p+n;
  167.  
  168. }
  169. }
  170.  
  171. void robin(char matrice[][NMAX], char jucator, int n_lin_col) {
  172. int i, control = 0, k;
  173. for (i = 0; i < n_lin_col; i++) {
  174. if ((matrice[i][i] != 'X') && matrice[i][i] != '0') {
  175. matrice[i][i] = jucator;
  176. control = 1;
  177. break;
  178. }
  179. }
  180. k = 1;
  181. while (control == 0) {
  182. for (i = 0; i < n_lin_col - k; i++) {
  183. if ((matrice[i][i + k] != 'X') && matrice[i][i + k] != '0') {
  184. matrice[i][i + k] = jucator;
  185. control = 1;
  186. break;
  187. }
  188. }
  189. if (control != 1) {
  190. for (i = 0; i < n_lin_col - k; i++) {
  191. if ((matrice[i + k][i] != 'X') && matrice[i + k][i] != '0') {
  192. matrice[i + k][i] = jucator;
  193. control = 1;
  194. break;
  195. }
  196. }
  197. }
  198. k++;
  199. }
  200. }
  201.  
  202. void incarcare_matrice(char matrice[][NMAX], int n_lin_col) {
  203. int i, j;
  204. for (i = 0; i < pow(n_lin_col, 2); i++)
  205. for (j = 0; j < pow(n_lin_col, 2); j++)
  206. matrice[i][j] = '*';
  207. }
  208.  
  209. void afisare_matrice(char matrice[][NMAX], int n_lin_col) {
  210. int i, j;
  211. for (i = 0; i < pow(n_lin_col, 2); i++) {
  212. for (j = 0; j < pow(n_lin_col, 2); j++) {
  213. printf("%c ", matrice[i][j]);
  214. }
  215. printf("\n");
  216. }
  217. }
  218.  
  219. void mutari(char matrice[][NMAX], int numar_mutari, int n_lin_col, char v[NMAX]) {
  220. int n = n_lin_col * n_lin_col, fullboard = 0;
  221. int i, x, y, erorr, k=1;
  222. char jucator;
  223. for (i = 1; i <= numar_mutari; i++) {
  224. if (k > n * n) {
  225. fullboard = 1;
  226. scanf(" %c %d %d", &jucator, &x, &y);
  227. } else {
  228. erorr = 0;
  229. scanf(" %c %d %d", &jucator, &x, &y);
  230. k++;
  231. if (i % 2 == 1 && jucator == 'X') {
  232. if (matrice[x][y] == 'X' || matrice[x][y] == '0') {
  233. printf("NOT AN EMPTY CELL \n");
  234. robin(matrice, jucator, n);
  235. } else {
  236. if ((x < 0 || x >= n) || (y < 0 || y >= n)) {
  237. printf("INVALID INDEX \n");
  238. robin(matrice, jucator, n);
  239. } else
  240. matrice[x][y] = 'X';
  241. }
  242. }
  243.  
  244. if (i % 2 == 1 && jucator != 'X') {
  245. printf("NOT YOUR TURN \n");
  246. erorr = 1;
  247. }
  248.  
  249. if (i % 2 == 0 && jucator == '0') {
  250. if (matrice[x][y] == 'X' || matrice[x][y] == '0') {
  251. printf("NOT AN EMPTY CELL \n");
  252. robin(matrice, jucator, n);
  253. } else {
  254. if ((x < 0 || x >= n) || (y < 0 || y >= n)) {
  255. printf("INVALID INDEX \n");
  256. robin(matrice, jucator, n);
  257. } else
  258. matrice[x][y] = '0';
  259. }
  260. }
  261.  
  262. if (i % 2 == 0 && jucator != '0') {
  263. printf("NOT YOUR TURN \n");
  264. erorr = 1;
  265. }
  266. if (erorr == 1) {
  267. i++;
  268. numar_mutari++;
  269. }
  270. minimatrice(matrice, n_lin_col, v);
  271. }
  272. }
  273. if (fullboard == 1 )
  274. printf("Fullboard \n");
  275. }
  276.  
  277. int main() {
  278. char c[NMAX][NMAX];
  279. char v[NMAX];
  280. int n, m, k;
  281. scanf("%d", &n);
  282. scanf("%d", &m);
  283. incarcare_matrice(c, n);
  284. mutari(c, m, n, v);
  285. printf("################## S U C C E S ################## \n");
  286. for (k=0; k<n*n; k++) {
  287. printf("%c ", v[k]);
  288. if ((k+1)%n==0)
  289. printf("\n");
  290. }
  291. afisare_matrice(c, n);
  292.  
  293. return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement