Category Archives: Swift

Tigers & Goats: A Strategy Game

Board games have a rich tradition across various countries and cultures – games such as Chess and Go are hugely popular and are probably what you think of when you hear “strategy based board games”. But in ancient times, there were other games that kept people entertained for hours. In Egypt, one of the most popular 2-player board games was Senet – and it was played not just by Pharoahs and royalty but also by ordinary Egyptians.

In this post, we will explore a lesser known but immensely entertaining and simple board game called Tigers & Goats. This game has many incarnations and the original version comes to us from Nepal, where it is referred to as Bagh Chal. However, the variant I learned is from India and is commonly known by its Marathi name – Wagh-Shelya. I was introduced to Tigers & Goats by my grandmother – who taught me to play this on the chess board. Now, we have a modern version – an app – that we can play on an iPhone, iPad, Mac or Apple TV.

Simulator Screen Shot - iPad Pro (12.9-inch) (6th generation) - 2023-02-12 at 07.58.01

The rules are really simple – you start with an 8 x 8 board and arrange 28 goats around the perimeter. The four tigers are placed at the center of the board.

Simulator Screen Shot - iPad Pro (12.9-inch) (6th generation) - 2023-02-12 at 08.01.22

Goats always begin the game. They can move to an adjacent square in any direction as long as that square is empty. But be warned – the tigers can “eat” goats if they are not careful. In order for a tiger to be able to eat a goat, the goat should have an empty square behind it as shown below – either horizontally or vertically.

Simulator Screen Shot - iPad Pro (12.9-inch) (6th generation) - 2023-02-12 at 08.06.25

Tigers move in a fashion similar to how an elephant or rook moves in Chess – any number of squares horizontally or vertically. In addition, a tiger can “jump” over a goat and eat it. It is not mandatory for a tiger to kill a goat and it may choose not to depending on the situation in the game. The goats have to work together as a team to advance without being killed and eventually trap all 4 tigers so they cannot move at all. If this happens, the goats win.

The latest version of this game (version 2.7) can be played in 3 modes:

  • Practice
  • 1-Player
  • 2-Player

Practice mode lets you play both tigers and goats and helps you learn the game. One level up is 1-Player mode, where you take charge of goats and match wits against the computer, which plays tigers. Finally, 2-Player mode allows you to play with friends you invite using Apple’s Game Center.

The screenshot below shows the initial stage of the game when both goats and tigers have made a few moves. It is now the tigers move (indicated by a orange circle around them) and as a player who plays tigers, your job is to occupy strategic locations on the board such that goats will find it really difficult to advance any further.

Simulator Screen Shot - iPad Pro (12.9-inch) (6th generation) - 2023-02-12 at 08.14.39

If you are playing goats, you will now need to think of clever ways to advance without getting killed. This may seem hard but is not impossible. In fact, most of the fun while playing this game comes from exploring various strategies to advance (from the goats perspective) and of making this impossible (from the tigers perspective). The simple rules make this very easy to understand and play even for kids. And the endless possibilities can keep us adults (and the little kid inside each and every one of us) entertained for hours.

In the app, if a goat is in danger of being eaten because a tiger moved in for the kill, the square on which it is located gets highlighted in red. You should move this goat to safety either by retreating or moving another goat behind it so the tiger can’t kill it. For example, the tiger pointed out in the situation below has moved in for the kill, but we can move the goat indicated by the other finger to the left, thereby making the goat safe from the tiger.

Screenshot 2023-02-12 at 8.25.18 AM

If the goats move carelessly and get eaten, it will get harder and harder for them to trap tigers. If 4 goats are eaten it is next to impossible for goats to trap tigers and so in this situation, tigers win. On the other hand, if goats manage to keep advancing and trap all tigers, goats win! Here is an example of how all tigers are now in big trouble and likely to get trapped by goats soon.

Simulator Screen Shot - iPad Pro (12.9-inch) (6th generation) - 2023-02-12 at 08.34.47

To play this game with your friends, simply follow the steps below to invite them via Apple’s GameKit UI. Note that you will need to be logged in to Game Center to invite friends and will also need to add them as a friend before you can invite them. All of this is managed by Game Center. In the steps below, Player 1 initiates the invite and this player plays goats. The other person who gets invited plays tigers. You can then interchange the process to switch roles.

As soon as Player 1 moves a goat piece, the invite gets sent to Player 2 and the game begins! GameKit also sends notifications when a move is made by your opponent so you can get back to the game if you are doing something else on your device.

Communication between the two devices happens via GameKit and is handled by Apple. The video below shows the first several moves as Player 1 (on an iPhone) and Player 2 (on a Mac) take turns in moving the pieces. The game continues until one of the player wins or forfeits the game. You can have multiple games with different opponents going on at the same time. GameKit keeps track of the status of each game – and you can also stop and pick up your game at a later time.

The game is best played on large screens – such as iPads, Macs and Apple TV but also works on iPhones. On the more recent iPhones, it supports a new feature called Live Activities. This allows you to see at a glance whose turn it is to play and how long it has been since the turn started. You will need to be on iOS 16.1 or higher in order to see Live Activities.

On newer phone models such as the iPhone 14 Pro and the iPhone 14 Pro Max, you will also see Live Activity information in Apple’s beautifully designed Dynamic Island – a shape-shifting elongated capsule that lives near the front camera. Tapping on this Live Activity immediately takes you to your game – from whatever app you happen to be in.

Screenshot 2023-02-12 at 9.53.42 AM

Board games have changed and evolved over the ages and we have learned to use modern technology to play them. The internet now allows players who are separated by thousands of miles to play them as if they are sitting in the same room. And with rapid advancements and improvements in AI, machines will soon come up with interesting strategies that we may not think of right now – and be unbeatable at these games.

But at the core, games such as these retain their charm and simplicity and will continue to exist in some form or another and entertain us for generations to come.

REFERENCES

https://apps.apple.com/us/app/tigers-goats/id1599385271

iOS – A simple introduction to Swift

I don’t know who once said these words, but they apply without question to this blog post.

Make everything as simple as possible – but not simpler.

Anyways, if you have nothing better to do with your day, you might as well learn how to program using Apple’s shiny new language called Swift. Granted it is not so shiny because it has been a while since it was born. But it is still somewhat of a toddler and lots of fun to play with – although this toddler is a lot smarter than what you might think.

Ok – so this post is not about to teach you how to program – or about how to write apps for iOS. It is more of a visual inspiration to start coding in Swift for those still reluctant to make the leap from good old Objective-C. Yes, I am one of those who fell in love with and still love Objective-C. I was particularly shocked when I just found out what Swift is going to do to the increment operator ( i ++ ). But you can read about that elsewhere if you don’t already know.

Enough chit chat – let’s get to the point. Here we go:

Screen Shot 2016-06-20 at 11.00.58 PM

The project here has a single button and a label. The label begins life with the number 1. Pressing the button doubles the value of the label and this keeps happening forever. I have not checked what happens if you keep doing this for a long time. Your finger may not like it much.

1…2…4…8…16…32…you get the idea.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var label: UILabel!
    
    var i = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func buttonPressed(sender: AnyObject) {
        i += i
        label.text = String(i)
    }
}

Granted, this is a really simple app – but hey, we got to start somewhere and this is as good a place as any.

Happy Coding!