From d41ebfae814d6a687ba213a8da97c8c675b4a275 Mon Sep 17 00:00:00 2001 From: Henry Bush Date: Sat, 22 Nov 2008 23:07:05 +0000 Subject: T7G Microscope: Stauf now makes legal moves (though not good ones) svn-id: r35154 --- engines/groovie/cell.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++--- engines/groovie/cell.h | 14 ++++++++ engines/groovie/script.cpp | 15 ++++---- 3 files changed, 107 insertions(+), 11 deletions(-) (limited to 'engines/groovie') diff --git a/engines/groovie/cell.cpp b/engines/groovie/cell.cpp index 5ec060c2d9..0babbce40c 100644 --- a/engines/groovie/cell.cpp +++ b/engines/groovie/cell.cpp @@ -30,23 +30,102 @@ namespace Groovie { CellGame::CellGame(byte *board) : _board(board) { - //printf ("*** In cellgame constructor ***"); + _startX = _startY = _endX = _endY = 255; +} + +int8 CellGame::calcMove(byte *origboard, uint8 color, uint8 depth) { + uint8 i, j; + int8 di, dj; + byte *newboard; + uint8 boardmemsize = sizeof(byte) * BOARDSIZE * BOARDSIZE; + int8 maxdiff = -100; + + newboard = (byte*) malloc(boardmemsize); + memcpy(newboard, origboard, boardmemsize); + + if (0 == depth) { + return 0; + } + + for (i = 0; BOARDSIZE > i; i++) { + for (j = 0; BOARDSIZE > j; j++) { // For every square on the board + if (color == *(origboard + i + (BOARDSIZE * j))) { // If the square is the desired colour + for (di = -2; 2 >= di; di++) { + for (dj = -2; 2 >= dj; dj++) { + if (di != 0 || dj != 0) { // Don't allow a move onto itself + if (validMove(origboard, color, i+di, j+dj)) { + _startX = i; + _startY = j; + _endX = i+di; + _endY = j+dj; + } + } + } + } + } + } + } + + free(newboard); + return maxdiff; +} + +bool CellGame::validMove(byte *board, uint8 color, int8 endX, int8 endY) { + if (0 > endX || 0 > endY || BOARDSIZE <= endX || BOARDSIZE <= endY) { // Move is out of bounds + return false; + } + if (0 == *(board + endX + (BOARDSIZE * endY))) { + return true; + } + return false; +} + +uint8 CellGame::countBoard(byte *board, uint8 color) { + uint8 total = 0; + for (uint8 i = 0; BOARDSIZE > i; i++) { + for (uint8 j = 0; BOARDSIZE > j; j++) { + if (color == *(board + i + (BOARDSIZE * j))) { + total++; + } + } + } + return total; } byte CellGame::getStartX() { - return 0; // TODO: implement something here + if (_startX > BOARDSIZE) { + warning ("CellGame::getStartX: not calculated yet!"); + return 0; + } else { + return _startX; + } } byte CellGame::getStartY() { - return 6; // TODO: implement something here + if (_startY > BOARDSIZE) { + warning ("CellGame::getStartY: not calculated yet!"); + return 6; + } else { + return _startY; + } } byte CellGame::getEndX() { - return 1; // TODO: implement something here + if (_endX > BOARDSIZE) { + warning ("CellGame::getEndX: not calculated yet!"); + return 1; + } else { + return _endX; + } } byte CellGame::getEndY() { - return 6; // TODO: implement something here + if (_endY > BOARDSIZE) { + warning ("CellGame::getEndY: not calculated yet!"); + return 6; + } else { + return _endY; + } } CellGame::~CellGame() { diff --git a/engines/groovie/cell.h b/engines/groovie/cell.h index 862500938d..14e44511e3 100644 --- a/engines/groovie/cell.h +++ b/engines/groovie/cell.h @@ -27,6 +27,12 @@ #define GROOVIE_CELL_H #include "common/file.h" +#include "common/util.h" + +#define BOARDSIZE 7 +#define CELL_CLEAR 0 +#define CELL_BLUE 1 +#define CELL_GREEN 2 namespace Groovie { @@ -37,13 +43,21 @@ class CellGame { public: CellGame(byte *board); ~CellGame(); + int8 calcMove(byte *origboard, uint8 color, uint8 depth); byte getStartX(); byte getStartY(); byte getEndX(); byte getEndY(); private: + bool validMove(byte *board, uint8 color, int8 endX, int8 endY); + uint8 countBoard(byte* board, uint8 color); byte *_board; + + byte _startX; + byte _startY; + byte _endX; + byte _endY; }; } // End of Groovie namespace diff --git a/engines/groovie/script.cpp b/engines/groovie/script.cpp index 894e4d2c60..c1cdd1e7d4 100644 --- a/engines/groovie/script.cpp +++ b/engines/groovie/script.cpp @@ -1315,7 +1315,7 @@ void Script::o_sub() { void Script::o_cellmove() { uint16 arg = readScript8bits(); byte *scriptBoard = &_variables[0x19]; - byte board[7][7]; + byte *board = (byte*) malloc (BOARDSIZE * BOARDSIZE * sizeof(byte)); byte startX, startY, endX, endY; debugScript(1, true, "CELL MOVE var[0x%02X]", arg); @@ -1323,22 +1323,23 @@ void Script::o_cellmove() { // Arguments used by the original implementation: (2, arg, scriptBoard) for (int y = 0; y < 7; y++) { for (int x = 0; x < 7; x++) { - board[x][y] = 0; - if (*scriptBoard == 0x32) board[x][y] = 1; - if (*scriptBoard == 0x42) board[x][y] = 2; + uint8 offset = x + BOARDSIZE * y; + *(board + offset) = 0; + if (*scriptBoard == 0x32) *(board + offset) = CELL_BLUE; + if (*scriptBoard == 0x42) *(board + offset) = CELL_GREEN; scriptBoard++; - debugScript(1, false, "%d", board[x][y]); + debugScript(1, false, "%d", *(board + offset)); } debugScript(1, false, "\n"); } CellGame staufsMove((byte*) board); + staufsMove.calcMove((byte*) board, CELL_GREEN, 2); startX = staufsMove.getStartX(); startY = staufsMove.getStartY(); endX = staufsMove.getEndX(); endY = staufsMove.getEndY(); - //printf("Moving from %d,%d to %d,%d\n", startX, startY, endX, endY); // Set the movement origin setVariable(0, startY); // y @@ -1346,6 +1347,8 @@ void Script::o_cellmove() { // Set the movement destination setVariable(2, endY); setVariable(3, endX); + + free (board); } void Script::o_returnscript() { -- cgit v1.2.3