Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <conio.h> //Gives us infamous getch()
- using namespace std;
- char sheet[10][10];
- void cls()
- {
- // Get the Win32 handle representing standard output.
- // This generally only has to be done once, so we make it static.
- static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- COORD topLeft = { 0, 0 };
- // std::cout uses a buffer to batch writes to the underlying console.
- // We need to flush that to the console because we're circumventing
- // std::cout entirely; after we clear the console, we don't want
- // stale buffered text to randomly be written out.
- std::cout.flush();
- // Figure out the current width and height of the console window
- if (!GetConsoleScreenBufferInfo(hOut, &csbi)) {
- // TODO: Handle failure!
- abort();
- }
- DWORD length = csbi.dwSize.X * csbi.dwSize.Y;
- DWORD written;
- // Flood-fill the console with spaces to clear it
- FillConsoleOutputCharacter(hOut, TEXT(' '), length, topLeft, &written);
- // Reset the attributes of every character to the default.
- // This clears all background colour formatting, if any.
- FillConsoleOutputAttribute(hOut, csbi.wAttributes, length, topLeft, &written);
- // Move the cursor back to the top left for the next sequence of writes
- SetConsoleCursorPosition(hOut, topLeft);
- }
- void setCursorPosition(int x, int y)
- {
- static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
- std::cout.flush();
- COORD coord = { (SHORT)x, (SHORT)y };
- SetConsoleCursorPosition(hOut, coord);
- }
- void initialize()
- {
- for(int x = 0; x <10; x++)
- {
- for(int y = 0; y < 10; y++)
- {
- sheet[x][y] = '*';
- }
- }
- sheet[5][5] = 'X'; //This is where the player starts
- }
- void draw()
- {
- for(int xx = 0; xx < 10; xx++)
- {
- for(int yy = 0; yy < 10; yy++)
- {
- cout << sheet[xx][yy];
- }
- cout << endl;
- }
- }
- void input_and_update()
- {
- //Get row and column (position) of player
- int c;
- int p;
- int breaking = 0;
- for(c = 0; c < 10; c++)
- {
- for(p = 0; p < 10; p++)
- {
- if(sheet[c][p] == 'X')
- {
- breaking = 1;
- break;
- }
- }
- if(breaking == 1)
- break;
- }
- breaking = 0;
- char variable;
- //77 = right arrow
- //72 = up arrow
- //80 down arrow
- //75 = left arrow
- // c = row, p = column
- while(breaking == 0)
- {
- variable = getch();
- if(variable == 75)
- {
- if(p - 1 < 0)
- {
- ;
- }
- else
- {
- sheet[c][p] = '*';
- setCursorPosition(p, c);
- cout << "*";
- sheet[c][p - 1] = 'X';
- setCursorPosition((p - 1), c);
- cout << "X";
- setCursorPosition(0, 11);
- breaking = 1;
- }
- }
- else if(variable == 72)
- {
- if(c - 1 < 0)
- {
- ;
- }
- else
- {
- sheet[c][p] = '*';
- setCursorPosition(p, c);
- cout << "*";
- sheet[c - 1][p] = 'X';
- setCursorPosition(p, c - 1);
- cout << "X";
- setCursorPosition(0, 11);
- breaking = 1;
- }
- }
- else if(variable == 77)
- {
- if(p + 1 > 9)
- {
- ;
- }
- else
- {
- sheet[c][p] = '*';
- setCursorPosition(p, c);
- cout << "*";
- sheet[c][p + 1] = 'X';
- setCursorPosition((p + 1), c);
- cout << "X";
- setCursorPosition(0, 11);
- breaking = 1;
- }
- }
- else if(variable == 80)
- {
- if(c + 1 > 9)
- {
- ;
- }
- else
- {
- sheet[c][p] = '*';
- setCursorPosition(p, c);
- cout << "*";
- sheet[c + 1][p] = 'X';
- setCursorPosition(p, c + 1);
- cout << "X";
- setCursorPosition(0, 11);
- breaking = 1;
- }
- }
- }
- }
- int main()
- {
- initialize(); // Create play sheet
- draw();
- while(1)
- input_and_update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement