10409 - Die Game

Problem G: Die Game

Life is not easy. Sometimes it is beyond your control. Now, as contestants of ACM ICPC, you might be just tasting the bitter of life. But don't worry! Do not look only on the dark side of life, but look also on the bright side. Life may be an enjoyable game of chance, like throwing dice. Do or die! Then, at last, you might be able to find the route to victory.

This problem comes from a game using a die. By the way, do you know a die? It has nothing to do with "death." A die is a cubic object with six faces, each of which represents a different number from one to six and is marked with the corresponding number of spots. Since it is usually used in pair, "a die" is a rarely used word. You might have heard a famous phrase "the die is cast," though.

When a game starts, a die stands still on a flat table. During the game, the die is tumbled in all directions by the dealer. You will win the game if you can predict the number seen on the top face at the time when the die stops tumbling.

Now you are requested to write a program that simulates the rolling of a die. For simplicity, we assume that the die neither slips nor jumps but just rolls on the table in four directions, that is, north, east, south, and west. At the beginning of every game, the dealer puts the die at the center of the table and adjusts its direction so that the numbers one, two, and three are seen on the top, north, and west faces, respectively. For the other three faces, we do not explicitly specify anything but tell you the golden rule: the sum of the numbers on any pair of opposite faces is always seven.

Your program should accept a sequence of commands, each of which is either "north", "east", "south", or "west". A "north" command tumbles the die down to north, that is, the top face becomes the new north, the north becomes the new bottom, and so on. More precisely, the die is rotated around its north bottom edge to the north direction and the rotation angle is 90 degrees. Other commands also tumble the die accordingly to their own directions. Your program should calculate the number finally shown on the top after performing the commands in the sequence. Note that the table is sufficiently large and the die never falls off during the game.

        

Input

The input consists of one or more command sequences, each of which corresponds to a single game. The first line of a command sequence contains a positive integer, representing the number of the following command lines in the sequence. You may assume that this number is less than or equal to 1024. A line containing a zero indicates the end of the input. Each command line includes a command that is one of northeastsouth, and west. You may assume that no white space occurs in any lines.

Output

For each command sequence, output one line containing solely the number on the top face at the time when the game is finished.

Sample Input

1
north
3
north
east
south
0

Output for the Sample Input

5
1
#include<stdio.h>
int main()
{
	int N,i;
	char x[10];
	while(scanf("%d",&N)&&N)
	{
		int n=2,w=3,e=4,s=5,t=1,b=6;
		getchar();
		for(i=0;i<N;i++)
		{
			int n1,b1,t1,s1,w1,e1;
			gets(x);
			if(x[0]=='n')
			{n1=t;b1=n;w1=w;e1=e;s1=7-n1;t1=7-b1;}
			else if(x[0]=='s')
			{s1=t;b1=s;w1=w;e1=e;n1=7-s1;t1=7-b1;}
			else if(x[0]=='w')
			{w1=t;b1=w;n1=n;s1=s;e1=7-w1;t1=7-b1;}
			else if(x[0]=='e')
			{e1=t;b1=e;n1=n;s1=s;w1=7-e1;t1=7-b1;}
			n=n1;b=b1;t=t1;s=s1;w=w1;e=e1;
		}
		printf("%d\n",t);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's a possible implementation of the game using the classes described: ```python import random class Die: """Class representing a 6-sided die.""" def __init__(self): self.value = None def roll(self): """Rolls the die and sets its value to a random integer between 1 and 6.""" self.value = random.randint(1, 6) class DiceCup: """Class representing a cup containing five dice.""" def __init__(self): self.dice = [Die() for _ in range(5)] self.banked_dice = [] def roll(self): """Rolls all non-banked dice in the cup.""" for die in self.dice: if die not in self.banked_dice: die.roll() def bank(self, index): """Banks the die at the specified index.""" self.banked_dice.append(self.dice[index]) del self.dice[index] def release(self, index): """Releases the die at the specified index from the bank.""" self.dice.append(self.banked_dice[index]) del self.banked_dice[index] class ShipOfFoolsGame: """Class representing the game logic.""" def __init__(self, winning_score): self.winning_score = winning_score def play_round(self, players): """Plays one round of the game with the given players.""" for player in players: player.play_round(self) winner = max(players, key=lambda p: p.score) print(f"Player {winner.id} wins the round with a score of {winner.score}") if winner.score >= self.winning_score: print(f"Player {winner.id} wins the game with a total score of {winner.score}") return True else: return False class Player: """Class representing a player.""" def __init__(self, id): self.id = id self.score = 0 def play_round(self, game): """Plays one round of the game.""" cup = DiceCup() cup.roll() for i in range(5): if i in [0, 1, 2] and cup.dice[i].value == i+4: cup.bank(i) elif i == 3 and cup.dice[i].value == 5: cup.bank(i) elif i == 4 and cup.dice[i].value == 6: cup.bank(i) cup.roll() self.score += sum(cup.banked_dice) class PlayRoom: """Class representing a room with multiple players and a game.""" def __init__(self, num_players, winning_score): self.players = [Player(i+1) for i in range(num_players)] self.game = ShipOfFoolsGame(winning_score) def play_game(self): """Plays the game until a player reaches the winning score.""" while not self.game.play_round(self.players): pass ``` In this implementation, the `Die` class represents a 6-sided die and has a `roll()` method that generates a random value between 1 and 6. The `DiceCup` class represents a cup containing five dice and has methods for rolling all non-banked dice, banking and releasing individual dice, and calculating the score of the banked dice. The `ShipOfFoolsGame` class represents the game logic and has a `play_round()` method that lets each player play a round of the game, calculates the winner, and checks if the game has been won. The `Player` class represents a player and has a `play_round()` method that plays a round of the game for that player, using a `DiceCup` object and following the game rules. Finally, the `PlayRoom` class represents a room with multiple players and a `ShipOfFoolsGame` object, and has a `play_game()` method that plays the game until a player reaches the winning score.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值