Skip to content

Commit cfddc7c

Browse files
committed
done
1 parent 36706db commit cfddc7c

23 files changed

+1134
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
function KeyboardInputManager() {
2+
this.events = {};
3+
4+
if (window.navigator.msPointerEnabled) {
5+
//Internet Explorer 10 style
6+
this.eventTouchstart = "MSPointerDown";
7+
this.eventTouchmove = "MSPointerMove";
8+
this.eventTouchend = "MSPointerUp";
9+
} else {
10+
this.eventTouchstart = "touchstart";
11+
this.eventTouchmove = "touchmove";
12+
this.eventTouchend = "touchend";
13+
}
14+
15+
this.listen();
16+
}
17+
18+
KeyboardInputManager.prototype.on = function (event, callback) {
19+
if (!this.events[event]) {
20+
this.events[event] = [];
21+
}
22+
this.events[event].push(callback);
23+
};
24+
25+
KeyboardInputManager.prototype.emit = function (event, data) {
26+
var callbacks = this.events[event];
27+
if (callbacks) {
28+
callbacks.forEach(function (callback) {
29+
callback(data);
30+
});
31+
}
32+
};
33+
34+
KeyboardInputManager.prototype.listen = function () {
35+
var self = this;
36+
37+
var map = {
38+
38: 0, // Up
39+
39: 1, // Right
40+
40: 2, // Down
41+
37: 3, // Left
42+
75: 0, // Vim up
43+
76: 1, // Vim right
44+
74: 2, // Vim down
45+
72: 3, // Vim left
46+
87: 0, // W
47+
68: 1, // D
48+
83: 2, // S
49+
65: 3 // A
50+
};
51+
52+
// Respond to direction keys
53+
document.addEventListener("keydown", function (event) {
54+
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
55+
event.shiftKey;
56+
var mapped = map[event.which];
57+
58+
// Ignore the event if it's happening in a text field
59+
if (self.targetIsInput(event)) return;
60+
61+
if (!modifiers) {
62+
if (mapped !== undefined) {
63+
event.preventDefault();
64+
self.emit("move", mapped);
65+
}
66+
}
67+
68+
// R key restarts the game
69+
if (!modifiers && event.which === 82) {
70+
self.restart.call(self, event);
71+
}
72+
});
73+
74+
// Respond to button presses
75+
this.bindButtonPress(".retry-button", this.restart);
76+
this.bindButtonPress(".restart-button", this.restart);
77+
this.bindButtonPress(".keep-playing-button", this.keepPlaying);
78+
79+
// Respond to swipe events
80+
var touchStartClientX, touchStartClientY;
81+
var gameContainer = document.getElementsByClassName("game-container")[0];
82+
83+
gameContainer.addEventListener(this.eventTouchstart, function (event) {
84+
if ((!window.navigator.msPointerEnabled && event.touches.length > 1) ||
85+
event.targetTouches > 1 ||
86+
self.targetIsInput(event)) {
87+
return; // Ignore if touching with more than 1 finger or touching input
88+
}
89+
90+
if (window.navigator.msPointerEnabled) {
91+
touchStartClientX = event.pageX;
92+
touchStartClientY = event.pageY;
93+
} else {
94+
touchStartClientX = event.touches[0].clientX;
95+
touchStartClientY = event.touches[0].clientY;
96+
}
97+
98+
event.preventDefault();
99+
});
100+
101+
gameContainer.addEventListener(this.eventTouchmove, function (event) {
102+
event.preventDefault();
103+
});
104+
105+
gameContainer.addEventListener(this.eventTouchend, function (event) {
106+
if ((!window.navigator.msPointerEnabled && event.touches.length > 0) ||
107+
event.targetTouches > 0 ||
108+
self.targetIsInput(event)) {
109+
return; // Ignore if still touching with one or more fingers or input
110+
}
111+
112+
var touchEndClientX, touchEndClientY;
113+
114+
if (window.navigator.msPointerEnabled) {
115+
touchEndClientX = event.pageX;
116+
touchEndClientY = event.pageY;
117+
} else {
118+
touchEndClientX = event.changedTouches[0].clientX;
119+
touchEndClientY = event.changedTouches[0].clientY;
120+
}
121+
122+
var dx = touchEndClientX - touchStartClientX;
123+
var absDx = Math.abs(dx);
124+
125+
var dy = touchEndClientY - touchStartClientY;
126+
var absDy = Math.abs(dy);
127+
128+
if (Math.max(absDx, absDy) > 10) {
129+
// (right : left) : (down : up)
130+
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));
131+
}
132+
});
133+
};
134+
135+
KeyboardInputManager.prototype.restart = function (event) {
136+
event.preventDefault();
137+
this.emit("restart");
138+
};
139+
140+
KeyboardInputManager.prototype.keepPlaying = function (event) {
141+
event.preventDefault();
142+
this.emit("keepPlaying");
143+
};
144+
145+
KeyboardInputManager.prototype.bindButtonPress = function (selector, fn) {
146+
var button = document.querySelector(selector);
147+
button.addEventListener("click", fn.bind(this));
148+
button.addEventListener(this.eventTouchend, fn.bind(this));
149+
};
150+
151+
KeyboardInputManager.prototype.targetIsInput = function (event) {
152+
return event.target.tagName.toLowerCase() === "input";
153+
};

