You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于文本的Monopoly游戏:骰子数与数组索引匹配问题及代码修复

Fixing the Memory Address Output in Your Text-Based Monopoly Game

Hey there! Let's tackle that annoying memory address issue you're seeing instead of the grid position number. Here's exactly what's going wrong and how to fix it, plus a few extra tips to polish your game.


The Root Cause of the Memory Address

That weird output like 006ff65 comes from this line:

cout << "# You move to grid "<< values << endl;

In C++, when you print an array name directly, it doesn't output the elements—it outputs the memory address of the first element in the array. That's why you're getting that gibberish instead of your desired grid number.


Step-by-Step Fix

First, let's align with your original goal: you want to add the rolled dice number to the starting index (0) to get the target grid index, then show that position. To do this properly, we need to track the player's current position, calculate the new position, and then print the correct value.

1. Add a Current Position Tracker

Start by adding a variable to keep track of where the player is on the board (initial position is index 0, which maps to grid 1):

int currentPosition = 0; // Starts at the first grid spot

2. Calculate the Target Position

After rolling the dice, compute the new index. Don't forget to handle the classic Monopoly "wrap-around" logic for when the player passes the 40th grid:

int targetIndex = currentPosition + rnum;
// Wrap around if we exceed the 40-grid board
if (targetIndex >= 40) {
    targetIndex -= 40;
    // Optional: Add the "pass Go" reward here!
    cout << "# You passed Go! Collect $2000!" << endl;
    moneyscore += 2000;
}

3. Print the Correct Grid Number

Replace the problematic line with this to print the actual grid value. Since your values array is just 1-40 in order, you don't even need the array—you can calculate the grid number directly:

cout << "# You move to grid " << (targetIndex + 1) << endl;

4. Clean Up Unnecessary Code

Remove this line—it overwrites your rolled dice number with an array value, which will mess up future turns:

rnum = values[rnum]; // Delete this!

Bonus Improvements

  • Seed the Random Number Generator: Right now, your rand() will produce the same sequence of numbers every time you run the game. Fix this by adding this at the top of main():
    srand(time(0)); // Uses the current time to create unique random sequences
    
  • Fix Input Handling: After using cin >> choiceOne_Path, there's a leftover newline in the input buffer. If you add more getline() calls later, clear it with cin.ignore() to avoid unexpected behavior.
  • Track Owned Properties: For the "Buy Property" and "Receive Rent" features, you'll want an array to track which grids the player owns—something like bool owned[40] = {false}; that you update when a property is bought.

Full Fixed Code Snippet

Here's the updated code with all the fixes included:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>

using namespace std;

int main() {
    srand(time(0)); // Initialize random seed for unique dice rolls
    int number = 12;
    int rnum = (rand() % number) + 1;
    int house = 1;
    int moneyscore = 10000;
    char name[50];
    int currentPosition = 0; // Player starts at grid 1 (index 0)

    cout << "Who are you, Dog, Car, Hat or Bus?" << endl;
    cin.getline(name, 50);
    cout << "Welcome to Our Game " << name << "! You have " << moneyscore << ". Please roll the dice to get started." << endl;
    cout << "\n----------------------Press Enter to roll dice----------------------" << endl;
    cin.get(); // Wait for user to press enter
    system("cls");

    // Calculate new position with wrap-around logic
    int targetIndex = currentPosition + rnum;
    if (targetIndex >= 40) {
        targetIndex -= 40;
        cout << "# You passed Go! Collect $2000!" << endl;
        moneyscore += 2000;
    }

    cout << "# You roll a " << rnum << endl;
    cout << "# You have " << moneyscore << endl;
    cout << "# You move to grid " << (targetIndex + 1) << endl;
    currentPosition = targetIndex; // Update position for next turn

    cout << "\t >> Enter '1' Buy Property" << endl;
    cout << "\t >> Enter '2' Receive Rent" << endl;
    cout << "\t >> Enter '3' End turn" << endl;

    retry:
    cout << "\nEnter your choice: ";
    int choiceOne_Path;
    cin >> choiceOne_Path;

    if (choiceOne_Path == 1) {
        cout << "\nBuy Property" << endl;
        cout << " " << name << " has " << moneyscore << endl;
        cout << " 1 House has been placed by " << name << " who spent 2,500" << endl;
        moneyscore -= 2500;
        cout << " " << name << " now has " << moneyscore << endl;
        cout << "\nRoll again" << endl;
        rnum = (rand() % number) + 1;
        cout << "# You roll a " << rnum << endl;
    } else if (choiceOne_Path == 2) {
        cout << "\nYou received 2500 from rent" << endl;
        moneyscore += 2500;
        cout << " " << name << " now has " << moneyscore << endl;
        cout << "\n(Note: Later, you'll need to track owned positions to calculate correct rent amounts)" << endl;
        cout << "Ends turn" << endl;
    } else if (choiceOne_Path == 3) {
        cout << "\nRoll again" << endl;
        rnum = (rand() % number) + 1;
        cout << "# You roll a " << rnum << endl;
    } else {
        cout << "You're doing it wrong, player! Press '1', '2', or '3'—nothing else!" << endl;
        goto retry;
    }

    cout << "\n----------------------Press any key to continue----------------------" << endl;
    _getch();
    return 0;
}

内容的提问来源于stack exchange,提问作者Antonio B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 13:02:27