CISC181 S2015 Lab4
From class_wiki
Contents
Preliminaries
- Make a new project with n = 4 (following these instructions)
- Name your main class "Lab4" (when creating a new module in the instructions above, in the Java class name field)
Instructions
Modify Lab4.java by adding your name and section number in a comment before the Lab4 class body, and inside the Lab4 class add the following:
Prime number stats
- Create a method static boolean[] allThePrimes(int N). This will return an array of N booleans such that the value at array index i is true if the number i + 1 is prime, and false if it is not. This method should call isItPrime() from Lab 2 (or copy its functionality)
- Create a method static void printAndCountTrues(boolean[] boolArray, int lower, int upper) which will print '0' for false and '1' for true for all values in the range of index lower to index upper (inclusive) in the array passed to it. The 0's and 1's should all be on one line with no spaces in between them, followed by a single space, followed by how many true's were found, followed by a newline. Print an error message if lower or upper is somehow illegal (i.e., out of the array bounds)
- In main(), call allThePrimes(1000) and store the result in an array primeArray. Then call printAndCountTrues(primeArray, 0, 49), printAndCountTrues(primeArray, 50, 99) and so on up to 999 as the upper index. Put these method calls in a loop
Random tic-tac-toe
- Create a method static int randomTicTacToeGame(boolean doPrint) as described below. In main() (after the prime number stuff above), call it 1000 times (with doPrint true for the first 10 and false for the rest) and report the tally for each possible outcome. That is, print how many times X won, how many times O won, and how many times there was a tie
- Required features of randomTicTacToeGame():
- Use a multi-dimensional array to create a 3 x 3 Tic-Tac-Toe "board". Use an enumerated type to represent empty, X, or O in a particular square. So the first thing your function should do is declare a 3 x 3 array of the enumerated type and set them all to the empty value. Note: use an array, not an ArrayList
- Alternate between a computer "X player" (who always goes first) and a computer "O player" who put their moves into randomly chosen empty spaces until the game is over
- Just to review the rules: a player wins if they get 3 in a row either vertically, horizontally, or diagonally. It is a tie if there are no empty spaces left and no one has won (you do not have to try to detect a tie any earlier)
- If doPrint is true, print the board after each move (3 x 3 grid of 'X', 'O', or '-' if empty), with an empty line after
- When the game is over, if doPrint is true print the result ("X wins!", "O wins!", or "Cat's game"). No matter what, return +1 for an X win, -1 for an O, and 0 for a tie
- Make sure to create "helper" methods for randomTicTacToeGame() to call -- don't try to put all your code into it. Some suggested helper methods:
- static void printBoard(...) ("..." indicates the board array as an argument)
- static boolean isXWin(...), static boolean isOWin(...), static boolean isTie(...)
- static void randomXMove(...), static void randomOMove(...)
Advice: isXWin() and isOWin() are the hardest functions to write. Get EVERYTHING else working before you tackle them
Finishing up
Remember to use proper naming and formatting style throughout your code, and submit your Lab4.java on Sakai by Sunday, March 8 (2 day extension due to University pre-empting March 4 lecture)