Tic Tac Toe

Hello, I tried the tic-tac-toe exercise and this is what i did, it works the way i wanted it is for 1vs1 and later i will upgrade to be playable with a computer. What I want is you to post your tic-tac-toe codes and/or give me feedback about my game like, what could be better, if it's confuse and bad, what should I improve, etc...

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
#include <iostream>
using namespace std;
int main()
{
	char GameBoard[3][3]={{'.','.','.'},{'.','.','.'},{'.','.','.'}};
	int Pos1,Pos2;
	char Player_Symb;
	while(cout << "Play: ",cin>>Player_Symb>>Pos1>>Pos2)
	{
		GameBoard[Pos1][Pos2]=Player_Symb;
		for(int a=0;a<3;a++)
		{
			for(int b=0;b<3;b++)
			{	
				cout<< GameBoard[a][b];
			}
			cout << endl;
		}
		if(GameBoard[0][0]==GameBoard[1][1] && GameBoard[1][1]==GameBoard[2][2] && GameBoard[0][0]!='.')
		{
			cout <<"\n"<< GameBoard[0][0] << " Won!";
		}
		else if(GameBoard[0][2]==GameBoard[1][1] && GameBoard[1][1]==GameBoard[2][0] && GameBoard[0][2]!='.')
		{
			cout <<"\n"<< GameBoard[1][1] << " Won!";
		}
		else
		{
			for(int s=0;s<3;s++)
			{
				if(GameBoard[0][s]==GameBoard[1][s] && GameBoard[1][s]==GameBoard[2][s] && GameBoard[1][s]!='.')
				{
					cout <<"\n"<< GameBoard[0][s] << " Won!";
				}
				else if(GameBoard[s][0]==GameBoard[s][1] && GameBoard[s][1]==GameBoard[s][2] && GameBoard[s][1]!='.')
				{
					cout <<"\n"<< GameBoard[s][1] << " Won!";
				}
			}
		}
	}
	cin.get();
	return 0;
}


Thanks,
Well, gr8 if the code works. But try to define functions in your code, to make it more readable.

Like, a function for displaying the board and such.

