示例#1
0
        public bool Ma(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            if (Math.Abs(chozenX - X) == 1 && Math.Abs(chozenY - Y) == 2)                                       //横着走日字
            {
                if (Matrix[chozenX, chozenY + (Y - chozenY) / Math.Abs(Y - chozenY)].side != Base.Player.blank) //斩马腿
                {
                    return(false);
                }
            }
            else if (Math.Abs(chozenX - X) == 2 && Math.Abs(chozenY - Y) == 1)                                  //竖着走日字
            {
                if (Matrix[chozenX + (X - chozenX) / Math.Abs(X - chozenX), chozenY].side != Base.Player.blank) //斩马腿
                {
                    return(false);
                }
            }
            else
            {                                                       //避免走出日字以外的路径
                return(false);
            }

            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)             //避免走到自己方的棋子上
            {
                return(false);
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return(true);
        }
        public void makeLayout(Base[,] currentMatrix, Grid boardGrid)                        //布局,布置棋子
        {
            Button[,] btn = new Button[10, 9];
            ImageBrush brush = new ImageBrush();

            brush.ImageSource = new BitmapImage(new Uri(@"..\Resources\box.png", UriKind.RelativeOrAbsolute));

            for (int col = 0; col < 10; col++)
            {
                for (int row = 0; row < 9; row++)
                {
                    btn[col, row]                 = new Button();                   //设置棋子各种属性
                    btn[col, row].Width           = 60;
                    btn[col, row].Height          = 60;
                    btn[col, row].Margin          = new Thickness(8, 10, 0, 0);
                    btn[col, row].BorderThickness = new Thickness(0, 0, 0, 0);
                    btn[col, row].Background      = Brushes.Transparent;    //匹配parent(即前一个)的背景
                    btn[col, row]                 = loadPiecesPics(currentMatrix, btn[col, row], col, row);
                    btn[col, row].SetValue(Grid.RowProperty, col);
                    btn[col, row].SetValue(Grid.ColumnProperty, row);

                    if (path[col, row].path == Base.PiecePath.yes)
                    {
                        btn[col, row].Background = brush;   //画刷,即使之能画出路径
                    }

                    boardGrid.Children.Add(btn[col, row]);
                }
            }
            btnEvent(btn);         //赋予按钮事件
        }
示例#3
0
        public bool Shi(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            if (Matrix[chozenX, chozenY].side == Base.Player.black)
            {
                if (Y < 3 || Y > 5 || X > 2)
                {
                    return(false);
                }
            }
            else
            {
                if (Y < 3 || Y > 5 || X < 7)
                {
                    return(false);
                }
            }

            if (Math.Abs(X - chozenX) != 1 || Math.Abs(chozenY - Y) != 1)
            {
                return(false);
            }

            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)
            {
                return(false);
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return(true);
        }
示例#4
0
        public bool checkResult(Base[,] Matrix)       //判断结果
        {
            int  count  = 0;
            bool result = true;

            for (int col = 0; col < 10; col++)
            {
                for (int row = 0; row < 9; row++)
                {
                    if (Matrix[col, row].type == Base.PieceType.jiang)
                    {
                        count++;
                    }
                }
            }

            if (count == 2)
            {
                return(result);
            }
            else
            {
                result = false;
                return(result);
            }
        }
示例#5
0
 public void getMove(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)       //基本移动方式
 {
     Matrix[X, Y].side             = Matrix[chozenX, chozenY].side;
     Matrix[X, Y].type             = Matrix[chozenX, chozenY].type;
     Matrix[chozenX, chozenY].side = Base.Player.blank;
     Matrix[chozenX, chozenY].type = Base.PieceType.blank;
 }
示例#6
0
    public Grid(int width, int height)
    {
        Width  = width;
        Height = height;

        _grid = new Base[Width, Height];
    }
        public MainWindow()
        {
            InitializeComponent();

            currentMatrix = proMod.setGround();                 //初始化当前的矩阵,即实际的棋盘按钮矩阵
            path          = proMod.setRoad();                   //初始化棋子的路径
            makeGrid(currentMatrix);                            //打印棋盘与棋子
        }
示例#8
0
    public MapData(int width, int height, float tileSize)
    {
        this.width    = width;
        this.height   = height;
        this.tileSize = tileSize;

        tiles = new int[width, height];
        objs  = new Base[width, height];
    }
