please help

Dec 17, 2012 at 11:13am
I made tictactoe program for practice, however i wonder is there a way to get program output(text on screen) 1 at the time. heres example, this is how the program runs on eclipse

XO game by IronMan!
Player (X), Computer (O)!

____________
 1 | 2 | 3
-----------
 4 | 5 | 6
-----------
 7 | 8 | 9
____________
Player X:1
Computer O:2
____________
 X | O | 3
-----------
 4 | 5 | 6
-----------
 7 | 8 | 9
____________
Player X:9
Computer O:5
____________
 X | O | 3
-----------
 4 | O | 6
-----------
 7 | 8 | X
____________
Player X:8
Computer O:7
____________
 X | O | 3
-----------
 4 | O | 6
-----------
 O | X | X
____________
Player X:


is it possible to make it display only one grid and refresh it each turn.

XO game by IronMan!
Player (X), Computer (O)!

____________
 X | O | 3
-----------
 4 | O | 6
-----------
 O | X | X
____________
Player X:


Much abliged
Dec 17, 2012 at 11:26am
Not using standard C++. The console isn't generally designed to do that.

If you want to do that I would suggest just getting a graphics lib like SFML and doing it yourself. It's actually not really that hard.
Dec 17, 2012 at 11:53am
you could use a bunch of newline commands
Dec 17, 2012 at 3:12pm
is it possible to make it display only one grid and refresh it each turn.


Not exactly sure what your asking but by grid if you mean the 3x3 board, I would get player input, calculate computer move, clear screen and redraw the board.

If you mean you only want to redraw the # and replace it with a X or O, I think it's possible, meaning I think I have seen it done with some ASCI games but not sure how you would do that.
Dec 17, 2012 at 9:03pm
To refresh the console screen after each turn use this function (don't forget to #include<windows.h>)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void clear_screen()
{
	HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(hndl, &csbi);
	DWORD written;
	DWORD n = csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X + 1;
	COORD curhome = {0,0};
	FillConsoleOutputCharacter(hndl, ' ', n, curhome, &written);
	csbi.srWindow.Bottom -= csbi.srWindow.Top;
	csbi.srWindow.Top = 0;
	SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
	SetConsoleCursorPosition(hndl, curhome);
};
Dec 17, 2012 at 10:00pm
Use system("CLS"); in cstdlib.This may be easiest way.
Dec 17, 2012 at 10:09pm
@Gupta try this, runs fine (10 times repeat)

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
#include <iostream>
#include<windows.h>
using namespace std;

void hide_cursor()
{
  CONSOLE_CURSOR_INFO cursor = {1, FALSE};
  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
}
void show_cursor()
{
  CONSOLE_CURSOR_INFO cursor = {1, TRUE};
  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
}

void clear_screen()
{
	HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo(hndl, &csbi);
	DWORD written;
	DWORD n = csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X + 1;
	COORD curhome = {0,0};
	FillConsoleOutputCharacter(hndl, ' ', n, curhome, &written);
	csbi.srWindow.Bottom -= csbi.srWindow.Top;
	csbi.srWindow.Top = 0;
	SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
	SetConsoleCursorPosition(hndl, curhome);
};
int main()
{
hide_cursor();
        for(int xxx=0;xxx<5;xxx++)
        {
            cout << "********************";
            for(int a=0;a<10;a++)
            cout << endl;
            cout << "********************";
            Sleep(1000);
            clear_screen();
            for(int c=0;c<5;c++)
            cout << endl;
            cout << "     **********     ";
            Sleep(1000);
            clear_screen();

        }
show_cursor();
}
Dec 18, 2012 at 5:17am
why to use such a long code when we have simple combination of system("CLS"); and Sleep(m_time);


try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<cstdlib>
#include<windows.h>
using namespace std;
int main()
{
    for(int i=0;i<10;i++)
    {
        system("CLS");
        cout<<"**************\n\n\n\n\n\n***************";
        Sleep(1000);
        system("CLS");
        cout<<"\n\n\n***********";
        Sleep(1000);
    }
}
Dec 18, 2012 at 6:32am
There is an article on this site about why the use of system is bad, and different ways of doing clearscreen.

The windows version is obviously not portable.

Having a function that prints 50 newlines might be the easiest brute force method, as darkmaster was alluding to.

Any way read the article.
Dec 18, 2012 at 6:46am
Why system() is evil by Duoas: http://cplusplus.com/articles/j3wTURfi/
Clear the screen by Duoas: http://cplusplus.com/articles/4z18T05o/

Also, windows specific functions (Sleep) shouldn't be suggested without knowing the OP is on Windows. I have a feeling that this is going to turn into a sticky like the "Why does my console keep closing?" one.

As firedraco said, and the above articles, the console just isn't meant to do that. However, if you insist, there is several possibilities. I suggest doing research and deciding for yourself what is needed/absolutely necessary.
Dec 18, 2012 at 9:01am
@ IronmanCro

Since you haven't mentioned your platform (run on eclipse doesn't make any sense in terms of platform).

As per my perspective, every console has been built focusing on different goals. Using newlines won't be a good solution when you think of portability. One of the basic issue would be "line wrapping" in consoles.

So thinking about a having a good design of 'tictactoe' program which separates display information from game logic by providing a generic interface and having concrete displaying solutions for each relevant console would be a good idea.
Dec 18, 2012 at 6:30pm
adseran wrote:
As per my perspective, every console has been built focusing on different goals. Using newlines won't be a good solution when you think of portability. One of the basic issue would be "line wrapping" in consoles.

I believe you have the wrong concept of newlines. That's about as portable of a clearscreen as you can get. Output 100 new lines, there is now nothing on the screen. I fail to see how line wrapping has anything to do with this. Even if you meant word wrapping, that doesn't affect newlines. If you meant line wrapping in the sense that the console stores everything in essentially a large array and wraps back to the top, I don't know of any consoles that do this (it's possible that characters might still be left on the display) but that is going to be an issue overall with that specific console. Any console that I know of that might do that would clear the buffer before wrapping it around, to prevent garbage from being displayed. If you could show me an example, that'd be great.
Topic archived. No new replies allowed.