2048/js/keyboard_input_manager.js

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
function KeyboardInputManager() {
2+
this.events = {};
3+
4+
if (window.navigator.msPointerEnabled) {
5+
//Internet Explorer 10 style
6+
this.eventTouchstart = "MSPointerDown";
7+
this.eventTouchmove = "MSPointerMove";
8+
this.eventTouchend = "MSPointerUp";
9+
} else {
10+
this.eventTouchstart = "touchstart";
11+
this.eventTouchmove = "touchmove";
12+
this.eventTouchend = "touchend";
13+
}
14+
15+
this.listen();
16+
}
17+
18+
KeyboardInputManager.prototype.on = function (event, callback) {
19+
if (!this.events[event]) {
20+
this.events[event] = [];
21+
}
22+
this.events[event].push(callback);
23+
};
24+
25+
KeyboardInputManager.prototype.emit = function (event, data) {
26+
var callbacks = this.events[event];
27+
if (callbacks) {
28+
callbacks.forEach(function (callback) {
29+
callback(data);
30+
});
31+
}
32+
};
33+
34+
KeyboardInputManager.prototype.listen = function () {
35+
var self = this;
36+
37+
var map = {
38+
38: 0, // Up
39+
39: 1, // Right
40+
40: 2, // Down
41+
37: 3, // Left
42+
75: 0, // Vim up
43+
76: 1, // Vim right
44+
74: 2, // Vim down
45+
72: 3, // Vim left
46+
87: 0, // W
47+
68: 1, // D
48+
83: 2, // S
49+
65: 3 // A
50+
};
51+
52+
// Respond to direction keys
53+
document.addEventListener("keydown", function (event) {
54+
var modifiers = event.altKey || event.ctrlKey || event.metaKey ||
55+
event.shiftKey;
56+
var mapped = map[event.which];
57+
58+
// Ignore the event if it's happening in a text field
59+
if (self.targetIsInput(event)) return;
60+
61+
if (!modifiers) {
62+
if (mapped !== undefined) {
63+
event.preventDefault();
64+
self.emit("move", mapped);
65+
}
66+
}
67+
68+
// R key restarts the game
69+
if (!modifiers && event.which === 82) {
70+
self.restart.call(self, event);
71+
}
72+
});
73+
74+
// Respond to button presses
75+
this.bindButtonPress(".retry-button", this.restart);
76+
this.bindButtonPress(".restart-button", this.restart);
77+
this.bindButtonPress(".keep-playing-button", this.keepPlaying);
78+
79+
// Respond to swipe events
80+
var touchStartClientX, touchStartClientY;
81+
var gameContainer = document.getElementsByClassName("game-container")[0];
82+
83+
gameContainer.addEventListener(this.eventTouchstart, function (event) {
84+
if ((!window.navigator.msPointerEnabled && event.touches.length > 1) ||
85+
event.targetTouches > 1 ||
86+
self.targetIsInput(event)) {
87+
return; // Ignore if touching with more than 1 finger or touching input
88+
}
89+
90+
if (window.navigator.msPointerEnabled) {
91+
touchStartClientX = event.pageX;
92+
touchStartClientY = event.pageY;
93+
} else {
94+
touchStartClientX = event.touches[0].clientX;
95+
touchStartClientY = event.touches[0].clientY;
96+
}
97+
98+
event.preventDefault();
99+
});
100+
101+
gameContainer.addEventListener(this.eventTouchmove, function (event) {
102+
event.preventDefault();
103+
});
104+
105+
gameContainer.addEventListener(this.eventTouchend, function (event) {
106+
if ((!window.navigator.msPointerEnabled && event.touches.length > 0) ||
107+
event.targetTouches > 0 ||
108+
self.targetIsInput(event)) {
109+
return; // Ignore if still touching with one or more fingers or input
110+
}
111+
112+
var touchEndClientX, touchEndClientY;
113+
114+
if (window.navigator.msPointerEnabled) {
115+
touchEndClientX = event.pageX;
116+
touchEndClientY = event.pageY;
117+
} else {
118+
touchEndClientX = event.changedTouches[0].clientX;
119+
touchEndClientY = event.changedTouches[0].clientY;
120+
}
121+
122+
var dx = touchEndClientX - touchStartClientX;
123+
var absDx = Math.abs(dx);
124+
125+
var dy = touchEndClientY - touchStartClientY;
126+
var absDy = Math.abs(dy);
127+
128+
if (Math.max(absDx, absDy) > 10) {
129+
// (right : left) : (down : up)
130+
self.emit("move", absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0));
131+
}
132+
});
133+
};
134+
135+
KeyboardInputManager.prototype.restart = function (event) {
136+
event.preventDefault();
137+
this.emit("restart");
138+
};
139+
140+
KeyboardInputManager.prototype.keepPlaying = function (event) {
141+
event.preventDefault();
142+
this.emit("keepPlaying");
143+
};
144+
145+
KeyboardInputManager.prototype.bindButtonPress = function (selector, fn) {
146+
var button = document.querySelector(selector);
147+
button.addEventListener("click", fn.bind(this));
148+
button.addEventListener(this.eventTouchend, fn.bind(this));
149+
};
150+
151+
KeyboardInputManager.prototype.targetIsInput = function (event) {
152+
return event.target.tagName.toLowerCase() === "input";
153+
};

