Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 2048 Moving the Tiles
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Rift




PostPosted: Wed May 28, 2014 1:58 pm   Post subject: 2048 Moving the Tiles

What is it you are trying to achieve?
Make a Turing version of 2048

What is the problem you are having?
I'm not sure how to approach the tile movements. I'm not sure how to make it work just like 2048 and the only way I can think of doing this is through writing the thousands of lines of code for every scenario possible.

I'm basing this game off of http://gabrielecirulli.github.io/2048/

Since you need to account for the tile merges and movements, I can't think of any way to do this without writing a metric ton of code. Could anyone shed some light on this issue?

Describe what you have tried to solve this problem
I have no idea how to approach the issue. I'm a complete amateur with Turing and the only way I can think of doing this is writing thousands of lines of code just for every single possible scenario.+


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Here's what I have so far but it's horribly inefficient


Please specify what version of Turing you are using
4.1



2048.zip
 Description:
Just what I have so far, probably very inefficient

Download
 Filename:  2048.zip
 Filesize:  146.29 KB
 Downloaded:  384 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Nathan4102




PostPosted: Wed May 28, 2014 4:27 pm   Post subject: RE:2048 Moving the Tiles

I've actually just finished a Java version of 2048, if I were you I'd tackle the actual number mechanics first, and then once you have that working, you can make your animations look smooth.

Now instead of writing thousands of lines for every possible scenario, this is how I did it: Take in the user's key input, and based on which arrow key is pressed, direct the user to a procedure that processes this. To start off, use one procedure for each arrow key, but doing it in one compact procedure is doable.

In these procedures you're going to need to check each number, and check:
1. Can this number move x direction?
2. Can this number merge with another number in x direction?

Say we're working on the moveRight procedure. I iterated from the right row of numbers to the left row of numbers and did checks for if there is either a blank in the spot to the right and if there is a matching number in the spot to the right. Because numbers can move a maximum of 3 times, repeat this step 3 times with a loop.

Extra checks need to be performed to assure one tile can't merge more than once in one move like in the actual game, but this is how I tackled the problem. Animation is a whole different thing, but its not my forte so I won't attempt to help, and you shouldn't worry about it until your mechanics are working.

Note: I would store your tiles in a 2d int array, I can't check what code you have right now but working with a 2d array would be easiest.
Rift




PostPosted: Sat Jun 07, 2014 11:33 pm   Post subject: Re: RE:2048 Moving the Tiles

Nathan4102 @ Wed May 28, 2014 4:27 pm wrote:
I've actually just finished a Java version of 2048, if I were you I'd tackle the actual number mechanics first, and then once you have that working, you can make your animations look smooth.

Now instead of writing thousands of lines for every possible scenario, this is how I did it: Take in the user's key input, and based on which arrow key is pressed, direct the user to a procedure that processes this. To start off, use one procedure for each arrow key, but doing it in one compact procedure is doable.

In these procedures you're going to need to check each number, and check:
1. Can this number move x direction?
2. Can this number merge with another number in x direction?

Say we're working on the moveRight procedure. I iterated from the right row of numbers to the left row of numbers and did checks for if there is either a blank in the spot to the right and if there is a matching number in the spot to the right. Because numbers can move a maximum of 3 times, repeat this step 3 times with a loop.

Extra checks need to be performed to assure one tile can't merge more than once in one move like in the actual game, but this is how I tackled the problem. Animation is a whole different thing, but its not my forte so I won't attempt to help, and you shouldn't worry about it until your mechanics are working.

Note: I would store your tiles in a 2d int array, I can't check what code you have right now but working with a 2d array would be easiest.

I'm trying to fix up the number mechanics but I'm finding myself constantly stuck on adding a single new block to my 2D array after every user input. For whatever reason it fills up the entire screen with new blocks every time and I end up with the error "stack overflow", I understand it's an issue with one of my loops but I have no idea how I might go about fixing this

I've attached what I have right now (started from scratch for the 2nd time), hope you guys can maybe shed some light on my problem?

EDIT:
Okay so I figured out that the issue is just in adding a new block to the 2D array, can anyone help me figure out how to set it up so that it only adds one block to the 2d array after each key input?


code:

var boxes : array 1 .. 4, 1 .. 4 of int
var availableboxes : array 1 .. 4, 1 .. 4 of boolean
var a, b : int
var chars : array char of boolean
forward proc definearray
forward proc addnewblock
forward proc printarray
forward proc movetiles
forward proc arrowup
forward proc arrowright
forward proc arrowleft
forward proc arrowdown
forward proc newscreen

body proc definearray
    for k : 1 .. 4
        for d : 1 .. 4
            boxes (k, d) := 0
            availableboxes (k, d) := true
        end for
    end for
end definearray

body proc addnewblock
    randint (a, 1, 4)
    randint (b, 1, 4)
    if availableboxes (a, b) = true and boxes (a, b) = 0 then
        boxes (a, b) := 2
        availableboxes (a, b) := false
    else
    end if
end addnewblock
body proc printarray
    for k : 1 .. 4
        for d : 1 .. 4
            locate (2 * k, d)
            put boxes (k, d)
        end for
    end for
end printarray

definearray
newscreen
body proc movetiles
    loop
        Input.KeyDown (chars)
        if chars (KEY_UP_ARROW) then
            arrowup
            View.Update
        end if
        if chars (KEY_RIGHT_ARROW) then
            arrowright
            View.Update
        end if
        if chars (KEY_LEFT_ARROW) then
            arrowleft
            View.Update
        end if
        if chars (KEY_DOWN_ARROW) then
            arrowdown
            View.Update
        end if
    end loop
end movetiles
body proc arrowup
    newscreen
end arrowup
body proc arrowright
    newscreen
end arrowright
body proc arrowleft
    newscreen
end arrowleft
body proc arrowdown
    newscreen
end arrowdown
body proc newscreen
    addnewblock
    printarray
    movetiles
    View.Update
end newscreen



2048.zip
 Description:

Download
 Filename:  2048.zip
 Filesize:  32.43 KB
 Downloaded:  188 Time(s)

Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: