Sie sind auf Seite 1von 3

/// Tic-Tac-Toe (Ristinolla)

#include <iostream>
#include <string>
#include <Windows.h>
#define board std::cout << "
("<<a<<") |("<<b<<")| ("<<c<<")"<<std::endl;std
::cout << "
___|___|___ "<<std::endl;std::cout << "
("<<d<<") |("<<e<<"
)| ("<<f<<") "<<std::endl;std::cout << "
___|___|___ "<<std::endl;std::cout
<< "
("<<g<<") |("<<h<<")| ("<<i<<") "<<std::endl;
int main ()
{
int neuvo=1, gamenro=1,P1wins=0, P2wins=0;
char xo; // declaritions
std::string choice, player, Pelaaja1, Pelaaja2; // declaritions
SetConsoleTitle(TEXT("Tic-Tac-Toe")); // Naming Console title
std::cout << "Welcome to Tic-Tac-Toe game" << std::endl<< std::endl; //
Welcoming
std::cout << "Enter Player 1 name: ";
std::cin >> Pelaaja1;
std::cout << std::endl << "Enter Player 2 name: ";
std::cin >> Pelaaja2;
start: // defining start location (accessible with goto ; -function
char a=' ',b=' ',c=' ',d=' ',e=' ',f=' ',g=' ',h=' ',i=' ';
float turn=1;
std::cout
std::cout
std::cout
std::cout
std::cout
std::cout

<<
<<
<<
<<
<<
<<

"
"
"
"
"
"

(7 )|(8 )|(9 )"<<std::endl;


___|____|___ "<<std::endl;
(4 )|(5 )|(6 ) "<<std::endl;
___|____|___ "<<std::endl;
(1 )|(2 )|(3 ) "<<std::endl;
|
|
" << std::endl;

for( int gamenrostart=1, choicenro=1,y=1,ox=1, realturn, turn1of2=1; y<=9;gamenr


ostart++,turn1of2++, turn+=0.5,neuvo++, choicenro++, y++, ox++){
valinta:
if (gamenrostart == 1)
std::cout << "Game " << gamenro << std::endl;
realturn= turn;
turn1of2 %= 2;
if (turn1of2 == 1)
{
std::cout << "Round " << realturn<< std::endl << std::endl;
}
if(neuvo<=2)
std::cout << "Make your choice by typing 7, 8, 9, 4, 5, 6, 1, 2
or 3 ( with NumPad). " << std::endl<< std::endl;
ox %= 2;
switch (ox){
case 1:
xo='x'; player = Pelaaja1; break;
case 0:
xo= 'o'; player = Pelaaja2; break;
}

Sleep(500);std::cout << player<< "'s turn: " << std::endl<<std::endl;;


std::cin >> choice;
if (choice != "7" && choice != "8" && choice !="9" && choice !="4" && ch
oice != "5" && choice != "6" && choice !="1" && choice !="2" && choice !="3" )
{std::cout << "You entered invalid number. Please enter your cho
ice again" << std::endl; board; goto valinta;}
if (choice== "7")
a=xo;
if (choice== "8")
b=xo;
if (choice=="9")
c= xo;
if (choice=="4")
d =xo;
if (choice=="5")
e =xo;
if (choice=="6")
f =xo;
if (choice=="1")
g =xo;
if (choice=="2")
h =xo;
if (choice=="3")
i =xo;
board;
if ( b == 'x' && a == 'x' && c == 'x' || d == 'x' && e == 'x'&& f == 'x
' || g == 'x'&& h == 'x'&& i == 'x' )
{std::cout << Pelaaja1<< " won!" << std::endl; P1wins+=1; goto e
nd;}
else if ( a == 'x'&& d == 'x'&& g == 'x' || b == 'x'&& e == 'x'&& h == '
x' || c == 'x'&& f == 'x'&& i== 'x' )
{std::cout << Pelaaja1<< " won!" << std::endl; P1wins+=1; goto e
nd;}
else if ( a == 'x'&& e == 'x'&& i == 'x' || g == 'x'&& e == 'x'&& c == '
x' )
{std::cout << Pelaaja1<< " won!" << std::endl; P1wins+=1; goto end;}
else if ( b == 'o'&& a == 'o'&& c == 'o' || d == 'o'&& e == 'o'&& f == '
o' || g == 'o'&& h == 'o'&& i == 'o' )
{
std::cout << Pelaaja2<< " won!" << std::endl; P2wins+=1;
goto end;}
else if ( a == 'o'&& d == 'o'&& g == 'o' || b == 'o'&& e == 'o'&& h == '
o' || c == 'o'&& f == 'o'&& i== 'o' )
{std::cout << Pelaaja2<< " won!" << std::endl;P2wins+=1; goto en
d;}
else if ( a == 'o'&& e == 'o'&&i == 'o' || g == 'o'&& e == 'o'&& c == 'o
' )
{std::cout << Pelaaja2<< " won!" << std::endl;P2wins+=1; goto en
d;}
}
std::cout << "No one won!" << std::endl;
end:
gamenro++;
std::cout <<std::endl << Pelaaja1 << " has won " << P1wins << " times" << std::e
ndl;
std::cout << Pelaaja2 << " has won " << P2wins << " times" << std::endl << std::
endl;

if (gamenro < 2){


if (gamenro==P1wins )
std::cout << "Sorry " << Pelaaja2 << ", you SUCK!";
if (gamenro==P2wins )
std::cout << "Sorry " << Pelaaja1 << ", you SUCK!";
}
std::string again;
std::cout << "Do you want to play again? Type 'yes' or 'no'." << std::endl;
std::cin >> again;
if (again == "yes")
goto start;
if (again == "no" )
std::cout << "Thank you for using my Tic-Tac-Toe -program!" << std::endl
;
return 0;
return 0;
}

Das könnte Ihnen auch gefallen