Partially implemented interprocess game of "Scotland Yard". Fork, fill in the gaps, and turn in on Moodle!

Overview

Scotland Yard

The Game


The city is an 8 x 8 grid, a Fugitive runs amok. In a single turn, the Fugitive can move like a chess Queen: horizontally, vertically, or diagonally. Upto k (say, 5, but that’s easily winning for a smart Fugitive. You can tweak this) Detectives attempt to catch the Fugitive. Detectives move like chess Rooks: horizontally, or vertically. In this game, Players don’t block each others’ paths.

The Detectives win if one of them lands on the same square as the Fugitive, or if the Fugitive quits the game.

The Detectives lose if none of them can catch the Fugitive within N (say, 25; again, you can tweak this) timesteps.

The Fugitive can see everyone, but is mostly invisible to the Detectives: the Fugitive only surfaces when 3 mod 5 timesteps have passed, i.e. at timesteps, 3, 8, … , 23, and finally 25, when the game is over.

The Client-Server Model


How do we make computer programs play Scotland Yard? We’ve given you ManualPlayer.java and RandomPlayer.java. A process running the main method of either of these files is what we call a Client. The Client establishes a socket connection to the game Server (that’s the process running the main method of ScotlandYard.java) that listens on a port.

Look at the client code. It basically listens for incoming input from the connection with a BufferedReader, and then decides on a move (either gets it from a human typing on the terminal, or generates it randomly), and sends that move across to the server with the PrintWriter associated with the socket connection.

The Server, on the other hand, caters to several clients, who are playing a single game. The Oracle Java Documentation is a good place to start learning about socket servers. For multiple clients, the way to go is to spawn threads, one for serving each client, and then continue listening again.

Our server is organised as follows: we have a few ports that we can specify on the command line. For each port, the server loops: spawning a ScotlandYardGame and letting it run to completion. It is the ScotlandYardGame that listens on a port for client connections.

For each client that connects via a socket, we create a ServerThread to talk to it. This ServerThread facilitates round by round play. ServerThreads playing the same game have shared access to the same Board, and need to access data consistently and run in synchrony.

Finally, we also have the Moderator, whose primary job is to allow ServerThreads into the next round.

Your job is to get this synchronization right, by completing ScotlandYard.java, Moderator.java, ServerThread.java

Barriers


We said that the game proceeds, round by round. Here’s what a round looks like:

  1. ServerThread gets its move from the client, reading a single line from the socket connection buffer.
  2. ServerThread is allowed to play, and makes the move on the board
  3. ServerThread then waits for all other active ServerThreads to make their move
  4. ServerThread gets the feedback for the round, and relays it to the client via the socket buffer.
  5. ServerThread then waits for all other active ServerThreads before proceeding to the next round.

This scenario, where several threads have to wait for each other before proceeding to the next step, is typical: we call the solution a barrier. Java does have an inbuilt barrier, but that only works for a fixed number of threads, once initialised.

In our game, the number of threads each round can vary. A player might quit, either gracefully by pressing “Q”, or rage quit with Ctrl+C. We need to deal with both gracefully. There can also be players waiting to join, and need to be inducted into the game as soon as possible.

It is the Moderator’s job to regulate all this between rounds, and set this flexible barrier up for the round. Have a look at the code and comments to see how this can be done. The idea is, the number of threads that we expect to trigger the barrier is playingThreads.

You might also like...

A basic representation of the 0-player game, "Game-of-Life", a simple example of basic cellular automata

A basic representation of the 0-player game,

Game-Of-Life-Basic A basic representation of the 0-player game, "Game-of-Life", a simple example of basic cellular automata. A cellular automaton is a

Oct 27, 2022

Jetserver is a high speed nio socket based multiplayer java game server written using Netty and Mike Rettig's Jetlang.It is specifically tuned for network based multiplayer games and supports TCP and UDP network protocols.

Note New version of Jetserver is called Nadron and is in a new netty 4 branch of this same repo. JetServer is a java nio based server specifically des

Dec 14, 2022

A fairly Simple Game made in Java,You can adopt Pets, name them, and take care of them for XpPoints and level up!

A fairly Simple Game made in Java,You can adopt Pets, name them, and take care of them for XpPoints and level up!

Introducing PetGame! A simple console based game made by @denzven in Java ☕ About the Game PetGame is my first big project in Java, the rules are simp

Jun 7, 2022

A Java game development framework that deploys to JVM, HTML5, Android and iOS.

