在学完了Android的基础之后,我开始尝试着写一些小项目练练手,同时进一步巩固自己的基础知识,而我选的的第一个项目就是做一个简单的人人对战的五子棋小游戏。 首先,我们要新建一个自定义控件类Panel,这基本上涵盖着整个项目的大部分操作,比如棋盘的设计等等,下面开始Panel的 ...
在学完了Android的基础之后,我开始尝试着写一些小项目练练手,同时进一步巩固自己的基础知识,而我选的的第一个项目就是做一个简单的人人对战的五子棋小游戏。
首先,我们要新建一个自定义控件类Panel,这基本上涵盖着整个项目的大部分操作,比如棋盘的设计等等,下面开始Panel的编写,代码如下:
1 public class Chess_Panel extends View{ 2 private int myPanelWidth ; //棋盘宽度 3 private float myLineHeight; //行宽 4 private int maxLine = 10; //行数 5 6 private Paint myPaint; //画笔 7 private Bitmap myWhitePice; //白棋子 8 private Bitmap myBlackPice; //黑棋子 9 private float ratioPieceOfLineHight = 3 * 1.0f / 4; //棋子为行宽的3/4;10 11 private boolean isGemOver; //游戏结束12 public static int WHITE_WIN = 0; //胜利为白方标志13 public static int BLACK_WIN = 1; //胜利为黑方标志14 private boolean isWhite = true; //判断是否是白棋先手,或当前为白棋下子15 16 private List<Point> myWhiteArray = new ArrayList<Point>(); //白棋子位置信息17 private List<Point> myBlackArray = new ArrayList<Point>(); //黑棋子位置信息18 19 private onGameListener onGameListener; //回调接口20 private int mUnder; //dialog的Y坐标21 22 public Chess_Panel(Context context) {23 this(context, null);24 }25 26 public Chess_Panel(Context context ,AttributeSet attributeSet){ //构造函数27 super(context , attributeSet);28 29 init(); 30 }31 32 //初始化函数33 private void init() { 34 myPaint = new Paint();35 myPaint.setColor(0X44ff0000); //给画笔设置颜色36 myPaint.setAntiAlias(true); //设置画笔是否使用抗锯齿37 myPaint.setDither(true); //设置画笔是否防抖动38 myPaint.setStyle(Paint.Style.STROKE); //设置画笔样式,这里使用描边39 40 myWhitePice = BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2); //设置棋子图片41 myBlackPice = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);42 43 }44
接下来就开始设计棋盘线,代码如下:
drawBitmap(Bitmap bitmap, float left, float top, Paint paint) Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置,panit为我们设计的画笔
接下来的就是游戏中的一些判断动作了:
1 //检测游戏是否结束 2 private void checkGameOver(){ 3 boolean whiteWin = checkFiveInLine(myWhiteArray); 4 boolean blackWin = checkFiveInLine(myBlackArray); 5 6 if (whiteWin || blackWin) { 7 isGemOver = true; 8 if (onGameListener != null) { 9 onGameListener.onGameOver(whiteWin ? WHITE_WIN : BLACK_WIN); 10 } 11 } 12 } 13 //回调一个int数据用于设置Dialog的位置 14 public int getUnder() { 15 16 return mUnder; 17 } 18 19 //检测是否存在五棋子相连的情况 20 private boolean checkFiveInLine(List<Point> myArray){ 21 for(Point p : myArray){ 22 int x = p.x; 23 int y = p.y; 24 25 boolean win_flag = //判断是否存在五子相连情况 26 checkHorizontal(x , y ,myArray)||checkVertical(x,y,myArray) 27 ||checkLeftDiagonal(x,y,myArray)||checkRightDiagonal(x,y,myArray); 28 if (win_flag) { 29 return true; 30 } 31 } 32 return false; 33 } 34 35 //横向检查是否满足五子相连 36 private boolean checkHorizontal(int x ,int y ,List<Point> myArray){ 37 int count = 1; 38 for(int i = 1;i < 5; i++){ 39 if (myArray.contains(new Point(x+i,y))) { 40 count++; 41 }else { 42 break; 43 } 44 } 45 if (count == 5) { 46 return true; 47 } 48 for(int i = 1;i < 5; i++){ 49 if (myArray.contains(new Point(x-i,y))) { 50 count++; 51 }else { 52 break; 53 } 54 55 56 if (count == 5) { 57 return true; 58 } 59 } 60 return false; 61 } 62 63 //纵向检查是否满足五子相连 64 private boolean checkVertical(int x ,int y ,List<Point> myArray){ 65 int count = 1; 66 for(int i = 1;i < 5; i++){ 67 if (myArray.contains(new Point(x,y+i))) { 68 count++; 69 }else { 70 break; 71 } 72 73 } 74 if (count == 5) { 75 return true; 76 } 77 for(int i = 1;i < 5; i++){ 78 if (myArray.contains(new Point(x,y-i))) { 79 count++; 80 }else { 81 break; 82 } 83 if (count == 5) { 84 return true; 85 } 86 } 87 return false; 88 } 89 90 //左斜向检查是否满足五子相连 91 private boolean checkLeftDiagonal(int x ,int y ,List<Point> myArray){ 92 int count = 1; 93 for(int i = 1;i < 5; i++){ 94 if (myArray.contains(new Point(x-i,y+i))) { 95 count++; 96 }else { 97 break; 98 } 99 100 }101 if (count == 5) {102 return true;103 }104 for(int i = 1;i < 5; i++){105 if (myArray.contains(new Point(x+i,y-i))) {106 count++;107 }else {108 break;109 }110 if (count == 5) {111 return true;112 }113 }114 return false;115 }116 117 //右斜向检查是否满足五子相连118 private boolean checkRightDiagonal(int x ,int y ,List<Point> myArray){ 119 int count = 1;120 for(int i = 1;i < 5; i++){ //切记,i = 1 开始,否则就会只检测到三个子相连就结束了121 if (myArray.contains(new Point(x-i,y-i))) {122 count++;123 }else {124 break;125 }126 }127 if (count == 5) {128 return true;129 }130 for(int i = 1;i < 5; i++){131 if (myArray.contains(new Point(x+i,y+i))) {132 count++;133 }else {134 break;135 }136 if (count == 5) {137 return true;138 }139 }140 return false;141 }142 143 //重新开始游戏144 protected void restartGame(){145 myWhiteArray.clear();146 myBlackArray.clear();147 isGemOver = false;148 isWhite = false;149 invalidate(); //刷新150 }
原标题:Android学习之旅:五子棋
关键词:Android
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。