java 五子棋编码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hufangzhou_hfz/article/details/79981305

刚刚学java不久,顺手编了一个五子棋游戏,没啥智能,电脑下棋只是随机,没有引入图形库,一个很简陋的五子棋游戏,感兴趣的朋友可以看看,下面附上源码,(纯手打):

import java.util.Scanner;
import java.lang.Math;

public class WuZiQi
{
    public static boolean ValidateStr(String str)//判断输入的字符是否合法
    {
        for(int i=0;i<str.length();i++)
        {
            if(str.equals("") && (!Character.isDigit(str.charAt(i)) && str==""))
            //if(!Character.isDigit(str.charAt(i)))
            {
                System.out.println("输入数据有误,请重新输入!");
                return false;
            }
        }
        return true;
    }
    public static void pr(char[][] str)//输出棋盘上的内容
    {
        for(int i=0;i<15;i++)
        {
            for (int j = 0; j < 15; j++)
            {
                System.out.print(str[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static int fun(char[][] pan,int x1,int y1,int x,int y)
    {
        if(pan[x1][y1] == pan[x][y])
        {
            return 1;
        }
        else
            return 0;
    }
    public static boolean result(char[][] pan,int x,int y)                   //判断输赢
    {
        int count=1;
        int flag=1;
        int x1=x;
        int y1=y;
        while(flag==1 && x1+1<=14)     //先向右查找
        {
            x1=x1+1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        flag=1; x1=x; y1=y;
        while(flag==1 && x1-1>=0)      //再向左查找
        {
            x1=x1-1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        if(count>=5)
            return true;

        count=1; flag=1; x1=x; y1=y;
        while(flag==1 && y1+1<=14)    //向下查找
        {
            y1=y1+1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;;
        }
        flag=1; x1=x;  y1=y;
        while(flag==1 && y1-1>=0)         //向上查找
        {
            y1=y1-1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        if(count>=5)
            return true;

        count=0; flag=1; x1=x; y1=y;
        while(flag==1 && x1-1>=0 && y1-1>=0)   //向左上查找
        {
            x1=x1-1;
            y1=y1-1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        flag=1; x1=x;  y1=y;
        while(flag==1 && x1+1<=14 && y1+1<=14)  //向右下查找
        {
            x1=x1+1;
            y1=y1+1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        if(count>=5)
            return true;

        count=0; flag=1; x1=x; y1=y;
        while(flag==1 && x1+1<=14 && y1-1>=0)  //向右上查找
        {
            x1=x1+1;
            y1=y1-1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        flag=1; x1=x;  y1=y;
        while(flag==1 && x1-1>=0 && y1+1<=14) //向左下查找
        {
            x1=x1-1;
            y1=y1+1;
            flag = fun(pan,x1,y1,x,y);
            count = (flag==1)?count+1:count;
        }
        if(count>=5)
            return true;
        else
            return false;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        String[] ch;
        int[] strArray;
        char[][] pan = new char[15][15];
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                pan[i][j] = '+';
            }
        }
        System.out.println("本局开始!");
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                System.out.print(pan[i][j] + " ");
            }
            System.out.println();
        }
        while(true)
        {
            int x;
            int y;
            //pan[x][y] = 1;
            System.out.println("玩家落子:");
            String str = input.nextLine();

            ch = str.split(" ");

            if(!ValidateStr(str))
            {
                continue;               //输入若不合法重新输入
            }

            strArray = new int[ch.length];
            for(int i=0;i<ch.length;i++)
            {
                strArray[i] = Integer.parseInt(ch[i]);
            }
            if(pan[strArray[0]][strArray[1]] != '+')  //有效性检验
            {System.out.println("该位置已有棋子,请重新输入!");  continue;}
            else
                pan[strArray[0]][strArray[1]]='1';
            pr(pan);
            if(result(pan,strArray[0],strArray[1]))
            {
                System.out.println("玩家获胜!");
                break;
            }


            System.out.println("电脑落子:");
            x = (int) (Math.random()*15);
            y = (int) (Math.random()*15);
            while(pan[x][y] != '+')            //有效性检验
            {
                x = (int) (Math.random()*15);
                y = (int) (Math.random()*15);
            }
            pan[x][y] = '2';
            pr(pan);
            if(result(pan,x,y))
            {
                System.out.println("电脑获胜!");
                break;
            }
        }
    }
}

阅读更多

没有更多推荐了,返回首页