使用继承C++时出错

Errors when using inheritance C++

本文关键字:出错 C++ 继承      更新时间:2023-10-16

所以我有一个3D井字游戏,它在一个类中运行良好。当我尝试创建另一个类并将cpu()函数放入其中时,我遇到了一些问题。用户可以输入他们的选择,它运行良好,但一旦cpu()运行,屏幕上就会打印垃圾代码,而不是x、0或数字,但网格显示良好。

我的一级

class TTT {
      public: //defines functions in class
            void setup();
            void display();
            //void cpu();
            void player();
            void check(int); 
      protected: //sets variables only acessable in this class
                 int cp; //counts cpu points
            int pp; //counts player points
              char board[9]; //spots on board
              char board2[9];
              char board3[9];
              char xo[2]; //xo[0] = comp ||| xo[1] = human
              int rn; //picks who goes first
              int won;//if game has been won or tied
              int turn;//keeps track of whos turn
              int rc;//random placement by cpu
              int full;//tracks board to see if full
              int b1f; //board is full?
              int b2f;
              int b3f;

};

我试着取出cpu();函数,并将其放置在继承第一个的新类中

class CPUClass : public TTT{
      public:
      void cpu();
      };

我的新cpu();低于的功能

void CPUClass::cpu(){
//stuff happens in here
}

当我调用cpu()时;另一个函数中的函数我使用

CPUClass cc;
cc.cpu();

