Created
January 7, 2022 22:17
-
-
Save 5cotts/3ca70c09daf35b2a22ef2e58871c23dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Tic Tac Toe | |
Write a program that lets two humans play a game of Tic Tac Toe in a terminal. | |
The program should let the players take turns to input their moves. | |
The program should report the outcome of the game. | |
*/ | |
#include <stdio.h> | |
void main() | |
{ | |
int counter, player, go, grid_x, grid_y, line, winner = 0; | |
char empty = ' ', x = 'X', o = 'O'; | |
char board[3][3] = { | |
{empty, empty, empty}, | |
{empty, empty, empty}, | |
{empty, empty, empty}, | |
}; | |
/* Only 9 spaces at the board, so only 9 iterations */ | |
for (counter = 0; counter < 9 && winner == 0; counter++) | |
{ | |
/* Print the board on each iteration */ | |
printf("\n\n\n"); | |
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]); | |
printf("---+---+---\n"); | |
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]); | |
printf("---+---+---\n"); | |
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]); | |
player = (counter % 2) + 1; | |
/* Each coordinate pair on the grid will be assigned a number between 1 and 9 */ | |
do { | |
printf("\nPlayer %d, please enter your go: ", player); | |
scanf("%d", &go); | |
grid_x = --go / 3; | |
grid_y = go % 3; | |
} while ( | |
go < 0 || | |
go > 9 || | |
board[grid_x][grid_y] == x || | |
board[grid_x][grid_y] == o | |
); | |
/* Sets the value in the game board */ | |
board[grid_x][grid_y] = (player == 1) ? x : o; | |
for (line = 0; line <= 2 && winner == 0; line++) | |
{ | |
/* Horizontal win */ | |
if ( | |
board[line][0] != empty && | |
board[line][0] == board[line][1] && | |
board[line][0] == board[line][2] | |
) | |
{ | |
winner = player; | |
} | |
/* Vertical win */ | |
if ( | |
board[0][line] != empty && | |
board[0][line] == board[1][line] && | |
board[0][line] == board[2][line] | |
) | |
{ | |
winner = player; | |
} | |
} | |
/* Checking diagonal wins */ | |
if ( | |
board[0][0] != empty && | |
board[0][0] == board[1][1] && | |
board[0][0] == board[2][2] | |
) | |
{ | |
winner = player; | |
} | |
if ( | |
board[0][2] != empty && | |
board[0][2] == board[1][1] && | |
board[0][2] == board[2][0] | |
) | |
{ | |
winner = player; | |
} | |
} | |
printf("\n\n\n"); | |
printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]); | |
printf("---+---+---\n"); | |
printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]); | |
printf("---+---+---\n"); | |
printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]); | |
if (winner == 0) | |
printf("\nHow boring, it is a draw.\n"); | |
else | |
printf("\nCongrats, player %d. YOU ARE THE WINNER.\n", winner); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment