Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- char playground[3][3] =
- {
- {' ', ' ', ' '},
- {' ', ' ', ' '},
- {' ', ' ', ' '}
- };
- void game();
- int check_combo();
- void make_turn(int x, int y, char player);
- void print_playground();
- int main(){
- printf("Wanna play ?(y/n)\n");
- char choice = 'y';
- choice = getchar();
- if((choice == 'y') || (choice == 'Y')) game();
- else return 0;
- }
- void game(){
- char player = 'X';
- int turnscount = 0;
- int x, y;
- system("cls");
- while(1){
- system("cls");
- turnscount++;
- print_playground();
- top : ;
- scanf("%d %d", &x, &y);
- if (!(playground[x][y] == ' ')){ printf("Try another position !\n"); goto top;}
- make_turn(x, y, player);
- if(player == 'X') player = 'O';
- else if(player == 'O') player = 'X';
- if((check_combo())){
- printf("The winner is %c!\n",(player == 'X') ? 'O' : 'X' );
- break;
- }
- if(turnscount > 9){
- printf("Noones ! wp\n");
- break;
- }
- }
- }
- int check_combo(){
- if(((playground[0][0]=='X') && (playground[0][1]=='X') && (playground[0][2]=='X')) ||
- ((playground[0][0]=='O') && (playground[0][1]=='O') && (playground[0][2]=='O'))){
- return 1;
- }
- else if(((playground[1][0]=='X') && (playground[1][1]=='X') && (playground[1][2]=='X')) ||
- ((playground[1][0]=='O') && (playground[1][1]=='O') && (playground[1][2]=='O'))){
- return 1;
- }
- else if(((playground[2][0]=='X') && (playground[2][1]=='X') && (playground[2][2]=='X')) ||
- ((playground[2][0]=='O') && (playground[2][1]=='O') && (playground[2][2]=='O'))){
- return 1;
- }
- else if(((playground[0][0]=='X') && (playground[1][0] == 'X') && (playground[2][0] == 'X'))||
- ((playground[0][0]=='O') && (playground[1][0] == 'O') && (playground[2][0] == 'O'))){
- return 1;
- }
- else if(((playground[0][1]=='X') && (playground[1][1] == 'X') && (playground[2][1] == 'X'))||
- ((playground[0][1]=='O') && (playground[1][1] == 'O') && (playground[2][1] == 'O'))){
- return 1;
- }else if(((playground[0][2]=='X') && (playground[1][2] == 'X') && (playground[2][2] == 'X'))||
- ((playground[0][2]=='O') && (playground[1][2] == 'O') && (playground[2][2] == 'O'))){
- return 1;
- }
- else if(((playground[0][0] == 'X') && (playground[1][1] == 'X') && (playground[2][2] == 'X')) ||
- ((playground[0][0] == 'O') && (playground[1][1] == 'O') && (playground[2][2] == 'O'))){
- return 1;
- }else if(((playground[0][2] == 'X') && (playground[1][1] == 'X') && (playground[2][0] == 'X')) || ((playground[0][2] == 'O') && (playground[1][1] == 'O') && (playground[2][0] == 'O'))){
- return 1;
- }else {
- return 0;
- }
- }
- void make_turn(int x, int y, char player){
- playground[x][y] = player;
- return ;
- }
- void print_playground(){
- for (int i = 0; i < 3; ++i)
- {
- for (int j = 0; j < 3; ++j)
- {
- printf("%c ",playground[i][j] );
- }printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement