9 added 187 characters in body
source | link

I'm making a game battle system, where there are 2 players.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

At the moment I have this code:

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but I feel that this is confusing and the 'wrong way' to do it.

I need tips on how I can code this in a better way.

UPDATE: New version of the code including all the suggestions can be found here.

I'm making a game battle system, where there are 2 players.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

At the moment I have this code:

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but I feel that this is confusing and the 'wrong way' to do it.

I need tips on how I can code this in a better way.

I'm making a game battle system, where there are 2 players.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

At the moment I have this code:

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but I feel that this is confusing and the 'wrong way' to do it.

I need tips on how I can code this in a better way.

UPDATE: New version of the code including all the suggestions can be found here.

    Tweeted twitter.com/#!/StackCodeReview/status/457979910607618048
8 added 18 characters in body
source | link

I'm making a game battle system, where there are 2 players.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

At the moment I have this code:

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but i I feel that this is confusing and the 'wrong way' to do it.

I need tips on how i I can code this in a better way.

I'm making a game battle system, where there are 2 players.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

At the moment I have this code:

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but i feel that is confusing and 'wrong way'.

I need tips on how i can code this in a better way.

I'm making a game battle system, where there are 2 players.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

At the moment I have this code:

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but I feel that this is confusing and the 'wrong way' to do it.

I need tips on how I can code this in a better way.

7 Rollback to Revision 5
source | link

It is like a round based game.

  • Every player can have multiple units to control, but can only use one of them each round. at a time.
  • Every unit has an int CurrentSpeedcurSpeed variable;

I need to have in 2 variables who attacks first (Player firstPlayerf) and who attacks second (Player secondPlayers), based on the speed of the active Unit current controlling unit.

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
public class Player {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Unit> Party { get; set; }
    public Unit Active { get; set; }
}

public class Unit {
    public string Name;
    public int InitialSpeed { get; set; }
    public int CurrentSpeed { get; set; }
}

public class BattleSystem {

    private void BattleTurn(){
        Player firstPlayerf = null; //Player attacking first
        Player secondPlayers = null; //Player attacking second

        //Check the speed of the units, and determine who attack first
        firstPlayerf = player_1.Activecurrent.CurrentSpeedcurSpeed > player_2.Activecurrent.CurrentSpeedcurSpeed ? player_1 : player_1.Activecurrent.CurrentSpeedcurSpeed < player_2.Activecurrent.CurrentSpeedcurSpeed ? player_2 : null;

        //if f is null (units have the same speed)
        if(firstPlayer == nullf==null){
            //Randomize who goes first
            System.Random rnd = new System.Random();
            int rng = rnd.Next(0,2);
            firstPlayerf = rng == 0 ? player_1 : player_2;
            secondPlayers = rng == 0 ? player_2 : player_1;
        }else{
            secondPlayers = firstPlayerf.Idid == player_1.Idid ? player_2 : player_1;
        }
    }
}

It is working, but i feel that is confusing and 'wrong way'.

The part of determine the firstPlayer is ok, but i don't like the checks and assignements of the secondPlayer, like checking the ids.

I need tips on how i can code this in a better way.

EDIT: I have edited the code with all the suggestion from comment and answers.

It is like a round based game.

  • Every player can have multiple units to control, but can only use one of them each round..
  • Every unit has an int CurrentSpeed variable;

I need to have in 2 variables who attacks first (Player firstPlayer) and who attacks second (Player secondPlayer), based on the speed of the active Unit.

public class Player {
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Unit> Party { get; set; }
    public Unit Active { get; set; }
}

public class Unit {
    public string Name;
    public int InitialSpeed { get; set; }
    public int CurrentSpeed { get; set; }
}

public class BattleSystem {

    private void BattleTurn(){
        Player firstPlayer = null; //Player attacking first
        Player secondPlayer = null; //Player attacking second

        //Check the speed of the units, and determine who attack first
        firstPlayer = player_1.Active.CurrentSpeed > player_2.Active.CurrentSpeed ? player_1 : player_1.Active.CurrentSpeed < player_2.Active.CurrentSpeed ? player_2 : null;

        //if f is null (units have the same speed)
        if(firstPlayer == null){
            //Randomize who goes first
            System.Random rnd = new System.Random();
            int rng = rnd.Next(2);
            firstPlayer = rng == 0 ? player_1 : player_2;
            secondPlayer = rng == 0 ? player_2 : player_1;
        }else{
            secondPlayer = firstPlayer.Id == player_1.Id ? player_2 : player_1;
        }
    }
}

It is working, but i feel that is confusing and 'wrong way'.

The part of determine the firstPlayer is ok, but i don't like the checks and assignements of the secondPlayer, like checking the ids.

I need tips on how i can code this in a better way.

EDIT: I have edited the code with all the suggestion from comment and answers.

  • Every player can have multiple units to control, but only one at a time.
  • Every unit has an int curSpeed variable;

I need to have in 2 variables who attacks first (Player f) and who attacks second (Player s), based on the speed of the current controlling unit.

  • player_1 and player_2 are instances of Player Class
  • current, is a instance of Unit Class, it is the current controlling unit.
Player f = null; //Player attacking first
Player s = null; //Player attacking second

//Check the speed of the units, and determine who attack first
f = player_1.current.curSpeed > player_2.current.curSpeed ? player_1 : player_1.current.curSpeed < player_2.current.curSpeed ? player_2 : null;

//if f is null (units have the same speed)
if(f==null){
    //Randomize who goes first
    System.Random rnd = new System.Random();
    int rng = rnd.Next(0,2);
    f = rng == 0 ? player_1 : player_2;
    s = rng == 0 ? player_2 : player_1;
}else{
    s = f.id == player_1.id ? player_2 : player_1;
}

It is working, but i feel that is confusing and 'wrong way'.

I need tips on how i can code this in a better way.

6 edited code with suggestions
source | link
5 edited title
| link
4 added 33 characters in body; edited tags
source | link
3 title, formatting, and tags
source | link
2 added 6 characters in body
source | link
1
source | link