#include "iostream"
#include <mpi.h>

int main(int argc, char ** argv)
{
	int myRank = 0, size = 0, message = 0;
	double v = 0, S = 0, fullS = 1, dt = 0.1;
	int tag = 1, player0 = 0, player1 = 1;

	MPI_Init(&argc, &argv);	// init of MPI, must be called before using MPI-functions

	MPI_Comm_size(MPI_COMM_WORLD, &size);	// defining size of specific communicator
	MPI_Comm_rank(MPI_COMM_WORLD, &myRank);	// defining rank of the current process in specific communicator

	if  (myRank == player0)	//	first process is a first "runner"
	{
		while (S < fullS)
		{
			v = (double)(rand())/RAND_MAX;
			S = S + v * dt;
			std::cout << "Player" << myRank << " run " << S << std::endl;
		} 
		MPI_Send(&message, 1, MPI_INT, player1, tag, MPI_COMM_WORLD);	// gives baton to next runner =)
		std::cout << "Next runner!" << std::endl;
	}
	else
	if (myRank == player1)	//	second process is a second "runner"
	{
		MPI_Recv(&message, 1, MPI_INT, player0, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);	//	receives baton from previous runner and starts
		while (S < fullS)
		{
			v = (double)(rand())/RAND_MAX;
			S = S + v * dt;
			std::cout << "Player" << myRank << " run "<< S << std::endl;
		}
		std::cout << "Finish!" << std::endl;
	}

	MPI_Finalize();	// close MPI
	return 0;
}

