Question

Overview: In this course, you will be responsible for completing a number of programming-based assignments by...

Overview: In this course, you will be responsible for completing a number of programming-based assignments by filling in the missing pieces of code. Learning to program in C++ requires developing an understanding of general programming concepts and learning the syntax of the C++ programming language. These exercises will build on each other and help you cultivate you programming knowledge. It is recommended that students do not limit their practice to just that which is graded. The more you write your own code, the more proficient you will become with the tools and techniques available to you in C++. Prompt: Your submission should include your completed source code. The following critical elements should be addressed in your project submission: • Code Description: A brief explanation of the code and a brief discussion of any issues that you encountered while completing the exercise. • Functioning Code: A source code must meet its specifications and behave as desired. To develop proper code, you should produce fully functioning code (produces no errors) that aligns with accompanying annotations. You should write your code in such a way that the submitted file(s) actually executes, even if it does not produce, the correct output. You will be given credit for partially correct output that can actually be viewed and seen to be partially correct. • Code Results: Properly generated results establishes that your source code: A. Generates accurate output B. Produces results that are streamlined, efficient, and error-free • Annotation / Documentation: All added code should also be well commented. This is a practiced “art” that requires striking a balance between commenting everything, which adds a great deal of unneeded noise to the code, and commenting nothing. Well-annotated code requires you to: A. Explain the purpose of lines or sections of your code detailing the approach and method the programmer took to achieve a specific task in the code B. Document any section of code that is producing errors or incorrect results. • Style and Structure: Part of the lesson to be learned in this course is how to write code that is clearly readable and formatted in an organized manner. To achieve this, you should: A. Develop logically organized code that can be modified and maintained; B. Utilize proper syntax, style, and language conventions/best practices Guidelines for Submission: For each exercise, your submission is the completed source code file containing functioning code and should start with a header comment containing a title (name, course, date, project number) and your discussion of the code.

// TicTacToe.cpp: Follow along with the comments to create a fully functional Tic Tac Toe game

// that uses function calls. Each function will get called multiple times during the execution

// of the code, however, the code itself only needed to be written once. Also notice the use of

// an array to store the contents of the board. The comments marked with a TODO denote where code

// needs to be added.

#include "stdafx.h"

#include <iostream>

using namespace std;