示例#9
0
        public bool Bing(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            if (X != chozenX && Y != chozenY)
            {
                return(false);
            }

            if (Matrix[chozenX, chozenY].side == Base.Player.black)
            {
                if (chozenX < 5 && X - chozenX != 1)
                {
                    return(false);
                }

                if (chozenX > 4)
                {
                    if (X == chozenX && Math.Abs(Y - chozenY) != 1)
                    {
                        return(false);
                    }

                    if (Y == chozenY && X - chozenX != 1)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                if (chozenX > 4 && chozenX - X != 1)
                {
                    return(false);
                }

                if (chozenX < 5)
                {
                    if (X == chozenX && Math.Abs(Y - chozenY) != 1)
                    {
                        return(false);
                    }

                    if (Y == chozenY && chozenX - X != 1)
                    {
                        return(false);
                    }
                }
            }

            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)
            {
                return(false);
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return(true);
        }
示例#10
0
        public bool Jiang(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            int col, row, temp;

            if (Matrix[X, Y].type == Base.PieceType.jiang && chozenY == Y)         //飞将
            {
                col = chozenX < X ? chozenX : X;
                row = chozenX > X ? chozenX : X;

                for (temp = col + 1; temp < row; temp++)                             //遍历当前列,当双方将军直接有其他棋子时不能飞将
                {
                    if (Matrix[temp, Y].side != Base.Player.blank)
                    {
                        return false;
                    }
                }

                if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)         //避免走到自己方的棋子上(其实这在飞将时有点多余)
                {
                    return false;
                }

                getMove(X, Y, chozenX, chozenY, Matrix);

                return true;        //先一个setmove,为了能飞将时能移动,避免被下面的条件限制
            }

            if (Matrix[chozenX, chozenY].side == Base.Player.black)        //限制黑方将军在九宫格内移动
            {
                if (Y < 3 || Y > 5 || X > 2)
                {
                    return false;
                }
            }
            else
            {                                                       //限制红方将军在九宫格内移动
                if (Y < 3 || Y > 5 || X < 7)
                {
                    return false;
                }
            }

            if ((chozenX - X) * (chozenX - X) + (chozenY - Y) * (chozenY - Y) != 1)
            {
                return false;                           //水平移动时,移动距离仅为1格 || 竖直移动时,移动距离为1格 || 避免第一次移动将时可移动距离大于一个格
            }

            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)             //避免走到自己方的棋子上
            {
                return false;
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return true;
        }
示例#11
0
        public bool Che(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            int col, row, temp;

            if (chozenX == X)
            {
                col = chozenY < Y ? chozenY : Y;//如果chozenY<Y为ture 返回chozenY 否则Y;
                row = chozenY > Y ? chozenY : Y;

                for (temp = col + 1; temp < row; temp++)
                {
                    if (Matrix[X, temp].side != Base.Player.blank)
                    {
                        return(false);
                    }
                }
            }

            if (chozenY == Y)
            {
                col = chozenX < X ? chozenX : X;
                row = chozenX > X ? chozenX : X;

                for (temp = col + 1; temp < row; temp++)
                {
                    if (Matrix[temp, Y].side != Base.Player.blank)
                    {
                        return(false);
                    }
                }
            }

            if (chozenX != X && chozenY != Y)
            {
                return(false);
            }

            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)
            {
                return(false);
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return(true);
        }
示例#12
0
        public bool movePiece(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)       //定义每种棋子的移动方式
        {
            Rook     che   = new Rook();
            Horse    ma    = new Horse();
            Elephant xiang = new Elephant();
            Guard    shi   = new Guard();
            General  jiang = new General();
            Cannon   pao   = new Cannon();
            Soldier  bing  = new Soldier();
            bool     re;

            switch (Matrix[chozenX, chozenY].type)
            {
            case Base.PieceType.che:
                re = che.Che(X, Y, chozenX, chozenY, Matrix);
                return(re);

            case Base.PieceType.ma:
                re = ma.Ma(X, Y, chozenX, chozenY, Matrix);
                return(re);

            case Base.PieceType.xiang:
                re = xiang.Xiang(X, Y, chozenX, chozenY, Matrix);
                return(re);

            case Base.PieceType.shi:
                re = shi.Shi(X, Y, chozenX, chozenY, Matrix);
                return(re);

            case Base.PieceType.jiang:
                re = jiang.Jiang(X, Y, chozenX, chozenY, Matrix);
                return(re);

            case Base.PieceType.pao:
                re = pao.Pao(X, Y, chozenX, chozenY, Matrix);
                return(re);

            case Base.PieceType.bing:
                re = bing.Bing(X, Y, chozenX, chozenY, Matrix);
                return(re);
            }

            return(false);
        }
