TicTacToe
¶
TicTacToe (No Packages)¶
TicTacToe (No Packages)
Prerequisite Information
This practice activity should be done on Odin, so make to complete all of the tutorials in the Unix chapter before attempting it and that you followed the steps in that chapter to activate the CSCI 1302 shell profile on your Odin account.
Step 1: Get and inspect the starter code
Execute the command below to download and extract the files:
sh -c "$(curl -fsSl https://cs1302uga.github.io/cs1302-book/_bundle/cs1302-ttt-alpha.sh)"
- downloading cs1302-ttt-alpha bundle...
- verifying integrity of downloaded files using sha256sum...
- extracting downloaded archive...
- removing intermediate files...
subdirectory cs1302-ttt-alpha successfully created
Change to the cs1302-ttt-alpha
directory that was created using
cd, then look at the files that were bundled as part of the
starter code using tree.
cd cs1302-ttt-alpha
tree
.
`-- src
|-- TicTacToeDriver.java
`-- TicTacToeGame.java
2 directories, 2 files
Use mkdir to create a directory named bin
.
mkdir bin
tree
.
|-- bin
`-- src
|-- TicTacToeDriver.java
`-- TicTacToeGame.java
Step 2: Compile the starter code
From this location, compile each .java
file in src
into bin
separately using javac, the Java compiler command. Make sure
that you include the appropriate command-line options to adjust the
compiler’s destination directory and, if needed, its class path. Also make
sure that you compile TicTacToeGame.java
first, even though it was not
the first file shown in the tree output. The other file depends
on it.
Sample Solution (try to figure out the commands yourself first)
javac -d bin src/TicTacToeGame.java
javac -d bin -cp bin src/TicTacToeDriver.java
If you entered the commands correctly, then the output of running tree should now look like this:
tree
.
|-- bin
| |-- TicTacToeDriver.class
| `-- TicTacToeGame.class
`-- src
|-- TicTacToeDriver.java
`-- TicTacToeGame.java
3 directories, 4 files
Even though the start code is not fully implemented, it is provided to you
in a state that compiles without any compile-time errors or warnings.
With that in mind, run TicTacToeDriver
from this lication using
java, the Java Virtual Machine command. Make sure that you
include the appropriate command-line options to adjust the class path so
that the compiler knows where to find the TicTacToeDriver
class.
Sample Solution (try to figure out the command yourself first)
java -cp bin TicTacToeDriver
0 | |
--+-+--
1 | |
--+-+--
2 | |
0 1 2
numTurns = 0
0 X| |
--+-+--
1 | |
--+-+--
2 | |
0 1 2
numTurns = 1
Step 3: Edit, recompile, and run the code
Open TicTacToeGame.java
for editing using the emacs
command. If you are not familiar with Emacs, then be sure to read the
chapter titled “The Emacs Text Editor” found elsewhere in this
book.
emacs src/TicTacToeGame.java
Some of the methods have been left unimplemented. Try to finish the code
for the game by implementing those methods. As you go, add code to
TicTacToeDriver.java
to test your work.
To save your edits, type C-x C-s. After that, exit Emacs by typing
C-x C-c to return to the shell so that you can recompile
TicTacToeGame.java
and TicTacToeDriver.java
Run the program with your changes until you are confident that it works correctly.
Important
Encounter Compiler Errors? If you encounter a compiler error when using javac, then inspect the error message emitted by the compiler carefully, then edit the file again to address the issue.
Important
Encounter Runtime Errors? If you encounter a runtime
error when using java, then you should add code to
TicTacToeDriver
that reproduce the error and additional
code to help you see what is going on. One you have a fix in
mind, edit the relevant file again to address the issue.
That’s it! You do not need to turn this in; however, you should keep your work around just in case it comes up again in a future activity.
TicTacToe (Packages and Checkstyle)¶
TicTacToe (Packages and Checkstyle)
Prerequisite Information
This practice activity should be done on Odin, so make to complete all of the tutorials in the Unix chapter before attempting it and that you followed the steps in that chapter to activate the CSCI 1302 shell profile on your Odin account. Students are also expected to be familiar with packages.
Step 1: Get and inspect the starter code
Execute the command below to download and extract the files:
sh -c "$(curl -fsSl https://cs1302uga.github.io/cs1302-book/_bundle/cs1302-ttt-beta.sh)"
- downloading cs1302-ttt-beta bundle...
- verifying integrity of downloaded files using sha256sum...
- extracting downloaded archive...
- removing intermediate files...
subdirectory cs1302-ttt-beta successfully created
Change to the cs1302-ttt-beta
directory that was created using
cd, then look at the files that were bundled as part of the
starter code using tree.
cd cs1302-ttt-beta
tree
.
`-- src
|-- TicTacToeDriver.java
`-- TicTacToeGame.java
2 directories, 2 files
Use mkdir to create a directory named bin
.
mkdir bin
tree
.
|-- bin
`-- src
|-- TicTacToeDriver.java
`-- TicTacToeGame.java
Step 2: Compile the starter code
The code is intended to be separated into packages. If use emacs to
edit each .java
file, you will notice that they both contain a package
statement near the top. Before compile, you will need to move each .java
file into its expected location based on the package structure you observe
in those package
statements.
Sample Solution (try to figure out the commands yourself first)
mkdir -p src/cs1302/state
mv src/TicTacToeGame.java src/cs1302/state/
mkdir -p src/cs1302/ui
mv src/TicTacToeDriver.java src/cs1302/ui/
After that, compile each .java
file in src
into bin
separately using javac, the Java compiler command. Make sure
that you include the appropriate command-line options to adjust the
compiler’s destination directory and, if needed, its class path. Also make
sure that you compile TicTacToeGame.java
first, even though it was not
the first file shown in the tree output. The other file depends
on it.
Sample Solution (try to figure out the commands yourself first)
javac -d bin src/cs1302/state/TicTacToeGame.java
javac -d bin -cp bin src/cs1302/ui/TicTacToeDriver.java
Even though the start code is not fully implemented, it is provided to you
in a state that compiles without any compile-time errors or warnings.
With that in mind, run TicTacToeDriver
from this lication using
java, the Java Virtual Machine command. Make sure that you
include the appropriate command-line options to adjust the class path so
that the compiler knows where to find the TicTacToeDriver
class.
Sample Solution (try to figure out the command yourself first)
java -cp bin cs1302.ui.TicTacToeDriver
0 | |
--+-+--
1 | |
--+-+--
2 | |
0 1 2
numTurns = 0
0 X| |
--+-+--
1 | |
--+-+--
2 | |
0 1 2
numTurns = 1
Step 3: Edit, recompile, and run the code
Open TicTacToeGame.java
for editing using the emacs
command. If you are not familiar with Emacs, then be sure to read the
chapter titled “The Emacs Text Editor” found elsewhere in this
book.
emacs src/cs1302/state/TicTacToeGame.java
Some of the methods have been left unimplemented. Try to finish the code
for the game by implementing those methods. As you go, add code to
TicTacToeDriver.java
to test your work.
To save your edits, type C-x C-s. After that, exit Emacs by typing
C-x C-c to return to the shell so that you can recompile
TicTacToeGame.java
and TicTacToeDriver.java
Run the program with your changes until you are confident that it works correctly.
Important
Check Style! Make sure that you run the check1302 command on Odin periodically so check for style guide violations.
check1302 src
Important
Encounter Compiler Errors? If you encounter a compiler error when using javac, then inspect the error message emitted by the compiler carefully, then edit the file again to address the issue.
Important
Encounter Runtime Errors? If you encounter a runtime
error when using java, then you should add code to
TicTacToeDriver
that reproduce the error and additional
code to help you see what is going on. One you have a fix in
mind, edit the relevant file again to address the issue.
That’s it! You do not need to turn this in; however, you should keep your work around just in case it comes up again in a future activity.