Skip to content

Instantly share code, notes, and snippets.

@starhopp3r
Created October 19, 2021 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starhopp3r/8db2e607839d7c3181c49a5f9216f8e8 to your computer and use it in GitHub Desktop.
Save starhopp3r/8db2e607839d7c3181c49a5f9216f8e8 to your computer and use it in GitHub Desktop.
#include <stdio.h>
// Blocks in map
char blk_1 = ' ';
char blk_2 = ' ';
char blk_3 = ' ';
char blk_4 = ' ';
char blk_5 = ' ';
char blk_6 = ' ';
char blk_7 = ' ';
char blk_8 = ' ';
char blk_9 = ' ';
// Player turn
int turn = 1;
// Tic Tac Toe map
void map(int, int);
// Check winner
int winner(void);
int main(void)
{
// Print welcome message
printf("Welcome to Tic Tac Toe Game!\n");
printf("To indicate your move, please enter a number corresponding to the boxes as shown:\n");
printf("[1][2][3]\n[4][5][6]\n[7][8][9]\n");
// Loop for 9 turns
for (int i = 0; i < 9; i++)
{
int move;
// Get move from player
printf("\nYour move:");
scanf("%d", &move);
// Make a move on the map
map(move, turn);
// Check if a player has won
if (winner())
break;
// Next turn
turn++;
}
return 0;
}
void map(int move, int turn)
{
if (turn % 2 == 1)
{
// Symbol O
if (move == 1)
blk_1 = 'O';
else if (move == 2)
blk_2 = 'O';
else if (move == 3)
blk_3 = 'O';
else if (move == 4)
blk_4 = 'O';
else if (move == 5)
blk_5 = 'O';
else if (move == 6)
blk_6 = 'O';
else if (move == 7)
blk_7 = 'O';
else if (move == 8)
blk_8 = 'O';
else
blk_9 = 'O';
}
else
{
// Symbol X
if (move == 1)
blk_1 = 'X';
else if (move == 2)
blk_2 = 'X';
else if (move == 3)
blk_3 = 'X';
else if (move == 4)
blk_4 = 'X';
else if (move == 5)
blk_5 = 'X';
else if (move == 6)
blk_6 = 'X';
else if (move == 7)
blk_7 = 'X';
else if (move == 8)
blk_8 = 'X';
else
blk_9 = 'X';
}
printf("\n[%c][%c][%c]\n[%c][%c][%c]\n[%c][%c][%c]\n", blk_1, blk_2, blk_3, blk_4, blk_5, blk_6, blk_7, blk_8, blk_9);
}
int winner(void)
{
// Compute row sums
int row_sum_1 = blk_1 + blk_2 + blk_3;
int row_sum_2 = blk_4 + blk_5 + blk_6;
int row_sum_3 = blk_7 + blk_8 + blk_9;
// Compute column sums
int col_sum_1 = blk_1 + blk_4 + blk_7;
int col_sum_2 = blk_2 + blk_5 + blk_8;
int col_sum_3 = blk_3 + blk_6 + blk_9;
// Compute diagonal sums
int dig_sum_1 = blk_1 + blk_5 + blk_9;
int dig_sum_2 = blk_3 + blk_5 + blk_7;
// Check if player 1 or 2 won by checking modulus of row, col. and diag. sums
if (row_sum_1 % 'O' == 0 || row_sum_2 % 'O' == 0 || row_sum_3 % 'O' == 0 || col_sum_1 % 'O' == 0 || col_sum_2 % 'O' == 0 || col_sum_3 % 'O' == 0 || dig_sum_1 % 'O' == 0 || dig_sum_2 % 'O' == 0)
{
printf("Player 1 won!");
return 1;
}
else if (row_sum_1 % 'X' == 0 || row_sum_2 % 'X' == 0 || row_sum_3 % 'X' == 0 || col_sum_1 % 'X' == 0 || col_sum_2 % 'X' == 0 || col_sum_3 % 'X' == 0 || dig_sum_1 % 'X' == 0 || dig_sum_2 % 'X' == 0)
{
printf("Player 2 won!");
return 1;
}
else if (turn == 9)
{
// No sum has modulus equal to zero and all turns are complete
printf("Tie!");
return 1;
}
else
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment