Clearing code

Hi everyone!

I want to learn to code so I decided to start by making a simple tic tac toe game. I have a plan in my mind but I'm not sure if you can actually do it.

I've made the tic tac toe board fine. Below the tic tac toe board I want to have it so it says something like "Player 1 make your move!" and then after they have clear those couple lines of text and write something new for player 2. I know there is a system("CLS") command but I don't want to clear all the code just a couple of lines.

Is there a way to do this? If not, any suggestions on what to do.

Thanks in advance!
-analyst
There is a way to only clear/overwrite a few lines depending on your operating system however for this system("cls") will do.
I want to have it so it says something like "Player 1 make your move!"


For this simply #include <iostream> at the top. And then you could add the line using namespace std. After that inside of main use the cout object from the iostream to display text. cout << "some text in quotes";. If you didn't use using namespace std it would be like this std::cout << "some text in quotes";. Also if you want to take in input use std::cin >> someVariable.

and then after they have clear those couple lines of text and write something new for player 2


For this use the system("cls") command and then after that redisplay the tictactoe board, loops is what you would need here. After that it would like you only cleared the responses and text whilst displaying updated board.

If you need help I could show you my tic-tac-toe game or just ask for more help here.

Good resources:
http://www.cplusplus.com/reference/istream/iostream/?kw=iostream
For input/output.

http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm
For loops.
Why don't you do a little search here on the forum?
There are plenty of threads about TicTacToe games.
The search box is at the top.
I think you could use clrscr() of<conio.h> but it may be deprecated so I would recommend system("CLS");

For what you're intending to do, why don't you just leave a few lines like:

std::cout << std::endl << std::endl ... ;

Or, if you're on windows, you could change the color of the text to its background. Just use my function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <Windows.h>

void color(int);

int main()
{
   color(4);
   std::cout << "I'm red" << std::endl;

   std::cin.get();
   return 0;
}

void color(int x)
{
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
}


Here's my tic-tac-toe:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <windows.h>


char num[9]={'0', '1', '2', '3', '4', '5', '6', '7', '8'};

//Create colors
void color(int no)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), no);
}

//non-return functions
void boardSetup();
void check2();
void check1();


int main()
{
    system("COLOR 01");
    int decision;
    boardSetup();

    //Do this 8 times
    for(int counter=0; counter<=3; counter++)
    {
        color(7); std::cout << "Enter your number player 1:";
        color(2); std::cin >> decision;
        system("cls");
        num[decision]='X';
        boardSetup();

        color(7); std::cout << "Enter your number player 2:";
        color(2); std::cin >> decision;
        system("cls");
        num[decision]='O';
        boardSetup();

        //check for player 2 to see if he wins
        color(7);
        if(num[0]=='O' and num[4]=='O' and num[8]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[2]=='O' and num[4]=='O' and num[6]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[0]=='O' and num[3]=='O' and num[6]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[1]=='O' and num[4]=='O' and num[7]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[2]=='O' and num[5]=='O' and num[8]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[0]=='O' and num[1]=='O' and num[2]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[3]=='O' and num[4]=='O' and num[5]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        if(num[6]=='O' and num[7]=='O' and num[8]=='O')
            std::cout << "Player 2 wins! Congratulations!";
        //check for player 1 to see if he wins
        if(num[0]=='X' and num[4]=='X' and num[8]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[2]=='X' and num[4]=='X' and num[6]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[0]=='X' and num[3]=='X' and num[6]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[1]=='X' and num[4]=='X' and num[7]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[2]=='X' and num[5]=='X' and num[8]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[0]=='X' and num[1]=='X' and num[2]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[3]=='X' and num[4]=='X' and num[5]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        if(num[6]=='X' and num[7]=='X' and num[8]=='X')
            std::cout << "Player 1 wins! Congratulations!";
        //check to see whether 'O' is being replaced by 'X'
        /*for(int x=0; x<=sizeof(num); x++)
        {
            if(num[x]=='O' and decision==x)
            {
                std::cerr << "You can't do that! Player 2 has "
                << "aldready marked the spot!\n\n";
                exit(EXIT_SUCCESS);
            }
            if(num[x]=='X' and decision==x)
            {
                std::cerr << "You can't do that! Player 1 has "
                << "aldready marked the spot!\n\n";
                exit(EXIT_SUCCESS);
            }
        }*/
    }
}

void boardSetup()
{
    color(5);
    std::cout << num[0] << "|" << num[1] << "|" << num[2] << std::endl;
    std::cout << "_" << "|" << "_" << "|" << "_" << std::endl;
    std::cout << num[3] << "|" << num[4] << "|" << num[5] << std::endl;
    std::cout << "_" << "|" << "_" << "|" << "_" << std::endl;
    std::cout << num[6] << "|" << num[7] << "|" << num[8] << std::endl;
}

Thanks everyone,

I figured out how to do it now with loops!

Last edited on
Topic archived. No new replies allowed.