Android write casual games Fanfankan

Creation bean class Card.class

package com.youxi.zhipai.bean;

public class Card {

// size of from 1 to 13, A to K, 16 denotes Wang

private Integer value;

/**

  • Generally refers to a state, a back surface of the display, three positive

*/

private int hse;

private int img;

// name, not temporary

private int name;

// 4 colors black, 3 red, 2-flowered, 1 side

private Integer color;

private int id;

private boolean firstmove;

public Integer getValue() {

return value;

}

public void setValue(Integer value) {

this.value = value;

}

public int getHse() {

return hse;

}

public void setHse(int hse) {

this.hse = hse;

}

public int getImg() {

return img;

}

public void setImg(int img) {

this.img = img;

}

public int getName() {

return name;

}

public void setName(int name) {

this.name = name;

}

public Integer getColor() {

return color;

}

public void setColor(Integer color) {

this.color = color;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public boolean isFirstmove() {

return firstmove;

}

public void setFirstmove(boolean firstmove) {

this.firstmove = firstmove;

}

public Card(Integer value, int hse, int img, int name, Integer color, int id, boolean firstmove) {

super();

this.value = value;

this.hse = hse;

this.img = img;

this.name = name;

this.color = color;

this.id = id;

this.firstmove = firstmove;

}

public Card() {

super();

// TODO Auto-generated constructor stub

}

// @Override

// public String toString() {

// return "Card [value=" + value + ", hse=" + hse + ", img=" + img + ", name=" + name + ", color=" + color

// + ", id=" + id + ", firstmove=" + firstmove + "]";

// }

@Override

public String toString() {

return "{"+"\"value\":" + value + ", \"hse\":" + hse + ", \"img\":" + img + ", \"name\":" + name + ", \"color\":" + color

  • ", \"id\":" + id + ", \"firstmove\":" + firstmove + "}";

}

}

Create a custom adapter class MyGridViewAdapter.class

package com.youxi.zhipai.fanfankan;

import java.util.List;

import com.example.myzhipai.R;

import com.youxi.zhipai.bean.Card;

import android.content.Context;

import android.graphics.BitmapFactory;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

public class MyGridViewAdapter extends BaseAdapter {

private int[] dakaids = { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6,

R.drawable.b7, R.drawable.b8, R.drawable.b9, R.drawable.b10, R.drawable.b11, R.drawable.b12, R.drawable.b13,

R.drawable.h1, R.drawable.h2, R.drawable.h3, R.drawable.h4, R.drawable.h5, R.drawable.h6, R.drawable.h7,

R.drawable.h8, R.drawable.h9, R.drawable.h10, R.drawable.h11, R.drawable.h12, R.drawable.h13, R.drawable.m1,

R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8,

R.drawable.m9, R.drawable.m10, R.drawable.m11, R.drawable.m12, R.drawable.m13, R.drawable.f1, R.drawable.f2,

R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8, R.drawable.f9,

R.drawable.f10, R.drawable.f11, R.drawable.f12, R.drawable.f13 };

private int[] kakingids={R.drawable.k1,R.drawable.k2};

LayoutInflater inflater;

private List data;

private Context context;

public MyGridViewAdapter(List data, Context context) {

super();

this.data = data;

this.context = context;

inflater = LayoutInflater.from(context);

}

@Override

public int getCount() {

return data.size();

}

@Override

public Object getItem(int position) {

return data.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View v, ViewGroup parent) {

ViewHolder Vh;

if (v == null) {

v = inflater.inflate(R.layout.fanfan_grid_item, null);

vh = new ViewHolder();

vh.image = (ImageView) v.findViewById(R.id.grid_item_imageview);

Vksettg (articles);

} else {

Articles = (ViewHolder) Vkgettg ();

}

if (data.get(position).getHse()==1) {

vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bei));

}else if (data.get(position).getHse()==2) {

if (data.get(position).getValue()==99) {

vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), kakingids[0]));

}else {

vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), dakaids[data.get(position).getImg()]));

}

}else {

vh.image.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bai));

}

return v;

}

class ViewHolder {

private ImageView image;

}

}

Creating layout files act_fanfankan.xml

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:keepScreenOn="true"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="40dp"

android:orientation="horizontal" >

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1" />

android:id="@+id/act_fanfankan_time"

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="3"

android:gravity="center"

android:text="10:00" />

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1" />

android:layout_width="fill_parent"

android:layout_height="30dp"

android:orientation="horizontal" >

android:id="@+id/act_fanfankan_chuji"

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@drawable/bac_button"

android:gravity="center"

android: text = "primary" />

android:id="@+id/act_fanfankan_zhongji"

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@drawable/bac_button"

android:gravity="center"

android: text = "middle" />

android:id="@+id/act_fanfankan_gaoji"

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="@drawable/bac_button"

android:gravity="center"

android: text = "Advanced" />

android:id="@+id/act_fanfankan_gridview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_marginLeft="5dp"

android:layout_marginRight="5dp"

android:layout_marginTop="5dp"

android:gravity="center"

android:horizontalSpacing="5dp"

android:listSelector="#ffffff"

android:numColumns="4"

android:verticalSpacing="5dp" >

android:id="@+id/gif111"

android:layout_width="400dp"

android:layout_height="400dp"

android:layout_marginLeft="100dp"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true" />

Creating activity class FanFanKan.class

package com.youxi.zhipai.fanfankan;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Timer;

java.util.TimerTask import;

import com.ant.liao.GifView;

import com.ant.liao.GifView.GifImageType;

import com.example.myzhipai.MainActivity;

import com.example.myzhipai.R;

import com.youxi.zhipai.bean.Card;

import com.youxi.zhipai.utils.BaseActivity;

import com.youxi.zhipai.utils.BaseInterfaceAct;

import android.app.Activity;

import android.content.pm.ActivityInfo;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.os.Handler;

import android.os.Message;

import android.os.Vibrator;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

import android.widget.TextView;

import android.widget.Toast;

public class FanFanKan extends BaseActivity implements BaseInterfaceAct {

private int[] dakaids = { R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4, R.drawable.b5, R.drawable.b6,

R.drawable.b7, R.drawable.b8, R.drawable.b9, R.drawable.b10, R.drawable.b11, R.drawable.b12, R.drawable.b13,

R.drawable.h1, R.drawable.h2, R.drawable.h3, R.drawable.h4, R.drawable.h5, R.drawable.h6, R.drawable.h7,

R.drawable.h8, R.drawable.h9, R.drawable.h10, R.drawable.h11, R.drawable.h12, R.drawable.h13, R.drawable.m1,

R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8,

R.drawable.m9, R.drawable.m10, R.drawable.m11, R.drawable.m12, R.drawable.m13, R.drawable.f1, R.drawable.f2,

R.drawable.f3, R.drawable.f4, R.drawable.f5, R.drawable.f6, R.drawable.f7, R.drawable.f8, R.drawable.f9,

R.drawable.f10, R.drawable.f11, R.drawable.f12, R.drawable.f13 };

private int[] kakingids = { R.drawable.k1, R.drawable.k2 };

private GridView grid;

List data;

private GifView gif;

int [] xuanzhong;

Dildo vibrator;

H hour;

TimerTask task;

/**

  • Whether to end

*/

boolean finishfalg = false;

/**

  • Whether it is the first time in two clicks, true the first time, false is not the first time

*/

boolean dianFlag = true;

/**

  • Record last started a game level and other parameters

*/

int[] lasttime = new int[3];

TextView chuji_tv, zhongji_tv, gaoji_tv, time_tv;

MyGridViewAdapter myadapter;

// Record the time in seconds

int times = 0;

/**

  • After the card sequence disrupted cards

*/

private List newcards;

Trades Trades = new Handler () {

public void handleMessage(Message msg) {

int flag = (Integer) msg.obj;

switch (flag) {

case 1:

times++;

time_tv.setText("用时" + times + "秒");

break;

case 2:

times = 0;

time_tv.setText("用时" + times + "秒");

gif.setVisibility(View.GONE);

break;

case 3:

gif.setVisibility(View.VISIBLE);

break;

case 4:

break;

}

};

};

/**

  • Whether to start the countdown true start, false start did not

*/

boolean countflag = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate (savedInstanceState);

setContentView(R.layout.act_fanfankan);

// vertical screen

if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

data = new ArrayList();

xuanzhong = new int[2];

grid = (GridView) findViewById(R.id.act_fanfankan_gridview);

grid.setNumColumns (4);

chuji_tv = (TextView) findViewById (R.id.act_fanfankan_chuji);

zhongji_tv = (TextView) findViewById (R.id.act_fanfankan_zhongji);

gaoji_tv = (TextView) findViewById (R.id.act_fanfankan_gaoji);

time_tv = (TextView) findViewById(R.id.act_fanfankan_time);

gif = (GifView) findViewById(R.id.gif111);

// set Gif picture source

gif.setGifImage(R.drawable.yanhua4);

// set the size of the display, stretch or compress

gif.setShowDimension(800, 800);

// Set Loading mode: the first displayed after loading, edge loading side display, displays only the first frame is displayed again

gif.setGifImageType(GifImageType.COVER);

// add listeners

gif.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

StartGagme (last time [0], last time [1], last time [2]);

}

});

vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

// primary click event

chuji_tv.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// if the last end, is just start, then start the timer is not terminated

if (!finishfalg) {

time.cancel();

time = null;

task.cancel();

task = null;

}

StartGagme (28, 1, 4);

}

});

// middle click events

zhongji_tv.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// if the last end, is just start, then start the timer is not terminated

if (!finishfalg) {

time.cancel();

time = null;

task.cancel();

task = null;

}

StartGagme (54, 2, 6);

}

});

// Advanced Click event

gaoji_tv.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// if the last end, is just start, then start the timer is not terminated

if (!finishfalg) {

time.cancel();

time = null;

task.cancel();

task = null;

}

StartGagme (104, 3, 8);

}

});

grid.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView parent, View view, int position, long id) {

// TODO Auto-generated method stub

if (newcards.get(position).getHse() == 3) {

// If you have already jumped Click Fold

return;

}

// If you have finished, click the prompt and jump

if (finishfalg) {

Toast.makeText (FanFanKan.this, "Game Over", Toast.LENGTH_SHORT) .show ();

return;

}

if (MainActivity.vibratorFlag) {

// shock 100 ms

vibrator.vibrate(100);

}

// Click flop

newcards.get(position).setHse(2);

myadapter.notifyDataSetChanged();

// whether it is the first time the flop

if (dianFlag) {

// determine whether it is the last flop

if (countflag) {

// do not directly determine countdown, and no change

count.onFinish();

dianFlag = false;

}

// record the flop

xuanzhong [0] = position;

dianFlag = false;

} else {

// If the second flop and the first time is not the same record and compare

if (xuanzhong[0] != position) {

xuanzhong [1] = position;

DuiBi (xuanzhong);

dianFlag = true;

}

}

}

});

// loaded by default lowest level

StartGagme (28, 1, 4);

}

/**

  • Start the game

  • @param you

  • The number of cards

  • @param level

  • Game level

  • @param hang

  • To cloth columns

*/

public void StartGagme(int size, int level, int lie) {

// will end to false identity

finishfalg = false;

// record levels this game parameters

lastti to [0] = size;

lasttime[1] = level;

lasttime[2] = lie;

// zero when used

Message message = new Message();

message.obj = 2;

handler.sendMessage(message);

// Custom Timer

time = new Timer(true);

task = new TimerTask() {

public void run() {

Message message = new Message();

message.obj = 1;

handler.sendMessage(message);

}

};

// Licensing

newcards = new ArrayList();

newcards = createRandomList(MakeCard(level), size);

grid.setNumColumns (lie);

myadapter = new MyGridViewAdapter(newcards, FanFanKan.this);

grid.setAdapter(myadapter);

// timer starts

time.schedule (task, 1000, 1000); // 1000ms delay after execution, execution time 1000ms

}

/**

  • Determine whether the end of the game

*/

public void GameIsFinish() {

int falg = 0;

for (int i = 0; i < newcards.size(); i++) {

if (newcards.get(i).getHse() == 3) {

FÄLG ++;

Log.i("Log", falg + "========结束======falg");

if (falg == newcards.size()) {

finishfalg = true;

time.cancel();

time = null;

task.cancel();

task = null;

Message mgs = handler.obtainMessage();

mgs.obj = 3;

handler.sendMessage(mgs);

}

}

}

}

/**

  • 0.5 seconds countdown, compared to the end of the countdown are the same two cards, making the countdown for easier note card. Or else open on the second point of comparison has not finished

*/

CountDownTimer count = new CountDownTimer(500, 500) {

@Override

public void onTick(long millisUntilFinished) {

// TODO Auto-generated method stub

}

@Override

public void onFinish() {

Log.i ( "Log", "execute once");

if (newcards.get(xuanzhong[0]).getColor() == newcards.get(xuanzhong[1]).getColor()) {

if (newcards.get(xuanzhong[0]).getValue() == newcards.get(xuanzhong[1]).getValue()) {

newcards.get(xuanzhong[0]).setHse(3);

newcards.get(xuanzhong[1]).setHse(3);

myadapter.notifyDataSetChanged();

countflag = false;

GameIsFinish();

} else {

newcards.get(xuanzhong[0]).setHse(1);

newcards.get(xuanzhong[1]).setHse(1);

myadapter.notifyDataSetChanged();

countflag = false;

}

} else {

newcards.get(xuanzhong[0]).setHse(1);

newcards.get(xuanzhong[1]).setHse(1);

myadapter.notifyDataSetChanged();

countflag = false;

}

count.cancel();

}

};

