~hudzilla/brainparty/devel

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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)

// Brain Party 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 3
// 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "diceoff.h"
#include "Minigame.h"

BPMiniGame_DiceOff::BPMiniGame_DiceOff(BPGame* game) : BPMiniGame(game) {
	TheGame = game;
	
	State = WAITING;
	OurTurn = true;
	FirstAITurn = true;
	
	PlaySound = false;
	
	NeutralColor = Colour(0.7f, 0.7f, 0.7f, 1.0f);
	PlayerColor = Colour(0.0f, 0.0f, 1.0f, 1.0f);
	AIColor = Colour(1.0f, 0.0f, 0.0f, 1.0f);
	
	DicePics.Add(NULL);
	DicePics.Add(TheGame->LoadBitmap("die_1", 50, 50));
	DicePics.Add(TheGame->LoadBitmap("die_2", 50, 50));
	DicePics.Add(TheGame->LoadBitmap("die_3", 50, 50));
	DicePics.Add(TheGame->LoadBitmap("die_4", 50, 50));
	
	for (int i = 0; i < NumRows; ++i) {
		for (int j = 0; j < NumCols; ++j) {
			BPMiniGame_DiceOff_Die* die = new BPMiniGame_DiceOff_Die();
			die->Pos = BPPoint(10 + (j * DiceSize), 8 + (i * DiceSize));
			die->Value = 1;
			die->Index = Dice.Count;
			die->X = j;
			die->Y = i;
			die->Col = NeutralColor;
			die->NumNeighbours = CountNeighbours(j, i);
			Dice.Add(die);
		}
	}
	
	UpdateScore();
	
	GameTitle = "Dice Off";
	GameHelp = "Press a dice to add one to its score. When it goes higher than the number of neighbours it has, it will overflow, taking over each of its neighbours and adding one to them. Can you take over all your opponent's dice?";
	GameHelp2 = "Dice in the corners have just two neighbours, so as soon as they reach three they will overflow and add one to each neighbour, taking control of them at the same time.";
	
	MiniGameType = ACTION;
}

BPMiniGame_DiceOff::~BPMiniGame_DiceOff() {
	DicePics.Clear();
	Dice.Clear();
}

void BPMiniGame_DiceOff::Start() {
	
}

int BPMiniGame_DiceOff::GetWeight() {
	return 500;
}

void BPMiniGame_DiceOff::Tick() {
	switch (State) {
		case SUCCESS:
			if (LastStateChange + 1000 < TheGame->TickCount) {
				Success();
			}
			
			break;
			
		case FAILURE:
			if (LastStateChange + 1000 < TheGame->TickCount) {
				Failure();
			}
			
			break;
			
		case WAITING:
			// do nothing!
			break;
			
		case THINKING:
			// give a short delay, then make the AI act
			if (LastStateChange + AIThinkSpeed < TheGame->TickCount) {
				AITurn();
			}
			
			break;
			
		case CHANGING:
			// wait before changing more pieces
			if (LastStateChange + ChainSpeed < TheGame->TickCount) {
				PlaySound = false;
				
				if (CheckChangeList.Count == 0 || ChangeStartTime + 4000 < TheGame->TickCount) { // force kill changes that take too long
					// no more moves to make
					if (OurTurn) {
						OurTurn = false;
						SetGameState(THINKING);
					} else {
						OurTurn = true;
						SetGameState(WAITING);
					}
					
					return;
				}
				
				BPList<BPMiniGame_DiceOff_Die*> ToChange;
				
				for (int i = 0; i < CheckChangeList.Count; ++i) {
					ToChange.Add(CheckChangeList[i]);
				}
				
				CheckChangeList.Clear();
				
				for (int i = 0; i < ToChange.Count; ++i) {
					BPMiniGame_DiceOff_Die* die = ToChange[i];
					
					if (OurTurn) {
						BumpDie(die, OWNER_PLAYER);
					} else {
						BumpDie(die, OWNER_AI);
					}
				}
				
				LastStateChange = TheGame->TickCount;
				
				if (PlaySound) {
					// something interesting changed; we should play a sound!
					TheGame->PlaySound("whack");
				}
				
				UpdateScore();
			}
	}
}

int BPMiniGame_DiceOff::CountNeighbours(int x, int y) {
	int result = 0;
	if (x > 0) ++result; // one to the left
	if (x < NumCols - 1) ++result; // one to the right
	if (y > 0) ++result; // one above
	if (y < NumRows - 1) ++result; // one below
	
	return result;
}

void BPMiniGame_DiceOff::Render() {
	TheGame->Clear(TheGame->Black);
	
	for (int i = 0; i < Dice.Count; ++i) {
		BPMiniGame_DiceOff_Die* die = Dice[i];
		
		if (die->LastStateChange + ChangeSpeed > TheGame->TickCount) {
			// this needs to be animated!
			float diff = (TheGame->TickCount - die->LastStateChange) / (float)ChangeSpeed;
			if (diff > 1.0f) diff = 1.0f;
			
			Colour drawcol = TheGame->ColourSmoothStep((*TheGame->White), die->Col, diff);
			
			DicePics[die->Value]->Draw(die->Pos.X + 25, die->Pos.Y + 25, 0.0f, 1.0f, drawcol);
		} else {
			// draw plain-old die
			DicePics[die->Value]->Draw(die->Pos.X + 25, die->Pos.Y + 25, 0.0f, 1.0f, die->Col);
		}
	}
}

