Activities

S6Problem1.java

import java.util.*;    

public class S6Problem1 {
    public static void main(String[] args) {
        System.out.println("Problem 1");
        Scanner in = new Scanner(System.in);
        


    } 
}

S6Problem2.java

import java.util.*;    

public class S6Problem2 {
    public static void main(String[] args) {
        System.out.println("Problem 2");
        Scanner in = new Scanner(System.in);
        
        
    }
}

S6Problem3.java

import java.util.*;    

public class S6Problem3 {
    public static void main(String[] args) {
        System.out.println("Problem 3");
        Scanner in = new Scanner(System.in);
    
    
    }
}

S6Problem4.java

import java.util.*;    

public class S6Problem4 {
    public static void main(String[] args) {
        System.out.println("Problem 4");
        Scanner in = new Scanner(System.in);
        


    }
}

S6Problem5.java

import java.util.*;    

public class S6Problem5 {
    public static void main(String[] args) {
        System.out.println("Problem 5");
        Scanner in = new Scanner(System.in);
        


    }
}

S6Problem6.java

import java.util.*;    

public class S6Problem6 {
    public static void main(String[] args) {
        System.out.println("Problem 6");
        Scanner in = new Scanner(System.in);
        


    }
}

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
    }
}

Last updated