string - Struggling with Tic tac toe C++ -


i'm relatively new coding , have been tasked creating tic tac toe game in c++, thought had code done there various issues such user input not being allowed, , allowing type 1 players name instead of two, help/advice give me on how make work appreciated.

#include <iostream> using namespace std; int menumain; int oneplayer = 'x';  char square1('1'); char square2('2'); char square3('3'); char square4('4'); char square5('5'); char square6('6'); char square7('7'); char square8('8'); char square9('9');  void toggleplayers() {   if (oneplayer == 'x')     oneplayer = 'o';   else     oneplayer = 'x'; }  char win()  {   if (square1 == 'x' && square2 == 'x' && square3 == 'x') return 'x';   if (square4 == 'x' && square5 == 'x' && square6 == 'x') return 'x';   if (square7 == 'x' && square8 == 'x' && square9 == 'x') return 'x';   if (square1 == 'x' && square5 == 'x' && square9 == 'x') return 'x';   if (square3 == 'x' && square5 == 'x' && square7 == 'x') return 'x';   if (square3 == 'x' && square6 == 'x' && square9 == 'x') return 'x';   if (square1 == 'x' && square4 == 'x' && square7 == 'x') return 'x';   if (square2 == 'x' && square5 == 'x' && square8 == 'x') return 'x';   if (square1 == 'o' && square2 == 'o' && square3 == 'o') return 'o';   if (square4 == 'o' && square5 == 'o' && square6 == 'o') return 'o';   if (square7 == 'x' && square8 == 'x' && square9 == 'x') return 'x';   if (square1 == 'o' && square5 == 'o' && square9 == 'o') return 'o';   if (square3 == 'o' && square5 == 'o' && square7 == 'o') return 'o';   if (square3 == 'o' && square6 == 'o' && square9 == 'o') return 'o';   if (square1 == 'o' && square4 == 'o' && square7 == 'o') return 'o';   if (square2 == 'o' && square5 == 'o' && square8 == 'o') return 'o';   return '/'; }  int main() {   int playerone, playertwo;   system("cls");    cout << "tic tac toe" << endl;   cout << "-----------" << endl;   cout << "start game (1)" << endl;   cout << "quit game (2)" << endl;    cout << "press 1 or 2 proceed." << endl;   cin >> menumain;   if (menumain == 2)    {     return 0;   } else {     cout << "player one, please enter name: " << endl;     cin >> playerone;     system("cls");      cout << "player two, please enter name: " << endl;     cin >> playertwo;     system("cls");      int playermove;     cout << "choose number between 1-9 place!" << endl;     cin >> playermove;      if (playermove == 1)       square1 = oneplayer;     else if (playermove == 2)       square2 = oneplayer;     else if (playermove == 3)       square3 = oneplayer;     else if (playermove == 4)       square4 = oneplayer;     else if (playermove == 5)       square5 = oneplayer;     else if (playermove == 6)       square6 = oneplayer;     else if (playermove == 7)       square7 = oneplayer;     else if (playermove == 8)       square8 = oneplayer;     else if (playermove == 9)       square9 = oneplayer;      cout << "tic tac toe!" << endl;      cout << "------------" << endl;      cout << " " << square1 << " | " << square2 << " | " << square3 << " "          << endl;      cout << " " << square4 << " | " << square5 << " | " << square6 << " "          << endl;      cout << " " << square7 << " | " << square8 << " | " << square9 << " "          << endl;      cout << " " << endl;     cout << playerone << endl;     cout << playertwo << endl;      while (1) {       if (win() == 'x') {         cout << playerone << " wins!" << endl;         break;       }       else if (win() == 'o') {         cout << playertwo << " wins!" << endl;         break;       }       toggleplayers();     }      system("pause");     return 0;   } } 

i made quick fix. copy, paste, compile , test. job in terms of game. works. it's tic tac toe. :d. luck further adjustments.

these }; }; }; markers myself since edited in text editor , not ide. suggestion, add clause draw. because not if there no winner.

  #include <iostream>   using namespace std;   int menumain;   int oneplayer = 'x';     char square1('1');   char square2('2');   char square3('3');   char square4('4');   char square5('5');   char square6('6');   char square7('7');   char square8('8');   char square9('9');    void toggleplayers()   {   if (oneplayer == 'x')   oneplayer = 'o';   else   oneplayer = 'x';   };    char win()   {   if (square1 == 'x' && square2 == 'x' &&square3 == 'x')   return 'x';   if (square4 == 'x' && square5 == 'x' &&square6 == 'x')   return 'x';    if (square7 == 'x' && square8 == 'x' &&square9 == 'x')   return 'x';    if (square1 == 'x' && square5 == 'x' && square9 == 'x')   return 'x';   if (square3 == 'x' && square5 == 'x' &&square7 == 'x')   return 'x';    if (square3 == 'x' && square6 == 'x' &&square9 == 'x')   return 'x';   if (square1 == 'x' && square4 == 'x' &&square7 == 'x')   return 'x';    if (square2 == 'x' && square5 == 'x' &&square8 == 'x')   return 'x';    if (square1 == 'o' && square2 == 'o' &&square3 == 'o')   return 'o';   if (square4 == 'o' && square5 == 'o' &&square6 == 'o')   return 'o';    if (square7 == 'x' && square8 == 'x' &&square9 == 'x')   return 'x';    if (square1 == 'o' && square5 == 'o' && square9 == 'o')   return 'o';   if (square3 == 'o' && square5 == 'o' &&square7 == 'o')   return 'o';    if (square3 == 'o' && square6 == 'o' &&square9 == 'o')   return 'o';   if (square1 == 'o' && square4 == 'o' &&square7 == 'o')   return 'o';    if (square2 == 'o' && square5 == 'o' &&square8 == 'o')   return 'o';    return '/';   };    int main()   {   int playerone, playertwo;     cout << "tic tac toe" << endl;   cout << "-----------" << endl;   cout << "start game (1)" << endl;   cout << "quit game (2)" << endl;    cout << "press 1 or 2 proceed." << endl;   cin >> menumain;   if (menumain == 2)    {   return 0;   }   else   {     cout << "player one, please enter name: " << endl;     // cin >> playerone; create char array or string , ask input;      cout << "player two, please enter name: " << endl;    // cin >> playertwo;  create char array or string , ask input;      cout << "tic tac toe!" << endl;     cout << "------------" << endl;     cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;     cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;     cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<     endl;     cout << " " << endl;    cout << playerone << endl; //here replace playerone char array variable or string;    cout << playertwo << endl; //here replace playerone char array variable or string;             while(1)     {       int playermove;     cout << "choose number between 1-9 place!" << endl;     cin >> playermove;      if (playermove == 1)     square1 = oneplayer;      else if (playermove == 2)     square2 = oneplayer;     else if (playermove == 3)     square3 = oneplayer;      else if (playermove == 4)     square4 = oneplayer;     else if (playermove == 5)     square5 = oneplayer;     else if (playermove == 6)     square6 = oneplayer;      else if (playermove == 7)     square7 = oneplayer;     else if (playermove == 8)     square8 = oneplayer;     else if (playermove == 9)     square9 = oneplayer;      cout << "tic tac toe!" << endl;      cout << "------------" << endl;      cout << " " << square1 << " | " << square2 << " | " << square3 << " " << endl;      cout << " " << square4 << " | " << square5 << " | " << square6 << " " << endl;      cout << " " << square7 << " | " << square8 << " | " << square9 << " " <<     endl;      cout << " " << endl;     cout << playerone << endl;     cout << playertwo << endl;          if (win() == 'x')     {       cout << playerone << " wins!" << endl;         break;     }      else if (win() == 'o')     {       cout << playertwo << " wins!" << endl;         break;       };     toggleplayers();      };         return 0;      };        }; 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -