Always says do not and do buy if r1t = 3

closed account (NUj6URfi)
My code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    
    int r1;
    int r2;
    int r1t;
    int r2t;
    int loopcount;
    int money1;
    int money2;
    int dumb;
    int boardtilenum;
    int turn;
    int jaildice1;
    int jaildice2;
    int random;
    int reserve;
    int texasstadiumhouses = 0;
    int jacobsfieldhouses = 0;
    string a;
    money1 = 20000000;
    money2 = 20000000;
    bool bankrupt1;
    bool bankrupt2;
    bool jacobsfieldpurchased;
    int jacobsfieldowner;
    bool texasstadiumpurchased;
    int texasstadiumowner;
    bool ohareiapurchased;
    bool grandoleoprypurchased;
    int ohareiaowner;
    int grandoleopryowner;
    bool jail1;
    bool jail2;
    bankrupt1 = false;
    bankrupt2 = false;
    loopcount = 1;
    jacobsfieldpurchased = false;
    texasstadiumpurchased = false;
    ohareiapurchased = false;
    grandoleoprypurchased = false;
    bool gatewayarchpurchased = false;
    int gatewayarchowner;
    bool mallofamericapurchased = false;
    int mallofamericaowner;
    bool cenolyparpurchased = false;
    int cenolyparowner;
    bool cellphoneservicepurchased = false;
    int cellphoneserviceowner;
    bool redrocamppurchased = false;
    int redrocampowner;
    jail1 = false;
    jail2 = false;
    money1 = 20000000;
    money2 = 20000000;
    bool gamestatus = false;
    reserve = 3000000;
    while (bankrupt1 == false) {
    while (bankrupt2 == false) {
          if (jail1 == true) {
                    random = 1;
                    jaildice1 = 0;
                    jaildice2 = 14;
                    while (random <= 3) {
                   cout << "You are in jail. Please enter your first dice number. After that please enter the second dice number.";
                   cin >> jaildice1;
                   cin >> jaildice2;
                   if (jaildice1 == jaildice2)  {
                   r1t = 10;
                   jail1 = false;
                   break;
                   r1t = 10; }
                   random++;
                   }
                   r1t = 10;
                   }
          if (jail1 == false) {
                    if (loopcount == 1) {
          cout << "This program recognizes:\nJacobs Field\nTexas Stadium\nO'Hare International Airport\nGrand Ole Opry\nGateway Arch\nMall of America\nCentenniel Olympic Park\nCell Phone Service\nRed Rocks Amphitheatre\nLiberty Bell\nLos Angeles International Airport\nSouth Beach\n";
          cout << "Jhonson Space Center\nPioneer Square\nCamelback MTN.\nWaikiki Beach\nDisney World\nJhon F. Kennedy International Airport\nFrench Quarter\n Hollywood\nInternet Service\nGolden Gate Bridge\nLas Vegas BLVD.\nWrigley Field\nWhite House\nH-J Atlanta International Airport\nFenway Park\nTimes Square"; 
          loopcount++; }
          cout << "\n\nWhat did you roll P1?"; 
          cin >> r1;
          if (r1 >> 12) {
             cout << "Cheater. Closing.";
             cin >> dumb;
             return 0; }
          if (r1 == 1) {
             cout << "Cheater. Closing.";
             cin >> dumb;
             return 0; }
          r1t = r1t + r1;
          if (r1t >= 41) {
          r1t - 40; }
          if (r1t == 1) {
                  cout << "You are on Jacobs field.\n";
                       if (jacobsfieldpurchased == false) {
                                                if (money1 >= (reserve + 600000)) {
                                                           cout << "Purchase Jacobs Field.\n";  }
                                                if (texasstadiumpurchased  == true) {
                                                                           if (texasstadiumowner == 1) {
                                                                                                 cout << "Buy it and you will have a set.\n"; }
                                                                           if (texasstadiumowner == 2) {
                                                                                                 cout << "Definitely.\n"; }
                                                                                                 }
                                                                                                 }
                                                if (money1 << reserve + 600000) {
                                                           cout << "It's not worth it UNLESS TEXAS STADIUM IS OWNED.\n"; }
                                                           
                    if (jacobsfieldpurchased = true) {
                                              if (jacobsfieldowner == 2) {
                                              cout << "You owe player 2:\n";
                                              if (jacobsfieldhouses == 0) {
                                                                    cout << "40000\n";
                                                                    }
                                              if (jacobsfieldhouses == 1) {
                                                                    cout << "200000\n"; 
                                                                    } 
                                              if (jacobsfieldhouses == 2) {
                                                                    cout << "600000\n";
closed account (NUj6URfi)
Code not fully uploaded yet.
closed account (NUj6URfi)
That is the important part though.
line 116 : use == operator, not =.

Also, you need to make more use of functions - already too much code in main.

If you format your IDE to use spaces instead of tabs it will display a lot better on this site - the indenting.

You can build up output with cout - no need to have massively long statements, that are 3 screens wide on my system.

The code will be easier to read if you place the closing brace in the same column as it's opening statement:

94
95
96
97
98
99
         if (r1 == 1) {
             cout << "Cheater. Closing.";
             cin >> dumb;
             return 0; 
         } // <--- here


EDIT: Were you intending to do bit shifting on line 90?

I haven't looked at anything else in detail.
Last edited on
closed account (NUj6URfi)
Rest of code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
} 
                                              if (jacobsfieldhouses == 3) {
                                                                    cout << "1800000\n";
                                                                    }
                                              if (jacobsfieldhouses == 4) {
                                                                    cout << "3200000\n";
                                                                    }
                                              if (jacobsfieldhouses == 5) {
                                                                    cout << "4500000\n";
                                                                    }
                                              }
                                              if (jacobsfieldowner == 1) {
                                                                   cout  << "You own this property.\n"; }
                                              }}
          if (r1t == 2) {
                  cout << "You are on Community chest. Please take a card.\n"; }
          if (r1t == 3) {
                  cout << "You are on Texas stadium.\n"; 
                  if (texasstadiumpurchased == false) {
                                                if (money1 >> (2600000)) {
                                                           cout << "Purchase Texas Stadium.";  }
                                                if (jacobsfieldpurchased  == true) {
                                                                           if (jacobsfieldowner == 1) {
                                                                                                 cout << "Buy it and you will have a set.\n"; }
                                                                           if (jacobsfieldowner == 2) {
                                                                                                 cout << "Definitely.\n"; }
                                                                                                 }
                                                                                                 }
                                                if (money1 << 2600000) {
                                                           cout << "It's not worth it UNLESS JACOBS FIELD IS OWNED.\n"; }
                    if (texasstadiumpurchased == true) {
                                              if (texasstadiumowner == 2) {
                                              cout << "You owe player 2:\n";
                                              if (texasstadiumhouses == 0) {
                                                                    cout << "40000\n";
                                                                    }
                                              if (texasstadiumhouses == 1) {
                                                                    cout << "200000\n"; 
                                                                    } 
                                              if (texasstadiumhouses == 2) {
                                                                    cout << "600000\n";
                                                                    } 
                                              if (texasstadiumhouses == 3) {
                                                                    cout << "1800000\n";
                                                                    }
                                              if (texasstadiumhouses == 4) {
                                                                    cout << "3200000\n";
                                                                    }
                                              if (texasstadiumhouses == 5) {
                                                                    cout << "4500000\n";
                                                                    }
                                              }
                                              if (texasstadiumowner == 1) {
                                                                   cout  << "You own this property.\n"; }
                                              }
                                              }
                  
                                                
          if (r1t == 4) {
                  cout << "You are on Income tax.";
                  if (money1 == 20000000) {
                     cout << "Pay either one."; }
                  if (money1 >> 20000001) {
                     cout << "Pay 2m."; }
                  if (money1 << 19999999) {
                     cout << "Pay 10%."; }
                     }
          if (r1t == 5) {
                  cout << "You are on O'hare International Airport.";
                       if (ohareiapurchased == false) {
                                                if (money1 >> reserve) {
                                                           cout << "Purchase O'hare International Airport.";  }
                                                if (money1 <= reserve) {
                                                           cout << "It's not worth it."; }
                                                           }
                                                           }
          if (r1t == 6) {
                  cout << "You are on Grand Ole Opry, Nashville.";}
          if (r1t == 7) {
                  cout << "You are on Chance. Please take a card.";}
          if (r1t == 8) {
                  cout << "You are on Gateway Arch.";}
          if (r1t == 9) {
                  cout << "You are on Mall of America.";}
          if (r1t == 10) {
                  if (jail2 == true) {
                            cout << "You are visiting P2.";}
                  else
                            cout << "You are on Just visting.";
                  }
          if (r1t == 11) {
                  cout << "You are on Centennial Olympic Park.";}
          if (r1t == 12) {
                  cout << "You are on Cell Phone service.";}
          if (r1t == 13) {
                  cout << "You are on Red Rocks Amphitheatre.";}
          if (r1t == 14) {
                  cout << "You are on Liberty Bell.";}
          if (r1t == 15) {
                  cout << "You are on Los Angeles International Airport.";}
          if (r1t == 16) {
                  cout << "You are on South Beach.";}
          if (r1t == 17) {
                  cout << "You are on Community Chest. Please take a card.";}
          if (r1t == 18) {
closed account (NUj6URfi)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
cout << "You are on Johnson Space Center.";}
          if (r1t == 19) {
                  cout << "You are on Pioneer Square.";}
          if (r1t == 20) {
                  cout << "You are on Free Parking.";}
          if (r1t == 21) {
                  cout << "You are on Camelback MTN..";}
          if (r1t == 22) {
                  cout << "You are on Chance. Please take a card.";}
          if (r1t == 23) {
                  cout << "You are on Wakiki Beach.";}
          if (r1t == 24) {
                  cout << "You are on Disney World.";}
          if (r1t == 25) {
                  cout << "You are on Jhon F. Kennedy International Airport.";}
          if (r1t == 26) {
                  cout << "You are on French Quarter.";}
          if (r1t == 27) {
                  cout << "You are on Hollywood.";}
          if (r1t == 28) {
                  cout << "You are on Internet Service.";}
          if (r1t == 29) {
                  cout << "You are on Golden Gate Bridge.";}
          if (r1t == 30) {
                  cout << "You are on Go to Jail.";
                  jail1 = true; }
          if (r1t == 31) {
                  cout << "You are on Las Vegas BLVD..";}
          if (r1t == 32) {
                  cout << "You are on Wrigley Field.";}
          if (r1t == 33) {
                  cout << "You are on Community Chest. Please take a card..";}
          if (r1t == 34) {
                  cout << "You are on White House..";}
          if (r1t == 35) {
                  cout << "You are on H-J Atlanta International Airport.";}
          if (r1t == 36) {
                  cout << "You are on Chance. Please take a card";}
          if (r1t == 37) {
                  cout << "You are on Fenway Park.";}
          if (r1t == 38) {
                  cout << "You are on Interest On Credit Card Debit. Pay 750000.";}
          if (r1t == 39) {
                  cout << "You are on Times Square.";}
          if (r1t == 40) {
                  cout << "You are on Go. Collect 2m";}
          }
          cout << "\nPlayer 2.\n";
          if (jail2 == true) {
                    jaildice1 = 0;
                    jaildice2 = 14;
                    random = 1;
                    while (random <= 3) {
                   cout << "You are in jail. Please enter your first dice number. After that please enter the second dice number.";
                   cin >> jaildice1;
                   cin >> jaildice2;
                   if (jaildice1 == jaildice2)  {
                   r1t = 10;
                   jail2 = false;
                   break; }
                   random++;
                   }
                   r1t = 10;
                   }
          if (jail2 == false) {
          cout << "\n\nWhat did you roll P2?"; 
          cin >> r2;
          if (r2 >> 12) {
             cout << "Cheater. Closing.";
             cin >> dumb;
             return 0; }
          if (r2 == 1) {
             cout << "Cheater. Closing.";
             cin >> dumb;
             return 0; }
          r2t = r2t + r2;
          if (r2t >= 41) {
          r1t - 40; }
          if (r2t == 1) {
                  cout << "You are on Jacobs field.\n"; }
          if (r2t == 2) {
                  cout << "You are on Community chest. Please take a card."; }
          if (r2t == 3) {
                  cout << "You are on Texas stadium."; }
          if (r2t == 4) {
                  cout << "You are on Income tax.";}
          if (r2t == 5) {
                  cout << "You are on O'hare International Airport."; }
          if (r2t == 6) {
                  cout << "You are on Grand Ole Opry, Nashville.";}
          if (r2t == 7) {
                  cout << "You are on Chance. Please take a card.";}
          if (r2t == 8) {
                  cout << "You are on Gateway Arch.";}
          if (r2t == 9) {
                  cout << "You are on Mall of America.";}
          if (r2t == 10) {
                  if (jail1 == true) {
                            cout << "You are visiting P1.";}
                  else
                            cout << "You are on Just visting.";
                  }
          if (r2t == 11) {
                  cout << "You are on Centennial Olympic Park.";}
          if (r2t == 12) {
                  cout << "You are on Cell Phone service.";}
          if (r2t == 13) {
                  cout << "You are on Red Rocks Amphitheatre.";}
          if (r2t == 14) {
                  cout << "You are on Liberty Bell.";}
          if (r2t == 15) {
                  cout << "You are on Los Angeles International Airport.";}
          if (r2t == 16) {
                  cout << "You are on South Beach.";}
          if (r2t == 17) {
                  cout << "You are on Community Chest. Please take a card.";}
          if (r2t == 18) {
                  cout << "You are on Johnson Space Center.";}
          if (r2t == 19) {
                  cout << "You are on Pioneer Square.";}
          if (r2t == 20) {
                  cout << "You are on Free Parking.";}
          if (r2t == 21) {
                  cout << "You are on Camelback MTN..";}
          if (r2t == 22) {
                  cout << "You are on Chance. Please take a card.";}
          if (r2t == 23) {
                  cout << "You are on Wakiki Beach.";}
          if (r2t == 24) {
                  cout << "You are on Disney World.";}
          if (r2t == 25) {
                  cout << "You are on Jhon F. Kennedy International Airport.";}
          if (r2t == 26) {
                  cout << "You are on French Quarter.";}
          if (r2t == 27) {
                  cout << "You are on Hollywood.";}
          if (r2t == 28) {
                  cout << "You are on Internet Service.";}
          if (r2t == 29) {
                  cout << "You are on Golden Gate Bridge.";}
          if (r2t == 30) {
                  cout << "You are on Go to Jail.";
                  jail2 = true; }
          if (r2t == 31) {
                  cout << "You are on Las Vegas BLVD..";}
          if (r2t == 32) {
                  cout << "You are on Wrigley Field.";}
          if (r2t == 33) {
                  cout << "You are on Community Chest. Please take a card..";}
          if (r2t == 34) {
                  cout << "You are on White House..";}
          if (r2t == 35) {
                  cout << "You are on H-J Atlanta International Airport.";}
          if (r2t == 36) {
                  cout << "You are on Chance. Please take a card";}
          if (r2t == 37) {
                  cout << "You are on Fenway Park.";}
          if (r2t == 38) {
                  cout << "You are on Interest On Credit Card Debit. Pay 750000.";}
          if (r2t == 39) {
                  cout << "You are on Times Square.";}
          if (r2t == 40) {
                  cout << "You are on Go.";}
          }
          }
          }
