Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // X si 0.cpp : Defines the entry point for the console application.
- //
- #include <iostream>
- using namespace std;
- char matrix[3][3] = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };
- char player = 'X';
- int contor = 0;
- int PlayerX = 0;
- int PLayerO = 0;
- int n;
- // Aceasta functie afiseaza tabla de joc
- void Draw(void)
- {
- system("cls");
- cout << "Tic Tac Toe";
- cout << endl;
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j)
- cout << matrix[i][j] << " | ";
- cout << "\n";
- cout << endl;
- }
- }
- void Input() {
- int choice;
- cout << "Press a number from 1 to 9: ";
- cin >> choice;
- if (choice == 1){
- if (matrix[0][0] == '1'){
- matrix[0][0] = player;
- }
- else {
- cout << "Try again!" << endl;
- Input();
- }
- }
- else if (choice == 2){
- if (matrix[0][1] == '2'){
- matrix[0][1] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 3){
- if (matrix[0][2] == '3'){
- matrix[0][2] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 4){
- if (matrix[1][0] == '4'){
- matrix[1][0] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 5){
- if (matrix[1][1] == '5'){
- matrix[1][1] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 6){
- if (matrix[1][2] == '6'){
- matrix[1][2] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 7){
- if (matrix[2][0] == '7'){
- matrix[2][0] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 8){
- if (matrix[2][1] == '8'){
- matrix[2][1] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- else if (choice == 9){
- if (matrix[2][2] == '9'){
- matrix[2][2] = player;
- }
- else {
- cout << "Try again!"<<endl;
- Input();
- }
- }
- }
- void TogglePlayer() {
- if (player == 'X')
- player = 'O';
- else
- player = 'X';
- }
- char Win() {
- // X won
- if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X') {
- return 'X';
- }
- if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X') {
- return 'X';
- }
- if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X') {
- return 'X';
- }
- if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X') {
- return 'X';
- }
- if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == 'X') {
- return 'X';
- }
- if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X') {
- return 'X';
- }
- if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X') {
- return 'X';
- }
- if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X') {
- return 'X';
- }
- // O won
- if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O') {
- return 'O';
- }
- if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O') {
- return 'O';
- }
- if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O') {
- return 'O';
- }
- if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O') {
- return 'O';
- }
- if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == 'O') {
- return 'O';
- }
- if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O') {
- return 'O';
- }
- if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O') {
- return 'O';
- }
- if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O') {
- return 'O';
- }
- return '/';
- }
- void PlayerTurns (){
- if (player == 'X'){
- cout << " " << "Player X turn!";
- cout << endl;
- }
- else {
- cout << " " << "Player O turn!";
- cout << endl;
- }
- }
- void PlayAgain (){
- matrix[0][0] = '1';
- matrix[0][1] = '2';
- matrix[0][2] = '3';
- matrix[1][0] = '4';
- matrix[1][1] = '5';
- matrix[1][2] = '6';
- matrix[2][0] = '7';
- matrix[2][1] = '8';
- matrix[2][2] = '9';
- contor = 0;
- }
- void AllFunctions (){
- Draw();
- while (1) {
- PlayerTurns();
- ++contor;
- Input();
- Draw();
- TogglePlayer();
- if (Win() == 'X'){
- cout << "X wins" << endl;
- cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
- cin >> n;
- if (n == 1){
- PlayAgain();
- AllFunctions();
- }
- else {
- break;
- }
- }
- if (Win() == 'O'){
- cout << "O wins" << endl;
- cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
- cin >> n;
- if (n == 1){
- PlayAgain();
- AllFunctions();
- }
- else {
- break;
- };
- }
- if (Win() == '/' && contor == 9){
- cout << "Draw" << endl;
- cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
- cin >> n;
- if (n == 1){
- PlayAgain();
- AllFunctions();
- }
- else {
- break;
- }
- }
- }
- }
- int main()
- {
- Draw();
- while (1) {
- PlayerTurns();
- ++contor;
- Input();
- Draw();
- TogglePlayer();
- if (Win() == 'X'){
- ++PLayerX;
- cout << "X wins" << endl;
- cout << "Player X has " << PlayerX << " points" << endl;
- cout << "Player O has " << PlayerO << " points" << endl;
- cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
- cin >> n;
- if (n == 1){
- PlayAgain();
- AllFunctions();
- }
- else {
- break;
- }
- }
- if (Win() == 'O'){
- ++PlayerO;
- cout << "O wins" << endl;
- cout << "Player X has " << PlayerX << " points" << endl;
- cout << "Player O has " << PlayerO << " points" << endl;
- cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
- cin >> n;
- if (n == 1){
- PlayAgain();
- AllFunctions();
- }
- else {
- break;
- };
- }
- if (Win() == '/' && contor == 9){
- cout << "Draw" << endl;
- cout << "Player X has " << PlayerX << " points" << endl;
- cout << "Player O has " << PlayerO << " points" << endl;
- cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
- cin >> n;
- if (n == 1){
- PlayAgain();
- AllFunctions();
- }
- else {
- break;
- }
- }
- }
- cout << "To report bugs contact me on gmail (patrickondreovici2017@gmail.com)";
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement