Jack Armstrong
Jack Armstrong

Reputation: 1249

Ending program properly

I have a game, ConnectFour that plays and what not. But when the user is finished with a game, there is a prompt that says "would you like to play again? If the user types yes, then it works. But if the user types no, it prints, "Thanks for playing." But the output in the terminal window is:

Congrats Red won the game.

Would you like to play again? Y/N

n

Thanks for playing.

It does not reload the line that you can use to input stuff back into the terminal. This line

Jacks-MacBook-Pro:Cs124 Jack_XXX$ 

I don't know if that's a function of the main program using a GUI and how it wont close. But I have to hit control C every time to end the program this way and I want to know if there is a way to stop that.

The code for main is:

public static void main(String[] args) {

ConnectFour connectFour= new ConnectFour();
BoardView view =  connectFour.getView();

JFrame worldFrame = new JFrame();
JPanel buttonPanel = new JPanel();


view.repaint();

worldFrame.setContentPane(view);
worldFrame.add(buttonPanel);
worldFrame.validate();
worldFrame.pack();
worldFrame.setVisible(true);


Scanner input = new Scanner(System.in);
System.out.println("Welcome to Connect Four. The object of the game is to get four of the same colored pieces in a row.");
System.out.println("Red will go first.");

//sets up the gameboard and then draws it
for (int i = 0; i < connectFour.getGameBoard().length; i++) {
  for (int j = 0; j < connectFour.getGameBoard()[0].length; j++) {
    connectFour.getGameBoard()[i][j] = 0;
  }
}

view.repaint();

connectFour.printGameBoard();

String pwon = "";
boolean playgame=true;
//actual gamecode
while(playgame=true){
    boolean game=false;
    while (game == false) {

        System.out.println("Red, which column would you like to move into?");
        System.out.println("Note Column Numbers 1 to 7 are accepted.");

        int x = input.nextInt();
        connectFour.move1(x, connectFour.getGameBoard());
        if ((connectFour.getGameBoard()[0][x-1]==1)||(connectFour.getGameBoard()[0][x-1]==2)){
            System.out.println("Invalid move. You lost your turn");
        }
        view.repaint();
        connectFour.printGameBoard();

        if (connectFour.isFourInaRow()) {
            pwon = "Red";
            break;
        }

        System.out.println("Black, which column would you like to move into?");
        System.out.println("Note Column Numbers 1 to 7 are accepted.");

        x = input.nextInt();
        connectFour.move2(x, connectFour.getGameBoard());
        if ((connectFour.getGameBoard()[0][x-1]==1)||(connectFour.getGameBoard()[0][x-1]==2)){
            System.out.println("Invalid move. You lost your turn");
        }
        view.repaint();
        connectFour.printGameBoard();

        if (connectFour.isFourInaRow()) {
            pwon = "Black";
            break;
        }

        }

    if (connectFour.checkRow() == true) {
        System.out.println("row");
    } else if (connectFour.checkColumn() == true) {
        System.out.println("col");
    } else if (connectFour.checkDiag() == true) {
        System.out.println("diag");
    } else{ 
        System.out.println("draw");

    }

    System.out.println("Congrats " + pwon + " won the game.");
    System.out.println("Would you like to play again? Y/N");
    input.nextLine();
    String answer = input.nextLine();

    if((answer.equals("N"))||(answer.equals("n"))){
        break;
    }
 }
 System.out.println("Thanks for playing.");


 }

Upvotes: 2

Views: 105

Answers (4)

TheRealFawby
TheRealFawby

Reputation: 112

try this :

if ( answer . toUpperCase ( ) . equals ( "N" ) ) 
{
    System . out . println ( "Thanks for playing!" ) ; 
    System . exit ( 0 ) ; 
}

Upvotes: 1

James Parsons
James Parsons

Reputation: 6057

You could use the System.exit(); to exit the application. Also, you should set a default close operation on your Jframe. Something like worldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Upvotes: 0

Sebastian Redl
Sebastian Redl

Reputation: 71989

When you create a JFrame, the program starts a new thread running the message loop. It won't terminate while this loop is running.

Simplest thing is to just call System.exit(0).

Upvotes: 3

Mailkov
Mailkov

Reputation: 1231

Two input.nextLine()?

input.nextLine();
String answer = input.nextLine();

you write only

String answer = input.nextLine();

Upvotes: 1

Related Questions