1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include<iostream>
#include<windows.h>
const int N = 10;
using namespace std;
//======================================
void gotoxy( int x, int y )
{
static HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
COORD c = { x, y };
SetConsoleCursorPosition( h, c );
}
//======================================
void fill_table( char tab[N][N], char c )
{
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ ) tab[i][j] = c;
}
}
//======================================
void draw_table( char tab[N][N] )
{
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < N; j++ )
{
gotoxy( i, j );
cout << tab[i][j];
}
}
}
//======================================
void update_table( int xold, int yold, char cdefault, int x, int y, char c )
{
gotoxy( xold, yold ); cout << cdefault;
gotoxy( x , y ); cout << c;
gotoxy( 0 , N + 1 ); cout << "Enter u, d, l, r: ";
}
//======================================
int main()
{
char cdefault = '-';
char c = 'X';
char tab[N][N];
fill_table( tab, cdefault );
draw_table( tab );
int x = 5, y = 5;
update_table( x, y, cdefault, x, y, c );
char direction;
while ( true )
{
int xold = x, yold = y;
cin >> direction;
switch( direction )
{
case 'u': if ( y != 0 ) y--; break;
case 'd': if ( y != N - 1 ) y++; break;
case 'l': if ( x != 0 ) x--; break;
case 'r': if ( x != N - 1 ) x++; break;
}
update_table( xold, yold, cdefault, x, y, c );
}
}
|