Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucsd!nprdc!malloy From: malloy@nprdc.arpa (Sean Malloy) Newsgroups: comp.lang.pascal Subject: Re: Algorithm to Count Hexes Message-ID: <2790@skinner.nprdc.arpa> Date: 12 Jul 89 14:29:17 GMT References: <20217@adm.BRL.MIL> Reply-To: malloy@nprdc.arpa (Sean Malloy) Organization: Navy Personnel R&D Center, San Diego Lines: 58 In article <20217@adm.BRL.MIL> elder@wpafb-info5.af.mil writes: >I am working on a game written in Turbo Pascal. The game board consists >of hexes with the columns 'numbered' with letters and the rows 'numbered' >with digits. I have been trying to wtite a routine which is given two hex >positions and will return the minimum number of hexes inbetween, i.e., the >shortest "hex moves" between two hexes. Has anyone written a routine like >this before. So far I have not had any success at getting my code to >work for all combinations of hexes. I know I don't have a correct algorithm >(since it is not working). Can anyone offer any assistance? If, instead of the 'letter+number' hexid, you use the hex numbering system pioneered by (I think) SPI: ____ ____ ____ ____ ____ /0101\ /0301\ /0501\ /0701\ /0901\ / \____/ \____/ \____/ \____/ \____ \ /0201\ /0401\ /0601\ /0801\ /1001\ \____/ \____/ \____/ \____/ \____/ \ /0102\ /0302\ /0502\ /0702\ /0902\ / / \____/ \____/ \____/ \____/ \____/ \ /0202\ /0402\ /0602\ /0802\ /1002\ \____/ \____/ \____/ \____/ \____/ \ /0103\ /0303\ /0503\ /0703\ /0903\ / / \____/ \____/ \____/ \____/ \____/ \ /0203\ /0403\ /0603\ /0803\ /1003\ \____/ \____/ \____/ \____/ \____/ \ /0104\ /0304\ /0504\ /0704\ /0904\ / / \____/ \____/ \____/ \____/ \____/ \ /0204\ /0404\ /0604\ /0804\ /1004\ \____/ \____/ \____/ \____/ \____/ \ /0105\ /0305\ /0505\ /0705\ /0905\ / / \____/ \____/ \____/ \____/ \____/ \ /0205\ /0405\ /0605\ /0805\ /1005\ \____/ \____/ \____/ \____/ \____/ \ \ / \ / \ / \ / \ / \____/ \____/ \____/ \____/ \____/ you can use a relatively simple algorithm to count hex distances (if this isn't legal Pascal, bear with me -- I haven't done any programming in Pascal for over three years, and I'm rusty): FUNCTION Hex_Dist(hex1,hex2:integer):integer; VAR temp1,temp2 : integer; BEGIN temp1 := abs((hex1 DIV 100) - (hex2 DIV 100)); temp2 := abs((hex1 MOD 100) - (hex2 MOD 100)); if temp1 > temp2 then Hex_Dist := temp1 else Hex_Dist := (temp1 DIV 2) + temp2; END; {Hex_Dist} Sean Malloy | "The proton absorbs a photon Navy Personnel Research & Development Center | and emits two morons, a San Diego, CA 92152-6800 | lepton, a boson, and a malloy@nprdc.navy.mil | boson's mate. Why did I ever | take high-energy physics?"