最后是判断五子连心,将以下思路:从落子点开始检测4个方向
1.左右方向相同颜色的子是否达到5个
2.上下方向相同颜色的子是否达到5个
3.左斜方向相同颜色的子是否达到5个
4.右斜方向相同颜色的子是否达到5个
- /// <summary>
- /// 判断是否五子连心
- /// </summary>
- /// <param name="p"></param>
- /// <returns></returns>
- private bool IsWin(Piece p) {
- int x = p.X;
- int y = p.Y;
- bool win = IsFive(x, y, 0, 1, p.Color); //纵向
- win |= IsFive(x, y, 1, 1, p.Color); //右下斜
- win |= IsFive(x, y, 1, 0, p.Color); //横向
- win |= IsFive(x, y, 1, -1, p.Color); //左上斜
- return win;
- }
- private bool IsFive(int x, int y, int offsetX, int offsetY, Color c) {
- int count = 1;
- int searchX = x + offsetX;
- int searchY = y + offsetY;
- bool stop = false;
- while(searchX >= 0 && searchX < ChessBoard.Lines &&
- searchY >= 0 && searchY < ChessBoard.Lines &&
- !stop) {
- int intColor = c == Color.White ? 1 : 2;
- if(pieces[searchX, searchY] == intColor) {
- count++;
- searchX += offsetX;
- searchY += offsetY;
- } else {
- stop = true;
- }
- }
- stop = false;
- searchX = x - offsetX;
- searchY = y - offsetY;
- while(searchX >= 0 && searchX < ChessBoard.Lines &&
- searchY >= 0 && searchY < ChessBoard.Lines &&
- !stop) {
- int intColor = c == Color.White ? 1 : 2;
- if(pieces[searchX, searchY] == intColor) {
- count++;
- searchX -= offsetX;
- searchY -= offsetY;
- } else {
- stop = true;
- }
- }
- return count == 5;
- }
视频内容请见http://www.itcast.net/course/detail/1989
总结:通过这个游戏我们关注游戏本身,更重要的是运用C#的知识点。在这个项目中有很多知识点比如:面向对象、GDI+、方法封装等。
在这个项目基础上还能进行很多扩展,比如使用集合、动画效果、人机对战等。在此抛砖引玉,希望多提宝贵意见!