Hello,

I was just wondering how i could write a TicTacToe program in java without arrays.
I am new to programming and i don't know lot of stuffs, so any help would be appreciated

Thanks

Recommended Answers

All 6 Replies

Hello,

I was just wondering how i could write a TicTacToe program in java without arrays.
I am new to programming and i don't know lot of stuffs, so any help would be appreciated

Thanks

Without arrays? That would to me be harder then doing it using arrays, if i was you i'd learn arrays and pick up from there:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html and here: http://www.javatutorialhub.com/java-arrays.html

well, my teacher told me not to use any arrays...he will deduct point if i do. So is there any ways i can do this program without arrays?

Without an array, you'd just have to make 9 separate variables, each corresponding with a row and column on the board. For example

int space11 = 0; //Row 1 Col 1, 0 is an O and 1 is an X

space12 would be row 1 col 2 and so forth. Then take those as your inputs.

For others out there, I'm new here, so let me know if there is something I SHOULD or SHOULD NOT be doing. Hopefully this is ok

This is what i have so far. I think i am using redundant code, is there anyway i can reduce it, if so how?

import java.util.Scanner;
public class TicTac {

    static int row, col;

    static char topLeft = ' ' ;
    static char topCenter = ' ';
    static char topRight = ' ';
    static char centerLeft = ' ';
    static char centerCenter = ' ';
    static char centerRight= ' ';
    static char bottomLeft = ' ';
    static char bottomCenter = ' ';
    static char bottomRight = ' ';


    public static void main(String[] args) { 

        System.out.println("Welcome to Tic-Tac-Toe!");
        System.out.println("Rows and columns are numbered 0-2");
        System.out.println();  



         playerTurn();



         computerTurn();

    }   

    static void playerTurn() {



           char player = 'x';


           printGameBoard(player);


           Scanner input = new Scanner(System.in);



           System.out.print("Row:"+ "" );
           row = input.nextInt();

           System.out.print("Column:"+ "" );
           col = input.nextInt();



          if(row==0 && col==0){

             topLeft = player;

            printGameBoard(player);

            }
          else if (row==0 && col==1){

              topCenter = player;

                printGameBoard(player);

                }

          else if (row==0 && col==2){

              topRight = player;

                printGameBoard(player);

                }
          else if (row==1 && col ==0){

              centerLeft = player;

                printGameBoard(player);

                }

          else if (row==1 && col ==1){

              centerCenter = player;

                printGameBoard(player);

                }
          else if (row==1 && col ==2){

              centerRight = player;

                printGameBoard(player);

                }
          else if (row==2 && col ==0){

              bottomLeft = player;

                printGameBoard(player);

                }
          else if (row==2 && col ==1){

              bottomCenter = player;

                printGameBoard(player);

                }
          else if (row==2 && col ==2){

              bottomRight = player;

                printGameBoard(player);

                }
          else {
              System.out.println( "Invalid location, try again");

          }   


    }


    static void computerTurn() {



    }

    static boolean gameWon(char player) {
        return false;
    }

    static boolean isValidMove(int row, int col){
    return false;
    }
    static void printGameBoard(char player) {





        System.out.println( topLeft + "|" +  topCenter +  "|" +    topRight  );
        System.out.println( "-+" +    "-"     + "+-"  );
        System.out.println( centerLeft + "|" +  centerCenter +  "|" +    centerRight  );
        System.out.println( "-+" +    "-"     + "+-"  );
        System.out.println( bottomLeft + "|" +  bottomCenter +  "|" +    bottomRight  );




    }
}

I think i am using redundant code, is there anyway i can reduce it, if so how?

That's the use of arrays, to make it less redundant and less complicated :)
But since your not allowed to and if you have no more problems with the code then it should all right the way it is now

pitamber

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.