Create a Main Line Algorithm and Subordinate Algorithms to execute a game of tic
ID: 3717628 • Letter: C
Question
Create a Main Line Algorithm and Subordinate Algorithms to execute a game of tic - tac - toe. Your Project is to include the following:
Logic for The Game Itself (15 points)
Accommodations for TWO players (5pts)
Players must be called by name when selecting a square (5pts)
Logic to pass the move from Player One to Player Two in subordinate module
Must receive passed parameter (5pts)
Logic to Draw the Game Board (25 points)
Actual graphical representation of the game board as a subordinate module (15pts)Each line of the board must be fully represented in text - I will not use my imagination to guess my way there
" W H A T E V E R Y O U N E E D T O D O H E R E "
" I S W H A T E V E R Y O U N E E D T O D O H E R E "
Uses array to manage drawing of the board and moves (5pts)
This module must receive passed parameters to draw players chosen moves
Must receive passed parameter (5pts)
Logic to Maintain the Game (25 points)
Subordinate module to ensure that players cannot overwrite another player's selection (5pts)
Ensure that bad selection allows user to recover and continue playing successfully (5pts)
Logic to check for a winner
Must receive passed parameter (5pts)
Logic to check for a draw / tie
Must receive passed parameter (5pts)
Logic to allow a play to continue to play more games as desired (5pts)
Organizational Bits and Pieces (35 points)
Mainline should be mostly free of structure other than to call on subordinate modules (5 pts)
All variables / structures used must be declared in mainline or in subordinate modules (5 pts)
Array must be used to maintain player moves (5 pts)
Defining Diagrams for your pseudocode (10 points)
A hierarchy chart of your program (10 points)
Explanation / Answer
// As there were multiple parts of the question, I have implemented almost all the features
#include <iostream>
#include <cstring>
using namespace std;
int board[3][3]; // our tic tac toe board
string player1; // player1's name
string player2; // player2's name
// this function initiates the game
void init()
{
memset(board, 0, sizeof board); // sets all elements of board to zero
cout<<"Enter player1's name"<<endl;
cin>> player1;
cout<< "Enter player2's name"<<endl;
cin>>player2;
}
// check_error return 0 if coordinates are out of bound
// return 1 if player attempts to play in already played cell
// return 2 if move is valid
int check_error(int row,int col)
{
if(row>3||row<1||col>3||col<1)
return 0;
else if(board[row-1][col-1]!=0)
return 1;
else
return 2;
}
// displays board
void display_board(void)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<board[i][j]<<" ";
cout<<endl;
}
}
// updates the board
void update_board(int row,int col,int turn)
{
board[row-1][col-1]=turn;
display_board();
}
void input(string player,int turn)
{
// keep taking input will exit the loop once player enter's the correct coordinates
while(true)
{
cout<<player<<", Enter row and column to make your move"<<endl;
int row,col;
cin>>row>>col;
int flag= check_error(row,col);
if(flag==0)
cout<<"Invalid coordinates, enter again"<<endl;
else if(flag==1)
cout<<"Cannot make move in this position, it is already used, enter again"<<endl;
else if(flag==2)
{
update_board(row,col,turn);
return;
}
}
}
int check_state(int turn)
{
for(int i=0;i<3;i++)
{
//check for rows
if(turn==board[i][0] && board[i][0]==board[i][1] && board[i][1]==board[i][2])
return 1;
//check for columns
if(turn==board[0][i] && board[0][i]==board[1][i] && board[1][i]==board[2][i])
return 1;
}
//check for diagnols
if(turn==board[0][0]&&board[0][0]==board[1][1] && board[1][1]==board[2][2])
return 1;
if(turn==board[0][2] && board[0][2]==board[1][1] && board[1][1]==board[2][0])
return 1;
// check if game is a draw by checking if there is a 0 or not
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
return 2;
}
}
return 0;
}
int main(void)
{
int continue_playing=1;
while(continue_playing)
{
init();
int turn=1; // tells us about which player has to move
bool winner=false;
//status =0 represents game is drawn
//status =1 represents game is won by somebody
// status=2 game is not won and play can still go on
int status=2;
int name_winner; // stores who is the winner(0 or 1)
while(status==2)
{
if(turn==1)
{
input(player1,turn);
status=check_state(turn);
if(status==1)
name_winner=turn;
turn=2;
}
else if(turn==2)
{
input(player2, turn);
status=check_state(turn);
if(status==1)
name_winner=turn;
turn=1;
}
}
if(status==0)
cout<<"Game Drawn!!"<<endl;
else
{
winner=true;
if(name_winner==1)
cout<<"Winner is 1 i.e. "<<player1<<endl;
else
cout<<"Winner is 2 i.e. "<< player2<<endl;
}
cout<<"Another Game?, enter any number for yes,0 for no";
cin >> continue_playing;
}
return 0;
}