PlayN PlayN is a cross-platform Java game development library written in Java that targets HTML5 browsers (via GWT), desktop JVMs, Android and iOS dev

Dec 9, 2022

Small mod for Minecraft Forge 1.16.5 that sends messages of in-game events to a channel in your Discord server. This mod also enables cross-chatting between Minecraft and Discord.

DiscordSync Small mod for Minecraft Forge 1.16.5 that sends messages of in-game events to a channel in your Discord server. This mod also enables cros

Dec 20, 2022

LITIENGINE is a free and open source Java 2D Game Engine

LITIENGINE is a free and open source Java 2D Game Engine

LITIENGINE is a free and open source Java 2D Game Engine. It provides a comprehensive Java library and a dedicated map editor to create tile-based 2D games.

Jan 7, 2023

A game made in Javafx that includes animation, background music, and view leaderboards

A game made in Javafx that includes animation, background music, and view leaderboards

A game made in Javafx that includes animation, background music, and view leaderboards. To keep the game alive, a player has to move the ball up/down by pressing the up/down arrow so that the ball only touches different rotating obstacles with the same color. ..

Nov 19, 2022

The game is about a girl and she is a university student majoring in Computer Science

The game is about a girl and she is a university student majoring in Computer Science. The goal of the game is to help her pass all of the game's levels. it has 3 levels and each level has 4-5 questions about two subjects from her academic career arranged from the easiest to hardest. (group work)

Dec 18, 2022

2048FX - The game 2048, built using JavaFX and Java 11

2048FX - The game 2048, built using JavaFX and Java 11

The game 2048 built using JavaFX 16 and Java 17.

Dec 28, 2022
Owner
Mihir Jayesh Vahanwala
CSE undergrad student at IIT-Bombay
Mihir Jayesh Vahanwala
Kingdoms Wars - a turn-based strategy game created with Java.

Kingdoms Wars is a turn-based strategy game created with Java. The game is loosely based on the Punic Wars.

Sharvai Patil 4 Feb 7, 2022
A turn-based strategy game

Stalemate Game This is a turn-based strategy game. Support You can report issues in issue tracker. Contributing Contributions are welcomed. Building T

null 1 Jul 6, 2022
Golden Axe (1989) game implemented in java using only standard libraries (Java 2D, Swing, AWT & Java Sound API)

Golden Axe (1989) game implemented in java using only standard libraries (Java 2D, Swing, AWT & Java Sound API), so no external libraries required. Video: https://youtu.be/uevIVLNhQqs

Leo 101 Jul 21, 2022
Run Fabric Mods on Forge! It's an mod loading api, too (not implemented yet). No any releationship between Python library PILlow.

Pillow Mod Loader 中文 | English Quilt that runs on Forge Not implemented yet. Yes, you can believe it. This mod will make Quilt compatible with Forge.

null 18 Dec 20, 2022
Othello PvP (2-Player) Game Application created in Java using Swing and AWT, for playing the Othello game

Othello PvP (2-Player) Game Application created in Java using Swing and AWT, for playing the Othello game, a simple game that played on an 8 by 8 checkered board with 64 double-sided black and white discs. The game is easy to learn, but it takes time to master and develop winning strategies.

Soumyadeep Pal 1 Feb 28, 2022
My Game Engine tested via my Cubecraft Game

My Game Engine tested via my Cubecraft Game Install: mvn -P {your OS name} clean install mvn -P mac clean install mvn -P linux clean install mvn -P wi

null 30 Oct 3, 2022
BattleShip-Game - This repository contains the code of the BattleShip (Bataille Navale) game programmed in Java.

Bataille Navale Auteur : SABIL Mohamed Amine Comment générer la documentation ? Placez vous dans le sous-dossier src : pour générer la documentation d

Mohamed Amine SABIL 1 Jan 4, 2022
Snake-Game - A simple snake game written in java.

What's this? A simple snake game written in java. How to play Download the latest release. Ensure that a compatible java runtime is installed (optimal

Patrick260 4 Oct 31, 2022
The game is a 2D side scrolling system, where a player moves left, right, vertically or diagonally to traverse the game.

The game is a 2D side scrolling system, where a player moves left, right, vertically or diagonally to traverse the game.

Ravi Mandal 7 Sep 11, 2022
A simple puzzle game made with Unity to practice the game engine

A simple puzzle game made with Unity to practice the game engine.

Eyüb Salih Özdemir 1 Mar 30, 2022