Javascript to Copy to clipboard

Apparently you can use Javascript to copy to the system clipboard. Don’t know if this works on non-Windows systems, but certainly, Internet Explorer seems to support it.

I’m sure that there must be a security risk here – but I haven’t really thought about it yet. And certainly, it appears that Mozilla agree that there is a security issue as it works another way, as shown at Krikkit.net. In short, you have to sign your code or drop your security level…

Javascript to Copy to clipboard

JDBC and database access

Slow day today, so I finally (after 2 years of saying I’d give it a go) got around to trying to use JDBC.

I wrote a simple class to query a mySql database. It worked like a charm! Now all I need to learn is good design practice (which could, I fear, include Beans). I’m not entirely sure how the object model matches up to the database structure – I guess that this is what I need to learn…
import java.sql.*;
import java.util.Properties;

public class JDBCTest {
public static void main( String args[] ){
Connection conn= null;
try{
String cURL = "jdbc:mysql://localhost/jtest";
Properties p = new Properties();

p.put("user","root");
p.put("password","voodoo");

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection( cURL, p );

Statement s = conn.createStatement();
ResultSet r = s.executeQuery( "SELECT * FROM jtable WHERE iID = 1" );
r.first();
System.out.println ( r.getString("cFEN"));
}
catch ( Exception e ) {
e.printStackTrace();
}
finally {
if( conn != null ) {
try { conn.close(); }
catch ( Exception e ) {}
}
}
}
}

JDBC and database access

Chinese Chess Engine Again

Work continues…

Gaze of Death – Kings may not face each other directly without an intervening piece

and I have this thing through to the point that it will read a FEN (a notation describing a board state, like
4KAr2/4A4/3Hr4/7h1/9/9/9/9/4aP1R1/3ak3C w---1
), create a board recording that, work out the valid moves for each piece (that is, the squares that that piece can move to), then work out what moves are ‘allowed’ – that is, the move doesn’t result in check, or causing the Gaze of Death.

I’ve used bit boards for most of it, but I haven’t precomputed them, as has been the case for some chess engines. The reason, mainly, was being unsure how to cope with ‘Blocking’ rules – that is, some pieces can be blocked by a piece up close to them. For example, Knights can’t jump – they move 1 square straight, and another diagonally. If there is a piece on the straight square, they can’t move that way.

I couldn’t figure out how to precompute this, though now I wonder if a second bit board with ‘blocked’ squares on it would have sufficed. Still, the precomputation might have been a bit much – 90 squares, times 8 for the number of ways it could be blocked is 720 combinations, plus 90 for the blocked squares masks.

Actually, it might be worth a look. Anyway, speed is not a huge issue for this right now – it’s only point is to determine valid moves, and I intend to cache the result in a database, so that valid moves can be referred to by FEN…

Chinese Chess Engine Again

Chinese Chess Engine

Okay, so few people in the West have heard of Chinese Chess, or Xiangqi, but trust me, it’s pretty cool. It doesn’t have as many Pawns, and there is a piece called the Canon that can only take if it jumps over another piece on the way.

I’m wondering about making a chinese chess website, to play online. It’s a bit trickier – chinese chess has a 9×10 board (not 8×8), so bit boards will be more complex.

But I might give it a go, building an engine. I think I have the guts of it worked out, although I’m concerned about efficiency…

Chinese Chess Engine

Computer Science isn't Programming

I’ve often thought this, and it’s interesting to see that Joel Spolsky agrees.

Computer Science is the science of, well, computation. It’s a question of what is possible with computers, is more like a branch of Mathematics, and much of it can be done without computers. I mean, a lot of the fundamentals had been figured out before the development of the transistor.

Software writing, well, that’s an Engineering skill. It’s got a practical end. I guess it is the difference between someone proving by thought that bridges can cross rivers, and someone actually building a bridge to cross a river this wide, strong enough for that type of cart, etc..

And at the moment, nobody really teaches Software Engineering. Which is a shame. But at least it explains how I came to software writing from a Cybernetics background without suffering much of a disadvantage. Just like Computer Science, we’d picked up programming as just one of our ‘tools’ during our studies…

Computer Science isn't Programming

Capturing Comments in HTML

A friend I work with was asking me today how to match HTML comments using regular expressions. It was an interesting example of some of the pitfalls and design that needs to go into regular expression code.

HTML comments are marked out by <!– and –>, for those who don’t know. For our examples we’ll use the code below, and change the patterns and subjects defined by $pattern and $subject respectively. This is written in PHP, but the same patterns are true for any Perl Compatible regular expression. Continue reading “Capturing Comments in HTML”

Capturing Comments in HTML