Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<math.h>
  4. #include <time.h>
  5.  
  6. char GerarDirecao() {
  7. int num = rand();
  8. char direcao;
  9. if (num % 2 == 0) {
  10. direcao = 'H';
  11. } else {
  12. direcao = 'V';
  13. }
  14. return direcao;
  15. }
  16.  
  17. int GerarPosX(int tamanho) {
  18. int x;
  19.  
  20. do{
  21. x = rand() % tamanho;
  22. }while(x >= (tamanho - 2));
  23. return x;
  24. }
  25.  
  26. int GerarPosY(int tamanho) {
  27. int y;
  28. y = rand() % tamanho - 2;
  29. return y;
  30. }
  31.  
  32. int PovoarMatrizNavios(int ** matriz, int navios) {
  33. int posx, posy, indice_controle = 0;
  34. char direcao;
  35. char x;
  36. int tamanho_matriz = (navios)* (navios);
  37.  
  38.  
  39. while(indice_controle < navios){
  40. direcao = GerarDirecao();
  41.  
  42.  
  43. if(direcao == 'H'){
  44. posx = rand() % (tamanho_matriz-2);
  45. posy = rand() % (tamanho_matriz);
  46. if (matriz [posy][posx]==0 && matriz [posy][posx+1]==0 && matriz [posy][posx+2]==0){
  47. for(int i = posx; i < posx+3; i++){
  48. matriz[posy][i] = 1;
  49. }
  50. indice_controle++;
  51. }
  52. }else{
  53. posx = rand() % (tamanho_matriz);
  54. posy = rand() % (tamanho_matriz-2);
  55. if (matriz [posx][posy]==0 && matriz [posx][posy+1]==0 && matriz [posx][posy+2]==0){
  56. for(int i = posy; i < posy+3; i++){
  57. matriz[i][posy] = 1;
  58. }
  59. indice_controle++;
  60. }
  61.  
  62. }
  63. }
  64.  
  65.  
  66. //Imprimindo matriz
  67. for(int i = 0; i < tamanho_matriz; i++){
  68. for(int j = 0; j < tamanho_matriz; j++){
  69. printf("%d ", matriz[i][j]);
  70. }
  71. printf("\n");
  72. }
  73. }
  74.  
  75. int GerarNovoTabuleiro(int ** matriz) {
  76. char direcao;
  77. int tamanho_matriz;
  78. int navios = 0;
  79. int i,j;
  80. do {
  81. printf("Digite a quantidade de navios: ");
  82. scanf("%d", &navios);
  83. } while (navios < 3);
  84.  
  85. tamanho_matriz = (navios) * (navios);
  86. matriz = (int**)malloc(tamanho_matriz * sizeof(int*));
  87. for (i = 0; i < tamanho_matriz; i++){
  88. matriz[i] = (int*) malloc(tamanho_matriz * sizeof(int));
  89. for (j = 0; j < tamanho_matriz; j++){
  90. matriz[i][j] = 0;
  91. }
  92. }
  93.  
  94. PovoarMatrizNavios(matriz, navios);
  95. }
  96.  
  97. int UsarTabuleiroArquivo (int ** matriz){
  98. FILE *arquivo;
  99. int cont = 0,valor,tamanho_matriz,navios,i,j;
  100. arquivo = fopen("C:/Users/julia cavaglieri/Documents/Engenharia Civil/1° Período/matriz.txt","r");
  101. if(arquivo == NULL){
  102. printf("Erro na abertura do arquivo");
  103. exit(1);
  104. }
  105.  
  106. while (! feof(arquivo)) {
  107. fscanf(arquivo, "%d", &valor);
  108. cont++;
  109. }
  110. tamanho_matriz = sqrt(cont);
  111. printf("Tamanho da matriz=%d",tamanho_matriz);
  112. navios= sqrt(tamanho_matriz);
  113. printf("\nNavios=%d",navios);
  114. fclose(arquivo);
  115.  
  116. tamanho_matriz = (navios)*(navios);
  117. arquivo = fopen("C:/Users/julia cavaglieri/Documents/Engenharia Civil/1° Período/matriz.txt","r");
  118. if(arquivo == NULL){
  119. printf("Erro na abertura do arquivo");
  120. exit(1);
  121. }
  122. matriz = (int**)malloc(tamanho_matriz * sizeof(int*));
  123.  
  124. for (i = 0; i < tamanho_matriz; i++){
  125. matriz[i] = (int*) malloc(tamanho_matriz * sizeof(int));
  126. for (j = 0; j < tamanho_matriz; j++){
  127. //Ler do arquivo e armazena na matriz
  128. fscanf(arquivo, "%d", &valor);
  129. matriz[i][j] = valor;
  130. }
  131. }
  132. fclose(arquivo);
  133. //Imprimindo matriz
  134. for(int i = 0; i < tamanho_matriz; i++){
  135. for(int j = 0; j < tamanho_matriz; j++){
  136. printf("%d ", matriz[i][j]);
  137. }
  138. printf("\n");
  139. }
  140. }
  141.  
  142. int Jogar (int ** matriz,int navios){
  143. int torpedo;
  144. int tamanho = navios * navios;
  145. int lin, col;
  146. torpedo = ((tamanho*tamanho)/2)*0.7;
  147. int jogadas=0;
  148.  
  149. char tabuleiro [tamanho][tamanho];
  150. for(lin=0; lin<tamanho; lin++){
  151. for(col=0; col<tamanho; col++){
  152. tabuleiro[lin][col] = '~';
  153. printf("%c", tabuleiro[lin][col]);
  154. }
  155. }
  156.  
  157. while (jogadas<torpedo){
  158. do{
  159. printf("Informe as coordenadas do tiro (linha | coluna): ");
  160. scanf("%d %d", &lin, &col);
  161. }while (lin>tamanho || col >tamanho || lin <0 || col <0);
  162.  
  163. if(matriz[lin][col] == 1){
  164. tabuleiro[lin][col] = 'X';
  165. }else{
  166. tabuleiro[lin][col] = '*';
  167. }
  168. for(lin=0; lin<tamanho; lin++){
  169. for(col=0; col<tamanho; col++){
  170. printf("%c", tabuleiro[lin][col]);
  171. }
  172. }
  173. jogadas ++;
  174.  
  175. }
  176. }
  177.  
  178. void main() {
  179. int opcao;
  180. int ** matriz;
  181.  
  182. srand(time(NULL));
  183. do {
  184. printf("Escolha as seguintes opçoes:\n(1) Gerar novo tabuleiro\n(2) Usar tabuleiro do arquivo\n(3) Jogar \n(0) Sair\n\nOpção: ");
  185. scanf("%d", &opcao);
  186. if (opcao > 3 || opcao < 0) {
  187. printf("\nopcao invalida\n");
  188. }
  189. } while (opcao > 3 || opcao < 0);
  190.  
  191.  
  192. switch (opcao) {
  193. case 1:
  194. GerarNovoTabuleiro(matriz); break;
  195. case 2:
  196. UsarTabuleiroArquivo(matriz); break;
  197. case 3:
  198. Jogar(matriz); break;
  199. }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement