A Proper CS304 Assignment 2 Solution Fall 2021. Easy to See Step by Step CS304 Assignment No 2 Fall 2021 Solution and Download .CPP, WORD, and PDF File Below.
CS304 ASSIGNMENT 2 SOLUTION FALL 2021
Due Date: 2 Jan 2021
Total Marks: 20
Uploading Assignment Instruction:
Submit the assignment in .CPP format only.
Save assignment with your student id (e.g., bc20000000000.cpp)
Assignment Objective:
Creating a Tic-tac-toe Game Code in C++.
CODE Solution:
// ID: ----ENTER YOUR ID----
// Name: ----ENTER YOUR NAME----
#include <iostream>
#include <windows.h>
using namespace std;
class TicTacToe
{
private:
char play[3][3];
public:
TicTacToe();
void makeMove();
void printBoard();
int validMove();
int gameStatus();
void startGame();
};
int TicTacToe::validMove()
{
int move, move1,player = 1, i;
char mark = 'X';
do
{
player = (player%2) ? 1 : 2;
printBoard();
mark=(player == 1) ? 'X' : 'O';
cout << "Player " << mark << " enter move: ";
cin >> move >> move1;
cout<<""<<endl;
mark=(player == 1) ? 'X' : 'O';
if(move == 0 &&move1 == 0&& play[0][0] == ' ')
{
play[0][0] = mark;
}
else if(move == 0 &&move1 == 1 && play[0][1] == ' ')
{
play[0][1] = mark;
}
else if(move == 0 &&move1 == 2 && play[0][2] == ' ')
{
play[0][2] = mark;
}
else if(move == 1&&move1 == 0 && play[1][0] == ' ')
{
play[1][0] = mark;
}
else if(move == 1&&move1 == 1 && play[1][1] == ' ')
{
play[1][1] = mark;
}
else if(move == 1 &&move1 == 2&& play[1][2] == ' ')
{
play[1][2] = mark;
}
else if(move == 2 &&move1 == 0 && play[2][0] == ' ')
{
play[2][0] = mark;
}
else if(move == 2 &&move1 == 1 && play[2][1] == ' ')
{
play[2][1] = mark;
}
else if(move == 2 &&move1 == 2 && play[2][2] == ' ')
{
play[2][2] = mark;
}
else
{
cout<<"Invalid move ";
player--;
cin.ignore();
cin.get();
}
i=gameStatus();
player++;
}while(i==-1);
printBoard();
if(i==1){
--player;
cout<<"\n==>\aPlayer "<<mark<<" win " << endl;
for(int sc = 0; sc < 3; sc++)
{
for(int ar = 0; ar < 3; ar++)
{
play[sc][ar] = ' ';
}
}
}
else
{
cout<<"\n==>\aGame Draw" << endl;
for(int sc = 0; sc < 3; sc++)
{
for(int ar = 0; ar < 3; ar++)
{
play[sc][ar] = ' ';
}
}
}
}
TicTacToe::TicTacToe()
{
for(int sc = 0; sc < 3; sc++)
{
for(int ar = 0; ar < 3; ar++)
{
play[sc][ar] = ' ';
}
}
}
int TicTacToe::gameStatus()
{
if (play[0][0] == play[0][1] && play[0][1] == play[0][2] && play[0][0] != ' ')
return 1;
else if (play[1][0] == play[1][1] && play[1][1] == play[1][2] && play[1][0] != ' ')
return 1;
else if (play[2][0] == play[2][1] && play[2][1] == play[2][2] && play[2][0] != ' ')
return 1;
else if (play[0][0] == play[1][0] && play[1][0] == play[2][0] && play[0][0] != ' ')
return 1;
else if (play[0][1] == play[1][1] && play[1][1] == play[2][1] && play[0][1] != ' ')
return 1;
else if (play[0][2] == play[1][2] && play[1][2] == play[2][2] && play[0][2] != ' ')
return 1;
else if (play[0][0] == play[1][1] && play[1][1] == play[2][2] && play[0][0] != ' ')
return 1;
else if (play[0][2] == play[1][1] && play[1][1] == play[2][0] && play[0][2] != ' ')
return 1;
else if (play[0][0] != ' ' && play[0][1] != ' ' && play[0][2] != ' '
&& play[1][0] != ' ' && play[1][1] != ' ' && play[1][2] != ' '
&& play[2][0] != ' ' && play[2][1] != ' ' && play[2][2] != ' ')
return 0;
else
return -1;
}
void TicTacToe::makeMove()
{
validMove();
}
void TicTacToe::startGame()
{
makeMove();
}
void TicTacToe::printBoard()
{
cout << " 0\t1\t2" << endl << endl;
cout << "0 : : " << endl;
cout << " "<< play[0][0] << " : "<< ""<< play[0][1] << " :" << " "<< play[0][2] << endl;
cout << " _____:_____:_____" << endl;
cout << "1 : :" << endl;
cout << " "<< play[1][0] << " : "<< ""<< play[1][1] << " :" << " "<< play[1][2] << endl;
cout << " _____:_____:_____" << endl;
cout << " : : " << endl;
cout << " "<< play[2][0] << " : "<< ""<< play[2][1] << " :" << " "<< play[2][2] << endl;
cout << "2 : : " << endl << endl;
cout << "\n";
}
int main()
{
TicTacToe myGame;
int run = 1;
char choice;
do
{
myGame.startGame();
cout << "\nDo You Want to Play Again[y/n]: ";
cin >> choice;
cout <<"\n";
if(choice == 'y' || choice == 'Y')
{
run = 1;
}
else
{
run = 0;
}
}while(run == 1);
return 0;
}
OUTPUT
DOWNLOAD CS304 Assignment 2 Solution Fall 2021 Word File
DOWNLOAD CS304 Assignment 2 Solution Fall 2021 .CPP File
DOWNLOAD CS304 Assignment 2 Solution Fall 2021 PDF File
Check Also:
CS304 Midterm Past Papers Mega Files
CS304 Solved Midterm Past Papers by Subjective and objectives
Share with fellows to get easily help in studies.
0 Comments