Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- It's a simple Tic Tac Toe game. 2 Players are playing.
- My algorithms is so easy, I have made a global (array) called board and its elements are zeros
- After that if the player1 plays it will set this element to be (1)
- and if player 2 plays element will be (100)
- and the who_winner algorithm:
- will depend on checking the sum of all (horizontal lines, vertical lines , diagonal lines)
- so if the sum equals the (3) then player 1 is the (winner=2)
- if the sum = 300 then player 2 is the winner (winner = 3)
- and if nothing match return winner = 0
- */
- #define YES 1
- #define NO 0
- #include <stdio.h>
- int board[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; //initialize the board with zeros
- int game_end_f = NO; // variable used as a flag
- int winner; // used in the who_winner() function
- void draw_board(int myboard[3][3]); // used for printing the current board
- void player_x(); // used to scan coordinates and put the proper value for player 1
- void player_y(); // the same but for player2
- void check_game(); // check if the game ends and then check (WIN,LOSE,TIE)
- int who_winner(int array[3][3]); // check who is the winner
- int no_places(int array[3][3]); // function used to check if there any left empty cells or not
- void reset(); // reset all the board elements to be zeros
- int x_cord,y_cord; // variables for coordinates
- char playerx_wins=0; // number of wins
- char playery_wins=0;
- int play_again=0;
- int main()
- {
- start: //this is a label to be used by a goto statement
- system("cls"); // clears the screen
- //system("clear"); // for linux :)
- reset(); // reset all
- draw_board(board); // draw the board for the first time
- // while the game is not ended , continue playing :)
- while(game_end_f == NO)
- {
- // if no one is winning continue
- if(winner == 0)
- player_x();
- else
- break;
- draw_board(board);
- // check now the board
- check_game();
- // if there's no winner till now and there are places left ,continue player 2 turn
- if (winner == 0 && !no_places(board))
- {
- player_y();
- draw_board(board);
- check_game();
- }
- else
- break;
- }
- // Do you want to play again ?
- printf("\n\n\tPLAYER1 Vs PLAYER2 : %d - %d",playerx_wins,playery_wins);
- printf("\n\nDo you want to play again [yes = 1] [no = anything else] : ");
- scanf("%d",&play_again);
- if (play_again == 1)
- goto start;
- return 0;
- }
- // this function checks the sate of the game (TIE,WIN,LOSE)
- void check_game()
- {
- if(no_places(board))
- {
- // TIE or WINNER
- game_end_f = YES;
- if ((who_winner(board) == 0))
- printf("TIE!!\n");
- else
- who_winner(board);
- }
- else
- {
- game_end_f = NO;
- }
- who_winner(board);
- // print the result if game ended :) winner 2 >> PLAYER 1 winner 3 >> PLAYER2
- if(winner == 2 )
- {
- printf("WIN: PLAYER 1 (X)\n");
- playerx_wins++;
- }
- else if(winner == 3)
- {
- printf("WIN: PLAYER 2 (O)\n");
- playery_wins++;
- }
- }
- int who_winner(int array[3][3])
- {
- int i;
- int sum;
- winner = 0;
- // check the horozintal
- for(i=0; i<3 ; ++i)
- {
- sum = array[i][0] + array [i][1] + array[i][2];
- if (sum == 3)
- {
- winner = 2;
- return 2;
- }
- else if (sum == 300)
- {
- winner = 3;
- return 3;
- }
- else
- {
- winner = 0;
- }
- }
- // check the vertical
- for(i=0; i<3 ; ++i)
- {
- sum = array[0][i] + array [1][i] + array[2][i];
- if (sum == 3)
- {
- winner = 2;
- return 2;
- }
- else if (sum == 300)
- {
- winner = 3;
- return 3;
- }
- else
- {
- winner = 0;
- }
- }
- // check the diagonal
- sum = array[0][0] + array [1][1] + array[2][2];
- if (sum == 3)
- {
- winner = 2;
- return 2;
- }
- else if (sum == 300)
- {
- winner = 3;
- return 3;
- }
- else
- {
- winner = 0;
- }
- sum = array[0][2] + array[1][1] + array[2][0];
- if (sum == 3)
- {
- winner = 2;
- return 2;
- }
- else if (sum == 300)
- {
- winner = 3;
- return 3;
- }
- else
- {
- winner = 0;
- }
- return 0;
- }
- // draw board function
- void draw_board(int myboard[3][3])
- {
- int i,j;
- system("cls");
- printf("TIC TAC TOE GAME - By Islam Negm \n\n");
- printf("-------------\n");
- for (i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- {
- if (myboard[i][j] == 0)
- printf("| ");
- else if(myboard[i][j] == 1)
- printf("| x ");
- else if(myboard[i][j] == 100)
- printf("| o ");
- }
- printf("|\n");
- printf("-------------\n");
- }
- }
- void player_x()
- {
- // scan coordinates , note that row=1 means the [0] in the array :) , the users are not programmers :D
- printf("PLAYER1 (X):\nEnter the row:");
- scanf("%d",&x_cord);
- printf("Enter the column:");
- scanf("%d",&y_cord);
- // take some decisions before ( if cell is not empty or if the coordinates is not right ) then send message to player
- if( (board[x_cord - 1][y_cord - 1] == 0) && x_cord<=3 && x_cord>=1 && y_cord<=3 && y_cord>=1)
- board[x_cord - 1][y_cord - 1]=1; //
- else
- {
- printf("Wrong Move! Enter the correct coordinates.\n");
- player_x();
- }
- }
- void player_y()
- {
- // scan coordinates , note that row=1 means the [0] in the array :) , the users are not programmers :D
- printf("PLAYER2 (O):\nEnter the row:");
- scanf("%d",&x_cord);
- printf("Enter the column:");
- scanf("%d",&y_cord);
- // take some decisions before ( if cell is not empty or if the coordinates is not right ) then send message to player
- if((board[x_cord - 1][y_cord - 1] == 0) && x_cord<=3 && x_cord>=1 && y_cord<=3 && y_cord>=1)
- board[x_cord - 1][y_cord - 1]=100;
- else
- {
- printf("Wrong Move! Enter the correct coordinates.\n");
- player_y();
- }
- }
- // check if no places lift ( return 0 >>> if still empty places ) (return 1 >> if there aren't any places )
- int no_places(int array[3][3])
- {
- int i,j;
- for (i=0; i<3; ++i)
- {
- for (j=0; j<3; ++j)
- {
- if (array[i][j]==0)
- return 0;
- }
- }
- return 1;
- }
- void reset()
- {
- int i,j;
- //resetting the gaming variables
- game_end_f = NO;
- winner = 0 ;
- //resetting the board array
- for (i=0; i<3; i++)
- {
- for(j=0; j<3; j++)
- {
- board[i][j] = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment