online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
import java.util.Scanner; import java.util.Random; public class Main { public static void main(String[] args) { // Initialize scanner and random generator Scanner scanner = new Scanner(System.in); Random rand = new Random(System.currentTimeMillis()); // Display intro art and game title System.out.println("\033[34m"); System.out.println(" # # ( ) "); System.out.println(" ___#_#___|__ "); System.out.println(" _ |____________| _ "); System.out.println(" _=====| | | | | |==== _ "); System.out.println(" =====| |.---------------------------. | |===="); System.out.println("<--------------------' . . . . . . . . '--------------/ "); System.out.println(" \\ / "); System.out.println(" \\_______________________________________________UOG__________/ "); System.out.println(" Welcome to MPJ BattleShip Game! "); System.out.println("\033[0m"); // Prompt user for game mode System.out.println("Choose your game mode:"); System.out.println("1. Player vs Computer (Square Board Only)"); System.out.println("2. Classic Mode (Manual Board Shape and Size Selection)"); System.out.print("Enter your choice (1 or 2): "); int gameMode = scanner.nextInt(); // Game Mode 1: Player vs Computer if (gameMode == 1) { // Ask user for board size and number of ships System.out.print("Enter board size: "); int boardSize = scanner.nextInt(); System.out.print("Enter number of ships: "); int numShips = scanner.nextInt(); // Initialize game boards and ship positions char[][] playerBoard = new char[boardSize][boardSize]; char[][] computerBoard = new char[boardSize][boardSize]; char[][] playerShips = new char[boardSize][boardSize]; char[][] computerShips = new char[boardSize][boardSize]; boolean[][] computerGuesses = new boolean[boardSize][boardSize]; // Fill boards with default values for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { playerBoard[i][j] = '-'; computerBoard[i][j] = '-'; playerShips[i][j] = '0'; computerShips[i][j] = '0'; } } // Randomly place player's and computer's ships for (int i = 0; i < numShips; i++) { // Place player's ship while (true) { int x = rand.nextInt(boardSize); int y = rand.nextInt(boardSize); if (playerShips[x][y] == '0') { playerShips[x][y] = '1'; break; } } // Place computer's ship while (true) { int x = rand.nextInt(boardSize); int y = rand.nextInt(boardSize); if (computerShips[x][y] == '0') { computerShips[x][y] = '1'; break; } } } // Display color selection menu String[] colors = {"Red", "Green", "Yellow", "Blue", "Cyan", "White"}; String[] colorCodes = {"\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[36m", "\033[37m"}; System.out.println("Choose a color for the text:"); for (int i = 0; i < colors.length; i++) { System.out.println((i + 1) + ". " + colors[i]); } // Let user choose a color System.out.print("Enter your choice (1-" + colors.length + "): "); int colorChoice = scanner.nextInt(); if (colorChoice < 1 || colorChoice > colors.length) colorChoice = 1; String selectedColor = colorCodes[colorChoice - 1]; String resetColor = "\033[0m"; // Track number of hits int playerHits = 0, computerHits = 0; int totalShips = numShips; // Main game loop while (playerHits < totalShips && computerHits < totalShips) { // Player's turn System.out.println(selectedColor + "Your Turn:" + resetColor); printBoard(computerBoard, selectedColor, resetColor); int row, col; // Get valid guess from player while (true) { System.out.print("Enter row (0 to " + (boardSize - 1) + "): "); row = scanner.nextInt(); System.out.print("Enter column (0 to " + (boardSize - 1) + "): "); col = scanner.nextInt(); if (row >= 0 && row < boardSize && col >= 0 && col < boardSize && computerBoard[row][col] == '-') break; System.out.println("Invalid or already guessed. Try again."); } // Check if player's guess is a hit if (computerShips[row][col] == '1') { System.out.println(selectedColor + "Hit!" + resetColor); computerBoard[row][col] = 'X'; playerHits++; } else { System.out.println(selectedColor + "Miss!" + resetColor); computerBoard[row][col] = 'O'; } // Check for player win if (playerHits == totalShips) break; // Computer's turn System.out.println(selectedColor + "Computer's Turn:" + resetColor); int compRow, compCol; do { compRow = rand.nextInt(boardSize); compCol = rand.nextInt(boardSize); } while (computerGuesses[compRow][compCol]); computerGuesses[compRow][compCol] = true; // Check if computer hit a ship if (playerShips[compRow][compCol] == '1') { System.out.println(selectedColor + "Computer hit your ship at (" + compRow + ", " + compCol + ")!" + resetColor); playerBoard[compRow][compCol] = 'X'; computerHits++; } else { System.out.println(selectedColor + "Computer missed at (" + compRow + ", " + compCol + ")!" + resetColor); playerBoard[compRow][compCol] = 'O'; } // Show player's board printBoard(playerBoard, selectedColor, resetColor); } // End of game: show winner if (playerHits == totalShips) { System.out.println(selectedColor + "You win! All enemy ships destroyed." + resetColor); } else { System.out.println(selectedColor + "You lost. The computer sank your fleet." + resetColor); } } // Game Mode 2: Classic Mode else { // Display alternative intro System.out.println(" ___/__"); System.out.println(" __/_____/"); System.out.println("____/___\\____\\____"); System.out.println("\\ MPJ E-2 < < < |"); System.out.println("~~~~~~~~~~~~~~~~~~"); // Ask for board shape and size System.out.println("Welcome to my battle ship game!"); System.out.println("Choose a board shape:"); System.out.println("1. Square"); System.out.println("2. Diamond Only For 7x7 Board"); System.out.println("3. Wide Rectangle"); System.out.println("4. Cross Only For 7x7 Board"); System.out.print("Enter your choice (1-4): "); int shapeChoice = scanner.nextInt(); // Enter custom board size and number of ships System.out.print("Enter the board size: "); int boardSize = scanner.nextInt(); System.out.print("Enter the number of ships: "); int numShips = scanner.nextInt(); char[][] board; char[][] ships; // Create board based on shape switch (shapeChoice) { case 3: board = new char[boardSize][boardSize + 2]; ships = new char[boardSize][boardSize + 2]; break; default: board = new char[boardSize][boardSize]; ships = new char[boardSize][boardSize]; } // Initialize board and mark playable tiles for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { boolean isPlayable = true; // Handle diamond shape logic if (shapeChoice == 2) { int center = board.length / 2; isPlayable = Math.abs(i - center) + Math.abs(j - center) <= center; } // Handle cross shape logic else if (shapeChoice == 4) { int mid = board.length / 2; isPlayable = (i == mid || j == mid); } // Assign symbols to board if (isPlayable) { board[i][j] = '-'; ships[i][j] = '0'; } else { board[i][j] = '#'; ships[i][j] = '#'; } } } // Randomly place ships for (int i = 0; i < numShips; i++) { while (true) { int x = rand.nextInt(board.length); int y = rand.nextInt(board[0].length); if (ships[x][y] == '0') { ships[x][y] = '1'; break; } } } // Choose text color String[] colors = {"Red", "Green", "Yellow", "Blue", "Cyan", "White"}; String[] colorCodes = {"\033[31m", "\033[32m", "\033[33m", "\033[34m", "\033[36m", "\033[37m"}; System.out.println("Choose a color for the text:"); for (int i = 0; i < colors.length; i++) { System.out.println((i + 1) + ". " + colors[i]); } System.out.print("Enter your choice (1-" + colors.length + "): "); int colorChoice = scanner.nextInt(); if (colorChoice < 1 || colorChoice > colors.length) { System.out.println("Invalid choice. Defaulting to Red."); colorChoice = 1; } String selectedColor = colorCodes[colorChoice - 1]; String resetColor = "\033[0m"; // Begin game turns System.out.println(selectedColor + numShips + " hidden ships on the board." + resetColor); int totalTurns = numShips * 2 + 1; int hits = 0, misses = 0; for (int turn = 0; turn < totalTurns; turn++) { System.out.println(selectedColor + "=== Turn " + (turn + 1) + " of " + totalTurns + " ===" + resetColor); System.out.println(selectedColor + "Current Board:" + resetColor); printBoard(board, selectedColor, resetColor); // Get player's guess int row = 0, col = 0; boolean validInput = false; while (!validInput) { System.out.print("Enter a row number (0-" + (board.length - 1) + "): "); row = scanner.nextInt(); System.out.print("Enter a column number (0-" + (board[0].length - 1) + "): "); col = scanner.nextInt(); if (row < 0 || row >= board.length || col < 0 || col >= board[0].length) { System.out.println("Invalid position. Try again."); } else if (board[row][col] != '-') { System.out.println("Position already taken or not playable. Try again."); } else { validInput = true; } } // Check if hit or miss if (ships[row][col] == '1') { System.out.println(selectedColor + "You hit a ship!" + resetColor); board[row][col] = 'X'; hits++; } else { System.out.println(selectedColor + "You missed!" + resetColor); board[row][col] = 'O'; misses++; } System.out.println(selectedColor + "Hits: " + hits + " Misses: " + misses + resetColor); // Check for win if (hits == numShips) { System.out.println(selectedColor + "Congratulations! You sank all the ships!" + resetColor); break; } } // End of game System.out.println(selectedColor + "********************" + resetColor); System.out.println(selectedColor + "Game Over!" + resetColor); System.out.println(selectedColor + "Final Board:" + resetColor); printBoard(board, selectedColor, resetColor); // Show final stats double hitPercentage = (double) hits / (hits + misses) * 100; hitPercentage = Math.round(hitPercentage * 100.0) / 100.0; System.out.println(selectedColor + "Hit percentage: " + hitPercentage + "%" + resetColor); } // Close scanner scanner.close(); } // Print the board with color formatting public static void printBoard(char[][] board, String colorCode, String resetCode) { int rows = board.length; int cols = board[0].length; System.out.print(" "); for (int i = 0; i < cols; i++) { System.out.print(colorCode + i + " " + resetCode); } System.out.println(); for (int i = 0; i < rows; i++) { System.out.print(colorCode + i + " " + resetCode); for (int j = 0; j < cols; j++) { System.out.print(colorCode + board[i][j] + " " + resetCode); } System.out.println(); } } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue