从其他类访问时方法不执行

时间:2019-05-04 23:16:16

标签: java function

最近,我在Java上构建了一个Connect四程序。但是,我是按程序构建的,所以现在我试图重构代码以确保它是面向对象的。

到目前为止,我已经设法将初始程序分为两个不同的类,如下所示:

ConnectFour.java

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

public class ConnectFour {

private BufferedReader input;


public ConnectFour() {

    input = new BufferedReader(new InputStreamReader(System.in));
    playGame();
}



private String getUserInput(){
    String toReturn = null;
    try{            
        toReturn = input.readLine();
    }
    catch(Exception e){

    }
    return toReturn;
}

public void playGame() {
    System.out.println("Welcome to Connect 4");
    System.out.println("There are 2 players red and yellow");
    System.out.println("Player 1 is Red, Player 2 is Yellow");
    System.out.println("To play the game type in the number of the column you want to drop you counter in");
    System.out.println("A player wins by connecting 4 counters in a row - vertically, horizontally or diagonally");
    System.out.println("");

    Board board = new Board();
    board.printBoard();

    boolean win = false;
    while(!win){

        // player 1
        String userInput = getUserInput();
        int move = Integer.parseInt(userInput);

        Board counter = new Board();
        counter.placeCounter('r', move);

        Board bop = new Board();
        char[][] boardx = bop.getBoard();


        boolean hasWon = false;
        int count = 0;
        // check horizontal
        for(int i=0; i<boardx.length; i++){
            for(int j=0; j<boardx[i].length; j++){
                if(boardx[i][j] == 'r'){
                    count = count + 1;
                    if(count >= 4){
                        hasWon = true;
                    }
                }
                else{
                    count = 0;
                }
            }

        }
        // check vertical 
        count = 0;
        for(int i=0; i<boardx[0].length; i++){
            for(int j=0; j<boardx.length; j++){
                if(boardx[j][i] == 'r'){
                    count = count + 1;
                    if(count >= 4){
                        hasWon = true;
                    }
                }
                else{
                    count = 0;
                }
            }

        }
        board.printBoard();
        if(hasWon){
            win = true;
            System.out.println("");
            System.out.println("R, You Have Won!!!");
        }
        else{
            //player 2
            userInput = getUserInput();
            move = Integer.parseInt(userInput);
            counter.placeCounter('y', move);;
            hasWon = false;
            count = 0;
            // check horizontal
            for(int i=0; i<boardx.length; i++){
                for(int j=0; j<boardx[i].length; j++){
                    if(boardx[i][j] == 'y'){
                        count = count + 1;
                        if(count >= 4){
                            hasWon = true;
                        }
                    }
                    else{
                        count = 0;
                    }
                }

            }
            // check vertical 
            count = 0;
            for(int i=0; i<boardx[0].length; i++){
                for(int j=0; j<boardx.length; j++){
                    if(boardx[j][i] == 'y'){
                        count = count + 1;
                        if(count >= 4){
                            hasWon = true;
                        }
                    }
                    else{
                        count = 0;
                    }
                }

            }
            board.printBoard();
            if(hasWon){
                win = true;
                System.out.println("");
                System.out.println("Y, You Have Won!!!");
            }
        }

    }

}

public static void main(String[] args) {
    new ConnectFour();
}

}

Board.java

public class Board {

private char [][] board;

public Board() {
    board = new char[6][7];

}

public char[][] getBoard() {
    return this.board;
}

public void placeCounter(char player, int position){
    boolean placed = false;


    if(player == 'r'){
        for( int i=board.length-1; i>=0; i--){
            if(!placed && board[i - 1][position] != 'r' && board[i - 1][position] != 'y') {
                if(board[i][position] == 'y'){
                    board[i-1][position] = 'r';
                    placed = true;
                }

                else if(board[i][position] != 'r'){
                    board[i][position] = 'r';
                    placed = true;
                }
            }
        }
    }

    else if (player == 'y') {
        for( int i=board.length-1; i>=0; i--){
            if (!placed && board[i - 1][position] != 'r' && board[i - 1][position] != 'y'){
                if(board[i][position] == 'r'){
                    board[i-1][position] = 'y';
                    placed = true;
                }

                else if(board[i][position] != 'y'){
                    board[i][position] = 'y';
                    placed = true;
                }
            }
        }
    }
}

public void printBoard(){

    for(int i=0;i<board.length;i++){
        for(int j=0;j<board[0].length;j++){
            if(board[i][j] == 0)
                System.out.print(".  ");
            else
                System.out.print(board[i][j]+"  ");
        }
        System.out.println();
    }
    System.out.println("*  *  *  *  *  *  *");
    System.out.println("0  1  2  3  4  5  6");
}

}

当我运行程序时,面板显示正常。但是,当我尝试通过选择行号来放置计数器时,没有计数器被添加到板上。

有人可以向我解释为什么会这样吗?我检查了我的代码很多次,从我所看到的来看,所有内容都被正确封装并正确调用。

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:0)

问题在于,您将在同一循环中继续创建新的Boards()并清除掉旧的Boards()。你为什么要把柜台当成董事会,而把鲍勃当成董事会呢?您要打印哪一个?

    // player 1
        String userInput = getUserInput();
        int move = Integer.parseInt(userInput);

        Board counter = new Board();
        counter.placeCounter('r', move);

        Board bop = new Board();
        char[][] boardx = bop.getBoard();

编辑:

请勿使用一个以上的Board类实例。请执行下列操作。如下所示,将这两个语句放在while语句上方。

      counter = new Board();
      counter.printBoard();
      while (!win) {

然后删除电路板的所有其他实例,并将所有对这些电路板的引用替换为计数器。您只需要一个。