1 import java.io.*; 2 public class Gobang{ 3 //定义棋盘大小 4 private static int BOARD_SIZE=15; 5 //定义一个二维数组来充当棋盘 6 private String [][] board; 7 public void initBoard(){ 8 //初始化棋盘数组 9 board=new String[BOARD_SIZE][BOARD_SIZE]; 10 //把每个元素赋值为“+”,用于在控制台画出棋盘 11 for(int i=0;i<BOARD_SIZE;i++){ 12 for(int j=0;j<BOARD_SIZE;j++){ 13 board[i][j]="+"; 14 } 15 } 16 } 17 //在控制台输出棋盘的方法 18 public void printBoard(){ 19 //打印每个数组元素 20 for(int i=0;i<BOARD_SIZE;i++){ 21 for(int j=0;j<BOARD_SIZE;j++){ 22 System.out.print(board[i][j]); 23 } 24 System.out.print("\n"); 25 } 26 } 27 public static void main(String args[]) throws Exception{ 28 Gobang w=new Gobang(); 29 w.initBoard(); 30 w.printBoard(); 31 //这是用于键盘输入的方法 32 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 33 String inputStr=null; 34 //每当从键盘输入一行内容后按回车,刚输入的内容就会被br接收到 35 while((inputStr=br.readLine())!=null){ 36 //将用户输入的字符串用“,”分隔开 37 String[] posStrArr=inputStr.split(","); 38 //将两个字符串转换成用户下棋的坐标 39 int xpos=Integer.parseInt(posStrArr[0]); 40 int ypos=Integer.parseInt(posStrArr[1]); 41 w.board[ypos-1][xpos-1]="●"; 42 w.printBoard(); 43 System.out.println("请输入您下棋的坐标,格式为x,y:"); 44 45 46 } 47 } 48 }
转载于:https://www.cnblogs.com/JayAnother/p/5078322.html