2048/main.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from numpy import random, array
2+
3+
a = array([[None for i in range(4)] for i in range(4)])
4+
5+
def addblock():
6+
col = random.randint(4)
7+
row = random.randint(4)
8+
if not a[row, col]:
9+
a[row, col] = 2
10+
else:
11+
addblock()
12+
13+
14+
def move(way, collapse=0):
15+
change = False
16+
if way in ['down', 'right']:
17+
rows = list(reversed(range(1,4)))
18+
if way in ['up', 'left']:
19+
rows = range(3)
20+
21+
for col in range(4):
22+
for row in rows:
23+
if way == 'down':
24+
curr = (row, col)
25+
prev = (row-1, col)
26+
if way == 'up':
27+
curr = (row, col)
28+
prev = (row+1, col)
29+
if way == 'left':
30+
curr = (col, row)
31+
prev = (col, row+1)
32+
if way == 'right':
33+
curr = (col, row)
34+
prev = (col, row-1)
35+
if a[prev]:
36+
if not a[curr]:
37+
a[curr] = a[prev]
38+
a[prev] = None
39+
change = True
40+
if a[prev] == a[curr]:
41+
a[curr] *= 2
42+
a[prev] = None
43+
collapse += 1
44+
#if collapse < 2:
45+
# change = True
46+
#else:
47+
change = False
48+
if change:
49+
move(way, collapse)
50+
else:
51+
addblock()
52+
53+
54+
addblock()
55+
addblock()
56+
57+
for i in range(4):
58+
a[0,i] = 2

2048/main.pyc

1.46 KB
Binary file not shown.

2048/main_1.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from numpy import random
2+
a = [[None for i in range(4)] for i in range(4)]
3+
4+
5+
def addblock():
6+
col = random.randint(4)
7+
row = random.randint(4)
8+
if not a[row][col]:
9+
a[row][col] = 2
10+
else:
11+
addblock()
12+
13+
addblock()
14+
addblock()
15+

0 commit comments

Comments
 (0)