Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- char bd[3][3] = { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
- static int temp1 = 0, temp2 = 0;
- void board(char bd[][3],int size)
- {
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- printf("%c ", bd[i][j]);
- if (j < 2)
- printf(" | ");
- }
- printf("\n");
- if (i < 2)
- printf("--------------------\n");
- }
- }
- int player(int a,int c, char bd[][3])
- {
- if (bd[a][c] == 'x' || bd[a][c] == 'o')
- {
- printf("Incorrect move...Try again\n");
- scanf_s("%d %d", &a, &c);
- return player(a, c, bd);
- }
- else
- bd[a][c] = 'x';
- return 0;
- }
- void computer(int a,int c, char bd[][3])
- {
- srand(time(NULL));
- if (bd[a][c] == 'x' || bd[a][c] == 'o')
- {
- while (bd[a][c] == 'x' || bd[a][c] == 'o')
- {
- a = rand() % 3;
- c = rand() % 3;
- }if (bd[a][c] != 'x' || bd[a][c] != 'o')
- bd[a][c] = 'o';
- }
- else
- bd[a][c] = 'o';
- }
- int result(char bd[][3])
- {
- int w = 0;
- // Diagnoal
- if (bd[0][0] == bd[1][1] && bd[0][0] == bd[2][2])
- w++;
- if (bd[0][2] == bd[1][1] && bd[0][2] == bd[2][0])
- w++;
- //Lines
- for (int line = 0; line <= 2; line++)
- {
- if (bd[line][0] == bd[line][1] && bd[line][0] == bd[line][2])
- w++;
- if (bd[0][line] == bd[1][line] && bd[0][line] == bd[2][line])
- w++;
- }
- return w;
- }
- void final(int temp1, int temp2)
- {
- printf("%d wins and %d draws\n", temp1, temp2);
- }
- void moves(char bd[][3],char *name)
- {
- int w = 3, s = 3;
- for (int i = 0; i<3; i++)
- {
- for (int j = 2; j >= 0; j--)
- {
- if ('x' == bd[i][i] || 'x' == bd[i][j])
- w--;
- }
- }
- for (int line = 0; line < 3; line++)
- {
- for (int k = 0; k < 3; k++)
- if ('x' == bd[k][line] || 'x' == bd[line][k])
- s--;
- }
- if (w == 0 || s == 0)
- {
- printf("Congrats %s !\n", name);
- temp1++;
- }
- else
- {
- printf("It's a draw %s !\n", name);
- temp2++;
- }
- }
- void main()
- {
- char name[26];
- printf("What's your name?\n");
- scanf_s("%s", name, 26);
- char *p = name;
- printf("Ok %s you will play a game of Tic-Tac-Toe with the computer.\n", name);
- board(bd, 3);
- int a, c;
- do
- {
- printf("Choose the coordinates in which you like to input X:\n");
- scanf_s("%d%d", &a, &c);
- player(a, c, bd);
- computer(a, c, bd);
- board(bd, 3);
- } while (result(bd) <= 0);
- printf("The game has ended!\n");
- moves(bd,name);
- printf("Would you like to play again ?\nIf yes enter 'y', else enter 'n'\n");
- char answer;
- scanf_s(" %c", &answer);
- int m = 1;
- if (answer == 'y')
- {
- while (answer == 'y')
- {
- char bd[3][3] = { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
- board(bd,3);
- m++;
- do
- {
- printf("Choose the coordinates in which you like to input X:\n");
- scanf_s("%d%d", &a, &c);
- player(a, c, bd);
- computer(a, c, bd);
- board(bd, 3);
- } while (result(bd) <= 0);
- printf("The game has ended!\n");
- moves(bd,name);
- printf("Would you like to play again ?\nIf yes enter 'y', else enter 'n'\n");
- scanf_s(" %c", &answer);
- }
- if (answer == 'n')
- {
- printf("Thank you %s for playing\n", name);
- printf("You played %d times\n", m);
- final(temp1,temp2);
- }
- }
- else
- {
- printf("Thank you %s for playing\n", name);
- printf("You played %d times\n", m);
- final(temp1,temp2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement