Question

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 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

Screenshot

uick Launch (Ci+a 빈 TicTaeToenCpp Given MicroofWsual Studio Fie Feit w Pmjert Build Dehug Team Toele Tet Anaye Windew Help #i------------------------------------------

Program

#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();

int main()
{
   int player = 1, i, choice;
   char mark;
   bool isMoveValid = false;
   do
   {
       //TODO: Call the function that draws the game board
       drawBoard();
       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
       isMoveValid = checkValidMove(choice);
       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]))
   {
       return 1;
   }
   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')
   {
       return 0;
   }
   else
   {
       return -1;
   }
}

// 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 checkValidMove(int choice)
{
   bool isValid = false;
   char aChar = '0' + choice;
   if (choice > 0 && choice <= 9) {
       if (boardTile[choice] == aChar) {
           isValid = true;
       }
   }

   return isValid;
}

----------------------------------------------------

Output

Player 1 has 'X' - Player 2 has 'O'


| X | O | X |
|____|___|___|
| O | O | X |
|____|___|___|
| 7 | 8 | X |
==>Player 1 wins!

Add a comment
Know the answer?
Add Answer to:
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...
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
  • 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...

  • 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...

  • 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)<<"...

  • 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...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • 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...

  • 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 =...

  • 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;...

  • I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs ca...

    I need help solving this assignment, you are given a full piece of code that does not run correctly. Some of the bugs can be found at compile time, while others are found at run time. Access the UserMenu.cpp code file (in the zip file in Start Here) and use debugging practices and the debugger in Visual Studio to find each bug. Fix the bug and write a comment in the code giving a description of how you found the...

  • /// c ++ question plz help me fix this not a new code and explain to...

    /// c ++ question plz help me fix this not a new code and explain to me plz /// Write a function to verify the format of an email address: bool VeryifyEmail(char email[ ]); Do NOT parse the email array once character at a time. Use cstring functions to do most of the work. Take a look at the available cstring and string class functions on cplusplus website. Use a two dimensional array to store the acceptable top domain names:...

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