Demo entry 6630895

DotCom

   

Submitted by anonymous on Jul 11, 2017 at 11:18
Language: Java. Code size: 11.2 kB.

import java.io.*;
import java.util.*;

class DotComMain {
	public static void main (String[] args) {
		
		// Create objects
		GetInput input = new GetInput();
		dotComGuessChecker theDotCom = new dotComGuessChecker();
		DotComGrid grid = new DotComGrid();
		
		// Create grid and cells
		grid.setGridLengthAndSize(7);
		grid.setGridCells();
		grid.setGirdCellNames();
		theDotCom.setLocationCells(grid.generateComs());
		
		// Game is alive
		boolean isAlive = true;
		// Guess number
		int numOfGuess = 0;
		// Result
		String result = null;
		
		// Title
		System.out.println("");
		System.out.println(".o*O*o_ [THE DOT COM GAME] _o*O*o.");
		System.out.println("O>>>-IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII");
		System.out.println("Your goal is to sink three ships in the fewest number of guesses.");
		System.out.println("");
		System.out.println("");
		
		while (isAlive == true) {
			// Guess input
			String guess = input.getUserInput("Enter the code of a cell between A1 and G7: ");
			
			// 
			if (grid.getGirdCellNames().contains(guess)) {
				result = theDotCom.checkInput(guess);		
				numOfGuess++;
			} else if (guess.equals("EXIT")) {
				System.out.println("O>>>-IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII");
				break;
			} else {
				System.out.println("");
				System.out.println("That's not a valid input.");
				System.out.println("");
			}
			
			// Quit & Print numofGuess
			if (result.equals("Fatality!")){
				isAlive = false;
				
				System.out.print("You've managed to sunk each and every ship. ");
				
				if (numOfGuess <= 9){
					System.out.println("But wait... WoW, awesome! How have you finished the game in only " + numOfGuess + " guesses? I GUESS, we've got a lucky boi here.");
				} else if (9 < numOfGuess && numOfGuess <= 18) {
					System.out.println("It's great! Congratulations! It has only taken you " + numOfGuess + " guesses. Keep it up, bro or sis!");
				} else if (18 < numOfGuess && numOfGuess <= 36) {
					System.out.println("You have been average, like being with a hottie on a warm summer night, who is a bit drunken and sweaty; not bad, but not extraordinary, either. Anyway, you've managed to complete the game in " + numOfGuess + " guesses.");
				} else if (36 < numOfGuess && numOfGuess <= 49) {
					System.out.println("Finally... I know... I know... It is not particularly nice to judge others, and at least you've managed to finish the game. However, you have really taken your time... If truth be told, it has taken you " + numOfGuess + " guesses.");
				} else if (49 < numOfGuess && numOfGuess <= 98) {
					System.out.println("Hah... I am actually surprised. Now I don't want to be rude, but you have been terrible... It has taken you " + numOfGuess + " guesses... I am not an expert, but that's not a few; quite a lot in fact.");
				} else {
					System.out.println("Which is insane! There were times when even I wanted to give it up and just say: 'Whatevs... You can win now, just leave me alone.' I ought not to even make a remark on this... But " + numOfGuess + " guesses??? You can't be for real, dude... It has long been out of the boundaries of ill luck. Make yourself and humanity a favour, will you? Just go home, lock the door of your room, and never touch a computer again... You had better not meet people, either. You might cause their brain to melt down. But we don't want that, do we?");
				}
			}
		}
	}
}

class dotComGuessChecker {
	// Locations of the ships
	private ArrayList<ArrayList<String>> locationCells = new ArrayList<ArrayList<String>>();
	private ArrayList<String> ship1 = new ArrayList<String>();
	private ArrayList<String> ship2 = new ArrayList<String>();
	private ArrayList<String> ship3 = new ArrayList<String>();
	
	// Set locationCells
	public void setLocationCells (ArrayList<ArrayList<String>> locations) {
		locationCells = locations;
		ship1 = locationCells.get(0);
		ship2 = locationCells.get(1);
		ship3 = locationCells.get(2);
	}
	
