Microsoft Word - Document2 CSCI 1520 Computer Principles and C++ Programming Game Description Start condition: There are 2 players (player Black and player White) and an empty square game board (N*N)....

1 answer below »
undergraduate year 1 c++ assignment


Microsoft Word - Document2 CSCI 1520 Computer Principles and C++ Programming Game Description Start condition: There are 2 players (player Black and player White) and an empty square game board (N*N). Game stage: The players take turn to place their pieces on empty cells of the board. (Player Black first place one black piece, then White and Black take turns, placing two pieces of their colors to empty positions each turn.) End condition: The player who first connects six or more consecutive pieces of his/her color in a line (horizontally ↔, vertically ↕, or diagonally ⤡⤢) wins the game When the board is full and no player connects six or more, it is a draw game 1 Game Description 2 Start with empty board and Black player places one piece The white player place his first piece in this turn The white player place his second piece in this turn Game Description 3 The Black player place one back piece at C16 Find 6 continuous black pieces => Black wins Ending condition (1) Game Description 4 The Black player place one back piece at F16 No empty space on board and no 6 consecutive White/Black pieces => We have a draw game Ending condition (2) Square board of size N*N where N is a constant integer In each position, “B” means black piece; “W” means white piece; and “.” indicates an empty cell. The rows and columns are named in numbers 0, 1, 2,… and uppercase letters A, B, C, … respectively. Code representation: Board Representation 5 N = 19 Board Representation board[0][0] => top-left board[0][N-1] => top-right board[N-1][0] => bottom-left board[N-1][N-1] => bottom-right When N is 19, the above refers to A0, S0, A18, and S18 respectively. We can simply modify the board size by change a single variable N. 6 Program Flow 7 Print initial empty board and ask black player to place the first piece 7 Program Flow 8 2. Handle B’s inputs When handling the input: (1)If the input is valid, update the board and print it. (2) Else ask for correct input again by printing “Invalid move. Enter again!” until a valid input is entered. A user input is valid if: (a) it is a proper board location within bounds, and (b) the input position is empty. Note that column letters must be uppercase. Lowercase letters are considered invalid. e.g., H14 will be invalid in the rest of this game since it’s been occupied H300 will be invalid since it’s out of the boundary b4 will be invalid since it is in lowercase Recall that N is not always 19 => The right most column / bottom row is not always S and 18 as shown in this case. 8 Program Flow 9 3. Ask white player to place the first piece in this turn, handle W’s input, check (1) if the game hasn’t met the ending condition, continue to step 4; (2) else we end the game and announce result. 9 Program Flow 10 4. Ask white player to place the second piece in this turn, handle W’s input, check (1) if the game hasn’t met the ending condition, continue to step 5; (2) else we end the game and announce result. 10 Program Flow 11 5. Ask black player to place the first piece in this turn, handle B’s input, check (1) if the game hasn’t met the ending condition, continue to step 6; (2) else we end the game and announce result. 11 Program Flow 12 6. Ask black player to place the second piece in this turn, handle B’s input, check (1) if the game hasn’t met the ending condition, we go back to step 3(white player take his turn); (2) else we end the game and announce result. 12 Program Flow 13 Two kinds of ending states: Find vertical, horizontal, and diagonal winning conditions(for 6 or more consecutive Bs or Ws) No empty space on board and no “6 or more consecutive Bs or Ws” If the game is over, print the messages “Black wins!”, “White wins!”, or “Draw game!” accordingly. Hint: For the above (1), after black player placed a black piece, we only need to check whether there are 6 consecutive Bs. Same thing applies to W. (1) (2) 13 Possible Functions(at least 4 including main()) 14 1. Function to initialize the board: Fill the board with ‘.’ 2. A function to print current board 3. A function to check if the game is over now Check current status after “player” just played and return a value to indicate the result … Important notes(1) 15 Read user input: In each move, the input is a character followed by an integer, e.g., A5, B14 How to read them ? Hint: E.g., If we input “K14” and press Enter, col_char will be ‘K’ and digit will be 14. To map chars ‘A-Z’ to integer values 0 – 25: Given a char col_char=‘C’, simply do “int col_int = col_char – ‘A’;” (will get 2 as result) char col_char; int digit; cin >> col_char >> digit ; 16 Important notes(2) 17 Do remember to use const int N = xxx to represent boarder size and to declare the board: Instead of declaring board like “char board[19][19];” We will manually modify the value of N when marking your code. Important notes(3) 18 Printing format for board When printing the left most column which contains row number, leave a single space at left if current row number has 1 digit only. Hint: use setw() from (1) Include header: #include (2) Usage: cout < setw(2)="" ;="" cout="">< 5="">< endl;="" print="" “="" 5”="" cout="">< setw(2)="" ;="" cout="">< 10="">< endl;="" print="" “10”="" reference:="" http://www.cplusplus.com/reference/iomanip/setw/="" important="" notes(4)="" 19="" check="" for="" 6="" or="" more="" consecutive="" b/w="" pieces:="" suppose="" black="" player="" just="" played="" at="" i1="" steps="" to="" check="" around="" i1:="" 1.="" check="" horizontally="" 2.="" check="" vertically(similar)="" 3.="" check="" for="" diagonal(coming="" slides)="" (1)="" record="" #(consecutive="" bs="" starting="" from="" i1="" to="" left)="" as="" count_left="" (i1="" excluded="" itself="" and="" it="" is="" 2="" now)="" (2)="" record="" #(consecutive="" bs="" starting="" from="" i1="" to="" right)="" as="" count_right="" (which="" is="" 1="" now)="" (3)="" count_horizontal="1" +="" count_left="" +="" count_right="4" (4)="" if="" (count_horizontal="">= 6) then Black wins. Important notes(4) 20 Check for 6 or more consecutive B/W pieces: Example: Code to count consecutive Bs horizontally (starting from row r and column c, counting to the left): (For counting to the right, please try it by yourselves since they are essentially the same) Important notes(4) 21 Check for 6 or more consecutive B/W pieces: When checking the diagonal case, don’t forget to check both ⤡⤢ directions. Example: Pseudocode to count consecutive Bs diagonally ⤡ (starting from row r and column c, counting towards upper left direction): Remember to count towards lower right as well to get the total sum. 1. Initialize current position to be row r-1, col c-1 2. While (current position is not out of boundary and piece at current position is black): count_upper_left += 1 Move current position one column left and one row up (row--, col--) 5. End While loop 21 Summary 22 1. Print initial empty board and ask black player to place the first piece 2. Black player place one piece on board 3. White player place the first piece in this turn, handle W’s input, and check game status (may jump to 8 or 9) 4. White player place the second piece in this turn, handle W’s input, and check game status (may jump to 8 or 9) 5. Black player place the first piece in this turn, handle B’s input, and check game status (may jump to 7 or 9) 6. Black player place the second piece in this turn, handle B’s input, and check game status (may jump to 7 or 9) 7. Black player wins 8. White player wins Ending State 9. Draw game 22 Thank You! Q&A 23 23
Answered Same DayApr 16, 2021

