Autor Beitrag
Regan
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2157
Erhaltene Danke: 72


Java (Eclipse), Python (Sublimetext 3)
BeitragVerfasst: So 17.12.06 12:47 
Hallo,
Ich möchte gerne ein einfaches Suche in Wikipedia BLACKJACK programmieren. Dieses soll erstmal ohne splitten, doublen und insurance ablaufen. Dabei möchte ich aber das Card-Counting trainieren. Nun mein Problem: wie bekomme ich sechs Pakete französischer Spielkarten zu 52 Blatt, also 312 Karten, in mein Delphi-Programm. Dabei soll jede Karte folgendes haben:
  • Einen Kartennamen (z.B. Karo König) oder Bild
  • Einen Kartenwert (z.B. 10)
  • Einen Count-wert (z.B. -1)

und jede karte muss also 6 mal vorhanden. Wie streiche ich die schon benutzten Karten wieder aus den arrays heraus?
Und wie kombiniere ich das Karten-array mit einem Zufallsgenerator?

MfG
Regan

Moderiert von user profile iconChristian S.: Wiki-Tag korrigiert
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 18.12.06 02:07 
Moin!

Naja, deine Frage ist sehr vage formuliert, da du keine eigene Datenstruktur definiert hast - das läuft ja praktisch darauf hinaus, die Anwendung überhaupt zu planen... :?

Ich würde halt erstmal eine Datenstruktur für eine Karte definieren:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
type
  TCard = record
    Name: String// der Name z.B. "Karo König"
    ImageIndex: Integer; // der Index in eine Imagelist mit den Kartenbildern
    Value: Integer; // Kartenwert
    CountValue: Integer; // Count-Wert
    Used: Boolean; // Karte im Spiel oder nicht
  end;

Dann brauchst du noch ein Feld, das groß genug für alle Karten ist:
ausblenden Delphi-Quelltext
1:
2:
var
  CardDeck: array[0..311of TCard;

Jetzt kannst du das Array passend vorbelegen und über den Index in CardDeck auf eine bestimmte Karte zugreifen. Diese Indizes sind dann praktisch eine eindeutige Möglichkeit, eine bestimmte Karte zu bezeichnen, so dass eine "Hand" einfach eine entsprechende Aufzählung dieser Indizes darstellt.

Vielleicht kommst du ja damit weiter. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Mo 18.12.06 08:51 
Hallo,

ein Zufallsarray Stapel wuerde die Werte (indizes) 0..311 enthalten und dann gemischt werden.
www.delphi-forum.de/viewtopic.php?t=51089
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
Stapel : array[0..311of integer;

For i := 0 to 311 do
  Stapel := i;
ShuffleMillerYates (Stapel);

ZufallsKarte := CardDeck[Stapel[Nummer]];

Gruss Horst
Regan Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2157
Erhaltene Danke: 72


Java (Eclipse), Python (Sublimetext 3)
BeitragVerfasst: Sa 23.12.06 15:55 
bedeutet das, dass ich 312 Variablen vom Typ record definieren muss, die ichd ann ins array einfüge?
und wenn ja, wie rufe ich die dann bei dem zufallswert, den ich raushabe wieder auf?
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Sa 23.12.06 17:49 
Hallo,

falls Du lieber mit Zeigern arbeitest:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
uses
  sysutils;

type
  tFarbe = (Karo,Herz,Pik,Kreuz);
  tWert = (Zwei,Drei,Vier,Fuenf,sechs,sieben,acht,neun,zehn,Bube,Dame,Koenig,Ass);//As= as ist reserviertes Wart

const
  //Achtung das sind etliche Megabyte
  AnzahlStapel = 60000;
  KartenProStapel =( integer(High(tFarbe))-integer(Low(tFarbe))+1)*(integer(High(tWert))-integer(Low(tWert))+1);
  KartenZahl = AnzahlStapel*KartenProStapel;
  FarbName : array [tFarbe] of string = ('Karo','Herz','Pik','Kreuz');
  WertName : array [tWert] of string= ('Zwei','Drei','Vier','Fuenf','sechs','sieben','acht','neun','zehn','Bube','Dame','Koenig','As');


type
  tAnzahl = 1..KartenZahl;
  TCard = packed record
    CardValue     : Integer; // Kartenwert
    CardImageIndex: Integer; // der Index in eine Imagelist mit den Kartenbildern
    CardStapelNr  : integer;
    CardName      : AnsiString;//oder String[5+1+6]; der Name z.B. "Kreuz Koenig"
    CardFarbe     : tFarbe;
    CardWert      : tWert;
    CardUsed      : Boolean; // Karte im Spiel oder nicht
  end;

  tpCard = ^tCard;

  tCardDeck = array[tAnzahl] of tCard;
  tStapel = array[tAnzahl] of tpCard;
var
  CardDeck:tCardDeck;
  Stapel:tStapel;

Procedure ShuffleMillerYates (Var A :tStapel);
Var
 I, J, t : Integer;
 data : tpCard;
begin
  j :=  High(A)-Low (A) +1;
  i := Low(A);
  while j>0 do
    begin
    t := Random(j)+i;//t aus [i..High(A)]
    data := A[t];
    A[t] := A[i];
    A[i] := data;
    inc(i);
    dec(j)
  end;
end;

procedure CreateCardDeck(var CardDeck:tCardDeck;
                         var Stapel  :tStapel);
var
  i   : integer;
  idx : tAnzahl;
  Farbe: tFarbe;
  Wert : tWert;
  Card : tCard;
begin
  idx := Low(CardDeck);
  For Farbe := Karo to Kreuz do
    For Wert := Zwei to Ass do
      with Card do
        begin
        CardName:= Format('%5s %6s',[Farbname[Farbe],WertName[Wert]]);
        CardImageIndex:= 13*integer(Farbe)+integer(Wert); // der Index in eine Imagelist mit den Kartenbildern
        case Wert of
          zwei..zehn    : CardValue := Longint(Wert)+2;
          Bube..Koenig  : CardValue := 10;
          Ass           : CardValue := 11;
        end;
        CardFarbe := Farbe;
        CardWert  := Wert;
        //CountValue: Integer; // Count-Wert
        CardUsed:= false; // Karte im Spiel oder nicht
        //Diese Karte jetzt AnzahlStapelfach hintereinander auf den Stapel legen
        For i := 1 to AnzahlStapel do
          begin
          CardStapelNr := i;
          CardDeck[idx] := Card;
          Stapel[idx] := @CardDeck[idx];
          inc(idx);
          end;
        end;

ShuffleMillerYates(Stapel);
end;

procedure Stapelausgeben(const Stapel:tStapel);
var
  i,sumStapelNr,sumValue : integer;
  s : string;
begin
sumStapelNr := 0;
sumValue :=0;
If AnzahlStapel < 10 then
  For I := low(Stapel) to High(STapel) do
    with Stapel[i]^ do
      begin
      inc(sumStapelnr,CardStapelNr);
      inc(sumValue,CardValue);
      s := Format('%20s %4d  %.1d',[CardName,CardValue,CardStapelNr]);
      // writeln oder memo1.lines.add etc
      Writeln(s);
      end;
s :=Format('Insgesamt belegt %0.0n Byte %d pro Karte',[double(sizeOf(tCardDeck)),sizeOf(tCardDeck)div KartenZahl]);
writeln(s);
end;

begin
randomize;
CreateCardDeck(Carddeck,Stapel);
Stapelausgeben(Stapel);
end.


Gruss Horst
Regan Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2157
Erhaltene Danke: 72


Java (Eclipse), Python (Sublimetext 3)
BeitragVerfasst: Mi 24.01.07 18:27 
OK. ich habe mich für user profile iconNarses variante entschieden. trotzdem danke an alle.
perry5
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 102



BeitragVerfasst: Mi 24.01.07 18:51 
Ähm, ICH würde eine Liste nehmen. weil man kann die sehr einfach mischen (neue Liste erstellen und zufälliges Element der alten anfügen, welches in der alten gelsöcht wird, solang bis die alte leer ist) und das herausnehmen geht auch wunderbar.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 24.01.07 21:32 
Moin!

user profile iconperry5 hat folgendes geschrieben:
Ähm, ICH würde eine Liste nehmen.

Was hat denn eine Liste (für die Karten) mit den Karteneigenschaften zu tun? :gruebel: Verwechselst du da nicht was? ;)

user profile iconperry5 hat folgendes geschrieben:
weil man kann die sehr einfach mischen (neue Liste erstellen und zufälliges Element der alten anfügen, welches in der alten gelsöcht wird, solang bis die alte leer ist) und das herausnehmen geht auch wunderbar.

Das ist aber eher ineffizient... :? Besser ist es, die Liste der Karten im Spiel nach Miller-Yates zu mischen. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.