From bb377e19b2bfca097b6fa119bcc46bade727cf43 Mon Sep 17 00:00:00 2001 From: Kamil Kaminski Date: Mon, 14 Mar 2011 11:23:26 -0500 Subject: support 1 by x pieces --- klotski.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/klotski.cpp b/klotski.cpp index a04eee2..20b3a14 100644 --- a/klotski.cpp +++ b/klotski.cpp @@ -253,6 +253,7 @@ Grid loadGrid(char **argv) int canMoveLeft(string board[], int row_sz, int col_sz, Piece piece) { int ret = 0; + int i; int offset = ( ((piece.getRow() - 1 ) * col_sz) + piece.getCol() ) - 1; if (piece.getH() == 1) @@ -273,7 +274,11 @@ int canMoveLeft(string board[], int row_sz, int col_sz, Piece piece) } else if (piece.getW() == 1) { - int offset2 = offset + col_sz; /* fix me later */ + int *offset2 = new int[piece.getH()-1]; + + for (i = 0; i < piece.getH(); i++) + offset2[i] = (offset + (col_sz * i)); + //int offset2 = offset + col_sz; /* fix me later */ if (!offset || (offset % col_sz == 0)) return 0; @@ -287,16 +292,25 @@ int canMoveLeft(string board[], int row_sz, int col_sz, Piece piece) } #endif - while (board[offset-1] == "." && board[offset2-1] == ".") + while (board[offset-1] == ".") { + for (i = 0; i < piece.getH(); i++) + { + if (board[offset2[i]-1] != ".") + goto end_tag; + } + offset--; - offset2--; + for (i = 0; i < piece.getH(); i++) + offset2[i] -= 1; + ret++; if (offset % col_sz == 0) break; } +end_tag: return ret; } else @@ -309,6 +323,7 @@ int canMoveLeft(string board[], int row_sz, int col_sz, Piece piece) int canMoveRight(string board[], int row_sz, int col_sz, Piece piece) { int ret = 0; + int i; int offset = ( ((piece.getRow() - 1 ) * col_sz) + piece.getCol() ) - 1; if (piece.getH() == 1) @@ -335,21 +350,34 @@ int canMoveRight(string board[], int row_sz, int col_sz, Piece piece) offset += piece.getW(); offset--; - int offset2 = offset + col_sz; /* fix me later */ + int *offset2 = new int[piece.getH()-1]; /* FIXME: memory leaks */ + + for (i = 0; i < piece.getH(); i++) + offset2[i] = (offset + (col_sz * i)); + //int offset2 = offset + col_sz; /* fix me later */ if (!offset || ((offset+1) % col_sz == 0)) return 0; - while (board[offset+1] == "." && board[offset2+1] == ".") + while (board[offset+1] == ".") { + for (i = 0; i < piece.getH(); i++) + { + if (board[offset2[i]-1] != ".") + goto end2_tag; + } + offset++; - offset2++; + for (i = 0; i < piece.getH(); i++) + offset2[i] += 1; + ret++; if (offset % col_sz == 0) break; } +end2_tag: return ret; } else @@ -362,6 +390,7 @@ int canMoveRight(string board[], int row_sz, int col_sz, Piece piece) int canMoveUp(string board[], int row_sz, int col_sz, Piece piece) { int ret = 0; + int i; int offset = ( ((piece.getRow() - 1 ) * col_sz) + piece.getCol() ) - 1; if (piece.getW() == 1) @@ -382,21 +411,34 @@ int canMoveUp(string board[], int row_sz, int col_sz, Piece piece) } else if (piece.getH() == 1) { - int offset2 = offset + piece.getW() - 1; /* fix me later */ + int *offset2 = new int[piece.getW()-1]; /* FIXME: memory leaks */ + + for (i = 0; i < piece.getW(); i++) + offset2[i] = offset + i; + //int offset2 = offset + piece.getW() - 1; /* fix me later */ if (!offset || offset < col_sz) return 0; - while (board[offset-col_sz] == "." && board[offset2-col_sz] == ".") + while (board[offset-col_sz] == ".") { + for (i = 0; i < piece.getW(); i++) + { + if (board[offset2[i]-col_sz] != ".") + goto end3_tag; + } + offset -= col_sz; - offset2 -= col_sz; + for (i = 0; i < piece.getH(); i++) + offset2[i] -= col_sz; + ret++; if (offset-col_sz < 0) break; } +end3_tag: return ret; } else @@ -409,6 +451,7 @@ int canMoveUp(string board[], int row_sz, int col_sz, Piece piece) int canMoveDown(string board[], int row_sz, int col_sz, Piece piece) { int ret = 0; + int i; int offset = ( ((piece.getRow() - 1 ) * col_sz) + piece.getCol() ) - 1; if (piece.getW() == 1) @@ -432,21 +475,34 @@ int canMoveDown(string board[], int row_sz, int col_sz, Piece piece) } else if (piece.getH() == 1) { - int offset2 = offset + piece.getW() - 1; /* fix me later */ + int *offset2 = new int[piece.getW()-1]; /* FIXME: memory leaks */ + + for (i = 0; i < piece.getW(); i++) + offset2[i] = offset + i; + //int offset2 = offset + piece.getW() - 1; /* fix me later */ if (offset + col_sz > row_sz * col_sz) return 0; - while (board[offset+col_sz] == "." && board[offset2+col_sz] == ".") + while (board[offset+col_sz] == ".") { + for (i = 0; i < piece.getW(); i++) + { + if (board[offset2[i]+col_sz] != ".") + goto end4_tag; + } + offset += col_sz; - offset2 += col_sz; + for (i = 0; i < piece.getH(); i++) + offset2[i] += col_sz; + ret++; if (offset+col_sz > row_sz * col_sz) break; } +end4_tag: return ret; } else -- cgit v1.2.3