Small 'Matrix' Game

[EDIT] Appears that the original owner has done a bunk.

If you don't supply runnable code then people can't reproduce your problem.

You had better decide whether procedure nextplayer() should return the next player by function return value or by reference argument. It is currently doing both. And causing chaos.
Last edited on
Let's see what happens here.

- Your program starts in line 45 with the declaration of some variables and player 'A' is selected as the first player.
- You enter an infinite loop (one would expect a way to end the game).
- In the loop the user should enter two numbers (one would expect to an instruction and probably it would be more clear if you first get the first value and then ask for the second value)

- The unknown "playermove" function is executed in line 64.
- The unknown "playermove" function is executed in line 66 and this time the return value is evaluated.
- The unknown "playermove" function is executed in line 72 and this time the return value is evaluated.
(one would expect that line 64 to 70 should be removed, but we don't know what this function does).

- if the "playermove" function in line 72 returns false, the loop starts over by asking the user for input without telling the user what is expected. This may cause a user to input the wrong kind of value.

- the next player function is called, changing the current player from 'A' to 'B'.
- 'B' is printed to the screen.

- the unknown function drawAline is executed

- the claimSpot function is called in line 80. This function uses a 'line'-object that seems not to have been declared.
- the claimSpot function returns false, which is ignored.

- the claimSpot function is called in line 82. This function uses a 'line'-object that seems not to have been declared.
- the claimSpot function returns false, which causes line 83 to 98 to be unreachable.
- the loop starts over by asking the user for input without telling the user what is expected. This may cause a user to input the wrong kind of value.

- if claimSpot was to to magically return true:
--- the function call to nextPlayer in line 83 would change the current player back to 'A'
--- the function call to nextPlayer in line 84 would undo this by changing the current player back to 'B'
--- some uninitialized variables would be declared
--- the random value of p would be incremented by one.
--- with an almost 50% probability player B would be declared winner (almost 50% because there are almost as many integer values between 3 and MAXINT as there are between MININT and 3)

I would like to suggest you:
1. initialize all your variables as soon as you declare them.
2. clean the code up (remove all unnecessary code/duplicate function calls and calls to not yet implemented functions).
3. add a short comment behind every function call telling what that function is expected to do.
4. add a short comment behind every condition (if statement) telling when it should be executed and when it should not.
5. provide all the code you have (including the parts that you don't think cause the problem). Not because the problem is there, but because it is impossible to guess what influence a function may have on the rest of a program without knowing the function.

Kind regards, Nico
POST COMPILEABLE CODE

In function 'bool playermove(int, int, char)':
55:13: error: 'line' was not declared in this scope
67:13: error: 'line' was not declared in this scope
74:13: error: 'line' was not declared in this scope
85:13: error: 'line' was not declared in this scope
 In function 'void drawAline(int, int, char)':
104:13: error: 'line' was not declared in this scope
108:13: error: 'line' was not declared in this scope
119:13: error: 'line' was not declared in this scope
123:13: error: 'line' was not declared in this scope
 In function 'bool claimSpot(int, int)':
138:9: error: 'line' was not declared in this scope
141:19: error: 'space' was not declared in this scope
144:9: error: 'line' was not declared in this scope
147:19: error: 'space' was not declared in this scope
150:9: error: 'line' was not declared in this scope
153:19: error: 'space' was not declared in this scope
156:9: error: 'line' was not declared in this scope
159:19: error: 'space' was not declared in this scope



What is the point of this?
1
2
                nextplayer(player);
                player = nextplayer(player);




On the basis of this code, neither n or p is ever going to be bigger than 1, so neither player can win. This bears no resemblance to your stated problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
                int n=0;
                int p=0;

                if (player == 'A') {
                    n = n + 1;
                }
                if (player == 'B') {
                    p = p + 1;
                }

                //Who has more squares claimed? Out of four
                //If A or B has more than 3 squares he automatically wins, has the majority of 4 squares.
                if (n > 3 || p > 3

line isn't defined in the code given above - so it doesn't compile.
The code uses a variable called line - which isn't defined!

eg
if (line.horiz(rows, colum) == true) {

Topic archived. No new replies allowed.