cin.get();
return 0;
}
Is the second round of if (r2t == 1) through to if (r2t == 40) a repeat of the first? I didn't look at it all in detail, but if it is you need to rethink your design.

Btw, You can make use of the [ code firstline=][/ code] feature to ensure the line numbering is right in all of your posts. Like I did in my last post.

Hope all is well.
closed account (NUj6URfi)
Thanks for that firstline thing. r2t is roll player 2 total which doesn't have help yet r1t does because the program is made to have p2 lose and p1 win.
Hi toad1359,

I mentioned the firstline feature so you could edit your posts, so we could refer to your code with the correct line numbers. Can you also fix the tabs? In your IDE set indenting to be 4 spaces - not tabs, then reformat it, & edit your post by re-pasting your code . Some places in your code has 24 spaces worth of indenting.

If there is a bunch of code that is very similar for p1 & p2, can you figure out a way of writing one function that deals with both? If you find yourself repeating code, then you are probably doing something wrong.

You could have an array of strings that represent each tile in your monopoly game
(could have put that in the topic title). Then you could have a function that prints out the appropriate string based on it's number:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

unsigned short Player1Roll = 0; // Always initialise variables

const std::string[] Tile = {
     "You are on Jacobs field.",
     "You are on Community chest. Please take a card.",
     // etc for all the rest
}

