Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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 << " \tTic Tac Toe ";

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;

}

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

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions