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...
My TTTBoard class: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.
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!


LinkBack URL
About LinkBacks
Reply With Quote