C++ implementation of Tic-Tac-Toe game

The name of the pictureThe name of the pictureThe name of the pictureClash 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);







share|improve this question



























    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);







    share|improve this question























      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);







      share|improve this question













      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);









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 3 at 16:16
























      asked Apr 3 at 15:01









      coder

      911425




      911425




















          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:



          1. 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 than std::vector< std::vector<char> >::size_type.

          2. 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 the using statement rather than all instances of std::vector< std::vector<char> >.





          share|improve this answer






























            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?






            share|improve this answer





















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              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






























              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:



              1. 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 than std::vector< std::vector<char> >::size_type.

              2. 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 the using statement rather than all instances of std::vector< std::vector<char> >.





              share|improve this answer



























                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:



                1. 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 than std::vector< std::vector<char> >::size_type.

                2. 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 the using statement rather than all instances of std::vector< std::vector<char> >.





                share|improve this answer

























                  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:



                  1. 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 than std::vector< std::vector<char> >::size_type.

                  2. 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 the using statement rather than all instances of std::vector< std::vector<char> >.





                  share|improve this answer















                  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:



                  1. 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 than std::vector< std::vector<char> >::size_type.

                  2. 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 the using statement rather than all instances of std::vector< std::vector<char> >.






                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Apr 3 at 15:37


























                  answered Apr 3 at 15:31









                  Null

                  8571920




                  8571920






















                      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?






                      share|improve this answer

























                        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?






                        share|improve this answer























                          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?






                          share|improve this answer













                          • 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?







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Apr 3 at 15:53









                          yuri

                          3,3862832




                          3,3862832






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              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













































































                              Popular posts from this blog

                              Chat program with C++ and SFML

                              Function to Return a JSON Like Objects Using VBA Collections and Arrays

                              Will my employers contract hold up in court?