Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, 7th_zorro, dr_panther), 1,297 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Sorting problem #337062
08/07/10 23:39
08/07/10 23:39
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Hello,
I am trying to sort some randomly generated player turns and then correctly assign them and also make sure there are no repetitions. I will show what I mean below:

I have randomly generated 4 numbers for players from the code below, my goal is that the player with the highest number gets to go first, the player with the second highest number goes next, etc. etc.

Code:
function turns()
{
	random_seed(0);
	player1_turn = integer(random(100));
	turn_array[0] = player1_turn;
	
	player2_turn = integer(random(100));
	turn_array[1] = player2_turn;
		
	player3_turn = integer(random(100));
	turn_array[2] = player3_turn;
		
	player4_turn = integer(random(100));
	turn_array[3] = player4_turn;
}




Next I have sorted the data with a reverse bubble sort like this:
Code:
////////simple bubble sort///////
var i;
var j;
var hold;
	
for (i=0; i<7; i++) //passes
{
 for (j=0; j<7; j++) //one pass
 {
	if (turn_array[j] < turn_array[j+1]) //one comparison 
	{
	 hold = turn_array[j];   //one swap
	 turn_array[j] = turn_array[j+1];
	 turn_array[j+1] = hold;
				
	}
  }
 }



All of this code works successfully, my 4 generated numbers are sorted from highest to lowest. But I still have two problems:

#1 PROBLEM is that I need to assign the turn order to the respective players

#2 PROBLEM I don't know how to deal with situations where I have two of the same numbers like if 2 "45"'s show up.


So I was hoping there was a way to fix one or both of these problems in an efficient manner, but any help or directions at all would be excellent.

Let me know if I didn't describe the problem clearly. crazy

Thanks

Re: Sorting problem [Re: NITRO777] #337064
08/08/10 00:36
08/08/10 00:36
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
there is a solution to your second problem in this thread:

http://www.opserver.de/ubb7/ubbthreads.p...true#Post320572

Re: Sorting problem [Re: ventilator] #337068
08/08/10 02:24
08/08/10 02:24
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Hi,
Thanks thats an interesting method I haven't seen before but it makes the game slightly unfair if the first number chosen is the highest no one else will ever get a chance to get that number.

What I was hoping for is a solution which allows for repetition of the same numbers, but if that occurs will allow them to try again specifically against each other, then re-sort them in order.

Then I still need a way to assign the turn order to the structure or entity which generated it.

The players themselves will probably just be structs and the turn order number will just be a data member of that particular struct. Unless I can think of a better way of doing it with arrays or perhaps I will make each one an entity and store it's turn order in a skill?

Anyway, thanks for the input I definitely learned a new method.

Re: Sorting problem [Re: NITRO777] #337069
08/08/10 02:42
08/08/10 02:42
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
Originally Posted By: NITRO777
Hi,
Thanks thats an interesting method I haven't seen before but it makes the game slightly unfair if the first number chosen is the highest no one else will ever get a chance to get that number.

That's slightly flawed logic. Each player has the same chance of going 1st.

i)Imagine 3 players,

PLAYER A picks from 1 2 or 3, he has 33.333r% chance of picking any number, we'll choose 3 (highest) to demonstrate.

Once he's picked 3, PLAYER B and PLAYER C still have an equal chance of going next (50%).

If PLAYER B picks 2, this now leaves PLAYER C with 1, going last (unfair? no).

Use this logic backwards now.

If PLAYER A picks 1, B and C now have 50% chance of going 1st, increasing both of their odds.

If PLAYER B now picks 2 again, this guarantees PLAYER C going 1st even though both times he hasn't picked and has the same chance the whole way through.

ii) imagine 13 players with a suit of cards.

There's 1/13 chance of getting the ace for each player.

If PLAYER 1 draws the ace, everyone still thinks they have the same chance of drawing the ace until it's actually revealed, just because they're not going 1st doesn't lower their chances of getting it.

Trawl the internet for 'Progressive Probability' for more examples. grin

Originally Posted By: NITRO777
Then I still need a way to assign the turn order to the structure or entity which generated it.

When I made a turnbased game, I initially thought too that you'll then need to change the turns. but that's the reason why you're generating the initial turn?

Originally Posted By: NITRO777
The players themselves will probably just be structs and the turn order number will just be a data member of that particular struct. Unless I can think of a better way of doing it with arrays or perhaps I will make each one an entity and store it's turn order in a skill?

For storing whose turn it is, just have an integer array, which saves each position from the initial randomisation. Then just ++ through the array until the end.

You can also assign this number to a skill for each entity, then reference it to make sure they're moving the right pieces.

Hope this helps

Re: Sorting problem [Re: MrGuest] #337081
08/08/10 12:14
08/08/10 12:14
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Quote:
That's slightly flawed logic. Each player has the same chance of going 1st.

Ahh that is interesting. I did look online and it appears that it is called "conditional probability" and most closely models situations such as drawing straws. After studying your post and others online I now see the logic of it, thanks.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1