Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define TAM 4
  6.  
  7. int tab[TAM][TAM], pontuacao, movimento_preso;
  8.  
  9. int selecionar_movimento();
  10. void iniciar_pesos();
  11.  
  12.  
  13. void resetar_tabuleiro()
  14. {
  15. pontuacao = 0;
  16.  
  17. int i, j;
  18. for(i = 0; i < TAM; i++)
  19. {
  20. for(j = 0; j < TAM; j++)
  21. {
  22. tab[i][j] = 0;
  23. }
  24. }
  25. }
  26.  
  27. int existem_casas_vazias()
  28. {
  29. int existe = 0;
  30.  
  31. int i, j;
  32.  
  33. for(i = 0; i < TAM && !existe; i++)
  34. {
  35. for(j = 0; j < TAM && !existe; j++)
  36. {
  37. if(!tab[i][j])
  38. {
  39. existe = 1;
  40. }
  41. else
  42. {
  43. if(i > 0 && tab[i - 1][j] == tab[i][j])
  44. existe = 1;
  45.  
  46. if(j > 0 && tab[i][j - 1] == tab[i][j])
  47. existe = 1;
  48.  
  49. if(j < (TAM -1) && tab[i][j + 1] == tab[i][j])
  50. existe = 1;
  51.  
  52. if(i < (TAM - 1) && tab[i + 1][j] == tab[i][j])
  53. existe = 1;
  54. }
  55. }
  56. }
  57.  
  58. return existe;
  59. }
  60.  
  61. void gerar_casa()
  62. {
  63. int gerouCasa = 0;
  64.  
  65. do
  66. {
  67. int randomX = rand() % TAM;
  68. int randomY = rand() % TAM;
  69.  
  70. if(tab[randomX][randomY] == 0)
  71. {
  72. tab[randomX][randomY] = 2;
  73. gerouCasa = 1;
  74. }
  75. }
  76. while(gerouCasa == 0);
  77. }
  78.  
  79. void imprimir_tabuleiro()
  80. {
  81. printf("\nPontuacao: %17d\n\n", pontuacao);
  82.  
  83. int i, j;
  84. for(i = 0; i < TAM; i++)
  85. {
  86. for(j = 0; j < TAM; j++)
  87. {
  88. printf("[%4d]", tab[i][j]);
  89. }
  90. printf("\n");
  91. }
  92. }
  93.  
  94. void fundir_casas(int comando)
  95. {
  96. switch (comando)
  97. {
  98. case 0: //Ir para baixo
  99. {
  100. int j;
  101. for(j = 0; j < TAM; j++)
  102. {
  103. int linha_pivot = TAM - 1;
  104.  
  105. while(linha_pivot > 0)
  106. {
  107. int pivot = tab[linha_pivot][j];
  108.  
  109. if(pivot == 0)
  110. {
  111. linha_pivot--;
  112. }
  113. else
  114. {
  115. int i;
  116. for(i = linha_pivot - 1; i >= 0; i--)
  117. {
  118. if(tab[i][j] != 0 && tab[linha_pivot][j] != tab[i][j])
  119. {
  120. break;
  121. }
  122. else if(tab[linha_pivot][j] == tab[i][j])
  123. {
  124. pontuacao += tab[linha_pivot][j] + tab[i][j];
  125. tab[linha_pivot][j] += tab[i][j];
  126. tab[i][j] = 0;
  127. movimento_preso = 0;
  128. break;
  129. }
  130. }
  131. linha_pivot--;
  132. }
  133. }
  134. }
  135. break;
  136. }
  137.  
  138. case 1: //Ir para cima
  139. {
  140. int j;
  141. for(j = 0; j < TAM; j++)
  142. {
  143. int linha_pivot = 0;
  144.  
  145. while(linha_pivot < TAM - 1)
  146. {
  147. int pivot = tab[linha_pivot][j];
  148.  
  149. if(pivot == 0)
  150. {
  151. linha_pivot++;
  152. }
  153. else
  154. {
  155. int i;
  156. for(i = linha_pivot + 1; i <= TAM - 1; i++)
  157. {
  158. if(tab[i][j] != 0 && tab[linha_pivot][j] != tab[i][j])
  159. {
  160. break;
  161. }
  162. else if(tab[linha_pivot][j] == tab[i][j])
  163. {
  164. pontuacao += tab[linha_pivot][j] + tab[i][j];
  165. tab[linha_pivot][j] += tab[i][j];
  166. tab[i][j] = 0;
  167. movimento_preso = 0;
  168. break;
  169. }
  170. }
  171. linha_pivot++;
  172. }
  173. }
  174. }
  175. break;
  176. }
  177.  
  178. case 2: //Ir para esquerda
  179. {
  180. int i;
  181. for(i = 0; i < TAM; i++)
  182. {
  183. int coluna_pivot = 0;
  184.  
  185. while(coluna_pivot < TAM - 1)
  186. {
  187. int pivot = tab[i][coluna_pivot];
  188.  
  189. if(pivot == 0)
  190. {
  191. coluna_pivot++;
  192. }
  193. else
  194. {
  195. int j;
  196. for(j = coluna_pivot + 1; j <= TAM - 1; j++)
  197. {
  198. if(tab[i][j] != 0 && tab[i][coluna_pivot] != tab[i][j])
  199. {
  200. break;
  201. }
  202. else if(tab[i][coluna_pivot] == tab[i][j])
  203. {
  204. pontuacao += tab[i][coluna_pivot] + tab[i][j];
  205. tab[i][coluna_pivot] += tab[i][j];
  206. tab[i][j] = 0;
  207. movimento_preso = 0;
  208. break;
  209. }
  210. }
  211. coluna_pivot++;
  212. }
  213. }
  214. }
  215. break;
  216. }
  217.  
  218. case 3: //Ir para direita
  219. {
  220. int i;
  221. for(i = 0; i < TAM; i++)
  222. {
  223. int coluna_pivot = TAM - 1;
  224.  
  225. while(coluna_pivot > 0)
  226. {
  227. int pivot = tab[i][coluna_pivot];
  228.  
  229. if(pivot == 0)
  230. {
  231. coluna_pivot--;
  232. }
  233. else
  234. {
  235. int j;
  236. for(j = coluna_pivot - 1; j >= 0; j--)
  237. {
  238. if(tab[i][j] != 0 && tab[i][coluna_pivot] != tab[i][j])
  239. {
  240. break;
  241. }
  242. else if(tab[i][coluna_pivot] == tab[i][j])
  243. {
  244. pontuacao += tab[i][coluna_pivot] + tab[i][j];
  245. tab[i][coluna_pivot] += tab[i][j];
  246. tab[i][j] = 0;
  247. movimento_preso = 0;
  248. break;
  249. }
  250. }
  251. coluna_pivot--;
  252. }
  253. }
  254. }
  255. break;
  256. }
  257. }
  258. }
  259.  
  260. void aplicar_gravidade(int comando)
  261. {
  262. switch(comando)
  263. {
  264. case 0: //Ir para baixo
  265. {
  266. int j;
  267. for(j = 0; j < TAM; j++)
  268. {
  269. int linha_pivot = TAM - 1;
  270.  
  271. while(linha_pivot > 0)
  272. {
  273. int pivot = tab[linha_pivot][j];
  274.  
  275. if(pivot != 0)
  276. {
  277. linha_pivot--;
  278. }
  279. else
  280. {
  281. int i;
  282. for(i = linha_pivot - 1; i >= 0; i--)
  283. {
  284. if(tab[i][j] != 0)
  285. {
  286. tab[linha_pivot][j] = tab[i][j];
  287. tab[i][j] = 0;
  288. movimento_preso = 0;
  289. break;
  290. }
  291. }
  292. linha_pivot--;
  293. }
  294. }
  295. }
  296. break;
  297. }
  298.  
  299. case 1: //Ir para cima
  300. {
  301. int j;
  302. for(j = 0; j < TAM; j++)
  303. {
  304. int linha_pivot = 0;
  305.  
  306. while(linha_pivot < TAM - 1)
  307. {
  308. int pivot = tab[linha_pivot][j];
  309.  
  310. if(pivot != 0)
  311. {
  312. linha_pivot++;
  313. }
  314. else
  315. {
  316. int i;
  317. for(i = linha_pivot + 1; i <= TAM - 1; i++)
  318. {
  319. if(tab[i][j] != 0)
  320. {
  321. tab[linha_pivot][j] = tab[i][j];
  322. tab[i][j] = 0;
  323. movimento_preso = 0;
  324. break;
  325. }
  326. }
  327. linha_pivot++;
  328. }
  329. }
  330. }
  331. break;
  332. }
  333.  
  334. case 2: //Ir para esquerda
  335. {
  336. int i;
  337. for(i = 0; i < TAM; i++)
  338. {
  339. int coluna_pivot = 0;
  340.  
  341. while(coluna_pivot < TAM - 1)
  342. {
  343. int pivot = tab[i][coluna_pivot];
  344.  
  345. if(pivot != 0)
  346. {
  347. coluna_pivot++;
  348. }
  349. else
  350. {
  351. int j;
  352. for(j = coluna_pivot + 1; j <= TAM - 1; j++)
  353. {
  354. if(tab[i][j] != 0)
  355. {
  356. tab[i][coluna_pivot] = tab[i][j];
  357. tab[i][j] = 0;
  358. movimento_preso = 0;
  359. break;
  360. }
  361. }
  362. coluna_pivot++;
  363. }
  364. }
  365. }
  366. break;
  367. }
  368.  
  369. case 3: //Ir para direita
  370. {
  371. int i;
  372. for(i = 0; i < TAM; i++)
  373. {
  374. int coluna_pivot = TAM - 1;
  375.  
  376. while(coluna_pivot > 0)
  377. {
  378. int pivot = tab[i][coluna_pivot];
  379.  
  380. if(pivot != 0)
  381. {
  382. coluna_pivot--;
  383. }
  384. else
  385. {
  386. int j;
  387. for(j = coluna_pivot - 1; j >= 0; j--)
  388. {
  389. if(tab[i][j] != 0)
  390. {
  391. tab[i][coluna_pivot] = tab[i][j];
  392. tab[i][j] = 0;
  393. movimento_preso = 0;
  394. break;
  395. }
  396. }
  397. coluna_pivot--;
  398. }
  399. }
  400. }
  401. break;
  402. }
  403. }
  404. }
  405.  
  406. void aplicar(int comando)
  407. {
  408. fundir_casas(comando);
  409. aplicar_gravidade(comando);
  410. }
  411.  
  412. int main()
  413. {
  414. srand(time(NULL));
  415. iniciar_pesos();
  416.  
  417. resetar_tabuleiro();
  418. gerar_casa();
  419. movimento_preso = 0;
  420.  
  421. while(existem_casas_vazias())
  422. {
  423. if(!movimento_preso)
  424. {
  425. gerar_casa();
  426. }
  427. movimento_preso = 1;
  428. imprimir_tabuleiro();
  429.  
  430. int comando = -1;
  431.  
  432. do
  433. {
  434. comando = selecionar_movimento();
  435. printf("\nComando: %d", comando);
  436. //scanf("%d", &comando);
  437. } while(comando < 0 || comando > 3);
  438.  
  439. aplicar(comando);
  440.  
  441. }
  442.  
  443. printf("\n\nGameover! Sua pontuacao final foi: %d", pontuacao);
  444.  
  445. // desafio: gere uma sequencia de movimentos (IA)
  446. // simulando-a com o seu programa
  447. // envie para tpshc2@cin.ufpe.br o seu programa.
  448. // se ela atingir 1024 em uma execucao,
  449. // do monitor, voce ganha bonus (se tiver
  450. // a maior pontuacao dentre os que enviaram).
  451. // ATENCAO: se a regra do jogo nao estiver implementada
  452. // corretamente, perde 1 ponto na prova.
  453.  
  454. system("pause");
  455. return 0;
  456. }
  457.  
  458. // AI***AI***AI***AI***AI***AI***AI***AI***AI***AI***AI***AI***AI***AI***AI
  459.  
  460. #define PROFUNDIDADE 50
  461.  
  462. //int node_percentual(int arr[TAM][TAM], int cmd, int profundidade);
  463.  
  464. //int node_max(int arr[TAM][TAM], int profundidade, int i2, int j2);
  465.  
  466. double non_terminal_score(int arr[TAM][TAM], int cmd, int profundidade);
  467. double terminal_score(int arr[TAM][TAM]);
  468.  
  469. //esse comando irá verificar se um comando {0, 1, 2, 3} pode ser executado em
  470. //uma dada matriz que representa um tabuleira
  471. int comando_valido(int arr[TAM][TAM])
  472. {
  473. //printf("iniciando comando_valido\n");
  474.  
  475. int i, j, resposta = 0;
  476.  
  477. for(i = 0; i < TAM; i++)
  478. for(j = 0; j < TAM; j++)
  479. {
  480. if(i < (TAM - 1) && arr[i][j] && (arr[i][j] == arr[i + 1][j] || !arr[i + 1][j]))
  481. resposta |= 1 << 0;
  482.  
  483. if(i > 0 && arr[i][j] && (arr[i][j] == arr[i - 1][j] || !arr[i - 1][j]))
  484. resposta |= 1 << 1;
  485.  
  486. if(j > 0 && arr[i][j] && (arr[i][j] == arr[i][j - 1] || !arr[i][j - 1]))
  487. resposta |= 1 << 2;
  488.  
  489. if(j < (TAM - 1) && arr[i][j] && (arr[i][j] == arr[i][j + 1] || !arr[i][j + 1]))
  490. resposta |= 1 << 3;
  491. }
  492.  
  493. //printf("encerrando comando_valido\n");
  494. return resposta;
  495. }
  496.  
  497. int selecionar_movimento()
  498. {
  499. int maior = -1, validos = comando_valido(tab);
  500. double maior_valor = 0;
  501. double temp;
  502.  
  503. if(validos)
  504. {
  505. int i;
  506. for(i = 0; i < 4; i++)
  507. {
  508. if(validos & (1 << i))
  509. {
  510. temp = non_terminal_score(tab, i, PROFUNDIDADE);
  511.  
  512. if(maior == -1 || maior_valor < temp)
  513. {
  514. maior = i;
  515. maior_valor = temp;
  516. }
  517. }
  518. }
  519. }
  520. else
  521. {
  522. return 0;
  523. }
  524.  
  525. return maior;
  526.  
  527. }
  528.  
  529.  
  530. double non_terminal_score(int arr[TAM][TAM], int cmd, int profundidade)
  531. {
  532. printf("%d\n", profundidade);
  533. if(!profundidade)
  534. return terminal_score(arr);
  535.  
  536. int array[TAM][TAM], i, j;
  537.  
  538. double score = 0;
  539.  
  540. switch(cmd)
  541. {
  542. case 0:
  543. {
  544. int last_added, ja_unido = 0;
  545.  
  546. for(j = 0; j < TAM; j++)
  547. {
  548. last_added = -1;
  549. for(i = TAM - 1; i >= 0; i--)
  550. {
  551. if(arr[i][j])
  552. {
  553. if(last_added != -1 && arr[i][j] == array[last_added][j] && !ja_unido)
  554. {
  555. array[last_added][j] = 2*arr[i][j];
  556. //score += array[last_added][j];
  557. ja_unido = 1;
  558. }
  559. else
  560. {
  561. last_added += 1;
  562. array[last_added][j] = arr[i][j];
  563. ja_unido = 0;
  564. }
  565. }
  566. }
  567. }
  568. break;
  569. }
  570. case 1:
  571. {
  572. int last_added, ja_unido = 0;
  573.  
  574. for(j = 0; j < TAM; j++)
  575. {
  576. last_added = -1;
  577. for(i = 0; i < TAM; i++)
  578. {
  579. if(arr[i][j])
  580. {
  581. if(last_added != -1 && arr[i][j] == array[last_added][j] && !ja_unido)
  582. {
  583. array[last_added][j] = 2*arr[i][j];
  584. //score += array[last_added][j];
  585. ja_unido = 1;
  586. } else
  587. {
  588. last_added += 1;
  589. array[last_added][j] = arr[i][j];
  590. ja_unido = 0;
  591. }
  592. }
  593. }
  594. }
  595. break;
  596. }
  597. case 2:
  598. {
  599. int last_added, ja_unido = 0;
  600.  
  601. for(i = 0; i < TAM; i++)
  602. {
  603. last_added = -1;
  604. for(j = 0; j < TAM; j++)
  605. {
  606. if(arr[i][j])
  607. {
  608. if(last_added != -1 && arr[i][j] == array[i][last_added] && !ja_unido)
  609. {
  610. array[i][last_added] = 2*arr[i][j];
  611. //score += array[i][last_added];
  612. ja_unido = 1;
  613. } else
  614. {
  615. last_added += 1;
  616. array[i][last_added] = arr[i][j];
  617. ja_unido = 0;
  618. }
  619.  
  620. }
  621. }
  622. }
  623. break;
  624. }
  625. case 3:
  626. {
  627. int last_added, ja_unido = 0;
  628.  
  629. for(i = 0; i < TAM; i++)
  630. {
  631. last_added = -1;
  632. for(j = TAM - 1; j >= 0; j--)
  633. {
  634. if(arr[i][j])
  635. {
  636. if(last_added != -1 && arr[i][j] == array[i][last_added] && !ja_unido)
  637. {
  638. array[i][last_added] = 2*arr[i][j];
  639. //score += array[i][last_added];
  640. ja_unido = 1;
  641. } else
  642. {
  643. last_added += 1;
  644. array[i][last_added] = arr[i][j];
  645. ja_unido = 0;
  646. }
  647.  
  648. }
  649. }
  650. }
  651. break;
  652. }
  653. }
  654.  
  655. int k, validos, qtd_score = 0, sum_score = 0;
  656. for(i = 0; i < TAM; i++)
  657. {
  658. for(j = 0; j < TAM; j++)
  659. {
  660. if(!array[i][j])
  661. {
  662. printf("Entrou, segunda parte do non terminal\n");
  663. array[i][j] = 2;
  664. validos = comando_valido(array);
  665. if(validos)
  666. {
  667. int maior_valor_score, maior_score = -1, temp_score;
  668. for(k = 0; k < 4; k++) if(validos & (1 << k))
  669. {
  670. temp_score = non_terminal_score(array, k, profundidade - 1);
  671. if(maior_score == -1 || temp_score > maior_valor_score){
  672. maior_valor_score = temp_score;
  673. maior_score = k;
  674. }
  675. }
  676.  
  677. sum_score += maior_valor_score;
  678. }
  679. else
  680. {
  681. sum_score += terminal_score(array);
  682. }
  683. array[i][j] = 0;
  684. qtd_score += 1;
  685. }
  686. }
  687. }
  688.  
  689. if(qtd_score) score += sum_score/qtd_score;
  690.  
  691. return score;
  692. }
  693.  
  694. double weight[8][TAM][TAM];
  695.  
  696.  
  697. void iniciar_pesos(){
  698. weight[0][0][0] = 0.135759; weight[0][0][1] = 0.121925; weight[0][0][2] = 0.102812; weight[0][0][3] = 0.099937;
  699. weight[0][1][0] = 0.0997992; weight[0][1][1] = 0.0888405; weight[0][1][2] = 0.076711; weight[0][1][2] = 0.0724143;
  700. weight[0][2][0] = 0.060654; weight[0][2][1] = 0.0562579; weight[0][2][2] = 0.037116; weight[0][2][3] = 0.0161889;
  701. weight[0][3][0] = 0.0125498; weight[0][3][1] = 0.00992495; weight[0][3][2] = 0.00575871; weight[0][3][3] = 0.00335193;
  702.  
  703. /*
  704. weight[0][0][0] = 7; weight[0][0][1] = 6; weight[0][0][2] = 5; weight[0][0][3] = 4;
  705. weight[0][1][0] = 6; weight[0][1][1] = 5; weight[0][1][2] = 4; weight[0][1][2] = 3;
  706. weight[0][2][0] = 5; weight[0][2][1] = 4; weight[0][2][2] = 3; weight[0][2][3] = 2;
  707. weight[0][3][0] = 4; weight[0][3][1] = 3; weight[0][3][2] = 2; weight[0][3][3] = 1;
  708. */
  709.  
  710. int i, j, k, l;
  711.  
  712. for(i = 0; i < TAM; i++)
  713. for(j = 0; j < TAM; j++)
  714. weight[4][i][TAM - j - 1] = weight[0][i][j];
  715.  
  716. for(i = 0; i < 2; i++)
  717. {
  718. for(j = 1; j < 4; j++)
  719. {
  720. for(k = 0; k < TAM; k++)
  721. for(l = 0; l < TAM; l++)
  722. {
  723. weight[i*4 + j][l][TAM - (k + 1)] = weight[i*4 + j - 1][k][l];
  724. }
  725. }
  726. }
  727. }
  728.  
  729.  
  730.  
  731. double terminal_score(int arr[TAM][TAM])
  732. {
  733. printf("Entrou\n");
  734. int i, j, k;
  735. double maior = -1, atual;
  736.  
  737. for(k = 0; k < 8; k++)
  738. {
  739. atual = 0;
  740.  
  741. for(i = 0; i < TAM; i++)
  742. for(j = 0; j < TAM; j++)
  743. {
  744. atual += weight[k][i][j]*arr[i][j];
  745. }
  746.  
  747. if(maior == -1 || atual > maior) maior = atual;
  748. }
  749.  
  750. return maior;
  751. }
  752.  
  753. /*
  754. //esse comando representa a primeira MAX node do expectmax algorithm
  755. //ela irá transformar o valor maior do passo em um comando válido
  756. int selecionar_movimento()
  757. {
  758. //printf("iniciando selecionar_movimento\n");
  759. int maior = -1, maior_valor = 0,i;
  760.  
  761.  
  762. int validos = comando_valido(tab);
  763.  
  764. for(i = 0; i < 4; i++)
  765. {
  766. if(validos & (1 << i))
  767. {
  768. int temp = node_percentual(tab, i, PROFUNDIDADE);
  769. if(maior == -1 || maior_valor < temp)
  770. maior = i;
  771. maior_valor = temp;
  772. }
  773. }
  774.  
  775. if(maior == -1)
  776. maior = 0;
  777.  
  778. //printf("encerrando selecionar_movimento\n");
  779. return maior;
  780. }
  781.  
  782.  
  783. //esse comando representa todas as nodes percentuais da arvore
  784. int node_percentual(int arr[TAM][TAM], int cmd, int profundidade)
  785. {
  786. //printf("iniciando node_percentual\n");
  787. int array[TAM][TAM], resposta = 0, i, j;
  788.  
  789. for(i = 0; i < TAM; i++)
  790. memset(array[i], 0, TAM);
  791.  
  792. //printf("%d\n", cmd);
  793.  
  794. switch(cmd)
  795. {
  796. case 0:
  797. {
  798. //printf("1\n");
  799.  
  800. int last_added;
  801.  
  802. for(j = 0; j < TAM; j++)
  803. {
  804. last_added = -1;
  805. for(i = TAM - 1; i >= 0; i--)
  806. {
  807. if(arr[i][j])
  808. {
  809. if(last_added != -1 && arr[i][j] == arr[last_added][j])
  810. {
  811. array[last_added][j] *= 2;
  812. resposta += array[last_added][j];
  813. last_added += 1;
  814. } else
  815. {
  816. last_added += 1;
  817. array[last_added][j] = arr[i][j];
  818. }
  819.  
  820. }
  821. }
  822. }
  823. break;
  824. }
  825. case 1:
  826. {
  827. //printf("1\n");
  828. int last_added;
  829.  
  830. for(j = 0; j < TAM; j++)
  831. {
  832. last_added = -1;
  833. for(i = 0; i < TAM; i++)
  834. {
  835. if(arr[i][j])
  836. {
  837. if(last_added != -1 && arr[i][j] == arr[last_added][j])
  838. {
  839. array[last_added][j] *= 2;
  840. resposta += array[last_added][j];
  841. last_added += 1;
  842. } else
  843. {
  844. last_added += 1;
  845. array[last_added][j] = arr[i][j];
  846. }
  847.  
  848. }
  849. }
  850. }
  851. break;
  852. }
  853. case 2:
  854. {
  855. //printf("1\n");
  856. int last_added;
  857.  
  858. for(i = 0; i < TAM; i++)
  859. {
  860. last_added = -1;
  861. for(j = 0; j < TAM; j++)
  862. {
  863. if(arr[i][j])
  864. {
  865. if(last_added != -1 && arr[i][j] == arr[i][last_added])
  866. {
  867. array[i][last_added] *= 2;
  868. resposta += array[i][last_added];
  869. last_added += 1;
  870. } else
  871. {
  872. last_added += 1;
  873. array[i][last_added] = arr[i][j];
  874. }
  875.  
  876. }
  877. }
  878. }
  879. break;
  880. }
  881. case 3:
  882. {
  883. //printf("1\n");
  884. int last_added;
  885.  
  886. for(i = 0; i < TAM; i++)
  887. {
  888. last_added = -1;
  889. for(j = TAM; j >= 0; j--)
  890. {
  891. if(arr[i][j])
  892. {
  893. if(last_added != -1 && arr[i][j] == arr[i][last_added])
  894. {
  895. array[i][last_added] *= 2;
  896. resposta += array[i][last_added];
  897. last_added += 1;
  898. } else
  899. {
  900. last_added += 1;
  901. array[i][last_added] = arr[i][j];
  902. }
  903.  
  904. }
  905. }
  906. }
  907. break;
  908. }
  909. }
  910.  
  911. //printf("1\n");
  912.  
  913. int temp = 0, qtd = 0;
  914. for(i = 0; i < TAM; i++)
  915. {
  916. for(j = 0; j < TAM; j++)
  917. {
  918. if(!array[i][j])
  919. {
  920. temp += node_max(array, profundidade - 1, i, j);
  921. qtd += 1;
  922. }
  923. }
  924. }
  925. if(qtd) resposta += (int)(temp / qtd);
  926.  
  927. //printf("encerrando node_percentual\n");
  928. return resposta;
  929. }
  930.  
  931. int node_max(int arr[TAM][TAM], int profundidade, int i2, int j2)
  932. {
  933. //printf("iniciando node_max\n");
  934. if(!profundidade)
  935. return 0;
  936.  
  937. int i, j, array[TAM][TAM];
  938. for(i = 0; i < TAM; i++)
  939. for(j = 0; j < TAM; j++)
  940. array[i][j] = arr[i][j];
  941.  
  942. array[i2][j2] = 2;
  943.  
  944. int maior = -1, maior_valor;
  945.  
  946. int validos = comando_valido(array);
  947.  
  948. if(validos == 0)
  949. return -5000000;
  950.  
  951. for(i = 0; i < 4; i++)
  952. {
  953. if(validos & (1 << i))
  954. {
  955. int temp = node_percentual(array, i, profundidade);
  956. if(maior == -1 || maior_valor < temp)
  957. maior = i;
  958. maior_valor = temp;
  959. }
  960. }
  961.  
  962. //printf("encerrando node_max\n");
  963. return maior;
  964. }
  965. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement