没有实现人机对战,功力不到位。

package com.tarena.rungames;import java.math.BigDecimal;import java.util.ArrayList;import java.util.HashMap;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.Intent;import android.content.DialogInterface.OnClickListener;import android.media.AudioManager;import android.media.SoundPool;import android.os.Bundle;import android.view.KeyEvent;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.WindowManager;import android.view.View.OnTouchListener;import android.widget.RelativeLayout;public class MainActivity extends Activity {//计算上面空出部分的高度占屏幕高度的比例final double BILI=0.3674540682414698;//计算右边空出部分占屏幕宽度的比例final double RBILI=0.954070979166667;//计算左边空出部分占屏幕宽度的比例final double LBILI=0.0490605458888;static //给一个int值来记录总共在棋盘上下了多少步棋int iu=0;//给定一个boolean值判定选择的是人机对战还是人人对战boolean isdu;//设定棋盘对应的二维数组public static int[][] mGameMap = new int[9][9];static //音效SoundPool soundPool;HashMap<Integer, Integer> inHashMap=new HashMap<Integer, Integer>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);soundPool=new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);inHashMap.put(1, soundPool.load(this, R.raw.am, 1));musicGame(inHashMap);//加载主屏幕final RelativeLayout root=(RelativeLayout) this.findViewById(R.id.relativelayout);//全屏显示getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);Intent mIntent=getIntent();//获取选择人机对战的Intentisdu=mIntent.getBooleanExtra("moshi", false);//设置全屏Touch事件root.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent arg1) {// TODO Auto-generated method stub//获取屏幕的高并计算出上面空出的高度float a=(float) ((v.getHeight())*BILI);//获取屏幕的宽并计算左边空出的宽度float lKo=(float) (v.getWidth()*LBILI);float Ko=lKo/4;//获取屏幕的宽并计算右边空出的宽度float rKo=(float) (v.getWidth()*RBILI);//计算棋盘的实际宽度float shijikuan=rKo-lKo;//计算棋盘的高每个格所占距离float heights=(v.getHeight()-(a+lKo))/8;//计算棋盘的宽每个格所占距离float oneSize=shijikuan/8;//获取触摸事件必须在棋盘上if(arg1.getY()>(a-lKo)){//设置下棋位置必须是触摸事件弹起的位置if(arg1.getAction()==MotionEvent.ACTION_UP){//记录更新iu++;//获取触摸点上x接近棋盘这个二维数组那个点,此处采用四舍五入BigDecimal myxBigDecimal=new BigDecimal(Math.abs((arg1.getX()-lKo))/oneSize).setScale(0, BigDecimal.ROUND_HALF_UP);//获取触摸点上y接近棋盘这个二维数组那个点,此处采用四舍五入BigDecimal myBigDecimal=new BigDecimal((arg1.getY()-a)/heights).setScale(0, BigDecimal.ROUND_HALF_UP);//转化为int便于保存和设置int indexX=myxBigDecimal.intValue();int indexY=myBigDecimal.intValue();//Log.i("TTR", ""+indexX+"--->"+indexY);//Log.i("TTR", ""+mGameMap[0][0]);if(mGameMap[indexX][indexY]==0){//创建并设置一个棋子在屏幕上的位置//判断是否是人机对战if(isdu){//设定记录不更新iu--;final MyView yMyView=new MyView(MainActivity.this,indexX*(oneSize)+Ko,indexY*(heights)+(a-lKo)+Ko,iu);root.addView(yMyView);mGameMap[indexX][indexY]=-1;int[] sss=autogames();int inX=sss[0];int inY=sss[1];final MyView jMyView=new MyView(MainActivity.this,inX*(oneSize)+Ko,inY*(heights)+(a-lKo)+Ko,iu+1);root.addView(jMyView);mGameMap[inX][inY]=1;}else{//执行人人对战的代码final MyView yMyView=new MyView(MainActivity.this,indexX*(oneSize)+Ko,indexY*(heights)+(a-lKo)+Ko,iu);//将棋子添加到主屏幕上root.addView(yMyView);if(iu%2==0){mGameMap[indexX][indexY]=-1;}else {mGameMap[indexX][indexY]=1;}}int win=isOver();int xwin=xieOver();int fwin=FxieOver();if(win!=0){if(win==-1){showDialog1("黑方胜出!!!点击确定重玩一局 ");}else{showDialog1("白棋胜出!!!点击确定重玩一局 ");}}else if(xwin!=0){if(xwin==-1){showDialog1("黑方胜出!!!点击确定重玩一局 ");}else{showDialog1("白棋胜出!!!点击确定重玩一局 ");}}else if(fwin!=0){if(fwin==-1){showDialog1("黑方胜出!!!点击确定重玩一局 ");}else{showDialog1("白棋胜出!!!点击确定重玩一局 ");}}else if(iu==80){showDialog1("平局!点击确定重玩 ");}}else{iu--;}}}return true;}});}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();iu=0;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}//横竖判断输赢public static int isOver(){for(int i=0;i<9;i++){for(int j=8;j>=4;j--){//竖向判断输赢if(mGameMap[i][j]==mGameMap[i][j-1]&&mGameMap[i][j]==mGameMap[i][j-2]&&mGameMap[i][j]==mGameMap[i][j-3]&&mGameMap[i][j]==mGameMap[i][j-4]&&mGameMap[i][j]!=0){return mGameMap[i][j];//横向判断输赢}else if(mGameMap[j][i]==mGameMap[j-1][i]&&mGameMap[j][i]==mGameMap[j-2][i]&&mGameMap[j][i]==mGameMap[j-3][i]&&mGameMap[j][i]==mGameMap[j-4][i]&&mGameMap[j][i]!=0){return mGameMap[j][i];}}}return 0;}//正斜判断输赢public static int xieOver(){for(int i=0;i<5;i++){for(int j=8;j>=4;j--){if(mGameMap[i][j]==mGameMap[i+1][j-1]&&mGameMap[i][j]==mGameMap[i+2][j-2]&&mGameMap[i][j]==mGameMap[i+3][j-3]&&mGameMap[i][j]==mGameMap[i+4][j-4]&&mGameMap[i][j]!=0){return mGameMap[i][j];}}}return 0;}//反斜判断输赢public static int FxieOver(){for(int i=8;i>=4;i--){for(int j=8;j>=4;j--){ if(mGameMap[i][j]==mGameMap[i-1][j-1]&&mGameMap[i][j]==mGameMap[i-2][j-2]&&mGameMap[i][j]==mGameMap[i-3][j-3]&&mGameMap[i][j]==mGameMap[i-4][j-4]&&mGameMap[i][j]!=0){ return mGameMap[i][j];}}}return 0;}//电脑自动下棋public static int[] autogames(){for(int i=0;i<9;i++){for(int j=8;j>=1;j--){if(mGameMap[i][j]==mGameMap[i][j-1]&&mGameMap[i][j]==-1){int[] se=new int[2];if(j==1&&mGameMap[i][j+1]==0){se[0]=i;se[1]=j+1;return se;}else if(j==8&&mGameMap[i][j-2]==0){se[0]=i;se[1]=j-2;return se;}else if(j!=1&&mGameMap[i][j-2]==0){se[0]=i;se[1]=j-2;return se;}else if(j!=8&&mGameMap[i][j+1]==0){se[0]=i;se[1]=j+1;return se;}}else if(mGameMap[j][i]==mGameMap[j-1][i]&&mGameMap[j][i]==-1){int[] se=new int[2];if(j==1&&mGameMap[j+1][i]==0){se[1]=i;se[0]=j+1;return se;}else if(j==8&&mGameMap[j-2][i]==0){se[1]=i;se[0]=j-2;return se;}else if(j!=1&&mGameMap[j-2][i]==0){se[1]=i;se[0]=j-2;return se;}else if(j!=8&&mGameMap[j+1][i]==0){se[1]=i;se[0]=j+1;return se;}}else if(i<8&&mGameMap[i][j]==mGameMap[i+1][j-1]&&mGameMap[i][j]==-1){int[] se=new int[2];if((i<7||j>1)&&mGameMap[i+2][j-2]==0){se[0]=i+2;se[1]=j-2;return se;}else if((i==7||j==1)&&mGameMap[i-1][j+1]==0){se[0]=i-1;se[1]=j+1;return se;}}else if(i<8&&mGameMap[8-i][j]==mGameMap[(8-i)-1][j-1]&&mGameMap[8-i][j]==-1){if(((8-i)>1||j>1)&&mGameMap[(8-i)-2][j-2]==0){int[] se=new int[2];se[0]=(8-i)-2;se[1]=j-2;return se;}else if((8-i==1||j==1)&&mGameMap[(8-i)+1][j+1]==0){int[] se=new int[2];se[0]=(8-i)+1;se[1]=j+1;return se;}}else{int[] se=new int[2];if(mGameMap[i][j]!=0&&mGameMap[i][j]==-1&&mGameMap[i][j-1]==0){se[0]=i;se[1]=j-1;return se;}else if(mGameMap[j][i]!=0&&mGameMap[i][j]==-1&&mGameMap[j-1][i]==0){se[0]=j-1;se[1]=i;return se;}}}}return null;}public static int[] autogamesthree(){return null;}private static void musicGame(final HashMap<Integer, Integer> map) {// TODO Auto-generated method stubnew Thread(){public void run() {soundPool.play(map.get(1),1, 1, 0, 0, 1);};}.start();}AlertDialog mAlertDialog1;public void showDialog1(String ss) {AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);mAlertDialog1 = mBuilder.setTitle("游戏结束").setIcon(R.drawable.ic_launcher).setMessage(ss).setPositiveButton("确定",new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubmGameMap = new int[9][9];//onDestroy();onCreate(null);iu=0;}}).show();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode == KeyEvent.KEYCODE_BACK) {MainActivity.this.finish();}return true;}@Overrideprotected void onSaveInstanceState(Bundle outState) {// TODO Auto-generated method stubArrayList<Integer> map=new ArrayList<Integer>();for(int i=0;i<9;i++){for(int j=0;j<9;j++){map.add(mGameMap[i][j]);}}outState.putIntegerArrayList("data", map);super.onSaveInstanceState(outState);}@SuppressWarnings("unchecked")@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onRestoreInstanceState(savedInstanceState);ArrayList<Integer> map=null;map=(ArrayList<Integer>) savedInstanceState.get("data");int index=0;for(int i=0;i<9;i++){for(int j=0;j<9;j++){mGameMap[i][j]=map.get(index);index++;}}}@Overrideprotected void onPause() {// TODO Auto-generated method stub super.onPause();}}
主要代码就一个Activity来完成,没有项目概念

上源码

更多相关文章

  1. 2016, 微信'应用号'可能横空出世!
  2. 用java画跳棋棋盘

随机推荐

  1. MySQL InnoDB表空间加密示例详解
  2. 利用MySQL空间函数实现位置打卡的完整步
  3. 详细分析MySQL主从复制
  4. MySQL百万级数据量分页查询方法及其优化
  5. MySQL数据库升级的一些"陷阱"
  6. 简述MySql四种事务隔离级别
  7. MySQL优化SQL语句的技巧
  8. MySQL如何优化查询速度
  9. MySQL性能优化之如何高效正确的使用索引
  10. MySQL单表恢复的步骤