C++ OOP Summary
Object Oriented Programming (C++)
Course: CSCI 211
Lab: Lab 18
Instructor: Yun Wang
Institution: Queens College
Fancy 8 Queens
The program's core objective is to solve the classic 8 Queens Problem, which involves placing eight chess queens on an chessboard such that no two queens threaten each other. This means no two queens can share the same row, column, or diagonal.
The implementation represents an chessboard visually, with each square being represented by a 2D character array for detailed graphical output.
Black and white boxes (squares on the chessboard) are visually constructed using these 2D character arrays, allowing for a structured and stylized representation of the board.
Typedef in C++
typedefis a C++ keyword that allows programmers to create a new name for an existing data type, effectively creating an alias.Syntax:
typedef existing_type new_type_name;Application in this program: A
typedefis used to defineboxas an alias for achar[5][7]data type. This simplifies code readability and maintenance by providing a clear, descriptive name for the complex 2D character array structure used to represent a single chessboard square.
Chessboard Representation
The datatype
boxis defined astypedef char box[5][7];wherechar[5][7]is a 2D array capable of storing characters for a grid.Two primary
boxinstances are utilized:bb(black box) andwb(white box).Each element in
bbcontains the ASCII character(char)219(a solid block character) to visually represent a black square.Each element in
wbcontains a space character to represent an empty white square.
The actual chessboard,
board, is declared as a 2D array of pointers toboxtype (e.g.,box *board[8][8];). This design choice is critical for memory efficiency because instead of creating 64 individualboxobjects (each being characters), only twoboxobjects (bbandwb) are created.Each element
board[i][j]on the chessboard array stores the address of either thebborwbbox, depending on whether the square at position is black or white, respectively. This minimizes memory usage by pointing multiple board squares to the same twoboxcharacter arrays.
Printing the Board
The board is printed using a series of nested loops: two outer loops iterate through the board squares ( for rows, for columns) and two inner loops iterate through the dimensions of each
box( for box rows, for box columns).Accessing box elements through pointers when printing: The expression
cout << (*board[i][j])[k][l];is used to print each character.board[i][j]retrieves the pointer to abox(either&bbor&wb) stored at the current board position.*board[i][j]dereferences this pointer, yielding the actualboxchar[5][7]array.[k][l]then accesses the specific character within thatboxat row and column .
Sample Output
A representation of a solution for the 8 queens problem, indicating the column position of the queen for each row. For example,
The Holy solution is | 825164signifies a solution where:Queen in row 0 is in column 8 (this seems like a typo, typically columns are 1-8 or 0-7; assuming 1-based, 8 implies out-of-bounds or specific problem encoding)
Queen in row 1 is in column 2
Queen in row 2 is in column 5
Queen in row 3 is in column 1
Queen in row 4 is in column 6
Queen in row 5 is in column 4
(Implicitly, rows 6 and 7 would follow, or the solution is incomplete/truncated).