SVG Perl Script to Make Alquerque Game Boards

Alquerque boards are used for a variety of games. Fanorama, Tigers vs Goats (Bagh bandi), Fox and Geese and other variants similar games are some of the examples. Here is how to make Alquerque game boards in SVG using Perl.

Games that use Alquerque boards:

Fox & Geese

Several variants of this such as pirates vs soldiers or tiger vs goats.

A unique game that takes longer to setup than to play, but you slide in and out using strategy to capture all of your opponents pieces.

Creative Problem Solving through Coding

The math and logic to create this Alquerque board was interesting, so we wanted to share it. More importantly is the idea of creative problem solving, and that is what this article talks about specifically. Anyone can copy code and use formulas. It’s harder, and more fun I’d argue, to come up with solutions in the first place, and know how to apply code and formulas.

The rules for the various games using these boards will be put into the games category. This article will talk only about the process of creating the game board and the script involved.

Step 1: Know Your Objective

One of my favorite verses is Proverbs 29:18 “Where there is no vision, the people perish: but he that keepeth the law, happy is he.” This rings true in all aspects of life. If you do not know what your objective is, you will most certainly have a more difficult time reaching it.

In the book of James 1:6-8, a similar verse echoes “6 But let him ask in faith, nothing wavering. For he that wavereth is like a wave of the sea driven with the wind and tossed. 7 For let not that man think that he shall receive any thing of the Lord. 8 A double minded man is unstable in all his ways.”

Knowing that I simply wanted a square with lines through it like an X greatly simplified the task list. Now it was time to identify the sub-tasks.

Step 2: Refine Objective

The first goal was to simply create a 4×4 Alquerque board, which I drew out on paper. I drew a 3×3 board too and saw that the math didn’t work right, but a 6 or 8 did. So I refined my goal to boards with an even number of squares only.

One Bite at a Time

When deciding how to eat an elephant, surely you’ve heard that eating it one bite at a time is the best way. By breaking down the objectives into smaller steps, they problems seem easier. Habbukuk 2 says “Write the vision; make it plain on tablets, so he may run who reads it.” Simply outline in very clear objectives all of the steps it takes to do what your goal requires.

“The important lesson is to break down big tasks into smaller ones, then focus on solving the smaller ones.”

Now we need to Refine Sub Goals

It would appear that the problems can be identified as:

A: Make a Square

B: Make an X through the Square

C: Stack them together orderly

A: Make a Square

This part was easy. I already figured out some code for making a square when doing the other scripts. I skipped over this hurdle as it was already solved.

B: Make an X Through the Square

I drew out the board we were trying to make and then broke it down into coordinates. At first, I thought long lines would be best, but after looking at it. After a few thoughts about it, long lines would require recalculating things over and over. If instead I made it from 2×2 squares, I would have all of the coordinates already, and do less math. It would only require advancing 1 square x or 1 square y. That is the path I chose – to make a stack of squares with X inside of them instead of a other ways.

C: Stack the Squares Together, Orderly

One way would be to solve for every possible combination and then use that list. This is obviously not an efficient way, as I’d have to solve by hand. So, I decided to solve a few to see the patterns and then write a program to match the patterns as that is the time consuming part, and I didn’t want to do it by hand hundreds of times.

Demo Code for Logic Only

Then, after the patterns were apparent, I wrote up mock code that only gave me coordinates with a fixed input. On paper this was 3 lines to tell me what logic I wanted to accomplish. Then I started writing the actual code to do the “test”.

Again the goal of this was to make sure my logic was sound. Do I loop over X first? Y first? I had to test it to see before going further.

1 Peter 1:13 explains a key point here. Too much thought and no action isn’t profitable. Take action in all things (test it, try it)!

Therefore, preparing your minds for action, and being sober-minded, set your hope fully on the grace that will be brought to you at the revelation of Jesus Christ.

The key point is: take action. Whatever your goal is, put it into motion. We are not blindly coding at this point. We have a goal, and know specific problems to solve.

Alquerque logic test

Perl

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

my$squares=8;

my$steps=$squares/2;

my@start0;

my@start2;

my$step=0;

for(1..$steps){

push@start0,$step;

$step=$step+2;

}

$step=2;

for(1..$steps){

push@start2,$step;

$step=$step+2;

}

foreachmy$y1(@start0){

foreachmy$x1(@start0){

my$x2=$x1+2;

my$y2=$y1+2;

print"$x1\t$y1\t$x2\t$y2\n"

}

}

foreachmy$y1(@start0){

foreachmy$x1(@start2){

my$x2=$x1-2;

my$y2=$y1+2;

print"$x1\t$y1\t$x2\t$y2\n"

}

}

Later, I polished it a bit, used better coding practices, commented it, and tried to break the logic with all types of different configurations, like a user might.

Perl Script to Make Alquerque boards in SVG, Final

Perl

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

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

#!/usr/bin/perl

# Makes alquerque boards to fit on 1 page A4

usestrict;

usewarnings;

useSVG;# https://metacpan.org/pod/SVG

useGetopt::Std;# for CLI options

# A4 Paper, .5 margins, Scale of 67 seems to fit

useconstant SCALE=>'66';# dots per inch scale, sorta

useconstant DOTSIZE=>'7';# should be an odd number so line can center

useconstant LINEWIDTH=>'3';#

useconstant FILL=>'black';# can be 'rgb(0,0,0)' too

useconstant STROKE=>'black';# can be 'rgb(0,0,0)' too

# create an SVG object, canvas which we use for the rest of the draws

# using 9 width and .5 starting dot, means use 8.5 ending dots

# that way it's centered, printable. However, it's not exactly 8.5in

# the SCALE adjusts the exact sizes.

my$svg=SVG->new(

width=>11*SCALE,

height=>11*SCALE,

);

# globals

my($outfile,$squares,%options);

my@coors;# holds the steps of cooridinates for each square

my$offset=1.5;# start at 1.5 away from sides

my$start=$offset;# we start at 1.5 as a margin

my@start0;# we need to start at 0 for 1 array

my@start2;# and we need to start at 0 + 2 steps for the other array

#----------------------------------------------

# Input and Verify

#----------------------------------------------

# Make sure at least one argument provided

if(!@ARGV){usage("$0 requires arguments,\n");}

# What are the options they entered

# allowing only "o" or "s"

getopt("os",\%options);

# How many squares?

if(defined$options{s}){

$squares=$options{s};

unless($squares>0){usage("That board is too small. Size matters.")}

if($squares%2){usage("Alquerque boards should be an even number of squares.")}

Enjoy the Good of Your Labor

The final product, produces boards exactly like we planned. We ran it a few times to make the boards we wanted and saved the SVG files for later use with the CNC router that will cut the game boards.

We Enjoy Coding

Coding isn’t fun for everyone, but it is something we love to do. Ecclesiastes 3 talks about the joy of your labor.

12 I know that there is nothing better for them than to rejoice and to do good as long as they live; 13 and also that every man should eat and drink and see and enjoy the good of all his labor—it is the gift of God.

God gave us problems to solve, and skills to solve them. We enjoy coding, one of our labors. We enjoy problem solving. I also enjoy playing games with my family and kids. What do you enjoy?

We help solve IT related problems, such as writing programs. If you want help, please contact us and we can give you a free quote on custom programming so you can spend more time doing the labor you enjoy, and we can enjoy coding for you!