summaryrefslogtreecommitdiff
path: root/src/2048.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2048.cpp')
-rw-r--r--src/2048.cpp178
1 files changed, 178 insertions, 0 deletions
diff --git a/src/2048.cpp b/src/2048.cpp
new file mode 100644
index 0000000..13afd1f
--- /dev/null
+++ b/src/2048.cpp
@@ -0,0 +1,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;
+ }
+}