Question

I've created a functioning program but I can't figure out how to implement class structure into...

I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you.

#pragma once
#include <iostream>
using namespace std;


   bool DeclareResults(char ch, char gameboard[][3]);

   bool FilledBoard(char gameboard[][3]);

   void printGameBoard(char gameboard[][3]);


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


int main()
{

   //declare the gameboard with a two dimensional board
   char GameBoard[3][3] =
   {
       { '*', '*', '*'},
       { '*', '*', '*'},
       { '*', '*', '*'}
   };

   cout << " Tic Tac Toe Game "<<endl << endl;

   //The two dimensional gameboard
   cout << " New Game board: ";

   //displays the gameboard
   printGameBoard(GameBoard);
   cout <<endl;

   //checks the condition with a while loop
   while (true)

   {
       cout << "Player 1 - Please enter the row (0, 1, 2) to plot \'X\': ";

       int rows;
       cin >> rows;

       cout << "Player 1 - Please enter the column (0, 1, 2) to plot \'X\': ";

       //declares the columns variable
       int columns;
       cin >> columns;

       // checks out the condition of the gameboard
       if (GameBoard[rows][columns] == '*')
       {
           GameBoard[rows][columns] = 'x';
       }
       //displays the gameboard
       printGameBoard(GameBoard);

       //display when player 1 wins the match
       if (DeclareResults('x', GameBoard))
       {
           cout << "Player 1 wins the game!" << endl;
           system("pause");
           exit(0);
       }

       //display when no one is a winner
       else if (FilledBoard(GameBoard))
       {
           cout << "No one is a winner, it is a tie" << endl;
           system("pause");
           exit(0);

       }

       cout << "Player 2 - Please enter the row (0, 1, 2) to plot \'O\': ";
       cin >> rows;

       cout << "Player 2 - Please input the column (0, 1, 2) to plot \'O\': ";
       cin >> columns;

       //checks out the condition of the gameboard
       if (GameBoard[rows][columns] == '*')
       {
           GameBoard[rows][columns] = 'O';
       }

       //displays the gameboard
       printGameBoard(GameBoard);

       //Displays the results when player 2 wins
       if (DeclareResults('O', GameBoard))
       {
           //when player 2 wins the match
           cout << "player 2 wins the game!" << endl;
           system("pause");
           exit(0);
       }

       //display when no one is a winner
       else if (FilledBoard(GameBoard))
       {
           cout << "No one is a winner" << endl;

           //hold the screen
           system("pause");
           return(0);
       }
   }
   //hold the screen
   system("pause");
   return 0;
}

//declares the result of the game based on the prompt condition
bool DeclareResult(char ch, char gameboard[][3])
{
   for (int i = 0; i < 3; i++)
   {
       if (ch == gameboard[i][0] && ch == gameboard[i][1] && ch == gameboard[i][2])
       {
           return true;
       }
   }

   for (int j = 0; j < 3; j++)
   {

       //checks out the condition of the gameboard
       if (ch == gameboard[0][j] && ch == gameboard[1][j] && ch == gameboard[2][j])
       {
           return true;
       }
   }

   if (ch == gameboard[0][0] && ch == gameboard[1][1] && ch == gameboard[2][2])
   {

       return true;
   }
   if (ch == gameboard[0][2] && ch == gameboard[1][1] && ch == gameboard[2][0])
   {
       return true;
   }
   return false;
}
//funtion to fill the gameboard by user entry
bool isSellectFilledBoard(char gameboard[][3])
{
   for (int i = 0; i < 3; i++)
   {
       for (int j = 0; j < 3; j++)
       {
           if (gameboard[i][j] == '*')
           {
               return false;
           }
       }
   }
   return true;
}

bool DeclareResults(char ch, char gameboard[][3])
{
   return false;
}

bool FilledBoard(char gameboard[][3])
{
   return false;
}

//funtion to display the game board
void printGameBoard(char gameboard[][3])
{
   cout << "\n-----------" << endl;

   for (int i = 0; i < 3; i++)
   {
       //displays lines for the rows
       cout << "|";

       for (int j = 0; j < 3; j++)
       {
           //displays lines for the columns
           cout << gameboard[i][j] << " |";
       }
       //displays line for the bottom of gameboard
       cout << "\n------------------" << endl;
   }

}


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

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

fnt.h

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

#pragma once

#include <iostream>

using namespace std;

class Test {

public:

Test(){

}

bool DeclareResults(char ch, char gameboard[][3]);

bool FilledBoard(char gameboard[][3]);

void printGameBoard(char gameboard[][3]);

bool isSellectFilledBoard(char gameboard[][3]);

};

--------------------------------------------------------------------------------------------------------------
fnt.cpp
------------------------------------------------


#include "fnt.h"

#include <iostream>

using namespace std;

