2012-05-04 17 views
0

私は地雷探偵ゲームを作成しようとしていますが、コンパイルエラーが発生します。左辺のオペランドには左辺値が必要です。左辺オペランドに左辺値が必要C++コンパイルエラー

#include <iostream> 
#include <ctime> 
using namespace std; 

// ------------------------------------------------------ 
// class Cell 
// represents one grid element in the Minesweeper game 
// ------------------------------------------------------ 

class Cell { 
public: 
    Cell(); 
    void print(); 
    void peek(); 
    void unittest(); 
    void setMined(bool); 
    bool getMined(); 
    void setAdj(int); 
private: 
    bool covered; 
    bool marked; 
    bool mined; 
    int adjcount; 
}; 


// ------------------------ 
// functions for class Cell 
// ------------------------ 

Cell::Cell(){ 
    covered = true; 
    marked = false; 
    mined = false; 
    adjcount = 0; 
// cout << "Creating a Cell" << endl; 
} 

void Cell::setAdj(int n){ 
    adjcount = n; 
} 

bool Cell::getMined(){ 
    return mined; 
} 

void Cell::setMined(bool b){ 
    mined = b; 
} 

void Cell::print(){ 
    if (marked) cout << " L "; 
    else { 
     if (covered) cout << " ? "; 
     else{ 
      if (mined) cout << " @ "; 
      else if (adjcount == 0) cout << " _ "; 
      else cout << " " << adjcount << " "; 
     } 
    } 
} 

void Cell::peek(){ 
    if (mined) cout << " @ "; 
    else if (adjcount == 0) cout << " _ "; 
    else cout << " " << adjcount << " "; 
} 

void Cell::unittest(){ 
    print(); cout << endl; 

    covered = false; 
    print(); cout << endl; 

    adjcount = 4; 
    print(); cout << endl; 

    mined = true; 
    print(); cout << endl; 

    covered = true; 
    print(); cout << endl; 

    marked = true; 
    print(); cout << endl; 
} 

// ------------------------------------- 
// class Board 
// this class represents a 2 dimensional 
// array of Cell objects for the game 
// of minesweeper 
//-------------------------------------- 

class Board{ 
public: 
    Board(); 
    void print(); 
    void peek(); 
    void adjacencycount(); 
    void mixMined(); 
private: 
    static const int rows = 5; 
    static const int cols = 5; 
    Cell cells [rows][cols]; 
    int mines; 
}; 

// -------------------------- 
// functions for class Board 
// -------------------------- 
Board::Board(){ 
    mines = 6; 
     for(int i = 0; i < 1 ; i++){ 
      for(int j = 0; j < mines; j++){ 
       cells[i][j].setMined(true); 
      } 
     } 
} 

void Board::mixMined(){ 
    int shuffle = 1000; 
    for(int i = 0; i < shuffle; i++){ 
     int r1 = (rand()%rows); 
     int c1 = (rand()%cols); 
     int r2 = (rand()%rows); 
     int c2 = (rand()%cols); 
     if(r1 && c1 != r2 && c2){ 
      bool temp = cells[r1][c1].getMined(); 
      cells[r1][c1].getMined() = cells[r2][c2].getMined(); 
      cells[r2][c2].getMined() = temp; 
     } 
    } 

} 

void Board::adjacencycount(){ 

    for(int i = 0; i < rows; i++){  
     for(int j = 0; j < cols; j++){ 
      if(!cells[i][j].getMined()){ 
       int count = 0; 
        if (i-1 >= 0 && j-1 >= 0 && cells[i-1][j-1].getMined()) count++; 
        if (i-1 >= 0 && cells[i-1][j].getMined()) count++; 
        if (i-1 >= 0 && j+1 <= cols-1 && cells[i-1][j+1].getMined()) count++;           
        if (j-1 >= 0 && cells[i][j-1].getMined()) count++; 
        if (j+1 <= cols-1 && cells[i][j+1].getMined()) count++;          
        if (i+1 <= rows-1 && j-1 >= 0 && cells[i+1][j-1].getMined()) count++; 
        if (i+1 <= rows-1 && cells[i+1][j].getMined()) count++; 
        if (i+1 <= rows-1 && j+1 <= cols-1 && cells[i+1][j+1].getMined()) count++; 
        cells[i][j].setAdj(count);    
        // cout << count; -- for testing purposes 
      } 
     } 
    } 
} 

void Board::print(){ 
     for (int i = 0; i < rows; i++){ 
       for (int j = 0; j < cols; j++){ 
         cells[i][j].print(); 
             } 
       cout << endl << endl; 
     } 
} 

void Board::peek(){ 
     for (int i = 0; i < rows; i++){ 
       for (int j = 0; j < cols; j++){ 
         cells[i][j].peek(); 
       } 
       cout << endl << endl; 
     } 
} 

// ------------------------- 
// main function for testing 
// ------------------------- 

int main(void) { 
    //Cell c; 
    //c.unittest(); 
    srand(time(0)); 

    Board b; 
    b.mixMined(); 
    b.adjacencycount(); 
    b.peek(); 

    return 0; 
} 

私は自分の細胞を交換しようとしているので、鉱山はすべての新しいゲームを無作為化するだろう。 Iveは検索して、これに対する解決策を見つけることができませんでした。私は "=="を追加しましたが、その機能は私が望むことをするつもりはありません。

++ EDITは++私はそれが国家の左辺値が必要でしたごめんなさい、私が見逃し発生するエラー厥その

minesweeper.cpp: In member function ‘void Board::mixMined()’: 
minesweeper.cpp:130: error: lvalue required as left operand of assignment 
minesweeper.cpp:131: error: lvalue required as left operand of assignment 

を入力しました。

+0

getMindedは(正確に)何を返しますか? – Mat

+0

完全なコードを投稿してください。 – besworland

+0

'getMined()'の署名は何ですか?これは 'bool&getMined()'のようなものでなければなりません。 (参照に注意してください) – Vlad

答えて

5

私はgetMined()が実際にこのようなものだと思う:

:あなたが好きないくつかの関数を記述したい場合がありますことは不可能であるので、あなたは rValueに割り当てるしようとしている

bool getMined()

void setMined(bool m)とそれを好きに使う:

cells[r1][c1].setMined(cells[r2][c2].getMined());

関連する問題