Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.youdothemath;
- import android.content.Context;
- import android.content.res.Resources;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import java.util.ArrayList;
- import java.util.Collections;
- public class GridAdapter extends BaseAdapter {
- private Context mContext;
- private Integer mCols, mRows;
- private ArrayList<String> arrPict; // массив картинок
- private String PictureCollection; // префикс набора картинок
- private Resources mRes; // ресурсы приложения
- private static final int CELL_CLOSE = 0;
- private static final int CELL_OPEN = 1;
- private static final int CELL_DELETE = -1;
- private static enum Status {CELL_CLOSE, CELL_OPEN, CELL_DELETE};
- private ArrayList<Status> arrStatus; // состояние ячеек
- public GridAdapter(Context context, int cols, int rows) {
- mContext = context;
- mCols = cols;
- mRows = rows;
- arrPict = new ArrayList<>();
- // определяем префикс
- PictureCollection = "flags";
- // получаем все ресурсы приожение
- mRes = mContext.getResources();
- // метод, заполняющий массив картинок
- makePictArray();
- // метод, устанавливающий всем ячейкам статус CELL_CLOSE
- closeAllCells();
- }
- public void checkOpenCells() {
- int first = arrStatus.indexOf(Status.CELL_OPEN);
- int second = arrStatus.lastIndexOf(Status.CELL_OPEN);
- if (first == second)
- return;
- if (arrPict.get(first).equals(arrPict.get(second))) {
- arrStatus.set(first, Status.CELL_DELETE);
- arrStatus.set(second, Status.CELL_DELETE);
- }
- else
- {
- arrStatus.set(first, Status.CELL_CLOSE);
- arrStatus.set(second, Status.CELL_CLOSE);
- }
- return;
- }
- public void openCell(int position) {
- if (arrStatus.get(position) != Status.CELL_DELETE)
- arrStatus.set(position, Status.CELL_OPEN);
- notifyDataSetChanged();
- return;
- }
- public boolean checkGameOver() {
- if (arrStatus.indexOf(Status.CELL_CLOSE) < 0)
- return true;
- return false;
- }
- private void closeAllCells() {
- arrStatus.clear();
- for (int i = 0; i < mCols*mRows; i++)
- arrStatus.add(Status.CELL_CLOSE);
- }
- private void makePictArray() {
- // очищаем массив
- arrPict.clear();
- // добавляем
- for (int i = 1; i <= ((mCols * mRows) / 2); i++)
- {
- arrPict.add(PictureCollection + Integer.toString(i));
- arrPict.add(PictureCollection + Integer.toString(i));
- }
- // перемешиваем
- Collections.shuffle(arrPict);
- }
- @Override
- public int getCount() {
- return mCols * mRows;
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ImageView view; // для вывода картинки
- if (convertView == null)
- view = new ImageView(mContext);
- else
- view = (ImageView)convertView;
- switch (arrStatus.get(position)) {
- case CELL_OPEN:
- // получаем идентификатор ресурса для картинки,
- // которая находится в массиве на позиции position
- Integer drawableId = mRes.getIdentifier(arrPict.get(position), "drawable", mContext.getPackageName());
- view.setImageResource(drawableId);
- break;
- case CELL_CLOSE:
- view.setImageResource(R.drawable.planet3);
- break;
- default:
- //view.setImageResource(R.drawable.none);
- }
- return view;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment