Devg
Devg

Reputation: 9

Tic Tac Toe - How to Make Program Restart itself if user wants to play again

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

Upvotes: 0

Views: 977

Answers (2)

Richard Tran
Richard Tran

Reputation: 418

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

Upvotes: 2

Matheus Mohr
Matheus Mohr

Reputation: 128

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

Upvotes: 0

Related Questions