Touchstone JAVA Journal
Touchstone JAVA Journal
Directions: Follow the directions for each part of the journal template. Include in your response
all the elements listed under the Requirements section. Prompts in the Inspiration section are
not required; however, they may help you to fully think through your response.
Remember to review the Touchstone page for entry requirements, examples, and grading
specifics.
1
PART 1: Defining Your Problem
Task
State the problem you are planning to solve.
Requirements
● Describe the problem you are trying to solve.
● Describe any input data you expect to use.
● Describe what the program will do to solve the problem.
● Describe any outputs or results the program will provide.
Inspiration
When writing your entry below, ask yourself the following questions:
● Is your problem clearly defined?
● What source(s) of data do you believe you will need? Will the user need to supply that data,
or will you get it from an external file or another source?
● Will you need to interact with the user throughout the program? Will users continually need to
enter data in and see something to continue?
● What are your expected results or what will be the end product? What will you need to tell a
user of your program when it is complete?
Problem Description:
The problem to be solved is implementing a Tic Tac Toe game in Java. Tic Tac Toe is a classic
two-player game where players take turns marking spaces in a 3x3 grid with their respective
symbols ('X' and 'O') until one player wins by getting three of their symbols in a row, column, or
diagonal, or the game ends in a draw if the grid is full and there is no winner.
Input Data:
The program expects input data from the user in the form of row and column numbers to specify
their moves on the game board. These inputs should be integers ranging from 0 to 2, indicating
the row and column where the player wants to place their symbol ('X' or 'O').
Program Functionality:
The program will create a 3x3 grid to represent the Tic Tac Toe board. It will alternate between
the two players ('X' and 'O') allowing them to make moves by entering row and column numbers.
After each move, the program will check if the move is valid (i.e., the chosen cell is empty),
update the board with the player's symbol, and then check for a winner or a draw condition. The
2
game continues until a player wins or the board is full.
Outputs/Results:
The program will output the current state of the game board after each move, showing the positions
of 'X' and 'O' symbols. When a player wins, the program will display a message indicating the winner
('X' or 'O'). If the game ends in a draw, it will display a message indicating that the game is a draw.
After the game is complete, the program will terminate, and the user will be notified that the game is
over.
3
PART 2: Working Through Specific Examples
Task
Write down clear and specific steps to solve a simple version of your problem you identified in Part 1.
Requirements
Complete the three steps below for at least two distinct examples/scenarios.
● State any necessary input data for your simplified problem.
● Write clear and specific steps in English (not Java) detailing what the program will do to solve
the problem.
● Describe the specific result of your example/scenario.
Inspiration
When writing your entry below, ask yourself the following questions:
● Are there any steps that you don’t fully understand? These are places to spend more time
working out the details. Consider adding additional smaller steps in these spots.
● Remember that a computer program is very literal. Are there any steps that are unclear? Try
giving the steps of your example/scenario to a friend or family member to read through and
ask you questions about parts they don’t understand. Rewrite these parts as clearly as you
can.
● Are there interesting edge cases for your program? Try to start one of your
examples/scenarios with input that matches this edge case. How does it change how your
program might work?
Input Data:
Player 'X' makes the following moves:
1. Row: 0, Column: 0 (top-left)
2. Row: 1, Column: 1 (middle)
3. Row: 0, Column: 1 (top-middle)
4. Row: 1, Column: 2 (top-right)
5. Row: 0, Column: 2 (middle-right)
Steps:
1. Create a 3x3 grid representing the Tic Tac Toe board.
2. Display the initial empty board.
3. Prompt Player 'X' to enter their move:
4
- Player 'X' enters row: 0, column: 0.
4. Update the board with 'X' at position (0, 0).
5. Display the updated board.
6. Prompt Player 'O' to enter their move:
- Player 'O' enters row: 1, column: 1.
7. Update the board with 'O' at position (1, 1).
8. Display the updated board.
9. Prompt Player 'X' to enter their move:
- Player 'X' enters row: 0, column: 1.
10. Update the board with 'X' at position (0, 1).
11. Display the updated board.
12. Prompt Player 'O' to enter their move:
- Player 'O' enters row: 1, column: 2.
13. Update the board with 'O' at position (1, 2).
14. Display the updated board.
15. Prompt Player 'X' to enter their move:
- Player 'X' enters row: 0, column: 2.
16. Update the board with 'X' at position (0, 2).
17. Display the updated board.
18. Check for a winner:
- Since Player 'X' has three 'X's in a row horizontally (top row), declare Player 'X' as the
winner.
19. Display the final board.
20. Output "Player 'X' wins!".
Result:
Player 'X' wins the game by getting three of their symbols in a row horizontally on the top row of
the board.
Input Data:
Players 'X' and 'O' make the following moves:
1. 'X' at (0, 0)
2. 'O' at (0, 1)
3. 'X' at (0, 2)
4. 'O' at (1, 0)
5. 'X' at (1, 2)
6. 'O' at (1, 1)
7. 'X' at (2, 0)
8. 'O' at (2, 2)
9. 'X' at (2, 1)
Steps:
5
1. Create a 3x3 grid representing the Tic Tac Toe board.
2. Display the initial empty board.
3. Prompt Player 'X' to enter their move:
- Player 'X' enters row: 0, column: 0.
4. Update the board with 'X' at position (0, 0).
5. Display the updated board.
6. Prompt Player 'O' to enter their move:
- Player 'O' enters row: 0, column: 1.
7. Update the board with 'O' at position (0, 1).
8. Display the updated board.
9. Repeat steps 3-8 until the board is full or a player wins.
10. After the last move, check if there is a winner or if the board is full:
- Since there are no three symbols in a row, column, or diagonal, and the board is full, declare
the game as a draw.
11. Display the final board.
12. Output "It's a draw!".
Result:
The game ends in a draw as neither player was able to get three of their symbols in a row, column, or
diagonal, and the board is full.
6
PART 3: Generalizing Into Pseudocode
Task
Write out the general sequence your program will use, including all specific examples/scenarios you
provided in Part 2.
Requirements
● Write pseudocode for the program in English but refer to Java program elements where they
are appropriate. The pseudocode should represent the full functionality of the program, not
just a simplified version. Pseudocode is broken down enough that the details of the program
are no longer in any paragraph form. One statement per line is ideal.
Inspiration
When writing your entry below, ask yourself the following questions:
● Do you see common program elements and patterns in your specific examples/scenarios in
Part 2, like variables, conditionals, functions, loops, and classes? These should be part of
your pseudocode for the general sequence as well.
● Are there places where the steps for your examples/scenarios in Part 2 diverged? These may
be places where errors may occur later in the project. Make note of them.
● When you are finished with your pseudocode, does it make sense, even to a person that does
not know Java? Aim for the clearest description of the steps, as this will make it easier to
convert into program code later.
7
Class Card:
// Represents a single card in the game
Attributes:
- value: int
Methods:
- Constructor(value: int): Initialize card with given value
Class Deck:
// Represents a deck of cards in the game
Attributes:
- deck: Stack<Card>
Methods:
- Constructor(): Initialize deck with 52 cards and shuffle them
- draw(): Draw a card from the deck
Class Player:
// Represents a player in the game
Attributes:
- hand: ArrayList<Card>
- handValue: int
- bust: boolean
Methods:
- Constructor(): Initialize player's hand and attributes
- draw(deck: Deck): Draw a card from the deck and update hand
- getHandValue(): Return the total value of the hand
- isBust(): Check if the player has busted
Class TicTacToe:
// Represents the Tic Tac Toe game
Attributes:
- board: 2D array of chars
- currentPlayer: char
Methods:
- Constructor(): Initialize the board and set current player to 'X'
- initializeBoard(): Fill the board with empty cells
- printBoard(): Display the current state of the board
- isValidMove(row: int, col: int): Check if the move is valid
- hasWinner(): Check if there's a winner
- switchPlayer(): Switch between players
- makeMove(row: int, col: int): Perform the move on the board
- play(): Main game loop to play the Tic Tac Toe game
Main:
8
Function main():
- Create an instance of TicTacToe game
- Call the play() method to start the game
9
PART 4: Testing Your Program
Task
While writing and testing your program code, describe your tests, record any errors, and state your
approach to fixing the errors.
Requirements
● For at least one of your test cases, describe how your choices for the test helped you
understand whether the program was running correctly or not.
For each error that occurs while writing and testing your code:
● Record the details of the error from Replit. A screenshot or copy-and-paste of the text into the
journal entry is acceptable.
● Describe what you attempted in order to fix the error. Clearly identify which approach was the
one that worked.
Inspiration
When writing your entry below, ask yourself the following questions:
● Have you tested edge cases and special cases for the inputs of your program code? Often
these unexpected values can cause errors in the operation of your program.
● Have you tested opportunities for user error? If a user is asked to provide an input, what
happens when they give the wrong type of input, like a letter instead of a number, or vice
versa?
● Did the outcome look the way you expected? Was it formatted correctly?
● Does your output align with the solution to the problem you coded for?
10
During the development of the Tic Tac Toe program, I performed various tests to ensure its
correctness and robustness. Here's an example of a test case:
**Error Handling:**
While implementing the `isValidMove` method in the `TicTacToe` class to check for valid moves, I
encountered an error regarding array index out of bounds. Here's the error message:
```
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for
length 3
at TicTacToe.isValidMove(TicTacToe.java:50)
at TicTacToe.play(TicTacToe.java:105)
at TicTacToe.main(TicTacToe.java:154)
```
**Approach to Fix:**
To fix this error, I reviewed the `isValidMove` method and identified that the condition for validating
row and column indices should be `< 3` instead of `<= 3`. I corrected the condition to ensure that the
indices are within the range of the board dimensions. After making this change, the error was
resolved, and the program executed without issues.
Task
Submit your full program code, including thorough comments describing what each portion of the
program should do when working correctly.
11
Requirements
● The purpose of the program and each of its parts should be clear to a reader that does not
know the Java programming language.
Inspiration
When writing your entry, you are encouraged to consider the following:
● Is each section or sub-section of your code commented to describe what the code is doing?
● Give your code with comments to a friend or family member to review. Add additional
comments to spots that confuse them to make it clearer.
```java
import java.util.Scanner;
12
}
}
// Switch players
private void switchPlayer() {
// Switch between 'X' and 'O' players
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
13
public void play() {
Scanner scanner = new Scanner(System.in);
// Game loop continues until there's a winner or the board is full
while (true) {
System.out.println("Player " + currentPlayer + "'s turn:");
printBoard();
System.out.print("Enter row and column (0-2): ");
int row = scanner.nextInt();
int col = scanner.nextInt();
// Check if the move is valid
if (isValidMove(row, col)) {
// Make the move and check for a winner
makeMove(row, col);
if (hasWinner()) {
// Display the winner and end the game
System.out.println("Player " + currentPlayer + " wins!");
printBoard();
break;
}
// Check if the board is full
boolean isBoardFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == '-') {
isBoardFull = false;
break;
}
}
}
// If the board is full and there's no winner, it's a draw
if (isBoardFull) {
System.out.println("It's a draw!");
printBoard();
break;
}
} else {
// Invalid move, prompt the player to try again
System.out.println("Invalid move. Try again.");
}
}
scanner.close();
}
14
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.play();
}
}
```
In this program, we implement a simple Tic Tac Toe game. Here's a breakdown of the program:
1. **TicTacToe Class**: This class represents the Tic Tac Toe game and contains methods for
initializing the board, printing the board, checking for valid moves, checking for a winner, switching
players, making moves, and running the main game loop.
2. **Constructor**: Initializes the game board and sets the starting player to 'X'.
5. **isValidMove(int row, int col)**: Checks if the specified move (row, col) is valid.
Task
Provide the Replit link to your full program code.
Requirements
● The program must work correctly with all the comments included in the program.
Inspiration
● Check before submitting your Touchstone that your final version of the program is running
successfully.
15
https://replit.com/@keosamnangoffic/Tic-Tac-Toe-in-Java
16