//declares the result of the game based on the prompt condition

bool Test::DeclareResults(char ch, char gameboard[][3])

{

for (int i = 0; i < 3; i++)

{

if (ch == gameboard[i][0] && ch == gameboard[i][1] && ch == gameboard[i][2])

{

return true;

}

}

for (int j = 0; j < 3; j++)

{

//checks out the condition of the gameboard

if (ch == gameboard[0][j] && ch == gameboard[1][j] && ch == gameboard[2][j])

{

return true;

}

}

if (ch == gameboard[0][0] && ch == gameboard[1][1] && ch == gameboard[2][2])

{

return true;

}

if (ch == gameboard[0][2] && ch == gameboard[1][1] && ch == gameboard[2][0])

{

return true;

}

return false;

}

//funtion to fill the gameboard by user entry

bool Test::isSellectFilledBoard(char gameboard[][3])

{

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

{

if (gameboard[i][j] == '*')

{

return false;

}

}

}

return true;

}


bool Test::FilledBoard(char gameboard[][3])

{

return false;

}

//funtion to display the game board

void Test::printGameBoard(char gameboard[][3])

{

cout << " -----------" << endl;

for (int i = 0; i < 3; i++)

{

//displays lines for the rows

cout << "|";

for (int j = 0; j < 3; j++)

{

//displays lines for the columns

cout << gameboard[i][j] << " |";

}

//displays line for the bottom of gameboard

cout << " ------------------" << endl;

}

}







------------------------------------------------------------------------------------------------------------------------------------------------------
main.cpp

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

#include "fnt.h"

#include <iostream>

using namespace std;


int main()

{

Test t;

//declare the gameboard with a two dimensional board

char GameBoard[3][3] =

{

{ '*', '*', '*'},

{ '*', '*', '*'},

{ '*', '*', '*'}

};

cout << " Tic Tac Toe Game "<<endl << endl;

//The two dimensional gameboard

cout << " New Game board: ";

//displays the gameboard

t.printGameBoard(GameBoard);

cout <<endl;

//checks the condition with a while loop

while (true)

{

cout << "Player 1 - Please enter the row (0, 1, 2) to plot 'X': ";

int rows;

cin >> rows;

cout << "Player 1 - Please enter the column (0, 1, 2) to plot 'X': ";

//declares the columns variable

int columns;

cin >> columns;

// checks out the condition of the gameboard

if (GameBoard[rows][columns] == '*')

{

GameBoard[rows][columns] = 'x';

}

//displays the gameboard

t.printGameBoard(GameBoard);

//display when player 1 wins the match

if (t.DeclareResults('x', GameBoard))

{

cout << "Player 1 wins the game!" << endl;

system("pause");

exit(0);

}

//display when no one is a winner

else if (t.FilledBoard(GameBoard))

{

cout << "No one is a winner, it is a tie" << endl;

system("pause");

exit(0);

}

cout << "Player 2 - Please enter the row (0, 1, 2) to plot 'O': ";

cin >> rows;

cout << "Player 2 - Please input the column (0, 1, 2) to plot 'O': ";

cin >> columns;

//checks out the condition of the gameboard

if (GameBoard[rows][columns] == '*')

{

GameBoard[rows][columns] = 'O';

}

//displays the gameboard

t.printGameBoard(GameBoard);

//Displays the results when player 2 wins

if (t.DeclareResults('O', GameBoard))

{

//when player 2 wins the match

cout << "player 2 wins the game!" << endl;

system("pause");

exit(0);

}

//display when no one is a winner

else if (t.FilledBoard(GameBoard))

{

cout << "No one is a winner" << endl;

//hold the screen

system("pause");

return(0);

}

}

//hold the screen

system("pause");

return 0;

}





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


Created class with all your methods. There are now 3 files


Add a comment
Know the answer?
Add Answer to:
I've created a functioning program but I can't figure out how to implement class structure into...
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 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)<<"...

  • In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user....

    In the C++ programming language write a program capable of playing 3D Tic-Tac-Toe against the user. Your program should use OOP concepts in its design. Use Inheritance to create a derived class from your Lab #9 Tic-Tac-Toe class. You can use ASCII art to generate and display the 3x3x3 playing board. The program should randomly decide who goes first computer or user. Your program should know and inform the user if an illegal move was made (cell already occupied). The...

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

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

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...

    I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated? #include using namespace std; const int ROWS = 3; const int COLS = 3; int counter = 0; void aITurn(char b[][COLS]); void fillBoard(char board[ROWS][COLS]); void getChoice(char b[][COLS], bool playerToggle); bool gameOver(char b[][COLS]); void showBoard(char board[ROWS][COLS]); int main() {    ///main is complete, nothing to do here    char board[ROWS][COLS];    bool playerToggle =...

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

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

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

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