新手学java-运用数组写一个五子棋小程序

package GoBang;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GoBang {
    //定义棋盘大小
    private static int BOARD_SIZE=15;
    //定义一个二维数组来充当棋盘
    private String [][]board;
    public void initBoard(){
        //初始化棋盘数组
        board=new String[BOARD_SIZE][BOARD_SIZE];
        //把每个元素赋为"+",用于在控制台画出棋盘
        for(int i=0;ifor (int j=0;j"+";
            }
        }
    }   
        //在控制台输出棋盘方法        
    public void printBoard(){

        //打印每组数组元素
        for(int i=0;ifor (int j=0;j//打印数组元素后不换行
                System.out.print(board[i][j]);
            }
            //每打印完一行数组后输出一个换行符
            System.out.print("\n");
        }   
    }       
    public static void main(String[] args) {
        GoBang gb=new GoBang();
        gb.initBoard();
        gb.printBoard();
        //这是用于获取键盘输入的方法
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String inputStr=null;
        //br.readLine();每当键盘输入一行内容后按回车键,刚输入的内容将会被br读取到
        try {
            while((inputStr=br.readLine())!=null){
                //将用户输入的字符串以逗号作为分隔符,分割成两个字符
                String[]posStrArr =inputStr.split(",");
                //将两个字符串转换为用户下棋的坐标
                int  xPos=Integer.parseInt(posStrArr[0]);
                int  yPos=Integer.parseInt(posStrArr[1]);
                //把对应的数组元素赋为"."
                gb.board[yPos-1][xPos-1]=".";
                gb.printBoard();
                System.out.println("请输入你的下棋坐标,应以x,y的格式");


            }
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }




    }

}

代码运行示例
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
1,5
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
.++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
请输入你的下棋坐标,应以x,y的格式
4,6
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
.++++++++++++++
+++.+++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
+++++++++++++++
请输入你的下棋坐标,应以x,y的格式

你可能感兴趣的:(java学习)