⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sugoroku.c

📁 日文版畅销书籍10天学会C语言的源程序。适合入门者学习使用。希望对大家有帮助。
💻 C
字号:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

//憃榋
struct  board {
	int p; //恑傓悢
	int h; //媥傒僼儔僌
	char comment[128];
};

int p_position = 1; //僾儗僀儎乕偺尰嵼埵抲
int c_position = 1; //僐儞僺儏乕僞偺尰嵼埵抲
int max = 20;

void writeBoard(void);

main () {
	int player; //僾儗僀儎乕偺僒僀僐儘偺栚
	int computer; //僐儞僺儏乕僞偺僒僀僐儘偺栚
	int p_turn = 1; //僾儗僀儎乕偺僞乕儞僼儔僌
	int player_h = 0; //僾儗僀儎乕偺巆傝媥傒夞悢
	int computer_h = 0; //僐儞僺儏乕僞偺巆傝媥傒夞悢

	int add;
	struct board sugoroku[20] = {
		{0, 0, "僗僞乕僩"},
		{0, 0, "妛廗弶擔丅弴挷側妸傝弌偟"},
		{0, 2, "婥崌傪擖傟偡偓偰偟傚偭傁側嵙愜丅俀夞媥傒"},
		{3, 0, "挷巕椙偄偧両俁偮恑傓"},
		{-2, 0, "榑棟墘嶼棟夝晄擻丅俀偮栠傞"},
		{0, 0, "暘婒惂屼傪儅僗僞乕両"},
		{0, 0, "儖乕僾惂屼傪儅僗僞乕両"},
		{0, 0, "攝楍傪儅僗僞乕両"},
		{0, 3, "堄枴傕側偔俁夞媥傒"},
		{-4, 0, "億僀儞僞偵媰偔丅係偮栠傞"},
		{0, 0, "娭悢傪儅僗僞乕両"},
		{1, 0, "娙扨側僎乕儉偑弌棃偨両侾偮恑傓"},
		{0, 0, "僼傽僀儖擖弌椡傪儅僗僞乕両"},
		{0, 1, "偲傝偁偊偢侾夞媥傒"},
		{0, 0, "峔憿懱傪儅僗僞乕両"},
		{-3, 0, "峔憿懱偺棟夝偱旀傟偨丅俁偮栠傞"},
		{0, 0, "姷傟偰偒偨偺偱儅僋儘側傫偐巊偭偰傒傞"},
		{0, 0, "偪傚偭偲暋嶨側僎乕儉僾儘僌儔儉側傫偰嶌偭偰傒傞"},
		{0, 0, "挷巕椙偄偧両傕偆彮偟偱扙弶怱幰丠両"},
		{-9, 0, "夝偭偨偮傕傝偱偄偨傜幚偼慡慠夝偭偰側偐偭偨丅俋偮栠傞"},
	};

	printf("亂俠尵岅儅僗僞乕憃榋亃僾儗僀儎乕偺愭峌\n");
	srand(time(NULL));

	for(;; p_turn = !p_turn) {
		if(p_turn){
			//僾儗僀儎乕偺巆傝媥傒夞悢偑偁傞応崌
			if(player_h > 0) {
				printf("\n僾儗僀儎乕偼偁偲 %d 夞媥傒偱偡\n", player_h);
				player_h--;
				continue;
			}

			//僒僀僐儘傪怳傞
			printf("\n僾儗僀儎乕偺斣偱偡丅僒僀僐儘傪怳傞>");
			while (getchar() != '\n') {  }
			player = rand()%6 + 1;

			//埵抲寛掕仌儅僢僾堏摦
			p_position += player;
			printf("栚丗%d\n", player);
			if(p_position > max) { break; }

			//捛壛堏摦
			do{ 
				add = sugoroku[p_position-1].p;
				player_h = sugoroku[p_position-1].h;
				printf("%s\n", sugoroku[p_position-1].comment);
				p_position += add;
			} while(add != 0);
			if(p_position > max) { break; }
			printf("尰嵼埵抲 %d / %d\n", p_position, max);
		} else {
			//僐儞僺儏乕僞偺巆傝媥傒夞悢偑偁傞応崌
			if(computer_h > 0) {
				printf("\n僐儞僺儏乕僞偼偁偲 %d 夞媥傒偱偡\n", computer_h);
				computer_h--;
				continue;
			}

			//僒僀僐儘傪怳傞
			printf("\n僐儞僺儏乕僞偺斣偱偡丅");
			while (getchar() != '\n') {  }
			computer = rand()%6 + 1;

			//埵抲寛掕仌儅僢僾堏摦
			c_position += computer;
			printf("栚丗%d\n", computer);
			if(c_position > max) { break; }

			//捛壛堏摦
			do{ 
				add = sugoroku[c_position-1].p;
				computer_h = sugoroku[c_position-1].h;
				printf("%s\n", sugoroku[c_position-1].comment);
				c_position += add;
			} while(add != 0);
			if(c_position > max) { break; }
			printf("尰嵼埵抲 %d / %d\n", c_position, max);
		}
		writeBoard();
	}
	printf("偁偑傝両\n");
	if(p_turn) {
		printf("僾儗僀儎乕偺彑偪両\n"); 
	} else { printf("僾儗僀儎乕偺晧偗\n"); }
	return 0;
}

//尰嵼埵抲傪昞帵偡傞娭悢
void writeBoard(void) {
	int i;
	for(i = 0; i < max; i++) {
		if((i == p_position-1) && (i == c_position-1)) { 
			printf("踢

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -