~ubuntu-branches/ubuntu/trusty/grhino/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
	rand.cc		Random board number generator
	Copyright (c) 2004 Kriang Lerdsuwanakij
	email:		lerdsuwa@users.sourceforge.net

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#include "rand.h"
#include "randboard.h"
#include "order.h"
#include "gtstream.h"
#include <stdexcept>

#ifdef _
#undef _
#endif

#ifdef N_
#undef N_
#endif

#include <libintl.h>
#define _(x) gettext(x)
#define N_(x) x

// Pieces are randomly placed according to the following steps:
// All '0' position must be placed before a piece is added on
// any '1' position, and so on.
// . . . . . . . .
// . . 3 2 2 3 . .
// . 3 2 1 1 2 3 .
// . 2 1 0 0 1 2 .
// . 2 1 0 0 1 2 .
// . 3 2 1 1 2 3 .
// . . 3 2 2 2 . .
// . . . . . . . .

static const int NUM_RANDOM_STEP0 = 4;
static const int NUM_RANDOM_STEP1 = 8;
static const int NUM_RANDOM_STEP2 = 12;
static const int NUM_RANDOM_STEP3 = 8;

static const int TOTAL_NUM_RANDOM_STEP = NUM_RANDOM_STEP0
					 + NUM_RANDOM_STEP1
					 + NUM_RANDOM_STEP2
					 + NUM_RANDOM_STEP3;
static const int MAX_NUM_RANDOM_STEP = 12;

static int random_step0[NUM_RANDOM_STEP0] = { D4, E4,
					      D5, E5 };

static int random_step1[NUM_RANDOM_STEP1] = {	  D3, E3,
					      C4,	  F4, 
					      C5,	  F5,
					          D6, E6 };

static int random_step2[NUM_RANDOM_STEP2] = {	      D2, E2,
						  C3,	      F3,
					      B4,		  G4,
					      B5,		  G5,
						  C6,	      F6,
						      D7, E7 };

static int random_step3[NUM_RANDOM_STEP3] = {	  C2, F2,
					      B3,	  G3, 
					      B6,	  G6,
					          C7, F7 };

// Valid values for num are 4, 6, 8, ..., 32
// Reentrant: only write to local variable
void	random_board(byte_board_info *b, int num)
{
	if (num < NUM_RANDOM_STEP0 || num > TOTAL_NUM_RANDOM_STEP
	    || (num & 1)) {
		gtstream bufstr;
		gtout(bufstr, _("invalid number of pieces %$ in random_board"))
			<< num;
		throw std::range_error(bufstr.str());
	}

	byte_board_type init;
	for (int i = 0; i < 64; ++i)
		init[i] = EMPTY;

	int pos_array_step[MAX_NUM_RANDOM_STEP];
	int pos_array[TOTAL_NUM_RANDOM_STEP];
	int cur_pos_array = 0;

					// Do step 0
	int cur_step = NUM_RANDOM_STEP0;
	int cur_step_left = NUM_RANDOM_STEP0;
	int num_left = num - cur_step;

	for (int i = 0; i < NUM_RANDOM_STEP0; ++i)
		pos_array_step[i] = random_step0[i];

	while (cur_step) {
		int pos = (cur_step_left > 1) ? get_random(cur_step_left) : 0;
		pos_array[cur_pos_array++] = pos_array_step[pos];
		--cur_step;
		--cur_step_left;
		pos_array_step[pos] = pos_array_step[cur_step_left];
	}

					// Do step 1
	if (num_left) {
		if (num_left < NUM_RANDOM_STEP1)
			cur_step = num_left;
		else
			cur_step = NUM_RANDOM_STEP1;
		cur_step_left = NUM_RANDOM_STEP1;
		num_left -= cur_step;

		for (int i = 0; i < NUM_RANDOM_STEP1; ++i)
			pos_array_step[i] = random_step1[i];

		while (cur_step) {
			int pos = (cur_step_left > 1) ? get_random(cur_step_left) : 0;
			pos_array[cur_pos_array++] = pos_array_step[pos];
			--cur_step;
			--cur_step_left;
			pos_array_step[pos] = pos_array_step[cur_step_left];
		}
	}

					// Do step 2
	if (num_left) {
		if (num_left < NUM_RANDOM_STEP2)
			cur_step = num_left;
		else
			cur_step = NUM_RANDOM_STEP2;
		cur_step_left = NUM_RANDOM_STEP2;
		num_left -= cur_step;

		for (int i = 0; i < NUM_RANDOM_STEP2; ++i)
			pos_array_step[i] = random_step2[i];

		while (cur_step) {
			int pos = (cur_step_left > 1) ? get_random(cur_step_left) : 0;
			pos_array[cur_pos_array++] = pos_array_step[pos];
			--cur_step;
			--cur_step_left;
			pos_array_step[pos] = pos_array_step[cur_step_left];
		}
	}

					// Do step 3
	if (num_left) {
		if (num_left < NUM_RANDOM_STEP3)
			cur_step = num_left;
		else
			cur_step = NUM_RANDOM_STEP3;
		cur_step_left = NUM_RANDOM_STEP3;
		num_left -= cur_step;

		for (int i = 0; i < NUM_RANDOM_STEP3; ++i)
			pos_array_step[i] = random_step3[i];

		while (cur_step) {
			int pos = (cur_step_left > 1) ? get_random(cur_step_left) : 0;
			pos_array[cur_pos_array++] = pos_array_step[pos];
			--cur_step;
			--cur_step_left;
			pos_array_step[pos] = pos_array_step[cur_step_left];
		}
	}

	cur_step = cur_pos_array;
	int color = BLACK;
	while (cur_step) {
		int pos = (cur_step > 1) ? get_random(cur_step) : 0;
		init[pos_array[pos]] = color;
		color = switch_color(color);
		--cur_step;
		pos_array[pos] = pos_array[cur_step];
	}
	*b = &init;
}