Help with this CS Project I\'m using Visual Studio OOP, operator overloading, co
ID: 3852824 • Letter: H
Question
Help with this CS Project
I'm using Visual Studio
OOP, operator overloading, constructors (copy constructor, default constructor etc.), destructors, ‘this’ pointer, friendship relation, and static variables.
String Class implementation. DO NOT USE/INCLUDE string type (e.g. #include <string>)
Objective: Learning design and use of object oriented programming, operator overloading, constructors (copy constructor, default constructor etc.), destructors, ‘this’ pointer, friendship relation, and static variables.
Input: String or strings (e.g. s1+s2 will return concatenation of strings s1 and s2)
Output: Required operations’ outputs e.g. s1>s2 returns true or false, s1[2] returns third char in string s1, s1.Length() returns length of s1 etc.
Project Description: You are required to create your own String class (Note: String class is different than built in string class) like string type in C++. This class should include following operations:
(10 Points) Separate specifications from implementation. Keep header file and class implementation in separate files.
(20 Points) Can initialize created object with a given string or given instance of the same class (use copy constructor)
(10 Points) Dynamically allocates memory for a given string (e.g. String s(“Joe”); in this case allocates 3 memory spaces for String class instance s)
(10Points) Returns string length if it is requested (e.g. s.Length())
(10 Points) Clears dynamically allocated memory for String object while destructing (do it in destructor)
(20 Points) Checks boundaries (e.g. if String s size is 10, s[11]= ‘A’; will display error message on screen)
(20 Points) In public section of the String class only include interfaces. All other variables and functions must be in private section.
(30 Points) Overloads + operator so that two string can be concatenated. (e.g. String s1,s2,s3; s3 = s1+s2; ) when only have s1+s2; concatenated string will be included in s1.
(30 Points) Overloads [ ] operator so that s[index] will be possible.
(30 Points) Compares two given objects (e.g. String a,b) a>b or a<b or a==b. To do that overload <,>,== operators
(30 Points) Copies one object to another (e.g. a=b or a=b=c) (overload ‘=’ operator for this purpose)
(20 Points) When required returns number of objects created so far. (e.g. String obj; obj.count()). Obj.count() returns total number of non-empty String objects).
(30 Points) Can get input from the istream class instance (e.g. cin>>s;). Make it friend of the String class.
(30 Points) Can output to the ostream class instance (e.g. cout<<s;). Make it friend of the String class.
You may use below given class definition as a reference. Complete code for IntArray class
class IntArray
{ private:
int *aptr; // Pointer to the array int arraySize; // Holds the array size void subscriptError(); // Handles invalid subscripts public:
IntArray(int); // Constructor
IntArray(const IntArray &); // Copy constructor
~IntArray(); // Destructor
int size() const // Returns the array size { return arraySize; }
int &operator[](const int &); // Overloaded [] operator friend ostream& operator <<(ostream& stream,IntArray ob); };
Explanation / Answer
The required code is as follows for the TicTacToe game in C# using the Windows Form. Hence , the code uses all the techniques of coding that you require to learn like satic varibale use , constructors ,etc. Hence, I found out that this was the perfect match for the coding requirements and hence, I posted this code as below:
TTT.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TTT
{
public partial class Form1 : Form
{
bool isChance = true; // Boolean value to know whose turn is it
int ChanceCnt = 0; // To count the chances played in the game.
public Form1()
{
InitializeComponent();
}
// To make the input in the grid
private void button_click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (isChance)
{
btn.Text = "X";
btn.BackColor = Color.Red;
}
else
{
btn.Text = "O";
btn.BackColor = Color.Black;
}
isChance = !isChance;
btn.Enabled = false;
ChanceCnt++;
Victorious(); // Calling the function to know who wins the game
}
private void Victorious()
{
bool Winners = false; // To initialize with no winners
// Conditions for winning the game
if ((R1.Text == R2.Text) && (R2.Text == R3.Text) && (!R1.Enabled))
Winners = true;
else if ((S1.Text == S2.Text) && (S2.Text == S3.Text) && (!S1.Enabled))
Winners = true;
else if ((T1.Text == T2.Text) && (T2.Text == T3.Text) && (!T1.Enabled))
Winners = true;
else if ((R1.Text == S1.Text) && (S1.Text == T1.Text) && (!R1.Enabled))
Winners = true;
else if ((R2.Text == S2.Text) && (S2.Text == T2.Text) && (!R2.Enabled))
Winners = true;
else if ((R3.Text == S3.Text) && (S3.Text == T3.Text) && (!R3.Enabled))
Winners = true;
else if ((R1.Text == S2.Text) && (S2.Text == T3.Text) && (!R1.Enabled))
Winners = true;
else if ((R3.Text == S2.Text) && (S2.Text == T1.Text) && (!T2.Enabled))
Winners = true;
if (Winners)
{
disBtn(); // To disable the buttons call the function on winning
string wins = "";
if (isChance) //Deciding which side wins and displaying it.
wins = "O";
else
wins = "X";
MessageBox.Show(wins + " You Win!", "TicTacToe Game");
}
else // Condition for the Game Draw
{
if (ChanceCnt == 9)
MessageBox.Show("Game Draws!", "TicTacToe Game");
}
}
private void disBtn() // To select the options on the screen whether to start New Game or Exit.
{
foreach (Control cont in Controls)
{
Button btn = (Button)cont;
if (btn.Text != "New Game" && btn.Text != "Exit")
{
btn.Enabled = false;
}
}
}
private void btnClose_Click(object sender, EventArgs e) // For Exitting the Application
{
Application.Exit();
}
private void btnNG_Click(object sender, EventArgs e) // To start the new game
{
isChance = true;
ChanceCnt = 0;
foreach (Control cont in Controls)
{
Button btn = (Button)cont;
if(btn.Text!= "New Game" && btn.Text != "Exit")
{
btn.Enabled = true;
btn.Text = "";
btn.BackColor = System.Drawing.Color.White;
}
}
}
}
}
Please rate the answer if it helped.....Thankyou
Hope it helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.