This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#76 2012-05-04 11:01:21

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

blob8108 wrote:

Hardmath123 wrote:

Ok, another question: due to absolutely un-thought-out CSS whitespace compatibility in textareas, you have a choice of being able to indent your code, or having code scroll horizontally rather than wrap if the line is too long. Which would you prefer? Should you be able to expand the coding window to the whole screen (it currently resides in a sidebar)?

Horizontal scrolling is a pain. Expanding it might be useful; but I imagine one would mainly use a real text editor, to be honest. Indentation is much more important; go with that.  smile

...what precisely was the problem with CSS/textareas?

Textareas do not support whiteSpace:pre. They only support no-wrap (scroll but don't indent) and pre-wrap (indent but don't scroll). I will go for indentation, as you suggested, since I don't expect you to code there, just tweak numbers.  smile

"pre-wrap" is precisely what I was going to suggest. Go with that  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#77 2012-05-04 12:18:56

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

blob8108 wrote:

Hardmath123 wrote:

blob8108 wrote:

Horizontal scrolling is a pain. Expanding it might be useful; but I imagine one would mainly use a real text editor, to be honest. Indentation is much more important; go with that.  smile

...what precisely was the problem with CSS/textareas?

Textareas do not support whiteSpace:pre. They only support no-wrap (scroll but don't indent) and pre-wrap (indent but don't scroll). I will go for indentation, as you suggested, since I don't expect you to code there, just tweak numbers.  smile

"pre-wrap" is precisely what I was going to suggest. Go with that  smile

Great, thanks!  smile  Should I provide stuff like who won or what moves you can make, or should that be left to the competitors?

Pros:
Less work for you

Cons:
More work for me  tongue
Not custom-fit for you
Might be slower, you might have better algorithms

Last edited by Hardmath123 (2012-05-04 12:19:53)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#78 2012-05-04 14:34:40

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

Should I provide stuff like who won or what moves you can make, or should that be left to the competitors?

Who won: absolutely. It should handle all the game-mechanic-y stuff, like endgames — so all you have to do is write AI code to decide where to go. I presume you've already done all the stuff like computer vs computer, UI so you can do player vs computer, and everything...  smile

