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
|
package com.aviy.memory;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import com.aviy.memory.Manager.UpdateCardsHandler;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class Jeu extends Activity{
public int largeur;
public int longueur;
private Card firstCard;
private Card seconedCard;
private Card carte1;
private Card carte2;
private Card carte3;
private Card carte4;
private Card carte5;
private Card carte6;
private Card carte7;
private Card carte8;
private Card carte9;
private Card carte10;
private Card carte11;
private Card carte12;
private Card carte13;
private Card carte14;
private Card carte15;
private Card carte16;
private int backImage;
private List<Card> tableaudepart;
private List<Card> tableaufinal;
private List<Button> boutton;
Button button1;
Button button2;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;
Button button9;
Button button10;
Button button11;
Button button12;
Button button13;
Button button14;
Button button15;
Button button16;
private static Object lock = new Object();
private UpdateCardsHandler handler;
public void creationcarte(Button button,int IDpaire, int y, int x, int image){
//on crée les instances
carte1=new Card(button,x,y,IDpaire=1 ,image = R.drawable.card1);
carte2=new Card(button,x,y,IDpaire=1 ,image = R.drawable.card2);
carte3=new Card(button,x,y,IDpaire=2 ,image = R.drawable.card3);
carte4=new Card(button,x,y,IDpaire=2 ,image = R.drawable.card4);
carte5=new Card(button,x,y,IDpaire=3 ,image = R.drawable.card5);
carte6=new Card(button,x,y,IDpaire=3 ,image = R.drawable.card6);
carte7=new Card(button,x,y,IDpaire=4 ,image = R.drawable.card7);
carte8=new Card(button,x,y,IDpaire=4 ,image = R.drawable.card8);
carte9=new Card(button,x,y,IDpaire=5 ,image = R.drawable.card9);
carte10=new Card(button,x,y,IDpaire=5 ,image = R.drawable.card10);
carte11=new Card(button,x,y,IDpaire=6 ,image = R.drawable.card11);
carte12=new Card(button,x,y,IDpaire=6 ,image = R.drawable.card12);
carte13=new Card(button,x,y,IDpaire=7 ,image = R.drawable.card13);
carte14=new Card(button,x,y,IDpaire=7 ,image = R.drawable.card14);
carte15=new Card(button,x,y,IDpaire=8 ,image = R.drawable.card15);
carte16=new Card(button,x,y,IDpaire=8 ,image = R.drawable.card16);
tableaudepart = new ArrayList<Card>();
tableaudepart.add(carte1);
tableaudepart.add(carte2);
tableaudepart.add(carte3);
tableaudepart.add(carte4);
tableaudepart.add(carte5);
tableaudepart.add(carte6);
tableaudepart.add(carte7);
tableaudepart.add(carte8);
tableaudepart.add(carte9);
tableaudepart.add(carte10);
tableaudepart.add(carte11);
tableaudepart.add(carte12);
tableaudepart.add(carte13);
tableaudepart.add(carte14);
tableaudepart.add(carte15);
tableaudepart.add(carte16);
}
public void placement(){
//on choisis une place dans le tableau pour chaque carte
setContentView(R.layout.jeux1);
for (int i=16; i>0; i--){
int a =1;
int b=i;
int random = (int)(Math.random() * (b-a));
tableaufinal.add(tableaudepart.get(random));
tableaudepart.remove(random);
}
}
public void booton(){
boutton = new ArrayList<Button>();
boutton.add(button1);
boutton.add(button2);
boutton.add(button3);
boutton.add(button4);
boutton.add(button5);
boutton.add(button6);
boutton.add(button7);
boutton.add(button8);
boutton.add(button9);
boutton.add(button10);
boutton.add(button11);
boutton.add(button12);
boutton.add(button13);
boutton.add(button14);
boutton.add(button15);
boutton.add(button16);
}
public void turn(){
View.OnClickListener first ;
first = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (button1.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(1);
int u = t.IDpaire;
backImage = t.image;
button1.setBackgroundColor(backImage);
}
else if (button2.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(2);
int u = t.IDpaire;
backImage = t.image;
button2.setBackgroundColor(backImage);
}
else if (button3.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(3);
int u = t.IDpaire;
backImage = t.image;
button3.setBackgroundColor(backImage);
}
else if (button4.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(4);
int u = t.IDpaire;
backImage = t.image;
button4.setBackgroundColor(backImage);
}
else if (button5.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(5);
int u = t.IDpaire;
backImage = t.image;
button5.setBackgroundColor(backImage);
}
else if (button6.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(6);
int u = t.IDpaire;
backImage = t.image;
button6.setBackgroundColor(backImage);
}
else if (button7.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(7);
int u = t.IDpaire;
backImage = t.image;
button7.setBackgroundColor(backImage);
}
else if (button8.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(8);
int u = t.IDpaire;
backImage = t.image;
button8.setBackgroundColor(backImage);
}
else if (button9.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(9);
int u = t.IDpaire;
backImage = t.image;
button9.setBackgroundColor(backImage);
}
else if (button10.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(10);
int u = t.IDpaire;
backImage = t.image;
button10.setBackgroundColor(backImage);
}
else if (button11.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(11);
int u = t.IDpaire;
backImage = t.image;
button11.setBackgroundColor(backImage);
}
else if (button12.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(12);
int u = t.IDpaire;
backImage = t.image;
button12.setBackgroundColor(backImage);
}
else if (button13.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(13);
int u = t.IDpaire;
backImage = t.image;
button13.setBackgroundColor(backImage);
}
else if (button14.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(14);
int u = t.IDpaire;
backImage = t.image;
button14.setBackgroundColor(backImage);
}
else if (button15.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(15);
int u = t.IDpaire;
backImage = t.image;
button15.setBackgroundColor(backImage);
}
else if (button16.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(16);
int u = t.IDpaire;
backImage = t.image;
button16.setBackgroundColor(backImage);
}
View.OnClickListener second ;
second = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (button1.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(1);
int p = t.IDpaire;
backImage = t.image;
button1.setBackgroundColor(backImage);
}
else if (button2.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(2);
int p = t.IDpaire;
backImage = t.image;
button2.setBackgroundColor(backImage);
}
else if (button3.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(3);
int p = t.IDpaire;
backImage = t.image;
button3.setBackgroundColor(backImage);
}
else if (button4.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(4);
int p = t.IDpaire;
backImage = t.image;
button4.setBackgroundColor(backImage);
}
else if (button5.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(5);
int p = t.IDpaire;
backImage = t.image;
button5.setBackgroundColor(backImage);
}
else if (button6.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(6);
int p = t.IDpaire;
backImage = t.image;
button6.setBackgroundColor(backImage);
}
else if (button7.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(7);
int p = t.IDpaire;
backImage = t.image;
button7.setBackgroundColor(backImage);
}
else if (button8.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(8);
int p = t.IDpaire;
backImage = t.image;
button8.setBackgroundColor(backImage);
}
else if (button9.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(9);
int p = t.IDpaire;
backImage = t.image;
button9.setBackgroundColor(backImage);
}
else if (button10.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(10);
int p = t.IDpaire;
backImage = t.image;
button10.setBackgroundColor(backImage);
}
else if (button11.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(11);
int p = t.IDpaire;
backImage = t.image;
button11.setBackgroundColor(backImage);
}
else if (button12.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(12);
int p = t.IDpaire;
backImage = t.image;
button12.setBackgroundColor(backImage);
}
else if (button13.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(13);
int p = t.IDpaire;
backImage = t.image;
button13.setBackgroundColor(backImage);
}
else if (button14.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(14);
int p = t.IDpaire;
backImage = t.image;
button14.setBackgroundColor(backImage);
}
else if (button15.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(15);
int p = t.IDpaire;
backImage = t.image;
button15.setBackgroundColor(backImage);
}
else if (button16.getId()==((Button)v).getId())
{
Card t = tableaufinal.get(16);
int p = t.IDpaire;
backImage = t.image;
button16.setBackgroundColor(backImage);
}
};
};
}
};
};
public void chek(){
TimerTask tt = new TimerTask() {
@Override
public void run() {
try{
synchronized (lock) {
handler.sendEmptyMessage(0);
}
}
catch (Exception e) {
Log.e("E1", e.getMessage());
}
}
};
Timer t = new Timer(false);
t.schedule(tt, 1300);
if(p == u){
first.button.setVisibility(View.INVISIBLE);
second.button.setVisibility(View.INVISIBLE);
}
else {
//sinon on remet l'image de fond
second.button.setBackgroundDrawable(backImage);
first.button.setBackgroundDrawable(backImage);
}
}
} |
Partager