I was trying out a C++ code for the Tic Tac Toe program with help of classes . I am new to coding and would appreciate some help.This is what I have done till now.
#include <iostream>
using namespace std;
int turn=1;
class abc
{
private:
int a[9];
char ch1,ch2;
public:
void accept()
{
if(turn==1)
{
cout<<"\n\nPlayer 1 enter choice : ";
cin>>ch1;
turn=2;
}
else
{
cout<<"\n\nPlayer 2 enter choice :";
cin>>ch2;
turn=1;
}
}
void layout()
{
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
a[5]=6;
a[6]=7;
a[7]=8;
a[8]=9;
cout<<"\n\n"<<"-----------"<<" "<<a[0]<<" "<<a[1]<<" "<<a[2]<<" -----------";
cout<<"\n"<<"-----------"<<" "<<a[3]<<" "<<a[5]<<" "<<a[6]<<" -----------";
cout<<"\n"<<"-----------"<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" -----------";
}
void process ()
{
if (turn==1)
{
switch(ch1)
{
case 1:
a[0]='x';
break;
}
}
}
/*void check()
{
if(a[0]==a[1] && a[1]=a[2]
}
*/
};
int main()
{
abc def;
cout<<"-----------TIC - TAC - TOE-----------";
def.layout();
do
{
do
{
def.accept();
def.process();
def.layout();
//def.check();
}while(turn==2);
}while(turn==1);
return 0;
}
So , I can switch from player 1 to player 2 , I intend to use the process function for modifying the layout i.e if the turn 1 player enters 1 then the array should be layout should be modified accordingly . And basically I think I know the logic for the check function i.e a[0]==a[1] && a[1]=a[2] and the other 7 conditions . I just want to know how to use switch case in this condition.
As the comments on your question have implied, your code needs better formatting. However, to answer your question, you could do something along the lines of:
As you can see, you don't really need a switch here. Just use their input to directly access the correct spot in the array.