Hi!
I have to make a very simple game. There are 4 players in a 10x10 array, they get there random starting position and then go collecting other positions in the array. The player can go 1 position in any direction in one iteration, they can battle other players for there places, but they cant stand in the same place, because thats just useless.. I made a player class and then a few functions, everything works almost fine. There is such a thing that, sometimes, players jums 2 positions in a direction :/ Canf find why they are doing sush a thing :|
What does the ran(int) function do? If it's a std::rand-like function, check how it works. std::rand returns a random number inside the range [0,RAND_MAX] (RAND_MAX being a [usually irrelevant] value). You then have to use something like
PS: also, you have lots of unnecessary tests that could be reduced, to improve performance and clarity. Your while loop in next() could be replaced by:
1 2 3 4 5 6 7 8
while(true) {
if(xn > 9 || xn < 0 || xn == x)
xn = x + ran(3) - 1;
elseif(yn > 9 || yn < 0 || yn == y)
yn = y + ran(3) - 1;
elsebreak;
}
ran(int max) is a rand()%max function, just so that the code would be easier to read. I though i forgot to mention something i should have mentioned..
The other two functions look ok?
I had a very similar code with yours (a bit longer though). There is a problem: xn==y && yn==y is bad, because stays at the same position, but xn==x || yn==y is ok. In your variant xn==x || yn==y is not possible. I could go that way, but then there would be a lot of unused turns for players.. Ill add my full code, nothing too complicated.
P.S. Got a question about printing a array with the use of a function (got one there), it prints only one array, if i modified it so that it takes a array and prints i recieved an error :/ Thanks in advance!
I could of course hand in the programm as it is, becuase i think nobody will notice that it does not function exactly as i said it would, but i really want to make it function correctly^^
In your go function, you move, then check if the colour on that position is different from the player's, then keep moving and checking, until you get to a valid position. That way, your player can move the entire map if it gets the correct random movements. Example:
Board:
1000
0100
2010
0001
If your player is at position [0,0], your go function can check positions [1,1], [2,2] and [3,3], then find [3,2]. You player will then move from [0,0] to [3,2]. Maybe that's desirable, I'm just pointing it out.
Just cheked the return values, i dont need them, because each player has there own coordinates, thanks for pointing out.
Thanks mate, that kind of behavior is not desirable, any suggestions?
One can not simply move from [0,0] to [3,2].. Only to the positions next to the current loaction.
How did you find the problem so fast? From experiance?
Did a bit of correcting, deleted the color check from the go function and added the color check to the next function, how should a make a break for the check, because there is a possibility for the player to get stuck in the corner.. or by the side..
What you're doing is a kind of "brute force path finding", where you randomly hop around until you get to a valid position. Since your player can only move in (at most) 9 directions, you could check those directions before the random move checks, to see if there is at least one valid move.
But, if you do that, you can restrict your random moves to just these possible moves, avoiding the need to check for bounds or colours.
As for finding bugs, the first step is to know your problem. You had one that was very clear: your players sometimes moved two steps. Then you isolate the places in your code where you make movements, and look for spots (usually conditionals) where you might have done something wrong. It's not an easy task, but you get better at it the more you do it. Also, that's where encapsulation makes your task easier, because it reduces the places you have to look, by reducing the places where things can mess up other things.
Well, i made a little code to check if there are any positions to move to, seems to work flawlessly and that bothers me, can someone please take a look for possible problems? These lines were added to the next function. If numbers get equal to 9 (nowhere to move) the function stops, otherwise everything goes on as it used to.