纸牌游戏之五 主界面类
【摘要】
package com.example.azhipai; import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import ...
-
package com.example.azhipai;
-
-
import java.util.ArrayList;
-
import java.util.List;
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.app.AlertDialog;
-
import android.app.AlertDialog.Builder;
-
import android.content.ContentValues;
-
import android.content.DialogInterface;
-
import android.content.DialogInterface.OnClickListener;
-
import android.database.Cursor;
-
import android.database.sqlite.SQLiteDatabase;
-
import android.view.Menu;
-
import android.view.View;
-
import android.widget.ArrayAdapter;
-
import android.widget.Button;
-
import android.widget.EditText;
-
import android.widget.ImageView;
-
import android.widget.ListView;
-
import android.widget.TextView;
-
import android.widget.Toast;
-
-
public class MainActivity extends Activity {
-
private int imgpath [] =new int[]{R.drawable.img14,R.drawable.img15,R.drawable.img3,R.drawable.img4,
-
R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11
-
,R.drawable.img12,R.drawable.img13};
-
private int paire[]=new int [20];
-
private int [] cmsjs5=new int [5];
-
private int [] cmsjs7=new int [7];
-
private String[]cmsjsindex=new String[7];
-
private ImageView ivcpu,ivcm;
-
private TextView tvshow;
-
private EditText edtcm,edtusename;
-
private Button btnesc,btnok,btnlogin,btnget;
-
private int szcpu,szcm,cmexam,cpuexam,gamecishu,aa,cpusj;
-
private MyDatabaseHelper mydbhelper;
-
private String usename;
-
private ListView lvZhipai,lvcmsjs;
-
private List<Zhipai> zhipai;
-
protected boolean gamesz=false;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.usenamesave);
-
ivcpu=(ImageView) findViewById(R.id.ivcpu);
-
ivcm=(ImageView) findViewById(R.id.ivcm);
-
tvshow=(TextView)findViewById(R.id.tvshow);
-
edtcm=(EditText) findViewById(R.id.edtcm);
-
btnok=(Button)findViewById(R.id.buttonok);
-
btnlogin=(Button)findViewById(R.id.btnlogin);
-
edtusename=(EditText)findViewById(R.id.edtusename);
-
lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
-
btnlogin.setOnClickListener(new View.OnClickListener(){
-
@Override
-
public void onClick(View v) {
-
setusename();
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("你好"+usename+"欢迎进入纸牌游戏!");
-
aldg.setButton(DialogInterface.BUTTON_POSITIVE, "进入", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
setContentView(R.layout.activity_main);
-
ivcpu=(ImageView) findViewById(R.id.ivcpu);
-
ivcm=(ImageView) findViewById(R.id.ivcm);
-
tvshow=(TextView)findViewById(R.id.tvshow);
-
edtcm=(EditText) findViewById(R.id.edtcm);
-
btnok=(Button)findViewById(R.id.buttonok);
-
lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
-
star();
-
getcmpai();
-
}
-
});
-
aldg.show();
-
}});
-
-
}
-
private void setusename(){
-
mydbhelper=new MyDatabaseHelper(MainActivity.this, "examsto.db", null, 2);
-
mydbhelper.getWritableDatabase();
-
usename=edtusename.getText().toString();
-
SQLiteDatabase db=mydbhelper.getWritableDatabase();
-
ContentValues cv=new ContentValues();
-
cv.clear();
-
cv.put("name",usename);
-
cv.put("examcm", 0);
-
long l1=db.insert("exam", null, cv);
-
if(l1!=-1){
-
Toast.makeText(MainActivity.this, "插入数据成功", Toast.LENGTH_SHORT).show();
-
}
-
}
-
private void ok(){
-
// cmsjsindex=new String[1];
-
// int i=cmsjsindex.length-1;
-
// if(i>0){
-
// cmsjsindex[i]="";
-
// i--;
-
// lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
-
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系统已经实现好的xml文件simple_list_item_1
-
// lvcmsjs.setAdapter(adapter);
-
// }
-
btnok.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
paicm();
-
}
-
});
-
}
-
private void star(){
-
for(int i=0;i<paire.length;i++){paire[i]=0;}cmexam=0;cpuexam=0;gamecishu=0;
-
final String[] s = new String[] {"开始游戏", "游戏设置", "排行榜"};
-
tvshow.setText(R.string.show1);
-
Builder bdr=new AlertDialog.Builder(MainActivity.this);
-
bdr.setIcon(R.drawable.ic_launcher);
-
bdr.setTitle("纸牌游戏");
-
bdr.setItems(s, new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
if(s[which].equals("游戏设置")){
-
gameset();
-
}
-
else if(s[which].equals("排行榜")){Button1(); }
-
else if(s[which].equals("开始游戏")){
-
-
}
-
Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
-
}
-
});
-
bdr.show();
-
ivcpu.setImageDrawable(getResources().getDrawable(R.drawable.img16));
-
ivcm.setImageDrawable(getResources().getDrawable(R.drawable.img16));
-
ok();
-
}
-
private void regame(){
-
edtcm.setText("");
-
ivcpu.setImageDrawable(getResources().getDrawable(R.drawable.img16));
-
ivcm.setImageDrawable(getResources().getDrawable(R.drawable.img16));
-
ok();
-
}
-
private void pai(){
-
szcpu=(int)(Math.random()*12)+1;
-
if(szcpu!=cpusj){
-
cpusj=szcpu;
-
}else{
-
cpusj=(int)(Math.random()*12)+1;
-
szcpu=cpusj;
-
}
-
paire[cpusj-1]=cpusj;
-
ivcpu.setImageResource(imgpath[szcpu-1]);
-
}
-
private void gameset(){
-
final String gameset[]=new String[]{"五局制","七局制"};
-
Builder bdr=new AlertDialog.Builder(MainActivity.this);
-
bdr.setIcon(R.drawable.ic_launcher);
-
bdr.setItems(gameset, new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
if(gameset[which].equals("五局制")){
-
paicmsjs5();
-
regame();
-
}
-
else if(gameset[which].equals("七局制")){
-
paicmsjs7();
-
gamesz=true;
-
regame();
-
}
-
}
-
});
-
bdr.show();
-
}
-
private void paicmsjs5(){
-
for(int k=0;k<cmsjs5.length;k++){
-
cmsjs5[k]=(int)(Math.random()*12+1);
-
cmsjsindex[k]="数字: "+cmsjs5[k];
-
}
-
lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系统已经实现好的xml文件simple_list_item_1
-
lvcmsjs.setAdapter(adapter);
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("恭喜你获得了5个数字!");
-
aldg.setButton(DialogInterface.BUTTON_NEGATIVE,"确定",new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
}});
-
aldg.show();
-
}
-
private void paicmsjs7(){
-
for(int k=0;k<cmsjs7.length;k++){
-
cmsjs7[k]=(int)(Math.random()*12+1);
-
cmsjsindex[k]="数字: "+cmsjs7[k];
-
}
-
lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
-
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系统已经实现好的xml文件simple_list_item_1
-
lvcmsjs.setAdapter(adapter);
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("恭喜你获得了7个数字!");
-
aldg.setButton(DialogInterface.BUTTON_NEGATIVE,"确定",new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
}});
-
aldg.show();
-
}
-
private void finalyexam(){
-
if(gamecishu==5){
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("您选择的是5局制。您已经进行了"+gamecishu+"场游戏了!\n"+"是否查看计分表?");
-
aldg.setButton(DialogInterface.BUTTON_POSITIVE, "重开游戏", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
star();
-
}
-
});
-
aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戏", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
finish();
-
}
-
});
-
aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看计分表", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
Button1();
-
}
-
});
-
aldg.show();
-
-
}else if(gamecishu==7){
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("您选择的是七局制。您已经进行了"+gamecishu+"场游戏了!\n"+"是否查看计分表?");
-
aldg.setButton(DialogInterface.BUTTON_POSITIVE, "重开游戏", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
star();
-
}
-
});
-
aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戏", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
finish();
-
}
-
});
-
aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看计分表", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
Button1();
-
}
-
});
-
}
-
}
-
private void getcmpai(){
-
btnget=(Button)findViewById(R.id.btnget);
-
btnget.setOnClickListener(new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
if(gamesz==false){
-
paicmsjs5();
-
}
-
else{
-
paicmsjs7();
-
}
-
}
-
});
-
}
-
private void paicm(){
-
String xz=edtcm.getText().toString();
-
if(xz.equals("")){
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("请您输入相应的数字");
-
aldg.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
edtcm.setText("");
-
edtcm.findFocus();
-
}
-
});
-
aldg.show();
-
}
-
else{
-
aa=Integer.parseInt(xz);
-
szcm = Integer.parseInt(xz);
-
if(aa<14&&aa>0){
-
if(paire[aa-1]==aa){
-
Toast.makeText(MainActivity.this, "你或电脑已经使用过此牌了。", Toast.LENGTH_SHORT).show();edtcm.setText("");return;
-
}
-
switch (aa){
-
case 1:ivcm.setImageResource(imgpath[0]);paire[aa-1]=aa;break;
-
case 2:ivcm.setImageResource(imgpath[1]);paire[aa-1]=aa;;break;
-
case 3:ivcm.setImageResource(imgpath[2]);paire[aa-1]=aa;;break;
-
case 4:ivcm.setImageResource(imgpath[3]);paire[aa-1]=aa;;break;
-
case 5:ivcm.setImageResource(imgpath[4]);paire[aa-1]=aa;;break;
-
case 6:ivcm.setImageResource(imgpath[5]);paire[aa-1]=aa;;break;
-
case 7:ivcm.setImageResource(imgpath[6]);paire[aa-1]=aa;;break;
-
case 8:ivcm.setImageResource(imgpath[7]);paire[aa-1]=aa;;break;
-
case 9:ivcm.setImageResource(imgpath[8]);paire[aa-1]=aa;;break;
-
case 10:ivcm.setImageResource(imgpath[9]);paire[aa-1]=aa;break;
-
case 11:ivcm.setImageResource(imgpath[10]);paire[aa-1]=aa;break;
-
case 12:ivcm.setImageResource(imgpath[11]);paire[aa-1]=aa;break;
-
case 13:ivcm.setImageResource(imgpath[12]);paire[aa-1]=aa;break;
-
}
-
pai();game();
-
}else
-
{
-
Toast.makeText(MainActivity.this, "请输入1~13的数字", Toast.LENGTH_LONG).show();
-
edtcm.setText("");
-
edtcm.findFocus();
-
return;
-
}
-
}
-
}
-
public void game(){
-
gamecishu+=1;
-
if(szcm>szcpu){tvshow.setText("恭喜你获胜了!分数+2");cmexam+=2;}
-
else if(szcm<szcpu){tvshow.setText("你失败了呢,再接再厉!");cpuexam+=2;}
-
else if(szcm==szcpu){tvshow.setText("打了个平手,都不得分!");}
-
AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
-
aldg.setIcon(R.drawable.ic_launcher);
-
aldg.setTitle("提示");
-
aldg.setMessage("您已经进行了"+gamecishu+"场游戏了!\n"+"您的得分:"+cmexam+"\nAI得分:"+cpuexam+"是否继续?");
-
aldg.setButton(DialogInterface.BUTTON_POSITIVE, "继续游戏", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
regame();
-
}
-
});
-
aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戏", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
finish();
-
}
-
});
-
aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看排行榜", new OnClickListener() {
-
@Override
-
public void onClick(DialogInterface dialog, int which) {
-
Button1();
-
}
-
});
-
aldg.show();
-
caozuodata();
-
finalyexam();
-
}
-
private void caozuodata(){
-
//更新
-
SQLiteDatabase db2=mydbhelper.getWritableDatabase();
-
ContentValues cv2=new ContentValues();
-
cv2.put("examcm", cmexam);
-
int i3=db2.update("exam", cv2, "name=?", new String[]{usename});
-
if(i3!=0){
-
Toast.makeText(MainActivity.this, "录入成功", Toast.LENGTH_SHORT).show();
-
}
-
}
-
public void Button1() {
-
setContentView(R.layout.star_main);
-
exam();
-
btnesc = (Button) findViewById(R.id.buttonesc);
-
btnesc.setOnClickListener(new Button.OnClickListener() {
-
public void onClick(View v) {
-
setContentView(R.layout.activity_main);
-
ivcpu=(ImageView) findViewById(R.id.ivcpu);
-
ivcm=(ImageView) findViewById(R.id.ivcm);
-
tvshow=(TextView)findViewById(R.id.tvshow);
-
edtcm=(EditText) findViewById(R.id.edtcm);
-
btnok=(Button)findViewById(R.id.buttonok);
-
lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
-
star();
-
}
-
});
-
}
-
private void exam(){
-
Button btnDataToList=(Button)findViewById(R.id.buttoning);
-
lvZhipai=(ListView)findViewById(R.id.lvZhipai);
-
btnDataToList.setOnClickListener(new View.OnClickListener(){
-
@Override
-
public void onClick(View v) {
-
SQLiteDatabase db=mydbhelper.getWritableDatabase();
-
//Cursor c=db.query("book", null, "pages>?", null, null, null, "pages desc");
-
Cursor c=db.query("exam", null, null, null,null,null ,"examcm desc"); //最后一个参数:页数降序! name:玩家;分数:score desc;
-
zhipai=new ArrayList<Zhipai>();
-
if(c!=null){
-
while(c!=null && c.moveToNext()){
-
Zhipai zp=new Zhipai(); //新建一个book对象;实体类,要实体类的每个属性;
-
zp.setId(c.getInt(0));
-
zp.setName(c.getString(1));
-
zp.setExamcm(c.getInt(2));
-
zhipai.add(zp); //每一个对象,都要加进books集合里面;
-
}
-
c.close(); //关闭游标,切记,不要忘记完毕cursor对象;
-
}
-
//加载
-
String []examInfo=new String[zhipai.size()]; //数组长度,集合长度;
-
ArrayAdapter<String>arrayAdapter=null; //适配器;
-
int i=0;
-
for(Zhipai z:zhipai){ //写了一个迭代器,从books集合里面逐个取对象book;
-
//将图书相关信息组合成一个字符串,存储在字符串数组中的相应位置;
-
examInfo[i]=z.getId()+"|\t\t\t"+z.getName()+" \t\t"+z.getExamcm();
-
i++;
-
}
-
//使用字符串数组初始化ArrayAdapter对象;
-
arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,examInfo);
-
lvZhipai.setAdapter(arrayAdapter);
-
}
-
});
-
-
}
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.main, menu);
-
return true;
-
}
-
-
}
视频课:https://edu.csdn.net/course/play/7621
文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。
原文链接:aaaedu.blog.csdn.net/article/details/51755634
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)