美文网首页
无智能算法简陋cmd五子棋程序

无智能算法简陋cmd五子棋程序

作者: Tom爱吃鱼 | 来源:发表于2016-12-06 11:46 被阅读0次

    java疯狂讲义习题,以实现要求功能,未加入智能算法,后续会做五子棋GUI程序,放出代码。
    对初学者还是有点用的,第一次下五子棋赢电脑(random),哈哈!

    import java.io.*;
    
    class GobangTest 
    {
        private static final int BOARD_SIZE = 15;
        private static String[][] chessBoard = new String[BOARD_SIZE][BOARD_SIZE];
        private static void init()//创建棋盘
        {
            for(String[] e : chessBoard)
                for(int i = 0;i < BOARD_SIZE;i++)
                    e[i] = "╋";
        }
        
        private static void print()//绘制棋盘
        {
            for(String[] e : chessBoard)
            {
                for(String f : e)
                    System.out.print(f);
                System.out.println();
            }
        }
        public static void main(String[] args) throws Exception 
        {
            init();
            print();
            System.out.println("请输入您要落子的坐标,以\"x,y\"的格式:");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String inputStr = null;
            while((inputStr = br.readLine())!=null)
            {
                String[] posStrArr = inputStr.split(",");
                int xPos = Integer.parseInt(posStrArr[0]);
                int yPos = Integer.parseInt(posStrArr[1]);
                if(chessBoard[xPos-1][yPos-1]=="●"||chessBoard[xPos-1][yPos-1]=="○")
                {
                    System.out.println("你这是要干撒???");
                    continue;
                }
                chessBoard[xPos-1][yPos-1] = "●";
                int hNumRob=0,vNumRob=0,ldiaNumRob=0,rdiaNumRob=0,hNumPlay=0,vNumPlay=0,ldiaNumPlay=0,rdiaNumPlay=0;//胜负判断
                for(int i = 0;i<BOARD_SIZE;i++)
                {
                    if(chessBoard[i][yPos-1]=="●")
                    {
                        hNumPlay++;
                    }
                    else
                    {
                        hNumPlay=0;
                    }
                    if(chessBoard[xPos-1][i]=="●")
                    {
                        vNumPlay++;
                    }
                    else
                    {
                        vNumPlay=0;
                    }
                    if(hNumPlay>=5||vNumPlay>=5)
                    {
                        System.out.println("你赢了!!!");
                        init();
                        break;
                    }
                }
                int posMin = (xPos>yPos)?yPos-1:xPos-1;
                for(int i = -posMin;xPos-1+i<BOARD_SIZE&&yPos-1+i<BOARD_SIZE;i++)
                {
                    if(chessBoard[xPos-1+i][yPos-1+i] == "●")
                        rdiaNumPlay++;
                    else
                        rdiaNumPlay=0;
                    if(rdiaNumPlay>=5)
                    {
                        System.out.println("你赢了!!!");
                        init();
                        break;
                    }
                }
                posMin = (xPos-1>15-yPos)?15-yPos:xPos-1;
                for(int i = -posMin;xPos-1+i<BOARD_SIZE&&yPos-1-i>0;i++)
                {
                    if(chessBoard[xPos-1+i][yPos-1-i] == "●")
                        ldiaNumPlay++;
                    else
                        ldiaNumPlay=0;
                    if(ldiaNumPlay>=5)
                    {
                        System.out.println("你赢了!!!");
                        init();
                        break;
                    }
                }
                int robotX,robotY;
                while(true)//使用随机数来充当对手,留待改进
                {
                    robotX = (int)(Math.random()*BOARD_SIZE);
                    robotY = (int)(Math.random()*BOARD_SIZE);
                    if(chessBoard[robotX][robotY] != "●"&&chessBoard[robotX][robotY] != "○")
                    {
                        chessBoard[robotX][robotY] = "○";
                        break;
                    }
                }
                for(int i = 0;i<BOARD_SIZE;i++)
                {
                    if(chessBoard[i][robotY]=="○")
                    {
                        hNumRob++;
                    }
                    else
                    {
                        hNumRob=0;
                    }
                    if(chessBoard[robotX][i]=="○")
                    {
                        vNumRob++;
                    }
                    else
                    {
                        vNumRob=0;
                    }
                    if(hNumRob>=5||vNumRob>=5)
                    {
                        System.out.println("你输了!!!");
                        init();
                        break;
                    }
                }
                int posXMin = (robotX>robotY)?robotY:robotX;
                for(int i = -posXMin;robotX+i<BOARD_SIZE&&robotY+i<BOARD_SIZE;i++)
                {
                    if(chessBoard[robotX+i][robotY+i] == "○")
                        rdiaNumRob++;
                    else
                        rdiaNumRob=0;
                    if(rdiaNumRob>=5)
                    {
                        System.out.println("你输了!!!");
                        init();
                        break;
                    }
                }
                posXMin = (robotX>14-robotY)?14-robotY:robotX;
                for(int i = -posXMin;robotX+i<BOARD_SIZE&&robotY-i>=0;i++)
                {
                    if(chessBoard[robotX+i][robotY-i] == "○")
                        ldiaNumRob++;
                    else
                        ldiaNumRob=0;
                    if(ldiaNumRob>=5)
                    {
                        System.out.println("你输了!!!");
                        init();
                        break;
                    }
                }
                print();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:无智能算法简陋cmd五子棋程序

          本文链接:https://www.haomeiwen.com/subject/rhclmttx.html