	// Check guess
	public String checkInput(String guess) {
		String result = "Miss...";
		
		// Check if hit
		if (ship1.contains(guess)) {
			result = "Ouch! You hit Larry's ship.";
			ship1.remove(guess);
		} else if (ship2.contains(guess)) {
			result = "Ouch! You hit Bob's ship.";
			ship2.remove(guess);
		} else if (ship3.contains(guess)) {
			result = "Com'n... You hit the TITANIC.";
			ship3.remove(guess);
		}
			
		// Check if kill
		if (ship1.isEmpty()){
			result = "Oh, well... You sank Larry's ship.";
			ship1.add("sunk");
		} else if (ship2.isEmpty()) {
			result = "Oh, well... You sank Bob's ship.";
			ship2.add("sunk");
		} else if (ship3.isEmpty()) {
			result = "Oh, nooo! You sank the TITANIC.";
			ship3.add("sunk");
		}
		
		// Print & Return
		System.out.println(result);	
		
		if (result.equals("Oh, well... You sank Larry's ship.") || result.equals("Oh, well... You sank Bob's ship.") || result.equals("Oh, nooo! You sank the TITANIC.")) {
			System.out.println("O>>>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		}
		
		System.out.println("");
		
		// Check if all ships are sunk
		if (ship1.contains("sunk") && ship2.contains("sunk") && ship3.contains("sunk")) {
			return "Fatality!";
		}
		
		return result;

	}
}

// Not quite there yet...
class GetInput {
  public String getUserInput(String prompt) {
     String inputLine = null;
     System.out.print(prompt + "  ");
     try {
       BufferedReader is = new BufferedReader(
	 new InputStreamReader(System.in));
       inputLine = is.readLine();
       if (inputLine.length() == 0 )  return null; 
     } catch (IOException e) {
       System.out.println("IOException: " + e);
     }
     return inputLine.toUpperCase();
  }
}

class DotComGrid {
	private ArrayList<Integer> gridCells = new ArrayList<Integer>();
	private ArrayList<String> gridCellNames = new ArrayList<String>();
	
	// Get gridCellNames
	public ArrayList<String> getGirdCellNames() {
		return gridCellNames;
	}
	
	// ABC
	private ArrayList<String> alphabet = new ArrayList<String>(Arrays.asList("abcdefghijklmnopqrstuvwxyz".split("")));
	
	// Essential parameters
	private int gridLength;
	private int gridSize;
	
	// Set grid length and size
	public void setGridLengthAndSize(int length) {
		if (length <= alphabet.size()) {
			gridLength = length;
			gridSize = gridLength * gridLength;
		} else {
			System.out.println("Grid length is too big.");
		}
	}
	
	// Create grid cells
	public void setGridCells() {
		for (int cellNumber = 1; cellNumber <= gridSize; cellNumber++) {
			gridCells.add(new Integer(cellNumber));
		}	
	}
	
	// Create grid cells with names
	public void setGirdCellNames() {
		for (int rowNumber = 1; rowNumber <= gridLength; rowNumber++) {
			for (int columnNumber = 1; columnNumber <= gridLength; columnNumber++) {
				gridCellNames.add(alphabet.get((rowNumber - 1)).toUpperCase() + columnNumber);
			}
		}
	}
	
	// Create containers for coms
	private ArrayList<ArrayList<String>> coms = new ArrayList<ArrayList<String>>(); 
	private ArrayList<String> com0 = new ArrayList<String>();
	private ArrayList<String> com1 = new ArrayList<String>();
	private ArrayList<String> com2 = new ArrayList<String>();
	
	public ArrayList<ArrayList<String>> generateComs() {
		// Add com0, com1, and com2 to coms
		coms.add(com0);
		coms.add(com1);
		coms.add(com2);
		
		// Place coms on grid
		for (int comNumber = 0; comNumber < 3; comNumber++) {
			int horizontalOrVertical = (int) (Math.random() * 2); // Horizontal = 0; Vertical = 1
			
			boolean isGenerated = false; // Until true, keep trying to place a com randomly
			
			if (horizontalOrVertical == 0) {
				while (isGenerated == false) {
					int randomCellIndex = (int) (Math.random() * 48); // Gerenate random first cell of com
					
					// Get randomCell letter
					String[] randomCellName = gridCellNames.get(randomCellIndex).split("");
					int letterNumber = alphabet.indexOf(randomCellName[0].toLowerCase()) + 1;
					
					// Check if com's cells would be placed in different rows
					if (letterNumber * gridLength != randomCellIndex + 1 && letterNumber * gridLength - 1 != randomCellIndex + 1 && letterNumber * gridLength - 2 != randomCellIndex + 1) {
						// Check if the com is placable or the cells are already reserved 
						if ((gridCells.get(randomCellIndex) == -1 && gridCells.get(randomCellIndex + 1) != -1 && gridCells.get(randomCellIndex + 2) != -1)
								|| (gridCells.get(randomCellIndex) == -1 && gridCells.get(randomCellIndex + 1) == -1 && gridCells.get(randomCellIndex + 2) != -1)
								|| (gridCells.get(randomCellIndex) == -1 && gridCells.get(randomCellIndex + 1) == -1 && gridCells.get(randomCellIndex + 2) == -1)
								|| (gridCells.get(randomCellIndex) != -1 && gridCells.get(randomCellIndex + 1) == -1 && gridCells.get(randomCellIndex + 2) != -1)
								|| (gridCells.get(randomCellIndex) != -1 && gridCells.get(randomCellIndex + 1) != -1 && gridCells.get(randomCellIndex + 2) == -1)) {
						} else {
							// Place com and make cells reserved
							for (int i = 0; i < 3; i++) {
								coms.get(comNumber).add(gridCellNames.get(randomCellIndex + i));
								gridCells.set(randomCellIndex + i, -1);
							}
							
							break;
						}
					}
				}
			} else if (horizontalOrVertical == 1) {
				while (isGenerated == false) {
					int randomCellIndex = (int) (Math.random() * 48); // Gerenate random first cell of com
					
					// Check if com's cells would be placed in different columns
					if (gridSize - gridLength * 2 >= randomCellIndex + 1) {
						// Check if the com is placable or the cells are already reserved 
						if ((gridCells.get(randomCellIndex) == -1 && gridCells.get(randomCellIndex + gridLength) != -1 && gridCells.get(randomCellIndex + gridLength * 2) != -1)
								|| (gridCells.get(randomCellIndex) == -1 && gridCells.get(randomCellIndex + gridLength) == -1 && gridCells.get(randomCellIndex + gridLength * 2) != -1)
								|| (gridCells.get(randomCellIndex) == -1 && gridCells.get(randomCellIndex + gridLength) == -1 && gridCells.get(randomCellIndex + gridLength * 2) == -1)
								|| (gridCells.get(randomCellIndex) != -1 && gridCells.get(randomCellIndex + gridLength) == -1 && gridCells.get(randomCellIndex + gridLength * 2) != -1)
								|| (gridCells.get(randomCellIndex) != -1 && gridCells.get(randomCellIndex + gridLength) != -1 && gridCells.get(randomCellIndex + gridLength * 2) == -1)) {
						} else {
							// Place com and make cells reserved
							for (int i = 0, verticalIndex = 0; i < 3; i++) {
								coms.get(comNumber).add(gridCellNames.get(randomCellIndex + verticalIndex));
								gridCells.set(randomCellIndex + verticalIndex, -1);
								verticalIndex = verticalIndex + gridLength;
							}
							
							break;
						}
					}	
				}
			}
		}
		
		return coms; // Return coms
	}
}

// javac "C:\Users\Zoltán\Google Drive\Works & Learning\Development\Java\DotCom\DotComMain.java"
// java -cp "C:\Users\Zoltán\Google Drive\Works & Learning\Development\Java\DotCom" DotComMain

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).