Answer To: Microsoft Word - Document2 CSCI 1520 Computer Principles and C++ Programming Game Description Start...

Shivani answered on Apr 19 2021
132 Votes
// Connect6.cpp: Code for game Connect6
#include
#include
using namespace std;
// constant to define size of the board
const
int N = 19;
// structure to store the cell
// for player's current move
struct playerMove
{
    int col;
    int row;
};
// function declarations
void InitializeBoard ( char board[N][N] );
void DisplayBoard ( char board[N][N] );
playerMove validPlayerMove( char board[N][N] );
int checkForConnect6(char currentPlayer, struct playerMove currMove, char board[N][N]);
// main() definition
int main()
{
    // initialize variables
    char board[N][N];
    struct playerMove currentMove;
    char currentPlayer = 'B';
    
    int win = 0;
    int turn=0, first_turn=1;
    
    // set the game's board
    InitializeBoard( board );
    
    while(win==0)
    {
        DisplayBoard(board);
        
        cout << currentPlayer << "'s turn: ";
        
        currentMove = validPlayerMove(board);
        // After this function, col#s are actual array indx
        
        board[currentMove.row][currentMove.col]=currentPlayer;
        turn++;
        
        win=checkForConnect6(currentPlayer, currentMove, board);
        if(win==1)
        {
            DisplayBoard(board);
            cout << endl << ((currentPlayer=='B')?"Black":"White") << " wins!";
            exit(1);
        }
        else if(win == -1)
        {
            DisplayBoard(board);
            cout << endl << "Draw game!";
            exit(1);
        }
        else
        {
            if(first_turn == 1)
            {
                currentPlayer = 'W';
                first_turn=0;
                turn=0;
            }
            else
            {
                if(turn==2)
                {
                    currentPlayer =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here