Jan
23
2009
The next in my series of teach-myself-java programs is a brute force Sudoku solver. You give it a file (or a list of files) containing 9×9 sudoku grids (empty cells are represented with “-” or “0″), and it gives you the solution:
C:\Home\working\java\Sudoku>type problem1.txt
----6---9
-6----3-1
2----98--
196--2--8
---3-8---
8--9--175
--17----4
6-7----8-
4---9----
C:\Home\working\java\Sudoku>java -jar BruteForceSolver.jar problem1.txt
318267549
769854321
245139867
196572438
574318296
832946175
921783654
657421983
483695712
Downloads:
bruteforcesolver.zip – source files.
bruteforcesolver.jar – java archive.
no comments | tags: learning java, programming, sudoku | posted in Java
Jan
13
2009
I’ve just finished reading the section on Basic I/O on the Java essential classes trail. So I put together a little Guess-The-Number game.
import java.io.*;
import java.lang.Math.*;
public class NumberGame {
private Console c;
private int upperBound;
private int lowerBound;
public NumberGame() {
c = System.console();
if (c == null) {
System.err.println("Unable to aquire console. Exiting...");
System.exit(1);
}
}
private void playGame() {
c.format("Welcome to the guess-the-number game!%n");
c.format("Think of a number between 1 and 100 and I'll try to " +
"guess it!%n");
upperBound = 100;
lowerBound = 1;
boolean finished;
do {
finished = makeGuess();
} while (finished == false);
c.format("Thank you for playing!%n");
}
private boolean makeGuess() {
if (upperBound == lowerBound) {
makeAccusation(upperBound);
return true;
}
else {
int choice = ((upperBound - lowerBound) / 2) + lowerBound;
if (Math.random() < 0.5) {
boolean answer = askYesOrNo(String.format("%nIs your " +
"number higher than %d? ", choice));
if (answer) {
lowerBound = choice + 1;
}
else {
upperBound = choice;
}
}
else {
boolean answer = askYesOrNo(String.format("%nIs your " +
"number lower than %d? ", choice));
if (answer) {
upperBound = choice;
}
else {
lowerBound = choice - 1;
}
}
}
return false;
}
private boolean askYesOrNo(String query) {
int answer = -1;
String result;
char resultChar = ' ';
while (true) {
result = c.readLine(query);
if (result != "") {
result = result.toLowerCase();
if (result.length() > 0) {
resultChar = result.charAt(0);
if (resultChar == 'y') {
return true;
}
else if (resultChar == 'n') {
return false;
}
}
c.format("Please answer 'yes' or 'no'.%n");
}
}
}
private void makeAccusation(int accusation) {
boolean result = askYesOrNo(String.format("%nIs your number %d? ",
accusation));
if (result) {
c.format("Awesome!%n");
}
else {
c.format("One of us is obviously confused.%n");
}
}
public static void main(String[] args) {
NumberGame ng = new NumberGame();
ng.playGame();
}
}
no comments | tags: learning java, programming, Software | posted in Java, Software
Jan
8
2009
I’m planning to do a masters degree in computing in the next year or two. Most of the courses I’ve been looking at are built around Java, so I thought it would be a good idea to learn a bit now when I can relax and take my time. So I’ve been working through the tutorial over the last few days. I’m done with the basics and will shortly be moving on to those essential java classes. So I’m at the point where I can write some little programs to test that I actually do know what’s going on. This fancy book learning is all very well, but I like to get my hands dirty with whatever I’m trying to understand, so I’ll be trying out my knowledge as I go along.
This first program is pretty damn simple. But it’s the first program I’ve ever written in Java, so I’m kind of proud of it. It’s an implementation of the rot13 cipher.:
public class Rot13 {
static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
private static char rotateChar(char c) {
if (Character.isLetter(c)) {
int idx = ALPHABET.indexOf(Character.toLowerCase(c));
idx += 13;
if (idx >= 26) {
idx -= 26;
}
char rotatedChar = ALPHABET.charAt(idx);
if (Character.isUpperCase(c)) {
return Character.toUpperCase(rotatedChar);
}
else {
return rotatedChar;
}
}
else {
return c;
}
}
public static String rotate(String source) {
char[] resultArray = new char[source.length()];
for (int i = 0; i < source.length(); i++) {
resultArray[i] = rotateChar(source.charAt(i));
}
return new String(resultArray);
}
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println(rotate(args[i]));
}
}
}
This stuff is easier in Python, where I’d write something like:
import string, sys
transtable = string.maketrans(string.ascii_letters,
string.ascii_lowercase[13:] +
string.ascii_lowercase[:13] +
string.ascii_uppercase[13:] +
string.ascii_uppercase[:13])
for arg in sys.argv[1:]:
print string.translate(arg, transtable)
Actually, I could write it on one line using less than 200 characters. Not that this is a good idea, but...
import string as s;import sys;a=s.ascii_lowercase;
b=s.ascii_uppercase;[sys.stdout.write(s.translate(
arg,s.maketrans(s.ascii_letters,a[13:]+a[:13]+
b[13:]+b[:13]))+"\n") for arg in sys.argv[1:]]
So yeah, Java does seems a bit verbose at the moment.
no comments | tags: learning java, programming, Software | posted in Java, Software