IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

Abcisse et ordonnée inversé => comprend pas


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 12
    Par défaut Abcisse et ordonnée inversé => comprend pas
    Bonjour,

    Je dois faire un programme tout bête, une sorte de démineur.
    Pour ca j'ai fais un tableau dynamic à deux dimensions, mais le problème est que l'abcisse et l'ordonnée sont inversé.
    Je voudrais : tableau[x][y] et j'ai l'inverse.
    Je suis sur que c'est tout bête mais je bloque depuis un moment.
    Si quelqu'un voit la solution,

    Merci !!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    void display_grid(char**);
    char** instruction(char**);
     
    int width = 0; //wide of the grid 
    int height = 0; // height of the grid
    int flag = 10; //Numbers of max flag
     
    int main()
    {
     
    	char **grid=NULL; //Array for the grid, we don't know the size yet
    	//initialization of the grid 
       while(flag>0) //while it remains any flags
       {
       	grid = instruction(grid);//main program
       }
       //display_grid(grid);
     
        return 0;
    }
     
    char** instruction(char **grid)
    {
    	char test[1]; //for the type of instruction
       int i,j;
       int x[1]; //width
       int y[1]; //height
       printf("\nInstruction ?\n\ng:Initialize the grid\nb:Mine placement\nu:Uncover a tile\nf:Place a flag\n=> ");
       scanf("%s %d %d",test,x,y);
       printf("x:%d y:%d\n",x[0],y[0]);
    	switch(test[0]) //which instruction is asked
    	{
    		case 'g': //set up the grip
    		{
    			if(x[0]>100 || y[0]>100 || (x[0]*y[0]<10)) { //check the grid size
    				printf("grid error\n");
    				exit(1);
    			}				
     
    			width = x[0];
       		height = y[0];
       		//allocation
    			grid = (char **) malloc (height * sizeof(char *)); 
    			for(i=0; i<width*height; i++)
    			grid[i] = (char *) malloc (width * sizeof(char *));
     
    			for(i=0;i<height;i++)
    			{
    				for(j=0;j<width;j++)
    				{
    				grid[i][j] = '*'; //fill up the grid with *
    				}
    			}
    			display_grid(grid);
    			int mines = 10;
    			while(mines>9)
    			{
     
    					printf("\nYou have %d mines remaining\nPlease choose an emplacement\n",mines);
    					scanf("%s %d %d",test,x,y);
    					if(test[0]!='b' || x[0] < 0 || x[0]>width-1 || y[0]<0 || y[0]> height-1 || grid[x[0]][y[0]]=='b')
    					{
    						printf("mine-error\n");
    						exit(1);	
    					}
    					grid[x[0]][y[0]]='b';
    					printf("Mine successfully placed in (%d,%d)\n",x[0],y[0]);
    					mines--;
    			}
    			break;
    		}
     
    		case 'u': //uncover tile
    		{
    			if(x[0] < 0 || x[0]>width-1 || y[0]<0 || y[0]> height-1 || grid[x[0]][y[0]]!='*') 
    			{
    				if(grid[x[0]][y[0]]=='b') {
    					printf("You have uncovered a mine !\nYou have lost !\n");
    				}
    				else {
    				printf("uncover-error\n");
    				}
    				exit(1);
    			}
    			int counter=0; //numbers of mines around the uncover tile
    			//check the neighbor mines
    			if(x[0]>0 && x[0]<width-1 && y[0]>0 && y[0]<height-1) 
    			{
    				for(j=-1;j<=1;j++)
    				{
    					for(i=-1;i<=1;i++) 
    					{
    							if(grid[x[0]+i][y[0]+j]=='b') 
    							{
    								counter++;
    							}
    					}	
    				}
     
    				char tmp[1];
    				sprintf(tmp,"%d",counter);//to convert int in char 
    				grid[x[0]][y[0]] = tmp[0];
    			}
    			display_grid(grid);
    			break;
    		}
     
    		case 'f': //drop a flag
    		{
    			if(x[0] < 0 || x[0]>=width-1 || y[0]<0 || y[0]>= height-1 || grid[x[0]][y[0]]=='f') 
    			{
    				printf("uncover-error");
    				exit(1);
    			}
    			grid[x[0]][y[0]]='f';
    			printf("Flag successfully placed in (%d,%d)\n",x[0],y[0]);
    			flag--;
     
    			break;
    		}
    	}
     
    	return grid;
    }
     
     
    void display_grid(char **grid) //display the grid
    {
    	int i,j;
    	for(i=0;i<height;i++)
    	{
    	printf("|");
    		for(j=0;j<width;j++)
    		{
    			/*if(grid[i][j]=='b') 
    			{
    				printf("*");
    			}
    			else 
    			{*/
    				printf("%c",grid[i][j]);
    			//}
    		}
    	printf("|\n");
    	}
     
    }

  2. #2
    Rédacteur

    Avatar de ram-0000
    Homme Profil pro
    Consultant en sécurité
    Inscrit en
    Mai 2007
    Messages
    11 517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Consultant en sécurité
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Mai 2007
    Messages : 11 517
    Par défaut
    Citation Envoyé par karbox Voir le message
    Je voudrais : tableau[x][y] et j'ai l'inverse.
    Effectivement, ceci dit, tableau[y][x] me parait plus logique que tableau[x][y].

    C'est dans ton code, dans la fonction instruction() que tu initialises ton tableau.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    			grid = (char **) malloc (height * sizeof(char *)); 
    			for(i=0; i<width*height; i++)
    			grid[i] = (char *) malloc (width * sizeof(char *));
    ceci génère un tableau grid[height][width]

    Au fait, tu es sûr que pour le 2eme malloc, le sizeof est un char * et pas plutôt un char (tu as de la chance, char * est plus gros que char, donc il n'y a pas de débordement)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    grid[i] = (char *) malloc (width * sizeof(char));
    Raymond
    Vous souhaitez participer à la rubrique Réseaux ? Contactez-moi

    Cafuro Cafuro est un outil SNMP dont le but est d'aider les administrateurs système et réseau à configurer leurs équipements SNMP réseau.
    e-verbe Un logiciel de conjugaison des verbes de la langue française.

    Ma page personnelle sur DVP
    .

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 12
    Par défaut
    Merci pour la réponse.
    Je pensais bien que le problème venait de la, mais même en inversant "width" et "height" bizarement ca ne change rien.
    J'ai maintenant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    grid = (char **) malloc (width * sizeof(char *)); 
    for(i=0; i<width*height; i++)
    grid[i] = (char *) malloc (height * sizeof(char));
    (Merci pour la correction d'allocation)

  4. #4
    Membre expérimenté Avatar de quetzacoatl
    Profil pro
    Inscrit en
    Janvier 2011
    Messages
    168
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2011
    Messages : 168
    Par défaut
    pour quoi tu fais:
    grid = (char **) malloc (height * sizeof(char *));
    for(i=0; i<width*height; i++)
    grid[i] = (char *) malloc (width * sizeof(char *));
    pouquoi tu va jusqu'à width*height alors que tu n'as que height cases réservées?

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 12
    Par défaut
    Exact, merci

  6. #6
    Membre habitué
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    12
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2009
    Messages : 12
    Par défaut
    Je pensais que c'était résolu mais non.

    J'ai maintenant le code de début suivant : mais toujours inversé (abcisse et ordonnée)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    grid = (char **) malloc (width * sizeof(char *));  //width x size of a char
    for(i=0; i<width; i++)
    grid[i] = (char *) malloc (height * sizeof(char)); //height x size of a char for each row (that's a square)
     
    for(i=0;i<height;i++)
    {
    	for(j=0;j<width;j++)
    	{
    		grid[j][i] = '*'; //fill up the grid with *
    	}
    }
    Je dois finir ce petit projet bientôt et je suis vraiment perdu ... pourtant ca parait tellement simple mais ca me fait tourner en rond

Discussions similaires

  1. Erreur Objet requis : 'this' --> Comprend pas!!
    Par Grozeil dans le forum ASP
    Réponses: 3
    Dernier message: 30/03/2005, 09h46
  2. [thread][methodologie]Quelque chose que je ne comprends pas!
    Par norkius dans le forum Général Java
    Réponses: 5
    Dernier message: 16/03/2005, 14h01
  3. sql ne comprend pas mon where!et me demande des parametres
    Par marie10 dans le forum Langage SQL
    Réponses: 10
    Dernier message: 20/04/2004, 11h08
  4. [Rave] un message que je ne comprends pas
    Par Clotilde dans le forum Rave
    Réponses: 2
    Dernier message: 30/09/2003, 21h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo