2014-09-18 34 views
-2

我正在開發一個程序,該程序允許用戶對其他玩家玩井字遊戲。到目前爲止,我已經創建了一個「棋盤」,這是一系列允許用戶選擇放置「x」或「o」的選項。但是,我的主板是一個多維數組,數據類型爲char。每當我嘗試編輯這個數組時,我得到一個錯誤,我也無法在頭文件中聲明數組原型,然後在cpp文件中定義這些值。 我的問題是:我怎樣才能聲明這個數組,使我能夠在整個程序中修改它? 這是我到目前爲止有:如何在多維數組中使用數據類型char

頁眉

class Game 
{ 
    public: 
     Game(); 
     void printBoard(); 
     void firstMove(); 
     int updateBoard(int m); 
    private: 
     char board[3][3]; 
     int userMove; 
     int x; 
     int y; 
}; 

的.cpp

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

Game::Game() 
{ 
board[3][3]= {{'o','o','o'},{'o','o','o'},{'o','o','o'}} 
} 

void Game::printBoard() 
{ 
    for(x=0; x < 3; x++) 
    { 
     for(y =0; y<3; y++) 
     { 
      cout << board[x][y] << " " ; 
     } 
     cout << endl; 
    } 
} 

void Game::firstMove() 
{ cout << endl; 
    cout << "Your move, enter where you want to place an 'x' " << endl; 
    cout << endl; 
    cout << "Top right (1)" << endl; 
    cout << "Top left (2)" << endl; 
    cout << "Center (3)" << endl; 
    cout << "Center top (4) " << endl; 
    cout << "Center bottom (5)" << endl; 
    cout << "Center right (6) " << endl; 
    cout << "Center left (7)" << endl; 
    cout << "Bottom right (8)" << endl; 
    cout << "Bottom left (9)" << endl; 
    cin >> userMove; 
    updateBoard(userMove); 
} 

int Game::updateBoard(int m) 
{ 
    switch(userMove) 
    case 1: 
     board[1][3] = 'x'; 
     printBoard(); 

     return 0; 
    } 

每當我嘗試運行此我得到一個錯誤,有可能是打印出的其他的方法來在屏幕上的「董事會」,但迄今爲止,這是我所知道的。

+0

我推薦使用['標準::陣列<性病::陣列 ,3 >>'](http://en.cppreference.com/w/cpp/container/array)而不是'char board [3] [3]'。 – 2014-09-18 22:46:42

+1

基於'C++'的數組是0,爲board [0] [2]更改'board [1] [3]'' – imreal 2014-09-18 22:48:34

回答

0

一些修正之後它現在看到http://codepad.org/B5YwnSx1 (雖然我不太清楚你想達到什麼目的)

#include <iostream> 
#include <string> 
using namespace std; 
class Game 
{ 
    public: 
     Game(); 
     void printBoard(); 
     void firstMove(); 
     int updateBoard(int m); 
    private: 
     char board[3][3]; 
     int userMove; 
     int x; 
     int y; 


}; 



Game::Game() 
{ 

char b[3][3]= { {'o','o','o'},{'o','o','o'},{'o','o','o'}}; 
//copy from b to board 
for(x=0; x < 3; x++) for(y =0; y<3; y++)board[x][y]=b[x][y]; 
} 
//added main 
int main() 
{ 
Game* g = new Game(); 
g->updateBoard(1); 
} 

void Game::printBoard() 
{ 

    for(x=0; x < 3; x++) 
    { 
     for(y =0; y<3; y++) 
     { 
      cout << board[x][y] << " " ; 
     } 
     cout << endl; 
    } 


} 

void Game::firstMove() 
{ cout << endl; 
    cout << "Your move, enter where you want to place an 'x' " << endl; 
    cout << endl; 
    cout << "Top right (1)" << endl; 
    cout << "Top left (2)" << endl; 
    cout << "Center (3)" << endl; 
    cout << "Center top (4) " << endl; 
    cout << "Center bottom (5)" << endl; 
    cout << "Center right (6) " << endl; 
    cout << "Center left (7)" << endl; 
    cout << "Bottom right (8)" << endl; 
    cout << "Bottom left (9)" << endl; 
    cin >> userMove; 
    updateBoard(userMove); 
} 

int Game::updateBoard(int m) 
{ 
    switch(m) //where is usermove set? why was m not used?(userMove) 
case 1: 
board[0][2] = 'x';//board[1][3] is out of bounds 0..2 
printBoard(); 

return 0; 
}