Week 6-7 Multipeer Connectivity

Mobile Lab Musical Chairs is a virtual musical chairs game where certain number of players occupy less number of circular buttons(chairs), each player can only occupy one button per round, whoever did not end up occupying any button lose the round.

Game logic:

We used MultipeerService to establish connections between devices. The devices are able to communicate with each other over Wi-Fi or Bluetooth. We planned to have three view controllers: a ReadyViewController where players input player names, assigned a unique color and read instructions; a CountDownViewController with 5 seconds count down once all players are joined and ready; a GameViewController where players occupy buttons. Once all buttons occupied, game result alert is displayed.

Illustrator prototype:

Data Structure:

data structure

data structure

Development:

We ended up displaying our instructions in a carousel view with a CollectionViewController instead of a regular viewController. In order to pass data back and forth more easily between the CollectionViewController and the GameViewController, we decided to take out the CountdownViewController and just have the countdown displayed in GameViewController once it is instantiated.

We first had MultipeerServiceDelegate in the GameViewController, but since we need to know the number of connected devices of MultipeerService in the IntroCollectionViewController to instantiate GameViewController, we figured we had to start MultipeerService in IntroCollectionViewController to get the number of connected devices. The solution is to have MultipeerServiceDelegate in both ViewControllers and pass multipeerService as an object down to GameViewController when it is instantiated from IntroCollectionViewController.

When we tried to restart the game on “restart” tap going back to IntroCollectionViewController, the number of connected devices would not increment again to instantiate GameViewController since players are still connected. We thought about multiple solutions, one is to keep a variable of playerReadyCount on “ready” tap instead of number of connected Devices. However, the first time when players log in, earlier logged-in players will not be able to send the variable to players that have not logged in yet so they will never receive the variable. Therefore we decided to try disconnect multipeerService at the end of GameViewController, but for some reason, the connected devices array still kept incrementing second time into IntroCollectionViewController. And we figured out through debugging that the issue was when we passed multipeerService back and forth between view controllers, we also created multiple instances of it. Therefore we had a function of startMatchmaking() in MultipeerService.swift file which calls startAdvertisingPeer and startBrowsingForPeers once multipeerService is initiated, and another function of stopMatchmaking which stops advertising and browsing for peers once multipeerService object is passed down to GameViewController and it solved the problem.

Game Demo:

Since Xcode can only launch 4 simulator devices at a time and I can only click on one screen at a time, for the purpose of demonstration, I had the total number of buttons occupied set to 3 to end the game, therefore there would be 1 loser and 3 winners. But in reality, we expect there to be at least 10 players connected to occupy 10 buttons at the same time.