I need to create a Tic-Tac-Toe Program in C. It must allow for the the following
ID: 3558634 • Letter: I
Question
I need to create a Tic-Tac-Toe Program in C. It must allow for the the following :1. Two players. They enter their names, which are saved. 2. One uses X, one uses O. (option to allow players to select their own marks) 3. Display the playing board (3x3 array) 4. Players take alternate turns placing their mark. Display whose turn it is. 5. Cannot take another players square. 6. Use a function to check for a winner. 7. Display winner when you have determined that someone has won. 8. Decide how to handle draws (tie). 9. Keep track of games won by each player. 10. Ask to play again. If yes, play again, changing who starts. 11. If no, display won/lost statistics, pause, then exit.
Explanation / Answer
Program Code:
// TicTacToeC.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<iostream>
using namespace std;
void displayBoard();
int verifySelection(int , int);
void checkForWin();
char board[8];
char cWhoWon=' ';
int iCurrentPlayer=0;
int main()
{
int x;
int iSquareNum=0;
for(x=0;x<9;x++)
board[x]=' ';
displayBoard();
while (cWhoWon==' ')
{
printf("%c ", cWhoWon);
if(iCurrentPlayer==0||iCurrentPlayer==1)
{
printf(" Player X ");
printf("Enter an available number(1-9): ");
scanf("%d",&iSquareNum);
if(verifySelection(iSquareNum,iCurrentPlayer)==1)
iCurrentPlayer=1;
else
iCurrentPlayer=2;
}
else
{
printf(" Player O ");
printf("Enter an available square number(1-9): ");
scanf("%d",&iSquareNum);
if(verifySelection(iSquareNum,iCurrentPlayer)==1)
iCurrentPlayer=2;
else
iCurrentPlayer=1;
}
displayBoard();
checkForWin();
}
system("pause");
return 0;
}
void displayBoard()
{
printf(" | ");
printf(" | | ");
printf("%c |%c |%c ",board[0],board[1],board[2]);
printf("--------|-------|------- ");
printf("%c |%c |%c ",board[3],board[4],board[5]);
printf("--------|-------|------- ");
printf("%c |%c |%c ",board[6],board[7],board[8]);
printf(" | | ");
}
int verifySelection(int iSquare,int iPlayer)
{
if(board[iSquare-1]==' ' && (iPlayer==1 || iPlayer==0))
{
board[iSquare-1]='X';
return 0;
}
else if(board[iSquare-1]==' ' && iPlayer==2)
{
board[iSquare-1]='O';
return 0;
}
else
return 1;
}
void checkForWin()
{
int catTotal;
int x;
if(board[0]=='X' && board[1]=='X' && board[2]=='X')
cWhoWon='X';
else if(board[3]=='X' && board[4]=='X' && board[5]=='X')
cWhoWon='X';
else if(board[6]=='X' && board[7]=='X' && board[8]=='X')
cWhoWon='X';
else if(board[0]=='X' && board[3]=='X' && board[6]=='X')
cWhoWon='X';
else if(board[1]=='X' && board[4]=='X' && board[7]=='X')
cWhoWon='X';
else if(board[2]=='X' && board[5]=='X' && board[8]=='X')
cWhoWon='X';
else if(board[0]=='X' && board[4]=='X' && board[8]=='X')
cWhoWon='X';
else if(board[2]=='X' && board[4]=='X' && board[6]=='X')
cWhoWon='X';
else if(board[0]=='O' && board[1]=='O' && board[2]=='O')
cWhoWon='O';
else if(board[3]=='O' && board[4]=='O' && board[5]=='O')
cWhoWon='X';
else if(board[6]=='O' && board[7]=='O' && board[8]=='O')
cWhoWon='O';
else if(board[0]=='O' && board[3]=='O' && board[6]=='O')
cWhoWon='O';
else if(board[1]=='O' && board[4]=='O' && board[7]=='O')
cWhoWon='O';
else if(board[2]=='O' && board[5]=='O' && board[8]=='O')
cWhoWon='O';
else if(board[0]=='O' && board[4]=='O' && board[8]=='O')
cWhoWon='O';
else if(board[2]=='O' && board[4]=='O' && board[6]=='O')
cWhoWon='O';
}
Sample Output:
|
| |
| |
--------|-------|-------
| |
--------|-------|-------
| |
| |
Player O
Enter an available square number(1-9): 1
|
| |
| |
--------|-------|-------
| |
--------|-------|-------
| |?
| |
Player O
Enter an available square number(1-9): 1
|
| |
O | |
--------|-------|-------
| |
--------|-------|-------
| |?
| |
Player X
Enter an available number(1-9): 2
|
| |
O |X |
--------|-------|-------
| |
--------|-------|-------
| |?
| |
Player O
Enter an available square number(1-9):
4
|
| |
O |X |
--------|-------|-------
O | |
--------|-------|-------
| |?
| |
Player X
Enter an available number(1-9): 5
|
| |
O |X |
--------|-------|-------
O |X |
--------|-------|-------
| |?
| |
Player O
Enter an available square number(1-9): 7
|
| |
O |X |
--------|-------|-------
O |X |
--------|-------|-------
O | |?
| |
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.