Skip to content

Commit e4747b5

Browse files
authored
Merge pull request #13 from FransFaase/master
Added domineering.ino to play the domineering game
2 parents 5e168fa + 470b7fd commit e4747b5

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Domineering
3+
*
4+
* Allows you to play the game domineering
5+
* (See https://en.wikipedia.org/wiki/Domineering)
6+
* with the use of a potentiometer. Player take
7+
* turns removing two blinking leds by turning the
8+
* potentiometer. After a user stops turning the
9+
* potentiometer the blinking slows down and removes
10+
* the two blinking leds, giving the turn to the
11+
* other player.
12+
*
13+
* The size of the playing field can be changed by
14+
* modifying BOTTOM and TOP defines (taking into
15+
* account: (0 <= BOTTOM < TOP <= 8)
16+
*/
17+
static const int DATA_PIN = 20;
18+
static const int CLK_PIN = 5;
19+
static const int CS_PIN = 21;
20+
21+
#include "LedControl.h"
22+
#include "Timer.h"
23+
24+
#define POTPIN A5 // Potentiometer
25+
26+
LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1);
27+
28+
#define brightness 1 //Values from 1 to 15 to set the brightness
29+
uint8_t matrix[8];
30+
#define MAT(X,Y) (matrix[X] & (1 << Y))
31+
#define SETMAT(X,Y) matrix[X] |= (1 << Y)
32+
#define RESETMAT(X,Y) matrix[X] &= ~(1 << Y)
33+
uint8_t x_p[56];
34+
uint8_t y_p[56];
35+
#define BOTTOM 2
36+
#define TOP 7
37+
38+
void showmat()
39+
{
40+
for (uint8_t x = 0; x < 8; x++)
41+
lc.setColumn(0, x, matrix[x]);
42+
}
43+
44+
void init_field()
45+
{
46+
for (uint8_t i = 0; i < 8; i++)
47+
matrix[i] = 0;
48+
for (uint8_t y = BOTTOM; y < TOP; y++)
49+
for (uint8_t x = BOTTOM; x < TOP; x++)
50+
SETMAT(x,y);
51+
showmat();
52+
}
53+
54+
bool vert_player = false;
55+
56+
void setup() {
57+
pinMode(POTPIN, INPUT);
58+
59+
lc.shutdown(0, false);
60+
lc.setIntensity(0, brightness);
61+
lc.clearDisplay(0);
62+
Serial.begin(9600);
63+
Serial.println("Start");
64+
init_field();
65+
}
66+
67+
68+
void loop() {
69+
uint8_t len = 0;
70+
uint8_t v_x = 0;
71+
uint8_t v_y = 0;
72+
if (vert_player)
73+
{
74+
v_x = 1;
75+
for (uint8_t y = BOTTOM; y < TOP; y++)
76+
for (uint8_t x = BOTTOM; x < TOP-1; x++)
77+
if (MAT(x,y) != 0 && MAT(x+1, y) != 0)
78+
{
79+
x_p[len] = x;
80+
y_p[len] = y;
81+
len++;
82+
}
83+
}
84+
else
85+
{
86+
v_y = 1;
87+
for (uint8_t x = BOTTOM; x < TOP; x++)
88+
for (uint8_t y = BOTTOM; y < TOP-1; y++)
89+
if (MAT(x,y) != 0 && MAT(x, y+1) != 0)
90+
{
91+
x_p[len] = x;
92+
y_p[len] = y;
93+
len++;
94+
}
95+
}
96+
if (len == 0)
97+
{
98+
init_field();
99+
return;
100+
}
101+
102+
int prev_pos = -1;
103+
bool moved = len == 1;
104+
int d = 50;
105+
for (;;)
106+
{
107+
int val = analogRead(POTPIN);
108+
int pos = (val - 12)/(1000/len);
109+
if (pos < 0)
110+
pos = 0;
111+
if (pos >= len)
112+
pos = len - 1;
113+
if (prev_pos != -1 && pos != prev_pos)
114+
{
115+
moved = true;
116+
d = 50;
117+
}
118+
else if (moved)
119+
d += 50;
120+
prev_pos = pos;
121+
122+
RESETMAT(x_p[pos], y_p[pos]);
123+
RESETMAT(x_p[pos]+v_x, y_p[pos]+v_y);
124+
showmat();
125+
126+
delay(d);
127+
128+
if (d > 500)
129+
break;
130+
131+
SETMAT(x_p[pos], y_p[pos]);
132+
SETMAT(x_p[pos]+v_x, y_p[pos]+v_y);
133+
showmat();
134+
135+
delay(d);
136+
}
137+
vert_player = !vert_player;
138+
}

0 commit comments

Comments
 (0)