Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<math.h>
- #include <time.h>
- char GerarDirecao() {
- int num = rand();
- char direcao;
- if (num % 2 == 0) {
- direcao = 'H';
- } else {
- direcao = 'V';
- }
- return direcao;
- }
- int GerarPosX(int tamanho) {
- int x;
- do{
- x = rand() % tamanho;
- }while(x >= (tamanho - 2));
- return x;
- }
- int GerarPosY(int tamanho) {
- int y;
- y = rand() % tamanho - 2;
- return y;
- }
- int PovoarMatrizNavios(int ** matriz, int navios) {
- int posx, posy, indice_controle = 0;
- char direcao;
- char x;
- int tamanho_matriz = (navios)* (navios);
- while(indice_controle < navios){
- direcao = GerarDirecao();
- if(direcao == 'H'){
- posx = rand() % (tamanho_matriz-2);
- posy = rand() % (tamanho_matriz);
- if (matriz [posy][posx]==0 && matriz [posy][posx+1]==0 && matriz [posy][posx+2]==0){
- for(int i = posx; i < posx+3; i++){
- matriz[posy][i] = 1;
- }
- indice_controle++;
- }
- }else{
- posx = rand() % (tamanho_matriz);
- posy = rand() % (tamanho_matriz-2);
- if (matriz [posx][posy]==0 && matriz [posx][posy+1]==0 && matriz [posx][posy+2]==0){
- for(int i = posy; i < posy+3; i++){
- matriz[i][posy] = 1;
- }
- indice_controle++;
- }
- }
- }
- //Imprimindo matriz
- for(int i = 0; i < tamanho_matriz; i++){
- for(int j = 0; j < tamanho_matriz; j++){
- printf("%d ", matriz[i][j]);
- }
- printf("\n");
- }
- }
- int GerarNovoTabuleiro(int ** matriz) {
- char direcao;
- int tamanho_matriz;
- int navios = 0;
- int i,j;
- do {
- printf("Digite a quantidade de navios: ");
- scanf("%d", &navios);
- } while (navios < 3);
- tamanho_matriz = (navios) * (navios);
- matriz = (int**)malloc(tamanho_matriz * sizeof(int*));
- for (i = 0; i < tamanho_matriz; i++){
- matriz[i] = (int*) malloc(tamanho_matriz * sizeof(int));
- for (j = 0; j < tamanho_matriz; j++){
- matriz[i][j] = 0;
- }
- }
- PovoarMatrizNavios(matriz, navios);
- }
- int UsarTabuleiroArquivo (int ** matriz){
- FILE *arquivo;
- int cont = 0,valor,tamanho_matriz,navios,i,j;
- arquivo = fopen("C:/Users/julia cavaglieri/Documents/Engenharia Civil/1° Período/matriz.txt","r");
- if(arquivo == NULL){
- printf("Erro na abertura do arquivo");
- exit(1);
- }
- while (! feof(arquivo)) {
- fscanf(arquivo, "%d", &valor);
- cont++;
- }
- tamanho_matriz = sqrt(cont);
- printf("Tamanho da matriz=%d",tamanho_matriz);
- navios= sqrt(tamanho_matriz);
- printf("\nNavios=%d",navios);
- fclose(arquivo);
- tamanho_matriz = (navios)*(navios);
- arquivo = fopen("C:/Users/julia cavaglieri/Documents/Engenharia Civil/1° Período/matriz.txt","r");
- if(arquivo == NULL){
- printf("Erro na abertura do arquivo");
- exit(1);
- }
- matriz = (int**)malloc(tamanho_matriz * sizeof(int*));
- for (i = 0; i < tamanho_matriz; i++){
- matriz[i] = (int*) malloc(tamanho_matriz * sizeof(int));
- for (j = 0; j < tamanho_matriz; j++){
- //Ler do arquivo e armazena na matriz
- fscanf(arquivo, "%d", &valor);
- matriz[i][j] = valor;
- }
- }
- fclose(arquivo);
- //Imprimindo matriz
- for(int i = 0; i < tamanho_matriz; i++){
- for(int j = 0; j < tamanho_matriz; j++){
- printf("%d ", matriz[i][j]);
- }
- printf("\n");
- }
- }
- int Jogar (int ** matriz,int navios){
- int torpedo;
- int tamanho = navios * navios;
- int lin, col;
- torpedo = ((tamanho*tamanho)/2)*0.7;
- int jogadas=0;
- char tabuleiro [tamanho][tamanho];
- for(lin=0; lin<tamanho; lin++){
- for(col=0; col<tamanho; col++){
- tabuleiro[lin][col] = '~';
- printf("%c", tabuleiro[lin][col]);
- }
- }
- while (jogadas<torpedo){
- do{
- printf("Informe as coordenadas do tiro (linha | coluna): ");
- scanf("%d %d", &lin, &col);
- }while (lin>tamanho || col >tamanho || lin <0 || col <0);
- if(matriz[lin][col] == 1){
- tabuleiro[lin][col] = 'X';
- }else{
- tabuleiro[lin][col] = '*';
- }
- for(lin=0; lin<tamanho; lin++){
- for(col=0; col<tamanho; col++){
- printf("%c", tabuleiro[lin][col]);
- }
- }
- jogadas ++;
- }
- }
- void main() {
- int opcao;
- int ** matriz;
- srand(time(NULL));
- do {
- 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: ");
- scanf("%d", &opcao);
- if (opcao > 3 || opcao < 0) {
- printf("\nopcao invalida\n");
- }
- } while (opcao > 3 || opcao < 0);
- switch (opcao) {
- case 1:
- GerarNovoTabuleiro(matriz); break;
- case 2:
- UsarTabuleiroArquivo(matriz); break;
- case 3:
- Jogar(matriz); break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement