Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
88
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 joueur * 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. free(jeu_);
  527. return value;
  528. }
  529.  
  530. //Fonction min trouve le minimum des noeuds fils
  531. 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)
  532. {
  533. node += 1;
  534. if(dep <= 0)
  535. {
  536. return f_eval(jeu, joueur);
  537. }
  538.  
  539. Pion* jeu_ = f_raz_plateau();
  540. f_copie_plateau(jeu, jeu_);
  541. int value = INFINI;
  542. int i, j, _i, _j;
  543. for (i = 0; i < NB_LIGNES; i++)
  544. {
  545. for (j = 0; j < NB_COLONNES; j++)
  546. {
  547. if(jeu[i * NB_COLONNES + j].couleur == -joueur)
  548. {
  549. for(int k = 0; k < 9; k++)
  550. {
  551. if(k == 4)
  552. continue;
  553.  
  554. _i = i + k/3 - 1;
  555. _j = j + k%3 - 1;
  556.  
  557. if(!f_bouge_piece(jeu_, i, j, _i, _j, -joueur))
  558. {
  559. int v_ = f_max(jeu_, dep - 1, joueur, NULL, NULL, NULL, NULL, alpha, beta);
  560. if(value > v_)
  561. {
  562. value = v_;
  563. }
  564. if(beta > value)
  565. {
  566. beta = value;
  567. }
  568. if(alpha >= beta)
  569. {
  570. break;
  571. }
  572. f_bouge_piece(jeu_, _i, _j, i, j, -joueur);
  573. }
  574. }
  575. }
  576. }
  577. }
  578.  
  579. free(jeu_);
  580. return value;
  581. }
  582.  
  583. //Fonction max trouve le maximum des noeuds fils
  584. 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)
  585. {
  586. node += 1;
  587. if(dep <= 0)
  588. {
  589. return f_eval(jeu, joueur);
  590. }
  591.  
  592. Pion* jeu_ = f_raz_plateau();
  593. f_copie_plateau(jeu, jeu_);
  594. int value = -INFINI;
  595. int i, j, _i, _j;
  596. for (i = 0; i < NB_LIGNES; i++)
  597. {
  598. for (j = 0; j < NB_COLONNES; j++)
  599. {
  600. if(jeu[i * NB_COLONNES + j].couleur == joueur)
  601. {
  602. for(int k = 0; k < 9; k++)
  603. {
  604. if(k == 4)
  605. continue;
  606.  
  607. _i = i + k/3 - 1;
  608. _j = j + k%3 - 1;
  609.  
  610. if(!f_bouge_piece(jeu_, i, j, _i, _j, joueur))
  611. {
  612. int v_ = f_min(jeu_, dep - 1, joueur, NULL, NULL, NULL, NULL, alpha, beta);
  613. if(value < v_)
  614. {
  615. value = v_;
  616. if(dep == depth)
  617. {
  618. *i_start = i;
  619. *j_start = j;
  620. *i_target = _i;
  621. *j_target = _j;
  622. }
  623. }
  624. if(alpha < value)
  625. {
  626. alpha = value;
  627. }
  628. if(alpha >= beta)
  629. {
  630. break;
  631. }
  632. f_bouge_piece(jeu_, _i, _j, i, j, joueur);
  633. }
  634. }
  635. }
  636. }
  637. }
  638.  
  639. free(jeu_);
  640. return value;
  641. }
  642.  
  643. /**
  644. * Calcule et joue le meilleur cout
  645. * */
  646. void f_IA(int joueur, int dep, int alpha, int beta)
  647. {
  648. #ifdef DEBUG
  649. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  650. #endif
  651. depth = dep;
  652. int i, j, ii, jj;
  653. int value = f_max(plateauDeJeu, dep, joueur, &i, &j, &ii, &jj, alpha, beta);
  654. if(!f_bouge_piece(plateauDeJeu, i, j, ii, jj, joueur))
  655. printf("IA bouge avec la valeur : %d, i = %d, j = %d, ii = %d, jj = %d\n", value, i, j, ii, jj);
  656. else
  657. printf("IA bouge avec la valeur : %d, i = %d, j = %d, ii = %d, jj = %d\n", value, i, j, ii, jj);
  658.  
  659. #ifdef DEBUG
  660. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  661. #endif
  662. }
  663.  
  664.  
  665. /**
  666. * Demande le choix du joueur humain et calcule le coup demande
  667. * */
  668. void f_humain(int joueur)
  669. {
  670. char c1, c2;
  671. char buffer[32];
  672. int l1, l2;
  673.  
  674.  
  675. #ifdef DEBUG
  676. printf("dbg: entering %s %d\n", __FUNCTION__, __LINE__);
  677. #endif
  678.  
  679. printf("joueur ");
  680. switch(joueur)
  681. {
  682. case -1:
  683. printf("o ");
  684. break;
  685. case 1:
  686. printf("x ");
  687. break;
  688. default:
  689. printf("inconnu ");
  690. }
  691. printf("joue:\n");
  692. while(1)
  693. {
  694. fgets(buffer, 32, stdin);
  695. if(sscanf(buffer, "%c%i%c%i\n", &c1, &l1, &c2, &l2) == 4)
  696. {
  697. if(f_bouge_piece(plateauDeJeu, l1, f_convert_char2int(c1), l2, f_convert_char2int(c2), joueur) == 0)
  698. break;
  699. }
  700. fflush(stdin);
  701. printf("mauvais choix\n");
  702. }
  703.  
  704. #ifdef DEBUG
  705. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  706. #endif
  707. }
  708.  
  709. int main(int argv, char *argc[])
  710. {
  711. int profondeur_MAX = 4;
  712. int data[profondeur_MAX][2];
  713.  
  714. /*printf("1 humain vs IA\n2 humain vs humain\n3 IA vs IA\n");
  715. scanf("%d",&mode);*/
  716.  
  717. for(int profondeur = 1; profondeur <= profondeur_MAX; profondeur++)
  718. {
  719. int fin, mode=3, ret, joueur = 1, alpha = -INFINI, beta = INFINI;
  720. plateauDeJeu = f_init_plateau();
  721. node = 0;
  722. fin = 0;
  723. while (!fin)
  724. {
  725. f_affiche_plateau(plateauDeJeu);
  726. if(mode==1)
  727. {
  728. if(joueur>0)
  729. f_humain(joueur);
  730. else
  731. f_IA(joueur, profondeur, alpha, beta);
  732. }
  733. else if(mode==2)
  734. {
  735. f_humain(joueur);
  736. }
  737. else
  738. {
  739. f_IA(joueur, profondeur, alpha, beta);
  740. }
  741.  
  742. if ((ret = f_gagnant()) != 0)
  743. {
  744. switch (ret)
  745. {
  746. case 1:
  747. f_affiche_plateau(plateauDeJeu);
  748. printf("joueur x gagne!\n");
  749. fin = 1;
  750. break;
  751. case -1:
  752. f_affiche_plateau(plateauDeJeu);
  753. printf("joueur o gagne!\n");
  754. fin = 1;
  755. break;
  756. }
  757. }
  758. joueur = -joueur;
  759. }
  760. data[profondeur - 1][0] = profondeur;
  761. data[profondeur - 1][1] = node;
  762. }
  763.  
  764. FILE* file=fopen("data_minimax_alphabeta_analyse.dat", "w+");
  765.  
  766. for(int i = 0; i < profondeur_MAX; i++)
  767. fprintf(file, "%d %d\n", data[i][0], data[i][1]);
  768.  
  769. fclose(file);
  770.  
  771. #ifdef DEBUG
  772. printf("dbg: exiting %s %d\n", __FUNCTION__, __LINE__);
  773. #endif
  774.  
  775. return 0;
  776. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement