Gaara Gaara - 25 days ago 11
C++ Question

Struggling with Tic tac toe C++

I'm relatively new to coding and have been tasked with creating a tic tac toe game in C++, I thought I had all the code done but there are various issues such as user input not being allowed, and only allowing to type one players name instead of two, any help/advice you could give me on how to make this work would be really 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 to proceed." << endl;
cin >> menumain;
if (menumain == 2)

{
return 0;
} else {
cout << "Player One, please enter your name: " << endl;
cin >> playerone;
system("cls");

cout << "Player Two, please enter your name: " << endl;
cin >> playertwo;
system("cls");

int playermove;
cout << "Choose a number between 1-9 to 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;
}
}

Answer

I made a quick fix. copy, paste, compile and test. it will do the job in terms of a game. It works. It's tic tac toe. :D. Good luck with further adjustments.

These }; }; }; are just markers for myself since I edited this in a text editor and not an IDE. My suggestion, add a clause for a Draw. Because It will not do anything if there is 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 to proceed." << endl;
  cin >> menumain;
  if (menumain == 2)

  {
  return 0;
  }
  else
  {

   cout << "Player One, please enter your name: " << endl;
    // cin >> playerone; create char array or string and ask for an input;


   cout << "Player Two, please enter your name: " << endl;
   // cin >> playertwo;  create char array or string and ask for an 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 with char array variable or string;
   cout << playertwo << endl; //here replace playerone with char array variable or string;








    while(1)
    {


    int playermove;
    cout << "Choose a number between 1-9 to 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;
     };


     };