From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,aa7f494bf30adbc7 X-Google-Attributes: gid103376,public Path: g2news1.google.com!news1.google.com!news.glorb.com!newsfeed1.ip.tiscali.net!tiscali!transit1.news.tiscali.nl!dreader2.news.tiscali.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: [newbie] simple(?) data structures References: <2j1e30Fsrg8vU1@uni-berlin.de> From: Ludovic Brenta Date: Sun, 13 Jun 2004 02:10:50 +0200 Message-ID: <878yesgvw5.fsf@insalien.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:O8Y8DVn5M8NGyBXI6VU907491HA= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 13 Jun 2004 02:10:09 CEST NNTP-Posting-Host: 83.134.237.205 X-Trace: 1087085409 dreader2.news.tiscali.nl 41751 83.134.237.205:33797 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:1432 Date: 2004-06-13T02:10:09+02:00 List-Id: Roland Illig writes: > I'm doing my first steps with Ada. I tried to model a Go board and > failed. In Java/C++, I would do it like this: [...] > How can I do this in Ada? I already tried something with packages > and generics, but that did not look nice. But I want it to look nice > and Adaish. Cool, great attitude. Here is how I would do it, using generics like you suggested: generic Size : in Positive; package Go_Board is type Width is range 1 .. Size; type Height is range 1 .. Size; type Stone is (Empty, Black, White); function Get_Stone (X : in Width; Y : in Height) return Stone; procedure Set_Stone (X : in Width; Y : in Height; S : in Stone); private Board : array (Width, Height) of Stone; end Go_Board; Each instance of this package contains one board; thus you do not need a constructor. To create an instance: with Ada.Text_IO; with Go_Board; procedure Test_Go_Board is package The_Board is new Go_Board (Size => 10); begin for X in The_Board.Width loop for Y in The_Board.Height loop The_Board.Set_Stone (X, Y, The_Board.Empty); Ada.Text_IO.Put_Line ("Board (" & Integer'Image (X) & ", " & Integer'Image (Y) & ") := " & The_Board.Stone'Image (The_Board.Get_Stone (X, Y))); end loop; end loop; Ada.Text_IO.Put_Line ("Width of the board is " & Integer'Image (The_Board.Width'Last - The_Board.Width'First)); Ada.Text_IO.Put_Line ("Height of the board is " & Integer'Image (The_Board.Height'Last - The_Board.Height'First)); end Test_Go_Board; You may also want helper functions in the package Go_Board: function Height return Positive is begin return Size; end Height; (note that I avoided using "use Ada.Text_IO; use The_Board" because I wanted to make things really obvious; but you may want to consider using such use clauses if you think the code is too cluttered) HTH -- Ludovic Brenta;