2016-03-31 12 views
-4

Merhaba, C++ 'yı yeni öğrenmeye başladım ve sorunumda herhangi bir yardımın takdir edilmesi gerekiyor.Bir tac toe kodunun bir sınıf ile oluşturulmasını ancak sadece ekranın görüntülenmesi ve oynatıcının tanımlanması için

  • ben bir oyun için bir kod parçası (seçilen tic tac toe)
  • oluşturmak için istedi ama oldum onun iki kişi arasında bölünmüş. Benim rolüm sadece kartın görüntülenmesi ve oyuncu x veya oyuncunun seçimi için bir sınıf yaratmaktır. O.

Şimdiye kadar bu C++ kodunu oluşturmayı denedim ama hatalar göstermeye devam et, herkes bunu düzeltmeyi biliyor mu? Oyunda p.s XsandOs olarak sınıflandırdım.

#include <string> 
#include <iostream> 

using namespace std; 

class XsandOs // Name of the class 
{ 
public: 

    XsandOs(); 

    void drawboard(); 
    void printBoard(); 
    void getMove(int move); 
    void choosePlayer(char player); 
    bool checkwinner(const char board[3][3], char symbol, int plays); 

private: 
    const char board[3][3]; 

}; 

void XsandOs::drawboard // Develops the board 
{ 
    cout << "Let's play X's and O's\n" << "_________________________________\n\n"; //This prints a title declaring the name of the game 
    //This action uses an array to create the board, the user will choose the number to select where they want the character to go. 

    char board[3][3] = 
    { 
     { '1', '2', '3', }; //This creates the top row 
     {'4', '5', '6', }; // This creates the middle row 
     {'7', '8', '9', }; // This creates the bottom row 
    }; 

    for (int i = 0; i < 3; ++i) 
    { 
     for (int j = 0; j < 3; ++j) 
     { 
      cout << board[i][j] << ""; 
     } 
     cout << endl; 
    } 
} 

void XsandOs::choosePlayer(char player) 
{ 
    if (player == 'X') 
     Player = 'O'; 
    elseif(player == 'O') 
     Player = 'X'; 

    return(void); 
} 

int main() 
{ 
    draw(); 

    while (1) 
    { 
     input(); 
     draw(); 
     choosePlayer(); 
    } 

    system("pause"); 

    return 0; 
} 
+1

Bunu daha önce görmüştüm ve size çok zaman kazandırabilir. "Kazanan tek hamle oynamak değildir." – user4581301

+0

Ana() ve sınıf arasındaki bağlantıyı göremiyorum. Iostream için olandan önce bir '' 'koydunuz mu? – Christophe

+0

'void XsandOs :: drawboard' void olmalıdır XsandOs :: drawboard()' – DimChtz

cevap

0

Bazı tasarım sorunlarını temizledim ve sizin için yöntemler oluşturdum. Hala yapılacak işler var, bunu sizin için yapmamızı bekleyemezsiniz. İyi şanslar.

#include <string> 
#include <iostream> 

using namespace std; 

class XsandOs // Name of the class 
{ 
public: 

    XsandOs(); 

    void draw(); 
    void input(); 
    int getMove(); // 1-9 or zero on error 
    void choosePlayer(); 
    bool checkWinner(); // true means game over 

private: 
    char board[3][3]; 
    char player; 
}; 

XsandOs::XsandOs() 
{ 
    // initialize the board 
    player = 'X'; // x goes first 
    for (int i = 0;i < 3;++i) 
     for (int j = 0;j < 3;++j) 
      board[i][j] = ' '; // blank means the spot is empty 
} 

void XsandOs::draw() // Develops the board 
{ 

    for (int i = 0; i < 3; ++i) 
    { 
     for (int j = 0; j < 3; ++j) 
     { 
      cout << board[i][j] << " "; 
     } 
     cout << endl; 
    } 
} 

int XsandOs::getMove() 
{ 
    // ask for a move 
    // validate the move or get a new move 
    // TODO  
} 
void XsandOs::input() 
{ 
    // ask for a move 
    int move = 0; 
    while (!move) { 
     move = getMove(); 
    } 

    // do the move and update the board 
    board[move-1] = player; 
} 

void XsandOs::choosePlayer() 
{ 
    if (player == 'X') 
     Player = 'O'; 
    elseif(player == 'O') 
     Player = 'X'; 
} 

bool XsandOs::checkWinner() 
{ 
    // TODO 
    // Check the board state and look for a winner. 
    return false; 
} 

int main() 
{ 
    XsandOs game; 
    bool gameOver = false; 
    game.draw(); 
    while (!gameOver) 
    { 
     game.input(); 
     game.draw(); 
     game.checkWinner() 
     gameOver = game.choosePlayer(); 
    } 

    system("pause"); 

    return 0; 
} 
İlgili konular