Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <conio.h>
- #include <windows.h>
- #include <time.h>
- #include <fstream>
- #include <iostream>
- #include <deque>
- using namespace std;
- struct position {
- int x;
- int y;
- int moveno;
- };
- #define MAX_X 77 // maximum x position for player
- #define SCROLL_POS 24 // the point that scrolling occurs
- void Init_Graphics(void);
- inline void Set_Color(int fcolor, int bcolor);
- inline void Draw_String(int x,int y, char *string);
- CONSOLE_SCREEN_BUFFER_INFO con_info; // holds screen info
- HANDLE hconsole; // handle to console
- int grid[11][11]={0};
- void Init_Graphics(void)
- {
- // this function initializes the console graphics engine
- COORD console_size = {80,30}; // size of console
- // seed the random number generator with time
- srand((unsigned)time(NULL));
- // open i/o channel to console screen
- hconsole=CreateFile(TEXT("CONOUT$"),GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- 0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);
- // make sure we are in 80x25
- SetConsoleScreenBufferSize(hconsole,console_size);
- // get details for console screen
- GetConsoleScreenBufferInfo(hconsole,&con_info);
- } // end Init_Graphics
- inline void Set_Color(int fcolor, int bcolor=0)
- {
- // this function sets the color of the console output
- SetConsoleTextAttribute(hconsole,(WORD)((bcolor << 4) |
- fcolor));
- } // Set_Color
- inline void Draw_String(int x,int y, char *string)
- {
- // this function draws a string at the given x,y
- COORD cursor_pos; // used to pass coords
- // set printing position
- cursor_pos.X = x;
- cursor_pos.Y = y;
- SetConsoleCursorPosition(hconsole,cursor_pos);
- // print the string in current color
- printf("%s",string);
- } // end Draw_String
- inline void Clear_Screen(void)
- {
- // this function clears the screen
- // set color to white on black
- Set_Color(15,0);
- // clear the screen
- for (int index=0; index<=25; index++)
- Draw_String(0, SCROLL_POS,"\n");
- } // end Clear_Screen
- void draw_grid() {
- int i, j;
- void drawch(int, int, int);
- int map(int);
- Init_Graphics();
- Set_Color(15,15); // white background
- //top row
- Set_Color(15, 0);
- drawch(0, 0, 218);
- for(i=1; i<24; i++)
- {
- drawch(i, 0, 196);
- }
- drawch(24, 0, 191);
- // upper middle row
- /*drawch(0, 8, 195);
- for(i=1; i<24; i++)
- if(i==8 || i==16)
- drawch(i, 8, 197);
- else
- drawch(i, 8, 196);
- drawch(24, 8, 180);*/
- // lower middle row
- /*drawch(0, 16, 195);
- for(i=1; i<24; i++)
- drawch(i, 16, 196);
- drawch(24, 16, 180);*/
- // left side
- for(i=1; i<24; i++)
- drawch(0, i, 179);
- // left middle vertical
- /*drawch(0, 8, 196);
- for(i=1; i<24; i++)
- if(i==8 || i==16)
- drawch(8, i, 197);
- else
- drawch(8, i, 179);
- drawch(8, 24, 193);
- // right middle vertical
- drawch(0, 16, 196);
- for(i=1; i<24; i++)
- if(i==8 || i==16)
- drawch(16, i, 197);
- else
- drawch(16, i, 179);
- drawch(16, 24, 193);*/
- // right side
- for(i=1; i<24; i++)
- drawch(24, i, 179);
- // bottom row
- drawch(0, 24, 192);
- for(i=1; i<24; i++)
- {
- drawch(i, 24, 196);
- }
- drawch(24, 24, 217);
- //drawch(0, 8, 195);
- //drawch(0, 16, 195);
- //drawch(24, 8, 180);
- //drawch(24, 16, 180);
- int cell;
- for(i=1; i<24; i++)
- for(j=1; j<24; j++){
- if((i%2 == 0))
- if((j%2 == 0)){
- Draw_String(i, j, "");
- cell = grid[map(j)][map(i)];
- if(cell == 300){
- cout << "S";
- }
- else if(cell == 301){
- cout << "T";
- }
- else
- cout << cell;
- }
- Draw_String(78, 24, "");
- }
- }
- void drawch(int x, int y, int ch){
- char str[] = "?";
- str[0] = ch;
- Draw_String(x, y, str);
- }
- int map(int k){
- int m = k/2;
- return m-1;
- }
- void main(){
- int runsearch = 1;
- deque<position> wset;
- struct position wposition;
- struct position cposition;
- grid[5][5] = 300;
- grid[2][8] = 301;
- draw_grid();
- wposition.x = 5;
- wposition.y = 5;
- wposition.moveno = 0;
- while(runsearch==1){
- draw_grid();
- Sleep(500);
- wposition.moveno++;
- wset.push_back(wposition);
- cposition = wset.front();
- wset.pop_front();
- //Search North
- if(cposition.y > 0)
- {
- wposition.y = cposition.y-1;
- if(grid[wposition.y][cposition.x]==301)
- {
- runsearch=0;
- }
- else
- {
- if(grid[wposition.y][cposition.x] == 0)
- {
- grid[wposition.y][cposition.x]=wposition.moveno;
- wposition.x=cposition.x;
- wset.push_back(wposition);
- }
- }
- }
- //Search South
- if(cposition.y < 10)
- {
- wposition.y = cposition.y+1;
- if(grid[wposition.y][cposition.x]==301)
- {
- runsearch=0;
- }
- else
- {
- if(grid[wposition.y][cposition.x] == 0)
- {
- grid[wposition.y][cposition.x]=wposition.moveno;
- wposition.x=cposition.x;
- wset.push_back(wposition);
- }
- }
- }
- //Search East
- if(cposition.x > 0)
- {
- wposition.x = cposition.x-1;
- if(grid[cposition.y][wposition.x]==301)
- {
- runsearch=0;
- }
- else
- {
- if(grid[cposition.y][wposition.x] == 0)
- {
- grid[cposition.y][wposition.x]=wposition.moveno;
- wposition.y=cposition.y;
- wset.push_back(wposition);
- }
- }
- }
- //Search West
- if(cposition.x < 10)
- {
- wposition.x = cposition.x+1;
- if(grid[cposition.y][wposition.x]==301)
- {
- runsearch=0;
- }
- else
- {
- if(grid[wposition.y][cposition.x] == 0)
- {
- grid[cposition.y][wposition.x]=wposition.moveno;
- wposition.y=cposition.y;
- wset.push_back(wposition);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment