Swiftpack.co - Package - matthesjh/sc19-swift-client

Swift client for Software-Challenge Germany 2018/2019

Build Status Swift Supported Platforms

This package contains a simple client written in Swift for Software-Challenge Germany 2018/2019.

The game of the contest year 2018/2019 is called Piranhas. It is played on a board with 10x10 fields. The goal of the game is to unite his piranhas to a single swarm. The complete documentation of the game rules and the XML communication with the game server can be found here.


Please make sure that you have installed the latest Swift toolchain (or at least version 4.2.1) on your operating system. The latest version of Swift can be found here.

To build and run the executable of the simple client, use the run command of the Swift Package Manager.

swift run

If you want to use e.g. another host address or port for the simple client, then you can pass additional arguments to the run command of the Swift Package Manager. Please note that you need to specify the name of the executable when using additional arguments.

swift run simple-client -h -p 13050

The simple client supports the following command-line arguments.

Usage: simple-client [options]
  -h, --host:
      The IP address or name of the host to connect to (default:
  -p, --port:
      The port used for the connection (default: 13050).
  -r, --reservation:
      The reservation code to join a prepared game.
  -s, --strategy:
      The strategy used for the game.
      Print this help message.
      Print the version number.

Creating an archive for upload

In order to use the simple client on the competition system (a.k.a. Wettkampfsystem), a zip archive must be created that contains a start script and the compiled executable. To create such an archive, run the following commands on the terminal.

chmod u+x scripts/zip-client.sh

The resulting archive (simple-client.zip) can then be uploaded to the competition system. Please make sure that you select the start script (start-client.sh) as the main file in the uploading process.

Note: The above script (zip-client.sh) builds the client with the release configuration and calls the Swift compiler with the -O flag to optimize the executable for speed.

Customizing the logic

To customize the logic of the simple client to your own needs, simply adjust the onMoveRequested() method in the SCGameLogic.swift class.

func onMoveRequested() -> SCMove? {
    print("*** A move is requested by the game server!")

    // TODO: Add your own logic here.

    return self.gameState.possibleMoves().randomElement()

If you want to return e.g. the last possible move, the method can be changed as follows.

func onMoveRequested() -> SCMove? {
    print("*** A move is requested by the game server!")

    return self.gameState.possibleMoves().last

In addition to the default logic class, you can also implement your own logic classes. To use one of your own logic classes, the simple client offers the possibility to select a strategy (logic class) based on the value of a command-line argument (-s or --strategy). By default, this feature is disabled. To enable the feature, create a logic instance based on the strategy property of the SCGameHandler.swift class. This can be done by replacing the existing code line with a switch-statement like the following.

switch self.strategy {
    case "winner":
        self.delegate = SCWinnerLogic(player: self.playerColor)
    case "crazy":
        self.delegate = SCCrazyGameLogic(player: self.playerColor)
    case "another_logic":
        self.delegate = AnotherLogic(player: self.playerColor)
    // ...
        self.delegate = SCGameLogic(player: self.playerColor)

Renaming the client

If you want to change the name of the simple client, you have to adjust the target name in the Package.swift file and the directory name in the Sources folder. Furthermore the EXECUTABLE_NAME variable in the shell scripts and the executableName constant in the main.swift file needs to be changed.


Should you encounter an error or unexpected behavior while using the simple client, you may be able to resolve the problem by following the advice below.

  • Increase the TCP socket timeout: On some systems the current socket timeout of 1ms results in fatal errors before sending the first move. To prevent this, you can increase the socket timeout to a higher value. Any value greater than or equal to 5000 (which corresponds to 5ms) should resolve the problem on most systems.

If none of these tips help and you feel that you have encountered a bug, feel free to open an issue. Please describe your problem as accurately as you can and include steps to reproduce the error if possible. Note that a GitHub account is required to create a new issue. You can also instruct your tutor to do it for you.


Stars: 4


Used By

Total: 0


1.6.0 - 2019-11-15 14:12:10

  • Added the following properties to the SCGameState class
    • round
  • Added the following constants to the SCConstants class
    • gameIdentifier
  • Fixed a typo in the SCScoreCause documentation comments

1.5.0 - 2019-06-09 20:17:52

  • A game logic can now access the game result received from the game server
  • Added the following methods to the SCGameHandlerDelegate protocol
    • onGameResultReceived(_:)

1.4.0 - 2019-05-12 08:34:19

  • Refactored some methods in the SCGameState class
  • Added the following methods to the SCField class
    • hasPiranha(ofPlayer:)
    • isCoverable(byPlayer:)
    • isSkippable(byPlayer:)

1.3.3 - 2019-04-06 22:05:26

  • Added the following methods to the SCGameState class
    • undoLastMove()

1.3.2 - 2019-03-26 17:01:03

  • Adjusted the start script to show the installed Swift version
  • Added the following methods to the SCGameState class
    • isSwarmConnected(forPlayer:)

1.3.1 - 2019-03-07 10:13:54

  • Adjusted the start script to correctly start the executable on the competition system
  • Improved the connect(toHost:withPort:) method to resolve hostnames that are not IP addresses
  • Reduced the maximum blocking time of the readable property in the SCSocket class from 50ms to 1ms

1.3.0 - 2019-02-22 20:38:28

  • The fieldsInDirection(ofMove:withDistance:) method now has an optional return type ([SCField]? instead of [SCField])
  • Added the following methods to the SCGameState class
    • biggestSwarm(ofPlayer:)
    • neighboursOfField(x:y:)
    • neighboursOfField(x:y:withState:)
    • swarms(ofPlayer:)
  • Improved the following methods in the SCGameState class
    • fieldsInDirection(ofMove:withDistance:)
    • moveDistance(x:y:direction:)
    • performMove(move:)
    • possibleMoves()
  • Improved the documentation comments in the SCSocket class

1.2.0 - 2018-12-29 19:01:24

  • The move distance methods now have an optional return type (Int? instead of Int)
  • A compiler error message will now be shown on unsupported platforms
  • Implemented the CustomStringConvertible protocol in the SCGameState class
  • Improved the following methods in the SCGameState class
    • getFields(ofPlayer:)
    • moveDistanceDiagonalFalling(x:y:)
    • moveDistanceDiagonalRising(x:y:)

1.1.0 - 2018-12-14 12:39:06

  • Added a --version command-line argument to the executable
  • Added the methods isEmpty() and isObstructed() to the SCField class
  • Added a subscript to the SCGameState class
  • Renamed the method getFieldsOf(player:) to getFields(ofPlayer:) in the SCGameState class
  • Improved some documentation comments

1.0.0 - 2018-10-05 13:30:16

  • First release