Archived

This topic is now archived and is closed to further replies.

Ekim_Gram

Clearing the console screen (C++)

Recommended Posts

Ekim_Gram    418
I'm working on making Tic Tac Toe right now and so far here's my code:
#include <iostream>

char Board[] = 
{ ' ', '|', ' ', '|', ' ', '\n',
  '-', '-', '-', '-', '-', '\n',
  ' ', '|', ' ', '|', ' ', '\n',
  '-', '-', '-', '-', '-', '\n',
  ' ', '|', ' ', '|', ' ', '\n',
		            \n',
};

void DrawBoard();
void DrawBoard()
{
	for (int i = 0; i < 30; i++)
		{
		std::cout << Board[i];
		}
}

void Move();
void Move()
{
	int i;
	std::cin >> i;
	if (i = 1)
		{
		Board[0] = 'X';
		std::cout << "\n" << std::endl;
		DrawBoard();
		}
}


int main()
{
	DrawBoard();
	Move();
	return 0;
}
It's not optimized or anything because I just started it 5 minutes ago. Now, my question is this: How do I have it so that every time the DrawBoard() function is called, the screen is cleared and then the game board is printed again after you move?
R.I.P. Mark Osback Solo Pa Mi Gente [edited by - Ekim_Gram on September 2, 2003 1:06:06 AM]

Share this post


Link to post
Share on other sites
Colin Jeanne    1114
One way is to print about 20 ''\n'' characters. If you are using Borland''s compiler there is a function clrscr() in conio.h. You can also use system("cls") or write your own clrscr() using the Windows API (which I wont get into here).


Colin Jeanne | Invader''s Realm

Share this post


Link to post
Share on other sites
drekkar    122
instead of clearing and redrawing it might be in your best interest to just write over the board thats already there

SetConsoleCursorPosition(hConsole, zeroCoordinate);

to get a handle to the console...
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

Share this post


Link to post
Share on other sites
Ekim_Gram    418
quote:
Original post by Tiffany_Smith
You could use the "dirty rectangles" approach that i use in AsciiRis.

An ASCII tetris clone... | AsciiRis


In what portion of the code is that located? I''ve looked through it and a lot of it is Win32 stuff (by the way, where did you learn that stuff?) and I couldn''t find anything commented or something like that. And I must say, that is a great game (i first found it when i asked a question about tetris)



R.I.P. Mark Osback
Solo Pa Mi Gente

Share this post


Link to post
Share on other sites
Tiffany Smith    224
quote:
Original post by Ekim_Gram
In what portion of the code is that located? I've looked through it and a lot of it is Win32 stuff and I couldn't find anything commented or something like that.

Yes, I just realized, it is badly commented :\. ...

Here is the part where I draw the pieces and use the dirty rectangle approach, hopefully a little more understandable with the new comments ...

////////////////////////////////////////////////////////////

//

// Process Piece

//

// This function, handles the dropping the

// rotation and the moving of the pieces.

//

////////////////////////////////////////////////////////////

void ProcessPiece()
{

bool bdropping = false;

currentPieceTime += frameTime;
if(currentPieceTime > TimeToDrop || bForceDrop)
{
previousX = pieceX;
previousY = pieceY;
previousRotation = pieceRotation;

pieceY++;
currentPieceTime -= TimeToDrop;
bdropping = true;
}
else if(!bInput)
return;

if(!bdropping)
{
previousX = pieceX; /*AsciiRis uses the "dirty rectangles" approach to draw the pieces, it draws a character and then draws over it with a blank space*/
previousY = pieceY; /*we are assigning "previousX" and "previousY" to "PieceX" and "pieceY" in order to use the previous position to tell our blank space where to draw next*/
previousRotation = pieceRotation;
}

if (bDropPiece)
{
}
/*here is where we say, if the left key is down move the piece to the left*/
else if (bLeft)
{ /*pieceX is the horizontal axis, decrementing pieceX with the -- symbol we are telling the cursor to go left (or maybe you prefer to think of it as going backwards, like the backspace key)*/
pieceX--;
bLeft = false; /*we set bleft to false again to prevent the piece from continuing to move left when we let go of the left arrow key*/
}
else if (bRight) /*if the right key is pressed*/
{
pieceX++; /*this instance of pieceX is being incremented, thus telling the cursor to move right (or forward, however you like to think of it) */
bRight = false; /*set it to false again so that we know when the key isnt being held down any longer*/
}
else if (bRotCounterClock)
{
pieceRotation--; /*turn counterclockwise*/
bRotCounterClock = false;

if(pieceRotation < 0)
pieceRotation += ROTATIONCOUNT; /*increment stepping through each of the four rotations*/
}
else if (bRotClock)
{
pieceRotation++; /*turn clockwise*/
bRotClock = false;

if(pieceRotation >= ROTATIONCOUNT) /*decrement stepping through each of the four rotations*/
pieceRotation -= ROTATIONCOUNT;
}

if (Collision()) /*a piece collides if...*/
{
if(pieceY <= 3) /*if piece is less than or equal to 3 spaces toward the top of the screen...*/
{
GameOver( pCurrentPieceTime); /*then games over*/
}
if(bdropping && pieceY != previousY) /*check if piece are still falling*/
{
if(pieceX != previousX) /*check if piece moved on the X axis*/
{
pieceX = previousX; /*if piece did move on the X axis, then set its new position*/
if(!Collision()) /*test for collision again*/
{
bmoved = true; /*yep piece moved, return a true*/
return;
}
}

else
{
for(int i = 0; i < 4; i++)
board[Pieces[currentPiece].pos[pieceRotation][i].x + pieceX - MINX][Pieces[currentPiece].pos[pieceRotation][i].y + pieceY - MINY - 1] = true;

CreatePiece( pCurrentPieceTime );
}
}
else
{
pieceRotation = previousRotation; // again we use the dirty rectanges approach to draw over the last rotation when the new one is drawn.

pieceX = previousX;
pieceY = previousY;
}
}
else
bmoved = true;


}


Here is the part where i actually draw the pieces...

//if a piece has been moved draw it in its new position

if(bmoved)
{
DrawPieces(currentPiece, previousX, previousY, previousRotation, ' '); /*here is the blank space we use to cover the characters that make up each piece, whenever the piece moves position*/
DrawPieces(currentPiece, pieceX, pieceY, pieceRotation, '*'); /*heres is the character that we use to make the pieces*/
bmoved = false;
}

ProcessFullRow(TimeToDrop);

You can see that I use an asterisk ('*') to draw the pieces and then a blank space(' ') to draw over the asterisk when it moves position.

quote:

by the way, where did you learn that stuff?


I have been learning C for 3 years but this was the first time I had ever used the win32api. Once I had used a couple of functions my confidence kicked in and it all started to seem much easier, I still have a very long way to go but at the moment I'm working on writing AsciiRis2 with classes and adding functionality for the mouse as well as a difficulty setting and maybe a high scores table (I need the practice).
quote:

And I must say, that is a great game (I first found it when i asked a question about tetris)


Thank you very much, its very nice to get positive feedback.

Hopefully my explanation was clear, if not please feel free to ask.

An ASCII tetris clone... | AsciiRis

[edited by - Tiffany Smith on September 2, 2003 3:06:01 AM]

Share this post


Link to post
Share on other sites