当我需要调用cpu()函数中的cpu(;

在用户离开后,我的板数组的值似乎为null或其他什么。

如果需要,我可以发布完整的来源,但我尝试了总结

我只是继承错了吗,我需要添加一个虚拟函数吗,任何帮助都会很棒。

谢谢。

要求的完整代码

#include <iostream> 
#include <time.h> //seeding random
#include <windows.h> //allows for Sleep()
using namespace std;

class TTT {
      public: //defines functions in class
            void setup();
            void display();
            void cpu();
            void player();
            void check(int); 
      protected: //sets variables only acessable in this class
                 int cp; //counts cpu points
            int pp; //counts player points
              char board[9]; //spots on board
              char board2[9];
              char board3[9];
              char xo[2]; //xo[0] = comp ||| xo[1] = human
              int rn; //picks who goes first
              int won;//if game has been won or tied
              int turn;//keeps track of whos turn
              int rc;//random placement by cpu
              int full;//tracks board to see if full
              int b1f; //board is full?
              int b2f;
              int b3f;

};
class CPUClass : public TTT{

      void cpu();
      };
class CPUClass2 : public TTT{
      //void cpu2();
      };      
int main(int argc, char *argv[])
{
    TTT ttt; //allows class to be acessable
    cout << "Welcome to Tic-Tac-Toe!" << endl; 
    cout << "Three in a row wins [vertical, horizontal, diagonal]" << endl;
    ttt.setup();//calls setup function in class TTT

}
void TTT::setup(){ //sets up board and all variables
     won = 0;  //game not won q = won/tied
     full = 0; //9 = board is full
     cp = 0;
     pp =0;
     b1f = 0;
     b2f = 0;
     b3f=0;
     board[0] = '1'; //sets up the board
     board[1] = '2';
     board[2] = '3';
     board[3] = '4';
     board[4] = '5';
     board[5] = '6';
     board[6] = '7';
     board[7] = '8';
     board[8] = '9';
     board2[0] = '1'; //sets up the board
     board2[1] = '2';
     board2[2] = '3';  
     board2[3] = '4';
     board2[4] = '5';
     board2[5] = '6';
     board2[6] = '7';
     board2[7] = '8';
     board2[8] = '9';
     board3[0] = '1'; //sets up the board
     board3[1] = '2';
     board3[2] = '3';
     board3[3] = '4';  
     board3[4] = '5';
     board3[5] = '6';
     board3[6] = '7';
     board3[7] = '8';
     board3[8] = '9';
     srand(time(NULL)); //seeds with clock
     rn = (rand() %2); 
     if (rn == 0){ //rnadom x and o and who goes first
            turn = 0; // cpu goes first
            xo[0] = 'x'; //comp = x
            xo[1] = 'o'; //human = o
             cout << "nYou are 'O'" << endl; 
          }else{
              turn = 1; //play goes first
              xo[0] = 'o'; //comp = o
              xo[1] = 'x'; // human = x
              cout << "nYou are 'X'" << endl; 
              }
     display(); //sends to display function
     }
void TTT::display(){
     cout << "" << endl; 
     for (int i = 0; i < 9; i++) //for all spots on board
    {

        if ( (i+1) % 3 == 0 ) // ends dont get |
        {
            cout << board[i] << endl; //print number
        }
        else 
        {
            cout << board[i] << " | "; //print number then border
        }
   }
     cout << "" << endl; 
     cout << "t";
     for (int i = 0; i < 9; i++) //for all spots on board
    {

        if ( (i+1) % 3 == 0 ) // ends dont get |
        {
            cout << board2[i] << endl; //print number
            cout << "t";
        }
        else 
        {
            cout << board2[i] <<    " | "; //print number then border
        }

   }
   cout << "" << endl; 
     cout << "tt";
     for (int i = 0; i < 9; i++) //for all spots on board
    {

        if ( (i+1) % 3 == 0 ) // ends dont get |
        {
            cout << board3[i] << endl; //print number
            cout << "tt";
        }
        else 
        {
            cout << board3[i] <<    " | "; //print number then border
        }

   }
   cout << "" << endl;
    CPUClass cc;
    if (full == 27){ //if board is full
    check(won); //check if game has been won/tied
        }
    else{
         if (turn%2 == 0){ //decides whos turn it is
             player();
             }else{
                 //Comp cpu;
                 cout << "Computer is moving" ;
                 cc.cpu();//cput starts turn
          }
         }
     }
void TTT::player(){
  int w;
  int p;
  while (won != 1) //if no one has won loop
  {
     cout << "Which grid would you like to place your tile? [top=1 middle=2 bottom=3]" << endl;
     cin>> p; //which layer user wants to place
     if (p == 1){
           cout << "Enter the tile number you wish to place your marker" << endl;
           cin>>w; //in the layer of tile where to place
           if ((board[w-1] == 'x') || (board[w-1] == 'o')){ //checks if spot is open
            cout << "nSomeone has already placed there! Please select a different tilen";
            player();//loops back
        }else{
              board[w-1] = xo[1];//draws what player is (x or o) on tile picked
              turn ++; //turn over
              full ++; //add one to the board
              b1f++;
              display(); //go to display
              }
           }  
        else if (p == 2){
           cout << "Enter the tile number you wish to place your marker" << endl;
           cin>>w;
           if ((board2[w-1] == 'x') || (board2[w-1] == 'o')){ //checks if spot is open
            cout << "nSomeone has already placed there! Please select a different tilen";
            player();//loops back
        }else{
              board2[w-1] = xo[1];//draws what player is (x or o) on tile picked
              turn ++; //turn over
              full ++; //add one to the board
              b2f++;
              display(); //go to display
              }
           }  
     else if (p == 3){
           cout << "Enter the tile number you wish to place your marker" << endl;
           cin>>w;
           if ((board3[w-1] == 'x') || (board3[w-1] == 'o')){ //checks if spot is open
            cout << "nSomeone has already placed there! Please select a different tilen";
            player();//loops back
        }else{
              board3[w-1] = xo[1];//draws what player is (x or o) on tile picked
              turn ++; //turn over
              full ++; //add one to the board
              b3f;
              display(); //go to display
              }
           } 
           else{
                cout <<"Please select one of the options!nn" ; 
                player();
                } 
     }
  }
void TTT::cpu(){

     srand(time(NULL)); //seeds with clock
     rc = (rand() %9); //8 spots on board 0-8
     int bc;
     if (b1f == 9){ //if a board is filled pick another one
          bc = 1+(rand() %2);
      }
      else if (b3f == 9){
           bc = (rand() %2);
           }
      else if (b2f == 9){
           bc = (rand() %2);
           if (bc == 1){
                  bc = 2;
                  }
           }
      else {
           bc = (rand() %3); //else just randomly pick one
           }
     if (bc == 0){
     if ((board[rc] == 'x' ) || ( board[rc] == 'o')){
         Sleep(100); //seeded by time. So to prevent memory overflow pause before getting a new random number
         cpu(); //loops and finds a new spot
         }else{
           turn++; //ends turn
           full ++; //add one to board
           b1f++;
           board[rc] = xo[0]; // draw what cpu is on tile selected
           Sleep(800); //sleeps for 800ms, gives more realistic playing
           display(); //displays board
         }
     }
     else if (bc == 1){
          if ((board2[rc] == 'x' ) || ( board2[rc] == 'o')){
         Sleep(100); //seeded by time. So to prevent memory overflow pause before getting a new random number
         cpu(); //loops and finds a new spot
         }else{
           turn++; //ends turn
           full ++; //add one to board
           b2f++;
           board2[rc] = xo[0]; // draw what cpu is on tile selected
           Sleep(800); //sleeps for 800ms, gives more realistic playing
           display(); //displays board
         }
     }
          else if (bc == 2){
               if ((board3[rc] == 'x' ) || ( board3[rc] == 'o')){
         Sleep(100); //seeded by time. So to prevent memory overflow pause before getting a new random number
         cpu(); //loops and finds a new spot
         }else{
           turn++; //ends turn
           full ++; //add one to board
           b3f++;
           board3[rc] = xo[0]; // draw what cpu is on tile selected
           Sleep(800); //sleeps for 800ms, gives more realistic playing
           display(); //displays board
         }
     }
               }

void TTT::check(int wo){
     //horizontal win
      if ((board[0] == 'x') && (board[1] == 'x') && (board[2] == 'x')){
                   if (rn == 0){
                          cp++;
                          }else{
                                pp++;
                                }
                   }

      if ((board[6] == 'x') && (board2[4] == 'x') && (board3[2] == 'x')){

          if (rn == 0){
                          cp++;
                          }else{
                                pp++;
                                }
          }
////////////////more checking goes here, deleted because it's too long
              wo =1;
              cout << "nGame Over!n" ;
              cout << "nScore: Player - "<<pp<<" Computer - "<<cp<<"n" ;
              if (pp == cp){ //if points are =
                     cout << "You tied!n" ;
                     }
              else if (pp > cp){ //if player points are greater, player wins
                   cout << "You won!n" ;
                   }
              else if (pp < cp){
                   cout << "You lose!n" ;
                   }
              int c;
           cout<<"nPlay again? [1] for yes anything else to exitn" ;
           cin >> c;
           if (c == 1){ //if user enters 1 then reset the game
                 setup();
                 }
           else{//else exit
           exit(0);
              }
     }

继承是在main中的类之间建立"is-a"关系的一种方法。如果创建了TTT,您的意思是实例化派生类CPUClass

TTT ttt; // Creates a tic tac toe board
CPUClass cc; // Creates a derived tic tac toe board 

ttt和cc互不了解,他们甚至不在同一块板上比赛。

也许你的意思是创建一个player类并派生一个人类/计算机变体?

我认为遗产继承对你没有帮助。?我认为你需要的是PvP PvC和CvC的选项?

display循环中,去掉新的板CPUClass,使用我们所在的板。

if (full == 27){ //if board is full
   check(won); //check if game has been won/tied  
}
else{
   if (turn%2 == 0){ //decides whos turn it is
      player();  
   }else{
      cpu(); //cput starts turn
   }
   // Should be this. 
   int currentPlayer= turn % 2; 
   if( playerIsCpu(currentPlayer) ){ // create a function to return if this player is a cpu 
      cpu(currentPlayer); // cpu makes move for player 1..2
   } else{
      player( currentPlayer); // player 1..2 is presented with a move.
   } 
}