'F' → Fullscreen
main. // Figure 1 — A printable board method (Allman style)public static void printBoard(char[][] board){ System.out.println(); System.out.println(" 0 1 2"); System.out.println(" -----------");
for (int r = 0; r < board.length; r++) { System.out.print(r + " |");
for (int c = 0; c < board[r].length; c++) { System.out.print(" " + board[r][c] + " |"); }
System.out.println(); System.out.println(" -----------"); }
System.out.println();}// Figure 2 — Move validation (Allman style)public static boolean isValidMove(char[][] board, int row, int col){ if (row < 0 || row >= board.length) { return false; }
if (col < 0 || col >= board[row].length) { return false; }
if (board[row][col] != '-') { return false; }
return true;}// Figure 3 — Win check (Allman style)public static boolean checkWin(char[][] board, char player){ // Rows for (int r = 0; r < board.length; r++) { if (board[r][0] == player && board[r][1] == player && board[r][2] == player) { return true; } }
// Columns for (int c = 0; c < board[0].length; c++) { if (board[0][c] == player && board[1][c] == player && board[2][c] == player) { return true; } }
// Diagonals if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { return true; }
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { return true; }
return false;}// Figure 4 — Tie check (Allman style)public static boolean isBoardFull(char[][] board){ for (int r = 0; r < board.length; r++) { for (int c = 0; c < board[r].length; c++) { if (board[r][c] == '-') { return false; } } }
return true;}'-'. public class Program{ public static void main(String[] args) { // Task 1 — Build the board + print it // // 1) Create a 3x3 char board filled with '-' // 2) Call printBoard(board) to display it // // Expected: an empty 3x3 grid with row/col labels.
char[][] board = { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} };
printBoard(board); }
public static void printBoard(char[][] board) { System.out.println(); System.out.println(" 0 1 2"); System.out.println(" -----------");
for (int r = 0; r < board.length; r++) { System.out.print(r + " |");
for (int c = 0; c < board[r].length; c++) { System.out.print(" " + board[r][c] + " |"); }
System.out.println(); System.out.println(" -----------"); }
System.out.println(); }}'X'. import java.util.Scanner;
public class Program{ public static void main(String[] args) { // Task 2 — Place a move (single turn) // // 1) Start with an empty board // 2) Ask the user for row and col (0-2) // 3) If the move is valid, place 'X' there // 4) Print the board // // Note: We'll build the full game loop later.
char[][] board = { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} };
Scanner scanner = new Scanner(System.in);
printBoard(board);
System.out.print("Enter row (0-2): "); int row = scanner.nextInt();
System.out.print("Enter col (0-2): "); int col = scanner.nextInt();
if (isValidMove(board, row, col)) { board[row][col] = 'X'; System.out.println("Move placed!"); } else { System.out.println("Invalid move."); }
printBoard(board);
scanner.close(); }
public static void printBoard(char[][] board) { System.out.println(); System.out.println(" 0 1 2"); System.out.println(" -----------");
for (int r = 0; r < board.length; r++) { System.out.print(r + " |");
for (int c = 0; c < board[r].length; c++) { System.out.print(" " + board[r][c] + " |"); }
System.out.println(); System.out.println(" -----------"); }
System.out.println(); }
public static boolean isValidMove(char[][] board, int row, int col) { if (row < 0 || row >= board.length) { return false; }
if (col < 0 || col >= board[row].length) { return false; }
if (board[row][col] != '-') { return false; }
return true; }}checkWin(board, player). public class Program{ public static void main(String[] args) { // Task 3 — Implement checkWin(board, player) // // 1) Copy the board below // 2) Call checkWin(board, 'X') and print the result // // The board below is a win for X on the top row.
char[][] board = { {'X', 'X', 'X'}, {'O', '-', 'O'}, {'-', '-', '-'} };
boolean xWon = checkWin(board, 'X'); System.out.println("X won? " + xWon); }
public static boolean checkWin(char[][] board, char player) { // Rows for (int r = 0; r < board.length; r++) { if (board[r][0] == player && board[r][1] == player && board[r][2] == player) { return true; } }
// Columns for (int c = 0; c < board[0].length; c++) { if (board[0][c] == player && board[1][c] == player && board[2][c] == player) { return true; } }
// Diagonals if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { return true; }
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { return true; }
return false; }}isBoardFull(board). public class Program{ public static void main(String[] args) { // Task 4 — Implement isBoardFull(board) // // 1) Create two boards: // A) a full board (no '-') // B) a not-full board (has '-') // 2) Print isBoardFull(...) for each
char[][] fullBoard = { {'X', 'O', 'X'}, {'X', 'O', 'O'}, {'O', 'X', 'X'} };
char[][] notFullBoard = { {'X', 'O', '-'}, {'X', '-', 'O'}, {'O', 'X', 'X'} };
System.out.println("Full board? " + isBoardFull(fullBoard)); System.out.println("Full board? " + isBoardFull(notFullBoard)); }
public static boolean isBoardFull(char[][] board) { for (int r = 0; r < board.length; r++) { for (int c = 0; c < board[r].length; c++) { if (board[r][c] == '-') { return false; } } }
return true; }}X and O. import java.util.Scanner;
public class Program{ public static void main(String[] args) { // Task 5 — Milestone: Full Tic-Tac-Toe Game // // Features: // - 2D char board // - player turns (X then O) // - input validation (row/col in range, cell empty) // - win detection // - tie detection // // Suggested flow: // while (true) // print board // prompt move for current player // if invalid -> message and continue // place piece // if win -> print board + winner and break // if tie -> print board + tie message and break // switch player
char[][] board = { {'-', '-', '-'}, {'-', '-', '-'}, {'-', '-', '-'} };
Scanner scanner = new Scanner(System.in);
char currentPlayer = 'X';
while (true) { printBoard(board);
System.out.println("Player " + currentPlayer + "'s turn."); System.out.print("Enter row (0-2): "); int row = scanner.nextInt();
System.out.print("Enter col (0-2): "); int col = scanner.nextInt();
if (!isValidMove(board, row, col)) { System.out.println("Invalid move. Try again."); continue; }
board[row][col] = currentPlayer;
if (checkWin(board, currentPlayer)) { printBoard(board); System.out.println("Player " + currentPlayer + " wins!"); break; }
if (isBoardFull(board)) { printBoard(board); System.out.println("It's a tie!"); break; }
// switch player if (currentPlayer == 'X') { currentPlayer = 'O'; } else { currentPlayer = 'X'; } }
scanner.close(); }
public static void printBoard(char[][] board) { System.out.println(); System.out.println(" 0 1 2"); System.out.println(" -----------");
for (int r = 0; r < board.length; r++) { System.out.print(r + " |");
for (int c = 0; c < board[r].length; c++) { System.out.print(" " + board[r][c] + " |"); }
System.out.println(); System.out.println(" -----------"); }
System.out.println(); }
public static boolean isValidMove(char[][] board, int row, int col) { if (row < 0 || row >= board.length) { return false; }
if (col < 0 || col >= board[row].length) { return false; }
if (board[row][col] != '-') { return false; }
return true; }
public static boolean checkWin(char[][] board, char player) { // Rows for (int r = 0; r < board.length; r++) { if (board[r][0] == player && board[r][1] == player && board[r][2] == player) { return true; } }
// Columns for (int c = 0; c < board[0].length; c++) { if (board[0][c] == player && board[1][c] == player && board[2][c] == player) { return true; } }
// Diagonals if (board[0][0] == player && board[1][1] == player && board[2][2] == player) { return true; }
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) { return true; }
return false; }
public static boolean isBoardFull(char[][] board) { for (int r = 0; r < board.length; r++) { for (int c = 0; c < board[r].length; c++) { if (board[r][c] == '-') { return false; } } }
return true; }}Your program output should something similar to the sample output below.
0 1 2 -----------0 | - | - | - | -----------1 | - | - | - | -----------2 | - | - | - | -----------
Player X's turn.Enter row (0-2): 1Enter col (0-2): 1
0 1 2 -----------0 | - | - | - | -----------1 | - | X | - | -----------2 | - | - | - | -----------
Player O's turn.Enter row (0-2): 0Enter col (0-2): 0
... (game continues) ...
Player X wins!You may write your reflection answers as comments at the bottom of your code.
checkWin method works.Submit your activity and reflection answers to the appropriate dropbox.