[英]Errors when using inheritance C++
因此,我有一個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()函數時,我只需使用cpu()即可;
用戶使用后,我的板陣列的值似乎只是空值或其他值。
如果需要,我可以發布完整的源代碼,但是我嘗試總結一下
我只是繼承錯誤了嗎,我需要添加一個虛函數嗎,任何幫助都會很棒。
謝謝。
完整代碼要求
#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 << "\t\t";
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 << "\t\t";
}
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 tile\n";
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 tile\n";
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 tile\n";
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!\n\n" ;
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 exit\n" ;
cin >> c;
if (c == 1){ //if user enters 1 then reset the game
setup();
}
else{//else exit
exit(0);
}
}
繼承是要建立一個辦法“是-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.
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.