开发者

Work with arrays

开发者 https://www.devze.com 2023-01-09 13:22 出处:网络
I\'m writing some game at c++. The problem is with arrays. It always shows the \'desk\' with points (empty parts). Need comments are in the code:

I'm writing some game at c++. The problem is with arrays. It always shows the 'desk' with points (empty parts). Need comments are in the code:

void showDesk(int someArray[][3])
{
    for(int i = 0; i < 3 ;i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if(someArray [i][j] == 0)      cout << ".";
            else if (someArray[i][j] == 1) cout << "x";
            else if (someArray[i][j] == 2) cout << "o";
        }
        cout<<"\n";
    }
}

void newDesk (int someArray[][3])
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            someArray [i][j] = 0;
}

bool empty(int someArray[][3], int x, int y)
{
    if (someArray[x][y] == 0)
        return true;       // It returns true
    else
        return false;
}

void setMark(int someArray[][3],int x,int y,int mark)
{
    if (empty(someArray,x,y))
        someArray [x][y] == mark; // But this never calls!
}

int main(int argc, char** argv) {
   int x,y;
   int mark;

   int someArray [3][3];

   newDesk(someArray);
   showDesk(someArray);
   cout << "------------------\n";

   while (true)
   {
       cout<< "put x,y\n";
       cin >> x>>y;

       cout &开发者_运维问答lt;< "put mark\n";
       cin >> mark;

       setMark(someArray,x,y,mark);
       showDesk(someArray);
   }
       return 0;
}


== is comparison, = is assignment.

Your line:

someArray [x][y] == mark;

Does nothing. It should be:

someArray [x][y] = mark;


someArray [x][y] == mark tests the array to see if its equal to mark, but never actually sets it to anything...

0

精彩评论

暂无评论...
验证码 换一张
取 消