I've been struggling with this issue for the past few days. I'm pretty new to javascript and jQuery and I'm a little bit confused on where to go.
Basically, I made the game, Snake, following a tutorial; on desktops, I'm using the keydown inputs for up, down, left, and right. I am trying to make it so the game is also user and touch friendly on jQuery mobile. I've downloaded a few touch plugins, but I am not sure where to go from there or if it even works.
As part of my game.js file,
$(document).keydown(function(e){
var key = e.which;
if(key == "37" && d!= "right") d= "left";
else if (key =="38" && d!= "down" ) d= "up";
else if (key == "39" && d!="left") d= "right";
else if (key == "40" && d!="up" ) d= "down";
The problem I'm having now is: how do I make implement the touch inputs so that it also is able to refer back to the keydown inputs? Would I include it in my index.html as inline? Part of game.js file? Or a new file?
Thanks! I'd appreciate any help/guidance!
Might be a bit of a hack solution, but it'll certainly work.
1) Move the keydown function out of an anonymous block into a named function that takes and event object as a parameter.
2) make you overlay controls call functions that override the default event object to assign e.which to whatever value the control corresponds to.
So...
This:
$(document).keydown(function(e){
var key = e.which;
if(key == "37" && d!= "right") d= "left";
else if (key =="38" && d!= "down" ) d= "up";
else if (key == "39" && d!="left") d= "right";
else if (key == "40" && d!="up" ) d= "down";
Becomes
function handle_keys(e){
var key = e.which;
if(key == "37" && d!= "right") d= "left";
else if (key =="38" && d!= "down" ) d= "up";
else if (key == "39" && d!="left") d= "right";
else if (key == "40" && d!="up" ) d= "down";
}
$(document).keydown(function(e){
handle_keys(e);
}
And for your touch events you would just do
$("#left_button").touchstart(function(e){
e.which = 37;
handle_keys(e);
})
Related
Is there a built-in data type in Dafny like List in Java (or any type for dynamic list)?
I've looked for it in Dafny Reference Manual, but nothing found.
It seems that a self-defined class must be defined for it.
If it is the fact, then how can the performance be assured for the generated java program and how can the gernerality of Dafny as a programming language be assured?
Not criticism, just curious.
Dafny's first collection is undoubtedly seq, which is an immutable list.
function sum(s: seq<int>): int {
if |s| == 0 then 0 else s[0] + sum(s[1..])
}
For anything else, the Dafny team is working on a standard library, but you might be interestest by the first example given in the Dafny documentation that also explain why lists are non trivial objects to verify:
https://dafny.org/dafny/DafnyRef/DafnyRef#sec-example
In short, to define a list, you want to write a class and store a ghost model of all the elements to ensure there is no cycle, and possibly write this node into another data structure. But the proofs are not obvious. Here is what I got so far.
class ListNode<T> {
var head: T
var tail: ListNode?<T>
ghost var Repr: seq<ListNode<T>>
constructor(h: T, t: ListNode?<T>) requires t != null ==> t.Valid() ensures Valid()
{
head:= h;
tail := t;
Repr := [this] + (if t == null then [] else t.Repr);
}
predicate Valid() reads this, Repr decreases |Repr|
{
&& |Repr| > 0
&& Repr[0] == this
&& (if tail == null then |Repr| == 1 else
&& |Repr| > 1
&& tail == Repr[1]
&& tail.Repr == Repr[1..]
&& tail.Valid())
}
lemma ReprAreDecreasing(i: int)
requires Valid()
requires 0 <= i < |Repr|
ensures Repr[i].Repr == Repr[i..]
ensures Repr[i].Valid()
{
if i == 0 {
} else {
tail.ReprAreDecreasing(i-1);
}
}
}
class List<T> {
var head: ListNode?<T>
var last: ListNode?<T>
ghost var Repr: seq<ListNode<T>>
constructor() ensures Valid() {
head := null;
last := null;
Repr := [];
}
lemma ValidImpliesAllNodesValid(n: ListNode<T>)
requires Valid()
requires n in Repr
ensures n.Valid() {
if n == head {
assert n.Valid();
} else {
var i :| 0 <= i < |Repr| && Repr[i] == n;
head.ReprAreDecreasing(i);
}
}
method Append(node: ListNode<T>)
{
...
}
predicate Valid() reads this, Repr
{
(if head != null then
&& last != null
&& head in Repr
&& head.Repr == Repr
&& head.Valid()
&& last == head.Repr[|head.Repr|-1]
&& assert last.Repr == head.Repr[|head.Repr|-1..] by {
head.ReprAreDecreasing(|head.Repr|-1);
} last.Valid()
else
&& last == null
&& |Repr| == 0)
}
}
References:
https://dafny.org/dafny/DafnyRef/DafnyRef#sec-seq-comprehension
I have been trying to implement the shunting yard algorithm, but the output of my parser is incorrect.
let mut stack: Vec<String> = vec![];
let mut op_stack: Vec<String> = vec![];
for current in sub_tree {
if current.tok_type == TokenType::NUMBER || current.tok_type == TokenType::NEGNUMBER {
self.parse();
stack.push(current.content.clone());
}
if current.tok_type == TokenType::SUBBIN
|| current.tok_type == TokenType::PLUSBIN
|| current.tok_type == TokenType::DIVBIN
|| current.tok_type == TokenType::MULBIN
{
while op_stack.len() > 0 && op_stack.last().unwrap().to_string() != "(" {
if op_prec(&op_stack.last().unwrap().to_string()) > op_prec(¤t.content)
|| (op_prec(&op_stack.last().unwrap().to_string()) == op_prec(¤t.content)
&& op_asso(¤t.content) == "left")
{
stack.push(op_stack.pop().unwrap().to_string());
} else {
break;
}
}
op_stack.push(current.content.to_string())
}
}
The original equation I am parsing: 1 + 2 * 3
I expected the following output: 1 2 3 * +
Instead I get this: 1 2 3 + *
I think I am going wrong somewhere in my while loop but I don't really know. I tried to follow the example on the Wikipedia article.
I forgot I had to pop from the operator stack back into the output stack at the end.
Comparing your code
if current.tok_type == TokenType::SUBBIN
|| current.tok_type == TokenType::PLUSBIN
|| current.tok_type == TokenType::DIVBIN
|| current.tok_type == TokenType::MULBIN
{
while op_stack.len() > 0 && op_stack.last().unwrap().to_string() != "(" {
if op_prec(&op_stack.last().unwrap().to_string()) > op_prec(¤t.content)
|| (op_prec(&op_stack.last().unwrap().to_string()) == op_prec(¤t.content)
&& op_asso(¤t.content) == "left")
{
stack.push(op_stack.pop().unwrap().to_string());
} else {
break;
}
}
op_stack.push(current.content.to_string())
}
with the Wikipedia code https://en.wikipedia.org/wiki/Shunting-yard_algorithm
- an operator o1:
while (
there is an operator o2 other than the left parenthesis at the top
of the operator stack, and (o2 has greater precedence than o1
or they have the same precedence and o1 is left-associative)
):
pop o2 from the operator stack into the output queue
push o1 onto the operator stack
It looks like they are functionally identical.
So I suspect the problem is not with the code, but instead with the precedence table. If you have the precedence of + and * the wrong way round, then you would get this behaviour. It is easy to get this mixed up as some source have precedence going from tighter binding to loser one and some have the opposite. Compare Wikipedia order of operations and Operator Precedence in Java, use the former.
I have a code that puts a timestamp in when column 1 is edited and then a second timestamp when another column has the word 'collected' input to it. From this I then work out time it took to be collected and use that data. However if someone edits column 1 again it updates the timestamp so I'm looking for a script to allow it to only do it on first edit. Here is my current script;
function onEdit(e) {
var sheet = SpreadsheetApp.getActiveSheet();
if( sheet.getName() == "CC sheet" ) {
//Update timestamp when changed to collected
var range = sheet.getActiveCell();
Logger.log(range.getColumn())
if( range.getColumn() == 7.0 ) { //if we are in the status column...
var nextCell = range.offset(0, 2);
if(range.getValue() == 'Collected')
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
}
//end
}
if(e.source.getActiveSheet().getName() !== 'CC sheet' || e.range.columnStart !== 1) return;
e.range.offset(0, 5).setValue(e.value ? new Date() : null);
}
It is the bottom bit I need to change to only update on first edit. Any help with this would be much appreciated.
replace last row or code with this one:
if (e.range.offset(0, 5).getValue() === '') {
e.range.offset(0, 5).setValue(e.value ? new Date() : null);
}
it's the same 'is empty?' check, as you have in the first part of script.
I need to create a single event rule for the below anchor tags. I have 2 classes to add or i can use id.
<a id="Message1" class="read">test1</a>
<a id="Message2" class="unread">test2</a>
In my event rule i have added a as selector and then used custom code like below to capture id or class. Both dint work. What is the issue here?
if ( $(this).attr('id').indexof("Message") > 0 ) {return true;} else {return false;}
if ( $(this).attr('class') == "read" || $(this).attr('class') == "unread" ) {return true;} else {return false;}
I'm trying to adapt my inEdit script to give not just a timestamp but also add a "2" in another field,
I thought the following would work but only the date works and it ignores my "2".
What am I missing for both the date and 2 to appear onEdit?
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sanshiro" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 6 ) { //checks the column
var nextCell = r.offset(0, -5);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue(new Date());
nextCell = r.offset(0, 10);
if( nextCell.getValue() === '' ) //is empty?
nextCell.setValue("2");
} }
I don't know if the check if the 'nextcell' is empty is really needed ? you still want to update that cell if a new edit takes place, right ? Anyway, I think you can simplify your script to:
function onEdit(e) {
if (e.source.getActiveSheet()
.getName() !== "Sanshiro" || e.range.columnStart !== 6) return //checks that we're on the correct sheet
e.range.offset(0, -5)
.setValue(new Date());
e.range.offset(0, 10)
.setValue(2);
}