android 五子棋
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="top|center"
android:background="#094734">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="重新开始"
android:onClick="开始Click"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="悔 棋"
android:onClick="悔棋Click"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="电脑下棋"
android:onClick="电脑走棋Click"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<CheckBox
android:layout_height="wrap_content"
android:layout_width="128dp"
android:text="人机对局"
android:checked="true"
android:id="@+id/mainCheckBox1"
android:textColor="#FFFFFF"/>
<CheckBox
android:layout_height="wrap_content"
android:layout_width="128dp"
android:text="显示禁点"
android:checked="true"
android:id="@+id/mainCheckBox2"
android:textColor="#FFFFFF"/>
<CheckBox
android:layout_height="wrap_content"
android:layout_width="94dp"
android:text="启用禁手"
android:checked="true"
android:id="@+id/mainCheckBox3"
android:textColor="#FFFFFF"/>
</LinearLayout>
<ImageView
android:layout_height="1080px"
android:layout_width="1080px"
android:id="@+id/棋盘id"
android:background="#CCCCCC"
android:layout_marginTop="15dp"/>
</LinearLayout>
import android.app.*;//葫芦岛.iv 2019 01 20
import android.media.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;
import android.graphics.Bitmap.*;
public class MainActivity extends Activity {
private String[][] 事 = new String[15][15];
private String[] 名={"","","三","四","五","长连 ","四 四"};
private int 最大分数,当前手,操作,卡点,成员,点成员,结束,补,L,i,j,ox,oy,ax,ay,bx,by,qx,qy,边长, 格宽, 半格,边距;
private int[] X={1,0,-1,1,1,0,-1,1};//基本图形条件
private int[] Y={0,1,1,1,0,1,1,1};//五连一 | / \六连一 | / \
private int[] 点个数={4,4,4,4,5,5,5,5};
private int[] 横={11,15,15,11,10,15,15,10};
private int[] 纵={15,11,11,11,15,10,10,10};
private int[] 禁={0,0,4,0,0,0,5,0};//这个数组临时用一下
private int[] 棋谱x=new int[200], 棋谱y=new int[200];
private int[][] 定位= new int[15][15], 棋盘点= new int[15][15], 棋盘标记= new int[15][15];//过滤重复运算用的
private int[][][] 白=new int[15][15][8], 黑=new int[15][15][8], 分数=new int[15][15][2], 下标=new int[15][15][8];
private int[][][][] 序号x=new int[15][15][8][6], 序号y=new int[15][15][8][6];//轴点图形序号
private SoundPool 落子声=new SoundPool(2, AudioManager.STREAM_SYSTEM, 5);
private CheckBox 人机,禁手,禁点;
private ImageView 棋盘;
private Bitmap bitmap,bitmap1;
private Paint p=new Paint();
private Canvas g;
private Rect rect=new Rect();
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
边距 = 1080%16;
边长 = 1080-边距;
格宽 = 边长 / 16;
半格 = 格宽/2;
棋盘 = (ImageView) this.findViewById(R.id.棋盘id);
bitmap = Bitmap.createBitmap(边长,边长, Config.ARGB_4444);
g = new Canvas(bitmap);
棋盘.setImageBitmap(bitmap);
p.setStrokeWidth(3);//线宽
p.setTextSize(36); //字体大小
p.setTextAlign(Paint.Align.CENTER);//字体居中
g.drawColor(Color.LTGRAY);
for (i = 1; i < 16; i++) {//画棋盘
g.drawLine(格宽, i*格宽, 边长 - 格宽, i*格宽, p);
g.drawLine(i*格宽, 格宽, i*格宽, 边长- 格宽, p);//网格
g.drawCircle(格宽*i*4,格宽*i*4,7,p);
g.drawCircle(格宽*(16-i*4),格宽*i*4,7,p);//星点
}
bitmap1= bitmap.copy(Config.ARGB_4444,true);//备份棋盘位图
人机=(CheckBox)findViewById(R.id.mainCheckBox1);
禁点=(CheckBox)findViewById(R.id.mainCheckBox2);
禁手=(CheckBox)findViewById(R.id.mainCheckBox3);
落子声.load(this, R.drawable.lzs, 1);
for(L = 0;L < 8;L++) //类型循环
for(ay = 0;ay < 纵[L];ay++)//棋盘点循环
for(ax = 禁[L];ax < 横[L];ax++)//临时用一下在这里
for(点成员 = 0;点成员 <= 点个数[L];点成员++) {
bx = ax + X[L] * 点成员;
by = ay + Y[L] * 点成员;
i = 下标[by][bx][L]++;
序号x[by][bx][L][i] = ax;
序号y[by][bx][L][i] = ay;
}//提取轴点图形序号
棋盘.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View p1,MotionEvent e){
if(e.getAction() == MotionEvent.ACTION_DOWN){
ax = ((int) e.getX()-33)/ 67;
ay = ((int) e.getY()-33)/ 67;
if(结束==0&&ax<15&&ay<15&& 棋盘点[ay][ax]<=0)
if(禁手.isChecked()&&当前手 % 2 == 0 && 棋盘点[ay][ax] ==-1)
禁点.setText(事[ay][ax]);
else{
卡点=当前手; ox=ax; oy=ay; 下棋子();//玩家
if(结束 == 0&&人机.isChecked()){
j=(int)System.currentTimeMillis();
下棋子();//电脑
人机.setText("用时:"+((int)System.currentTimeMillis()- j)+"毫秒");
}
}
}
return true; } });
棋盘.invalidate();
}
private void 下棋子 () {
当前手++;
棋谱x[当前手] = ox;
棋谱y[当前手] = oy;
棋盘点[oy][ox] = 当前手;
落子声.play(1, 1, 1, 0, 0, 1);
p.setColor(棋盘点[oy][ox]%2==0?Color.WHITE:Color.BLACK);
g.drawCircle(ox*格宽+格宽, oy*格宽+ 格宽,半格, p);//棋子
p.setColor(棋盘点[oy][ox]%2==1?Color.WHITE:Color.BLACK);
g.drawText(棋盘点[oy][ox]+"",ox*格宽+格宽, oy*格宽+ 格宽+13,p);//序号
操作++;//过滤重复运算用的
数据改变(1);
提取分数();
棋盘.invalidate();
}
private void 数据改变 (int 正负) {
for(L = 0;L < 8;L++)//类型循环
for(成员 = 0;成员 < 下标[oy][ox][L];成员++) {
qx = 序号x[oy][ox][L][成员];
qy = 序号y[oy][ox][L][成员];
if(当前手 % 2 == 0)
白[qy][qx][L] += 正负;
else 黑[qy][qx][L] += 正负;
if(结束 == 0 && L < 4 && (白[qy][qx][L] == 5 || 黑[qy][qx][L] == 5)){
Toast.makeText(this,白[qy][qx][L] ==5? "白方胜!":"黑方胜", Toast.LENGTH_LONG).show();
结束=1;
}//棋盘标记是过滤重复运算用的
for(点成员 = 0;点成员 <= 点个数[L];点成员++)
棋盘标记[qy + Y[L] * 点成员][qx + X[L] * 点成员] = 操作;
}
}
private void 提取分数 () {
最大分数 = 0;
for(ay = 0;ay < 15;ay++)
for(ax = 0;ax < 15;ax++)//棋盘点循环
if(棋盘点[ay][ax] <= 0) {
if(棋盘标记[ay][ax] == 操作) {
if(禁手.isChecked() && 棋盘点[ay][ax] ==-1){
rect.set(格宽*ax+半格,格宽*ay+半格,格宽*ax+格宽+半格,格宽*ay+格宽+半格);
g.drawBitmap(bitmap1,rect, rect,p);
棋盘点[ay][ax]=0;
}
定位 = new int [ay][ax];
禁 = new int [16];
分数[ay][ax][0] = 0;
分数[ay][ax][1] = 0;
事[ay][ax] = "";
for(L = 0;L < 8;L++)
for(成员 = 0;成员 < 下标[ay][ax][L];成员++) {
qx = 序号x[ay][ax][L][成员];
qy = 序号y[ay][ax][L][成员];
if(白[qy][qx][L] == 0 && 黑[qy][qx][L] > 1) {
补 = 黑[qy][qx][L];//横向提取禁手数据
if(补 > 禁[L] && 补 > 3) {
if(L > 3)
禁[L - 4] = 补 == 5 ?补: 禁[L - 4] == 3 ?1: 4;
禁[L] = 补;
} else if(L < 4 && 补 == 3 && 补 >= 禁[L]) {
禁[L] = 禁[L] == 3 && 成员 > 禁[L + 12] ?6: 3;
禁[L + 12] = 成员 + 1;
} else if(L < 4 && 补 == 2 && 补 >= 禁[L])
禁[L] = ++禁[L + 8] == 2 ?2: 禁[L];
} //提取分数
if(L < 4 && (白[qy][qx][L] == 0 || 黑[qy][qx][L] == 0)) {
分数[ay][ax][1] += Math.pow(10,白[qy][qx][L]);
分数[ay][ax][0] += Math.pow(10,黑[qy][qx][L]);
}
}
for(L = 0;L < 4;L++) {//纵向提取禁手数据
if(禁[L] == 4) {事[ay][ax] = "五";break;}
if(事[ay][ax].compareTo(名[禁[L]]) == 0 && 禁[L] > 1)
事[ay][ax] += " ";
事[ay][ax] += 名[禁[L]];
}
if(禁点.isChecked()&&事[ay][ax].length()>2) {棋盘点[ay][ax]=-1;g.drawText("❌",ax*格宽+格宽,ay*格宽+格宽+10,p);}
} //提取电脑落子点
if(分数[ay][ax][当前手 % 2] >= 10000) {ox = ax;oy = ay;最大分数 = 100000;}
if(最大分数 < 分数[ay][ax][0] + 分数[ay][ax][1]) {ox = ax;oy = ay;最大分数 = 分数[ay][ax][0] + 分数[ay][ax][1];}
}
}
public void 开始Click(View v) {
事 = new String[15][15];
棋盘点 = new int[15][15];
分数 = new int[15][15][2];
白 = new int[15][15][8];
黑 = new int[15][15][8];
当前手 = 0;
结束 = 0;
g.drawBitmap(bitmap1,0,0,p);
棋盘.invalidate();
}
public void 悔棋Click(View v) {
操作++;
while(当前手 > 0 && 当前手 > 卡点) {
ox = 棋谱x[当前手];
oy = 棋谱y[当前手];
rect.set(格宽*ox+半格,格宽*oy+半格,格宽*ox+格宽+半格,格宽*oy+格宽+半格);
g.drawBitmap(bitmap1,rect, rect,p);
数据改变(-1);
棋盘点[oy][ox] = 0;
当前手--;
}
提取分数();
结束 = 0;
卡点 = 当前手 - (人机.isChecked()?2: 1);
棋盘.invalidate();
}
public void 电脑走棋Click(View v) {
if(当前手==0) {ox=7; oy=7;}
if(结束==0) 下棋子();
卡点 = 当前手 - (人机.isChecked()?2: 1);
}
}