Code that determines what moves you can make should be part of the AI code, I think; and seems quite trivial to me  tongue  (doesn't it just look for empty tiles?)

As an aside (and I think we mentioned this): you should definitely copy the board before passing it to the AI code, and get the function to only return the [x, y] co-ordinates of where it wants to move — otherwise the AI could potentially "cheat" by moving/deleting opposing pieces  big_smile  So best check in your code to make sure the move is allowed (ie. doesn't overlap an existing piece), I think.

However, you might like to provide some example code for things like listing possible moves. Maybe include a really really dumb basic AI that does something like place its pieces in a random non-empty square, to demonstrate how it all works?  smile

Last edited by blob8108 (2012-05-04 14:38:18)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#79 2012-05-04 16:20:32

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

I'm actually going to advocate for more competitor-written code for precisely the reasons you put forth.  If the competition is about making AI, the various functions that go along with it are of great importance.  If a team can make more efficient algorithms than another, that should grant them an advantage.  If you provide the movement function and evaluation function, that leaves very little for the teams to make  tongue


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#80 2012-05-04 19:58:12

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: AI Strategy Tournament: Rescheduled to June 1st

I agree with MoreGamesNow about each team implementing its own algorithms, including "possible moves," etc.  But I also support blob8108's suggestion that Hardmath123 should make sure no one's AI cheats.

Offline

 

#81 2012-05-05 00:07:39

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

blob8108 wrote:

Hardmath123 wrote:

Should I provide stuff like who won or what moves you can make, or should that be left to the competitors?

Who won: absolutely. It should handle all the game-mechanic-y stuff, like endgames — so all you have to do is write AI code to decide where to go. I presume you've already done all the stuff like computer vs computer, UI so you can do player vs computer, and everything...  smile

Code that determines what moves you can make should be part of the AI code, I think; and seems quite trivial to me  tongue  (doesn't it just look for empty tiles?)

As an aside (and I think we mentioned this): you should definitely copy the board before passing it to the AI code, and get the function to only return the [x, y] co-ordinates of where it wants to move — otherwise the AI could potentially "cheat" by moving/deleting opposing pieces  big_smile  So best check in your code to make sure the move is allowed (ie. doesn't overlap an existing piece), I think.

However, you might like to provide some example code for things like listing possible moves. Maybe include a really really dumb basic AI that does something like place its pieces in a random non-empty square, to demonstrate how it all works?  smile

So I think the consensus here is that I should write in a whoWon(board) function and an isMoveValid (board, row, col), which is private to my back-end; your AI's have to re-implement it the best you can or copy it from me ( tongue ). I'll include a "random" player for testing and learning how to code the AI's.

Amcerbu, do you want to join, too? I noiced you've been posting but you never said you wanted to join. You're welcome to, though.  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#82 2012-05-05 11:43:43

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: AI Strategy Tournament: Rescheduled to June 1st

@Hardmath123: I'd like to, but I don't know a bit of JavaScript.  Thanks for the offer, though!

Offline

 

#83 2012-05-05 12:24:50

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

amcerbu wrote:

@Hardmath123: I'd like to, but I don't know a bit of JavaScript.

...and that's a problem because...?  tongue


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#84 2012-05-05 18:57:56

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

Hey guys, after helping make the Game Tree article on the Wiki, I decided to host this competition/tournament for all advanced Scratch coders. Basically, you need to code an AI to play a strategy game, and we pit them against each other until we have a dominating winner! Here are the current rules:

Your Code
• You need to code a single JavaScript function which takes in the position as an argument and returns the position formed after it moves.

I guess I could join someone's team...  I know a bit of C++ and a fair amount of C#, but I'm not at all familiar with JavaScript.  For instance, what's a textarea?

EDIT: Wait, never mind, I won't be able to do it.  I'm going out of town during June...  I'd love to see the results when it's over, though (and I'll keep up with the topic)!

Last edited by amcerbu (2012-05-05 19:02:23)

Offline

 

#85 2012-05-11 08:06:26

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

hardmath, how's going?

Offline

 

#86 2012-05-11 08:41:49

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Meh. I'm done with th eHTML/CSS UI, now I need to do the actual coding (which, as you may have guessed, I'm a bit lazy to deal with).  tongue  I've got till May end, so I'm taking it easy.  cool


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#87 2012-05-11 08:59:39

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

could write the js code  smile
just upload the page

Offline

 

#88 2012-05-11 11:59:07

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Promise not to add in loopholes to help you cheat? ( tongue )


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#89 2012-05-11 13:03:33

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

Promise not to add in loopholes to help you cheat? ( tongue )

i will write it nice and documented so you all can see i didn't do anything :cuteface:

Offline

 

#90 2012-05-13 01:04:34

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: AI Strategy Tournament: Rescheduled to June 1st

(Am I the only one obsessed with making beautiful, over-commenting code?)

Offline

 

#91 2012-05-13 04:16:09

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

amcerbu wrote:

(Am I the only one obsessed with making beautiful, over-commenting code?)

No.  tongue  But beautiful code is very difficult to write. One keeps being "forced" to use ugly hacks...  sad


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#92 2012-05-13 08:45:32

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

amcerbu wrote:

(Am I the only one obsessed with making beautiful, over-commenting code?)

Over-commented code will probably be a good thing in this case, since there'll be a team that need to understand your code.


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#93 2012-05-15 01:25:34

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

blob8108 wrote:

amcerbu wrote:

(Am I the only one obsessed with making beautiful, over-commenting code?)

No.  tongue  But beautiful code is very difficult to write. One keeps being "forced" to use ugly hacks...  sad

?

I love writing "beautiful" code. You never need an ugly hack, no matter what your situation is. There's always a reasonable cross-browser workaround, even if it means giving up on a smaller feature. If there isn't, you shouldn't be using that feature anyway.  wink

Roijac, if you're serious about helping out with the framework, I'll post the code here. The framework consists of a single HexBoard object with all the methods built right in—you just need to fill in the methods. I'll paste the code this afternoon, once I comment it out for you.  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#94 2012-05-15 02:30:10

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

blob8108 wrote:

amcerbu wrote:

(Am I the only one obsessed with making beautiful, over-commenting code?)

No.  tongue  But beautiful code is very difficult to write. One keeps being "forced" to use ugly hacks...  sad

?

I love writing "beautiful" code. You never need an ugly hack, no matter what your situation is. There's always a reasonable cross-browser workaround, even if it means giving up on a smaller feature. If there isn't, you shouldn't be using that feature anyway.  wink

Roijac, if you're serious about helping out with the framework, I'll post the code here. The framework consists of a single HexBoard object with all the methods built right in—you just need to fill in the methods. I'll paste the code this afternoon, once I comment it out for you.  smile

smile
bit busy these days with the private chat for scratch 2.0, but i'll finish it today probably  smile

Offline

 

#95 2012-05-15 02:37:35

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

blob8108 wrote:

amcerbu wrote:

(Am I the only one obsessed with making beautiful, over-commenting code?)

No.  tongue  But beautiful code is very difficult to write. One keeps being "forced" to use ugly hacks...  sad

I love writing "beautiful" code. You never need an ugly hack, no matter what your situation is. There's always a reasonable cross-browser workaround, even if it means giving up on a smaller feature. If there isn't, you shouldn't be using that feature anyway.  wink

I jest (partly).  tongue  And for web stuff, you can always use progressive enhancement.  smile

I meant for code in general -- like the Python code behind Kurt. It may be beautiful at first (or even merely kinda pretty), but then you have to make a small tweak or change and the temptation is to just "add it on", as it were; despite knowing the best (and more time-consuming) thing might be to rework the whole design.

Or sometimes, you can just hide the "ugly" code behind a nice, clean interface... JS libraries like jQuery or Raphael.js that handle cross-browser differences and quirks come to mind.

I do agree with you: beautiful code is much nicer; as well as being easier to read and debug and maintain.  smile  And the issue I describe is much less a problem for smaller projects, I'll admit.

Maybe I'll write a Kurt 2.0 someday... but I'm getting offtopic.  tongue

Last edited by blob8108 (2012-05-15 02:39:14)


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#96 2012-05-15 02:42:55

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

I must admit i'm a just-get-it-working kind of programmer, much to the chagrin of my C# lecturer. I truly appreciate beautiful code though, and wish I could do it  tongue

This is being written in JavaScript as a team effort, right?  smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#97 2012-05-15 02:45:01

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

sparks wrote:

I must admit i'm a just-get-it-working kind of programmer, much to the chagrin of my C# lecturer. I truly appreciate beautiful code though, and wish I could do it  tongue

This is the point — I feel like this sometimes...  tongue

This is being written in JavaScript as a team effort, right?  smile

As I understand it, the JS framework for the competition is being written by Hardmath123/as a team effort. For the competition, everyone writes their own AI in JS — I'm not entirely sure whether team entries are allowed or not.  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#98 2012-05-15 02:56:20

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

blob8108 wrote:

sparks wrote:

I must admit i'm a just-get-it-working kind of programmer, much to the chagrin of my C# lecturer. I truly appreciate beautiful code though, and wish I could do it  tongue

This is the point — I feel like this sometimes...  tongue

This is being written in JavaScript as a team effort, right?  smile

 I'm not entirely sure whether team entries are allowed or not.  smile

They are.  smile


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#99 2012-05-15 05:16:08

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

@Roijac: Here's the basic code which we need to fill in, test, debug, etc. Anyone is welcome to complete a method for me. You'll get to see how the backend works so your AI will work out better, too!  smile

Code:

/* HexFramework consists of a single HexBoard object which contains all the
 * methods needed by the AIs and the backend. Methods which need to be
 * filled in are marked, though you may want to optimize and debug the rest;
 * I wrote this quite late at night and I haven't tested any of it.
 *
 * When specifying cells, all the functions take in two arguments (row, col)
 * as the inputs. Row indicates which item along the X axis, and col
 * indicates which item along the Y axis. So the middle of the top row is
 * cell row:4 col:0 (Note: cells are named 0-9 for JS array compatibility).
 *
 */
function HexBoard() {
    this.contents = new Array(10);
    
    for (var i=0; i<10; i++) {// Create empty 10*10 array
        this.contents[i]=new Array(10);
        for (var j=0; j<10; j++) {
            this.contents[i][j] = "-";
        }
    }
    
    this.copy = function(newBoard) {// Create a copy, save it in newBoard pointer
        for (var i=0; i<10; i++) {
            for (var j=0; j<10; j++) {
                newBoard.contents[i][j] = this.contents[i][j];
            }
        }
    }
    
    this.whoWon = function() {// !!!!! returns who won, "R", "B", "-" (nobody) or "T" (tie)
    
        // To implement this, first implement the connected? method.
        // Then check if all pairs of cells which, if connected, would result in a win, are connected.
        // For example, (0,0) and (9,9) would result in a win if connected.
    }
    
    this.connected = function(row1, col1, row2, col2, player) {// !!!!! returns true if two specified cells are connected by a chain of the same color "player" (R, B, or -)
        // This should work recursively: two cells are connected if either they are adjacent, or a cell adjacent to one of the cells is connected to the second.
        // The code below is non-functional but explains how the working code should function
        
        /*
        var options = this.adjacent(row1, col1); // get adjacent cells
        for (var i=0; i<options.length; i++) {
            if (this.contents[options[i][0]][options[i][1]]==player) {// get adjacent cells of wanted color
                if (options[i] = [row2, col2]) {// if the adjacent cell is the one we are check to see if they are connected
                    return true;
                } else {
                    if (this.connected(options[i][0], options[i][1], row2, col2, player)) {// if the adjacent cell is connected to the cell...
                        return true;
                    }
                }
            }
        }
        return false;*/
    
    }
    
    this.rotate = function() {// !!!!! rotate board 90 degrees, don't return anything
    }
    
    this.moveTo = function(row, col, piece) { // Just places piece @ cell
        this.contents[row][col]=(piece=="R") ? "R" : "B";
    }
    
    this.empty = function(row, col) { // Checks if a cell exists
        return (this.contents[row] && this.contents[row][col]);
    }
    
    this.adjacent = function(row, col) {// returns all adjacent cells
        var options = [];
        if (this.empty(row-1, col)) {
            options.push([row-1, col]);
        }
        if (this.empty(row+1, col)) {
            options.push([row+1, col]);
        }
        if (this.empty(row, col+1)) {
            options.push([row, col+1]);
        }
        if (this.empty(row, col-1)) {
            options.push([row, col-1]);
        }
        if (this.empty(row-1, col+1)) {
            options.push([row-1, col+1]);
        }
        if (this.empty(row+1, col-1)) {
            options.push([row+1, col-1]);
        }
        return options;
    }
    
    this.drawOn = function(ctx) {// draws on a context
        ctx.save();
        for (var i=0; i<10; i++) {
            for (var j=0; j<10; j++) {
                switch (this.contents[i][j]) {
                    case "-":
                        ctx.fillStyle="gray";
                        break;
                    case "R":
                        ctx.fillStyle="red";
                        break;
                    case "B":
                        ctx.fillStyle="blue";
                        break;
                }
                ctx.beginPath();
                ctx.arc(i*33+15*j+15, j*30+15, 15, 0, Math.PI*2, false);
                ctx.fill();
            }
        }
        ctx.restore();
    }
}

Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#100 2012-05-15 08:16:37

roijac
Scratcher
Registered: 2010-01-19
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

@Roijac: Here's the basic code which we need to fill in, test, debug, etc. Anyone is welcome to complete a method for me. You'll get to see how the backend works so your AI will work out better, too!  smile

Code:

code

thanks!
i'm working on the whoWon method right now, though i'm using a bit another approach (caching if a piece is connected to one side, then marking the pieces around it as also connected, etc.)
little comment: there cannot be tie  tongue

Is red or blue playing from left to right?

Last edited by roijac (2012-05-15 08:22:43)

Offline

 

Board footer