void PrintTile(const unsigned short TileNum) {
    std::cout << Tile[TileNum] << "\n";

//calling the function

PrintTile(Player1Roll);
}


Declare you all your functions before main - hopefully in the same order that you call them, then define them in the same order after main. If you do this, you might find you have less variables in main, as more of them could be local to the functions.

Also with variable & function names, don't abbreviate them too much. Well named variables assist greatly in self documenting the code, and in other peoples understanding. People shouldn't have to ask what r1t or r2t means. Names like Player1, Player2, Player1Roll are much better names.

With functions, it is a good idea to have a divide & conquer approach. There was a rule that any function (including main) should be no longer than 80 LOC, this was so a function could be printed on 1 A4 page. These days 40LOC is more acceptable. Candidates for functions could include the compound statements that go with if-else if- else, loops, and switches. So rather than have a bunch of code in an if statement, call a function instead. With switches, have each case call a function. This idea also helps with understanding, as one can have a higher level view rather than a bunch of detail.

Hopefully all this info will help you write better code :+)

http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/reference/
http://www.cplusplus.com/articles/
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml


Last edited on
40 LOC? I agree don't let your functions get too long if you can help it. I've seen some people write functions over 1000 LOC and I just think "Really that's ridiculous" as it really wasn't necessary. However if 40-80 LOC is preferable then I'm not always matching up. I have wrote functions that go up to 300 LOC. I guess that wasn't too good...
@Austin J

Have a look at the Google style guide link I posted.

The main idea is to write code that is: clear, simple as is reasonable, easy to understand, easy to debug, easy to modify, re-usable, easy to test, and more than anything else - it always works.

A couple of things:

1. If I was a maintenance programmer, and your function had even 100LOC - I wouldn't be happy. Plus you should get nailed in the code review. The purpose of a code review is to at least see if the code complies with the style guide.

2. If someone is going to write code that attempts to always work, there is more to it than writing a function & hoping for the best. One has to try and cover as many of the situations which might cause it to fail, like all the pre-conditions and things like "What exceptions does this function we are using throw?"

Things being relatively simple and easy to understand, goes a long way to avoiding screw-ups. Stroustrup wrote an article about how one of the Mars Lander projects failed because code was written using the wrong system of units - people in the US use imperial units, while physicists use metric.....

Here is another style guide, which is for mission critical avionics code.
http://www.stroustrup.com/JSF-AV-rules.pdf
Last edited on
Topic archived. No new replies allowed.