summaryrefslogtreecommitdiff
path: root/src/2048.cpp
blob: 13afd1fe4af680b34cd6ffa0f3a89ea7127347ba (plain)
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
/*
  Date: 2 Feb 2016
  Author: Vasil Zlatanov
*/

#include "2048.h"
#include <stdio.h>
#include <stdlib.h> //needed for random
#include <time.h>   // needed to for "random" seed

void init(int board[SIZE][SIZE])
{
	int x, y;
	// Load start config
	for (x = 0; x < SIZE; x++) {
		for (y = 0; y < SIZE; y++) {
			board[x][y] = 0;
		}
	}
	board[SIZE - 1][SIZE - 1] = 1;
}

int move(int direction, int board[SIZE][SIZE]){
	int done;
	switch(direction) {
		case 3 :
			done = slideLeft(board);
			break;
		case 2 :
			done = slideDown(board);
			break;
		case 1 :
			done = slideUp(board);
			break;
		case 0 :
			done = slideRight(board);
			break;
		default :
			done = 0;
	}
	return done;
}

int slideLeft(int board[SIZE][SIZE])
{
	int done;
	rotate(board);
	done = slideUp(board);
	rotate(board);
	rotate(board);
	rotate(board);
	return done;
}

int slideRight(int board[SIZE][SIZE])
{
	int done;
	rotate(board);
	rotate(board);
	rotate(board);
	done = slideUp(board);
	rotate(board);
	return done;
}

int slideUp(int board[SIZE][SIZE])
{
	int done = 0;
	int x;
	for (x = 0; x < SIZE; x++) {
		done |= push(board[x]);
	}
	return done;
}

int slideDown(int board[SIZE][SIZE])
{
	int done;
	rotate(board);
	rotate(board);
	done = slideUp(board);
	rotate(board);
	rotate(board);
	return done;
}

int push(int array[SIZE])
{
	int x, n, stop = 0, done = 0;
	for (x = 1; x < SIZE; x++) {
		if (array[x] != 0) {	// make sure there is something in box
			done = 2;	// don't exit loop until we determine if there is a move
			for (n = x - 1; done == 2; n--) {
				if (array[n] != 0) {	// if box has number in it
					if (array[n] == array[x]) {	// same so merge
						array[n]++;	// increase by power of two
						stop = n + 1;	// avoid double merge such as 2 2 0 4
						array[x] = 0;
						done = 1;
					} else {	// not the same so move just below
						if (n + 1 != x) {
							array[n + 1] = array[x];
							array[x] = 0;
							done = 1;
						} else {
							done = 0;	// we can't slide any further
						}
					}
				} else {
					if (n == stop) {
						array[stop] = array[x];
						array[x] = 0;
						done = 1;
					}
				}
			}
		}
	}
	return done;
}

void rotate(int board[SIZE][SIZE])
{
	int x, y, n = SIZE, temp;
	for (x = 0; x < n / 2; x++) {
		for (y = x; y < n - x - 1; y++) {
			temp = board[x][y];
			board[x][y] = board[y][n - x - 1];
			board[y][n - x - 1] = board[n - x - 1][n - y - 1];
			board[n - x - 1][n - y - 1] = board[n - y - 1][x];
			board[n - y - 1][x] = temp;
		}
	}
}

int gameover(int board[SIZE][SIZE])
{
	int x, y;
	int nofree = 1;
	for (x = 0; x < SIZE - 1; x++) {
		for (y = 0; y < SIZE - 1; y++) {
			if (board[x][y] == 0) {
				nofree = 0;	// means we have a free square
			}
		}
	}

	if (nofree) {
		// Check if there is any moves horizontaly or vertically;
		for (x = 0; x < SIZE - 1; x++) {
			for (y = 0; y < SIZE - 1; y++) {
				if ((board[x][y] == board[x][y + 1]) ||
				    (board[x][y] == board[x + 1][y])) {
					return 0;
				}
			}
		}
	}
	return nofree;
}

void add2(int board[SIZE][SIZE])
{
	int x, y, r, pos = 0, free[SIZE * SIZE][2];
	for (x = 0; x < SIZE; x++) {
		for (y = 0; y < SIZE; y++) {
			if (board[x][y] == 0) {
				free[pos][0] = x;
				free[pos][1] = y;
				pos++;
			}
		}
	}
	if (free > 0) {
		r = rand() % pos;
		board[free[r][0]][free[r][1]] = 1;
	}
}