プログラミング入門 情報科学演習 宿題 2013.12.09

Back to text page


簡単なすごろくゲームを作ります.以下のようにマス目が15個あるとして,コマの位置を x で,駒のいないマスを o で表します.さいころを振って出た目の数だけコマを動かして行き,最後にちょうどの数が出たら上がれるものとします.さいころの目が大きすぎたらやり直しです.for 文では無く while 文で作りましょう.

My student number: s134099

Start!     xoooooooooooooo

Dice: 3    oooxooooooooooo

Dice: 4    oooooooxooooooo

Dice: 5    ooooooooooooxoo

Dice: 6    Too large to go. Cast dice again!

Dice: 2    oooooooooooooox
Goal!

-----------------------

Start! と,ゴールしたときの絵は反復処理を使用しないで,以下のように出力すれば結構です.

	printf("Start!     ");
	printf("xoooooooooooooo\n\n");
	
	.........
	
	printf("oooooooooooooox\n");
	printf("Goal!\n\n");

解答例

/* ************************************************** */
/*                                                    */
/*      プログラミング入門  情報科学演習C7                            */
/*      レポート課題                                  */
/*      2013.12.09                                    */
/*                                                    */
/* ************************************************** */
/*                                                    */
/*      学生番号:                                    */
/*                                                    */
/*      氏名:                                        */
/*                                                    */
/* ************************************************** */
/*                                                    */
/*      この行以降に解答のプログラムを書くこと        */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main()
{
	srand((unsigned) time(NULL));
	
	int i, p = 1, dice;
	
	printf("My student number: s134099\n\n");
	printf("Start!     ");
	printf("xoooooooooooooo\n\n");
	
	while(p<15){
		dice = rand() % 6 + 1;
		printf("Dice: %d    ", dice);
		
		p += dice;
		
		if(p>15){
			printf("Too large to go. Cast dice again!\n\n");	
			p -= dice;
		}
		else if(p==15){
			printf("oooooooooooooox\n");
			printf("Goal!\n\n");
		}
		else{		
			i = 1;
			while(i<=15){
				if(i==p)
					printf("x");
				else
					printf("o");
				
				i++;
			}
			printf("\n\n");
		}
	}
	
	printf("-----------------------\n\n");
	
	return(0);
}


Back to text page