java - 为什么将我的代码拆分到不同的类时会出现堆栈溢出错误?

标签 java

我最近制作了一款 Connect 4 游戏。但是,我在单个 Java 文件/类中完成了所有代码。我现在正在尝试重构我的代码,以便将不同的代码操作拆分到不同的类中。例如玩、反击和棋盘。

有人可以向我解释为什么我会收到以下错误吗?

Exception in thread "main" java.lang.StackOverflowError
at java.nio.Buffer.<init>(Buffer.java:201)
at java.nio.ByteBuffer.<init>(ByteBuffer.java:281)
at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:57)
at java.nio.ByteBuffer.allocate(ByteBuffer.java:335)
at sun.nio.cs.StreamDecoder.<init>(StreamDecoder.java:251)
at sun.nio.cs.StreamDecoder.<init>(StreamDecoder.java:231)
at sun.nio.cs.StreamDecoder.forInputStreamReader(StreamDecoder.java:69)
at java.io.InputStreamReader.<init>(InputStreamReader.java:74)
at Connect4Game.<init>(Connect4Game.java:19)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)
at Connect4Game.<init>(Connect4Game.java:21)
at play.<init>(play.java:2)

我真的很感激被指出正确的方向,因为这是我第一次将工作代码重构到多个类中。

这是我的代码:

Connect4Game.java

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

public class Connect4Game {

final int boardWidth=7;
final int boardHeight=7;
int totalMovesPlayed;

public BufferedReader input;
public char[][] board;

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

public Connect4Game(){
    board = new char[6][7];
    input = new BufferedReader(new InputStreamReader(System.in));

    play PlayObj = new play();
    PlayObj.playGame();

    }
}

play.java

public class play extends Connect4Game {

public void playGame() {
    System.out.println("Welcome to Connect 4");
    System.out.println("To play the game type in the number of the column you want to drop you counter in");
    System.out.println("Player One = r Player 2 = y");
    System.out.println("");


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


    boolean win = false;
    while(!win){

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

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


        boolean hasWon = false;
        int count = 0;

        // check horizontal
        for(int i=0; i<board.length; i++){
            for(int j=0; j<board[i].length; j++){
                if(board[i][j] == 'r'){
                    count = count + 1;
                    if(count == 4){
                        hasWon = true;
                        System.out.println("You Have Won!!!");
                    }
                }
                else{
                    count = 0;
                }
            }

        }

        // check vertical 
        count = 0;
        for(int i=0; i<board[0].length; i++){
            for(int j=0; j<board.length; j++){
                if(board[j][i] == 'r'){
                    count = count + 1;
                    if(count >= 4){
                        hasWon = true;
                        System.out.println("You Have Won!!!");
                    }
                }
                else{
                    count = 0;
                }
            }

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

        else {

            //player 2
            userInput = getUserInput();
            move = Integer.parseInt(userInput);


            counterObj.placeCounter('y',move);


            hasWon = false;
            count = 0;

            // check horizontal
            for(int i=0; i<board.length; i++){
                for(int j=0; j<board[i].length; j++){
                    if(board[i][j] == 'y'){
                        count = count + 1;
                        if(count >= 4){
                            hasWon = true;
                            System.out.println("You Have Won!!!");
                        }
                    }
                    else{
                        count = 0;
                    }
                }

            }

            // check vertical 
            count = 0;
            for(int i=0; i<board[0].length; i++){
                for(int j=0; j<board.length; j++){
                    if(board[j][i] == 'y'){
                        count = count + 1;
                        if(count >= 4){
                            hasWon = true;
                            System.out.println("You Have Won!!!");
                        }
                    }
                    else{
                        count = 0; 
                    }
                }

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

    }

}



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

    }
    return toReturn;
    }
}

board.java

public class board extends Connect4Game {

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();
    }

    for(int i=0;i<boardWidth;i++)
        System.out.print("*  ");
    System.out.println();

    for(int i=0;i<boardWidth;i++)
        System.out.print(i+"  ");
    System.out.println();
    }

}

counter.java

public class counter extends Connect4Game {

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 {
        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;
                }
            }
        }
    }
}
}

最佳答案

Connect4Game 的构造函数中,您正在调用子类 play 的构造函数,但该子类没有自己的构造函数,因此您实际上是在为再次 Connect4Game,然后调用自身,依此类推,直到堆栈溢出

public Connect4Game(){
  board = new char[6][7];
  input = new BufferedReader(new InputStreamReader(System.in));

  play PlayObj = new play();
  PlayObj.playGame();
}

你的main方法应该直接创建一个play对象然后调用playGame方法

public static void main(String[] args){
    play PlayObj = new play();
    PlayObj.playGame()
}

请遵循 java 命名标准,让你的类以大写字母开头

关于java - 为什么将我的代码拆分到不同的类时会出现堆栈溢出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55892014/

相关文章:

java - 如何将基于 Spring 的 RESTful 服务作为 Servlet 添加到 Tomcat

java - 定义颜色时0x30是什么?

java - Hibernate 将关联映射为 map

java - 避免在 If 条件 SonarQube 错误中使用文字

java - 如何开发使用 Android 源代码中的 C 文件的应用程序

提交多部分 post 请求时出现 java.io.FileNotFoundException

java - 如何发布具有非公共(public)依赖项的项目?

java - Jlist 自定义渲染器

java - Servlet 给出 404 而不是加载适当的页面。控制台在启动 Tomcat 时显示严重错误

java - Jruby 嵌入式模块和类