Tic Tac Toe Gamplay?

Widget360 here, posting again. I am creating the tic tac toe game for a simple beginner tutorial. However I have a question about the actual gameplay scripting for it( like everyting about it ). Here is what I got so far. ( PS: i am using Bloddshed Dev-C++ )


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
#include <iostream>
using namespace std;
int main ()
{
    int Title ();
    {
    cout<< " The Tic Tac Toe Game" << "     " << " By: Widget360\n"; 
    cout<< "\n";
    cout<< "Let The Games Begin!!! \n";
    cout<< "\n";
    int Playerone ();
    {
        char question[] = "Player 1, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cin.get(); 
    int Playertwo ();
    {
        char question[] = "Player 2, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
    int Gameboard (); 
    {
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n";
        cout<<"-----1-----1-----\n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n"; 
        cin.get(); }
    cin.get(); }
    return 0;
}


Of course this is just me trying to make it beutifal. Im sure you can also tell I like to be organized ( hint: all my int's ). Im pretty sure it has to do with imputing variables or something. Any help will be apreciated!
Here are some tips:

- Make Gameboard return void as it is not beneficial to return 0
- Create a for loop within Gameboard to write the board
- Pass an array containing the game board to Gameboard so you can change it. You may need a pointer or you could make the array global (although others will dislike that method)

There's more you could do to make it more organized but that would be a big improvement.
I should have included hte fact i am lower than a beginer. I look at these tutorials however all i see are loops for numbers. Am i suposed to say that x = ( whatever my gameboard is ) and then say

int x = 10?

here is the for loop i am looking at

1
2
        for (int n=10; n>0; n--) {
        cout << n << ", "; }


From what i am getting, i would put my gameboard betweeng the " " 's

A i have always said. I am superb at looking at a code and defining it han paragraphs of explanations. So a example or link to one would be nice. Thanx
little note, i believe you're trying to declare a function/ void in lines 11, 23 and 31. These declarations should be done outside of main.
and also you want to have a } at the end of line 23 (to end the title function, which would now come outside of main ;)

And i don't see where this loop is at, but basically you'd show:


n, n, n...
where n is an integer.

Hope it helps!

Cheers :)
For the for loop, how would i use that.
As kaduuk said, " I dont see where this loop is" I dont have one. I know hoe to make numbers count up and then stop. But how do i change that from numbers to the game board, What do i do? All loop examples i see are using integers. I have seen some whisper of something called the "exit function". As I said, my second commet shows the for loop i am studying. Thanx
****UPDATE*******
I found out how to make as many boards as i want in with the for loop
Heres my updated code:
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>
using namespace std;
int main (void)
{
    int Title ();
    {
    cout<< " The Tic Tac Toe Game" << "     " << " By: Widget360\n"; 
    cout<< "\n";
    cout<< "Let The Games Begin!!! \n";
    cout<< "\n"; }
    int Playeronename ();
    {
        char question[] = "Player 1, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cin.get(); 
    int Playertwoname ();
    {
        char question[] = "Player 2, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
    int Gameboard (); 
    {
        for (int n=3; n>0; n--)  {
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n";
        cout<<"-----1-----1-----\n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n";
        cout<<"     1     1     \n"; }
        cin.get(); }
    cin.get(); 
    return 0;
}

As shown, I have 3 boards.
Now how do i let it let me inter a variable before looping. Is it the " go to " or "continue" statement?

*** Important- as of right now, i have no gameplay code, meaning i havent written the code to input my x's and o's. Some tips on that will now be apreciated !!*******
****another update******

Here is the array example I found online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// arrays as parameters
#include <iostream>
using namespace std;

void printarray (int arg[], int length) {
  for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

int main ()
{
  int firstarray[] = {5, 10, 15};
  int secondarray[] = {2, 4, 6, 8, 10};
  printarray (firstarray,3);
  printarray (secondarray,5);
  return 0;
}



I am guesing i need to make it ask me what to print int the array?
thanx
Here's some functions I used for the Dungeon Crawl exercise a while back that may come in handy for your game:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void BlankBoard(char GameBoard[HEIGHT][WIDTH])
{
    for (i = 0; i < HEIGHT; i++)
    {
        for (j = 0; j < WIDTH; j++)
        {
            GameBoard[i][j] = board;
        }
    }
}

void PrintBoard(char GameBoard[HEIGHT][WIDTH])
{
    for (i = 0; i < HEIGHT; i++)
    {
        for (j = 0; j < WIDTH; j++)
        {
            cout << GameBoard[i][j];
        }
        cout << "\n";
    }
}


For the full implementation: http://codepad.org/RyvduP6E
I had to step away for awhile but i'm back. Heres a issue i have:
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
#include <iostream>
using namespace std;
int main ()
{
    int Title ();
    {
    cout<< " The Tic Tac Toe Game" << "     " << " By: Widget360\n"; 
    cout<< "\n";
    cout<< "Let The Games Begin!!! \n";
    cout<< "\n"; }
    int Playeronename ();
    {
        char question[] = "Player 1, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cin.get(); 
    int Playertwoname ();
    {
        char question[] = "Player 2, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
    int Gameboard (); 
    {
        for (int n=1; n>0; n--)  {
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        int a;
        cout<<"Player 1, Place Your X!\n";
        cin >> a;
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<" " << a << "                1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"-----------------1-----------------1-----------------\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"-----------------1-----------------1-----------------\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cin.get(); }
        cout<<"Well, Done!\n";
        cin.get();
        }
    cin.get();
    return 0;
}


For some reason it won't show my gameboard after i insert my x. My plan was to have

the tic tav board set up like this

a b c
d e f
g h i

then i'l have
int a;
cin << a
cout<<"" << a << " 1 1 "



what can i do?
Okay, Maybe my question was a little confusing but i want to know is why i am forced to enter a numeric value for cin<<a

if i try to enter a letter such as "x or o" then it gives me some weird number and closes automatically. OR can i declare that maybe 1 = x? then i could have a number for each tic tac toe slot; when i enter that slot number in, it puts the desired letter there.
I hope you can answer my question.
I guess i am talking to myself but if anyone out there can help loop my board, please reply!
Here's my code:
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
#include <iostream>
using namespace std;
int main ()
{
    int Title ();
    {
    cout<< " The Tic Tac Toe Game" << "     " << " By: Widget360\n"; 
    cout<< "\n";
    cout<< "Let The Games Begin!!! \n";
    cout<< "\n"; }
    int Playeronename ();
    {
        char question[] = "Player 1, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cin.get(); 
    int Playertwoname ();
    {
        char question[] = "Player 2, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
    int Gameboard (); 
    {
        for (int n=1; n>0; n--)  {
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        int x = 1;
        cout<<"Player 1, Place Your X or O!\n";
        cin >> x;
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        if ( x == 1 ) {
        cout<<"      x          1                 1                 \n";}
        if ( x == 2 ) {
        cout<<"      o          1                 1                 \n";}
        if ( x == 3 ) {
        cout<<"                 1        x        1                 \n";}
        if ( x == 4 ) {
        cout<<"                 1        o        1                 \n";}
        if ( x == 5 ) {
        cout<<"                 1                 1        x        \n";}
        if ( x == 6 ) {
        cout<<"                 1                 1        o        \n";}
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"-----------------1-----------------1-----------------\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"-----------------1-----------------1-----------------\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cin.get(); }
        cout<<"Well, Done!\n";
        cin.get();
        }
    cin.get();
    return 0;
}


I need to loop my board after i enter the first letter. How do i make it loop and show whatever variable i have already entered. If anybody need's help explaining what I'm trying to do, TELL ME!!!

(PS: odd numbers are x's and evens are o's;
the second and third row of the board i have not entered yet. But i am working on it.)
***update***

same question, updated code!!!!

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
#include <iostream>
using namespace std;
int main ()
{
    int Title ();
    {
    cout<< " The Tic Tac Toe Game" << "     " << " By: Widget360\n"; 
    cout<< "\n";
    cout<< "Let The Games Begin!!! \n";
    cout<< "\n"; }
    int Playeronename ();
    {
        char question[] = "Player 1, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cin.get(); 
    int Playertwoname ();
    {
        char question[] = "Player 2, Enter your name: ";
        char greeting[] = "Hello: ";
        char yourname [80];
        cout << question;
        cin >> yourname;
        cout << greeting << yourname << "!"; }
    int Gameboard (); 
    {
        for (int n=1; n>0; n--)  {
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        int x = 1;
        cout<<"Player 1, Place Your X or O!\n";
        cin >> x;
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        if ( x == 1 ) {
        cout<<"      x          1                 1                 \n";}
        if ( x == 2 ) {
        cout<<"      o          1                 1                 \n";}
        if ( x == 3 ) {
        cout<<"                 1        x        1                 \n";}
        if ( x == 4 ) {
        cout<<"                 1        o        1                 \n";}
        if ( x == 5 ) {
        cout<<"                 1                 1        x        \n";}
        if ( x == 6 ) {
        cout<<"                 1                 1        o        \n";}
        cout<<"                 1                 1                 \n";
        cout<<"-----------------1-----------------1-----------------\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        if ( x == 7 ) {
        cout<<"        x        1                 1                 \n";}
        if ( x == 8 ) {
        cout<<"        o        1                 1                 \n";}
        if ( x == 9 ) {
        cout<<"                 1       x         1                 \n";}
        if ( x == 10 ) {
        cout<<"                 1       o         1                 \n";}
        if ( x == 11 ) {
        cout<<"                 1                 1        x        \n";}
        if ( x == 12 ) {
        cout<<"                 1                 1        o        \n";}
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cout<<"-----------------1-----------------1-----------------\n";
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        if ( x == 13 ) {
        cout<<"       x         1                 1                 \n";}
        if ( x == 14 ) {
        cout<<"       o         1                 1                 \n";}
        if ( x == 15 ) {
        cout<<"                 1       x         1                 \n";}
        if ( x == 16 ) {
        cout<<"                 1       o         1                 \n";}
        if ( x == 17 ) {
        cout<<"                 1                 1        x        \n";}
        if ( x == 18 ) {
        cout<<"                 1                 1        o        \n";}
        cout<<"                 1                 1                 \n";
        cout<<"                 1                 1                 \n";
        cin.get(); }
        cout<<"Well, Done!\n";
        cin.get();
        }
    cin.get();
    return 0;
}
closed account (S6k9GNh0)
EDIT to make more sense: The code you provide shows that you don't have a running environment working because this won't compile. What beginner tutorial are you looking at? I would suggest you start from a book or our tutorial here if you don't like the pricing of a book. I would suggest you start over.
Last edited on
Alright, to be honest that code is very sloppy. I wrote up a Tic Tac Toe game in C++ just to show you how I would do this:

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.h>
#include <conio.h>

int main ()
{
    using namespace std;
    system ("title Tic Tac Toe Game by CPPProgrammer4L");

    // Spaces for the play mat
    char cSpaces [] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    // Bool for game over, we set this to false
    bool bGameOver = false;
    // Int for whos turn it is, 1 for first player, 2 for second player
    int iPlayerTurn = 1;
    // Int for the current move
    int iInput;

    // Game loop
    while (!bGameOver)
    {
        // Display the play mat
        cout << cSpaces [0] << " | " << cSpaces [1] << " | " << cSpaces [2] << endl;
        cout << "---------" << endl;
        cout << cSpaces [3] << " | " << cSpaces [4] << " | " << cSpaces [5] << endl;
        cout << "---------" << endl;
        cout << cSpaces [6] << " | " << cSpaces [7] << " | " << cSpaces [8] << endl << endl;

        // Get the move
        cout << "Player " << iPlayerTurn << "'s Move: ";
        cin >> iInput;

        // Clear the screen
        system ("cls");

        // If the selection was already used then continue
        if (cSpaces [iInput] == 'x' || cSpaces [iInput] == 'o')
        {
            continue;
        }

        // Go to the next player
        iPlayerTurn++;

        // If we go to player three go back to player one
        if (iPlayerTurn == 3) iPlayerTurn = 1;

        // Set the input to the play mat space
        if (iPlayerTurn == 1)
            cSpaces [--iInput] = 'x';
        else
            cSpaces [--iInput] = 'o';

        // Check if the game is over
        if (cSpaces [0] == cSpaces [1] && cSpaces [2] == cSpaces [0])
            break;
        else if (cSpaces [3] == cSpaces [4] && cSpaces [5] == cSpaces [3])
            break;
        else if (cSpaces [6] == cSpaces [7] && cSpaces [8] == cSpaces [6])
            break;
        else if (cSpaces [0] == cSpaces [3] && cSpaces [6] == cSpaces [0])
            break;
        else if (cSpaces [1] == cSpaces [4] && cSpaces [7] == cSpaces [1])
            break;
        else if (cSpaces [2] == cSpaces [5] && cSpaces [8] == cSpaces [2])
            break;
        else if (cSpaces [0] == cSpaces [4] && cSpaces [8] == cSpaces [0])
            break;
    }

    // Set the correct player winner
    if (iPlayerTurn == 2) iPlayerTurn = 1;
    else iPlayerTurn = 2;

    // Print a exit message and winner
    cout << "Winner: " << iPlayerTurn << endl << "Thank you for playing CPPProgrammer4L's Tic Tac Toe Game!" << endl;
    getch ();

    // Return from the main function and exit the program
    return 0;
}
Last edited on
As for computer quip: I am not using a tutorial and it run's and compiles fine on dev-c++.

As for CPPProgramer4L: Thanx, UR code does have some bugs but it has shown me some things i have never seen. For ex; bool and the fact u can define your char in the beggining and then use it later and not immediatley after stateing it.

2 questions:

on lines 14,42. Is that all you have to put down to intiate switching between turn's. And how did you state their are only 2 players.

Lastley; lines 12,19. Is that you'r entire loop. That looks so easy. Correct me if im worng. you stated that you continue playing until on of the lines between 54-67 are true? That's pretty sick.

I will study bools and char's more "closley" now!

Thanx, widget360
You should read a tutorial to understand how things work, try going to http://Youtube.com and search "Xoax C++ 0" and watch his tutorials, there are only 49 of them and you will learn a lot.

Question One: Basically yeah, but there is a few more lines in there (Like 47 - 52) that we use for the player turn.

Question Two: My game loop goes from 19 to 68. And it will loop around for ever but I added a few checks at 53 - 67 to see if the game is over, if it is then we break from the loop.

Out of the loop the variable iPlayerTurn is set to the loosing player so on lines 70 - 72 I set the correct winner and then display a quit message and the winner on lines 74 - 76.

If you have any more questions let me know.

P.S: Looking over the code I saw that I didn't add a check for a draw game, but I am sure you will be able to do this if you want, let me know if you can't and I will help.
I haven't looked into this, as I stopped reading when I saw this code:
1
2
3
4
5
6
char question[] = "Player 1, Enter your name: ";
char greeting[] = "Hello: ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!";

Please consider using the string class and getline() for this.

Your code structuring doesn't seem to make any sense to me, either. It seems like you enter the main function, define functions in them (but you still put semicolons behind them) and then hope it works. Or you use functions you did not show us, but then, what are the data specifying prefixes for? Mind telling us what you expect this code to do, per line?
Topic archived. No new replies allowed.