0% found this document useful (0 votes)
13 views

Tris CPP

This C++ program implements a simple tic-tac-toe game. The program uses arrays to store the positions on the board and includes functions to print the board and check for a win or draw. It prompts the user to enter a position for an 'x' and then randomly selects a position to place an 'o'. After each turn it checks if anyone has won or if it is a draw and repeats until there is a winner or a draw.

Uploaded by

Mattia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Tris CPP

This C++ program implements a simple tic-tac-toe game. The program uses arrays to store the positions on the board and includes functions to print the board and check for a win or draw. It prompts the user to enter a position for an 'x' and then randomly selects a position to place an 'o'. After each turn it checks if anyone has won or if it is a draw and repeats until there is a winner or a draw.

Uploaded by

Mattia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

char X[9];

void Print();

bool Vittoria;

bool Pieno;

int main()

srand(time(0));

int p,c;

while(Pieno==false and Vittoria==false)

cout<<"--------- TRIS ---------\n"<<endl;

Print();

cout<<"\nInserisci posizione da 1 a 9: \n"<<endl;

cin>>p;

X[p-1]='x';

if (Pieno==false)

c=rand()%9;

if (X[c]==' ')

while(X[c]!=' ')

c=rand()%9;
}

X[c]='o';

system("cls");

if ( X[0]!=' ' and X[1]!=' ' and X[2]!=' ' and X[3]!=' ' and X[4]!=' ' and X[5]!=' ' and X[6]!=' ' and X[7]!=' ' and
X[8]!=' ')

Pieno=true;

if (

(X[0]=X[1]=X[2]) or

(X[3]=X[4]=X[5]) or

(X[6]=X[7]=X[8]) or

(X[0]=X[3]=X[6]) or

(X[1]=X[4]=X[7]) or

(X[2]=X[5]=X[8]) or

(X[0]=X[4]=X[8]) or

(X[2]=X[4]=X[6])

Vittoria=true;

else{Vittoria=false;}

return 0;

}
void Print()

cout<<" "<<X[0]<<" | "<<X[1]<<" | "<<X[2]<<endl;

cout<<" "<<"---------\n";

cout<<" "<<X[3]<<" | "<<X[4]<<" | "<<X[5]<<endl;

cout<<" "<<"---------\n";

cout<<" "<<X[6]<<" | "<<X[7]<<" | "<<X[8]<<endl;

You might also like