How to resolve the algorithm Pig the dice game step by step in the Eiffel programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Pig the dice game step by step in the Eiffel programming language

Table of Contents

Problem Statement

The   game of Pig   is a multiplayer game played with a single six-sided die.   The object of the game is to reach   100   points or more.   Play is taken in turns.   On each person's turn that person has the option of either:

Create a program to score for, and simulate dice throws for, a two-person game.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Pig the dice game step by step in the Eiffel programming language

Source code in the eiffel programming language

class
	PLAYER
create
	set_name
feature
	set_name(n:STRING)
		do
			name := n.twin
			set_points(0)
		end

	strategy(cur_points:INTEGER)
		local
			current_points, thrown:INTEGER
		do
			io.put_string ("You currently have " +points.out+". %NDo you want to save your points? Press y or n.%N")
			io.read_line
			if io.last_string.same_string ("y") then
				set_points(cur_points)
			else
				io.put_string ("Then throw again.%N")
				thrown:=throw_dice
				if thrown= 1 then
					io.put_string("You loose your points%N")
				else
					strategy(cur_points+thrown)
				end
			end

		end
	set_points (value:INTEGER)
		require
			value_not_neg: value >= 0
		do
			points := points + value
		end

	random: V_RANDOM
			-- Random sequence.
		once
			create Result
		end
	throw_dice: INTEGER
	        do
		        random.forth
	         	Result := random.bounded_item (1, 6)
	        end

	name: STRING
	points: INTEGER
end


class
	PIG_THE_DICE

feature
	play
	local
		points, i: INTEGER
	do
		io.put_string("Welcome to the game.%N")
		initiate_players
		from

		until
			winner/=void
		loop
			across player as p  loop
			points:=p.item.throw_dice
			io.put_string ("%N" + p.item.name +" you throwed " + points.out + ".%N")
			if points =1 then
				io.put_string ("You loose your points.%N")
			else
				p.item.strategy(points)
			end
			if p.item.points >=100 then
				winner := p.item
				io.put_string ("%NThe winner is " + winner.name.out + ".%N")
			end
			end
		end
	end

	initiate_players
	local
		p1,p2: PLAYER
	do
		create player.make (1, 2)
		create p1.set_name ("Player1")
		player.put (p1, 1)
		create p2.set_name ("Player2")
		player.put (p2, 2)
	end

	player: V_ARRAY[PLAYER]
	winner: PLAYER
end


class
	APPLICATION
inherit
	ARGUMENTS
create
	make
feature {NONE} -- Initialization
	make
	local
		do
			create pig
			pig.initiate_players
			pig.play
		end
	pig: PIG_THE_DICE
end


  

You may also check:How to resolve the algorithm File size step by step in the RapidQ programming language
You may also check:How to resolve the algorithm A+B step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Gray code step by step in the K programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the WDTE programming language
You may also check:How to resolve the algorithm SEDOLs step by step in the C# programming language