😎
Introduction to Programming with Java
  • Introduction to Programming with Java
  • Session 1
    • Assignment1.java
  • Session 2
    • Source Code
    • Activities
    • Assignment
  • Session 3
    • Source Code
    • Activities
    • Assignment
  • Session 4
    • Source Code
    • Activities
  • Session 5
    • Source Code
    • Project
  • Session 6
    • Source Code
  • Session 7
    • Source Code
  • Session 8
    • Source Code
  • Session 9
    • Source Code
  • Session 10
    • Source Code
Powered by GitBook
On this page
  • Session6.java
  • CommandLine.java
  • TicTacToe.java
  1. Session 6

Source Code

Session6.java

import java.util.*;

public class Session6 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String myName = ""; // Add your name
        System.out.println("Session 6 - " + myName);

        // menu variables
        boolean done = false;
        String choice;

        do {
            // Menu
            System.out.println("Menu");
            System.out.println("E1 - Example 1");
            // optionally, add new menu items

            System.out.println("Q - Quit");
            System.out.print("Choice: ");
            choice = in.nextLine();

            switch (choice) {
                case "E1":
                    System.out.println("Example 1");

                    break;
                case "E2":
                    System.out.println("Example 2");

                    break;
                case "E3":
                    System.out.println("Example 3");
                    example3();
                    break;
                case "E4":
                    System.out.println("Example 4");
                    example4();
                    break;
                case "E5":
                    System.out.println("Example 5");
                    example5();
                    break;
                case "E6":
                    System.out.println("Example 6");

                    break;
                case "E7":
                    System.out.println("Example 7");

                    break;
                // quit and default cases
                case "Q":
                    System.out.println("Quit");
                    done = true;
                    break;
                default:
                    System.out.println("Invalid Choice");
                    break;
            }
        } while (!done);
    }
    // printArray method definition
    public static void printArray(int[] arrayName) {
        System.out.println("Index\tValue");
        for (int i = 0; i < arrayName.length; i++) {
            System.out.println(i + "\t" + arrayName[i]);
        }
    }

    // example1 method definition
    public static void example1() {

    }

    // example2 method definition
    public static void example2() {

    }

    // example3 method definition
    public static void example3() {


    // example4 method definition
    public static void example4() {


        // print 2D array via deepToString
        

        // print the number of rows
        

        // print the number of columns
        

        // print using nested for loops


   }
    // example5 method definition
    public static void example5() {
        // declare constants


        // create 2D array named seatingChart
 

        // initialize the array elements


        // print the array elements


        // print the rows and columns
 

        // using nested for loops, print the elements
 
 
    }
    // example6 method definition
    public static void example6() {
        int[][] grid = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };


    }
    // example6 method definition
    public static void example7() {
        int[][] grid = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };



    }
}

CommandLine.java

import java.util.*;

public class CommandLine {
    public static void main(String[] args) {
        System.out.println("Command Line Arguments Example");
        if (args.length > 0) {
            String choice = args[0];
            switch (choice) {
                case "E1":
                    System.out.println("Example 1");
                    break;
                case "E2":
 
                    break;
                default:
                    System.out.println("Invalid Command Line Option");
                    break;
            }
        } else {
            System.out.println("add a command line argument");
        }
    }
}

TicTacToe.java

import java.util.*;

public class TicTacToe {
    public static void main(String[] args) {
        // Initialize the game board with empty spaces
        char[][] board = {
            {' ', ' ', ' '},
            {' ', ' ', ' '},
            {' ', ' ', ' '}
        };
        
        // Set the starting player to 'X'
        char currentPlayer = 'X';
        boolean gameWon = false;
        Scanner in = new Scanner(System.in);
        
        // Main game loop, continues until the game is won or the board is full
        while (!gameWon && !isBoardFull(board)) {
            printBoard(board); // Print the current state of the board
            playerMove(board, currentPlayer, in); // Prompt the current player for their move
            gameWon = checkWinner(board, currentPlayer); // Check if the current player has won
            if (!gameWon) {
                // Switch to the other player if the game is not won
                currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
            }
        }
        
        printBoard(board); // Print the final state of the board
        
        // Announce the result of the game
        if (gameWon) {
            System.out.println("Player " + currentPlayer + " wins!");
        } else {
            System.out.println("The game is a tie!");
        }
        
        in.close(); // Close the scanner
    }

    // Method to check if the current player has won
    public static boolean checkWinner(char[][] board, char player) {
        // Check each row for a win    


        // Check each column for a win


        // Check the first diagonal (top-left to bottom-right) for a win


        // Check the second diagonal (top-right to bottom-left) for a win


        // If no win conditions are met, return false
        return false;
    }
    
    // Method to print the current state of the board
    public static void printBoard(char[][] board) {
        System.out.println("  0 1 2"); // Column indices
        for (int i = 0; i < board.length; i++) {
            System.out.print(i + " "); // Row index
            for (int j = 0; j < board[i].length; j++) {
                System.out.print(board[i][j]); // Print each cell
                if (j < board[i].length - 1) System.out.print("|"); // Print vertical dividers
            }
            System.out.println();
            if (i < board.length - 1) System.out.println("  -----"); // Print horizontal dividers
        }
    }

    // Method to prompt the player for their move
    public static void playerMove(char[][] board, char player, Scanner in) {
        int row = -1;
        int col = -1;
        boolean validMove = false;
        
        // Continue prompting until a valid move is entered
        while (!validMove) {
            System.out.println("Player " + player + ", enter your move (row and column): ");
            row = in.nextInt(); // Read the row index
            col = in.nextInt(); // Read the column index
            // Check if the move is within bounds and the cell is empty
            if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == ' ') {
                board[row][col] = player; // Place the player's symbol on the board
                validMove = true;
            } else {
                System.out.println("This move is not valid."); // Inform the player if the move is invalid
            }
        }
    }

    // Method to check if the board is full
    public static boolean isBoardFull(char[][] board) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] == ' ') {
                    return false; // Return false if any cell is empty
                }
            }
        }
        return true; // Return true if all cells are filled
    }
}
PreviousProjectNextSource Code

Last updated 10 months ago