-
Notifications
You must be signed in to change notification settings - Fork 0
/
GAhappyfaces.h
63 lines (48 loc) · 2.51 KB
/
GAhappyfaces.h
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
/***************************************************************************
GAhappyfaces.h -
for solving the happy faces puzzle
-------------------
begin : Wed Mar 03 2004
copyright : (C) 2004 by Craig Nicol
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/***************************************************************************
* The happy faces puzzle is played on a 5x5 grid. Each square can be in *
* one of two states (happy or sad, but we will use 0 and 1 here). *
* On each move, the chosen square and it's immediate neighbours above, *
* below, left and right are all switched into their opposite state. *
* The game starts with 25 sad faces. The game ends when the grid contains *
* 25 happy faces. It is possible to complete the game in 25 moves. *
***************************************************************************/
#include <string>
#include "GApopulation.h"
#ifndef _GAHAPPYFACES_H
#define _GAHAPPYFACES_H
typedef int grid_t; // Where grid contains at least 25 boolean values
// class HappyFaces { // Non-GA version
class HappyFaces : public mg_GA::fitness_base<int,50> { // GA version with a 50-move sequence
public:
static grid_t moves[25]; // List of possible moves
int make_move(unsigned int x, unsigned int y);
int make_move(unsigned int square);
std::string to_string(grid_t &grid);
std::string to_string() {return to_string(_current); };
int count_happy();
int count_sad() {return 25-count_happy();};
bool all_happy() {return (_current == (1<<25)-1); };
bool all_sad() {return (_current == 0); };
HappyFaces(grid_t start = 0) : _current(start) {};
// GA fitness function
double operator()(int chrom[50]);
private:
grid_t _current;
};
#endif // defined _GAHAPPYFACES_H