From 8c1f08f2133de108fd354dd07c65e0c24ecc1d38 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 18 Jun 2009 19:37:24 -0400 Subject: Wrote Life solver --- Makefile | 2 +- board.cpp | 48 --------------- board.h | 16 ----- gamestate.cpp | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- gamestate.h | 51 +++++++++++++--- includes.h | 2 - level.cpp | 57 ------------------ level.h | 20 ------- mazeoflife.cpp | 2 +- 9 files changed, 212 insertions(+), 166 deletions(-) delete mode 100644 board.cpp delete mode 100644 board.h delete mode 100644 level.cpp delete mode 100644 level.h diff --git a/Makefile b/Makefile index 2335076..0eb1738 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OBJS = mazeoflife.o gamestate.o board.o level.o +OBJS = mazeoflife.o gamestate.o CC = g++ CFLAGS = `pkg-config sdl --cflags` 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 @@ -#include "includes.h" - -Board::Board() -{ - Board(Level()); -} - -Board::Board(Level level) -{ - int x,y; - for (y=0;y13)&&(x<16)&&(y>13)&&(y<16)); + case 2: + return ((x>13)&&(x<17)&&(y>13)&&(y<17)); + case 3: + case 4: + return ((x>12)&&(x<18)&&(y>12)&&(y<18)); + case 5: + case 6: + return ((x>11)&&(x<19)&&(y>11)&&(y<19)); + default: + return true; + } +} + +Uint32 GameState::Level::getAliveColor() +{ + return alive[(getLevelGroup()-1)%5]; +} + +Uint32 GameState::Level::getDeadColor() +{ + return dead[(getLevelGroup()-1)%5]; +} + +GameState::Board::Board() +{ + GameState::Board::Board(new GameState::Info()); +} + +GameState::Board::Board(GameState::Info* info) +{ + this->info = info; + + int x,y; + for (y=0;ylevel.checkSquare(x, y)) + { + blocks[x][y] = rand() % 2; + } else { + blocks[x][y] = false; + } + } + } +} + +bool GameState::Board::isObstructed(int x, int y) +{ + return blocks[x][y]; +} + +void GameState::Board::render(SDL_Surface* screen) +{ + SDL_Rect block; + block.w = 16; + block.h = 16; + + int x,y; + + for (y=0;ylevel.getAliveColor() : info->level.getDeadColor())); + } + } +} + +void GameState::Board::tick() +{ + bool temp[WIDTH][HEIGHT]; + int x,y; + for (x=0;x0)&&(y>0)) incrementIfNeighbor(x-1,y-1,temp,&neighbors); + if ((x>0)) incrementIfNeighbor(x-1,y,temp,&neighbors); + if ((x>0)&&(y0)) incrementIfNeighbor(x,y-1,temp,&neighbors); + if ((y0)) incrementIfNeighbor(x+1,y-1,temp,&neighbors); + if ((x= 1) && (neighbors <= 4)); + } else { + blocks[x][y] = (neighbors == 3); + } + } + } +} + +void GameState::Board::incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick) +{ + wrap(&x, &y); + + if ((blocks[x][y])||((info->playerx==x)&&(info->playery==y))) + { + ++*tick; + } +} diff --git a/gamestate.h b/gamestate.h index 727c8f1..c36bcee 100644 --- a/gamestate.h +++ b/gamestate.h @@ -2,20 +2,55 @@ #define GAMESTATE_H class GameState : public State { + public: + GameState(); + void input(SDLKey key); + void tick(); + void render(SDL_Surface* screen); + + class Level + { + private: + int level; + Uint32 alive[5]; + Uint32 dead[5]; + + public: + Level(); + int getLevel(); + int getLevelGroup(); + bool checkSquare(int x, int y); + Uint32 getAliveColor(); + Uint32 getDeadColor(); + }; + + struct Info { + int playerx, playery; + Level level; + }; + + class Board + { + private: + bool blocks[WIDTH][HEIGHT]; + void incrementIfNeighbor(int x, int y, bool temp[WIDTH][HEIGHT], int* tick); + GameState::Info* info; + + public: + Board(); + Board(GameState::Info* info); + bool isObstructed(int x, int y); + void render(SDL_Surface* screen); + void tick(); + }; + private: Uint32 player_color; - int playerx, playery; bool newGame; bool doneMaking; - Level level; + Info info; Board board; void move(int x, int y); - - public: - GameState(); - void input(SDLKey key); - void tick(); - void render(SDL_Surface* screen); }; #endif diff --git a/includes.h b/includes.h index cf38bcb..1d9df7a 100644 --- a/includes.h +++ b/includes.h @@ -2,7 +2,5 @@ #include #include #include "mazeoflife.h" -#include "level.h" -#include "board.h" #include "state.h" #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 @@ -#include "includes.h" - -Level::Level() -{ - level = 1; - - alive[0] = getColor(0, 0, 0); // Black - alive[1] = getColor(255, 0, 0); // Red - alive[2] = getColor(0, 255, 0); // Green - alive[3] = getColor(85, 85, 85); // Dark Gray - alive[4] = getColor(255, 0, 255); // Magenta - - dead[0] = getColor(255, 255, 255); // White - dead[1] = getColor(255, 192, 203); // Pink - dead[2] = getColor(0, 255, 255); // Cyan - dead[3] = getColor(170, 170, 170); // Light Gray - dead[4] = getColor(255, 128, 0); // Orange -} - -int Level::getLevel() -{ - return level; -} - -int Level::getLevelGroup() -{ - return (level/10)+1; -} - -bool Level::checkSquare(int x, int y) -{ - switch (getLevelGroup()) - { - case 1: - return ((x>13)&&(x<16)&&(y>13)&&(y<16)); - case 2: - return ((x>13)&&(x<17)&&(y>13)&&(y<17)); - case 3: - case 4: - return ((x>12)&&(x<18)&&(y>12)&&(y<18)); - case 5: - case 6: - return ((x>11)&&(x<19)&&(y>11)&&(y<19)); - default: - return true; - } -} - -Uint32 Level::getAliveColor() -{ - return alive[(getLevelGroup()-1)%5]; -} - -Uint32 Level::getDeadColor() -{ - return dead[(getLevelGroup()-1)%5]; -} diff --git a/level.h b/level.h deleted file mode 100644 index 1cbb46f..0000000 --- a/level.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef LEVEL_H -#define LEVEL_H - -class Level -{ - private: - int level; - Uint32 alive[5]; - Uint32 dead[5]; - - public: - Level(); - int getLevel(); - int getLevelGroup(); - bool checkSquare(int x, int y); - Uint32 getAliveColor(); - Uint32 getDeadColor(); -}; - -#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[]) } SDL_WM_SetCaption("Maze Of Life", NULL); - SDL_EnableKeyRepeat(150, 75); + SDL_EnableKeyRepeat(100, 50); State* state = new GameState(); -- cgit 1.4.1