And now try to upgrade it, to play with the computer. (That's the tricky part)
And now try to upgrade it, to play with the computer. (That's the tricky part)

That's the part i will enjoy the most :D
Okay I'll define functions, i don't usually use functions, i guess this is the way i like but not the best way.
Can't understand how to play
for example to play a O in the middle do O 1 1
What I want is you to post your tic-tac-toe codes and/or give me feedback about my game like
I'm drunk and cannot give adequate feedback so here is my old Tic-Tac-Toe 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
#include <iostream>

enum fieldelement {O = -1, NONE, X, TIE};

fieldelement checkGameState(fieldelement const board[3][3])
{
    for(int i = 0; i < 3; ++i)
        if ( ((board[i][0]==board[i][1])&&(board[i][0]==board[i][2])) ||
             ((board[0][i]==board[1][i])&&(board[0][i]==board[2][i])) )
            return board[i][i];
    if ( ((board[1][1]==board[0][0])&&(board[1][1]==board[2][2])) ||
         ((board[1][1]==board[0][2])&&(board[1][1]==board[2][0])) )
        return board[1][1];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; ++j)
            if(board[i][j] == NONE)
                return NONE;
    return TIE;
}

char getSymbol(const fieldelement x)
{
    switch(x) {
        case   X : return 'X';
        case   O : return 'O';
        case NONE: return ' ';
        case  TIE: return '@';
        default  : return '!';
    }
}

void printBoard(fieldelement const (&board)[3][3])
{
    using std::cout;
    for(int i = 0; i < 3; ++i) {
        cout << "\n" << "-------------" <<
                "\n" << "|   |   |   |" <<
                "\n" << "| " << getSymbol(board[i][0]) << " | " <<
                getSymbol(board[i][1])<<" | "<<getSymbol(board[i][2]) <<" |" <<
                "\n" << "|   |   |   |";
    }
    cout  <<  "\n"  <<  "-------------" << std::endl;
}

void do_turn(const fieldelement player, fieldelement (&board)[3][3])
{
    std::cout << "Your move '" << getSymbol(player) << "'" << std::endl;
    int x, y;
    while(true) {
        std::cin >> x >> y;
        if(0 < x && x < 4 && 0 < y && y < 4)
            if(board[x-1][y-1] == NONE)
                break;
        std::cout << "Wrong move, try again" << std::endl;
    }
    board[x-1][y-1] = player;
}

void victoryMessage(fieldelement victor)
{
    std::cout << ((victor == TIE)?"The game is a TIE!":
                 (victor == NONE)?"What? How could that happen?":
                    (victor == X)?"Player 'X' wins!":"Player 'O' wins!") <<
                 std::endl;
}

int main()
{
    fieldelement board[3][3] = {{NONE,NONE,NONE},
                                {NONE,NONE,NONE},
                                {NONE,NONE,NONE}};
    fieldelement player = X;
    while(checkGameState(board) == NONE) {
        printBoard(board);
        do_turn(player, board);
        player = (player == X)? O : X ;
    }
    printBoard(board);
    victoryMessage(checkGameState(board));
    return 0;
}
Thank you I'd like to study your code to improve mine, is it okay for you?
Sure thing. If it wasn't okay I would not post it. However there might be thing which you will want to improve. I have wrote this code long ago.
sorry
i'd suggest having your user select their piece at the beginning and have the program automatically insert it from there on, both to stop them using different pieces accidentally and to save input time.
You could also have it say 'player 1 turn/player 2 turn' in case the users stop playing and come back to it. (or allow them to input their names)
My final suggestion would be to have it draw the game board before the start of play with rows 1,2,3 labelled and columns 1,2,3 labelled, so it's easier for the use to chose their position. Also i would have: -
1
2
3
4
cout << "Row for Piece: ";
cin >> pos1;
cout << "column for Piece";
cin >> pos2;


Good working code so far though! keep it up :)
miinipa I really liked your code so i added a play again function and added and row and column guide for ease of use. can anyone see what they think?
(all credit too miinipa)
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
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

enum fieldelement {O = -1, NONE, X, TIE};

fieldelement checkGameState(fieldelement const board[3][3])
{
    for(int i = 0; i < 3; ++i)
        if ( ((board[i][0]==board[i][1])&&(board[i][0]==board[i][2])) ||
             ((board[0][i]==board[1][i])&&(board[0][i]==board[2][i])) )
            return board[i][i];
    if ( ((board[1][1]==board[0][0])&&(board[1][1]==board[2][2])) ||
         ((board[1][1]==board[0][2])&&(board[1][1]==board[2][0])) )
        return board[1][1];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; ++j)
            if(board[i][j] == NONE)
                return NONE;
    return TIE;
}

char getSymbol(const fieldelement x)
{
    switch(x) {
        case   X : return 'X';
        case   O : return 'O';
        case NONE: return ' ';
        case  TIE: return '@';
        default  : return '!';
    }
}

void printBoard(fieldelement const (&board)[3][3])
{
    for(int i = 0; i < 3; ++i) {
        cout << "\n" << "       -------------" <<
                "\n" << "       |   |   |   |" <<
                "\n" << " ROW "<< i+1 <<" | " << getSymbol(board[i][0]) << " | " <<
                getSymbol(board[i][1])<<" | "<<getSymbol(board[i][2]) <<" |" <<
                "\n" << "       |   |   |   |";
    }
    cout  <<  "\n"  <<  "       -------------" << endl 
                    <<  "         1   2   3  " << endl << endl
                    <<  "          COLUMNS   " << endl << endl << endl;
}