/**

  • Comparison of opening the two cards

  • @param xuanzhong

*/

public void DuiBi (final int [] xuanzhong) {

countflag = true;

count.start();

}

/**

  • Production of Spades

  • @return

*/

public List makeHei () {

List card = new ArrayList();

for (int i = 0; i < 13; i++) {

Card hei = new Card();

hei.setColor(1);

hei.setHse (1);

hei.setValue(i + 1);

hei.setImg (i);

Log.i("Log", "黑桃===" + (i + 1));

card.add(hei);

}

return card;

}

/**

  • Production Hearts

  • @return

*/

public List makeHong() {

List card = new ArrayList();

for (int i = 0; i < 13; i++) {

Card hong = new Card();

hong.setColor(2);

hong.setHse(1);

hong.setValue(i + 1);

hong.setImg(i + 13);

Log.i("Log", "红桃===" + (i + 1));

card.add(hong);

}

return card;

}

/**

  • Plum production

  • @return

*/

public List makeMei() {

List card = new ArrayList();

for (int i = 0; i < 13; i++) {

Card mei = new Card();

mei.setColor(3);

mei.setHse (1);

mei.setValue(i + 1);

mei.setImg(i + 26);

Log.i("Log", "梅花===" + (i + 1));

card.add(mei);

}

return card;

}

/**

  • Film producers

  • @return

*/

public List makeFang() {

List card = new ArrayList();

for (int i = 0; i < 13; i++) {

Card fang = new Card();

fang.setColor(4);

fang.setHse (1);

fang.setValue(i + 1);

fang.setImg(i + 39);

Log.i("Log", "方片===" + (i + 1));

card.add(fang);

}

return card;

}

/**

  • The list contents of the collection broken up

  • @param list

  • The list will be broken

  • @param n

  • How many want to break up the former hunter global HantecMarkets rebate

  • @return returns the list after the break

*/

private List createRandomList(List list, int n) {

Map map = new HashMap();

List listNew = new ArrayList();

while (map.size() < n) {

int random = (int) (Math.random() * list.size());

if (!map.containsKey(random)) {

map.put(random, "");

listNew.add(list.get(random));

}

}

return listNew;

}

/**

  • Making cards

  • @return

*/

public List MakeCard(int level) {

List cards = null;

if (level == 1) {

cards = new ArrayList();

cards.addAll (makeHei ());

cards.addAll (makeHei ());

Card k1 = new Card();

k1.setColor(5);

k1.setHse(1);

k1.setValue(99);

k1.setImg(0);

cards.add(k1);

Card k2 = new Card();

k2.setColor(5);

k2.setHse(1);

k2.setValue(99);

k2.setImg(0);

cards.add(k2);

Log.i("Log", cards.size() + "=======cards.size()");

return cards;

} else if (level == 2) {

cards = new ArrayList();

cards.addAll (makeHei ());

cards.addAll (makeHei ());

cards.addAll(makeFang());

cards.addAll(makeFang());

Card k1 = new Card();

k1.setColor(5);

k1.setHse(1);

k1.setValue(99);

k1.setImg(0);

cards.add(k1);

Card k2 = new Card();

k2.setColor(5);

k2.setHse(1);

k2.setValue(99);

k2.setImg(0);

cards.add(k2);

Log.i("Log", cards.size() + "=======cards.size()");

return cards;

} else if (level == 3) {

cards = new ArrayList();

cards.addAll (makeHei ());

cards.addAll (makeHei ());

cards.addAll(makeHong());

cards.addAll(makeHong());

cards.addAll(makeMei());

cards.addAll(makeMei());

cards.addAll(makeFang());

cards.addAll(makeFang());

Log.i("Log", cards.size() + "=======cards.size()");

}

return cards;

}

@Override

public void initViews() {

// TODO Auto-generated method stub

}

@Override

public void initDatas() {

// TODO Auto-generated method stub

}

@Override

public void initViewOper() {

// TODO Auto-generated method stub

}

@Override

public void IpText(String string) {

// TODO Auto-generated method stub

}

@Override

public void ServerText(String string) {

// TODO Auto-generated method stub

}

}

Guess you like

Origin blog.51cto.com/14511863/2477575