Monday, March 16, 2015

Pig Latin Translator v1.0

Finally got the pig latin translator working properly (mostly). It accounts for special cases like qu, ph, ch, and sh. However, I have not yet implemented the ability to search for vowels past the first two characters. This requires a lot of extra booleans and reformatting my entire code. Here's the current code, nonetheless:

import javax.swing.JOptionPane;

public class NoPleaseStop {

 public static void main(String[] args){
  String user_input = JOptionPane.showInputDialog("Enter a word to 
translate:").toLowerCase();
  String[] vowels = {"a", "e", "i", "o", "u"}; //array of vowels
  String result = ""; //creates empty string for final translation
  boolean check = false; //Used for vowel/consonant rules
  
        if (isAlpha(user_input) == false) { //A little code Brian 
showed me, checks for proper notation
            JOptionPane.showMessageDialog(null, "No extra symbols 
or sentences, please. Just one word.", "USER ERROR", JOptionPane
.INFORMATION_MESSAGE); //Explains user error
        } else if (user_input.startsWith("qu")) { //Here's some 
extra rules
         result = user_input.substring(2) + "quay";
         JOptionPane.showMessageDialog(null, result, "Translation",
 JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("ph")) {
         result = user_input.substring(2) + "phay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("sh")) {
         result = user_input.substring(2) + "shay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else if (user_input.startsWith("ch")) {
         result = user_input.substring(2) + "chay";
         JOptionPane.showMessageDialog(null, result, "Translation"
JOptionPane.INFORMATION_MESSAGE); //Prints the translation
        } else {
          for (int i = 0; i < vowels.length; i++) {
           if (user_input.startsWith(vowels[i])) {
            check = true;
           }
          }
          if (check) {
           result = user_input + "yay"; //vowel rule
          } else {
           result = user_input.substring(1) + user_input.charAt(0)
 + "ay"; //consonant rule
          }
          JOptionPane.showMessageDialog(null, result, "Translation",
 JOptionPane.INFORMATION_MESSAGE); //Prints the translation
      }
 }
 
    public static boolean isAlpha(String user_input) {  
//The other bit of the code Brian showed me
        char[] chars = user_input.toCharArray(); 
//It checks for correct characters!
        for (char c : chars) {
            if(!Character.isLetter(c)) {
                return false;
            }
        }
        return true; 
    }
    
}
 
Sorry for the poor formatting, Blogger doesn't like code. Here's some example inputs/outputs:







Wednesday, March 4, 2015

Basic Battleship v1.1

I edited the code of my basic Battleship program to include option panels as opposed to writing directly in the console. The main things that were added were:

import javax.swing.JOptionPane; //Function used to create panel

/*
 * Pretend the rest of the code is here, nothing was changed
 */

String guess = JOptionPane.showInputDialog("Enter your guess here (0-7): ");  
//Creates a window where the user can input their guess

/*
 * Rest of the code goes here
 */

The remaining code from v1.0 was unchanged and worked smoothly. I want to add a window to display whether or not the user's guess is a hit or a miss, but that goes more into JFrames and JPanels and UI that I'm assuming gets covered more in Chapter 6. I just wanted to make sure I understood JOptionPane for this activity.

Monday, March 2, 2015

Tim and Ché's Fantabulous Age-Checker Machine-omatic 2000

We made some code that checks your age and sees if you're 18 or older.

Ché put the code on his blog, you can find it here.

Chapter 5's Basic Battleship Game

This chapter focused on making a Battleship-style game, but only in one dimension. The code in the chapter was left somewhat unfinished - they used a fake "GameHelper" object in place of real user input - but I fixed it and used a Scanner object to detect user input. Here's the code:

import java.util.Scanner;

public class SimpleDotCom {

  int[] locationCells;
 int numOfHits = 0;
 
 public void setLocationCells(int[] locs) {
  locationCells = locs;
 }
 
 public String checkYourself(String stringGuess) {
  int guess = Integer.parseInt(stringGuess);
  String result = "miss";
  
  for (int cell : locationCells) {
   if (guess == cell) {
    result = "hit";
    numOfHits++;
    break;
   }
  }
  
  if (numOfHits == locationCells.length) {
   result = "kill";
  }
  
  System.out.println(result);
   return result;
 }
 
 public static void main(String[] args) {
  int numOfGuesses = 0;
  Scanner user_input = new Scanner(System.in); //Switched "GameHelper" to Scanner function
  
  SimpleDotCom theDotCom = new SimpleDotCom();
  int randomNum = (int) (Math.random() *5);
  
  int[] locations = {randomNum, randomNum+1, randomNum+2};
  theDotCom.setLocationCells(locations);
  boolean isAlive = true;
  
  while(isAlive == true) {
   System.out.println("Enter a number:");
   String guess = user_input.nextLine(); //Used scanner functionality for user input
   String result = theDotCom.checkYourself(guess);
   numOfGuesses++;
   if (result.equals("kill")) {
    isAlive = false;
    System.out.println("You took " + numOfGuesses + " guesses!");
   }
  }
  user_input.close();
 }
}

And here's an example of a game in action:

Enter a number:
5
miss
Enter a number:
4
miss
Enter a number:
7
miss
Enter a number:
8
miss
Enter a number:
6
miss
Enter a number:
1
hit
Enter a number:
2
hit
Enter a number:
3
kill
You took 8 guesses!