void do_turn(const fieldelement player, fieldelement (&board)[3][3])
{
    cout << "\tYour move '" << getSymbol(player) << "'" << std::endl;
    int x, y;
    while(true) {
        cout << "\tRow: ";
        cin >> x;
        cout << "\tColumn: ";
        cin >> y;
        if(0 < x && x < 4 && 0 < y && y < 4)
            if(board[x-1][y-1] == NONE)
                break;
        cout << "Wrong move, try again" << std::endl;
    }
    system("CLS");
    board[x-1][y-1] = player;
}

void victoryMessage(fieldelement victor)
{
    cout << ((victor == TIE)?"The game is a TIE!":
                 (victor == NONE)?"What? How could that happen?":
                    (victor == X)?"Player 'X' wins!":"Player 'O' wins!") <<
                 std::endl;
}

bool rematch() {
     string yOrN;
     cout << "Would you like to play again? (y/n)\n";
     cin >> yOrN;
     if (yOrN == "n" || yOrN == "N") {
        cout << "Goodbye and Thankyou for playing!";
        Sleep(2000);
        return false;         
     } else if (yOrN == "y" || yOrN == "Y") {
        cout << "GoodLuck!\n";
        Sleep(1000);
        system("CLS");
        return true;       
     } else {
        cout << "input invalid.. GoodBye!";
        Sleep(2000);
        return false;       
     }
}

int main()
{
    do {
        fieldelement board[3][3] = {{NONE,NONE,NONE},
                                    {NONE,NONE,NONE},
                                    {NONE,NONE,NONE}};
        fieldelement player = X;
        while(checkGameState(board) == NONE) {
            printBoard(board);
            do_turn(player, board);
            player = (player == X)? O : X ;
        }
        
        printBoard(board);
        victoryMessage(checkGameState(board));
        Sleep(2000);
    } while (rematch());
}
sorry

