【Little_things】控制台五子棋(java)

   一共4个类:

  1,Board类 主要方法:

      1.1void initBoard(),初始化棋盘

      1.2void displayBoard(),输出棋盘

      1.3String[][] getBoard(),获取棋盘

2.Chessman类,是枚举类

3.Gogame类,主要方法

      3.1String input(),输入

      3.2 boolean isValid(String input()),判断输入是否合法

      3.3boolean isReplay(String Chessman),Chessman是黑子或白子,判断是否重玩

      3.4boolean isWon(int x,int y,String ico),x,y是落子的坐标,ico是棋子类型,判断是否赢了

      3.5int[] computerDo(int x,int y,int oldx,int oldy),x,y为当前落子点oldx,oldy为前一落子点,计算机生成落子点

      3.6void start()  启动游戏

4.Main类


完整代码:

package com.ztc.Chess;

/**
 * Created by ztc on 15-10-24.
 */

public class Board {
    static  final int SIZE=15;
    String[][] board;
    public void initBoard()     //
    {
        board = new String[SIZE][SIZE];
        for(int i=0;i<SIZE;i++)
            for(int j=0;j<SIZE;j++)
            {
                board[i][j]="十";
            }
    }

    public void displayBoard()
    {
        for(int i=0;i<SIZE;i++)
        {
            for(int j=0;j<SIZE;j++)
            {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
    }
    public String[][] getBoard()
    {
        return board;
    }
}

package com.ztc.Chess;

/**
 * Created by ztc on 15-10-24.
 */
public enum Chessman {
    BLACK("X"),WRITE("O");
    private String chessman;
    private Chessman(String chessman)
    {
        this.chessman=chessman;
    }

    public String getChessman()
    {
        return this.chessman;
    }
}

package com.ztc.Chess;

import java.util.Scanner;

/**
 * Created by ztc on 15-10-24.
 */
public class Gogame {
    Scanner in;     //=new Scanner(System.in);
    int X=7,Y=7,oldX,oldY;
    Board board=new Board();
    public String input()
    {
        in=new Scanner(System.in);
        return in.nextLine();
    }
    public boolean isValid(String xy)
    {
        String[] x_y=xy.split(",");          
        try {
            X = Integer.parseInt(x_y[0]) - 1;
            Y = Integer.parseInt(x_y[1]) - 1;
        }catch (NumberFormatException e)
        {
            board.displayBoard();
            System.out.println("请以 数字,数字 的格式输入!");
            return false;
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            board.displayBoard();
            System.out.println("请以 数字,数字 的格式输入!");
            return false;
        }
        if(X>board.SIZE-1||X<0||Y<0||Y>board.SIZE-1)
        {
            board.displayBoard();
            System.out.println("1<=(X,Y)<="+board.SIZE);
            return false;
        }
        if(board.getBoard()[X][Y]!="十") {
            board.displayBoard();
            System.out.println("该位置已有棋子!");
            return false;
        }
        return true;
    }

    public boolean isReplay(String chessman)
    {
        board.displayBoard();
        String message=chessman.equals(Chessman.BLACK.getChessman())?"恭喜,YOU WIN!
":"YOU LOSE!";
        System.out.println(message+"在来一局?(y/n)");
        in=new Scanner(System.in);
        String br=in.nextLine();
        if(br.equals("y"))
            return true;
        return false;
    }

    public boolean isWon(int x,int y,String ico)
    {
        int num=1;
        for(int i=1;i<5;i++)   //纵向
        {

            if((x+i)<Board.SIZE&&board.board[x+i][y].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {

            if((x-i)>=0&&board.board[x-i][y].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("纵"+num);
        if(num==5)
            return true;
        else
            num=1;
        for(int i=1;i<5;i++)   //横向
        {
            if((y+i)<Board.SIZE&&board.board[x][y+i].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {
            if((y-i)>=0&&board.board[x][y-i].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("横"+num);
        if(num==5)
            return true;
        else
            num=1;
        for(int i=1;i<5;i++)   //""向
        {
            if((x+i)<Board.SIZE&&(y+i)<Board.SIZE&&board.board[x+i][y+i].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {
            if((x-i)>=0&&(y-i)>=0&&board.board[x-i][y-i].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("\"+num);
        if(num==5)
            return true;
        else
            num=1;
        for(int i=1;i<5;i++)   //"/"向
        {
            if((x+i)<Board.SIZE&&(y-i)>=0&&board.board[x+i][y-i].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {
            if((x-i)>=0&&(y+i)<Board.SIZE&&board.board[x-i][y+i].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("/"+num);
        if(num==5)
            return true;
        else
            num=1;

        return false;
    }

    public int[] computerDo(int x,int y,int oldx,int oldy)
    {
        /*
        int poxX=(int)(Math.random()*Board.SIZE-1);
        int poxY=(int)(Math.random()*Board.SIZE-1);
        while(board.board[poxX][poxY]!="十")
        {
            poxX=(int)(Math.random()*Board.SIZE-1);
            poxY=(int)(Math.random()*Board.SIZE-1);
        }
        int[] a={poxX,poxY};
        return a;
        */

        int poxX=13,poxY=13,i=1;
        while(poxX>0&&poxY>0&&poxX<Board.SIZE&&poxY<Board.SIZE&&board.board[poxX][poxY]!="十")
        {
            if(x-i>=0&&x+i<Board.SIZE&&y-i>=0&&y+i<Board.SIZE) {
                if (x > oldx && y > oldy) {
                    poxX = x + i;
                    poxY = x + i;
                } else if (x > oldx && y < oldy) {
                    poxX = x + i;
                    poxY = y - i;
                } else if (x < oldx && y > oldy) {
                    poxX = x - i;
                    poxY = y + i;
                } else if (x < oldx && y < oldy) {
                    poxX = x - i;
                    poxY = y - i;
                } else if (x == oldx && y < oldy) {
                    poxX = x;
                    poxY = y - i;
                } else if (x == oldx && y > oldy) {
                    poxX = x;
                    poxY = y + i;
                } else if (x < oldx && y == oldy) {
                    poxX = x - i;
                    poxY = y;
                } else {
                    poxX = x + 1;
                    poxY = y;
                }
            }else
            {
                poxX=(int)(Math.random()*Board.SIZE-1);
                poxY=(int)(Math.random()*Board.SIZE-1);
            }
            i++;
        }

        int[] a={poxX,poxY};
        return a;
    }

    public void start()
    {
        board.initBoard();
        while(true)
        {
            board.displayBoard();
            boolean sign;
            oldX=X;
            oldY=Y;
            do
             {
                sign=isValid(input());
             }while(!sign);
            board.board[X][Y]=Chessman.BLACK.getChessman();
            if(isWon(X,Y,Chessman.BLACK.getChessman()))
             {
                 if(isReplay(Chessman.BLACK.getChessman()))
                        start();
                 else
                        break;
             }
            int[] CS=computerDo(X,Y,oldX,oldY);
            board.board[CS[0]][CS[1]]=Chessman.WRITE.getChessman();
            if(isWon(X, Y, Chessman.WRITE.getChessman()))
            {
                if(isReplay(Chessman.WRITE.getChessman()))
                    start();
                else
                    break;
            }
        }

    }

}

package com.ztc.Chess;

/**
 * Created by ztc on 15-10-24.
 */
public class Main {
    public static void main(String[] args)
    {
        Gogame go=new Gogame();
        go.start();
    }
}





原文地址:https://www.cnblogs.com/A-yes/p/9894229.html