C++ implementation of Tic-Tac-Toe game
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
I have written this simple code for Tic Tac Toe.
#include <iostream>
#include <vector>
void draw_board(const std::vector< std::vector<char> >& vec)
std::cout << " 0 1 2 n";
std::cout << " +---+---+---+n";
for (int i = 0; i < vec.size(); i++)
std::cout << i << " " ;
for (int j = 0; j < vec[i].size(); j++)
std::cout << "
std::cout << "
void enter(int row, int col, char ch, std::vector< std::vector<char> >& vec)
vec[row][col] = ch;
bool check(const std::vector< std::vector<char> >& vec)
(vec[0][2] == 'X' && vec[1][1] == 'X' && vec[2][0] == 'X'))
std::cout << "Player X won this gamen";
return true;
else if ((vec[0][0] == 'O' && vec[1][1] == 'O' && vec[2][2] == 'O')
void start(std::vector< std::vector<char> >& vec)
int row, col, res = 0;
char ch;
std::vector<int> index = 0, 1, 2;
for (int i = 0; i < vec.size(); i++)
for (int j = 0; j < vec[i].size(); j++)
vec[i][j] = ' ';
for (int i = 0; i < 9;)
if (res == 0)
std::cout << "This game drawsn";
int main()
std::vector< std::vector<char> > board(3, std::vector<char>(3));
draw_board(board);
start(board);
c++ tic-tac-toe
add a comment |Â
up vote
5
down vote
favorite
I have written this simple code for Tic Tac Toe.
#include <iostream>
#include <vector>
void draw_board(const std::vector< std::vector<char> >& vec)
std::cout << " 0 1 2 n";
std::cout << " +---+---+---+n";
for (int i = 0; i < vec.size(); i++)
std::cout << i << " " ;
for (int j = 0; j < vec[i].size(); j++)
std::cout << "
std::cout << "
void enter(int row, int col, char ch, std::vector< std::vector<char> >& vec)
vec[row][col] = ch;
bool check(const std::vector< std::vector<char> >& vec)
(vec[0][2] == 'X' && vec[1][1] == 'X' && vec[2][0] == 'X'))
std::cout << "Player X won this gamen";
return true;
else if ((vec[0][0] == 'O' && vec[1][1] == 'O' && vec[2][2] == 'O')
void start(std::vector< std::vector<char> >& vec)
int row, col, res = 0;
char ch;
std::vector<int> index = 0, 1, 2;
for (int i = 0; i < vec.size(); i++)
for (int j = 0; j < vec[i].size(); j++)
vec[i][j] = ' ';
for (int i = 0; i < 9;)
if (res == 0)
std::cout << "This game drawsn";
int main()
std::vector< std::vector<char> > board(3, std::vector<char>(3));
draw_board(board);
start(board);
c++ tic-tac-toe
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have written this simple code for Tic Tac Toe.
#include <iostream>
#include <vector>
void draw_board(const std::vector< std::vector<char> >& vec)
std::cout << " 0 1 2 n";
std::cout << " +---+---+---+n";
for (int i = 0; i < vec.size(); i++)
std::cout << i << " " ;
for (int j = 0; j < vec[i].size(); j++)
std::cout << "
std::cout << "
void enter(int row, int col, char ch, std::vector< std::vector<char> >& vec)
vec[row][col] = ch;
bool check(const std::vector< std::vector<char> >& vec)
(vec[0][2] == 'X' && vec[1][1] == 'X' && vec[2][0] == 'X'))
std::cout << "Player X won this gamen";
return true;
else if ((vec[0][0] == 'O' && vec[1][1] == 'O' && vec[2][2] == 'O')
void start(std::vector< std::vector<char> >& vec)
int row, col, res = 0;
char ch;
std::vector<int> index = 0, 1, 2;
for (int i = 0; i < vec.size(); i++)
for (int j = 0; j < vec[i].size(); j++)
vec[i][j] = ' ';
for (int i = 0; i < 9;)
if (res == 0)
std::cout << "This game drawsn";
int main()
std::vector< std::vector<char> > board(3, std::vector<char>(3));
draw_board(board);
start(board);
c++ tic-tac-toe
I have written this simple code for Tic Tac Toe.
#include <iostream>
#include <vector>
void draw_board(const std::vector< std::vector<char> >& vec)
std::cout << " 0 1 2 n";
std::cout << " +---+---+---+n";
for (int i = 0; i < vec.size(); i++)
std::cout << i << " " ;
for (int j = 0; j < vec[i].size(); j++)
std::cout << "
std::cout << "
void enter(int row, int col, char ch, std::vector< std::vector<char> >& vec)
vec[row][col] = ch;
bool check(const std::vector< std::vector<char> >& vec)
(vec[0][2] == 'X' && vec[1][1] == 'X' && vec[2][0] == 'X'))
std::cout << "Player X won this gamen";
return true;
else if ((vec[0][0] == 'O' && vec[1][1] == 'O' && vec[2][2] == 'O')
void start(std::vector< std::vector<char> >& vec)
int row, col, res = 0;
char ch;
std::vector<int> index = 0, 1, 2;
for (int i = 0; i < vec.size(); i++)
for (int j = 0; j < vec[i].size(); j++)
vec[i][j] = ' ';
for (int i = 0; i < 9;)
if (res == 0)
std::cout << "This game drawsn";
int main()
std::vector< std::vector<char> > board(3, std::vector<char>(3));
draw_board(board);
start(board);
c++ tic-tac-toe
edited Apr 3 at 16:16
asked Apr 3 at 15:01
coder
911425
911425
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
Check your inputs in enter()
You don't check that the user entered a valid vector index. For example, if I enter a 3
as the row and/or column number the program crashes. You should reject an input if the user enters an invalid row and/or column number (like you do if the user selects a space that is already taken).
All control paths of check()
should return a value
You only ever return true
in check()
. If none of the check conditions are true then you should explicitly return false
at the end of the function.
Use the correct type for indexing
You have for loops like for (int i = 0; i < vec.size(); i++)
, which compare the signed integer i
to the unsigned integer vec.size()
. Use std::vector< std::vector<char> >::size_type
.
Use a type alias / type definition for the std::vector< std::vector<char> >
container
Use a type alias like using container = std::vector< std::vector<char> >;
if your compiler supports it, otherwise use a type definition like typedef std::vector< std::vector<char> > container;
. You can put it after the #include
statements so you can use it for all your functions. This is useful for a couple of reasons:
- It's a shorter name for the container. This comes in handy when referring to the container and/or its
size_type
, for example:container::size_type
is shorter and easier to read thanstd::vector< std::vector<char> >::size_type
. - It's easier to change the container. You may decide later to use a
std::array
instead, in which case you only need to modify theusing
statement rather than all instances ofstd::vector< std::vector<char> >
.
add a comment |Â
up vote
4
down vote
Don't declare more than one variable per line
You can skip your initialization code for the vector and just do it when you declare it:
std::vector< std::vector<char> > board(3, std::vector<char>(3, ' '));
You confused rows and columns
You don't return
false
at the end of your check function which makes the game end after 3 turns.index
does not seem to be used?
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Check your inputs in enter()
You don't check that the user entered a valid vector index. For example, if I enter a 3
as the row and/or column number the program crashes. You should reject an input if the user enters an invalid row and/or column number (like you do if the user selects a space that is already taken).
All control paths of check()
should return a value
You only ever return true
in check()
. If none of the check conditions are true then you should explicitly return false
at the end of the function.
Use the correct type for indexing
You have for loops like for (int i = 0; i < vec.size(); i++)
, which compare the signed integer i
to the unsigned integer vec.size()
. Use std::vector< std::vector<char> >::size_type
.
Use a type alias / type definition for the std::vector< std::vector<char> >
container
Use a type alias like using container = std::vector< std::vector<char> >;
if your compiler supports it, otherwise use a type definition like typedef std::vector< std::vector<char> > container;
. You can put it after the #include
statements so you can use it for all your functions. This is useful for a couple of reasons:
- It's a shorter name for the container. This comes in handy when referring to the container and/or its
size_type
, for example:container::size_type
is shorter and easier to read thanstd::vector< std::vector<char> >::size_type
. - It's easier to change the container. You may decide later to use a
std::array
instead, in which case you only need to modify theusing
statement rather than all instances ofstd::vector< std::vector<char> >
.
add a comment |Â
up vote
4
down vote
Check your inputs in enter()
You don't check that the user entered a valid vector index. For example, if I enter a 3
as the row and/or column number the program crashes. You should reject an input if the user enters an invalid row and/or column number (like you do if the user selects a space that is already taken).
All control paths of check()
should return a value
You only ever return true
in check()
. If none of the check conditions are true then you should explicitly return false
at the end of the function.
Use the correct type for indexing
You have for loops like for (int i = 0; i < vec.size(); i++)
, which compare the signed integer i
to the unsigned integer vec.size()
. Use std::vector< std::vector<char> >::size_type
.
Use a type alias / type definition for the std::vector< std::vector<char> >
container
Use a type alias like using container = std::vector< std::vector<char> >;
if your compiler supports it, otherwise use a type definition like typedef std::vector< std::vector<char> > container;
. You can put it after the #include
statements so you can use it for all your functions. This is useful for a couple of reasons:
- It's a shorter name for the container. This comes in handy when referring to the container and/or its
size_type
, for example:container::size_type
is shorter and easier to read thanstd::vector< std::vector<char> >::size_type
. - It's easier to change the container. You may decide later to use a
std::array
instead, in which case you only need to modify theusing
statement rather than all instances ofstd::vector< std::vector<char> >
.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Check your inputs in enter()
You don't check that the user entered a valid vector index. For example, if I enter a 3
as the row and/or column number the program crashes. You should reject an input if the user enters an invalid row and/or column number (like you do if the user selects a space that is already taken).
All control paths of check()
should return a value
You only ever return true
in check()
. If none of the check conditions are true then you should explicitly return false
at the end of the function.
Use the correct type for indexing
You have for loops like for (int i = 0; i < vec.size(); i++)
, which compare the signed integer i
to the unsigned integer vec.size()
. Use std::vector< std::vector<char> >::size_type
.
Use a type alias / type definition for the std::vector< std::vector<char> >
container
Use a type alias like using container = std::vector< std::vector<char> >;
if your compiler supports it, otherwise use a type definition like typedef std::vector< std::vector<char> > container;
. You can put it after the #include
statements so you can use it for all your functions. This is useful for a couple of reasons:
- It's a shorter name for the container. This comes in handy when referring to the container and/or its
size_type
, for example:container::size_type
is shorter and easier to read thanstd::vector< std::vector<char> >::size_type
. - It's easier to change the container. You may decide later to use a
std::array
instead, in which case you only need to modify theusing
statement rather than all instances ofstd::vector< std::vector<char> >
.
Check your inputs in enter()
You don't check that the user entered a valid vector index. For example, if I enter a 3
as the row and/or column number the program crashes. You should reject an input if the user enters an invalid row and/or column number (like you do if the user selects a space that is already taken).
All control paths of check()
should return a value
You only ever return true
in check()
. If none of the check conditions are true then you should explicitly return false
at the end of the function.
Use the correct type for indexing
You have for loops like for (int i = 0; i < vec.size(); i++)
, which compare the signed integer i
to the unsigned integer vec.size()
. Use std::vector< std::vector<char> >::size_type
.
Use a type alias / type definition for the std::vector< std::vector<char> >
container
Use a type alias like using container = std::vector< std::vector<char> >;
if your compiler supports it, otherwise use a type definition like typedef std::vector< std::vector<char> > container;
. You can put it after the #include
statements so you can use it for all your functions. This is useful for a couple of reasons:
- It's a shorter name for the container. This comes in handy when referring to the container and/or its
size_type
, for example:container::size_type
is shorter and easier to read thanstd::vector< std::vector<char> >::size_type
. - It's easier to change the container. You may decide later to use a
std::array
instead, in which case you only need to modify theusing
statement rather than all instances ofstd::vector< std::vector<char> >
.
edited Apr 3 at 15:37
answered Apr 3 at 15:31
Null
8571920
8571920
add a comment |Â
add a comment |Â
up vote
4
down vote
Don't declare more than one variable per line
You can skip your initialization code for the vector and just do it when you declare it:
std::vector< std::vector<char> > board(3, std::vector<char>(3, ' '));
You confused rows and columns
You don't return
false
at the end of your check function which makes the game end after 3 turns.index
does not seem to be used?
add a comment |Â
up vote
4
down vote
Don't declare more than one variable per line
You can skip your initialization code for the vector and just do it when you declare it:
std::vector< std::vector<char> > board(3, std::vector<char>(3, ' '));
You confused rows and columns
You don't return
false
at the end of your check function which makes the game end after 3 turns.index
does not seem to be used?
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Don't declare more than one variable per line
You can skip your initialization code for the vector and just do it when you declare it:
std::vector< std::vector<char> > board(3, std::vector<char>(3, ' '));
You confused rows and columns
You don't return
false
at the end of your check function which makes the game end after 3 turns.index
does not seem to be used?
Don't declare more than one variable per line
You can skip your initialization code for the vector and just do it when you declare it:
std::vector< std::vector<char> > board(3, std::vector<char>(3, ' '));
You confused rows and columns
You don't return
false
at the end of your check function which makes the game end after 3 turns.index
does not seem to be used?
answered Apr 3 at 15:53
yuri
3,3862832
3,3862832
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191171%2fc-implementation-of-tic-tac-toe-game%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password