读书人

android游戏开发-06-五子棋(应用的是

发布时间: 2012-11-26 11:48:50 作者: rapoo

android游戏开发---06---五子棋(使用的是SurfaceView)

首先说明:这个五子棋使用的是SurfaceView,实际上并不好,因为这个本应该使用View类会更加合适的。但是我为了练习使用SurfaceView,所以才用了它。

SurfaceView和View那个更合适,可以看你开发的程序的性质。总结来说如下:(摘自别人博客中的一段话)

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*15for (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);}@Overridepublic 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 && piece.getY() == y) {return false;}}return true;}void drawPieces(List<Piece> all) {System.out.println("MainView.drawPieces()");Bitmap bitmap = null;for (int i = 0; i < all.size(); i++) {Piece piece = all.get(i);if (piece.getProperity() == Utils.BLACK) {bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.black));} else if (piece.getProperity() == Utils.WHITE) {bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.write));}Paint paint = new Paint();paint.setColor(Color.RED);paint.setTextSize(20);// 设置字体大小if (flag % 2 == Utils.WHITE) {// 注意flag已经加1了,如果下一步是白canvas.drawText("该白方走:", Utils.SCREEN_WIDTH / 3,Utils.SCREEN_WIDTH+ (Utils.SCREEN_HEIGHT - Utils.SCREEN_WIDTH)/ 3, paint);} else {canvas.drawText("该黑方走:", Utils.SCREEN_WIDTH / 3,Utils.SCREEN_WIDTH+ (Utils.SCREEN_HEIGHT - Utils.SCREEN_WIDTH)/ 3, paint);}canvas.drawBitmap(bitmap, piece.getX(), piece.getY(), paint);}surfaceHolder.unlockCanvasAndPost(canvas);}public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {System.out.println("MainView.surfaceChanged()");}public void surfaceCreated(SurfaceHolder holder) {System.out.println("MainView.surfaceCreated()");new Thread(this).start();}public void surfaceDestroyed(SurfaceHolder holder) {System.out.println("MainView.surfaceDestroyed()");}}

主要就是判断胜利的方式的算法,其实,我也知道,五子棋最好是用数组来存储。这样代码和运行效率是最高的,可我当时不知道怎么就非得使用list,估计脑门被加了。无所谓了,可能是因为贪食蛇也是用的list存储的,所以就很自然的用了list.有兴趣的可以自己试一试那个数组存储的,我猜一定比我这个简单,易懂。自认为自己写的代码的架构还有待提升。所以最近在狠看设计模式,学习一下人家写的代码的质量和扩展性。


好了,想下代码的,去我的资源下代码吧。





读书人网 >网络游戏

热点推荐