android 五子棋之小白java

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

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>();
	@Override
	protected 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();
		//获取选择人机对战的Intent
		isdu=mIntent.getBooleanExtra("moshi", false);
		//设置全屏Touch事件
		root.setOnTouchListener(new OnTouchListener() {		
			@Override
			public 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;
			}
		});		
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		iu=0;
	}
	@Override
	public 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 stub
		new 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() {		
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						mGameMap = new int[9][9];
						//onDestroy();
						onCreate(null);
						iu=0;
					}
				}).show();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			MainActivity.this.finish();
		}
		return true;
	}
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		ArrayList<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")
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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++;
			}
		}
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub		 
		super.onPause();
	}
}
主要代码就一个Activity来完成,没有项目概念

 上源码

你可能感兴趣的:(java,android,五子棋)