You're working on a team development project tasked with implementing the game
Ask Expert

Be Prepared For The Toughest Questions

Practice Problems

You're working on a team development project tasked with implementing the game

Background

In this assignment, you will implement a subset of functionality required for Connect 4 gameplay. Background and information about the game can be found in the Connect Four - Wikipedia entry. 

Assignment

You're working on a team development project tasked with implementing the game Connect 4. You have been assigned to implement a subset of functionality required for gameplay. It is important that you understand that you are not implementing the game, but several functions that would be necessary, but not necessarily sufficient, to do so.

Language features introduced in this assignment

Two dimensional arrays on the stack

We can declare a two dimensional array on the stack using the following notation: 

constexpr int kNoRows = 2;

constexpr int kNoCols = 3;

DiskType board[kNoRows][kNoCols];

Would create the following:

?? ?? ??

?? ?? ??

Recall that arrays on the stack must have their size known at compile time. In this case, we make this explicitly clear by stating the dimensions of our arrays using constexpr int values. Notice that the starting values at each location in the array is undefined. Therefore, it is extremely important to initialize each element to a well defined value. For our Connect 4 board, the value is DiskType::kEmpty. We can access the ith row, jth column using subscripting: 

board[i][j]

Static member variables

In struct Board you will notice two declarations with the non-type attribute static. These variables are shared across all instances of the type Board. Instead of accessing them with the dot operator, you will use the scope resolution operator. For instance, you can write:

Board::kBoardWidth

to access kBoardWidth anywhere in your code and

Board::kBoardHeight

to access kBoardHeight. One does not require a board object to ask about these static data members. 

Enumeration types

An enum class (an enumeration) is a simple user-defined type that specifies a set of values (its enumerators) as symbolic constants. The "body" an enumeration is a list of its enumerators. An enumerator can be given a specific representation value. For example, 

enum class DiskType { kPlayer1 = 82, kPlayer2 = 66, kEmpty = 32 };

In this case, DiskType::kPlayer1 is a symbolic constant for 82.

If a representation value is not specified, the compiler will pick one for you. For example,

enum class WinningDirection { kHorizontal, kVertical, kRightDiag, kLeftDiag };

The count will start at zero. Therefore, WinningDirection::kHorizontal becomes a symbolic constant for 0, WinningDirection::kVertical for 1, and so on. Why do we use enumerations in our program? Constants are a great way to give names to values, but enums are helpful when we would like group related constants or have a constant represent more of an idea than a value (and so it’s real value is arbitrary). Enums are additionally helpful when we’d like to represent a set up of alternatives (kHorizontal, kVertical, kRightDiag, kLeftDiag), or distinct values (kPlayer1, kPlayer2, kEmpty). If you're interested in the difference between an enum class and plain vanilla "enum", read Bjarne Stroustrup's FAQ entry on the topic.

Functions you're required to implement

Your team lead has asked you to deliver the following functions: 

void InitBoard(Board& b);

This function will initialize each element of Board object b's board data member with DiskType::kEmpty.

void DropDiskToBoard(Board& b, DiskType disk, int col);

This function will take in a Board object by reference to parameter b. Any member of the enum class DiskTypeis valid to initialize the disk parameter. The column that we are dropping the disk into is col. The disk should "fall" into the unoccupied row in that column. The zeroth row is the "lowest" position in each column. If an invalid column is provided, a std::runtime_error should be thrown. If the column is filled (all rows have a disk), a std::runtime_error should be thrown. 

For instance, if we drop an DiskType::kPlayer1 into column 6, we would observe the following:

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |    R    |

 |---------------------------------------------------------------------|

      0         1         2         3         4         5         6

If we drop another DiskType::kPlayer1 into column 6, we would observe the following: 

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |         |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |    R    |

 |---------------------------------------------------------------------|

 |         |         |         |         |         |         |    R    |

 |---------------------------------------------------------------------|

      0         1         2         3         4         5         6

Notice that the first drop places the disk into board.b[0][6] and the second into board.b[1][6]. 

bool CheckForWinner(Board& b, DiskType disk);

This function will invoke SearchForWinner on Board& b for DiskType disk for each member of the enum class WinningDirection. If SearchForWinner returns true for any WinningDirection, then this function will return true to communicate that the disk is a winner.

bool SearchForWinner(Board& b, DiskType disk, WinningDirection to_check)

This function does the heavy lifting for CheckForWinner. Given a Board& b and DiskType disk you will check whether the disk has four in a row anywhere on the board in the direction specified by WinningDirection to_check. That is, if WinningDirection to_check is 

WinningDirection::kHorizontal, check whether DiskType disk has four in a row in the horizontal direction;

WinningDirection::kVertical, check whether DiskType disk has four in a row in the vertical direction;

WinningDirection::kLeftDiag, check whether DiskType disk has four in a row in the left diagonal direction; OR

        |---------------------------------------------------------------------|

        |         |         |         |         |         |         |         |

        |---------------------------------------------------------------------|

        |         |         |         |    R    |         |         |         |

        |---------------------------------------------------------------------|

        |         |         |         |    B    |    R    |         |         |

        |---------------------------------------------------------------------|

        |         |         |         |    B    |    B    |    R    |         |

        |---------------------------------------------------------------------|

        |         |         |         |    B    |    B    |    B    |    R    |

        |---------------------------------------------------------------------|

             0         1         2         3         4         5         6  

WinningDirection::kRightDiag, check whether DiskType disk has four in a row in the right diagonal direction, ...

        |---------------------------------------------------------------------|

        |         |         |         |         |         |         |         |

        |---------------------------------------------------------------------|

        |         |         |         |    R    |         |         |         |

        |---------------------------------------------------------------------|

        |         |         |    R    |    B    |         |         |         |

        |---------------------------------------------------------------------|

        |         |    R    |    B    |    B    |         |         |         |

        |---------------------------------------------------------------------|

        |    R    |    B    |    B    |    B    |         |         |         |

        |---------------------------------------------------------------------|

             0         1         2         3         4         5         6  

... return true if four in a row for DiskType disk are observed anywhere on Board& b, and return falseotherwise.

bool BoardLocationInBounds(int row, int col);

This function will return true if the arguments to int row and int col are within the bounds of Board::kBoardHeight and Board::kBoardWidth respectively and both greater than or equal to 0; return falseotherwise.

Only the following header files are allowed to be #included in your solution files:"board.hpp" "cassert" "cctype" "iomanip" "iostream" "sstream" "stdexcept" "string"

board.cc

Hint
ComputerObject: In object-oriented programming (OOP), an object  is an abstract data type which is created by a developer. It usually include the multiple properties and methods and could even contain several other objects. Also, in most of the programming languages, objects are known as the classes....

Know the process

Students succeed in their courses by connecting and communicating with
an expert until they receive help on their questions

1
img

Submit Question

Post project within your desired price and deadline.

2
img

Tutor Is Assigned

A quality expert with the ability to solve your project will be assigned.

3
img

Receive Help

Check order history for updates. An email as a notification will be sent.

img
Unable to find what you’re looking for?

Consult our trusted tutors.

Developed by Versioning Solutions.