void BPMiniGame_DiceOff::OnMouseDown() {
	if (!OurTurn) return;
	if (State != WAITING) return;
	
	for (int i = 0; i < Dice.Count; ++i) {
		BPMiniGame_DiceOff_Die* die = Dice[i];
		
		if (TheGame->PointOverRect(TouchEvent.X, TouchEvent.Y, die->Pos.X, die->Pos.Y, DiceSize, DiceSize)) {
			if (die->Owner != OWNER_AI) {				
				CheckChangeList.Add(die);
				SetGameState(CHANGING);
				ChangeStartTime = TheGame->TickCount;
				break;
			}
		}
	}
}

void BPMiniGame_DiceOff::OnMouseUp() {
	
}

void BPMiniGame_DiceOff::OnMouseMove() {
	
}

void BPMiniGame_DiceOff::BumpDie(BPMiniGame_DiceOff_Die* die, Ownership newowner) {
	++die->Value;
	die->LastStateChange = TheGame->TickCount;
	die->Owner = newowner;
	
	if (newowner == OWNER_PLAYER) {
		die->Col = PlayerColor;
	} else {
		die->Col = AIColor;
	}
	
	PlaySound = true;
	
	if (die->Value > die->NumNeighbours) {
		// we need to split this!
		die->Value = 1;
		
		if (die->X > 0) CheckChangeList.Add(Dice[die->Index - 1]);
		if (die->X < NumCols - 1) CheckChangeList.Add(Dice[die->Index + 1]);
		if (die->Y > 0) CheckChangeList.Add(Dice[die->Index - NumCols]);
		if (die->Y < NumRows - 1) CheckChangeList.Add(Dice[die->Index + NumCols]);
	}
}

void BPMiniGame_DiceOff::AITurn() {
	// this isn't a particularly smart AI, but it's good enough to provide a challenge to all but the best players
	
	OurTurn = false;
	
	BPList<BPMiniGame_DiceOff_Die*> bestdice;
	int bestscore = 0;
	
	for (int i = 0; i < Dice.Count; ++i) {
		AIClosedList.Clear();
		
		BPMiniGame_DiceOff_Die* die = Dice[i];
		if (die->Owner == OWNER_PLAYER) continue; // we can't move their pieces
		AICheck(die);
		
		if (AIClosedList.Count > bestscore) {
			// this is a better score than what we had before - clear all the options and use this one
			bestscore = AIClosedList.Count;
			bestdice.Clear();
			bestdice.Add(die);
		} else if (AIClosedList.Count == bestscore) {
			// this move is as good as our previous move
			bestdice.Add(die);
		}
	}
	
	if (bestdice.Count > 0) {
		BPMiniGame_DiceOff_Die* die = bestdice[TheGame->RandomRange(0, bestdice.Count - 1)];
		
		if (FirstAITurn) {
			// IF the player started in a corner
			// AND if the AI chooses to start next to them
			// THEN the player can win in one turn just by bumping their corner piece one more
			// SO: stop the AI using an edge tile on their first turn
			
			while (die->NumNeighbours < 4) {
				die = bestdice[TheGame->RandomRange(0, bestdice.Count - 1)];				
			}
		}
		
		CheckChangeList.Add(die);
		SetGameState(CHANGING);
		FirstAITurn = false;
		ChangeStartTime = TheGame->TickCount;
	}
}

void BPMiniGame_DiceOff::AICheck(BPMiniGame_DiceOff_Die* die) {
	if (AIClosedList.Contains(die)) return;
	
	AIClosedList.Add(die);
	
	if (die->Value + 1 > die->NumNeighbours) {
		// this die would break if we bumped it!
		if (die->X > 0) AICheck(Dice[die->Index - 1]);
		if (die->X < NumCols - 1) AICheck(Dice[die->Index + 1]);
		if (die->Y > 0) AICheck(Dice[die->Index - NumCols]);
		if (die->Y < NumRows - 1) AICheck(Dice[die->Index + NumCols]);
	}
}

void BPMiniGame_DiceOff::SetGameState(MiniGameStates state) {
	// if we've already won/lost, bail out
	if (State == FAILURE || State == SUCCESS) return;
	
	State = state;
	LastStateChange = TheGame->TickCount;
}

void BPMiniGame_DiceOff::UpdateScore() {
	int playerscore = 0;
	int aiscore = 0;
	
	for (int i = 0; i < Dice.Count; ++i) {
		BPMiniGame_DiceOff_Die* die = Dice[i];
		
		if (die->Owner == OWNER_PLAYER) {
			++playerscore;
		} else if (die->Owner == OWNER_AI) {
			++aiscore;
		}
	}
	
//	TheGame->AllocString(&sfcScore, [NSString stringWithFormat:"%d:%d", playerscore, aiscore], XLARGE, 320, 64, CENTRED, false);	
	
	// if we're both low on points, this is the start of the game
	if (playerscore <= 1 && aiscore <= 1) return;
	
	if (playerscore == 0 && State != FAILURE) {
		SetGameState(FAILURE);
		return;
	} else if (aiscore == 0 && State != SUCCESS) {
		SetGameState(SUCCESS);
		return;
	}
}