Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. #define NB_LIGNES 10
  7. #define NB_COLONNES 10
  8. #define INFINI 10000
  9.  
  10. /*#define DEBUG*/
  11.  
  12. typedef struct pion_s
  13. {
  14. int couleur;
  15. int valeur;
  16. }Pion;
  17.  
  18. int depth;
  19. Pion *plateauDeJeu;
  20. int node;
  21.  
  22. void f_affiche_plateau(Pion *plateau);
  23. int f_convert_char2int(char c);
  24. char f_convert_int2char(int i);
  25. int negamax(Pion* jeu, int dep, int joueur, int* i_start, int* j_start, int* i_target, int* j_target, int alpha, int beta);
  26. int f_max(Pion* jeu, int dep, int joueur, int* i_start, int* j_start, int* i_target, int* j_target, int alpha, int beta);
  27. int f_min(Pion* jeu, int dep, int joueur, int* i_start, int* j_start, int* i_target, int* j_target, int alpha, int beta);
  28.  
  29. int f_convert_char2int(char c)
  30. {
  31. #ifdef DEBUG
  32. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  33. #endif
  34. if(c>='A' && c<='Z')
  35. return (int)(c-'A');
  36. if(c>='a' && c<='z')
  37. return (int)(c-'a');
  38. return -1;
  39. }
  40.  
  41. char f_convert_int2char(int i)
  42. {
  43. #ifdef DEBUG
  44. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  45. #endif
  46.  
  47. return (char)i+'A';
  48. }
  49.  
  50. Pion *f_init_plateau()
  51. {
  52. int i, j;
  53. Pion *plateau=NULL;
  54.  
  55.  
  56. #ifdef DEBUG
  57. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  58. #endif
  59.  
  60. plateau = (Pion *)malloc(NB_LIGNES*NB_COLONNES*sizeof(Pion));
  61. if(plateau == NULL)
  62. {
  63. printf("error: unable to allocate memory\n");
  64. exit(EXIT_FAILURE);
  65. }
  66.  
  67. for(i=0; i<NB_LIGNES; i++)
  68. {
  69. for(j=0; j<NB_COLONNES; j++)
  70. {
  71. plateau[i*NB_COLONNES+j].couleur = 0;
  72. plateau[i*NB_COLONNES+j].valeur = 0;
  73. }
  74. }
  75.  
  76. plateau[9*NB_COLONNES+5].couleur = 1;
  77. plateau[9*NB_COLONNES+5].valeur = 1;
  78.  
  79. plateau[9*NB_COLONNES+6].couleur = 1;
  80. plateau[9*NB_COLONNES+6].valeur = 2;
  81.  
  82. plateau[9*NB_COLONNES+7].couleur = 1;
  83. plateau[9*NB_COLONNES+7].valeur = 3;
  84.  
  85. plateau[9*NB_COLONNES+8].couleur = 1;
  86. plateau[9*NB_COLONNES+8].valeur = 2;
  87.  
  88. plateau[9*NB_COLONNES+9].couleur = 1;
  89. plateau[9*NB_COLONNES+9].valeur = 1;
  90.  
  91. plateau[8*NB_COLONNES+0].couleur = 1;
  92. plateau[8*NB_COLONNES+0].valeur = 1;
  93.  
  94. plateau[8*NB_COLONNES+1].couleur = 1;
  95. plateau[8*NB_COLONNES+1].valeur = 3;
  96.  
  97. plateau[8*NB_COLONNES+2].couleur = 1;
  98. plateau[8*NB_COLONNES+2].valeur = 3;
  99.  
  100. plateau[8*NB_COLONNES+3].couleur = 1;
  101. plateau[8*NB_COLONNES+3].valeur = 1;
  102.  
  103. plateau[8*NB_COLONNES+6].couleur = 1;
  104. plateau[8*NB_COLONNES+6].valeur = 1;
  105.  
  106. plateau[8*NB_COLONNES+7].couleur = 1;
  107. plateau[8*NB_COLONNES+7].valeur = 1;
  108.  
  109. plateau[8*NB_COLONNES+8].couleur = 1;
  110. plateau[8*NB_COLONNES+8].valeur = 1;
  111.  
  112. plateau[7*NB_COLONNES+1].couleur = 1;
  113. plateau[7*NB_COLONNES+1].valeur = 1;
  114.  
  115. plateau[7*NB_COLONNES+2].couleur = 1;
  116. plateau[7*NB_COLONNES+2].valeur = 1;
  117.  
  118. plateau[2*NB_COLONNES+7].couleur = -1;
  119. plateau[2*NB_COLONNES+7].valeur = 1;
  120.  
  121. plateau[2*NB_COLONNES+8].couleur = -1;
  122. plateau[2*NB_COLONNES+8].valeur = 1;
  123.  
  124. plateau[1*NB_COLONNES+1].couleur = -1;
  125. plateau[1*NB_COLONNES+1].valeur = 1;
  126.  
  127. plateau[1*NB_COLONNES+2].couleur = -1;
  128. plateau[1*NB_COLONNES+2].valeur = 1;
  129.  
  130. plateau[1*NB_COLONNES+3].couleur = -1;
  131. plateau[1*NB_COLONNES+3].valeur = 1;
  132.  
  133. plateau[1*NB_COLONNES+6].couleur = -1;
  134. plateau[1*NB_COLONNES+6].valeur = 1;
  135.  
  136. plateau[1*NB_COLONNES+7].couleur = -1;
  137. plateau[1*NB_COLONNES+7].valeur = 3;
  138.  
  139. plateau[1*NB_COLONNES+8].couleur = -1;
  140. plateau[1*NB_COLONNES+8].valeur = 3;
  141.  
  142. plateau[1*NB_COLONNES+9].couleur = -1;
  143. plateau[1*NB_COLONNES+9].valeur = 1;
  144.  
  145. plateau[0*NB_COLONNES+0].couleur = -1;
  146. plateau[0*NB_COLONNES+0].valeur = 1;
  147.  
  148. plateau[0*NB_COLONNES+1].couleur = -1;
  149. plateau[0*NB_COLONNES+1].valeur = 2;
  150.  
  151. plateau[0*NB_COLONNES+2].couleur = -1;
  152. plateau[0*NB_COLONNES+2].valeur = 3;
  153.  
  154. plateau[0*NB_COLONNES+3].couleur = -1;
  155. plateau[0*NB_COLONNES+3].valeur = 2;
  156.  
  157. plateau[0*NB_COLONNES+4].couleur = -1;
  158. plateau[0*NB_COLONNES+4].valeur = 1;
  159.  
  160. #ifdef DEBUG
  161. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  162. #endif
  163.  
  164. return plateau;
  165. }
  166.  
  167. void f_affiche_plateau(Pion *plateau)
  168. {
  169. int i,j,k;
  170.  
  171.  
  172. #ifdef DEBUG
  173. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  174. #endif
  175.  
  176. printf("\n ");
  177. for(k=0; k<NB_COLONNES; k++)
  178. printf("%2c ",f_convert_int2char(k));
  179. printf("\n ");
  180. for(k=0; k<NB_COLONNES; k++)
  181. printf("-- ");
  182. printf("\n");
  183. for(i=NB_LIGNES-1; i>=0; i--)
  184. {
  185. printf("%2d ", i);
  186. for(j=0; j<NB_COLONNES; j++)
  187. {
  188. printf("|");
  189. switch(plateau[i*NB_COLONNES+j].couleur)
  190. {
  191. case -1:
  192. printf("%do",plateau[i*NB_COLONNES+j].valeur);
  193. break;
  194. case 1:
  195. printf("%dx",plateau[i*NB_COLONNES+j].valeur);
  196. break;
  197. default:
  198. printf(" ");
  199. }
  200. }
  201. printf("|\n ");
  202. for(k=0; k<NB_COLONNES; k++)
  203. printf("-- ");
  204. printf("\n");
  205. }
  206. printf(" ");
  207.  
  208. #ifdef DEBUG
  209. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  210. #endif
  211. }
  212.  
  213.  
  214. int f_gagnant()
  215. {
  216. int i, j, somme1=0, somme2=0;
  217.  
  218.  
  219. #ifdef DEBUG
  220. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  221. #endif
  222.  
  223. //Quelqu'un est-il arrive sur la ligne de l'autre
  224. for(i=0; i<NB_COLONNES; i++)
  225. {
  226. if(plateauDeJeu[i].couleur == 1)
  227. return 1;
  228. if(plateauDeJeu[(NB_LIGNES-1)*NB_COLONNES+i].couleur == -1)
  229. return -1;
  230. }
  231.  
  232. //taille des armees
  233. for(i=0; i<NB_LIGNES; i++)
  234. {
  235. for(j=0; j<NB_COLONNES; j++)
  236. {
  237. if(plateauDeJeu[i*NB_COLONNES+j].couleur == 1)
  238. somme1++;
  239. if(plateauDeJeu[i*NB_COLONNES+j].couleur == -1)
  240. somme2++;
  241. }
  242. }
  243. if(somme1==0)
  244. return -1;
  245. if(somme2==0)
  246. return 1;
  247.  
  248. #ifdef DEBUG
  249. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  250. #endif
  251. return 0;
  252. }
  253.  
  254.  
  255. /**
  256. * Prend comme argument la ligne et la colonne de la case
  257. * pour laquelle la bataille a lieu
  258. * Renvoie le couleur du gagnant
  259. * */
  260. int f_bataille(int l, int c)
  261. {
  262. int i, j, mini, maxi, minj, maxj;
  263. int somme=0;
  264.  
  265. #ifdef DEBUG
  266. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  267. #endif
  268. mini = l-1<0?0:l-1;
  269. maxi = l+1>NB_LIGNES-1?NB_LIGNES-1:l+1;
  270. minj = c-1<0?0:c-1;
  271. maxj = c+1>NB_COLONNES-1?NB_COLONNES-1:c+1;
  272.  
  273. for(i=mini; i<=maxi; i++)
  274. {
  275. for(j=minj; j<=maxj; j++)
  276. {
  277. somme += plateauDeJeu[i*NB_COLONNES+j].couleur*plateauDeJeu[i*NB_COLONNES+j].valeur;
  278. }
  279. }
  280. somme -= plateauDeJeu[l*NB_COLONNES+c].couleur*plateauDeJeu[l*NB_COLONNES+c].valeur;
  281.  
  282. #ifdef DEBUG
  283. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  284. #endif
  285. if(somme < 0)
  286. return -1;
  287. if(somme > 0)
  288. return 1;
  289.  
  290. return plateauDeJeu[l*NB_COLONNES+c].couleur;
  291. }
  292.  
  293.  
  294. /**
  295. * Prend la ligne et colonne de la case d'origine
  296. * et la ligne et colonne de la case de destination
  297. * Renvoie 1 en cas d'erreur
  298. * Renvoie 0 sinon
  299. * */
  300. int f_test_mouvement(Pion *plateau, int l1, int c1, int l2, int c2, int couleur)
  301. {
  302. #ifdef DEBUG
  303. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  304. printf("de (%d,%d) vers (%d,%d)\n", l1, c1, l2, c2);
  305. #endif
  306. /* Erreur, hors du plateau */
  307. if(l1 < 0 || l1 >= NB_LIGNES || l2 < 0 || l2 >= NB_LIGNES ||
  308. c1 < 0 || c1 >= NB_COLONNES || c2 < 0 || c2 >= NB_COLONNES)
  309. return 1;
  310. /* Erreur, il n'y a pas de pion a deplacer ou le pion n'appartient pas au joueur*/
  311. if(plateau[l1*NB_COLONNES+c1].valeur == 0 || plateau[l1*NB_COLONNES+c1].couleur != couleur)
  312. return 1;
  313. /* Erreur, tentative de tir fratricide */
  314. if(plateau[l2*NB_COLONNES+c2].couleur == plateau[l1*NB_COLONNES+c1].couleur)
  315. return 1;
  316.  
  317. if(l1-l2 >1 || l2-l1 >1 || c1-c2 >1 || c2-c1 >1 || (l1==l2 && c1==c2))
  318. return 1;
  319. #ifdef DEBUG
  320. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  321. #endif
  322. return 0;
  323. }
  324.  
  325.  
  326. /**
  327. * Prend la ligne et colonne de la case d'origine
  328. * et la ligne et colonne de la case de destination
  329. * et effectue le trantement de l'operation demandée
  330. * Renvoie 1 en cas d'erreur
  331. * Renvoie 0 sinon
  332. * */
  333. int f_bouge_piece(Pion *plateau, int l1, int c1, int l2, int c2, int couleur)
  334. {
  335. int gagnant=0;
  336.  
  337.  
  338. #ifdef DEBUG
  339. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  340. #endif
  341.  
  342. if(f_test_mouvement(plateau, l1, c1, l2, c2, couleur) != 0)
  343. return 1;
  344.  
  345.  
  346. /* Cas ou il n'y a personne a l'arrivee */
  347. if(plateau[l2*NB_COLONNES+c2].valeur == 0)
  348. {
  349. plateau[l2*NB_COLONNES+c2].couleur = plateau[l1*NB_COLONNES+c1].couleur;
  350. plateau[l2*NB_COLONNES+c2].valeur = plateau[l1*NB_COLONNES+c1].valeur;
  351. plateau[l1*NB_COLONNES+c1].couleur = 0;
  352. plateau[l1*NB_COLONNES+c1].valeur = 0;
  353. }
  354. else
  355. {
  356. gagnant=f_bataille(l2, c2);
  357. /* victoire */
  358. if(gagnant == couleur)
  359. {
  360. plateau[l2*NB_COLONNES+c2].couleur = plateau[l1*NB_COLONNES+c1].couleur;
  361. plateau[l2*NB_COLONNES+c2].valeur = plateau[l1*NB_COLONNES+c1].valeur;
  362. plateau[l1*NB_COLONNES+c1].couleur = 0;
  363. plateau[l1*NB_COLONNES+c1].valeur = 0;
  364. }
  365. /* defaite */
  366. else if(gagnant != 0)
  367. {
  368. plateau[l1*NB_COLONNES+c1].couleur = 0;
  369. plateau[l1*NB_COLONNES+c1].valeur = 0;
  370. }
  371. }
  372.  
  373. #ifdef DEBUG
  374. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  375. #endif
  376. return 0;
  377. }
  378.  
  379. //Calcul du nombre de pions sur le plateau du joueur
  380. int f_nbPions(Pion* jeu, int joueur)
  381. {
  382. int nbPion=0;
  383. int i, j;
  384. for (i = 0; i < NB_COLONNES; ++i)
  385. {
  386. for (j = 0; j < NB_LIGNES; ++j)
  387. {
  388. if (jeu[i * NB_COLONNES + j].couleur == joueur)
  389. {
  390. ++nbPion;
  391. }
  392. }
  393. }
  394. return nbPion;
  395. }
  396.  
  397. //Calcul de la valeur de tous les pions du joueur
  398. int f_valeur(Pion* jeu, int joueur)
  399. {
  400. int i, j;
  401. int valeur=0;
  402. for (i = 0; i < NB_COLONNES; ++i)
  403. {
  404. for (j = 0; j < NB_LIGNES; ++j)
  405. {
  406. if (jeu[i * NB_COLONNES + j].couleur == joueur)
  407. {
  408. valeur += jeu[i * NB_COLONNES + j].valeur;
  409. }
  410. }
  411. }
  412. return valeur;
  413. }
  414.  
  415. //fonction d'évaluation
  416. int f_eval(Pion* jeu,int joueur)
  417. {
  418. int playerDist = 0, adversaryDist = 0;
  419. for (int i = 0; i < NB_LIGNES; i++)
  420. {
  421. for (int j = 0; j < NB_COLONNES; j++)
  422. {
  423. int col = jeu[i * NB_COLONNES + j].couleur;
  424. int goal = (col == 1)? 10: -1;
  425. if(col == joueur)
  426. {
  427. playerDist += abs(goal - i);
  428. }
  429. else if(col == -joueur)
  430. {
  431. adversaryDist += abs(goal - i);
  432. }
  433. }
  434. }
  435. int distDiff = playerDist - adversaryDist;
  436. int valDiff = f_valeur(jeu, joueur) - f_valeur(jeu, -joueur);
  437. return distDiff + 75 * valDiff + (rand()%10 == 0);
  438. }
  439.  
  440. //copie du plateau
  441. void f_copie_plateau(Pion* source, Pion* destination)
  442. {
  443. int i, j;
  444. for (i = 0; i < NB_LIGNES; i++)
  445. {
  446. for (j = 0; j < NB_COLONNES; j++)
  447. {
  448. destination[i * NB_COLONNES + j].couleur = source[i * NB_COLONNES + j].couleur;
  449. destination[i * NB_COLONNES + j].valeur = source[i * NB_COLONNES + j].valeur;
  450. }
  451. }
  452. }
  453.  
  454. //mise a zero du plateau
  455. Pion* f_raz_plateau()
  456. {
  457. Pion* jeu = NULL;
  458. int i, j;
  459. jeu = (Pion *) malloc(NB_LIGNES * NB_COLONNES * sizeof (Pion));
  460. for (i = 0; i < NB_LIGNES; i++)
  461. {
  462. for (j = 0; j < NB_COLONNES; j++)
  463. {
  464. jeu[i * NB_COLONNES + j].couleur = 0;
  465. jeu[i * NB_COLONNES + j].valeur = 0;
  466. }
  467. }
  468. return jeu;
  469. }
  470.  
  471. //Algorithm optimizations for minimax
  472. int negamax(Pion* jeu, int dep, int joueur, int* i_start, int* j_start, int* i_target, int* j_target, int alpha, int beta)
  473. {
  474. node += 1;
  475. if(dep <= 0)
  476. {
  477. return f_eval(jeu, joueur);
  478. }
  479.  
  480. Pion* jeu_ = f_raz_plateau();
  481. f_copie_plateau(jeu, jeu_);
  482. int value = -INFINI;
  483. int i, j, _i, _j;
  484. for (i = 0; i < NB_LIGNES; i++)
  485. {
  486. for (j = 0; j < NB_COLONNES; j++)
  487. {
  488. if(jeu[i * NB_COLONNES + j].couleur == joueur)
  489. {
  490. for(int k = 0; k < 9; k++)
  491. {
  492. if(k == 4)
  493. continue;
  494.  
  495. _i = i + k/3 - 1;
  496. _j = j + k%3 - 1;
  497.  
  498. if(!f_bouge_piece(jeu_, i, j, _i, _j, joueur))
  499. {
  500. int v_ = -negamax(jeu_, dep - 1, -joueur, NULL, NULL, NULL, NULL, -alpha, -beta);
  501. if(value < v_)
  502. {
  503. value = v_;
  504. if(dep == depth)
  505. {
  506. *i_start = i;
  507. *j_start = j;
  508. *i_target = _i;
  509. *j_target = _j;
  510. }
  511. }
  512. if(alpha < value)
  513. {
  514. alpha = value;
  515. }
  516. if(alpha >= beta)
  517. {
  518. break;
  519. }
  520. f_bouge_piece(jeu_, _i, _j, i, j, joueur);
  521. }
  522. }
  523. }
  524. }
  525. }
  526.  
  527. free(jeu_);
  528. return value;
  529. }
  530.  
  531. //Fonction min trouve le minimum des noeuds fils
  532. int f_min(Pion* jeu, int dep, int joueur, int* i_start, int* j_start, int* i_target, int* j_target, int alpha, int beta)
  533. {
  534. node += 1;
  535. if(dep <= 0)
  536. {
  537. return f_eval(jeu, joueur);
  538. }
  539.  
  540. Pion* jeu_ = f_raz_plateau();
  541. f_copie_plateau(jeu, jeu_);
  542. int value = INFINI;
  543. int i, j, _i, _j;
  544. for (i = 0; i < NB_LIGNES; i++)
  545. {
  546. for (j = 0; j < NB_COLONNES; j++)
  547. {
  548. if(jeu[i * NB_COLONNES + j].couleur == -joueur)
  549. {
  550. for(int k = 0; k < 9; k++)
  551. {
  552. if(k == 4)
  553. continue;
  554.  
  555. _i = i + k/3 - 1;
  556. _j = j + k%3 - 1;
  557.  
  558. if(!f_bouge_piece(jeu_, i, j, _i, _j, -joueur))
  559. {
  560. int v_ = f_max(jeu_, dep - 1, joueur, NULL, NULL, NULL, NULL, alpha, beta);
  561. if(value > v_)
  562. {
  563. value = v_;
  564. }
  565. if(beta > value)
  566. {
  567. beta = value;
  568. }
  569. if(alpha >= beta)
  570. {
  571. break;
  572. }
  573. f_bouge_piece(jeu_, _i, _j, i, j, -joueur);
  574. }
  575. }
  576. }
  577. }
  578. }
  579.  
  580. free(jeu_);
  581. return value;
  582. }
  583.  
  584. //Fonction max trouve le maximum des noeuds fils
  585. int f_max(Pion* jeu, int dep, int joueur, int* i_start, int* j_start, int* i_target, int* j_target, int alpha, int beta)
  586. {
  587. node += 1;
  588. if(dep <= 0)
  589. {
  590. return f_eval(jeu, joueur);
  591. }
  592.  
  593. Pion* jeu_ = f_raz_plateau();
  594. f_copie_plateau(jeu, jeu_);
  595. int value = -INFINI;
  596. int i, j, _i, _j;
  597. for (i = 0; i < NB_LIGNES; i++)
  598. {
  599. for (j = 0; j < NB_COLONNES; j++)
  600. {
  601. if(jeu[i * NB_COLONNES + j].couleur == joueur)
  602. {
  603. for(int k = 0; k < 9; k++)
  604. {
  605. if(k == 4)
  606. continue;
  607.  
  608. _i = i + k/3 - 1;
  609. _j = j + k%3 - 1;
  610.  
  611. if(!f_bouge_piece(jeu_, i, j, _i, _j, joueur))
  612. {
  613. int v_ = f_min(jeu_, dep - 1, joueur, NULL, NULL, NULL, NULL, alpha, beta);
  614. if(value < v_)
  615. {
  616. value = v_;
  617. if(dep == depth)
  618. {
  619. *i_start = i;
  620. *j_start = j;
  621. *i_target = _i;
  622. *j_target = _j;
  623. }
  624. }
  625. if(alpha < value)
  626. {
  627. alpha = value;
  628. }
  629. if(alpha >= beta)
  630. {
  631. break;
  632. }
  633. f_bouge_piece(jeu_, _i, _j, i, j, joueur);
  634. }
  635. }
  636. }
  637. }
  638. }
  639.  
  640. free(jeu_);
  641. return value;
  642. }
  643.  
  644. /**
  645. * Calcule et joue le meilleur cout
  646. * */
  647. void f_IA(int joueur, int dep, int alpha, int beta)
  648. {
  649. #ifdef DEBUG
  650. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  651. #endif
  652. depth = dep;
  653. int i, j, ii, jj;
  654. int value = f_max(plateauDeJeu, dep, joueur, &i, &j, &ii, &jj, alpha, beta);
  655. if(!f_bouge_piece(plateauDeJeu, i, j, ii, jj, joueur))
  656. printf("IA bouge avec la valeur : %d, i = %d, j = %d, ii = %d, jj = %d\n", value, i, j, ii, jj);
  657. else
  658. printf("IA bouge avec la valeur : %d, i = %d, j = %d, ii = %d, jj = %d\n", value, i, j, ii, jj);
  659.  
  660. #ifdef DEBUG
  661. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  662. #endif
  663. }
  664.  
  665.  
  666. /**
  667. * Demande le choix du joueur humain et calcule le coup demande
  668. * */
  669. void f_humain(int joueur)
  670. {
  671. char c1, c2;
  672. char buffer[32];
  673. int l1, l2;
  674.  
  675.  
  676. #ifdef DEBUG
  677. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  678. #endif
  679.  
  680. printf("joueur ");
  681. switch(joueur)
  682. {
  683. case -1:
  684. printf("o ");
  685. break;
  686. case 1:
  687. printf("x ");
  688. break;
  689. default:
  690. printf("inconnu ");
  691. }
  692. printf("joue:\n");
  693. while(1)
  694. {
  695. fgets(buffer, 32, stdin);
  696. if(sscanf(buffer, "%c%i%c%i\n", &c1, &l1, &c2, &l2) == 4)
  697. {
  698. if(f_bouge_piece(plateauDeJeu, l1, f_convert_char2int(c1), l2, f_convert_char2int(c2), joueur) == 0)
  699. break;
  700. }
  701. fflush(stdin);
  702. printf("mauvais choix\n");
  703. }
  704.  
  705. #ifdef DEBUG
  706. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  707. #endif
  708. }
  709.  
  710. int main(int argv, char *argc[])
  711. {
  712. int profondeur_MAX = 4;
  713. int data[profondeur_MAX][2];
  714.  
  715. /*printf("1 humain vs IA\n2 humain vs humain\n3 IA vs IA\n");
  716. scanf("%d",&mode);*/
  717.  
  718. for(int profondeur = 1; profondeur <= profondeur_MAX; profondeur++)
  719. {
  720. int fin, mode=3, ret, joueur = 1, alpha = -INFINI, beta = INFINI;
  721. plateauDeJeu = f_init_plateau();
  722. node = 0;
  723. fin = 0;
  724. while (!fin)
  725. {
  726. f_affiche_plateau(plateauDeJeu);
  727. if(mode==1)
  728. {
  729. if(joueur>0)
  730. f_humain(joueur);
  731. else
  732. f_IA(joueur, profondeur, alpha, beta);
  733. }
  734. else if(mode==2)
  735. {
  736. f_humain(joueur);
  737. }
  738. else
  739. {
  740. f_IA(joueur, profondeur, alpha, beta);
  741. }
  742.  
  743. if ((ret = f_gagnant()) != 0)
  744. {
  745. switch (ret)
  746. {
  747. case 1:
  748. f_affiche_plateau(plateauDeJeu);
  749. printf("joueur x gagne!\n");
  750. fin = 1;
  751. break;
  752. case -1:
  753. f_affiche_plateau(plateauDeJeu);
  754. printf("joueur o gagne!\n");
  755. fin = 1;
  756. break;
  757. }
  758. }
  759. joueur = -joueur;
  760. }
  761. data[profondeur - 1][0] = profondeur;
  762. data[profondeur - 1][1] = node;
  763. }
  764.  
  765. FILE* file=fopen("data_minimax_alphabeta_analyse.dat", "w+");
  766.  
  767. for(int i = 0; i < profondeur_MAX; i++)
  768. fprintf(file, "%d %d\n", data[i][0], data[i][1]);
  769.  
  770. fclose(file);
  771.  
  772. #ifdef DEBUG
  773. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  774. #endif
  775.  
  776. return 0;
  777. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement