diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-06-18 19:37:24 -0400 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2009-06-18 19:37:24 -0400 |
| commit | 8c1f08f2133de108fd354dd07c65e0c24ecc1d38 (patch) | |
| tree | d16d476ce82776a99d949234d0b8381223c7cc87 | |
| parent | 7b92903af52dbdf4b0dbfa10dc45345531d88595 (diff) | |
| download | mazeoflife-8c1f08f2133de108fd354dd07c65e0c24ecc1d38.tar.gz mazeoflife-8c1f08f2133de108fd354dd07c65e0c24ecc1d38.tar.bz2 mazeoflife-8c1f08f2133de108fd354dd07c65e0c24ecc1d38.zip | |
Wrote Life solver
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | board.cpp | 48 | ||||
| -rw-r--r-- | board.h | 16 | ||||
| -rw-r--r-- | gamestate.cpp | 180 | ||||
| -rw-r--r-- | gamestate.h | 51 | ||||
| -rw-r--r-- | includes.h | 2 | ||||
| -rw-r--r-- | level.cpp | 57 | ||||
| -rw-r--r-- | level.h | 20 | ||||
| -rw-r--r-- | mazeoflife.cpp | 2 |
9 files changed, 212 insertions, 166 deletions
| diff --git a/Makefile b/Makefile index 2335076..0eb1738 100644 --- a/Makefile +++ b/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | OBJS = mazeoflife.o gamestate.o board.o level.o | 1 | OBJS = mazeoflife.o gamestate.o |
| 2 | CC = g++ | 2 | CC = g++ |
| 3 | CFLAGS = `pkg-config sdl --cflags` | 3 | CFLAGS = `pkg-config sdl --cflags` |
| 4 | LIBS = `pkg-config sdl --libs` | 4 | LIBS = `pkg-config sdl --libs` |
| diff --git a/board.cpp b/board.cpp deleted file mode 100644 index 8af0015..0000000 --- a/board.cpp +++ /dev/null | |||
| @@ -1,48 +0,0 @@ | |||
| 1 | #include "includes.h" | ||
| 2 | |||
| 3 | Board::Board() | ||
| 4 | { | ||
| 5 | Board(Level()); | ||
| 6 | } | ||
| 7 | |||
| 8 | Board::Board(Level level) | ||
| 9 | { | ||
| 10 | int x,y; | ||
| 11 | for (y=0;y<HEIGHT;y++) | ||
| 12 | { | ||
| 13 | for (x=0;x<WIDTH;x++) | ||
| 14 | { | ||
| 15 | if (level.checkSquare(x, y)) | ||
| 16 | { | ||
| 17 | blocks[x][y] = rand() % 2; | ||
| 18 | } else { | ||
| 19 | blocks[x][y] = false; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | bool Board::isObstructed(int x, int y) | ||
| 26 | { | ||
| 27 | return blocks[x][y]; | ||
| 28 | } | ||
| 29 | |||
| 30 | void Board::render(SDL_Surface* screen, Level level) | ||
| 31 | { | ||
| 32 | SDL_Rect block; | ||
| 33 | block.w = 16; | ||
| 34 | block.h = 16; | ||
| 35 | |||
| 36 | int x,y; | ||
| 37 | |||
| 38 | for (y=0;y<HEIGHT;y++) | ||
| 39 | { | ||
| 40 | for (x=0;x<WIDTH;x++) | ||
| 41 | { | ||
| 42 | block.x = x*16; | ||
| 43 | block.y = y*16; | ||
| 44 | |||
| 45 | SDL_FillRect(screen, &block, (blocks[x][y] ? level.getAliveColor() : level.getDeadColor())); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | } | ||
| diff --git a/board.h b/board.h deleted file mode 100644 index fdeefc9..0000000 --- a/board.h +++ /dev/null | |||
| @@ -1,16 +0,0 @@ | |||
| 1 | #ifndef BOARD_H | ||
| 2 | #define BOARD_H | ||
| 3 | |||
| 4 | class Board | ||
| 5 | { | ||
| 6 | private: | ||
| 7 | bool blocks[WIDTH][HEIGHT]; | ||
| 8 | |||
| 9 | public: | ||
| 10 | Board(); | ||
| 11 | Board(Level level); | ||
| 12 | bool isObstructed(int x, int y); | ||
| 13 | void render(SDL_Surface* screen, Level level); | ||
| 14 | }; | ||
| 15 | |||
| 16 | #endif | ||
| diff --git a/gamestate.cpp b/gamestate.cpp index d1c17b2..e5c8d9f 100644 --- a/gamestate.cpp +++ b/gamestate.cpp | |||
| @@ -7,7 +7,7 @@ GameState::GameState() | |||
| 7 | newGame = true; | 7 | newGame = true; |
| 8 | doneMaking = false; | 8 | doneMaking = false; |
| 9 | 9 | ||
| 10 | level = Level(); | 10 | info.level = Level(); |
| 11 | } | 11 | } |
| 12 | 12 | ||
| 13 | void GameState::input(SDLKey key) | 13 | void GameState::input(SDLKey key) |
| @@ -17,19 +17,19 @@ void GameState::input(SDLKey key) | |||
| 17 | switch (key) | 17 | switch (key) |
| 18 | { | 18 | { |
| 19 | case SDLK_LEFT: | 19 | case SDLK_LEFT: |
| 20 | move(playerx-1, playery); | 20 | move(info.playerx-1, info.playery); |
| 21 | 21 | ||
| 22 | break; | 22 | break; |
| 23 | case SDLK_RIGHT: | 23 | case SDLK_RIGHT: |
| 24 | move(playerx+1, playery); | 24 | move(info.playerx+1, info.playery); |
| 25 | 25 | ||
| 26 | break; | 26 | break; |
| 27 | case SDLK_UP: | 27 | case SDLK_UP: |
| 28 | move(playerx, playery-1); | 28 | move(info.playerx, info.playery-1); |
| 29 | 29 | ||
| 30 | break; | 30 | break; |
| 31 | case SDLK_DOWN: | 31 | case SDLK_DOWN: |
| 32 | move(playerx, playery+1); | 32 | move(info.playerx, info.playery+1); |
| 33 | 33 | ||
| 34 | break; | 34 | break; |
| 35 | } | 35 | } |
| @@ -40,12 +40,14 @@ void GameState::tick() | |||
| 40 | { | 40 | { |
| 41 | if (newGame) | 41 | if (newGame) |
| 42 | { | 42 | { |
| 43 | playerx = 1; | 43 | info.playerx = 1; |
| 44 | playery = 1; | 44 | info.playery = 1; |
| 45 | board = Board(level); | 45 | board = Board(&info); |
| 46 | newGame = false; | 46 | newGame = false; |
| 47 | doneMaking = true; | 47 | doneMaking = true; |
| 48 | } | 48 | } |
| 49 | |||
| 50 | board.tick(); | ||
| 49 | } | 51 | } |
| 50 | 52 | ||
| 51 | void GameState::move(int x, int y) | 53 | void GameState::move(int x, int y) |
| @@ -54,19 +56,171 @@ void GameState::move(int x, int y) | |||
| 54 | 56 | ||
| 55 | if (board.isObstructed(x,y)) return; | 57 | if (board.isObstructed(x,y)) return; |
| 56 | 58 | ||
| 57 | playerx = x; | 59 | info.playerx = x; |
| 58 | playery = y; | 60 | info.playery = y; |
| 59 | } | 61 | } |
| 60 | 62 | ||
| 61 | void GameState::render(SDL_Surface* screen) | 63 | void GameState::render(SDL_Surface* screen) |
| 62 | { | 64 | { |
| 63 | board.render(screen, level); | 65 | board.render(screen); |
| 64 | 66 | ||
| 65 | SDL_Rect block; | 67 | SDL_Rect block; |
| 66 | block.x = playerx*16; | 68 | block.x = info.playerx*16; |
| 67 | block.y = playery*16; | 69 | block.y = info.playery*16; |
| 68 | block.w = 16; | 70 | block.w = 16; |
| 69 | block.h = 16; | 71 | block.h = 16; |
| 70 | 72 | ||
| 71 | SDL_FillRect(screen, &block, player_color); | 73 | SDL_FillRect(screen, &block, player_color); |
| 72 | } | 74 | } |
| 75 | |||
| 76 | GameState::Level::Level() | ||
| 77 | { | ||
| 78 | level = 1; | ||
| 79 | |||
| 80 | alive[0] = getColor(0, 0, 0); // Black | ||
| 81 | alive[1] = getColor(255, 0, 0); // Red | ||
| 82 | alive[2] = getColor(0, 255, 0); // Green | ||
| 83 | alive[3] = getColor(85, 85, 85); // Dark Gray | ||
| 84 | alive[4] = getColor(255, 0, 255); // Magenta | ||
| 85 | |||
| 86 | dead[0] = getColor(255, 255, 255); // White | ||
| 87 | dead[1] = getColor(255, 192, 203); // Pink | ||
| 88 | dead[2] = getColor(0, 255, 255); // Cyan | ||
| 89 | dead[3] = getColor(170, 170, 170); // Light Gray | ||
| 90 | dead[4] = getColor(255, 128, 0); // Orange | ||
| 91 | } | ||
| 92 | |||
| 93 | int GameState::Level::getLevel() | ||
| 94 | { | ||
| 95 | return level; | ||
| 96 | } | ||
| 97 | |||
| 98 | int GameState::Level::getLevelGroup() | ||
| 99 | { | ||
| 100 | return (level/10)+1; | ||
| 101 | } | ||
| 102 | |||
| 103 | bool GameState::Level::checkSquare(int x, int y) | ||
| 104 | { | ||
| 105 | switch (getLevelGroup()) | ||
| 106 | { | ||
| 107 | case 1: | ||
| 108 | return ((x>13)&&(x<16)&&(y>13)&&(y<16)); | ||
| 109 | case 2: | ||
| 110 | return ((x>13)&&(x<17)&&(y>13)&&(y<17)); | ||
| 111 | case 3: | ||
| 112 | case 4: | ||
| 113 | return ((x>12)&&(x<18)&&(y>12)&&(y<18)); | ||
| 114 | case 5: | ||
| 115 | case 6: | ||
| 116 | return ((x>11)&&(x<19)&&(y>11)&&(y<19)); | ||
| 117 | default: | ||
| 118 | return true; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | Uint32 GameState::Level::getAliveColor() | ||
| 123 | { | ||
| 124 | return alive[(getLevelGroup()-1)%5]; | ||
| 125 | } | ||
| 126 | |||
| 127 | Uint32 GameState::Level::getDeadColor() | ||
| 128 | { | ||
| 129 | return dead[(getLevelGroup()-1)%5]; | ||
| 130 | } | ||
| 131 | |||
| 132 | GameState::Board::Board() | ||
| 133 | { | ||
| 134 | GameState::Board::Board(new GameState::Info()); | ||
| 135 | } | ||
| 136 | |||
| 137 | GameState::Board::Board(GameState::Info* info) | ||
| 138 | { | ||
| 139 | this->info = info; | ||
| 140 | |||
| 141 | int x,y; | ||
| 142 | for (y=0;y<HEIGHT;y++) | ||
| 143 | { | ||
| 144 | for (x=0;x<WIDTH;x++) | ||
| 145 | { | ||
| 146 | if (info->level.checkSquare(x, y)) | ||
| 147 | { | ||
| 148 | blocks[x][y] = rand() % 2; | ||
| 149 | } else { | ||
| 150 | blocks[x][y] = false; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | bool GameState::Board::isObstructed(int x, int y) | ||
| 157 | { | ||
| 158 | return blocks[x][y]; | ||
| 159 | } | ||
| 160 | |||
| 161 | void GameState::Board::render(SDL_Surface* screen) | ||
| 162 | { | ||
| 163 | SDL_Rect block; | ||
| 164 | block.w = 16; | ||
| 165 | block.h = 16; | ||
| 166 | |||
| 167 | int x,y; | ||
| 168 | |||
| 169 | for (y=0;y<HEIGHT;y++) | ||
| 170 | { | ||
| 171 | for (x=0;x<WIDTH;x++) | ||
| 172 | { | ||
| 173 | block.x = x*16; | ||
| 174 | block.y = y*16; | ||
| 175 | |||
| 176 | SDL_FillRect(screen, &block, (blocks[x][y] ? info->level.getAliveColor() : info->level.getDeadColor())); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | void GameState::Board::tick() | ||
| 182 | { | ||
| 183 | bool temp[WIDTH][HEIGHT]; | ||
| 184 | int x,y; | ||
| 185 | for (x=0;x<WIDTH;x++) | ||
| 186 | { | ||
| 187 | for (y=0;y<HEIGHT;y++) | ||
| 188 | { | ||
| 189 | temp[x][y] = blocks[x][y]; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | for (x=0;x<WIDTH;x++) | ||
| 194 | { | ||
| 195 | for (y=0;y<HEIGHT;y++) | ||
| 196 | { | ||
| 197 | int neighbors = 0; | ||
| 198 | |||
| 199 | if ((x>0)&&(y>0)) incrementIfNeighbor(x-1,y-1,temp,&neighbors); | ||
| 200 | if ((x>0)) incrementIfNeighbor(x-1,y,temp,&neighbors); | ||
| 201 | if ((x>0)&&(y<HEIGHT-1)) incrementIfNeighbor(x-1,y+1,temp,&neighbors); | ||
| 202 | if ((y>0)) incrementIfNeighbor(x,y-1,temp,&neighbors); | ||
| 203 | if ((y<HEIGHT-1)) incrementIfNeighbor(x,y+1,temp,&neighbors); | ||
| 204 | if ((x<WIDTH-1)&&(y>0)) incrementIfNeighbor(x+1,y-1,temp,&neighbors); | ||
| 205 | if ((x<WIDTH-1)) incrementIfNeighbor(x+1,y,temp,&neighbors); | ||
| 206 | if ((x<WIDTH-1)&&(y<HEIGHT-1)) incrementIfNeighbor(x+1,y+1,temp,&neighbors); | ||
| 207 | |||
| 208 | if (temp[x][y]) | ||
| 209 | { | ||
| 210 | blocks[x][y] = ((neighbors >= 1) && (neighbors <= 4)); | ||
| 211 | } else { | ||
| 212 | blocks[x][y] = (neighbors == 3); | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | void GameState::Board::incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick) | ||
| 219 | { | ||
| 220 | wrap(&x, &y); | ||
| 221 | |||
| 222 | if ((blocks[x][y])||((info->playerx==x)&&(info->playery==y))) | ||
| 223 | { | ||
| 224 | ++*tick; | ||
| 225 | } | ||
| 226 | } | ||
| diff --git a/gamestate.h b/gamestate.h index 727c8f1..c36bcee 100644 --- a/gamestate.h +++ b/gamestate.h | |||
| @@ -2,20 +2,55 @@ | |||
| 2 | #define GAMESTATE_H | 2 | #define GAMESTATE_H |
| 3 | 3 | ||
| 4 | class GameState : public State { | 4 | class GameState : public State { |
| 5 | public: | ||
| 6 | GameState(); | ||
| 7 | void input(SDLKey key); | ||
| 8 | void tick(); | ||
| 9 | void render(SDL_Surface* screen); | ||
| 10 | |||
| 11 | class Level | ||
| 12 | { | ||
| 13 | private: | ||
| 14 | int level; | ||
| 15 | Uint32 alive[5]; | ||
| 16 | Uint32 dead[5]; | ||
| 17 | |||
| 18 | public: | ||
| 19 | Level(); | ||
| 20 | int getLevel(); | ||
| 21 | int getLevelGroup(); | ||
| 22 | bool checkSquare(int x, int y); | ||
| 23 | Uint32 getAliveColor(); | ||
| 24 | Uint32 getDeadColor(); | ||
| 25 | }; | ||
| 26 | |||
| 27 | struct Info { | ||
| 28 | int playerx, playery; | ||
| 29 | Level level; | ||
| 30 | }; | ||
| 31 | |||
| 32 | class Board | ||
| 33 | { | ||
| 34 | private: | ||
| 35 | bool blocks[WIDTH][HEIGHT]; | ||
| 36 | void incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick); | ||
| 37 | GameState::Info* info; | ||
| 38 | |||
| 39 | public: | ||
| 40 | Board(); | ||
| 41 | Board(GameState::Info* info); | ||
| 42 | bool isObstructed(int x, int y); | ||
| 43 | void render(SDL_Surface* screen); | ||
| 44 | void tick(); | ||
| 45 | }; | ||
| 46 | |||
| 5 | private: | 47 | private: |
| 6 | Uint32 player_color; | 48 | Uint32 player_color; |
| 7 | int playerx, playery; | ||
| 8 | bool newGame; | 49 | bool newGame; |
| 9 | bool doneMaking; | 50 | bool doneMaking; |
| 10 | Level level; | 51 | Info info; |
| 11 | Board board; | 52 | Board board; |
| 12 | void move(int x, int y); | 53 | void move(int x, int y); |
| 13 | |||
| 14 | public: | ||
| 15 | GameState(); | ||
| 16 | void input(SDLKey key); | ||
| 17 | void tick(); | ||
| 18 | void render(SDL_Surface* screen); | ||
| 19 | }; | 54 | }; |
| 20 | 55 | ||
| 21 | #endif | 56 | #endif |
| diff --git a/includes.h b/includes.h index cf38bcb..1d9df7a 100644 --- a/includes.h +++ b/includes.h | |||
| @@ -2,7 +2,5 @@ | |||
| 2 | #include <stdio.h> | 2 | #include <stdio.h> |
| 3 | #include <time.h> | 3 | #include <time.h> |
| 4 | #include "mazeoflife.h" | 4 | #include "mazeoflife.h" |
| 5 | #include "level.h" | ||
| 6 | #include "board.h" | ||
| 7 | #include "state.h" | 5 | #include "state.h" |
| 8 | #include "gamestate.h" | 6 | #include "gamestate.h" |
| diff --git a/level.cpp b/level.cpp deleted file mode 100644 index f1157eb..0000000 --- a/level.cpp +++ /dev/null | |||
| @@ -1,57 +0,0 @@ | |||
| 1 | #include "includes.h" | ||
| 2 | |||
| 3 | Level::Level() | ||
| 4 | { | ||
| 5 | level = 1; | ||
| 6 | |||
| 7 | alive[0] = getColor(0, 0, 0); // Black | ||
| 8 | alive[1] = getColor(255, 0, 0); // Red | ||
| 9 | alive[2] = getColor(0, 255, 0); // Green | ||
| 10 | alive[3] = getColor(85, 85, 85); // Dark Gray | ||
| 11 | alive[4] = getColor(255, 0, 255); // Magenta | ||
| 12 | |||
| 13 | dead[0] = getColor(255, 255, 255); // White | ||
| 14 | dead[1] = getColor(255, 192, 203); // Pink | ||
| 15 | dead[2] = getColor(0, 255, 255); // Cyan | ||
| 16 | dead[3] = getColor(170, 170, 170); // Light Gray | ||
| 17 | dead[4] = getColor(255, 128, 0); // Orange | ||
| 18 | } | ||
| 19 | |||
| 20 | int Level::getLevel() | ||
| 21 | { | ||
| 22 | return level; | ||
| 23 | } | ||
| 24 | |||
| 25 | int Level::getLevelGroup() | ||
| 26 | { | ||
| 27 | return (level/10)+1; | ||
| 28 | } | ||
| 29 | |||
| 30 | bool Level::checkSquare(int x, int y) | ||
| 31 | { | ||
| 32 | switch (getLevelGroup()) | ||
| 33 | { | ||
| 34 | case 1: | ||
| 35 | return ((x>13)&&(x<16)&&(y>13)&&(y<16)); | ||
| 36 | case 2: | ||
| 37 | return ((x>13)&&(x<17)&&(y>13)&&(y<17)); | ||
| 38 | case 3: | ||
| 39 | case 4: | ||
| 40 | return ((x>12)&&(x<18)&&(y>12)&&(y<18)); | ||
| 41 | case 5: | ||
| 42 | case 6: | ||
| 43 | return ((x>11)&&(x<19)&&(y>11)&&(y<19)); | ||
| 44 | default: | ||
| 45 | return true; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | Uint32 Level::getAliveColor() | ||
| 50 | { | ||
| 51 | return alive[(getLevelGroup()-1)%5]; | ||
| 52 | } | ||
| 53 | |||
| 54 | Uint32 Level::getDeadColor() | ||
| 55 | { | ||
| 56 | return dead[(getLevelGroup()-1)%5]; | ||
| 57 | } | ||
| diff --git a/level.h b/level.h deleted file mode 100644 index 1cbb46f..0000000 --- a/level.h +++ /dev/null | |||
| @@ -1,20 +0,0 @@ | |||
| 1 | #ifndef LEVEL_H | ||
| 2 | #define LEVEL_H | ||
| 3 | |||
| 4 | class Level | ||
| 5 | { | ||
| 6 | private: | ||
| 7 | int level; | ||
| 8 | Uint32 alive[5]; | ||
| 9 | Uint32 dead[5]; | ||
| 10 | |||
| 11 | public: | ||
| 12 | Level(); | ||
| 13 | int getLevel(); | ||
| 14 | int getLevelGroup(); | ||
| 15 | bool checkSquare(int x, int y); | ||
| 16 | Uint32 getAliveColor(); | ||
| 17 | Uint32 getDeadColor(); | ||
| 18 | }; | ||
| 19 | |||
| 20 | #endif | ||
| diff --git a/mazeoflife.cpp b/mazeoflife.cpp index cbf1f1d..62551c7 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp | |||
| @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) | |||
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | SDL_WM_SetCaption("Maze Of Life", NULL); | 29 | SDL_WM_SetCaption("Maze Of Life", NULL); |
| 30 | SDL_EnableKeyRepeat(150, 75); | 30 | SDL_EnableKeyRepeat(100, 50); |
| 31 | 31 | ||
| 32 | State* state = new GameState(); | 32 | State* state = new GameState(); |
| 33 | 33 | ||
