0

I have made this code for a tic tac toe game and it works perfectly fine except at the end of the code I ask the user a question to if they want to play again. I have id they say no to close the program but i cant find a way to make the program restart if they say yes. Thank you for the help!

import hsa.Console;
import java.awt.*;
import java.util.*;

public class tictactoe88
{ static Console c;
public static void main(String[]args)

{ c = new Console();

//Drawing the tic tac toe table
c.setColor(Color.black);
c.drawLine(450,250,450,475);
c.drawLine(550,250,550,475);
c.drawLine(350,325,650,325);
c.drawLine(350,400,650,400);

//Declaring variables
int location;
String name1, name2, endGame;

String[][] names = new String[3][3];

int[][] chart = 
{
  {1,2,3},
  {4,5,6},
  {7,8,9},
};

//Showing players what number corresponds to tic tac toe board
c.println("1  2  3");
c.println("4  5  6");
c.println("7  8  9");

//Asking players for their names
c.println("Names");
c.print("Player One : ");
name1 = c.readLine ();
c.print("Player Two : ");
name2 = c.readLine ();  

//Starting of main loop
while(true)
{
  c.setCursor(7,0);
  c.println("Where would you like to place the 'X' " + name1);

  //Making sure the number entered is valid
  while(true)
  {
    try 
    { String locationStr = c.readLine();
      location = Integer.parseInt(locationStr);
      break;
    }
    catch(NumberFormatException e)
    { c.println("Bad number, please try again.");
    }
  }
  //Drawing the 'X' and adjusting the 2d array's correspomding to the number entered
  {
    if (location == 1)
    {
      c.drawLine(350,250,450,325);
      c.drawLine(450,250,350,325);  
      chart [0][0] = 20;
    }
    else if (location == 2)
    {
      c.drawLine(450,250,550,325);
      c.drawLine(550,250,450,325);    
      chart [0][1] = 20;
    }
    else if(location == 3)
    { 
      c.drawLine(550,250,650,325);
      c.drawLine(650,250,550,325);  
      chart [0][2] = 20;
    }
    else if (location == 4)
    { 
      c.drawLine(350,325,450,400);
      c.drawLine(350,400,450,325);
      chart [1][0] = 20;
    }
    else if (location == 5)
    { 
      c.drawLine(450,325,550,400);
      c.drawLine(450,400,550,325); 
      chart [1][1] = 20;
    }
    else if (location == 6)
    { 
      c.drawLine(550,325,650,400);
      c.drawLine(550,400,650,325);
      chart [1][2] = 20;
    }
    else if (location == 7)
    {  
      c.drawLine(350,400,450,475);
      c.drawLine(350,475,450,400);   
      chart [2][0] = 20;
    }
    else if (location == 8)
    {  
      c.drawLine(450,400,550,475);
      c.drawLine(450,475,550,400); 
      chart [2][1] = 20;
    }
    else if (location == 9)
    { 
      c.drawLine(550,400,650,475);
      c.drawLine(550,475,650,400); 
      chart [2][2] = 20;
    }  
  }
  //Checks the winner by all 8 possibilities
  if (chart[0][0]+chart[0][1]+chart[0][2] == 60||
      chart[1][0]+chart[1][1]+chart[0][2] == 60||
      chart[2][0]+chart[2][1]+chart[2][2] == 60||
      chart[0][0]+chart[1][1]+chart[2][2] == 60||
      chart[2][0]+chart[1][1]+chart[0][2] == 60||
      chart[0][0]+chart[1][0]+chart[2][0] == 60||
      chart[0][1]+chart[1][1]+chart[2][1] == 60||
      chart[0][2]+chart[1][2]+chart[2][2] == 60)
  {break;}
  c.setCursor(9,0);
  c.println("Where would you like to place 'O' " + name2);
  //Making sure the number entered is valid
  while(true)
  {
    try 
    { String locationStr = c.readLine();
      location = Integer.parseInt(locationStr);
      break;
    }
    catch(NumberFormatException e)
    { c.println("Bad number, please try again.");
    }
  }
  {
    //Drawing the 'O' and adjusting the 2d array's correspomding to the number entered
    if (location == 1)
    {
      c.drawOval(350,250,100,75); 
      chart [0][0] = 30;
    }
    else if (location == 2)
    {
      c.drawOval(450,250,100,75);  
      chart [0][1] = 30;
    }
    else if(location == 3)
    { 
      c.drawOval(550,250,100,75);  
      chart [0][2] = 30;
    }
    else if (location == 4)
    { 
      c.drawOval(350,325,100,75); 
      chart [1][0] = 30;
    }
    else if (location == 5)
    { 
      c.drawOval(450,325,100,75);  
      chart [1][1] = 30;
    }
    else if (location == 6)
    { 
      c.drawOval(550,325,100,75);
      chart [1][2] = 30;
    }
    else if (location == 7)
    {  
      c.drawOval(350,400,100,75); 
      chart [2][0] = 30;
    }
    else if (location == 8)
    {  
      c.drawOval(450,400,100,75);  
      chart [2][1] = 30;
    }
    else if (location == 9)
    { 
      c.drawOval(550,400,100,75); 
      chart [2][2] = 30;
    }

  //Checks the winner by all 8 possibilities
    if (chart[0][0]+chart[0][1]+chart[0][2] == 90||
        chart[1][0]+chart[1][1]+chart[0][2] == 90||
        chart[2][0]+chart[2][1]+chart[2][2] == 90||
        chart[0][0]+chart[1][1]+chart[2][2] == 90||
        chart[2][0]+chart[1][1]+chart[0][2] == 90||
        chart[0][0]+chart[1][0]+chart[2][0] == 90||
        chart[0][1]+chart[1][1]+chart[2][1] == 90||
        chart[0][2]+chart[1][2]+chart[2][2] == 90)
    {break;}
  }  
}
//Ends the game by saying who teh winner is
if (chart[0][0]+chart[0][1]+chart[0][2] == 60||
    chart[1][0]+chart[1][1]+chart[0][2] == 60||
    chart[2][0]+chart[2][1]+chart[2][2] == 60||
    chart[0][0]+chart[1][1]+chart[2][2] == 60||
    chart[2][0]+chart[1][1]+chart[0][2] == 60||
    chart[0][0]+chart[1][0]+chart[2][0] == 60||
    chart[0][1]+chart[1][1]+chart[2][1] == 60||
    chart[0][2]+chart[1][2]+chart[2][2] == 60)
{c.println("Congratulations "+name1+" you are the winner!");}

else if (chart[0][0]+chart[0][1]+chart[0][2] == 90||
         chart[1][0]+chart[1][1]+chart[0][2] == 90||
         chart[2][0]+chart[2][1]+chart[2][2] == 90||
         chart[0][0]+chart[1][1]+chart[2][2] == 90||
         chart[2][0]+chart[1][1]+chart[0][2] == 90||
         chart[0][0]+chart[1][0]+chart[2][0] == 90||
         chart[0][1]+chart[1][1]+chart[2][1] == 90||
         chart[0][2]+chart[1][2]+chart[2][2] == 90)
{c.println("Congratulations "+name2+" you are the winner!");}

This is what I have so far

c.println("Would you like to play again? Enter 'y' if you want to play 
again, Enter 'n' if you do not wish to play again");
endGame = c.readLine ();
if (endGame == "y")
{

}
else if (endGame == "n")
{
 System.exit(0);
}
}
}
Devg
  • 9
  • 1
  • Possible duplicate [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MadProgrammer Apr 10 '18 at 23:13
  • Hello and welcome to StackOverflow, a website where you can get help on specific problems with code. Start with the [tour](https://stackoverflow.com/tour) and what questions are [suitable for asking](https://stackoverflow.com/help/on-topic) and which ones are [not](https://stackoverflow.com/help/dont-ask). You might also want to check out [Minimal, Complete and Verifiable Examples](https://stackoverflow.com/help/mcve). – Raymo111 Apr 10 '18 at 23:52
  • Check out [this tic tac toe game](https://github.com/Raymo111/Tic-Tac-Toe-GUI-Java). You may find it helpful with what you are looking for. – Raymo111 Apr 10 '18 at 23:53

2 Answers2

2

Based on Vince's recommendation where you use a loop to loop back to the start of the game.

// Your setup logic

// Start of main loop
boolean stillPlaying = true;
while(stillPlaying)
{
  // Your game logic

  c.println("Would you like to play again? Enter 'y' if you want to play again, Enter 'n' if you do not wish to play again");
  endGame = c.readLine().trim();
  if (endGame.equalsIgnoreCase("y") || endGame.equalsIgnoreCase("yes"))
  {
    // Reset your board
    c.clear();
    c.setColor(Color.black);
    c.drawLine(450,250,450,475);
    c.drawLine(550,250,550,475);
    c.drawLine(350,325,650,325);
    c.drawLine(350,400,650,400);

    // Reinitialize chart[][] to 1,2,3,4,5,6,7,8,9 your starting state
    resetChart(chart);
  }
  else if (endGame.equalsIgnoreCase("n") || endGame.equalsIgnoreCase("no"))
  {
    stillPlaying = false;
  }
  else 
  {
    // Should probably re prompt for y or n but I'm just gonna exit
    stillPlaying = false
  }
}
Richard Tran
  • 438
  • 3
  • 10
0

Put the code inside your main function into a method like "draw()", then call it in place of the System.exit(0);

Matheus Mohr
  • 128
  • 6
  • 2
    This would eventually lead to a `StackOverflowException`. The proper way to handle this type of behavior would be to use a loop. – Vince Apr 10 '18 at 23:42
  • 1
    Makes sense, didn't pay attention to that (would require a good amount of tic-tac-toe, but still, could happen). Mind adding that as an answer? – Matheus Mohr Apr 10 '18 at 23:45
  • 1
    I try to avoid answering questions that don't add much value to the site. This has been answered many times ([1](https://stackoverflow.com/questions/20228918/how-to-add-the-play-again-feature-for-java), [2](https://stackoverflow.com/questions/39627777/java-ask-user-if-they-want-to-play-again), [3](https://stackoverflow.com/questions/39822820/how-do-i-implement-a-play-again-feature?rq=1), tons more on just this site alone). Rather, in situations like this, I'd rather help those who are trying to help. Feel free to include it in your answer! Although you should flag this as duplicate. – Vince Apr 10 '18 at 23:52