Hi! Im a beginner in programming, and I have been given an assignment to program a TicTacToe game.

I could not find any programs as such for reference over the Interwebs thus would want the expertise of daniweb users to help.

The problem im having:

1. How do i transfer the input from the main method to the getBoard constructor method.
Just how do i transfer the input from the main method to userInput in the if statements in the constructor class.

2. when the user enters an input it can replace an input already on the board(That is ofcourse when i put everything in everything in the main method)

thx for helping

import java.util.Scanner;
public class TicTacToe
{
    public static void main(String[] args)
    {
        String userInput = "";
        System.out.println("Looking at the Key above enter X or O in the correct quadrant\nseperated with a hyphen; ie. X-NW" +"\n");
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Your first input ie. X and its Quadrant:"+"\n");
        System.out.println("Command Key:"+"\n"+"\t[R] = Play Again" +"\n"+ "\t[Q] = Quit");
        userInput = in.next();
        TicTacToe tac = new TicTacToe();
        String game = "";
        game = tac.getBoard;
    }
    public void getBoard(String[] args)
    {
        /*Tic-Tac-Toe Board (KEY)*/
        System.out.println("Tic Tac Toe Key:" +"\n");
        String topRow = "NW | N | NE";
        System.out.println(" "+topRow);
        System.out.println("----|---|----");
        String middleRow = " W | C | E";
        System.out.println(" "+middleRow);
        System.out.println("----|---|---- ");
        String bottomRow = "SW | S | SE";
        System.out.println(" "+bottomRow +"\n");
        
        
        
        /*Initialize the values of the Quadrants*/
        String NW = "   ";
        String N= "   ";
        String NE = "   ";
        String E= "   ";
        String C= "   ";
        String W= "   ";
        String SW= "   ";
        String S= "   ";
        String SE = "   ";
   
        /*Possible Inputs*/
        boolean checkWin = false;
        boolean checkPlay = false; 

     while(!checkPlay)
     {
      while(!checkWin)
      {
        /*Quit Command*/
        if(userInput.equalsIgnoreCase("Q")){
            System.out.println("Thanks you for playing Tic-Tac-Toe by Ritesh Pakala.");
            System.exit(0);
        }
        
        /*Input Locations*/
        if(userInput.equalsIgnoreCase("X-NW"))
        {
            NW = "[X]"; 
        }else if(userInput.equalsIgnoreCase("O-NW")){
            NW = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-N"))
        {
            N = "[X]";
        }else if(userInput.equalsIgnoreCase("O-N")){
            N = "[O]";
        }  
        if(userInput.equalsIgnoreCase("X-NE"))
        {
            NE = "[X]";
        }else if(userInput.equalsIgnoreCase("O-NE")){
            NE = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-W"))
        {
            W = "[X]";
        }else if(userInput.equalsIgnoreCase("O-W")){
            W = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-C"))
        {
            C = "[X]";
        }else if(userInput.equalsIgnoreCase("O-C")){
            C = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-E"))
        {
            E = "[X]";
        }else if(userInput.equalsIgnoreCase("O-E")){
            E = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-SW"))
        {
            SW = "[X]";
        }else if(userInput.equalsIgnoreCase("O-SW")){
            SW = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-S"))
        {
            S = "[X]";
        }else if(userInput.equalsIgnoreCase("O-S")){
            S = "[O]";
        }
        if(userInput.equalsIgnoreCase("X-SE"))
        {
            SE = "[X]";
        }else if(userInput.equalsIgnoreCase("O-SE")){
            SE = "[O]";
        }

        /*Win Scenarios*/
        if (NW.equalsIgnoreCase("[X]") && N.equalsIgnoreCase("[X]") && NE.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (NW.equalsIgnoreCase("[O]") && N.equalsIgnoreCase("[O]") && NE.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
        else if (W.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && E.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (W.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && E.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
        else if (SW.equalsIgnoreCase("[X]") && S.equalsIgnoreCase("[X]") && SE.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (SW.equalsIgnoreCase("[O]") && S.equalsIgnoreCase("[O]") && SE.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
        else if (NW.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && SE.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (NW.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && SE.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
        else if (NE.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && SW.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (NE.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && SW.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
         else if (NW.equalsIgnoreCase("[X]") && W.equalsIgnoreCase("[X]") && SW.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (NW.equalsIgnoreCase("[O]") && W.equalsIgnoreCase("[O]") && SW.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
         else if (N.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && S.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (N.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && S.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player O Wins!\n"+thumb);
            checkWin = true; 
        }
         else if (NE.equalsIgnoreCase("[X]") && E.equalsIgnoreCase("[X]") && SE.equalsIgnoreCase("[X]"))
        {
            System.out.println("Congrats! Player X Wins!\n"+thumb);
            checkWin = true; 
        }else if (NE.equalsIgnoreCase("[O]") && E.equalsIgnoreCase("[O]") && SE.equalsIgnoreCase("[O]")){
            System.out.println("Congrats! Player 0 Wins!\n"+thumb);
            checkWin = true; 
            System.exit(0);
        }else
            System.out.println("You tied the game.");
            

        
        /*Board*/
         String newGame = "     |     |      " +"\n"+ 
                        " " +NW+ " "+"|"+" " +N+" "+"|"+" " +NE +"\n"+
                        "-----|-----|-----" +"\n"+
                        " " +W+ " "+"|"+" " +C+" "+ "|"+" " +E +"\n"+
                        "-----|-----|-----" +"\n"+ 
                         " " +SW+ " "+"|"+" " +S+" "+ "|"+" " +SE +"\n"+
                          "     |     |     " +"\n";
                         
        System.out.print(newGame); 
      }
     }
    } 
}

How do i transfer the input from the main method to the getBoard constructor method.

Confusing terminology. Is getBoard a class with a constructor or a method in a class?
Either way, you pass data from one method to another as arguments to that method.
A very standard thing. Can you explain what your problem is?

when the user enters an input it can replace an input already on the board

Is that the "new" rules you are giving your game? Then the first to play will always win if he can play over the top of the other player.
Can you explain what you mean by this?


If you are getting compiler errors, please copy and paste here the full text of the message.

Confusing terminology. Is getBoard a class with a constructor or a method in a class?
Either way, you pass data from one method to another as arguments to that method.
A very standard thing. Can you explain what your problem is?

Is that the "new" rules you are giving your game? Then the first to play will always win if he can play over the top of the other player.
Can you explain what you mean by this?


If you are getting compiler errors, please copy and paste here the full text of the message.

import java.util.Scanner;
public class TicTacToeMain
{
    public static void main(String args[])
    {
        String userInput = "";
        System.out.println("Looking at the Key above enter X or O in the correct quadrant\nseperated with a hyphen; ie. X-NW" +"\n");
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Your first input ie. X and its Quadrant:"+"\n");
        System.out.println("Command Key:"+"\n"+"\t[R] = Play Again" +"\n"+ "\t[Q] = Quit");
        for(int i=0; i < 9; i++)
        {
             userInput = in.next();
             TicTacToeBoard tac = new TicTacToeBoard(userInput);
        }
    }
}

This is my tester class now while the class above i added this
after that public class line

public TicTacToeBoard(String input)
    {
        getBoard(input);
    }
    public void getBoard(String userInput)
    {

now the program is stuck in a wiered repetitive loop.

And again sorry for bad terminology

I Want to transfer the input from the main method to the if statements in the get board.

Where is the data? What variable is it in?
Can you pass that variable to the getBoard() method as an argument?

player 2 inputs ontop of player 1's input it replaces the piece

The code should test if the place is occupied and not allow a move if it is.

Where is the data? What variable is it in?
Can you pass that variable to the getBoard() method as an argument?


The code should test if the place is occupied and not allow a move if it is.

I fixed the first problem but as i said an infinite loop has started to occur, and how do i fix so.

And thank you for the replies, and the idea on replacing pice problem.

problem but as i said an infinite loop has started to occur, and how do i fix so.

Do you know where the loop is occurring? If not, you need to add printlns to the code to show where it is executing.

If you know where the loop is, what variable(s) control the looping? Why aren't those variables being changed so the code can exit the loop?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.