Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h> /* sets up 2 strings so names can be used */
char oxo[4][4];
void display_grid(void);
/*
=================================================
=================================================
*/
int main(void)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int r1, c1, r0, c0, rx, cx, count;
int name1[20], name2[20]; /* defines name1 & name2 are variables */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
for (r1 = 1; r1 <= 3; r1++)
{
for (c1 = 1; c1 <= 3; c1++)
{
oxo[r1][c1] = '-';
oxo[0][1] = '1'; /**/
oxo[0][2] = '2'; /* prints column numbers */
oxo[0][3] = '3'; /**/
oxo[1][0] = '1'; /**/
oxo[2][0] = '2'; /* prints row numbers */
oxo[3][0] = '3'; /**/
}
}
system("clear"); /* clears the screen */
printf("\n Player 1 what is your name?\n"); /* asks player 1 1s name */
scanf("%s", name1); /* reads name */
printf("\nHello %s you place the '0's & you go first! \n", name1); /* prints out */
printf("\n Player 2 what is your name?\n"); /* asks player 1 2s name */
scanf("%s", name2); /* reads name */
printf("\nHello %s you place the 'X's & you go second!\n", name2); /* prints out */
system("clear"); /* clears screen */
display_grid();
count = 0;
while (count < 9)
{
if (count < 9)
{
while (1)
{ /* start infinite loop, exits when user enters valid cell. */
printf(" %s (0's) enter vertical coordinate: ", name1); /* prints out */
while (scanf("%d", &r0) != 1)
{
printf("%s, 1 number was not entered, please try again\n", name1);
getchar(); /* needed to remove non-numeric character from input stream, I believe */
}
printf(" %s (0's) enter horizontal coordinate: ", name1); /* prints out */
while (scanf("%d", &c0) != 1)
{
printf("%s, 1 number was not entered, please try again\n", name1);
getchar(); /* needed to remove non-numeric character from input stream, I believe */
}
if ((r0 < 1) || (r0 > 3) || (c0 < 1) || (c0 > 3))
{
printf("Out of range grid is 3x3, enter 1-3, vertical then space bar then horizontal co-ordinate, try again\n");
continue; /* go back to top of infinite loop */
}
if (oxo[r0][c0] == 'X' || oxo[r0][c0] == '0')
{
printf("Cell already taken, please try again\n");
continue; /* go back to top of infinite loop */
}
printf("The co-ordinates you have chosen are %d (vertical) and %d (horizontal), are you sure this is the position you would like a '0' to be placed? Y/N\n", r0, c0);
{
int c;
getchar();
c = getchar();
if (c == 'q' || c == 'Q') return (0);
if (c != 'y' && c != 'Y')
continue;
}
break; /* leave the infitite loop */
}
oxo[r0][c0] = '0';
count++;
}
display_grid();
/*
* These lines look to see if there is
* a row of 0s in any of the 8 ways
* possible program looks to see if 0s
* have won
*/
if
(
(oxo[1][1] == '0' && oxo[1][2] == '0' && oxo[1][3] == '0')
|| (oxo[2][1] == '0' && oxo[2][2] == '0' && oxo[2][3] == '0')
|| (oxo[3][1] == '0' && oxo[3][2] == '0' && oxo[3][3] == '0')
|| (oxo[1][1] == '0' && oxo[2][1] == '0' && oxo[3][1] == '0')
|| (oxo[1][2] == '0' && oxo[2][2] == '0' && oxo[3][2] == '0')
|| (oxo[1][3] == '0' && oxo[2][3] == '0' && oxo[3][3] == '0')
|| (oxo[1][1] == '0' && oxo[2][2] == '0' && oxo[3][3] == '0')
|| (oxo[3][1] == '0' && oxo[2][2] == '0' && oxo[1][3] == '0')
)
{
printf("\n GAME OVER...%s WINS.... GAME OVER\n", name1); /* if 0 wins print this */
return; /* if '0' wins end game */
}
else
{ /* if 0 not won program continues */
if (count < 9) /* */
{
while (1)
{ /* start infinite loop, exits when user enters valid cell. */
printf(" %s (X's) enter vertical coordinate: ", name2);
while (scanf("%d", &rx) != 1)
{
printf("%s, 1 number was not entered, please try again\n", name2);
getchar(); /* needed to remove non-numeric character from input stream, I believe */
}
printf(" %s (X's) enter horizontal coordinate: ", name2);
while (scanf("%d", &cx) != 1)
{
printf("%s, 1 number was not entered, please try again\n", name2);
getchar(); /* needed to remove non-numeric character from input stream, I believe */
}
if ((rx < 1) || (rx > 3) || (cx < 1) || (cx > 3))
{
printf("Out of range grid is 3x3, enter 1-3 vertical then space bar then horizontal co-ordinate, try again\n");
continue; /* go back to top of infinite loop */
}
if (oxo[rx][cx] == 'X' || oxo[rx][cx] == '0')
{
printf("Cell already taken\n");
continue; /* go back to top of infinite loop */
}
printf("the co-ordinates you have chosen are %d (vertical) and %d (horizontal), are you sure this is the position you would like a '0' to be placed? Y/N?\n", rx, cx);
{
int c;
getchar();
c = getchar();
if (c == 'q' || c == 'Q') return (0);
if (c != 'y' && c != 'Y')
continue;
}
break; /* leave the infitite loop */
}
oxo[rx][cx] = 'X';
count++;
}
display_grid();
/*
* These lines look to see if
* there is a row of Xs in any
* of the 8 ways possible
* program looks to see if Xs
* have won
*/
if
(
(oxo[1][1] == 'X' && oxo[1][2] == 'X' && oxo[1][3] == 'X')
|| (oxo[2][1] == 'X' && oxo[2][2] == 'X' && oxo[2][3] == 'X')
|| (oxo[3][1] == 'X' && oxo[3][2] == 'X' && oxo[3][3] == 'X')
|| (oxo[1][1] == 'X' && oxo[2][1] == 'X' && oxo[3][1] == 'X')
|| (oxo[1][2] == 'X' && oxo[2][2] == 'X' && oxo[3][2] == 'X')
|| (oxo[1][3] == 'X' && oxo[2][3] == 'X' && oxo[3][3] == 'X')
|| (oxo[1][1] == 'X' && oxo[2][2] == 'X' && oxo[3][3] == 'X')
|| (oxo[3][1] == 'X' && oxo[2][2] == 'X' && oxo[1][3] == 'X')
)
{
printf("\nGAME OVER...%s WINS!!... GAME OVER\n", name2);
/* if X wins print this */
return; /* if X wins end game */
}
}
if (count == 9)
{ /* when 9 moves have been made with no winner game stops */
system("clear");
printf("\nGAME OVER...NO WINNER!!...GAME OVER\n"); /* print information telling users out */
}
}
}
/*
=================================================
=================================================
*/
void display_grid(void)
{
/*~~~~~~~~~*/
int row, col;
/*~~~~~~~~~*/
system ("clear");
for (row = 1; row <= 3; row++)
{
for (col = 1; col <= 3; col++)
{
printf(" %c |", oxo[row][col]); /* prints & spaces vertical g grid lines */
}
printf("\n");
if (row <= 3)
{
printf(" ------------\n"); /* prints & spaces horizontal g grid lines */
}
}
return;
}