I have been busy working and stuff... I got this done in my spare time.. Its version 1.2:
Code:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
int turn = 1, move = 0, done = 0; // Sets turn to 1, move to 0, and done to 0
char restart;
char name1[25], name2[25];
char board[9] = {'1','2','3','4','5','6','7','8','9'}; // The tic-tac-toe Board
void Playersnames()
{
// Players names
printf("What is 1st player's name: ");
scanf("%s", name1);
printf("\nWhat is 2nd player's name: ");
scanf("%s", name2);
system("cls");
}
void DrawBoard()
{
printf("\n\n");
printf(" %c | %c | %c \n", board[0], board[1], board[2]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[3], board[4], board[5]);
printf(" --------- \n");
printf(" %c | %c | %c \n", board[6], board[7], board[8]);
printf("\n\n");
}// DrawBoard
void Playersturn()
{
if(turn == 1)
{printf("It is %s's turn: ", name1);
scanf("%d", &move);
move--;
if((board[move]!='X')&&(board[move]!='O'))
{board[move]='X';turn=2;}
else
{printf("Invalid Move\n");}
system("cls");
}// player 1
else
{printf("It is %s's turn: ", name2);
scanf("%d", &move);
move--;
if((board[move]!='X')&&(board[move]!='O'))
{board[move]='O';turn=1;}
else
{printf("Invalid move\n");}
system("cls");
} //player 2
} //players turn
void Wincheck()
{
if ((board[0] == 'X' && board[1] == 'X' && board[2] == 'X') ||
(board[3] == 'X' && board[4] == 'X' && board[5] == 'X') ||
(board[6] == 'X' && board[7] == 'X' && board[8] == 'X') ||
(board[0] == 'X' && board[3] == 'X' && board[6] == 'X') ||
(board[1] == 'X' && board[4] == 'X' && board[7] == 'X') ||
(board[2] == 'X' && board[5] == 'X' && board[8] == 'X') ||
(board[0] == 'X' && board[4] == 'X' && board[8] == 'X') ||
(board[2] == 'X' && board[4] == 'X' && board[6] == 'X'))
{printf("%s wins!!!\n", name1);
done = 1;}
if ((board[0] == 'O' && board[1] == 'O' && board[2] == 'O') ||
(board[3] == 'O' && board[4] == 'O' && board[5] == 'O') ||
(board[6] == 'O' && board[7] == 'O' && board[8] == 'O') ||
(board[0] == 'O' && board[3] == 'O' && board[6] == 'O') ||
(board[1] == 'O' && board[4] == 'O' && board[7] == 'O') ||
(board[2] == 'O' && board[5] == 'O' && board[8] == 'O') ||
(board[0] == 'O' && board[4] == 'O' && board[8] == 'O') ||
(board[2] == 'O' && board[4] == 'O' && board[6] == 'O'))
{printf("%s wins!!!\n", name2);
done = 1;}
}
void Restart() // Clears and resets the board and done
{
memset(board, 0, 9);
done = 0;
system("cls");
}
void Playgame ( void )
{
do
{
DrawBoard();
Playersturn();
Wincheck();
} // playgame loop
while (done == 0);
}
int main()
{
printf("\n");
printf("Welcome to Tic-Tac-Toe!!!\n\n");
Playersnames();
do
{
Playgame();
printf("Do you want to play again?");
restart=getch();
Restart();
} // Do loop
while( toupper(restart) == 'Y');
return 0;
}// Int main()
The one thing you noticed, if you ran the code, was once you restarted it doesn't have the numbers on the board. Any ideas on fixing that