diff --git a/docs/index.html b/docs/index.html index caffc0f..5d75439 100644 --- a/docs/index.html +++ b/docs/index.html @@ -60,8 +60,8 @@ } } -function _log(ptr, len) { - console.log(toJsString(ptr, len)); +function reportBenchmark(number, time) { + console.log("Evaluated "+number+" moves in "+time+" ms"); } function getTimeMillis() { @@ -118,7 +118,8 @@ drawCircle, setAlpha, setFont, - getTimeMillis + getTimeMillis, + reportBenchmark } }; diff --git a/docs/tictac.wasm b/docs/tictac.wasm index e30042f..819c28a 100644 Binary files a/docs/tictac.wasm and b/docs/tictac.wasm differ diff --git a/source/app.d b/source/app.d index fc14749..02190c9 100644 --- a/source/app.d +++ b/source/app.d @@ -51,10 +51,16 @@ extern(C): } } - void _log(immutable(char)* ptr, size_t len); - - void consoleLog(string str) { + int getTimeMillis() @nogc; + void reportBenchmark(int number, int msecs) @nogc; + __gshared int benchmarkStartTime = 0; + void benchmarkStart() @nogc { + benchmarkStartTime = getTimeMillis(); + } + + void benchmarkEnd(int number) @nogc { + reportBenchmark(number, getTimeMillis()-benchmarkStartTime); } // These simple C standard library implementations are needed because @@ -144,4 +150,23 @@ extern(C): return result; } + + // Benchmark stuff + enum bool benchmarkShown = false; + long benchmarkMsecs = 0; + long benchmarkMoves = 0; + + import std.datetime.stopwatch: StopWatch, AutoStart; + private StopWatch stopWatch = StopWatch(AutoStart.no); + + void benchmarkStart() @nogc { + stopWatch.reset(); + stopWatch.start(); + } + + void benchmarkEnd(int movesAmount) @nogc { + stopWatch.stop(); + benchmarkMsecs = stopWatch.peek.total!"msecs"; + benchmarkMoves = movesAmount; + } } diff --git a/source/draw.d b/source/draw.d index 2cb271c..dced238 100644 --- a/source/draw.d +++ b/source/draw.d @@ -169,7 +169,11 @@ void drawMessage(in ref GameState state) { auto str = messageStrings[state.message]; int i = clamp(state.sinceLastMessage * 4, 0, cast(int) str.length); - window.write(1, 23, bgField, fgField, str[0..i]); //, str[0..$] + window.write(1, 23, bgField, fgField, str[0..i]); + + import app: benchmarkMoves, benchmarkMsecs, benchmarkShown; + if (benchmarkShown) + window.write(1, 24, bgField, fgField, "Evaluated ", benchmarkMoves, " moves in ", benchmarkMsecs, "ms"); } void drawTitleScreen(in ref GameState) { diff --git a/source/game.d b/source/game.d index 232b3d7..7dce71c 100644 --- a/source/game.d +++ b/source/game.d @@ -202,7 +202,7 @@ void updateGame(ref GameState state, Input input) { } break; case Mode.scramble: - if (globalTimer & 1) state.field.randomizeField(); + if ((globalTimer % 4) == 0) state.field.randomizeField(); if (input.enter) { field.getValidMoves(); @@ -358,9 +358,11 @@ void doMove(ref Field field, int x, int y) { Who winner = field.checkGameWin(); if (winner != Who.noone) { field.gameWon = winner; + field.validMoves = 0; + } else { + field.getValidMoves(); } - field.getValidMoves(); } // Cap amount of moves to prevent absurd waiting times for A.I. @@ -373,7 +375,13 @@ enum maxMovesConsidered = 3_000_000; void cpuDoMove(ref GameState state) { int x, y; totalMovesConsidered = 0; + + import app: benchmarkStart, benchmarkEnd; + + benchmarkStart(); bestMoveScore(state.field, state.cpuRecursionLevel, x, y); + benchmarkEnd(totalMovesConsidered); + state.field.doMove(x, y); state.updateAfterMove(); } diff --git a/source/jsdraw.d b/source/jsdraw.d index fbe0cca..82a5961 100644 --- a/source/jsdraw.d +++ b/source/jsdraw.d @@ -138,7 +138,7 @@ void drawField(in ref Field field, in ref WindowContext context, double anim = 1 drawLine(x, y+offset, x+edge, y+offset, innerThickness, fgColor); } - // Content and valid moves + // Content drawSetFont("30px Arial"); foreach(i; 0..9) foreach(j; 0..9) { auto str = field.fieldContent[i][j].toSymbol; @@ -148,18 +148,21 @@ void drawField(in ref Field field, in ref WindowContext context, double anim = 1 str.ptr, str.length, fgColor, ); + } + // Valid moves + if (field.gameWon == Who.noone) foreach(i; 0..9) foreach(j; 0..9) { if (field.validMove[i][j] == Validity.allowed) { // "Blink animation", going quickly to alpha 0.75 and slowly back to 0.25 if (anim > 0.5) setAlpha(1.25-anim); else if (anim > 0.25) setAlpha((anim-0.25)*3); else setAlpha(0); - + drawRect( x + i*cellEdge, y + j *cellEdge, cellEdge, cellEdge, allowedCellColor); - + setAlpha(1); } }