Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Tic-Tac-Toe Help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Tic-Tac-Toe Help

    This is for a college class so I already understand if you can't straight up tell me the answer. That being said I still am in need of help...

    HTML Code:
    CompSci 251: Spring 2012
    Programming Assignment #6
    Due Tuesday, March 6, 11:59pm
    Topics Covered: Arrays of classes and classes that contain arrays
    Introduction: TTTBoard class
    You will write a program that generates a number of boards for a TicTacToe game and fills each 
    one with equal numbers of X and O characters in random positions.  For each board, it will check 
    whether that board’s pattern of X and O characters has been seen before and if not, it will save it 
    in an array.  Once all the boards requested by the user have been generated, the program will print 
    out the number of unique board configurations that it saw.  Then it will ask the user if it should 
    print all of the unique board configurations and does so if requested.
    What you will do
    You will implement the TTTBoard class and the TTTBoardDriver class.  
    TTTBoard
    – board: char[][]  // each contains either -, X, or O
    – size: integer  // number of rows and columns of board, at least 2
    + TTTBoard(boardSize : integer) 
    + fillWithRandomXAndO() : void
    + getCellChar(rowNum : integer, columnNum : integer) : char
    + setCellChar(rowNum : integer, columnNum : integer, xo: char) : void
    + equals(TTTBoard) : boolean 
    + toString() : String 
    The TTTBoard class’s methods do the following:
    • The constructor instantiates the two-dimensional array of characters at the size 
    specified by the user and puts a hyphen character (‘-‘) in every space of the board.
    • The fillWithRandomXAndO() method puts either an X or O in every cell of the 
    array.  In general, it chooses the letter randomly.  However, it also ensures that 
    each letter is in half of the board spaces when it ends.  So, for 4-by-4 boards, there 
    will be 8 X characters and 8 O characters.  For a 5-by-5 board, there will be 13 of 
    one character and 12 of the other.
    • The setCellChar method is a mutator for individual board spaces.  It only lets the 
    three legal characters be put on the board.
    • The equals method returns true if the two boards are the same size and they have 
    the same characters in each board position.• The toString method returns a String object that contains the characters of the 
    board, with one line per row of the board.  It does not call System.out.println() 
    itself.  It just returns a String that main() can use in printing.
    •
    Sample session:
    Note: Since you are generating the boards randomly, you need to remember that:
    1. For the 2-by-2 example below, you should get at most 6 unique boards, but you can’t 
    predict the order in which they will appear.
    2. For larger board sizes (4 and up), you are likely to get different numbers of unique boards 
    on different runs.  
    Finally, the number of possible board configurations goes up really fast with increasing board 
    size.  For 2-by-2 boards, there are 6 possible configurations.  For 3-by-3, there are 252.  For 
    boards of size 4 and 5, the totals are 12,870 and 10,400,600, respectively.  Once we get to size 6, 
    my calculator starts using scientific notation.  Anyway, if you try to make too many boards of 
    larger sizes, the program will get quite slow.  
    Welcome to the Tic-Tac-Toe Board Generator
    Enter board size (at least 2): 2
    Enter number of boards to generate: 100
    Number of unique boards generated = 6
    Would you like to see all of the unique boards (y/n): y
    Unique Board #0
    OX
    OX
    Unique Board #1
    XO
    OX
    Unique Board #2
    OO
    XX
    Unique Board #3
    XX
    OO
    Unique Board #4
    OX
    XOUnique Board #5
    XO
    XO
    Goodbye!
    Welcome to the Tic-Tac-Toe Board Generator
    Enter board size (at least 2): 1
    ERROR: Board size must be at least 2
    Enter board size (at least 2): 3
    Enter number of boards to generate: 0
    ERROR: Must generate at least one board
    Enter number of boards to generate: 2
    Number of unique boards generated = 2
    Would you like to see all of the unique boards (y/n): y
    Unique Board #0
    OOX
    XOX
    XXO
    Unique Board #1
    XXX
    XXO
    OOO
    Goodbye!
    Welcome to the Tic-Tac-Toe Board Generator
    Enter board size (at least 2): 4
    Enter number of boards to generate: 4
    Number of unique boards generated = 4
    Would you like to see all of the unique boards (y/n): y
    Unique Board #0
    OXXX
    OOOX
    XXOO
    XXOO
    Unique Board #1
    OXXX
    OXXO
    XXXO
    OOOOUnique Board #2
    OOXX
    OOXO
    XXXX
    OXOO
    Unique Board #3
    XOOO
    OOOO
    XXXX
    OXXX
    Goodbye!
    Welcome to the Tic-Tac-Toe Board Generator
    Enter board size (at least 2): 4
    Enter number of boards to generate: 10000
    Number of unique boards generated = 5435
    Would you like to see all of the unique boards (y/n): n
    Goodbye!
    Welcome to the Tic-Tac-Toe Board Generator
    Enter board size (at least 2): 4
    Enter number of boards to generate: 10000
    Number of unique boards generated = 5387
    Would you like to see all of the unique boards (y/n): n
    Goodbye!
    Submission
    Submit both files of your program as a single .zip archive file to the D2L dropbox.  You 
    can create a .zip archive using the Export command in Eclipse.
    My TTTBoard class:
    import java.util.Arrays;
    import java.util.Random;
    public class TTTBoard {
    	private char[][] board;
    	private int size;
     
    	public TTTBoard(int boardSize)
    	{
    		board = new char[boardSize][boardSize];
    		for (int i=0;i < board.length;++i)
    		{
    			for(int j=0;j<i;++j)
    			{
    				board[i][j] = '-';
    			}
    		}
    	}
    	public void fillWithRandomXandO()
    	{
    		int xCounter=0;
    		int oCounter=0;
    		for(int i = 0; i < this.size; ++i)
    		{
    			for(int j = 0; j< this.size; ++j)
    			{
    				int XorO = (int)Math.random()*2;
    				if(XorO == 1 && xCounter != ((this.size*this.size)/2))
    				{
    					board[i][j] = 'X';
    					xCounter++;
     
    				}
    				else if (XorO == 2 && oCounter != ((this.size*this.size)/2))
    				{
    					board[i][j] = 'O';
    					oCounter++;
    				}
    			}
    		}
    	}
    	public char getCellChar(int row, int column)
    	{
    		return this.board[row][column];
    	}
    	public void setCellChar(int row, int column, char xo)
    	{
    		this.board[row][column] = xo;
    	}
    	public boolean equals(TTTBoard b)
    	{
    		for (int i = 0; i < this.size;++i)
    		{
    			for(int j = 0; j < this.size; ++i)
    			{
    				if (this.getCellChar(i, j) == b.getCellChar(i, j))
     
    				{
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    	public String toString()
    	{
    		/*String s = "";
    		for (int i = 0; i < this.size; ++i)
    		{
     
    			for (int j = 0; j<this.size;++j)
    			{
    				 s += ""+getCellChar(i,j);
    			}
    		}*/
    		String s = Arrays.toString(this.board);
    		return s;
    	}
    }

    My TTTBoardDriver Class:
    import java.util.Scanner;
    public class TTTDriver {
     
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
    		int size;
    		int numberOfBoards;
    		int actualNumberOfBoards = 0;
    		System.out.println("Welcome to Tic-Tac-Toe board game generator!");
    		System.out.println("");
    		System.out.print("Enter board size (at least 2): ");
    		size = stdIn.nextInt();
    		System.out.print("Enter the number of boards you would like to generate: ");
    		numberOfBoards = stdIn.nextInt();
     
    		while (actualNumberOfBoards != numberOfBoards)
    		{
    			TTTBoard board = new TTTBoard(size);
    		}
     
     
     
    	}
     
    }

    Right now I'm wondering how do i go about generating the proper number of "random" boards and also I'm not sure if i have my other methods functioning correcting. In particular the fillWithRandomXorO's method I'm wondering if how i have that set up will truly give me random x/o placements in the array and still keep them with the half/half bounds. this is due tonight at midnight so fast help is appreciated! thanks in advance and if I'm not making any sense point out where you need clarification please!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    how do i go about generating the proper number of "random" boards
    What determines what the "proper number" is?
    Does the code now generate a "random" board?

    I'm wondering if how i have that set up will truly give me random x/o placements in the array
    What output do you get now?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Help

    Quote Originally Posted by Norm View Post
    What determines what the "proper number" is?
    Does the code now generate a "random" board?

    the proper number would be, i guess being given the size of the boards from the user, the total number of boards possible without two boards being the same. (no duplicate boards)

    What output do you get now?
    I haven't even gotten to a point for board outputs atm, been trying to think of a way to generate the most possible (non duplicate boards) given the user's input of size.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    What is your problem now? Can you generate a board?

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Help

    I just tried to generate a board and my toString() method for my array doesn't work. we can't print from the method as printing the array seems simple to me with nested for loops. but generating a string of my array in order to call print from main is more complex. So... in short, My current problem is that the Arrays.toString(obj) method was returning addresses and my self made toString() (its currently commented out in code example) doesn't work at all.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    Can you post the code and show its output or the errors?
    If you want to print a 2D array with the Arrays class, it has a method to do that. Its name begins with deep

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Help

    Quote Originally Posted by Norm View Post
    Can you post the code and show its output or the errors?
    If you want to print a 2D array with the Arrays class, it has a method to do that. Its name begins with deep
    import java.util.Scanner;
    public class TTTDriver {
     
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
    		int size;
    		int numberOfBoards;
    		int actualNumberOfBoards = 0;
    		System.out.println("Welcome to Tic-Tac-Toe board game generator!");
    		System.out.println("");
    		System.out.print("Enter board size (at least 2): ");
    		size = stdIn.nextInt();
    		System.out.print("Enter the number of boards you would like to generate: ");
    		numberOfBoards = stdIn.nextInt();
     
    		TTTBoard board = new TTTBoard(size);
    		System.out.println(board.toString());
    		/*while (actualNumberOfBoards != numberOfBoards)
    		{
     
    			TTTBoard board = new TTTBoard(size);
    		}*/
     
     
     
    	}

    OUTPUT:
    Welcome to Tic-Tac-Toe board game generator!

    Enter board size (at least 2): 3
    Enter the number of boards you would like to generate: 1
    [[ , , ], [-, , ], [-,-, ]]

    Now with the deepToString its not showing x's or o's but there being hyphens still shows me that my fill with random x and o's method is failing... shittt...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    Why are there spaces in the print out?

    Print out the board before you change it
    Last edited by Norm; March 11th, 2012 at 07:33 PM.

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Help

    I'm not sure.. it should be either an 'x' or and 'o' in the output and they should be stacked to look like a tic-tac-toe board. heres my fill method code

    public void fillWithRandomXandO()
    	{
    		int xCounter=0;
    		int oCounter=0;
    		for(int i = 0; i < this.size; ++i)
    		{
    			for(int j = 0; j< this.size; ++j)
    			{
    				int XorO = (int)Math.random()*2;
    				if(XorO == 1 && xCounter < ((this.size*this.size)/2) || oCounter >= ((this.size*this.size)/2))
    				{
    					this.board[i][j] = 'X';
    					xCounter++;
     
    				}
    				else if (XorO == 0 && oCounter < ((this.size*this.size)/2) || xCounter >= ((this.size*this.size)/2))
    				{
    					this.board[i][j] = 'O';
    					oCounter++;
    				}
    			}
    		}
    	}

    I figured out where the spaces are, it was due to my constructor's second nested for-loop. now the output gives me all indecies of the array filled with '-'. so my fillWithRandomXorO method isn't doing anything... How would you suggest going about generating a random amount of x's and o's to fill the board?
    Last edited by coke32; March 11th, 2012 at 07:42 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    Print out the board after filling it with -

    Try debugging the fillWithRandomXandO method by adding some println statements to print the values of the variables it uses so you can see what the code is doing. The more you print out the more you'll know.

  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Help

    unfortunately I don't know how to debug my programs all that well. We haven't gone over that too much yet. but I have figured out why the x's or o's weren't generating! I never declare what this.size is in my constructor so when I was running the loops in my fill method the if statements were never true because of the (this.size*this.size/2) was = 0; as far as my toString() method now, I'd like for my output to be better formatted rather then displaying each row next to each other.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    Printing the endline char: "\n" will move the output to the next line.

  13. #13
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Tic-Tac-Toe Help

    NEW PROBLEM!!!!
    So I've been at this and have been making progress but at the moment I'm running into a problem of generating "unique non duplicate boards". I'll show you my current driver and my errors.
    import java.util.Scanner;
    import java.util.ArrayList;
    public class TTTDriver {
     
    	public static void main(String[] args) {
    		Scanner stdIn = new Scanner(System.in);
    		int size;
    		int numberOfBoards;
    		int actualNumberOfBoards = 1;
    		String displayBoards ="";
    		ArrayList<TTTBoard> boards = new ArrayList<TTTBoard>();
    		System.out.println("Welcome to Tic-Tac-Toe board game generator!");
    		System.out.println("");
    		System.out.print("Enter board size (at least 2): ");
    		size = stdIn.nextInt();
    		System.out.print("Enter the number of boards you would like to generate: ");
    		numberOfBoards = stdIn.nextInt();
    		TTTBoard firstBoard = new TTTBoard(size);
    		firstBoard.fillWithRandomXandO();
    		boards.add(firstBoard);
    		for (int i = 0; i < numberOfBoards;++i)
    		{
    			TTTBoard newboard = new TTTBoard(size);
    			newboard.fillWithRandomXandO();
    			for(int j=0; j<i;++j)
    			{
    				if (boards.get(i).equals(newboard))
    				{
    					break; // want this to basically stop the loops if the board equals every other board
    				}
    				else
    					boards.add(newboard);
    				++actualNumberOfBoards;
    			}
    		}
    		System.out.print("You generated "+actualNumberOfBoards+" boards would you like to display them? (y/n): ");
    		displayBoards = stdIn.next();
    		if (displayBoards.equalsIgnoreCase("y"))
    		{
    			for (int i=0;i<boards.size();++i)
    			{
    				System.out.println((boards.get(i).toString()));
    			}
    		}
     
     
     
    	}
     
    }

    OUTPUT:
    Welcome to Tic-Tac-Toe board game generator!

    Enter board size (at least 2): 3
    Enter the number of boards you would like to generate: 2
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.RangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at TTTDriver.main(TTTDriver.java:27)


    I can see that the output's error is related to my arraylist but i'm not sure why. All i want to happen in that double loop is if the board doesn't equal all the other boards *then create a new one* but i'm getting this error and don't understand why



    EDIT:

    when i comment out the if statement in the nested for loop I get this output:

    Welcome to Tic-Tac-Toe board game generator!

    Enter board size (at least 2): 4
    Enter the number of boards you would like to generate: 2
    You generated 2 boards would you like to display them? (y/n): y
    [[O, O, X, X], [X, O, X, X], [X, X, O, O], [O, X, O, O]]

    [[O, X, O, X], [X, X, O, X], [X, O, O, O], [X, O, X, O]]


    still having trouble making the output make a nice tic-tac-toe look.
    Last edited by coke32; March 11th, 2012 at 08:52 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tic-Tac-Toe Help

    The Arrays toString method is mostly for debugging.
    You'll have to write your own code using some loops to have multiple rows of output