1. /// Tic-Tac-Toe (Ristinolla)
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <Windows.h>
  6. #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;
  7.  
  8.  
  9. int main ()
  10. {
  11. int neuvo=1, gamenro=1,P1wins=0, P2wins=0;
  12. char xo; // declaritions
  13. std::string choice, player, Pelaaja1, Pelaaja2; // declaritions
  14.  
  15. SetConsoleTitle(TEXT("Tic-Tac-Toe")); // Naming Console title
  16. std::cout << "Welcome to Tic-Tac-Toe game" << std::endl<< std::endl; // Welcoming
  17. std::cout << "Enter Player 1 name: ";
  18. std::cin >> Pelaaja1;
  19. std::cout << std::endl << "Enter Player 2 name: ";
  20. std::cin >> Pelaaja2;
  21.  
  22. start: // defining start location (accessible with goto ; -function
  23. char a=' ',b=' ',c=' ',d=' ',e=' ',f=' ',g=' ',h=' ',i=' ';
  24. float turn=1;
  25.  
  26. std::cout << " (7 )|(8 )|(9 )"<<std::endl;
  27. std::cout << " ___|____|___ "<<std::endl;
  28. std::cout << " (4 )|(5 )|(6 ) "<<std::endl;
  29. std::cout << " ___|____|___ "<<std::endl;
  30. std::cout << " (1 )|(2 )|(3 ) "<<std::endl;
  31. std::cout << " | | " << std::endl;
  32.  
  33.  
  34.  
  35. for( int gamenrostart=1, choicenro=1,y=1,ox=1, realturn, turn1of2=1; y<=9;gamenrostart++,turn1of2++, turn+=0.5,neuvo++, choicenro++, y++, ox++){
  36. valinta:
  37. if (gamenrostart == 1)
  38. std::cout << "Game " << gamenro << std::endl;
  39. realturn= turn;
  40. turn1of2 %= 2;
  41. if (turn1of2 == 1)
  42. {
  43. std::cout << "Round " << realturn<< std::endl << std::endl;
  44. }
  45. if(neuvo<=2)
  46. std::cout << "Make your choice by typing 7, 8, 9, 4, 5, 6, 1, 2 or 3 ( with NumPad). " << std::endl<< std::endl;
  47. ox %= 2;
  48. switch (ox){
  49. case 1:
  50. xo='x'; player = Pelaaja1; break;
  51. case 0:
  52. xo= 'o'; player = Pelaaja2; break;
  53. }
  54.  
  55. Sleep(500);std::cout << player<< "'s turn: " << std::endl<<std::endl;;
  56.  
  57. std::cin >> choice;
  58. if (choice != "7" && choice != "8" && choice !="9" && choice !="4" && choice != "5" && choice != "6" && choice !="1" && choice !="2" && choice !="3" )
  59. {std::cout << "You entered invalid number. Please enter your choice again" << std::endl; board; goto valinta;}
  60. if (choice== "7")
  61. a=xo;
  62. if (choice== "8")
  63. b=xo;
  64. if (choice=="9")
  65. c= xo;
  66. if (choice=="4")
  67. d =xo;
  68. if (choice=="5")
  69. e =xo;
  70. if (choice=="6")
  71. f =xo;
  72. if (choice=="1")
  73. g =xo;
  74. if (choice=="2")
  75. h =xo;
  76. if (choice=="3")
  77. i =xo;
  78. board;
  79. if ( b == 'x' && a == 'x' && c == 'x' || d == 'x' && e == 'x'&& f == 'x' || g == 'x'&& h == 'x'&& i == 'x' )
  80. {std::cout << Pelaaja1<< " won!" << std::endl; P1wins+=1; goto end;}
  81. else if ( a == 'x'&& d == 'x'&& g == 'x' || b == 'x'&& e == 'x'&& h == 'x' || c == 'x'&& f == 'x'&& i== 'x' )
  82. {std::cout << Pelaaja1<< " won!" << std::endl; P1wins+=1; goto end;}
  83. else if ( a == 'x'&& e == 'x'&& i == 'x' || g == 'x'&& e == 'x'&& c == 'x' )
  84. {std::cout << Pelaaja1<< " won!" << std::endl; P1wins+=1; goto end;}
  85.  
  86. else if ( b == 'o'&& a == 'o'&& c == 'o' || d == 'o'&& e == 'o'&& f == 'o' || g == 'o'&& h == 'o'&& i == 'o' )
  87. { std::cout << Pelaaja2<< " won!" << std::endl; P2wins+=1;goto end;}
  88. else if ( a == 'o'&& d == 'o'&& g == 'o' || b == 'o'&& e == 'o'&& h == 'o' || c == 'o'&& f == 'o'&& i== 'o' )
  89. {std::cout << Pelaaja2<< " won!" << std::endl;P2wins+=1; goto end;}
  90. else if ( a == 'o'&& e == 'o'&&i == 'o' || g == 'o'&& e == 'o'&& c == 'o' )
  91. {std::cout << Pelaaja2<< " won!" << std::endl;P2wins+=1; goto end;}
  92.  
  93. }
  94. std::cout << "No one won!" << std::endl;
  95. end:
  96. gamenro++;
  97.  
  98. std::cout <<std::endl << Pelaaja1 << " has won " << P1wins << " times" << std::endl;
  99. std::cout << Pelaaja2 << " has won " << P2wins << " times" << std::endl << std::endl;
  100. if (gamenro < 2){
  101. if (gamenro==P1wins )
  102. std::cout << "Sorry " << Pelaaja2 << ", you SUCK!";
  103. if (gamenro==P2wins )
  104. std::cout << "Sorry " << Pelaaja1 << ", you SUCK!";
  105. }
  106. std::string again;
  107. std::cout << "Do you want to play again? Type 'yes' or 'no'." << std::endl;
  108. std::cin >> again;
  109. if (again == "yes")
  110. goto start;
  111. if (again == "no" )
  112. std::cout << "Thank you for using my Tic-Tac-Toe -program!" << std::endl;
  113. return 0;
  114. return 0;
  115. }