summaryrefslogtreecommitdiff
path: root/2ls10.c
blob: c3b0d70417a280d6d07f139ce406a388607f1046 (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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <termios.h>
#include <unistd.h>

int st[1<<7], lo, li, ls;
int m, pt, n, pl, t;
char nf;
struct termios t1, t2;

int cond (int j){
	return t? j/n : j%n;
}

void clean(char *str){
	if (str) printf("%s\n", str);
	tcsetattr(0, TCSANOW, &t1);
	exit(0);	
}

int dash(int i){
	return i?
		putchar('-') + dash(i-1):0;
		}
		
int line(int i){
	return putchar('+') + (i ?
		dash(6)+ line(i-1):
		putchar(10));	
}

int valid_move(){
	int i;
	for (i=0; i<n*n - 1; i++){
		if ((i%n < n-1 && st[i]==st[i+1]) || (i/n < n-1 && st[i] == st[i+n]))
			return 1;
	}
	return 0;
}

void print_state(){
	int i, j,v;
	printf("\033[2J\033[1;1HScore: %d\n", pt);
	line(n);
	for (i=0; i<n; i++){
		printf("|");
		for(j=0; j<n; j++){
			v=st[n*i+j];
			if (v > 0)
				printf("%5d |", 1<<v);
			else printf("      |");
		}
		printf("\n");
	}
	line(n);
}

void set_rnd(){
	int i;
	while(nf){
		i = rand();
		if (!st[i%(n*n)]){
			st[i%(n*n)] = 1 + (i%8==0);
			if (! --nf) break;
			return;
		}
	}
	if (!valid_move()){
		print_state();	
		clean("no more moves");
	}
}


void shift(int j){
	int num_empty=0;
	do{
		if (! st[j]) /* empty*/
			num_empty -= li;
		else  if (num_empty){
			st[j + num_empty] = st[j];
			st[j] ^= st[j];
		}
		j += li;
	} while (j >= 0 && cond(j) != ls);
}

void merge(int j){
	do {
		if (st[j] && st[j] == st[j-li]){
			st[j-li] += 1;
			if (st[j-li] == 11) pl ^= pl;
			pt += 1<<st[j-li];
			st[j] ^= st[j];
			nf ++;
		}
		j += li;
	} while( j>= 0 && cond(j) != ls);
}

void update(){
	int i;
	for(i =0; i<n; i++){
		shift(m * i + lo);
		merge(m * i + lo + li);
		shift(m * i + lo);	
	}
}

int main(int argc, char *argv[]){
 	srand(time(NULL));	
 	//srand(12345);
	n = 4;
	if (argc > 1) n = atoi(argv[1]);
	nf = n * n;	
	tcgetattr(0, &t1);
	t2 = t1;
	t2.c_lflag &= ~(ICANON | ECHO);
	tcsetattr(0, TCSANOW, &t2);
	set_rnd();
	pt = 0;
	pl = 1;
	while(pl){
		//pr_st();
		set_rnd();
		print_state();
		switch(getchar()){
			case 'l':
				lo = ls = n-1; 
				li = -1;
				m = n;
				t = 0;
				break;
			case 'h':
				lo = ls = 0;
				li = 1;
				m = n;
				t = 0;
				break;
			case 'j':
				lo = n * (n-1);
				li = -n;
				ls = -1;
				m = 1;
				t=1;
				break;
			case 'k':
				lo = 0;
				li = ls = n;
				m = 1;
				t=1;
				break;
			case 'q':
				clean("Thanks!");

		}
		update();

	}
	print_state();
	clean("you won!");
}