Created
October 26, 2019 06:22
-
-
Save paulbarbu/3e4d7ee1712221ff9839fb66c3883aa0 to your computer and use it in GitHub Desktop.
tic-tac-toe-basic for bumblebees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
//TODO: functii (cu si fara globale) | |
//TODO: verificare remiza inainte de a se umple tabla | |
//TODO: verificare mutare valida (in interiorul campului de joc) | |
//TODO: verificare mutare valida (pozitia in campul de joc e libera) | |
//TODO: optimizare verificare castigator pe diagonale intr-o singura conditie (hint: elementul din centru) | |
int main() | |
{ | |
char a[3][3], castigator = '_', jucatorCurent='x'; | |
int rand, col, jocIncheiat=0, nrMutari=0; | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
{ | |
a[i][j] = '_'; | |
} | |
} | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
{ | |
cout << a[i][j] << ' '; | |
} | |
cout << endl; | |
} | |
// atat timp cat nu am gasit castigator si inca nu s-a umplut campul de joc | |
while (castigator == '_' && nrMutari < 9) | |
{ | |
cout << "Player " << jucatorCurent << ": " << endl; | |
cout << "rand = "; | |
cin >> rand; | |
cout << "col = "; | |
cin >> col; | |
a[rand-1][col-1] = jucatorCurent; | |
for (int i = 0; i < 3; i++) | |
{ | |
for (int j = 0; j < 3; j++) | |
{ | |
cout << a[i][j] << ' '; | |
} | |
cout << endl; | |
} | |
//verificare linii | |
int castigPeLinie = 0; | |
for (int i=0; i<3 && !castigPeLinie; i++) | |
{ | |
int toateEgale = 1; | |
for (int j = 0; j < 3; j++) | |
{ | |
if (a[i][0] != a[i][j]) | |
{ | |
toateEgale = 0; | |
break; | |
} | |
} | |
if (toateEgale) | |
{ | |
castigPeLinie = 1; | |
castigator = a[i][0]; | |
} | |
} | |
//verificare coloane | |
if (castigator == '_') | |
{ | |
int castigPeColoana = 0; | |
for (int j = 0; j < 3 && !castigPeColoana; j++) | |
{ | |
int toateEgale = 1; | |
for (int i = 0; i < 3; i++) | |
{ | |
if (a[0][j] != a[i][j]) | |
{ | |
toateEgale = 0; | |
break; | |
} | |
} | |
if (toateEgale) | |
{ | |
castigPeColoana = 1; | |
castigator = a[0][j]; | |
} | |
} | |
} | |
//verificare diagonale | |
if(castigator == '_') | |
{ | |
if (a[0][0] == a[1][1] && a[1][1] == a[2][2]) | |
{ | |
castigator = a[0][0]; | |
} | |
if (a[0][2] == a[1][1] && a[1][1] == a[2][0]) | |
{ | |
castigator = a[0][2]; | |
} | |
} | |
nrMutari++; | |
if (jucatorCurent == 'x') | |
{ | |
jucatorCurent = 'o'; | |
} | |
else | |
{ | |
jucatorCurent = 'x'; | |
} | |
} | |
if (castigator == '_') | |
{ | |
cout << "Jocul s-a terminat, remiza" << endl; | |
} | |
else | |
{ | |
cout << "Jocul s-a terminat, catigator = " << castigator << endl; | |
} | |
cin >> rand; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment