package com.example.fivechess;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MainView extends SurfaceView implements SurfaceHolder.Callback,
Runnable {
private List<Piece> all = new ArrayList<Piece>();// 保存所有的棋子
private SurfaceHolder surfaceHolder;
private int flag = 1;
private Canvas canvas;
private int soundId;// 下棋后的音效
private SoundPool soundPool;
private float volumnRatio;
public MainView(Context context) {
super(context);
this.initSound(context);
System.out.println("MainView.MainView()");
surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
}
private void initSound(Context context) {
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 10);
soundId = soundPool.load(context, R.raw.piece_down, 5);
AudioManager audioManager = (AudioManager) context
.getSystemService(context.AUDIO_SERVICE);
float audioMaxVolumn = audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float audioCurrentVolumn = audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
volumnRatio = audioCurrentVolumn / audioMaxVolumn;
}
public void run() {
drawDesk();
}
/**
* 初始化界面,绘制棋盘和背景
*/
public void initDesk() {
System.out.println("MainView.initDesk()");
// 画笔
Paint paint = new Paint();
paint.setColor(Color.RED);
// 背景
Bitmap bitmap = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.five_chess_background));
// 画布
canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(bitmap, 0, 0, paint);
// 棋盘15*15
for (int i = 0; i <= Utils.NUMBER; i++) {
canvas.drawLine(0, i * Utils.CELL_LENGTH, Utils.NUMBER
* Utils.CELL_LENGTH, i * Utils.CELL_LENGTH, paint);
canvas.drawLine(i * Utils.CELL_LENGTH, 0, i * Utils.CELL_LENGTH,
Utils.NUMBER * Utils.CELL_LENGTH, paint);
}
}
/**
* 全部绘制桌面,桌面上的所有的图片重新绘制
*/
public void drawDesk() {
System.out.println("MainView.drawDesk()");
initDesk();
drawPieces(all);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("MainView.onTouchEvent()");
int x = (int) (event.getX() / Utils.CELL_LENGTH) * Utils.CELL_LENGTH;
int y = (int) (event.getY() / Utils.CELL_LENGTH) * Utils.CELL_LENGTH;
if (isInChessBoard(x, y) == false) {
return false;
}
soundEffect();
Piece piece = new Piece(x, y, flag++ % 2);
all.add(piece);
drawDesk();
// 落完子以后,查看是不是有胜利
int win = judgeIsWin(piece);
if (win != Utils.EMPTY) {// 如果有人胜利了
drawWinView(win);// 根据胜利的结果,画图
}
return super.onTouchEvent(event);
}
/**
* 播放音效
*/
private void soundEffect() {
if (Utils.isloud == true) {
soundPool.play(soundId, volumnRatio, volumnRatio, 1, 1, 1);
}
}
// 退出
private void drawWinView(int win) {
drawDesk();
Paint paint = new Paint();
paint.setColor(Color.RED);
Bitmap bitmap = null;
if (win == Utils.BLACK) {
bitmap = BitmapFactory.decodeStream(getResources().openRawResource(
R.drawable.black_win));
} else if (win == Utils.WHITE) {
bitmap = BitmapFactory.decodeStream(getResources().openRawResource(
R.drawable.white_win));
}
canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(bitmap, Utils.SCREEN_WIDTH / 3,
Utils.SCREEN_WIDTH / 3, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(0);
}
/**
* 判断是否棋子胜利,0,1,2分别代表,白,黑,空,
*
* @param piece
* 落子点
*/
private int judgeIsWin(Piece piece) {
System.out.println("MainView.judgeIsWin()");
if (oneJudge(piece) != Utils.EMPTY || twoJudge(piece) != Utils.EMPTY
|| threeJudge(piece) != Utils.EMPTY
|| fourJudge(piece) != Utils.EMPTY) {
System.out.println("胜利:" + piece.getProperity());
drawWinView(piece.getProperity());
return piece.getProperity();
}
return Utils.EMPTY;
}
/**
* 第一种判断方式,从棋子的纵坐标,上到下判断,返回值是黑,白,空,分别代表黑胜,白胜,没有五子连珠
*/
private int oneJudge(Piece piece) {
System.out.println("MainView.oneJudge()");
int count = 0;
for (int i = 0; i < Utils.NUMBER; i++) {
Piece p = new Piece(piece.getX(), i * Utils.CELL_LENGTH,
Utils.EMPTY);
if (isContainedPiece(p, piece)) {// 如果有这个子,且是一个颜色的
count++;
System.out.println("****count:" + count);
if (count == 5) {// 如果为5的话
return piece.getProperity();
}
} else {
count = 0;
}
}
return Utils.EMPTY;
}
/**
* 第二种判断方式,从棋子的反斜杠方向判断,返回值是黑,白,空,分别代表黑胜,白胜,没有五子连珠
*/
private int twoJudge(Piece piece) {
System.out.println("MainView.twoJudge()");
int x = piece.getX() / Utils.CELL_LENGTH;
int y = piece.getY() / Utils.CELL_LENGTH;
while (x > 0 && y > 0) {
x--;
y--;
}
int count = 0;
for (; x <= Utils.NUMBER && y <= Utils.NUMBER; x++, y++) {
Piece p = new Piece(x * Utils.CELL_LENGTH, y * Utils.CELL_LENGTH,
Utils.EMPTY);
if (isContainedPiece(p, piece)) {// 如果有这个子,且是一个颜色的
count++;
System.out.println("****count:" + count);
if (count == 5) {// 如果为5的话
return piece.getProperity();
}
} else {
count = 0;
}
}
return Utils.EMPTY;
}
/**
* 第三种判断方式,从棋子的横坐标,从左到右判断,返回值是黑,白,空,分别代表黑胜,白胜,没有五子连珠
*/
private int threeJudge(Piece piece) {
System.out.println("MainView.threeJudge()");
int count = 0;
for (int i = 0; i < Utils.NUMBER; i++) {
Piece p = new Piece(i * Utils.CELL_LENGTH, piece.getY(),
Utils.EMPTY);
if (isContainedPiece(p, piece)) {// 如果有这个子,且是一个颜色的
count++;
System.out.println("****count:" + count);
if (count == 5) {// 如果为5的话
return piece.getProperity();
}
} else {
count = 0;
}
}
return Utils.EMPTY;
}
/**
* 第四种判断方式,从棋子的正斜杠方向判断,返回值是黑,白,空,分别代表黑胜,白胜,没有五子连珠
*/
private int fourJudge(Piece piece) {
System.out.println("MainView.fourJudge()");
int x = piece.getX() / Utils.CELL_LENGTH;
int y = piece.getY() / Utils.CELL_LENGTH;
while (x > 0 && y < Utils.NUMBER) {
x--;
y++;
}
int count = 0;
for (; x <= Utils.NUMBER && y >= 0; x++, y--) {
Piece p = new Piece(x * Utils.CELL_LENGTH, y * Utils.CELL_LENGTH,
Utils.EMPTY);
if (isContainedPiece(p, piece)) {// 如果有这个子,且是一个颜色的
count++;
System.out.println("****count:" + count);
if (count == 5) {// 如果为5的话
return piece.getProperity();
}
} else {
count = 0;
}
}
return Utils.EMPTY;
}
/**
* 判断集合中是否包含这个棋子的位置,判断是不是同一种棋子
*/
public boolean isContainedPiece(Piece p, Piece piece) {
for (int i = 0; i < all.size(); i++) {
if (p.getX() == all.get(i).getX() && p.getY() == all.get(i).getY()
&& piece.getProperity() == all.get(i).getProperity()) {
return true;
}
}
return false;
}
/**
* 判断是否在棋盘,判断是否符合条件
*/
private boolean isInChessBoard(int x, int y) {
if (x < 0 || x >= Utils.NUMBER * Utils.CELL_LENGTH || y < 0
|| y >= Utils.NUMBER * Utils.CELL_LENGTH) {
return false;
}
for (int i = 0; i < all.size(); i++) {
Piece piece = all.get(i);
if (piece.getX() == x && piec