char boardTile[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

//Write the function declarations

bool checkValidMove(int);

void drawBoard();

//TODO: Write the declaration for the function that checks for a winner

int main()

{

       int player = 1, i, choice;

       char mark;

       bool isMoveValid = false;

       do

       {

              //TODO: Call the function that draws the game board

              player = (player % 2) ? 1 : 2;

              cout << "Player " << player << ", enter a number: ";

              cin >> choice;

              mark = (player == 1) ? 'X' : 'O';

              //TODO: Call the checkValidMove function, make sure to save the return value in one of the variables

              if (isMoveValid){

                     boardTile[choice] = mark;

              }

              else{

                     cout << "Invalid move ";

                     player--;

                     cin.ignore();

                     cin.get();

              }

              i = checkForWinner();

              player++;

       } while (i == -1);

       drawBoard();

       if (i == 1)

              cout << "==>Player " << --player << " wins!";

       else

              cout << "==>Game draw";

       cin.ignore();

       cin.get();

       return 0;

}

// Check the board for a winner.

// Returning a -1 is keep playing

// Returning a 0 is a draw (or cat wins)

// Returning a 1 shows a winner

int checkForWinner()

{

       //TODO: Read through the code in this function. Based on the commented rules before the function, determine

       //what type of return statement belongs in each of the comments below.

       if ((boardTile[1] == boardTile[2] && boardTile[2] == boardTile[3])

              || (boardTile[4] == boardTile[5] && boardTile[5] == boardTile[6])

              || (boardTile[7] == boardTile[8] && boardTile[8] == boardTile[9])

              || (boardTile[1] == boardTile[4] && boardTile[4] == boardTile[7])

              || (boardTile[2] == boardTile[5] && boardTile[5] == boardTile[8])

              || (boardTile[3] == boardTile[6] && boardTile[6] == boardTile[9])

              || (boardTile[1] == boardTile[5] && boardTile[5] == boardTile[9])

              || (boardTile[3] == boardTile[5] && boardTile[5] == boardTile[7]))

       {

              //Insert return statement

       }

       else if (boardTile[1] != '1' && boardTile[2] != '2' && boardTile[3] != '3'

              && boardTile[4] != '4' && boardTile[5] != '5' && boardTile[6] != '6'

              && boardTile[7] != '7' && boardTile[8] != '8' && boardTile[9] != '9')

       {

              //Insert return statement

       }

       else

       {

              //Insert return statement

       }

}

// Draw the board with the player marks

void drawBoard()

{

       system("cls");

       cout << "\n\n\tTic Tac Toe\n\n";

       cout << "Player 1 has 'X' - Player 2 has 'O'" << endl << endl;

       cout << endl;

       cout << "     |     |     " << endl;

       cout << " " << boardTile[1] << " | " << boardTile[2] << " | " << boardTile[3] << endl;

       cout << "_____|_____|_____" << endl;

       cout << "     |     |     " << endl;

       cout << " " << boardTile[4] << " | " << boardTile[5] << " | " << boardTile[6] << endl;

       cout << "_____|_____|_____" << endl;

       cout << "     |     |     " << endl;

       cout << " " << boardTile[7] << " | " << boardTile[8] << " | " << boardTile[9] << endl;

       cout << "     |     |     " << endl << endl;

}

//Check if the player's move is valid or if the tile has already been taken

//TODO: Based on the function initiation at the beginning of the program, write the function signature, make sure the variable names are consistent

{

       bool isValid = false;

       char aChar = '0' + choice;

       if (choice > 0 && choice <= 9){

              if (boardTile[choice] == aChar){

                     isValid = true;

              }

       }

       return isValid;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The palce where i added the code i put a cooment statement like

"// this is addeded as per the question requirement"

source code:-

#include "stdafx.h"
#include <iostream>
using namespace std;

char boardTile[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

//Write the function declarations
bool checkValidMove(int);
void drawBoard();
int checkForWinner(); // this was added

//TODO: Write the declaration for the function that checks for a winner



int main()
{
    int player = 1, i, choice;
    char mark;
    bool isMoveValid = false;
    do
    {
        //TODO: Call the function that draws the game board
        drawBoard(); // this is addeded as per the question requirement


        player = (player % 2) ? 1 : 2;
        cout << "Player " << player << ", enter a number: ";
        cin >> choice;
        mark = (player == 1) ? 'X' : 'O';

        //TODO: Call the checkValidMove function, make sure to save the return value in one of the variables
        // this is addeded as per the question requirement
        checkValidMove(choice);
        //bool isMoveValid = checkValidMove;
        cout << "checkValidMove is " << std::boolalpha << checkValidMove; // TEST


        if (isMoveValid) {
            boardTile[choice] = mark;
        }
        else {
            cout << "Invalid move ";
            player--;
            cin.ignore();
            cin.get();
        }

        i = checkForWinner();
        player++;
    } while (i == -1);

    drawBoard();
    if (i == 1)
        cout << "==>Player " << --player << " wins!";
    else
        cout << "==>Game draw";
    cin.ignore();
    cin.get();
    return 0;
}

// Check the board for a winner.
// Returning a -1 is keep playing
// Returning a 0 is a draw (or cat wins)
// Returning a 1 shows a winner
int checkForWinner()
{
    //TODO: Read through the code in this function. Based on the commented rules before the function, determine
    //what type of return statement belongs in each of the comments below.
    if ((boardTile[1] == boardTile[2] && boardTile[2] == boardTile[3])
        || (boardTile[4] == boardTile[5] && boardTile[5] == boardTile[6])
        || (boardTile[7] == boardTile[8] && boardTile[8] == boardTile[9])
        || (boardTile[1] == boardTile[4] && boardTile[4] == boardTile[7])
        || (boardTile[2] == boardTile[5] && boardTile[5] == boardTile[8])
        || (boardTile[3] == boardTile[6] && boardTile[6] == boardTile[9])
        || (boardTile[1] == boardTile[5] && boardTile[5] == boardTile[9])
        || (boardTile[3] == boardTile[5] && boardTile[5] == boardTile[7]))
    {
        //Insert return statement
        return 1; // this is addeded as per the question requirement
    }
    else if (boardTile[1] != '1' && boardTile[2] != '2' && boardTile[3] != '3'
        && boardTile[4] != '4' && boardTile[5] != '5' && boardTile[6] != '6'
        && boardTile[7] != '7' && boardTile[8] != '8' && boardTile[9] != '9')
    {
        //Insert return statement
        return 0; // this is addeded as per the question requirement
    }
    else
    {
        //Insert return statement
        return -1; // this is addeded as per the question requirement
    }
}

// Draw the board with the player marks
// this is addeded as per the question requirement
void drawBoard()
{
    system("cls");
    cout << "\n\n\tTic Tac Toe\n\n";
    cout << "Player 1 has 'X' - Player 2 has 'O'" << endl << endl;
    cout << endl;
    cout << "     |     |     " << endl;
    cout << " " << boardTile[1] << " | " << boardTile[2] << " | " << boardTile[3] << endl;
    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;
    cout << " " << boardTile[4] << " | " << boardTile[5] << " | " << boardTile[6] << endl;
    cout << "_____|_____|_____" << endl;
    cout << "     |     |     " << endl;
    cout << " " << boardTile[7] << " | " << boardTile[8] << " | " << boardTile[9] << endl;
    cout << "     |     |     " << endl << endl;
}

//Check if the player's move is valid or if the tile has already been taken
//TODO: Based on the function initiation at the beginning of the program, write the function signature, make sure the variable names are consistent

bool checkValidMove(int choice) // this is addeded as per the question requirement
{
    bool isValid = false;
    char aChar = '0' + choice;
    cout << "TEST checkValidMove " << choice; // TEST added this
    if (choice > 0 && choice <= 9) {
        if (boardTile[choice] == aChar) {
            isValid = true;
        }
    }

    return isValid;
}
***********************end of code*********************************************

Add a comment
Know the answer?
Add Answer to:
Overview: In this course, you will be responsible for completing a number of programming-based assignments by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe...

    I need help solving this in c++ using visual Studio. For this assignment, you will be filling in missing pieces of code within a program, follow the comments in the code. These comments will describe the missing portion. As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. // TicTacToe.cpp: Follow along with...

  • Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you...

    Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • Hello, I am working on my final and I am stuck right near the end of...

    Hello, I am working on my final and I am stuck right near the end of the the program. I am making a Tic-Tac-Toe game and I can't seem to figure out how to make the program ask if you would like to play again, and keep the same names for the players that just played, keep track of the player that wins, and display the number of wins said player has, after accepting to keep playing. I am wanting...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • There is a problem with thecode. I get the error messages " 'board' was not declared...

    There is a problem with thecode. I get the error messages " 'board' was not declared in this scope", \src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token| \src\TicTacToe.cpp|100|error: expected declaration before '}' token| Here is the code #ifndef TICTACTOE_H #define TICTACTOE_H #include class TicTacToe { private: char board [3][3]; public: TicTacToe () {} void printBoard(); void computerTurn(char ch); bool playerTurn (char ch); char winVerify(); }; ************************************ TicTacToe.cpp #endif // TICTACTOE_H #include "TicTacToe.h" #include #include using namespace std; TicTacToe() { for(int i =...

  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • i have created a program for a game of rock, paper, scissors but i must make...

    i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT