Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Made 2 days after I started learning C++
- #include <iostream>
- #include <string>
- using namespace std;
- int arr[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
- string najdiSimbolOdBroj(int x) {
- string symbol;
- switch (x) {
- case 1:
- symbol = "X";
- break;
- case 2:
- symbol = "O";
- break;
- default:
- symbol = "?";
- }
- return symbol;
- }
- void printajTabla() {
- cout << "-----------" << endl;
- for (int a=0; a<3; a++) {
- cout << "|";
- for (int b=0; b<3; b++) {
- string symbol = najdiSimbolOdBroj(arr[a][b]);
- cout << " " << symbol << " ";
- }
- cout << "|\n";
- }
- cout << "-----------" << endl;
- }
- int imaKombinacija(int x) {
- return
- ((arr[0][0] == x && arr[0][1] == x && arr[0][2] == x) || // xxx ??? ???
- (arr[1][0] == x && arr[1][1] == x && arr[1][2] == x) || // ??? xxx ???
- (arr[2][0] == x && arr[2][1] == x && arr[2][2] == x) || // ??? ??? xxx
- (arr[0][0] == x && arr[1][0] == x && arr[2][0] == x) || // x?? x?? x??
- (arr[0][1] == x && arr[1][1] == x && arr[2][1] == x) || // ?x? ?x? ?x?
- (arr[0][2] == x && arr[1][2] == x && arr[2][2] == x) || // ??x ??x ??x
- (arr[0][0] == x && arr[1][1] == x && arr[2][2] == x) || // x?? ?x? ??x
- (arr[2][2] == x && arr[1][1] == x && arr[0][0] == x)); // ??x ?x? x??
- }
- int imaPobednik() {
- if (imaKombinacija(1)) {
- return 1;
- } else if (imaKombinacija(2)) {
- return 2;
- } else {
- return 0;
- }
- }
- int smeni(int a, int b, int x) {
- if (arr[a][b] == 0) {
- arr[a][b] = x;
- return 1;
- } else {
- return 0;
- }
- }
- void novRed(int x) {
- int a;
- int b;
- string igrac = najdiSimbolOdBroj(x);
- cout << igrac << "'s turn to enter a row (1, 2, or 3)\n> ";
- cin >> a;
- cout << igrac << "'s turn to enter a column (1, 2 or 3)\n> ";
- cin >> b;
- int uspesno = smeni(a-1, b-1, x);
- if (!uspesno) {
- cout << "That space is already taken, try again." << endl;
- novRed(x);
- }
- }
- void novKrug(int x) {
- printajTabla();
- string igrac = najdiSimbolOdBroj(x);
- novRed(x);
- int pobednik = imaPobednik();
- if (pobednik != 0) {
- cout << igrac << " wins! Bravo!!!";
- } else {
- novKrug(x == 1 ? 2 : 1);
- }
- }
- int main() {
- novKrug(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment