2015-12-02 3 views
0

Я пытаюсь скомпилировать, но я продолжаю получать эту ошибку, кто-нибудь видит, где моя ошибка?Tic Tac Toe C++ связанная ошибка ld возвращен 1 статус выхода

Ошибка:

C:. \ Users \ Moody \ AppData \ Local \ Temp \ ccyClPvo.o Mattingly_Section2_Lab11.cpp :(текст + 0x9d9): неопределенная ссылка на `StartGame()»

C : \ Users \ Moody \ Documents \ collect2.exe [Error] л.д. возвращается 1 состояние выхода

// Computer Tic tac toe, 

// Include the iostream library 
#include <iostream> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
#include <limits> 
// Using the standard namespace 
using namespace std; 
// declare global variables 
char Board [9]; 

// Declare Functions 
void showBoard (); 
bool moveIsValid (int m); 
int whoWon(); 
int playerScore = 0; 
int computerScore = 0; 
int ties = 0; 
void playAgain(); 
void startGame(); 

int main () 
{ 
void startGame(); 
{ 
// Seed the random number 
srand (time (NULL)); 
// Declare Global Variables 
int Whose_Turn = 1; // 1 means players turn and 2 means player 2 turns 
int Move; // Store where the players wants to move 
int Total_Moves = 0; 
int num_players = 0; 

// Assigns values to the playing board 
Board[0] = '0'; 
Board[1] = '1'; 
Board[2] = '2'; 
Board[3] = '3'; 
Board[4] = '4'; 
Board[5] = '5'; 
Board[6] = '6'; 
Board[7] = '7'; 
Board[8] = '8'; 


cout << "Welcome to Tic Tac Toe.\n Enter 0 to watch computers duke it the fuck out in some good old tic tacers, bro" << endl; 
cin >> num_players; 

if (num_players == 0) { 
    while (whoWon () ==0 && Total_Moves < 9) { 
     do { 
     // Show the board 
     showBoard(); 
     // Tell which player to move 
     if (Whose_Turn ==1) { 
      cout << "The First Computer's move is: " << Move << endl; 
      Move = rand() % 9; 
     } else { 
      Move = rand() % 9; 
      cout << "The Second Computer's move is: " << Move << endl; 
     } 
     // Get move 
     // cout << "Enter the number of spot you'd like to move" << endl; 
     // cin >> Move; 
     } while (moveIsValid (Move) != true); 
     // Add 1 to Total_Moves 
     Total_Moves++; 
     // Change whose turn it is 
     switch (Whose_Turn) { 
     case (1): { 
      Board[Move] = 'x'; 
      Whose_Turn = 2; 
      break; 
     } 
     case (2): { 
      Board[Move] = 'o'; 
      Whose_Turn = 1; 
     } 
     } 
    } 
    // show the board 
    showBoard(); 
    if (whoWon() == 1) { 
     // show the board 
     showBoard(); 
     cout << "Computer 1 has won the game!" << endl; 
     ++playerScore; 
    } 
    if (whoWon() == 2) { 
     // Show the board 
     showBoard(); 
     cout << " The Second Computer has won the game! " << endl; 
     ++computerScore; 
    } 
    if (Total_Moves==9 && !whoWon()) { 
     // Show the board 
     showBoard(); 
     cout << "It's a tie game!" << endl; 
     ++ties; 

    } 
    cout << "Computer 1: " << playerScore << " "<< "Computer 2: " << computerScore << " Ties: "<< ties << endl; 
    } 
    system ("Pause"); 
    return 0; 
} 
} 
void showBoard () { 
    cout << endl; 
    cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl; 
    cout << "--+---+--" << endl; 
    cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl; 
    cout << "--+---+--" << endl; 
    cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl; 
    cout << endl; 
} 

bool moveIsValid (int m) { 
    if (Board[m] != 'x' && Board[m] != 'o') { 
    return true; 
    } else { 
    return false; 
    } 
} 

int whoWon () { 
if (Board[0] == Board[1] && Board[1] == Board[2]) { 
    if (Board[0] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[3] == Board[4] && Board[4] == Board[5]) { 
    if (Board[3] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[6] == Board[7] && Board[7] == Board[8]) { 
    if (Board[6] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[0] == Board[3] && Board[3] == Board[6]) { 
    if (Board[0] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[1] == Board[4] && Board[4] == Board[7]) { 
    if (Board[1] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[2] == Board[5] && Board[5] == Board[8]) { 
    if (Board[2] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[0] == Board[4] && Board[4] == Board[8]) { 
    if (Board[0] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[2] == Board[4] && Board[4] == Board[6]) { 
    if (Board[2] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 

return 0; 
} 
void playAgain() 
{ 
    char answer; 
    int Whose_Turn; 
    int Total_Moves; 
    cout << "Would you like to play again? (Y/N)" << endl; 

    while (cin >> answer) 
    { 
     if (answer == 'Y' || answer == 'y') 
     { 
      Whose_Turn = 1; 
      Total_Moves = 0; 
      cout << endl; 
      startGame(); 

     } 
     else if (answer == 'N' || answer == 'n') 
     { 
      cout << endl << "Bye!" << endl; 
      break; 

     } 
     else 
     { 
      cout << "Please enter a correct input: "; 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      string str; 
      getline(cin, str); 
      answer = str[0]; 
     } 
    } 
} 
+0

Вы не можете определить функцию внутри другой функции. –

+0

Знаете ли вы, что делать, чтобы исправить это? Удаление void startGame(); изнутри int main() не работал для меня. Я чувствую себя как дуф. –

+0

Вы определили другие функции в порядке. –

ответ

0
  1. Первые вещи первых, я уверен, что вы не можете определить функцию [StartGame()] внутри основной() так или в любой другой функции, если на то пошло.
  2. Синтаксис определения функции для startGame() также неверен. Вы не завершать т.е. удалить точку с запятой (аннулируются StartGame(); {)
  3. Ваша функция StartGame() является ничтожным, но вы возвращаете значение в конце вашей функции, т.е. возврата 0

Этот должен компилироваться и запускаться. Попробуйте следующее:

// Computer Tic tac toe, 

// Include the iostream library 
#include <iostream> 
#include <string> 
#include <ctime> 
#include <cstdlib> 
#include <limits> 
// Using the standard namespace 
using namespace std; 
// declare global variables 
char Board [9]; 

// Declare Functions 
void showBoard (); 
bool moveIsValid (int m); 
int whoWon(); 
int playerScore = 0; 
int computerScore = 0; 
int ties = 0; 
void playAgain(); 
void startGame(); 

int main () 
{ 
    startGame(); 
    return 0; 
} 


void startGame() 
{ 
// Seed the random number 
srand (time (NULL)); 
// Declare Global Variables 
int Whose_Turn = 1; // 1 means players turn and 2 means player 2 turns 
int Move; // Store where the players wants to move 
int Total_Moves = 0; 
int num_players = 0; 

// Assigns values to the playing board 
Board[0] = '0'; 
Board[1] = '1'; 
Board[2] = '2'; 
Board[3] = '3'; 
Board[4] = '4'; 
Board[5] = '5'; 
Board[6] = '6'; 
Board[7] = '7'; 
Board[8] = '8'; 


cout << "Welcome to Tic Tac Toe.\n Enter 0 to watch computers duke it the fuck out in some good old tic tacers, bro" << endl; 
cin >> num_players; 

if (num_players == 0) { 
    while (whoWon () ==0 && Total_Moves < 9) { 
     do { 
     // Show the board 
     showBoard(); 
     // Tell which player to move 
     if (Whose_Turn ==1) { 
      cout << "The First Computer's move is: " << Move << endl; 
      Move = rand() % 9; 
     } else { 
      Move = rand() % 9; 
      cout << "The Second Computer's move is: " << Move << endl; 
     } 
     // Get move 
     // cout << "Enter the number of spot you'd like to move" << endl; 
     // cin >> Move; 
     } while (moveIsValid (Move) != true); 
     // Add 1 to Total_Moves 
     Total_Moves++; 
     // Change whose turn it is 
     switch (Whose_Turn) { 
     case (1): { 
      Board[Move] = 'x'; 
      Whose_Turn = 2; 
      break; 
     } 
     case (2): { 
      Board[Move] = 'o'; 
      Whose_Turn = 1; 
     } 
     } 
    } 
    // show the board 
    showBoard(); 
    if (whoWon() == 1) { 
     // show the board 
     showBoard(); 
     cout << "Computer 1 has won the game!" << endl; 
     ++playerScore; 
    } 
    if (whoWon() == 2) { 
     // Show the board 
     showBoard(); 
     cout << " The Second Computer has won the game! " << endl; 
     ++computerScore; 
    } 
    if (Total_Moves==9 && !whoWon()) { 
     // Show the board 
     showBoard(); 
     cout << "It's a tie game!" << endl; 
     ++ties; 

    } 
    cout << "Computer 1: " << playerScore << " "<< "Computer 2: " << computerScore << " Ties: "<< ties << endl; 
    } 
    system ("Pause"); 
} 

void showBoard () { 
    cout << endl; 
    cout << Board[0] << " | " << Board[1] << " | " << Board[2] << endl; 
    cout << "--+---+--" << endl; 
    cout << Board[3] << " | " << Board[4] << " | " << Board[5] << endl; 
    cout << "--+---+--" << endl; 
    cout << Board[6] << " | " << Board[7] << " | " << Board[8] << endl; 
    cout << endl; 
} 

bool moveIsValid (int m) { 
    if (Board[m] != 'x' && Board[m] != 'o') { 
    return true; 
    } else { 
    return false; 
    } 
} 

int whoWon () { 
if (Board[0] == Board[1] && Board[1] == Board[2]) { 
    if (Board[0] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[3] == Board[4] && Board[4] == Board[5]) { 
    if (Board[3] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[6] == Board[7] && Board[7] == Board[8]) { 
    if (Board[6] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[0] == Board[3] && Board[3] == Board[6]) { 
    if (Board[0] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[1] == Board[4] && Board[4] == Board[7]) { 
    if (Board[1] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[2] == Board[5] && Board[5] == Board[8]) { 
    if (Board[2] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[0] == Board[4] && Board[4] == Board[8]) { 
    if (Board[0] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 
if (Board[2] == Board[4] && Board[4] == Board[6]) { 
    if (Board[2] == 'x') { 
    return 1; 
    } else { 
    return 2; 
    } 
} 

return 0; 
} 
void playAgain() 
{ 
    char answer; 
    int Whose_Turn; 
    int Total_Moves; 
    cout << "Would you like to play again? (Y/N)" << endl; 

    while (cin >> answer) 
    { 
     if (answer == 'Y' || answer == 'y') 
     { 
      Whose_Turn = 1; 
      Total_Moves = 0; 
      cout << endl; 
      startGame(); 

     } 
     else if (answer == 'N' || answer == 'n') 
     { 
      cout << endl << "Bye!" << endl; 
      break; 

     } 
     else 
     { 
      cout << "Please enter a correct input: "; 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      string str; 
      getline(cin, str); 
      answer = str[0]; 
     } 
    } 
}