Results 1 to 2 of 2

Thread: Let the computer play?

  1. #1

    Let the computer play?

    hi all,

    I'm new here, i came to you through NLDelphi.com.

    I have a little problem:

    I'm creating a dutch card game in Delphi with a L15 cardcomponent (TlfPile) Download@ www.l15lib.naron.ru/L15Lib.zip

    It works great so far , but how can i make the computer play against me?
    Is there anybody who can help me?

    Here is the unit so you can c how i made it.


    unit main;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ImgList, Cards, StdCtrls, Buttons, ExtCtrls, ComCtrls;

    type
    TForm1 = class(TForm)


    Deck: TlfPile;
    il: TImageList;
    you: TlfPile;
    com: TlfPile;
    open: TlfPile;
    SB: TStatusBar;
    Panel1: TPanel;
    SpeedButton2: TSpeedButton;
    r1: TRadioButton;
    R2: TRadioButton;
    Label1: TLabel;

    procedure youSelectCard(Pile: TlfPile; Card: TCard);
    procedure FormDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;
    State: TDragState; var Accept: Boolean);
    Procedure CheckCards(Deck: TlfPile);
    Procedure AddCardInDeck;
    procedure FormCreate(Sender: TObject);
    Procedure Deal;
    procedure SpeedButton2Click(Sender: TObject);
    procedure openClick(Sender: TObject);
    procedure Pakkaart (SelDeck: TlfPile); //Get Card from centre pile
    procedure DeckClick(Sender: TObject);
    procedure Weggooi(Deck:TlfPile); // throw a card away
    procedure CreateDeck;
    procedure r1MouseUp(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);



    private
    { Private declarations }
    Function CardToInt(Card:TCard):Integer;
    public

    { Public declarations }
    end;

    var
    Form1: TForm1;
    MyDeck : TlfPile;
    AddCard : TCard;
    MijnAantalKaarten : Integer; // My cards
    ZijnAantalKaarten : Integer; //Computer cards
    Type THackControl = class(TControl);
    implementation

    {$R *.dfm}

    Procedure TForm1.Weggooi(Deck:TlfPile);
    begin
    if AddCard.Rank = 'Q' then if AddCard.Suit = csSpade then
    begin //you are not allowed to trow the queen of spades away.
    ShowMessage('Schoppenvrouw mag je niet weggooien, jammer he?');
    exit;
    end;
    open.Cards.Add;
    open.Cards[open.Cards.Count-1]:= AddCard;
    open.OpenAllCards;
    Deck.Cards.Delete(AddCard.Index);
    MijnAantalKaarten := you.Cards.Count;
    ZijnAantalKaarten := com.Cards.Count;
    end;


    Procedure TForm1.Pakkaart(SelDeck:TlfPile);
    begin
    if MijnAantalKaarten = SelDeck.Cards.Count then
    begin
    SelDeck.Cards.Add;
    SelDeck.Cards[SelDeck.Cards.Count-1] := Deck.Cards[Deck.Cards.Count-1];
    Deck.Cards.Delete(Deck.Cards.Count-1);
    if SelDeck = you then
    you.OpenAllCards;
    end else
    ShowMessage( 'Je hebt al een kaart gepakt');
    end;

    Function TForm1.CardToInt(Card:TCard):integer;
    begin
    If card.Rank = '2'then result := 2;
    If card.Rank = '3'then result := 3;
    If card.Rank = '4'then result := 4;
    If card.Rank = '5'then result := 5;
    If card.Rank = '6'then result := 6;
    If card.Rank = '7'then result := 7;
    If card.Rank = '8'then result := 8;
    If card.Rank = '9'then result := 9;
    If card.Rank = '10'then result := 10;
    If card.Rank = 'J'then result := 11;
    If card.Rank = 'Q'then result := 12;
    If card.Rank = 'K'then result := 13;
    If card.Rank = 'A'then result := 14;
    end;

    Procedure TForm1.AddCardInDeck;
    begin
    Mydeck.Cards.Add;
    MyDeck.Cards[MyDeck.Cards.Count-1]:= AddCard;
    you.Cards.Delete(Addcard.Index);
    MyDeck.OpenAllCards;
    end;

    Procedure TForm1.CheckCards(Deck: TlfPile);
    var i ,a,b: Integer;
    begin
    A := CardToInt(AddCard)-1;
    B := CardToInt(AddCard)+1;

    If Deck.Cards.Count = 1 then
    if Deck.Cards[0].Rank = AddCard.Rank then
    if Deck.Cards[0].Suit <> AddCard.Suit then
    AddCardInDeck;

    if deck.Cards.Count = 1 then
    if deck.Cards[0].Suit = addcard.Suit then
    if CardToInt(Deck.Cards[0])= a or b then
    AddCardInDeck;

    If Deck.Cards.Count =0 then
    AddCardInDeck;


    If Deck.Cards.Count >1 then
    begin
    For i := 0 to Deck.Cards.Count -1 do
    if deck.Cards[i].Suit <> AddCard.Suit then
    if deck.Cards[i].Rank = Addcard.Rank then
    AddCardInDeck;
    end;

    if Deck.Cards.Count >1 then
    begin
    For i := 0 to Deck.Cards.Count -1 do
    if deck.Cards[i].Suit = AddCard.Suit then
    if CardToInt(Deck.Cards[i])= a or b then
    AddCardInDeck;
    end;

    if deck.Cards.Count >1 then
    begin
    if deck.Cards[0].Suit <> AddCard.Suit then
    if Deck.Cards[0].Rank <> AddCard.Rank then
    exit;
    end;

    end;

    Procedure TForm1.Deal;
    Begin
    you.cards.Clear;
    com.Cards.Clear;
    open.Cards.Clear;
    deck.Shuffle;
    deck.Deal([you,com],true,26);
    you.OpenAllCards;
    you.SortBySuit(CsSpade,CsDiamond,CsClub,CsHeart);
    deck.Deal(open,true,1);
    open.OpenAllCards;
    MijnAantalKaarten := you.Cards.Count;
    end;


    procedure TForm1.youSelectCard(Pile: TlfPile; Card: TCard);
    begin
    AddCard := Card;

    if R1.Checked = true then
    CheckCards(MyDeck);

    if R2.Checked = true then
    Weggooi(you);

    end;

    Procedure TForm1.CreateDeck;
    begin
    MyDeck := TlfPile.Create(Self);
    MyDeck.Parent := form1;
    MyDeck.Top := you.Top;
    MyDeck.Left :=you.Left - 200;
    MyDeck.Images := il;
    MyDeck.CardOfs := 10;
    MyDeck.DragMode := dmAutoMatic;
    end;

    procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer);
    begin
    If Source = MyDeck then
    Begin
    if MyDeck.Cards.Count < 3 then
    begin
    ShowMessage('Je moet minimaal 3 kaarten op tafel leggen');
    exit;
    end
    else
    MyDeck.Left := x;
    MyDeck.Top := y;
    end;
    end;

    procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer;
    State: TDragState; var Accept: Boolean);
    begin
    Accept := ( Source is TlfPile);
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    var MyImage : TImage;
    begin
    MyImage := Timage.Create(Self);
    MyImage.Width := Sb.Panels[0].Width;
    MyImage.Height := Sb.Height;
    MyImage.Parent := Sb;
    MyImage.Picture.LoadFromFile('glygh.bmp');
    MyImage.Left := 5;
    SB.Panels[1].Text :=' Klik op de stapel kaarten om te delen';
    end;

    procedure TForm1.SpeedButton2Click(Sender: TObject);
    begin
    deal;
    end;

    procedure TForm1.openClick(Sender: TObject);
    var i : integer;
    begin
    if MijnAantalKaarten = you.Cards.Count then
    For i := 0 to open.Cards.Count-1 do
    begin
    AddCard := Open.Cards[open.Cards.count-1];
    you.Cards.Add;
    you.Cards[you.Cards.Count-1]:= AddCard;
    Open.Cards.Delete(AddCard.Index);
    end
    else Showmessage('Je mag nu geen kaart meer pakken');
    end;

    procedure TForm1.DeckClick(Sender: TObject);
    begin
    PakKaart(you);
    end;


    procedure TForm1.r1MouseUp(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer);
    begin
    If R1.Checked = True then CreateDeck;
    end;



    end.
    Greets Bolus

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Let the computer play?

    In the tutorials section I wrote a small tutorial on using DWS to add Artificial Intellegence to a Checkers game. I would suggest you need to build something similar to let the compute rplay against you.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •