The goal of today’s lab is to practice using Java’s Scanner class and its
commonly used methods. We will also practice using printf, opening a text
file, reading items from a file with Scanner, being careful about == versus
.equals, such as with String, and other topics from the first few lectures.
Please submit Lab work using the CS5005 Canvas. Any extra time should
be spent working on CS5004 homework PS2.
0. Create a Java class, Tic.java, to represent a Tic-Tac-Toe (a.k.a.,
“Naughts and Crosses”) game as a 2-dimensional array of strings. Use
the single-character Strings “X”, “O”, and “_“ (underscore) to represent
the state of the game, where “_” means an empty cell. Usually the game
starts with all cells empty. “X” normally goes first and turns alternate until
one player achieves 3-in-a-row up, down, or diagonally.
a. Create a printBoard method that uses printf to print the state of
the game on the console. Don’t worry about “outlining” the game
board, just use white space to line things up reasonably. Here is a
sample board position:
X _ O
_ X O
_ _ X
b. Create a method, readBoardKbd, that uses Scanner to read in a
board position from the keyboard and then print it, as in part a.
c. Create another method, readBoardFile, that uses Scanner to
read in a board position from a text file, board1.txt. Create this file
in the same folder as your program. Remember to close the file
when done reading.
d. Create another method, readBoardsFile. Use your
readBoardFile method above to read multiple boards and store
Page 2 of 2
them in an Array of Boards. The first line of the file should contain
an integer saying how many Boards there are in the file. This will
enable creating an appropriately sized array. You can use a for
loop to then read the remaining boards. (For now, don’t worry
about the case where the rest of the file is flawed in some way.)
e. Use a for-each loop to then display all of the Boards from the file
on the screen.
1. Complete homework PS2. Submit that assignment to Canvas for 5004.