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

Android Discussion :

Identification case blanche jeu Taquin


Sujet :

Android

  1. #1
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 38
    Points : 10
    Points
    10
    Par défaut Identification case blanche jeu Taquin
    Bonjour à tous,

    Je suis actuellement en train de développer un Taquin sur android studio, cependant, je suis confronté à un petit soucis :

    Je génère mon image découpée et mélangée avec une case blanche, cependant, je ne sais pas trop comment identifier cette case blanche dans mon code afin de gérer les déplacements... j'ai fait des tests infructueux pour le moment et je vous avoue que je suis un peu agacé par un problème qui me paraît pourtant très simple...
    Dans le code ci-dessous, vous verrez que je gère un tableau d'entier, il s'agit d'un outil qui me permettra de detecter la fin du jeu (une fois que le tableau sera dans l'ordre.)

    Voici mon code source :

    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
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    package com.example.antho.taquin_progmobile;
     
    /**
     * Created by antho on 08/03/2017.
     */
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Chronometer;
    import android.widget.GridView;
     
    import java.util.Random;
     
     
    public class Jeu extends AppCompatActivity {
     
     
     
        private  int tab[];
        private Bitmap img[];
        private GridView gridView;
        private ImageAdapter adapter;
     
     
     
     
     
     
     
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.jeu);
            System.out.println(tab);
            Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); // initiate a chronometer
            simpleChronometer.start(); // start a chronometer
     
     
     
                Intent res = getIntent();
                final int taille = res.getIntExtra("taille", 3);
                this.tab=new int[taille*taille];
                for (int i=0; i<taille-i; i++){
                    tab[i]=i;
                }
     
                final int caseBlanche= tab.length-1;
     
                //Log.i("test", Integer.toString(getIntent().getIntExtra("taille", 0)));
                Bitmap image = BitmapFactory.decodeResource(this.getResources(), R.drawable.pinkfloyd);
                img= getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                gridView = (GridView) findViewById(R.id.gridView);
               adapter = new ImageAdapter(this, img);
                shuffleImages(img, adapter);
                gridView.setAdapter(adapter);
     
                gridView.setNumColumns(taille);
     
            // Colonne 1 gerée
     
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
                {
                    public void onItemClick(AdapterView<?> parent,
                                            View v, int position, long id)
                    {
                        System.out.println(caseBlanche);
                        int tmp;
                        Bitmap tmp2;
                        gridView.setAdapter(adapter);
     
     
                        // Si on à une case à droite de la case selectionnée
                        // Si cette case est la case blanche
                        if( position%taille!=(taille-1) && tab[position+1]==tab[caseBlanche]){
                            tmp = tab[position + 1];
                            tab[position + 1] = tab[position];
                            tab[position] = tmp;
                            tmp2 = img[position + 1];
                            img[position+1] = img[position];
                            img[position]=tmp2;
                        }
     
                        // Si on est pas sur la première colonne
                        // Si la case à gauche de la case selectionnée est la case blanche
                        if(position%taille!=0 && tab[position-1]==tab[caseBlanche]){
                            tmp= tab[position-1];
                            tab[position-1]=tab[position];
                            tab[position]=tmp;
                            tmp2=img[position-1];
                            img[position-1]=img[position];
                            img[position]=tmp2;
                        }
     
                        //S'il y a une case en haut de la case selectionnée
                        //Si elle est blanche
                        if(position-taille>=0 && tab[position-taille]==tab[caseBlanche])
                        {
                            tmp=tab[position-taille];
                            tab[position-taille]=tab[position];
                            tab[position]=tmp;
                            tmp2=img[position-taille];
                            img[position-taille]=img[position];
                            img[position]=tmp2;
                        }
     
                        //S'il y a une case en bas de la case selectionnée
                        //Si elle est blanche
                        if(position+taille <=(taille*taille) && tab[position+taille]==tab[caseBlanche]){
                            tmp=tab[position+taille];
                            tab[position+taille]=tab[position];
                            tab[position]=tmp;
                            tmp2=img[position+taille];
                            img[position+taille]=img[position];
                            img[position]=tmp2;
     
                        }
                        }
     
     
     
                });
            }
     
            private void shuffleImages(Bitmap[] images, ImageAdapter adapter) {
                int size = images.length-1;
                int cote = (int) Math.sqrt(size + 1);
                int position = size;
                int cpt = 0;
                int newPosition;
                Random r = new Random();
                int tmp;
     
                while (cpt < 500) {
                    newPosition = r.nextInt(size);
     
                    if (newPosition == position + 1 || newPosition == position -1 || newPosition == position + cote || newPosition == position - cote) {
                        adapter.exchangePositions(position, newPosition);
                        tmp = tab[position];
                        tab[position]=tab[newPosition];
                        tab[newPosition]=tmp;
                        position = newPosition;
     
     
     
                        cpt++;
                    }
                }
            }
     
            private Bitmap[] getCutImages(int factor, Bitmap image) {
                Bitmap[] bmp = new Bitmap[factor*factor];
                int width=image.getWidth();
                int height=image.getHeight();
                for(int i = 0; i < factor; i++){
                    for(int j = 0; j < factor; j++){
                        if (i*factor + j != factor*factor - 1) {
                            bmp[i * factor + j] = Bitmap.createBitmap(image, j * (width / factor), i * (height / factor), width / factor, height / factor);
                        } /*else {
                            bmp[i * factor + j] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.images), j * (width / factor), i * (height / factor), false);
                        }*/
                    }
                }
                return bmp;
            }
        }

    En vous remerciant d'avance pour l'aide que vous pourrez m'apporter !


    Anthony

  2. #2
    Membre actif Avatar de jmonga
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2014
    Messages
    175
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2014
    Messages : 175
    Points : 269
    Points
    269
    Par défaut
    Pourquoi utiliser un tableau simple au lieu d'une matrice

  3. #3
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 38
    Points : 10
    Points
    10
    Par défaut
    Très bonne question... Une lubie de ma part je crois... En fait j'ai trouvé plus simple de gérer des modulos avec la taille et la position pour identifier mes cases...

    J'ai réglé mon petit soucis de caseBlanche mais j'ai toujours un soucis pour ma dernière colonne (ça part un peu n'importe comment quand je clique sur une case de la dernière colonne, comme si elle n'était pas gérée...)

    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
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    package com.example.antho.taquin_progmobile;
     
    /**
     * Created by antho on 08/03/2017.
     */
     
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Chronometer;
    import android.widget.GridView;
     
    import java.util.Random;
     
     
    public class Jeu extends AppCompatActivity {
     
     
        private int tab[];
        private Bitmap img[];
        private GridView gridView;
        private ImageAdapter adapter;
     
        /**
         * Cette fonction permet d'instancier la page lors de sa création
         *
         * @param savedInstanceState
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.jeu);
     
            /////////////////////////////////////////Chronomètre///////////////////////////////////////////////////////////
            Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); // initie le chronomètre
            simpleChronometer.start(); // lance le chronomètre
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
            Bitmap image;
     
     
            /////////////////////////////////////////Gestion des intents///////////////////////////////////////////////////////
            Intent res = getIntent();
            final int taille = res.getIntExtra("taille", 3);
            final int imgId = res.getIntExtra("image", 1);
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     
            this.tab = new int[taille * taille];
            for (int i = 0; i < taille - i; i++) {
                tab[i] = i;
            }
     
     
            tab[tab.length-1]=666;
     
     
            switch (imgId) {
                case 0:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.pinkfloyd);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break;
                case 1:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_0);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break;
                case 2:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_1);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 3:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_2);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 4:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_3);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 5:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_4);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
     
                case 6:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_5);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 7:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_6);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                default:
                    ;
     
     
            }
     
     
     
            gridView = (GridView) findViewById(R.id.gridView);
            adapter = new ImageAdapter(this, img);
            //shuffleImages(img, adapter);
            gridView.setAdapter(adapter);
     
            gridView.setNumColumns(taille);
     
            // Colonne 1 gerée
     
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent,
                                        View v, int position, long id) {
     
                    int tmp;
                    Bitmap tmp2;
                    gridView.setAdapter(adapter);
     
     
                    // Si on à une case à droite de la case selectionnée
                    // Si cette case est la case blanche
                    if (position % taille != (taille - 1) && tab[position + 1] == 666) {
                        tmp = tab[position + 1];
                        tab[position + 1] = tab[position];
                        tab[position] = tmp;
                        tmp2 = img[position + 1];
                        img[position + 1] = img[position];
                        img[position] = tmp2;
                    }
     
                    // Si on est pas sur la première colonne
                    // Si la case à gauche de la case selectionnée est la case blanche
                    if (position % taille != 0 && tab[position - 1] == 666) {
                        tmp = tab[position - 1];
                        tab[position - 1] = tab[position];
                        tab[position] = tmp;
                        tmp2 = img[position - 1];
                        img[position - 1] = img[position];
                        img[position] = tmp2;
                    }
     
                    //S'il y a une case en haut de la case selectionnée
                    //Si elle est blanche
                    if (position - taille >= 0 && tab[position - taille] == 666) {
                        tmp = tab[position - taille];
                        tab[position - taille] = tab[position];
                        tab[position] = tmp;
                        tmp2 = img[position - taille];
                        img[position - taille] = img[position];
                        img[position] = tmp2;
                    }
     
                    //S'il y a une case en bas de la case selectionnée
                    //Si elle est blanche
                    if (position + taille <= (taille * taille) && tab[position + taille] == 666) {
                        tmp = tab[position + taille];
                        tab[position + taille] = tab[position];
                        tab[position] = tmp;
                        tmp2 = img[position + taille];
                        img[position + taille] = img[position];
                        img[position] = tmp2;
     
                    }
                }
     
     
            });
        }
     
        /**
         * @param images
         * @param adapter
         */
        private void shuffleImages(Bitmap[] images, ImageAdapter adapter) {
            int size = images.length - 1;
            int cote = (int) Math.sqrt(size + 1);
            int position = size;
            int cpt = 0;
            int newPosition;
            Random r = new Random();
            int tmp;
     
            while (cpt < 500) {
                newPosition = r.nextInt(size);
     
                if (newPosition == position + 1 || newPosition == position - 1 || newPosition == position + cote || newPosition == position - cote) {
                    adapter.exchangePositions(position, newPosition);
                    tmp = tab[position];
                    tab[position] = tab[newPosition];
                    tab[newPosition] = tmp;
                    position = newPosition;
     
     
                    cpt++;
                }
            }
        }
     
        private Bitmap[] getCutImages(int factor, Bitmap image) {
            Bitmap[] bmp = new Bitmap[factor * factor];
            int width = image.getWidth();
            int height = image.getHeight();
            for (int i = 0; i < factor; i++) {
                for (int j = 0; j < factor; j++) {
                    if (i * factor + j != factor * factor - 1) {
                        bmp[i * factor + j] = Bitmap.createBitmap(image, j * (width / factor), i * (height / factor), width / factor, height / factor);
                    } /*else {
                            bmp[i * factor + j] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.images), j * (width / factor), i * (height / factor), false);
                        }*/
                }
            }
            return bmp;
        }
    }

  4. #4
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 38
    Points : 10
    Points
    10
    Par défaut
    Il me reste plusieurs soucis de gestion de déplacement, notament sur la première et dernière colonne ainsi que sur les coins mais je ne vois aps trop pourquoi

  5. #5
    Membre à l'essai
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    38
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2015
    Messages : 38
    Points : 10
    Points
    10
    Par défaut
    Bonjour à tous,

    problème à moitié résolu, j'ai complètement refait le mouvements des cases en traitant chaque cas individuellement pour être sûr de ne rien oublier, ma dernière colonne bug : lorsque je tape sur un case de celle-ci, une ligne entière d'images disparait... Au lieu que cette case interchange sa place avec la case blanche...

    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
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    package com.example.antho.taquin_progmobile;
     
    /**
     * Created by antho on 08/03/2017.
     */
     
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Chronometer;
    import android.widget.GridView;
     
    import java.util.Random;
     
     
    public class Jeu extends AppCompatActivity {
     
     
        // private int tab[];
        private Bitmap img[];
        private GridView gridView;
        private ImageAdapter adapter;
        private int taillecarre;
     
        int tab[];
     
     
        //int positionB = tab.length-1;
     
        /**
         * Cette fonction permet d'instancier la page lors de sa création
         *
         * @param savedInstanceState
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.jeu);
     
            /////////////////////////////////////////Chronomètre///////////////////////////////////////////////////////////
            Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer); // initie le chronomètre
            simpleChronometer.start(); // lance le chronomètre
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
            Bitmap image;
     
     
            /////////////////////////////////////////Gestion des intents///////////////////////////////////////////////////////
            Intent res = getIntent();
            final int taille = res.getIntExtra("taille", 3);
            final int imgId = res.getIntExtra("image", 1);
            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
     
            this.tab = new int[taille * taille];
            for (int i = 0; i < taille - i; i++) {
                tab[i] = i;
            }
     
            taillecarre = taille * taille;
     
            //int[]resultat=new int[taillecarre-1];
     
           /* for (int i =0; i<taillecarre; i++){
                resultat[i]=i;
            }*/
     
     
            tab[tab.length - 1] = 666;
     
     
            switch (imgId) {
                case 0:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.pinkfloyd);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break;
                case 1:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_0);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break;
                case 2:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_1);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 3:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_2);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 4:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_3);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 5:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_4);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
     
                case 6:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_5);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                case 7:
                    image = BitmapFactory.decodeResource(this.getResources(), R.drawable.sample_6);
                    img = getCutImages(taille, Bitmap.createScaledBitmap(image, 500, 500, true));
                    break
                            ;
                default:
                    ;
     
     
            }
     
     
            gridView = (GridView) findViewById(R.id.gridView);
            adapter = new ImageAdapter(this, img);
            shuffleImages(img, adapter);
            gridView.setAdapter(adapter);
     
            gridView.setNumColumns(taille);
     
            // Colonne 1 gerée
     
     
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                                public void onItemClick(AdapterView<?> parent,
                                                                        View v, int position, long id) {
     
                                                    int tmp;
                                                    Bitmap tmp2;
                                                    gridView.setAdapter(adapter);
     
                                                    if (position % taille == 0) {
                                                        if ((position - taille) >= 0 && (position + taille) <= tab.length - 1) {
                                                            //deplacement possible droite, bas, haut
                                                            if (tab[position + 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + 1];
                                                                img[position + 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
     
                                                            } else if (tab[position + taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + taille];
                                                                img[position + taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + taille];
                                                                tab[position + taille] = tab[var];
     
                                                            } else if (tab[position - taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - taille];
                                                                img[position - taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - taille];
                                                                tab[position - taille] = tab[var];
     
                                                            }
     
                                                        } else if ((position - taille) <= 0) {
                                                            if (tab[position + 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + 1];
                                                                img[position + 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
     
                                                            } else if (tab[position + taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + taille];
                                                                img[position + taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + taille];
                                                                tab[position + taille] = tab[var];
     
                                                            }
                                                        } else if (position + taille >= tab.length - 1) {
     
                                                            if (tab[position + 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + 1];
                                                                img[position + 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
     
                                                            } else if (tab[position - taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - taille];
                                                                img[position - taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - taille];
                                                                tab[position - taille] = tab[var];
                                                            }
     
                                                        }
                                                    } else if (position % taille == (taille - 1)) {
                                                        if ((position - taille) >= 0 && (position + taille) <= tab.length - 1) {
                                                            //deplacement possible droite, bas, haut
                                                            if (tab[position - 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - 1];
                                                                img[position - 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
     
                                                            } else if (tab[position + taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + taille];
                                                                img[position + taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + taille];
                                                                tab[position + taille] = tab[var];
     
                                                            } else if (tab[position - taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - taille];
                                                                img[position - taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - taille];
                                                                tab[position - taille] = tab[var];
     
                                                            }
     
                                                        }
                                                        if (position - taille <= 0) {
                                                            if (tab[position - 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - 1];
                                                                img[position - 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - 1];
                                                                tab[position - 1] = tab[var];
     
                                                            } else if (tab[position + taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + taille];
                                                                img[position + taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + taille];
                                                                tab[position + taille] = tab[var];
     
                                                            }
     
                                                        } else if (position + taille >= tab.length - 1) {
                                                            if (tab[position - 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - 1];
                                                                img[position - 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - 1];
                                                                tab[position - 1] = tab[var];
     
                                                            } else if (tab[position - taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - taille];
                                                                img[position - taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - taille];
                                                                tab[position - taille] = tab[var];
     
                                                            }
                                                        }
                                                    } else {
                                                        if ((position - taille) >= 0 && (position + taille) <= tab.length - 1) {
                                                            //deplacement possible droite, bas, haut ,gauche
                                                            if (tab[position - 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - 1];
                                                                img[position - 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - 1];
                                                                tab[position - 1] = tab[var];
     
                                                            } else if (tab[position + taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + taille];
                                                                img[position + taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + taille];
                                                                tab[position + taille] = tab[var];
     
                                                            } else if (tab[position - taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - taille];
                                                                img[position - taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - taille];
                                                                tab[position - taille] = tab[var];
     
                                                            } else if (tab[position + 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + 1];
                                                                img[position + 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
                                                                ;
                                                            }
                                                        } else if ((position - taille) < 0) {
                                                            if (tab[position - 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - 1];
                                                                img[position - 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - 1];
                                                                tab[position - 1] = tab[var];
     
                                                            } else if (tab[position + taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + taille];
                                                                img[position + taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + taille];
                                                                tab[position + taille] = tab[var];
     
                                                            } else if (tab[position + 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + 1];
                                                                img[position + 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
     
                                                            }
                                                        } else if (position + taille > tab.length-1){
                                                            if (tab[position - 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - 1];
                                                                img[position - 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - 1];
                                                                tab[position - 1] = tab[var];
     
                                                            } else if (tab[position - taille] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position - taille];
                                                                img[position - taille] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position - taille];
                                                                tab[position - taille] = tab[var];
     
                                                            } else if (tab[position + 1] == 666) {
                                                                Bitmap image = img[position];
                                                                img[position] = img[position + 1];
                                                                img[position + 1] = image;
                                                                int var = tab[position];
                                                                tab[position] = tab[position + 1];
                                                                tab[position + 1] = tab[var];
     
                                                            }
                                                        }
     
     
                                                    }
                                                }
     
     
                                            }
     
     
            );
        }
     
        /**
         * @param images
         * @param adapter
         */
     
        private void shuffleImages(Bitmap[] images, ImageAdapter adapter) {
            int size = images.length - 1;
            int cote = (int) Math.sqrt(size + 1);
            int position = size;
            int cpt = 0;
            int newPosition;
            Random r = new Random();
            int tmp;
     
            while (cpt < 500) {
                newPosition = r.nextInt(size);
     
                if (newPosition == position + 1 || newPosition == position - 1 || newPosition == position + cote || newPosition == position - cote) {
                    adapter.exchangePositions(position, newPosition);
                    tmp = tab[position];
                    tab[position] = tab[newPosition];
                    tab[newPosition] = tmp;
                    position = newPosition;
     
     
                    cpt++;
                }
            }
        }
     
        private Bitmap[] getCutImages(int factor, Bitmap image) {
            Bitmap[] bmp = new Bitmap[factor * factor];
            int width = image.getWidth();
            int height = image.getHeight();
            for (int i = 0; i < factor; i++) {
                for (int j = 0; j < factor; j++) {
                    if (i * factor + j != factor * factor - 1) {
                        bmp[i * factor + j] = Bitmap.createBitmap(image, j * (width / factor), i * (height / factor), width / factor, height / factor);
                    } /*else {
                            bmp[i * factor + j] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.images), j * (width / factor), i * (height / factor), false);
                        }*/
                }
            }
            return bmp;
        }
     
    }

Discussions similaires

  1. Problème affichage label (case blanche)
    Par Joseldsm dans le forum Visual Studio
    Réponses: 1
    Dernier message: 15/02/2016, 12h32
  2. résolution du jeu Taquin avec la methode A*
    Par goldray dans le forum Intelligence artificielle
    Réponses: 1
    Dernier message: 19/02/2013, 12h50
  3. [QtSql] Case blanche à l'édition d'un combobox sur clé étrangère
    Par tyrtamos dans le forum PyQt
    Réponses: 2
    Dernier message: 23/09/2012, 12h54
  4. [phpMyAdmin] probleme acces phpMyAdmin 3.3.1, identification -> page blanche
    Par embargo dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 1
    Dernier message: 09/04/2010, 00h22
  5. Réponses: 2
    Dernier message: 27/01/2005, 11h47

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