示例#13
0
        public void makeGrid(Base[,] currentMatrix)
        {
            Grid totalGrid = new Grid();                                    //创建背景的boardGrid

            this.Content = totalGrid;
            Grid boardGrid = new Grid();                                        //创建棋盘的boardGrid

            totalGrid.Children.Add(boardGrid);                                  //在totalGrid里添加boardGrid
            boardGrid.HorizontalAlignment = HorizontalAlignment.Left;           //使boardGrid打开在左方
            WindowStartupLocation         = WindowStartupLocation.CenterScreen; //让窗口在屏幕中央打开

            ImageBrush background = new ImageBrush();                           //加载背景图

            background.ImageSource = new BitmapImage(new Uri(@"..\Resources\background.png", UriKind.RelativeOrAbsolute));
            totalGrid.Background   = background;


            ImageBrush board = new ImageBrush();                                            //加载棋盘图

            board.ImageSource    = new BitmapImage(new Uri(@"..\Resources\gameboard.png", UriKind.RelativeOrAbsolute));
            boardGrid.Background = board;

            ColumnDefinition[] col = new ColumnDefinition[9];                               //创建行和列
            for (int i = 0; i < 9; i++)
            {
                col[i]       = new ColumnDefinition();
                col[i].Width = new GridLength(75);                                      //设置宽的长度
                boardGrid.ColumnDefinitions.Add(col[i]);
            }

            RowDefinition[] row = new RowDefinition[10];
            for (int i = 0; i < 10; i++)
            {
                row[i]        = new RowDefinition();
                row[i].Height = new GridLength(75);                                     //设置高的长度
                boardGrid.RowDefinitions.Add(row[i]);
            }

            showWords(totalGrid);                                                       //打印文字
            makeLayout(currentMatrix, boardGrid);                                       //打印布局
        }
示例#14
0
        public Base[,] showRoad(int chozenX, int chozenY, Base[,] Matrix) // 使棋子显示可行路径时,并使移动前的棋子不移动
        {
            Piece mod = new Piece();                                      //实例化,方便使用该类里的方法

            Base[,] road  = mod.setRoad();                                //用Chess类创建出一个[19,17]的road数组,并一个个实例,再初始化路径,即将Chess.Piecepath.not赋到每个road里
            Base[,] trans = new Base[10, 9];                              //用Chess类创建出一个[19,17]的trans数组,方便临时储存信息
            bool cr;

            for (int col = 0; col < 10; col++)                //遍历整个棋盘
            {
                for (int row = 0; row < 9; row++)
                {
                    trans[col, row] = new Base();             //通过循环,一个个地具体实例化每个trans
                }
            }

            for (int col = 0; col < 10; col++)                //遍历整个棋盘
            {
                for (int row = 0; row < 9; row++)
                {
                    trans[col, row].side         = Matrix[col, row].side;         //把每个位置的side属性一个个赋到trans上暂时储存
                    trans[col, row].type         = Matrix[col, row].type;         //把每个位置的type属性一个个赋到trans上暂时储存
                    trans[chozenX, chozenY].side = Matrix[chozenX, chozenY].side; //把当前选择的具体位置的side属性赋到trans暂时储存
                    trans[chozenX, chozenY].type = Matrix[chozenX, chozenY].type; //把当前选择的具体位置的type属性赋到trans暂时储存
                    cr = movePiece(col, row, chozenX, chozenY, Matrix);           //使用该方法依据棋子类型,通过遍历一个个检查当前选择的棋子能走的格子,通过返回true或false来形成路径

                    if (cr == true)                                               //如果该格子能走
                    {
                        road[col, row].path = Base.PiecePath.yes;                 //把该格子的位置的path属性里赋上Chess.Piecepath.yes,即把之前初始化的not变成了yes
                    }

                    Matrix[col, row].side         = trans[col, row].side;       //每遍历了一个位置后及时把该位置的属性再赋回去,避免选择该棋子准备进行移动时棋子就已经提前移动影响整个函数的判断
                    Matrix[col, row].type         = trans[col, row].type;
                    Matrix[chozenX, chozenY].side = trans[chozenX, chozenY].side;
                    Matrix[chozenX, chozenY].type = trans[chozenX, chozenY].type;
                }
            }

            return(road);            //返回road即可行路径
        }
示例#15
0
        public bool Xiang(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            if (Matrix[chozenX, chozenY].side == Base.Player.black) //使象不能过河
            {
                if (X > 4)                                          //4和5为河界的行坐标
                {
                    return(false);
                }
            }
            else
            {
                if (X < 5)
                {
                    return(false);
                }
            }

            if ((X - chozenX) * (X - chozenX) + (chozenY - Y) * (chozenY - Y) != 8)      //选择的行坐标与移动前的行坐标的差的1/2的绝对值乘选择的列坐标和移动前的列坐标,其积不等于8时,粗略画出田字的路径。筛选条件1
            {
                return(false);
            }

            if (Matrix[(X + chozenX) / 2, (Y + chozenY) / 2].side != Base.Player.blank)        //使象走田字时,当要走的田中心有棋子时不能移动
            {
                return(false);
            }


            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)                 //避免走到自己方的棋子上
            {
                return(false);
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return(true);
        }
示例#16
0
        public bool Pao(int X, int Y, int chozenX, int chozenY, Base[,] Matrix)
        {
            int col, row, temp, count;

            if (chozenX == X)
            {
                col   = chozenY < Y ? chozenY : Y;
                row   = chozenY > Y ? chozenY : Y;
                count = 0;

                for (temp = col + 1; temp < row; temp++)
                {
                    if (Matrix[X, temp].side != Base.Player.blank)
                    {
                        count++;
                    }
                }

                if (count > 1)
                {
                    return(false);
                }
            }
            else if (chozenY == Y)
            {
                col   = chozenX < X ? chozenX : X;
                row   = chozenX > X ? chozenX : X;
                count = 0;

                for (temp = col + 1; temp < row; temp++)
                {
                    if (Matrix[temp, Y].side != Base.Player.blank)
                    {
                        count++;
                    }
                }

                if (count > 1)
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            if (count == 0 && Matrix[X, Y].side != Base.Player.blank)
            {
                return(false);
            }

            if (count == 1 && Matrix[X, Y].side == Base.Player.blank)
            {
                return(false);
            }

            if (Matrix[chozenX, chozenY].side == Matrix[X, Y].side)
            {
                return(false);
            }

            getMove(X, Y, chozenX, chozenY, Matrix);

            return(true);
        }
示例#17
0
        public void ckEvent(int btnRow, int btnCol)               //点击发生时进行事件的内容
        {
            bool result;

            if (firstClicked == true)                                                            //选中想移动的棋子
            {
                currentX = btnRow;
                currentY = btnCol;

                if (currentX == chozenX && currentY == chozenY)                 //再次选中到自己
                {
                    msgShow(1, count);
                    makeGrid(currentMatrix);                                                           //更新boardGrid,即把选中棋子时已显示的路径去掉
                }
                else                                                                                   //成功选中
                {
                    bool move = proCon.movePiece(currentX, currentY, chozenX, chozenY, currentMatrix); //检测该位置上的棋子的移动规则并给能走的位置标记

                    if (move == false)
                    {
                        msgShow(2, count);                                                       //如果选到已被标记不能走的位置
                    }
                    else
                    {
                        count++;                                            //选到能走的位置时,回合数加1,进行下一步
                    }
                    makeGrid(currentMatrix);                                //更新boardGrid,使棋子在显示上发生移动
                    result = proCon.checkResult(currentMatrix);             //检查游戏是否结束

                    if (result == false)                                    //如果结束
                    {
                        //makeGrid(currentMatrix);
                        if (count % 2 == 1)
                        {
                            msgShow(6, count);                                      //红方赢
                        }
                        else
                        {
                            msgShow(7, count);          //黑方赢
                        }
                        Environment.Exit(0);            //游戏结束自动退出程序
                    }
                }

                firstClicked = false;            //标记当前棋子已被选中
            }

            else          //选中了棋子开始选择移动时
            {
                chozenX = btnRow;
                chozenY = btnCol;

                if (currentMatrix[chozenX, chozenY].side == Base.Player.blank)      //选中到了空位置
                {
                    msgShow(3, count);
                }
                else if (count % 2 == 0)                                           //红方时
                {
                    if (currentMatrix[chozenX, chozenY].side == Base.Player.black) //选中到了黑方
                    {
                        msgShow(4, count);
                    }
                    else
                    {
                        path         = proCon.showRoad(chozenX, chozenY, currentMatrix); //显示当前棋子可行路径
                        firstClicked = true;                                             //初始化标记,使之回复到未选中棋子状态
                        makeGrid(currentMatrix);                                         //更新boardGrid
                        path = proMod.setRoad();                                         //初始化路径
                    }
                }
                else if (count % 2 == 1)                                         //黑方时
                {
                    if (currentMatrix[chozenX, chozenY].side == Base.Player.red) //选中到了红方
                    {
                        msgShow(5, count);
                    }
                    else
                    {                                                               //作用同上
                        path         = proCon.showRoad(chozenX, chozenY, currentMatrix);
                        firstClicked = true;
                        makeGrid(currentMatrix);
                        path = proMod.setRoad();
                    }
                }
            }
        }
示例#18
0
        public Button loadPiecesPics(Base[,] currentMatrix, Button btn, int col, int row)           //加载各个棋子的图片
        {
            switch (currentMatrix[col, row].side)
            {
            case Base.Player.red:
                switch (currentMatrix[col, row].type)
                {
                case Base.PieceType.che:
                    ImageBrush Red_che = new ImageBrush();
                    Red_che.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_che.png", UriKind.RelativeOrAbsolute));
                    btn.Background      = Red_che;
                    return(btn);

                case Base.PieceType.ma:
                    ImageBrush Red_ma = new ImageBrush();
                    Red_ma.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_ma.png", UriKind.RelativeOrAbsolute));
                    btn.Background     = Red_ma;
                    return(btn);

                case Base.PieceType.xiang:
                    ImageBrush Red_xiang = new ImageBrush();
                    Red_xiang.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_xiang.png", UriKind.RelativeOrAbsolute));
                    btn.Background        = Red_xiang;
                    return(btn);

                case Base.PieceType.shi:
                    ImageBrush Red_shi = new ImageBrush();
                    Red_shi.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_shi.png", UriKind.RelativeOrAbsolute));
                    btn.Background      = Red_shi;
                    return(btn);

                case Base.PieceType.jiang:
                    ImageBrush Red_jiang = new ImageBrush();
                    Red_jiang.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_jiang.png", UriKind.RelativeOrAbsolute));
                    btn.Background        = Red_jiang;
                    return(btn);

                case Base.PieceType.pao:
                    ImageBrush Red_pao = new ImageBrush();
                    Red_pao.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_pao.png", UriKind.RelativeOrAbsolute));
                    btn.Background      = Red_pao;
                    return(btn);

                case Base.PieceType.bing:
                    ImageBrush Red_bing = new ImageBrush();
                    Red_bing.ImageSource = new BitmapImage(new Uri(@"..\Resources\red_bing.png", UriKind.RelativeOrAbsolute));
                    btn.Background       = Red_bing;
                    return(btn);
                }
                return(btn);

            case Base.Player.black:
                switch (currentMatrix[col, row].type)
                {
                case Base.PieceType.che:
                    ImageBrush Black_che = new ImageBrush();
                    Black_che.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_che.png", UriKind.RelativeOrAbsolute));
                    btn.Background        = Black_che;
                    return(btn);

                case Base.PieceType.ma:
                    ImageBrush Black_ma = new ImageBrush();
                    Black_ma.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_ma.png", UriKind.RelativeOrAbsolute));
                    btn.Background       = Black_ma;
                    return(btn);

                case Base.PieceType.xiang:
                    ImageBrush Black_xiang = new ImageBrush();
                    Black_xiang.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_xiang.png", UriKind.RelativeOrAbsolute));
                    btn.Background          = Black_xiang;
                    return(btn);

                case Base.PieceType.shi:
                    ImageBrush Black_shi = new ImageBrush();
                    Black_shi.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_shi.png", UriKind.RelativeOrAbsolute));
                    btn.Background        = Black_shi;
                    return(btn);

                case Base.PieceType.jiang:
                    ImageBrush Black_jiang = new ImageBrush();
                    Black_jiang.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_jiang.png", UriKind.RelativeOrAbsolute));
                    btn.Background          = Black_jiang;
                    return(btn);

                case Base.PieceType.pao:
                    ImageBrush Black_pao = new ImageBrush();
                    Black_pao.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_pao.png", UriKind.RelativeOrAbsolute));
                    btn.Background        = Black_pao;
                    return(btn);

                case Base.PieceType.bing:
                    ImageBrush Black_bing = new ImageBrush();
                    Black_bing.ImageSource = new BitmapImage(new Uri(@"..\Resources\black_bing.png", UriKind.RelativeOrAbsolute));
                    btn.Background         = Black_bing;
                    return(btn);
                }
                return(btn);
            }
            return(btn);
        }