开发提示及要求:
1,界面使用TextView、Button以及三个ImageView,一开始三个ImageView都默认显示扑克牌背面,当用户选择了其中一张牌时,三个ImageView同时翻到正面,程序会依照选择的对错通过Toast消息提示出来,并可以通过点击“重新玩一次”来重新开始游戏。
2,当游戏开始时,玩家从三张牌中单击一张,然后程序会一次翻开所有牌面,并将玩家没选择的牌面以灰暗效果处理。
3,将三张图片的id存入数组,定义一个方法将数组中的id顺序做随机的调换,以制造洗牌的效果,三个ImageView默认加载图片都为扑克牌背面。
4,代码量控制在100行左右。
参考代码:(如有雷同,纯属巧合)
main.xml 开始
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#111111" tools:context=".MainActivity" > <TextView android:id="@+id/tvcai" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="72dp" android:text="@string/cai" android:textColor="#eeeeee" android:textSize="18sp" /> <ImageView android:id="@+id/puke1" android:layout_width="71dp" android:layout_height="96dp" android:layout_alignTop="@+id/puke3" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/puke3"/> <ImageView android:id="@+id/puke2" android:layout_width="71dp" android:layout_height="96dp" android:layout_alignParentLeft="true" android:layout_below="@+id/tvcai" android:layout_marginLeft="20dp" android:layout_marginTop="36dp" /> <ImageView android:id="@+id/puke3" android:layout_width="71dp" android:layout_height="96dp" android:layout_alignTop="@+id/puke2" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/puke2" /> <Button android:id="@+id/btnplayagian" android:layout_width="120dp" android:layout_height="30dp" android:layout_below="@+id/puke2" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:background="#cdcdcd" android:text="重新玩一次" /> </RelativeLayout>
main.xml 结束
MainActivity.java 开始
package com.ming.puke; import java.util.ArrayList; import java.util.Random; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private Button btnplayagian;// 再玩一次 private ImageView puke1, puke2, puke3;// 三张扑克 private Drawable bg;// 扑克背面(用于判断是否开牌) private int[] pks = { R.drawable.p01, R.drawable.p02, R.drawable.p03 };// 存放3张扑克 private ArrayList<Integer> ps = new ArrayList<Integer>();// 存放洗过的扑克(随机排列) protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); puke1 = (ImageView) findViewById(R.id.puke1); puke2 = (ImageView) findViewById(R.id.puke2); puke3 = (ImageView) findViewById(R.id.puke3); btnplayagian = (Button) findViewById(R.id.btnplayagian); puke1.setOnClickListener(l); puke2.setOnClickListener(l); puke3.setOnClickListener(l); btnplayagian.setOnClickListener(l); } protected void onResume() { randomPuke(); super.onResume(); } OnClickListener l = new OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.puke1: showPuke(0); // 将玩家没选择的牌面以灰暗效果处理,设置透明度,取值范围为0~255,数值越小越透明。 puke2.setAlpha(100); puke3.setAlpha(100); break; case R.id.puke2: showPuke(1); puke1.setAlpha(100); puke3.setAlpha(100); break; case R.id.puke3: showPuke(2); puke1.setAlpha(100); puke2.setAlpha(100); break; case R.id.btnplayagian: randomPuke(); break; } } }; /** 洗牌 */ public void randomPuke() { // 所有牌翻到背面 puke1.setImageResource(R.drawable.p04); puke2.setImageResource(R.drawable.p04); puke3.setImageResource(R.drawable.p04); bg = puke1.getDrawable(); // 实例化清空(收牌) ps = new ArrayList<Integer>(); // 随机填充(发牌) do { int item = new Random().nextInt(3);// 随机出{0,1,2}中其中一个数 if (!ps.contains(pks[item])) {// 如果桌上没有这张牌才发这张牌 ps.add(pks[item]); } } while (ps.size() < 3);// 发完3张牌就不发牌了 } /** 开牌 */ public void showPuke(int p) { // 如果牌还没开才能开牌 if (puke1.getDrawable() == bg) { puke1.setImageResource((Integer) ps.get(0)); puke2.setImageResource((Integer) ps.get(1)); puke3.setImageResource((Integer) ps.get(2)); Toast.makeText( MainActivity.this, R.drawable.p01 == (Integer) ps.get(p) ? "恭喜你猜对了" : "遗憾你猜错了", 1000).show(); } } }
MainActivity.java 结束