Hello, I tried the tic-tac-toe exercise and this is what i did, it works the way i wanted it is for 1vs1 and later i will upgrade to be playable with a computer. What I want is you to post your tic-tac-toe codes and/or give me feedback about my game like, what could be better, if it's confuse and bad, what should I improve, etc...
And now try to upgrade it, to play with the computer. (That's the tricky part)
That's the part i will enjoy the most :D
Okay I'll define functions, i don't usually use functions, i guess this is the way i like but not the best way.
i'd suggest having your user select their piece at the beginning and have the program automatically insert it from there on, both to stop them using different pieces accidentally and to save input time.
You could also have it say 'player 1 turn/player 2 turn' in case the users stop playing and come back to it. (or allow them to input their names)
My final suggestion would be to have it draw the game board before the start of play with rows 1,2,3 labelled and columns 1,2,3 labelled, so it's easier for the use to chose their position. Also i would have: -
1 2 3 4
cout << "Row for Piece: ";
cin >> pos1;
cout << "column for Piece";
cin >> pos2;
miinipa I really liked your code so i added a play again function and added and row and column guide for ease of use. can anyone see what they think?
(all credit too miinipa)
@Lostsoulparty
I really apreciate your feedback :)
And i enjoyed your code, I couldn't do the same but at least i understand the code, or almost everything
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
cout << "Welcome to Tic Tac Toe \n" << endl;
string board =" | | \n | | \n ------|--------|------\n | | \n ------|--------|------\n | | \n | | \n";
cout << board;
cout << "Player one, you are X's make your move as follows: tl=top left, tm=top middle, tr=top right, ml=middle left,bl=bottom left etc.\n";
string play;
string tl="tl";
string tm="tm";
string tr="tr";
string ml="ml";
string mm="mm";
string mr="mr";
string bl="bl";
string bm="bm";
string br="br";
string x="x";
string y="o";
bool start=true;
bool playerone=true;
while(start==true)
{
int indexy = board.find(y);
while(playerone==true)
{
cin>>play;
if(play==tl && indexy!=4)
{
board[4]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==tm && indexy!=12)
{
board[12]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==tr && indexy!=20)
{
board[20]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==ml && indexy!=72)
{
board[72]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==mm && indexy!=81)
{
board[81]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==mr && indexy!=88)
{
board[88]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==bl && indexy!=140)
{
board[140]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==bm && indexy!=147)
{
board[147]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
if (play==br && indexy!=154)
{
board[154]=x[0];
cout << board <<"player one it is your turn"<<endl;
playerone=false;
}
}
int index = board.find(x);
while(playerone==false)
{
cin>>play;
if(play==tl && index!=4)
{
board[4]=y[0];
cout << board<<"player one it is your turn"<<endl;
playerone=true;
}
if(play==tm && index!=12)
{
board[12]=y[0];
cout << board<<"player one it is your turn"<<endl;
playerone=true;
}
if (play==tr && index!=20)
{
board[20]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
if (play==ml && index!=72)
{
board[72]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
if (play==mm && index!=81)
{
board[81]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
if (play==mr && index!=88)
{
board[88]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
if (play==bl && index!=140)
{
board[140]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
if (play==bm && index!=147)
{
board[147]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
if (play==br && index!=154)
{
board[154]=y[0];
cout << board <<"player two it is your turn"<<endl;
playerone=true;
}
}
if you want to program tic tac toe and make a possibility for the player to play against the computer, there are 2 solutions:
you get the player input and then switch
this requires kind of a list or array, where your program can see what to do next
example: user inputs "X to 1|1"
then your program switches
switch(input)
{
case 11://reaction of your program
case 12://reaction of your program
//and so on: 13,21,22,23,31,32,33
}
this is the easy way
the difficult way is that you make kind of an AI(artificial intelligence)
the AI thinks over how the program COULD react, and what the user COULD do then
then the AI decides what to do to have the highest chance to win
this is the VERY difficult way