chessai
college code for ai playing chess in java
git clone https://9o.is/git/chessai.git
commit b4a8f820e71cb4dd2f4726f40c4ba21cdc4c5364 parent 472514cbd71424caffa2da001002256eefb74926 Author: Jul <jul@9o.is> Date: Mon, 3 Dec 2012 03:05:32 -0500 Added chess clock. Diffstat:
| A | src/chess/ChessClock.java | | | 23 | +++++++++++++++++++++++ |
| M | src/chess/ChessGame.java | | | 9 | +++++++++ |
2 files changed, 32 insertions(+), 0 deletions(-)
diff --git a/src/chess/ChessClock.java b/src/chess/ChessClock.java @@ -0,0 +1,23 @@ +package chess; + +/** + * + */ +public class ChessClock { + + private long start; + private long milliseconds; + + public ChessClock(int seconds) { + this.milliseconds = seconds * 1000; + this.start = System.currentTimeMillis(); + } + + public int timeRemaining() { + final long current = System.currentTimeMillis(); + final long running = current - start; + + final long remaining = milliseconds - running; + return (int) (remaining / 1000); + } +} diff --git a/src/chess/ChessGame.java b/src/chess/ChessGame.java @@ -49,5 +49,14 @@ public class ChessGame { System.out.println("Moves: "+player.getMoves()+ "\n"+board.printState()); + ChessClock clock = new ChessClock(60); // 60 seconds + try { + Thread.sleep((long)(5 * 1000)); // wait 5 seconds + } catch (InterruptedException e) { + + } + // 55 seconds + System.out.println(clock.timeRemaining()+" seconds remaining"); + } }