纸牌游戏之五 主界面类

举报
tea_year 发表于 2021/12/30 00:23:32 2021/12/30
【摘要】 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 ...

  
  1. package com.example.azhipai;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.app.AlertDialog.Builder;
  8. import android.content.ContentValues;
  9. import android.content.DialogInterface;
  10. import android.content.DialogInterface.OnClickListener;
  11. import android.database.Cursor;
  12. import android.database.sqlite.SQLiteDatabase;
  13. import android.view.Menu;
  14. import android.view.View;
  15. import android.widget.ArrayAdapter;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.ImageView;
  19. import android.widget.ListView;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22. public class MainActivity extends Activity {
  23. private int imgpath [] =new int[]{R.drawable.img14,R.drawable.img15,R.drawable.img3,R.drawable.img4,
  24. R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11
  25. ,R.drawable.img12,R.drawable.img13};
  26. private int paire[]=new int [20];
  27. private int [] cmsjs5=new int [5];
  28. private int [] cmsjs7=new int [7];
  29. private String[]cmsjsindex=new String[7];
  30. private ImageView ivcpu,ivcm;
  31. private TextView tvshow;
  32. private EditText edtcm,edtusename;
  33. private Button btnesc,btnok,btnlogin,btnget;
  34. private int szcpu,szcm,cmexam,cpuexam,gamecishu,aa,cpusj;
  35. private MyDatabaseHelper mydbhelper;
  36. private String usename;
  37. private ListView lvZhipai,lvcmsjs;
  38. private List<Zhipai> zhipai;
  39. protected boolean gamesz=false;
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.usenamesave);
  44. ivcpu=(ImageView) findViewById(R.id.ivcpu);
  45. ivcm=(ImageView) findViewById(R.id.ivcm);
  46. tvshow=(TextView)findViewById(R.id.tvshow);
  47. edtcm=(EditText) findViewById(R.id.edtcm);
  48. btnok=(Button)findViewById(R.id.buttonok);
  49. btnlogin=(Button)findViewById(R.id.btnlogin);
  50. edtusename=(EditText)findViewById(R.id.edtusename);
  51. lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
  52. btnlogin.setOnClickListener(new View.OnClickListener(){
  53. @Override
  54. public void onClick(View v) {
  55. setusename();
  56. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  57. aldg.setIcon(R.drawable.ic_launcher);
  58. aldg.setTitle("提示");
  59. aldg.setMessage("你好"+usename+"欢迎进入纸牌游戏!");
  60. aldg.setButton(DialogInterface.BUTTON_POSITIVE, "进入", new OnClickListener() {
  61. @Override
  62. public void onClick(DialogInterface dialog, int which) {
  63. setContentView(R.layout.activity_main);
  64. ivcpu=(ImageView) findViewById(R.id.ivcpu);
  65. ivcm=(ImageView) findViewById(R.id.ivcm);
  66. tvshow=(TextView)findViewById(R.id.tvshow);
  67. edtcm=(EditText) findViewById(R.id.edtcm);
  68. btnok=(Button)findViewById(R.id.buttonok);
  69. lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
  70. star();
  71. getcmpai();
  72. }
  73. });
  74. aldg.show();
  75. }});
  76. }
  77. private void setusename(){
  78. mydbhelper=new MyDatabaseHelper(MainActivity.this, "examsto.db", null, 2);
  79. mydbhelper.getWritableDatabase();
  80. usename=edtusename.getText().toString();
  81. SQLiteDatabase db=mydbhelper.getWritableDatabase();
  82. ContentValues cv=new ContentValues();
  83. cv.clear();
  84. cv.put("name",usename);
  85. cv.put("examcm", 0);
  86. long l1=db.insert("exam", null, cv);
  87. if(l1!=-1){
  88. Toast.makeText(MainActivity.this, "插入数据成功", Toast.LENGTH_SHORT).show();
  89. }
  90. }
  91. private void ok(){
  92. // cmsjsindex=new String[1];
  93. // int i=cmsjsindex.length-1;
  94. // if(i>0){
  95. // cmsjsindex[i]="";
  96. // i--;
  97. // lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
  98. // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系统已经实现好的xml文件simple_list_item_1
  99. // lvcmsjs.setAdapter(adapter);
  100. // }
  101. btnok.setOnClickListener(new View.OnClickListener() {
  102. @Override
  103. public void onClick(View v) {
  104. paicm();
  105. }
  106. });
  107. }
  108. private void star(){
  109. for(int i=0;i<paire.length;i++){paire[i]=0;}cmexam=0;cpuexam=0;gamecishu=0;
  110. final String[] s = new String[] {"开始游戏", "游戏设置", "排行榜"};
  111. tvshow.setText(R.string.show1);
  112. Builder bdr=new AlertDialog.Builder(MainActivity.this);
  113. bdr.setIcon(R.drawable.ic_launcher);
  114. bdr.setTitle("纸牌游戏");
  115. bdr.setItems(s, new OnClickListener() {
  116. @Override
  117. public void onClick(DialogInterface dialog, int which) {
  118. if(s[which].equals("游戏设置")){
  119. gameset();
  120. }
  121. else if(s[which].equals("排行榜")){Button1(); }
  122. else if(s[which].equals("开始游戏")){
  123. }
  124. Toast.makeText(MainActivity.this, s[which], Toast.LENGTH_SHORT).show();
  125. }
  126. });
  127. bdr.show();
  128. ivcpu.setImageDrawable(getResources().getDrawable(R.drawable.img16));
  129. ivcm.setImageDrawable(getResources().getDrawable(R.drawable.img16));
  130. ok();
  131. }
  132. private void regame(){
  133. edtcm.setText("");
  134. ivcpu.setImageDrawable(getResources().getDrawable(R.drawable.img16));
  135. ivcm.setImageDrawable(getResources().getDrawable(R.drawable.img16));
  136. ok();
  137. }
  138. private void pai(){
  139. szcpu=(int)(Math.random()*12)+1;
  140. if(szcpu!=cpusj){
  141. cpusj=szcpu;
  142. }else{
  143. cpusj=(int)(Math.random()*12)+1;
  144. szcpu=cpusj;
  145. }
  146. paire[cpusj-1]=cpusj;
  147. ivcpu.setImageResource(imgpath[szcpu-1]);
  148. }
  149. private void gameset(){
  150. final String gameset[]=new String[]{"五局制","七局制"};
  151. Builder bdr=new AlertDialog.Builder(MainActivity.this);
  152. bdr.setIcon(R.drawable.ic_launcher);
  153. bdr.setItems(gameset, new OnClickListener() {
  154. @Override
  155. public void onClick(DialogInterface dialog, int which) {
  156. if(gameset[which].equals("五局制")){
  157. paicmsjs5();
  158. regame();
  159. }
  160. else if(gameset[which].equals("七局制")){
  161. paicmsjs7();
  162. gamesz=true;
  163. regame();
  164. }
  165. }
  166. });
  167. bdr.show();
  168. }
  169. private void paicmsjs5(){
  170. for(int k=0;k<cmsjs5.length;k++){
  171. cmsjs5[k]=(int)(Math.random()*12+1);
  172. cmsjsindex[k]="数字: "+cmsjs5[k];
  173. }
  174. lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
  175. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系统已经实现好的xml文件simple_list_item_1
  176. lvcmsjs.setAdapter(adapter);
  177. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  178. aldg.setIcon(R.drawable.ic_launcher);
  179. aldg.setTitle("提示");
  180. aldg.setMessage("恭喜你获得了5个数字!");
  181. aldg.setButton(DialogInterface.BUTTON_NEGATIVE,"确定",new OnClickListener() {
  182. @Override
  183. public void onClick(DialogInterface dialog, int which) {
  184. }});
  185. aldg.show();
  186. }
  187. private void paicmsjs7(){
  188. for(int k=0;k<cmsjs7.length;k++){
  189. cmsjs7[k]=(int)(Math.random()*12+1);
  190. cmsjsindex[k]="数字: "+cmsjs7[k];
  191. }
  192. lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
  193. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系统已经实现好的xml文件simple_list_item_1
  194. lvcmsjs.setAdapter(adapter);
  195. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  196. aldg.setIcon(R.drawable.ic_launcher);
  197. aldg.setTitle("提示");
  198. aldg.setMessage("恭喜你获得了7个数字!");
  199. aldg.setButton(DialogInterface.BUTTON_NEGATIVE,"确定",new OnClickListener() {
  200. @Override
  201. public void onClick(DialogInterface dialog, int which) {
  202. }});
  203. aldg.show();
  204. }
  205. private void finalyexam(){
  206. if(gamecishu==5){
  207. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  208. aldg.setIcon(R.drawable.ic_launcher);
  209. aldg.setTitle("提示");
  210. aldg.setMessage("您选择的是5局制。您已经进行了"+gamecishu+"场游戏了!\n"+"是否查看计分表?");
  211. aldg.setButton(DialogInterface.BUTTON_POSITIVE, "重开游戏", new OnClickListener() {
  212. @Override
  213. public void onClick(DialogInterface dialog, int which) {
  214. star();
  215. }
  216. });
  217. aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戏", new OnClickListener() {
  218. @Override
  219. public void onClick(DialogInterface dialog, int which) {
  220. finish();
  221. }
  222. });
  223. aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看计分表", new OnClickListener() {
  224. @Override
  225. public void onClick(DialogInterface dialog, int which) {
  226. Button1();
  227. }
  228. });
  229. aldg.show();
  230. }else if(gamecishu==7){
  231. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  232. aldg.setIcon(R.drawable.ic_launcher);
  233. aldg.setTitle("提示");
  234. aldg.setMessage("您选择的是七局制。您已经进行了"+gamecishu+"场游戏了!\n"+"是否查看计分表?");
  235. aldg.setButton(DialogInterface.BUTTON_POSITIVE, "重开游戏", new OnClickListener() {
  236. @Override
  237. public void onClick(DialogInterface dialog, int which) {
  238. star();
  239. }
  240. });
  241. aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戏", new OnClickListener() {
  242. @Override
  243. public void onClick(DialogInterface dialog, int which) {
  244. finish();
  245. }
  246. });
  247. aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看计分表", new OnClickListener() {
  248. @Override
  249. public void onClick(DialogInterface dialog, int which) {
  250. Button1();
  251. }
  252. });
  253. }
  254. }
  255. private void getcmpai(){
  256. btnget=(Button)findViewById(R.id.btnget);
  257. btnget.setOnClickListener(new View.OnClickListener() {
  258. @Override
  259. public void onClick(View v) {
  260. if(gamesz==false){
  261. paicmsjs5();
  262. }
  263. else{
  264. paicmsjs7();
  265. }
  266. }
  267. });
  268. }
  269. private void paicm(){
  270. String xz=edtcm.getText().toString();
  271. if(xz.equals("")){
  272. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  273. aldg.setIcon(R.drawable.ic_launcher);
  274. aldg.setTitle("提示");
  275. aldg.setMessage("请您输入相应的数字");
  276. aldg.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new OnClickListener() {
  277. @Override
  278. public void onClick(DialogInterface dialog, int which) {
  279. edtcm.setText("");
  280. edtcm.findFocus();
  281. }
  282. });
  283. aldg.show();
  284. }
  285. else{
  286. aa=Integer.parseInt(xz);
  287. szcm = Integer.parseInt(xz);
  288. if(aa<14&&aa>0){
  289. if(paire[aa-1]==aa){
  290. Toast.makeText(MainActivity.this, "你或电脑已经使用过此牌了。", Toast.LENGTH_SHORT).show();edtcm.setText("");return;
  291. }
  292. switch (aa){
  293. case 1:ivcm.setImageResource(imgpath[0]);paire[aa-1]=aa;break;
  294. case 2:ivcm.setImageResource(imgpath[1]);paire[aa-1]=aa;;break;
  295. case 3:ivcm.setImageResource(imgpath[2]);paire[aa-1]=aa;;break;
  296. case 4:ivcm.setImageResource(imgpath[3]);paire[aa-1]=aa;;break;
  297. case 5:ivcm.setImageResource(imgpath[4]);paire[aa-1]=aa;;break;
  298. case 6:ivcm.setImageResource(imgpath[5]);paire[aa-1]=aa;;break;
  299. case 7:ivcm.setImageResource(imgpath[6]);paire[aa-1]=aa;;break;
  300. case 8:ivcm.setImageResource(imgpath[7]);paire[aa-1]=aa;;break;
  301. case 9:ivcm.setImageResource(imgpath[8]);paire[aa-1]=aa;;break;
  302. case 10:ivcm.setImageResource(imgpath[9]);paire[aa-1]=aa;break;
  303. case 11:ivcm.setImageResource(imgpath[10]);paire[aa-1]=aa;break;
  304. case 12:ivcm.setImageResource(imgpath[11]);paire[aa-1]=aa;break;
  305. case 13:ivcm.setImageResource(imgpath[12]);paire[aa-1]=aa;break;
  306. }
  307. pai();game();
  308. }else
  309. {
  310. Toast.makeText(MainActivity.this, "请输入1~13的数字", Toast.LENGTH_LONG).show();
  311. edtcm.setText("");
  312. edtcm.findFocus();
  313. return;
  314. }
  315. }
  316. }
  317. public void game(){
  318. gamecishu+=1;
  319. if(szcm>szcpu){tvshow.setText("恭喜你获胜了!分数+2");cmexam+=2;}
  320. else if(szcm<szcpu){tvshow.setText("你失败了呢,再接再厉!");cpuexam+=2;}
  321. else if(szcm==szcpu){tvshow.setText("打了个平手,都不得分!");}
  322. AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
  323. aldg.setIcon(R.drawable.ic_launcher);
  324. aldg.setTitle("提示");
  325. aldg.setMessage("您已经进行了"+gamecishu+"场游戏了!\n"+"您的得分:"+cmexam+"\nAI得分:"+cpuexam+"是否继续?");
  326. aldg.setButton(DialogInterface.BUTTON_POSITIVE, "继续游戏", new OnClickListener() {
  327. @Override
  328. public void onClick(DialogInterface dialog, int which) {
  329. regame();
  330. }
  331. });
  332. aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戏", new OnClickListener() {
  333. @Override
  334. public void onClick(DialogInterface dialog, int which) {
  335. finish();
  336. }
  337. });
  338. aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看排行榜", new OnClickListener() {
  339. @Override
  340. public void onClick(DialogInterface dialog, int which) {
  341. Button1();
  342. }
  343. });
  344. aldg.show();
  345. caozuodata();
  346. finalyexam();
  347. }
  348. private void caozuodata(){
  349. //更新
  350. SQLiteDatabase db2=mydbhelper.getWritableDatabase();
  351. ContentValues cv2=new ContentValues();
  352. cv2.put("examcm", cmexam);
  353. int i3=db2.update("exam", cv2, "name=?", new String[]{usename});
  354. if(i3!=0){
  355. Toast.makeText(MainActivity.this, "录入成功", Toast.LENGTH_SHORT).show();
  356. }
  357. }
  358. public void Button1() {
  359. setContentView(R.layout.star_main);
  360. exam();
  361. btnesc = (Button) findViewById(R.id.buttonesc);
  362. btnesc.setOnClickListener(new Button.OnClickListener() {
  363. public void onClick(View v) {
  364. setContentView(R.layout.activity_main);
  365. ivcpu=(ImageView) findViewById(R.id.ivcpu);
  366. ivcm=(ImageView) findViewById(R.id.ivcm);
  367. tvshow=(TextView)findViewById(R.id.tvshow);
  368. edtcm=(EditText) findViewById(R.id.edtcm);
  369. btnok=(Button)findViewById(R.id.buttonok);
  370. lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
  371. star();
  372. }
  373. });
  374. }
  375. private void exam(){
  376. Button btnDataToList=(Button)findViewById(R.id.buttoning);
  377. lvZhipai=(ListView)findViewById(R.id.lvZhipai);
  378. btnDataToList.setOnClickListener(new View.OnClickListener(){
  379. @Override
  380. public void onClick(View v) {
  381. SQLiteDatabase db=mydbhelper.getWritableDatabase();
  382. //Cursor c=db.query("book", null, "pages>?", null, null, null, "pages desc");
  383. Cursor c=db.query("exam", null, null, null,null,null ,"examcm desc"); //最后一个参数:页数降序! name:玩家;分数:score desc;
  384. zhipai=new ArrayList<Zhipai>();
  385. if(c!=null){
  386. while(c!=null && c.moveToNext()){
  387. Zhipai zp=new Zhipai(); //新建一个book对象;实体类,要实体类的每个属性;
  388. zp.setId(c.getInt(0));
  389. zp.setName(c.getString(1));
  390. zp.setExamcm(c.getInt(2));
  391. zhipai.add(zp); //每一个对象,都要加进books集合里面;
  392. }
  393. c.close(); //关闭游标,切记,不要忘记完毕cursor对象;
  394. }
  395. //加载
  396. String []examInfo=new String[zhipai.size()]; //数组长度,集合长度;
  397. ArrayAdapter<String>arrayAdapter=null; //适配器;
  398. int i=0;
  399. for(Zhipai z:zhipai){ //写了一个迭代器,从books集合里面逐个取对象book;
  400. //将图书相关信息组合成一个字符串,存储在字符串数组中的相应位置;
  401. examInfo[i]=z.getId()+"|\t\t\t"+z.getName()+" \t\t"+z.getExamcm();
  402. i++;
  403. }
  404. //使用字符串数组初始化ArrayAdapter对象;
  405. arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,examInfo);
  406. lvZhipai.setAdapter(arrayAdapter);
  407. }
  408. });
  409. }
  410. @Override
  411. public boolean onCreateOptionsMenu(Menu menu) {
  412. // Inflate the menu; this adds items to the action bar if it is present.
  413. getMenuInflater().inflate(R.menu.main, menu);
  414. return true;
  415. }
  416. }
视频课:https://edu.csdn.net/course/play/7621

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/51755634

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。