Changeset 59


Ignore:
Timestamp:
Sep 26, 2014, 12:05:30 AM (10 years ago)
Author:
chronos
Message:
  • Added: Move win probability calculation.
  • Fixed: Attack processing to be same as Risk game system.
Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMove.lfm

    r42 r59  
    11object FormMove: TFormMove
    2   Left = 770
     2  Left = 435
    33  Height = 257
    4   Top = 484
     4  Top = 165
    55  Width = 320
    66  ActiveControl = ButtonOk
     
    88  ClientHeight = 257
    99  ClientWidth = 320
     10  OnCreate = FormCreate
    1011  OnShow = FormShow
    11   LCLVersion = '1.3'
     12  LCLVersion = '1.0.10.0'
    1213  object SpinEditOnce: TSpinEdit
    1314    Left = 208
    14     Height = 32
    15     Top = 16
     15    Height = 27
     16    Top = 32
    1617    Width = 98
    1718    OnChange = SpinEditOnceChange
     
    2021  object Label1: TLabel
    2122    Left = 24
    22     Height = 22
    23     Top = 16
    24     Width = 46
     23    Height = 18
     24    Top = 32
     25    Width = 36
    2526    Caption = 'Once:'
    2627    ParentColor = False
     
    2930    Left = 184
    3031    Height = 25
    31     Top = 208
     32    Top = 224
    3233    Width = 75
    3334    Caption = 'Ok'
     
    3940    Left = 72
    4041    Height = 25
    41     Top = 208
     42    Top = 224
    4243    Width = 75
    4344    Caption = 'Cancel'
     
    4748  object Label2: TLabel
    4849    Left = 24
    49     Height = 22
    50     Top = 120
    51     Width = 87
     50    Height = 18
     51    Top = 136
     52    Width = 66
    5253    Caption = 'Every turn:'
    5354    ParentColor = False
     
    5556  object SpinEditRepeat: TSpinEdit
    5657    Left = 208
    57     Height = 32
    58     Top = 112
     58    Height = 27
     59    Top = 128
    5960    Width = 98
    6061    OnChange = SpinEditRepeatChange
     
    6465    Left = 56
    6566    Height = 43
    66     Top = 55
     67    Top = 71
    6768    Width = 208
    6869    OnChange = TrackBarOnceChange
     
    7677    Left = 56
    7778    Height = 43
    78     Top = 152
     79    Top = 168
    7980    Width = 208
    8081    OnChange = TrackBarRepeatChange
     
    8889    Left = 8
    8990    Height = 25
    90     Top = 64
     91    Top = 80
    9192    Width = 48
    9293    Caption = 'Min'
    93     OnClick = uttonOnceMinClick
     94    OnClick = ButtonOnceMinClick
    9495    TabOrder = 6
    9596  end
     
    9798    Left = 264
    9899    Height = 25
    99     Top = 64
     100    Top = 80
    100101    Width = 48
    101102    Anchors = [akTop, akRight]
     
    107108    Left = 8
    108109    Height = 25
    109     Top = 160
     110    Top = 176
    110111    Width = 48
    111112    Caption = 'Min'
     
    116117    Left = 264
    117118    Height = 25
    118     Top = 160
     119    Top = 176
    119120    Width = 48
    120121    Anchors = [akTop, akRight]
     
    123124    TabOrder = 9
    124125  end
     126  object Label3: TLabel
     127    Left = 16
     128    Height = 18
     129    Top = 8
     130    Width = 96
     131    Caption = 'Win probability:'
     132    ParentColor = False
     133  end
     134  object LabelWinProbability: TLabel
     135    Left = 232
     136    Height = 18
     137    Top = 8
     138    Width = 10
     139    Caption = '   '
     140    ParentColor = False
     141  end
    125142end
  • trunk/Forms/UFormMove.lrt

    r42 r59  
    88TFORMMOVE.BUTTONREPEATMIN.CAPTION=Min
    99TFORMMOVE.BUTTONREPEATMAX.CAPTION=Max
     10TFORMMOVE.LABEL3.CAPTION=Win probability:
     11TFORMMOVE.LABELWINPROBABILITY.CAPTION=   
  • trunk/Forms/UFormMove.pas

    r42 r59  
    2222    Label1: TLabel;
    2323    Label2: TLabel;
     24    Label3: TLabel;
     25    LabelWinProbability: TLabel;
    2426    SpinEditOnce: TSpinEdit;
    2527    SpinEditRepeat: TSpinEdit;
     
    2830    procedure ButtonOnceMaxClick(Sender: TObject);
    2931    procedure ButtonRepeatMinClick(Sender: TObject);
    30     procedure uttonOnceMinClick(Sender: TObject);
     32    procedure FormCreate(Sender: TObject);
     33    procedure ButtonOnceMinClick(Sender: TObject);
    3134    procedure ButtonRepeatMaxClick(Sender: TObject);
    3235    procedure FormShow(Sender: TObject);
     
    3841    { private declarations }
    3942  public
    40     { public declarations }
     43    DefendCount: Integer;
     44    procedure UpdateView;
    4145  end;
    4246
     
    4751
    4852{$R *.lfm}
     53
     54uses
     55  UCore;
    4956
    5057{ TFormMove }
     
    6269end;
    6370
     71procedure TFormMove.UpdateView;
     72begin
     73  LabelWinProbability.Caption :=
     74    IntToStr(Round(Core.Game.AttackProbability(SpinEditOnce.Value,
     75    DefendCount) * 100)) + ' %';
     76end;
     77
    6478procedure TFormMove.SpinEditOnceChange(Sender: TObject);
    6579begin
    6680  if SpinEditOnce.Value <> TrackBarOnce.Position then
    6781    TrackBarOnce.Position := SpinEditOnce.Value;
     82  UpdateView;
    6883end;
    6984
     
    7186begin
    7287  ButtonOk.SetFocus;
     88  UpdateView;
    7389end;
    7490
     
    8399end;
    84100
    85 procedure TFormMove.uttonOnceMinClick(Sender: TObject);
     101procedure TFormMove.FormCreate(Sender: TObject);
     102begin
     103
     104end;
     105
     106procedure TFormMove.ButtonOnceMinClick(Sender: TObject);
    86107begin
    87108  TrackBarOnce.Position := TrackBarOnce.Min;
  • trunk/Languages/xtactics.cs.po

    r58 r59  
    190190msgstr "Každý tah:"
    191191
     192#: tformmove.label3.caption
     193msgid "Win probability:"
     194msgstr ""
     195
     196#: tformmove.labelwinprobability.caption
     197msgid "   "
     198msgstr ""
     199
    192200#: tformnew.aplayeradd.caption
    193201msgid "Add player"
  • trunk/Languages/xtactics.po

    r58 r59  
    180180msgstr ""
    181181
     182#: tformmove.label3.caption
     183msgid "Win probability:"
     184msgstr ""
     185
     186#: tformmove.labelwinprobability.caption
     187msgid "   "
     188msgstr ""
     189
    182190#: tformnew.aplayeradd.caption
    183191msgid "Add player"
  • trunk/UCore.pas

    r56 r59  
    8888  FormMove.TrackBarRepeat.Max := FormMove.SpinEditRepeat.MaxValue;
    8989  FormMove.TrackBarRepeat.Position := FormMove.SpinEditRepeat.Value;
     90  FormMove.DefendCount := CellTo.Power;
    9091  if FormMove.ShowModal = mrOk then begin
    9192    CountOnce := FormMove.SpinEditOnce.Value;
  • trunk/UGame.pas

    r58 r59  
    252252    StayAliveForDefinedTurns: Integer;
    253253    MaxNeutralUnits: Integer;
     254    function AttackProbability(AttackCount, DefendCount: Integer): Double;
    254255    procedure SaveConfig(Config: TXmlConfig; Path: string);
    255256    procedure LoadConfig(Config: TXmlConfig; Path: string);
     
    10871088{ TGame }
    10881089
     1090function ComparePointer(Item1, Item2: Pointer): Integer;
     1091begin
     1092  Result := -CompareValue(Integer(Item1), Integer(Item2));
     1093end;
     1094
    10891095procedure TGame.Attack(var AttackPower, DefendPower: Integer);
    10901096var
     
    10941100  DefenderHighestRoll: Integer;
    10951101  RollResult: Integer;
    1096   LoseCount: Integer;
    1097   I: Integer;
    1098 begin
     1102  S: string;
     1103  I: Integer;
     1104  AttackRolls: TList;
     1105  DefendRolls: TList;
     1106begin
     1107  AttackRolls := TList.Create;
     1108  DefendRolls := TList.Create;
    10991109  if AttackPower < 1 then
    11001110    raise Exception.Create('Attacker power have to be higher then 0.');
     
    11041114    // Risk game rules:
    11051115    // Each side do their dice roll and compare result. Defender wins tie.
    1106     // Attacer can use three dices and defender two
    1107     if AttackPower < 3 then AttackerDiceCount := AttackPower
    1108       else AttackerDiceCount := 3;
    1109     if DefendPower < 2 then DefenderDiceCount := DefendPower
    1110       else DefenderDiceCount := 2;
    1111     // Roll and calculate each side highest number
    1112     AttackerHighestRoll := 1;
     1116    // Attacker can use three dices and defender two
     1117    AttackerDiceCount := Min(AttackPower, 3);
     1118    DefenderDiceCount := Min(DefendPower, 2);
     1119    // Roll and sort numbers
     1120    AttackRolls.Count := AttackerDiceCount;
    11131121    for I := 0 to AttackerDiceCount - 1 do begin
    1114       RollResult := Random(7);
    1115       if RollResult > AttackerHighestRoll then
    1116         AttackerHighestRoll := RollResult;
    1117     end;
    1118     DefenderHighestRoll := 1;
     1122      AttackRolls[I] := Pointer(Random(7));
     1123    end;
     1124    AttackRolls.Sort(ComparePointer);
     1125    S := 'Att:';
     1126    for I := 0 to AttackerDiceCount - 1 do
     1127      S := S + InTToStr(Integer(AttackRolls[I])) + ', ';
     1128    DefendRolls.Count := DefenderDiceCount;
    11191129    for I := 0 to DefenderDiceCount - 1 do begin
    1120       RollResult := Random(7);
    1121       if RollResult > DefenderHighestRoll then
    1122         DefenderHighestRoll := RollResult;
    1123     end;
     1130      DefendRolls[I] := Pointer(Random(7));
     1131    end;
     1132    DefendRolls.Sort(ComparePointer);
     1133    S := S + ' Def:';
     1134    for I := 0 to DefenderDiceCount - 1 do
     1135      S := S + IntToStr(Integer(DefendRolls[I])) + ', ';
    11241136    // Resolution
    1125     LoseCount := Min(AttackerDiceCount, DefenderDiceCount);
    1126     if AttackerHighestRoll > DefenderHighestRoll then Dec(DefendPower, LoseCount)
    1127       else Dec(AttackPower, LoseCount);
    1128   end;
     1137    for I := 0 to Min(AttackerDiceCount, DefenderDiceCount) - 1 do
     1138      if AttackRolls[I] > DefendRolls[I] then Dec(DefendPower)
     1139      else Dec(AttackPower);
     1140  end;
     1141  AttackRolls.Free;
     1142  DefendRolls.Free;
     1143end;
     1144
     1145function TGame.AttackProbability(AttackCount, DefendCount: Integer): Double;
     1146var
     1147  OA, OD: Integer;
     1148begin
     1149  if AttackCount = 0 then begin
     1150    Result := 0;
     1151    Exit;
     1152  end else Result := 1;
     1153  if DefendCount = 0 then Exit;
     1154
     1155  OA := Min(AttackCount, 3);
     1156  OD := Min(DefendCount, 2);
     1157
     1158  if (OA = 1) and (OD = 1) then
     1159    Result := 0.4167 * AttackProbability(AttackCount, DefendCount - 1) +
     1160      0.5833 * AttackProbability(AttackCount - 1, DefendCount)
     1161  else if (OA = 2) and (OD = 1) then
     1162    Result := 0.5787 * AttackProbability(AttackCount, DefendCount - 1) +
     1163      0.4213 * AttackProbability(AttackCount - 1, DefendCount)
     1164  else if (OA = 3) and (OD = 1) then
     1165    Result := 0.6597 * AttackProbability(AttackCount, DefendCount - 1) +
     1166      0.3403 * AttackProbability(AttackCount - 1, DefendCount)
     1167  else if (OA = 1) and (OD = 2) then
     1168    Result := 0.2546 * AttackProbability(AttackCount, DefendCount - 1) +
     1169      0.7454 * AttackProbability(AttackCount - 1, DefendCount)
     1170  else if (OA = 2) and (OD = 2) then
     1171    Result := 0.2276 * AttackProbability(AttackCount, DefendCount - 2) +
     1172      0.4483 * AttackProbability(AttackCount - 2, DefendCount) +
     1173      0.3241 * AttackProbability(AttackCount - 1, DefendCount - 1)
     1174  else if (OA = 3) and (OD = 2) then
     1175    Result := 0.3717 * AttackProbability(AttackCount, DefendCount - 2) +
     1176      0.2926 * AttackProbability(AttackCount - 2, DefendCount) +
     1177      0.3358 * AttackProbability(AttackCount - 1, DefendCount - 1);
    11291178end;
    11301179
  • trunk/xtactics.lpi

    r57 r59  
    138138        <HasResources Value="True"/>
    139139        <ResourceBaseClass Value="Form"/>
     140        <UnitName Value="UFormMove"/>
    140141      </Unit6>
    141142      <Unit7>
Note: See TracChangeset for help on using the changeset viewer.