Why do you apologize me? :)
@Lostsoulparty
I really apreciate your feedback :)
And i enjoyed your code, I couldn't do the same but at least i understand the code, or almost everything
Last edited on
my old code, first program i built so kind of sloppy
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Welcome to Tic Tac Toe \n" << endl;
    string board ="       |        |      \n       |        |   \n ------|--------|------\n       |        |   \n ------|--------|------\n       |        |   \n       |        |   \n";
    cout << board;
    cout << "Player one, you are X's make your move as follows: tl=top left, tm=top middle, tr=top right, ml=middle left,bl=bottom left etc.\n";
    string play;
    string tl="tl";
    string tm="tm";
    string tr="tr";
    string ml="ml";
    string mm="mm";
    string mr="mr";
    string bl="bl";
    string bm="bm";
    string br="br";
    string x="x";
    string y="o";
    bool start=true;
    bool playerone=true;
    while(start==true)
    {
        int indexy = board.find(y);
        while(playerone==true)
        {
            cin>>play;
            if(play==tl && indexy!=4)
            {
                board[4]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==tm && indexy!=12)
            {
                board[12]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==tr && indexy!=20)
            {
                board[20]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==ml && indexy!=72)
            {
                board[72]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
             if (play==mm && indexy!=81)
            {
                board[81]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==mr && indexy!=88)
            {
                board[88]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==bl && indexy!=140)
            {
                board[140]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==bm && indexy!=147)
            {
                board[147]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
            if (play==br && indexy!=154)
            {
                board[154]=x[0];
                cout << board <<"player one it is your turn"<<endl;
                playerone=false;
            }
        }
        int index = board.find(x);
        while(playerone==false)
        {
            cin>>play;
            if(play==tl && index!=4)
            {
                board[4]=y[0];
                cout << board<<"player one it is your turn"<<endl;
                playerone=true;
            }
            if(play==tm && index!=12)
            {
                board[12]=y[0];
                cout << board<<"player one it is your turn"<<endl;
                playerone=true;
            }
            if (play==tr && index!=20)
            {
                board[20]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
            if (play==ml && index!=72)
            {
                board[72]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
             if (play==mm && index!=81)
            {
                board[81]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
            if (play==mr && index!=88)
            {
                board[88]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
            if (play==bl && index!=140)
            {
                board[140]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
            if (play==bm && index!=147)
            {
                board[147]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
            if (play==br && index!=154)
            {
                board[154]=y[0];
                cout << board <<"player two it is your turn"<<endl;
                playerone=true;
            }
        }
My prog was a bit too large to paste in go, But you can see it here:

http://pastebin.com/4XC4SWqJ
if you want to program tic tac toe and make a possibility for the player to play against the computer, there are 2 solutions:

you get the player input and then switch
this requires kind of a list or array, where your program can see what to do next
example: user inputs "X to 1|1"
then your program switches
switch(input)
{
case 11://reaction of your program
case 12://reaction of your program
//and so on: 13,21,22,23,31,32,33
}
this is the easy way

the difficult way is that you make kind of an AI(artificial intelligence)
the AI thinks over how the program COULD react, and what the user COULD do then
then the AI decides what to do to have the highest chance to win
this is the VERY difficult way

practise makes perfect
Last edited on
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 <string>
#include <cstdlib>		//lib for pseudo-random
#include <ctime>		//lib for time to random

char GameBoard[3][3]={{'.','.','.'},{'.','.','.'},{'.','.','.'}};

using namespace std;

/******************************************************************
*             Verify if Player/Computer Won                       *
******************************************************************/
void VictoryConf ()
{
	// Verify diagonals
	if(GameBoard[0][0]==GameBoard[1][1] && GameBoard[1][1]==GameBoard[2][2] && GameBoard[0][0]!='.')
	{
		cout <<"\n"<< GameBoard[0][0] << " Won!"<<endl;
	}
	else if(GameBoard[0][2]==GameBoard[1][1] && GameBoard[1][1]==GameBoard[2][0] && GameBoard[0][2]!='.')
	{
		cout <<"\n"<< GameBoard[1][1] << " Won!"<<endl;
	}
	else
	{
		for(int s=0;s<3;s++)
		{
			// Verify rows and columns
			if(GameBoard[0][s]==GameBoard[1][s] && GameBoard[1][s]==GameBoard[2][s] && GameBoard[1][s]!='.')
			{
				cout <<"\n"<< GameBoard[0][s] << " Won!"<<endl;
			}
			else if(GameBoard[s][0]==GameBoard[s][1] && GameBoard[s][1]==GameBoard[s][2] && GameBoard[s][1]!='.')
			{
				cout <<"\n"<< GameBoard[s][1] << " Won!"<<endl;
			}
		}
	}
}
/********************************************************************
*                Function to print the gameboard                    *
********************************************************************/
void printboard()
{
	for(int a=0;a<3;a++)
	{
		for(int b=0;b<3;b++)
		{	
			cout<< GameBoard[a][b];
		}
		cout << endl;
	}
	cout << endl;
}
/******************
*  Main Function  *
******************/
int main()
{
	cout << "You are the 'O'\n\n\n";
	int Pos1,Pos2;
	char Player_Symb='O',Computer_Symb='X';
	while(cout << "Play(ex:0 1): ",cin>>Pos1>>Pos2,cout<<"\7")		//while diferent from computer play
	{
		GameBoard[Pos1][Pos2]=Player_Symb;
		printboard();
		VictoryConf();
		if(GameBoard[Pos1][Pos2]=='O'||GameBoard[Pos1][Pos2]=='X')		//if there is Player symbol, random a number
		{
			srand(unsigned (time(0)));
			Pos1=rand()%3;
			Pos2=rand()%3;
		}
		GameBoard[Pos1][Pos2]=Computer_Symb;
		printboard();
		VictoryConf();
	}
	cin.get();
	return 0;
}


I added the computer, i want to know what's the best practice, make headers or functions in the main source?
Topic archived. No new replies allowed.