Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab you will design and implement a simple C++ class for representing a position on a rectangular game board. Normally in a board

In this lab you will design and implement a simple C++ class for representing a position on a rectangular game board. Normally in a board game of tic tac toe or reversei you will represent the position of a player's move in INT and reflecting the reality that we use 2D arrays to represent board states. But a position on a board is a type of data which should really be modeled as a class BoardPosition. We can then write methods like ApplyMove which take BoardPosition objects representing where the user wants to go, rather than simple int values (which don't carry the same semantic context as a name like BoardPosition). Using primitive data types in the place of proper objects is something we call a "bad code smell" in software engineering - this particular "smell" is called primitive obsession.

The BoardPosition class will be immutable: once we make a BoardPosition object, it cannot be changed. You will write several operators and member methods that will make manipulating these objects easier than would be possible in a language like java.

Design

Write a class BoardPosition according to the following specification:

1. Instance variable of type char to represent the row and column of the position.

2. A public default constructor, setting row and column to 0.

3. A public constructor taking a row and column as parameters, and initializing the instance variables to those values.

4. Inline, const accessors/ getters for the row and column instance variables, but no mutators/ setters.

5. These operators:

(a) operator std:: string() const, which converts, the position into a string of the format (r,c) Hint: ostringstream, or std::to_string.

(b) Global std::ostream& operator << (std::ostream &lhs, BoardPosition& rhs): prints the string form of rhs to the lhs stream. (Popquiz: why is this parameter not accepted as a const reference?)

(c) Friend std:: istream& operator>>(std::istream &lhs, BoardPosition& rhs): assume the user has entered a string of the form (r,c); extract the row and column from that string and set the row and column instance variables of rhs accordingly. Be sure to "read" the ) character from the istream (dont be rude and leave it behind).4

(d) bool operator ==(BoardPosition rhs): two positions are equal if they have the same row and column.

(e) bool operator

6. These member methods:

(a) bool InBounds(int boardSize): returns true if this position's row and column are in the bounds of a board of the given size. Assume the board's valid coordinates go from 0 up to boardSize - 1.

(b) bool InBounds (int rows, int columns): same as above, except dont assume the board is square.

(c) static std:: vector GetRectangularPositions(int rows, int columns): this method generates and returns a vector of BoardPositions, one object in the vector for each position on a board of size rows x columns, in row-major order. For example, if rows is 3 and columns is 2, this method returns a vector containing the positions (0,0) (0,1) (1,0) (1,1) (2,0) (2,1) in that order. (Note that this is also in increasing order, according to the rules for operator< written above.)

BoardDirection Class

Next, create a class to represent a 1-square movement in shown direction on a game board, called BoardDirection. A direction consists of a "row change" and "column change", indicating the amount of change a row and column position by in order to move 1 square in the given direction.

Create:

1. Instance variable of type char to represent the row direction and column changes.

2. a public default constructor, setting both changes to 0.

3. A public constructor taking a row change and column change as parameters, and initializing the instance variables to those values.

4. Inline, const accessors / getters for the row change and column change instance variables, but not mutators/setters.

5. A public, static variable CARDINAL_DIRECTIONs, which is an std::array containing 8 direction objects for the 8 "cardinal directions." (north, north east, east, south east, south, south west.....) or ( Up-left, up, up-right,....)

Now go back to your boardPosition class and add one final operator:

1.BoardPosition operator+(BoardDirection dir): returns the position arrived at after moving a single square in the given direction.

Any help would be appreciated. Especially how to start because i am relatively new to "classes", yet alone C++ as a whole.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions

Question

Describe somatic symptom and related disorders.

Answered: 1 week ago