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

#251 2012-11-11 10:40:50

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

EDIT: Kind of unrelated, but where can I find the .py source of the modules? I can find all the pyc/pyo-optimized files, but I obviously can't read those.  hmm

if you have them, they're in the same place. if they aren't, you apparently don't have them  hmm

Last edited by roijac (2012-11-11 10:41:06)

Offline

 

#252 2012-11-11 10:45:32

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

Re: AI Strategy Tournament: Rescheduled to June 1st

roijac wrote:

prisoners dilemma, any1?

This Prisoner's Dilemma?

http://en.wikipedia.org/wiki/Prisoner%27s_dilemma

Offline

 

#253 2012-11-11 10:52:01

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

blob8108 wrote:

For the interface — it would be really nice for a human to be able to play against the AI whilst developing it, as well as watch the AIs play each other. How about an HTML/Ajax interface with a Twisted/Python backend? (I'll code the Twisted stuff...) We could have a simple HTTP interface returning JSON objects (or even use WebSockets!).

That's a wonderful idea! I've made Python backends with CGIHTTPServer...

No, no, you need to use Twisted. Seriously. I'm not backing down on this point  tongue

How about division of labour? after all

I'm great with <Canvas> UIs, so we're good that way, too.

— so how about I do the Python backend, and you do the HTML/JS frontend?  big_smile  We just need to agree on the interface!

We could use AJAX queries like MOVE.py?player=hardmath&boardpos=…. MOVE.py should be modified with the AIs added in.

That looks messy  tongue  Can't we at least use POST requests? And I think we should use jQuery, too, if you agree.

Also, we really need to pick a game soon. Hex or Mancala would be my main choices, but both are biased towards the first mover.

Either sounds good. I don't think the first mover bias matters too much; we can always alternate players. Kalah looks confusing; I'd need to try and understand it properly. We might as well do Hex, since we planned to before — but I really don't mind  smile  you choose something!

EDIT: Kind of unrelated, but where can I find the .py source of the modules? I can find all the pyc/pyo-optimized files, but I obviously can't read those.  hmm

Python source of what modules?


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

Offline

 

#254 2012-11-11 12:13:54

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: AI Strategy Tournament: Rescheduled to June 1st

Rock Paper Scissors could be a fun one. You know, try to predict patterns...or just try to be as random as possible to confuse the opponent.

A lot more strategies than Prisoner's Dilemma IMHO


Fork Clamor on GitHub!

Offline

 

#255 2012-11-11 12:18:52

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

Re: AI Strategy Tournament: Rescheduled to June 1st

I'll answer in reverse order because simplicity of answers seems to go that way:

Python source of what modules?

Python source of all included (Pure Python) modules, ideally.

Either sounds good. I don't think the first mover bias matters too much; we can always alternate players. Kalah looks confusing; I'd need to try and understand it properly. We might as well do Hex, since we planned to before — but I really don't mind    you choose something!

Hex, then.  smile

No, no, you need to use Twisted. Seriously. I'm not backing down on this point.

Well that gives me a whole lot of choice, doesn't it?  tongue

Actually, that sounds good. I'm not sure at all how Twisted works, but in the end I want to have:
1. A main server which I run in the background
2. A file which I view on localhost:****/Desktop/Hex/Hex.html: this one should echo back an HTML page I'll write you
3. A file in which I add multiple AIs which can be called
4. An API which given a board and an AI's name should return the coordinates of the move (using file 3).

Fair? I'll write up the JS and everything else.  smile

EDIT: I was bored, so I looked up the Twisted docs. It looks really cool, now I'm really excited.  tongue

Last edited by Hardmath123 (2012-11-12 03:31:45)


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

Offline

 

#256 2012-11-11 12:19:53

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

Re: AI Strategy Tournament: Rescheduled to June 1st

ohaiderstudios wrote:

Rock Paper Scissors could be a fun one. You know, try to predict patterns...or just try to be as random as possible to confuse the opponent.

A lot more strategies than Prisoner's Dilemma IMHO

I suggested that somewhere above... we're going with Hex though.


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

Offline

 

#257 2012-11-11 12:41:31

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

Re: AI Strategy Tournament: Rescheduled to June 1st

ohaiderstudios wrote:

Rock Paper Scissors could be a fun one. You know, try to predict patterns...or just try to be as random as possible to confuse the opponent.

A lot more strategies than Prisoner's Dilemma IMHO

WRONG

in RPS, no strategy could defeat random, in PD every good strategy could

Offline

 

#258 2012-11-11 13:22:14

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

Re: AI Strategy Tournament: Rescheduled to June 1st

I'm still for Go.  It's a pretty advanced game and could accommodate some really cool strategies.  Check it out, if you don't know about it already. 

http://en.wikipedia.org/wiki/Go_(game)

@roijac: Which "Prisoner's Dilemma" game are you talking about?  I could only find this.

Last edited by amcerbu (2012-11-11 13:22:24)

Offline

 

#259 2012-11-11 13:29:02

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

Re: AI Strategy Tournament: Rescheduled to June 1st

that's what i'm talking about  wink
http://en.wikipedia.org/wiki/Prisoner%27s_dilemma#Strategy_for_the_iterated_prisoners.27_dilemma

Offline

 

#260 2012-11-12 04:25:22

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

I'll answer in reverse order because simplicity of answers seems to go that way:

Python source of what modules?

Python source of all included (Pure Python) modules, ideally.

Either sounds good. I don't think the first mover bias matters too much; we can always alternate players. Kalah looks confusing; I'd need to try and understand it properly. We might as well do Hex, since we planned to before — but I really don't mind    you choose something!

Hex, then.  smile

No, no, you need to use Twisted. Seriously. I'm not backing down on this point.

Well that gives me a whole lot of choice, doesn't it?  tongue
...
EDIT: I was bored, so I looked up the Twisted docs. It looks really cool, now I'm really excited.  tongue

Twisted is really cool  big_smile


in the end I want to have:
1. A main server which I run in the background
2. A file which I view on localhost:****/Desktop/Hex/Hex.html: this one should echo back an HTML page I'll write you
3. A file in which I add multiple AIs which can be called
4. An API which given a board and an AI's name should return the coordinates of the move (using file 3).

Fair? I'll write up the JS and everything else.  smile

Sounds good — apart from a few minor tweaks...  tongue

I've already got some Twisted code for a chatbot (I showed it to you, once) — I'm working on adapting it for hex. How it works atm:

* the HTML lives at http://localhost:8000/static/
* JS makes a POST request to http://localhost:8000/chats/, getting back an ID: {"id": "10324c67317b4d1fa86fbd032ab1e889"}
* it then POSTs to http://localhost:8000/chats/10324c67317b4d1fa86fbd032ab1e889/events. This is a long poll, so it only returns when a new event happens.
* it can POST to http://localhost:8000/chats/10324c67317b4d1fa86fbd032ab1e889/send to send a message.

I'm thinking of replacing "chats" with "sessions", or something, and tweak it so I can send JSON events back and forth. Sound okay?  smile

I'll figure out the Python interface later...


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

Offline

 

#261 2012-11-12 05:07:50

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

Re: AI Strategy Tournament: Rescheduled to June 1st

I think we can simplify that a little:

Since we're doing all the AI in Python, I can drastically rework (rewrite, basically  tongue ) the HTML. When I have a move, here's what should happen:

JS sends an XHR POST request to aimove.py with a CGI encoding a) the board encoded as a sequence of 0,1,2; b) the AI to use.

aimove.py is a Twisted server that does something like this:

Code:

…
def render_POST(self,request):
    board = cgi.escape(request.args["board"][0])
    ai    = cgi.escape(request.args["ai-id"][0])
    import aiplayers
    move  = aiplayers.play(board,ai)
    return str(move[0])+"\n"+str(move[1])
…

Finally, the contestants should modify aiplayers by adding a method which takes a board as argument (ideally parsed into a list of lists) and returns a tuple of (X,Y) to move to.

Code:

#aiplayers should be something like this

def hm(board):
    # AI here
def bl(board):
    # AI here

aiList = {"hardmath123":hm,"blob8108":bl}
def play(board,ai)
    parsed = parse(board)
    return aiList[ai](board)

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

Offline

 

#262 2012-11-12 14:35:09

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

I think we can simplify that a little:

Since we're doing all the AI in Python, I can drastically rework (rewrite, basically  tongue ) the HTML. When I have a move, here's what should happen:

JS sends an XHR POST request to aimove.py with a CGI encoding a) the board encoded as a sequence of 0,1,2; b) the AI to use.

aimove.py is a Twisted server that does something like this:

Code:

…
def render_POST(self,request):
    board = cgi.escape(request.args["board"][0])
    ai    = cgi.escape(request.args["ai-id"][0])
    import aiplayers
    move  = aiplayers.play(board,ai)
    return str(move[0])+"\n"+str(move[1])
…

Finally, the contestants should modify aiplayers by adding a method which takes a board as argument (ideally parsed into a list of lists) and returns a tuple of (X,Y) to move to.

...except not actually CGI, because we're using Twisted.  tongue  But the principle is the same (from the point of view of making the HTTP request).

I see. I was thinking (had assumed) that all the AI-related logic — setting up games/tournaments, running the AIs, managing each game session, and so on — would run on the Python server. The server would keep track of the current state of the board, so you don't have to send it with each request. The JS would act purely as the interface, sending events like "user clicked this square", and receiving events like "ai moved here" and data representing the board.

Your view seems to be that the Python interface is a simple wrapper around the AI function call. The JS has to do all the hard stuff, like flipping the board between AIs, and that sort of thing. We probably lose consistency between sessions this way, not that it matters too much — the AI can't "remember" who it's playing (although we could send this information).

So, arguments in favour of using Python for this stuff: The JS would be simpler. I thought the point of using Python is that Python is nicer/easier to code. Wouldn't it make more sense to do the hard stuff in Python? It's probably better division of labour, too; I imagine the interface alone (displaying the board, etc) is more than enough work. But I'm not complaining  tongue

In favour of JS: the protocol would be a bit simpler, as we don't have to keep track of sessions, and we'd only need one RPC call. The Python code certainly gets simpler. You get multiple clients/simultaneous games pretty much for free.

Your thoughts?  smile

Do I even make sense?  tongue

Last edited by blob8108 (2012-11-12 14:35:50)


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

Offline

 

#263 2012-11-13 01:09:14

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

Re: AI Strategy Tournament: Rescheduled to June 1st

You make sense. Or the neighbors have finally succeeded in their long-standing attempt to drive me crazy (playing loud hard rock at 5AM: the perfect crime  tongue ).

First, I want to point out that you really don't need to worry about how much JS I have to right: the UI will be really easy (unless I choose to use THREE.js+WebGL to make a cool 3D board, which I don't think I will). So we don't need to consider division of labour.  smile

Let's try to figure out what all tasks we need to do, and assign them to either server or JS:

1. GUI to play Hex: JS, obviously
2. Protocol and connection between server and JS: both
3. Maintenance of the board's data and move log: ???
4. AI players: Python
5. Move authentication and winner/loser detection: ???

I think it's clearly more sensible to maintain the board on the JS side. If we have the server keep persistent data between moves the entire code will be much more complicated. As you said, it'll also give us simultaneous games for free. Additionally, we need to give the JS the board data anyway, so we might as well keep it on the browser (unless we keep a copy of the board on each side and just send X/Y tuples; but that sounds like too much trouble and lots of possibilities for error.)

Move authentication is probably better kept on the JS side too, since I'll have to authenticate the humans' moves. Also, what happens if the Python does catch an invalid move? Error protocol? Ew.  tongue  The JS can handle it appropriately by either sending another call just to be safe, or automatically forfeiting the offending computer (that sounds so weird to say  tongue ). This would be considered a part of  "interface", anyway, in my opinion since what if we play human-vs.-human mode?

By the same logic as above, we should ideally check for winner-loser detection on the JS-side, too, though I can think of a really beautiful recursive Pythonic definition right now based on pathfinding algorithms. I can probably pull it off in JS, so I'm fine.

If this really does take off, we could mount this on App Engine, it would be really awesome.  smile

Let's figure out a protocol: I think CGI-style stuff would be fine: ai=blah&boardpos=1011212010002102012...&aiplaysas=2. Something like that. Then we can get started with actual coding.


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

Offline

 

#264 2012-11-13 09:29:01

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

First, I want to point out that you really don't need to worry about how much JS I have to write: the UI will be really easy (unless I choose to use THREE.js+WebGL to make a cool 3D board, which I don't think I will). So we don't need to consider division of labour.  smile

Well, if you're happy...

By the same logic as above, we should ideally check for winner-loser detection on the JS-side, too, though I can think of a really beautiful recursive Pythonic definition right now based on pathfinding algorithms. I can probably pull it off in JS, so I'm fine.

Things like win detection are exactly why I wanted to use Python for this in the first place.  tongue  Although it shouldn't be too hard: isn't it sort of spreading "activation" from one side to the other?

Let's figure out a protocol: I think CGI-style stuff would be fine: ai=blah&boardpos=1011212010002102012...&aiplaysas=2. Something like that. Then we can get started with actual coding.

If you think about it, you're essentially just using the Twisted server to execute some Python code (the AI function). The HTTP request is functioning as a remote procedure call. We might as well support AIs written in both JS and Python, and I think the implementation actually becomes simpler if we do this.

I'll give you some javascript functions that will do do HTTP requests or whatever with jQuery and give you back the x, y position to move to.

We simply have to tweak the interface with the AI code to return results asynchronously. Have the interface look like this:

Code:

// dictionary of AIs that we discussed before
ais = {
    "Hardmath123": function(board, callback) {
        // ...
        callback(x, y);
    },
    "blob8108": function(board) { ... },
}

The JS for the Python AIs will look something like this:

Code:

{
    // ...
    "some_python_AI_entry": function(board, callback) {
        data = {
            ai: "blah",
            board: board.to_string(),
        };
        $.post("http://localhost:8000/ai/get_move", data)
            .success(function(data) {
                callback(data.x, data.y);
            })
            .error(function() {
                callback(null);
            });
    },
    "another_python_AI": // etc...
}

I've even tested the corresponding Python code, and it works, so can we go with that?  smile  (here it is)

Downside: you have to write the flip() routine in JS. Or have an argument to the AI function.

Last edited by blob8108 (2012-11-13 09:30:02)


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

Offline

 

#265 2012-11-13 10:46:44

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

If you haven't already written everything in Twisted, bottle might be of some interest. Though it's mostly for websites/web applications.  wink

http://bottlepy.org/docs/dev/

Gevent might also be interest. I suggest looking at the entire front page.

http://www.gevent.org/

Offline

 

#266 2012-11-13 10:56:18

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Magnie wrote:

If you haven't already written everything in Twisted, bottle might be of some interest. Though it's mostly for websites/web applications.  wink

Yeah, I already have working Twisted code. I'll have a look at those, though, thanks — I've heard of bottle somewhere already...  smile


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

Offline

 

#267 2012-11-13 12:09:56

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Well, that sounds ok, I guess. Here are my views:

Why is it better to use Python for all the "behind the scenes" stuff (board flipping, win testing, move authentication)? The biggest reason against is that we need to authenticate the human's move, too, and obviously we shouldn't bother sending that over to the server just to authenticate when we could be doing it locally. Instead, I think the JS should validate all moves, along with all other behind the scenes stuff.

We don't really need the board flipping code. Since we tell the AI whether to place a red or blue counter (1 or 2), we may as well just make a convention that blue plays top-to-bottom and red plays left-to-right.

Supporting JS AIs seems easy, but I think there are a bunch of catches, mainly that someone smart enough to code an AI is clearly smart enough to use scoping rules to his advantage and "cheat" with yucky code. I don't want to waste my time hacker-proofing my code, and besides, everyone who has said yes so far seems to have their hearts set on Python.

I definitely intended to have asynchronous move functions; but I'd rather use XHRs manually than resort to a big library like jQuery (I'm a minimalist). I'm actually going to use native DOM elements (DIVS, actually) for my Hex board, so I can have a single move/authenticate/detectwin function with wrappers to receive data from the AI or the human based on who's turn it is (a global boolean flag). This simplifies a lot of stuff.

On the Python side, I take back my idea about coding a single function, and would rather code a module AI script. This lets us define plenty of subroutines, as well as testing scripts which we can run without the server. You'd simply use something like __import__("Hardmath123") or import Hardmath123 as currentAI, though I'm not sure exactly how Python handles namespaces.

Overall, it's probably better if we each code our own side and agree on a "bridge" (the protocol). This will give us optimum code, without anything muddled.

EDIT: If I encode the board as a sequence of 0s, 1s and 2s, it makes sense to just convert it to base 3, which would have a max of about half the digits and min of 1 for obvious cases. I'll leave to back-to-sequence decoding to you in Python, but I think this will be helpful. If we have a sequence of n digits, then the worst case in base 3 would be n digits of 22222... which is 2*(3^0+3^1+3^2+...+3^(n-1)) or (3^n-1). The number of digits is about the log of this, which for n=100 amounts to 47.

EDIT2: Why do we communicate with walls of text on this thread? I'm almost feeling compelled to lengthen my posts just to keep up.  tongue

Last edited by Hardmath123 (2012-11-13 12:23:33)


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

Offline

 

#268 2012-11-13 15:55:13

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

Why is it better to use Python for all the "behind the scenes" stuff (board flipping, win testing, move authentication)? The biggest reason against is that we need to authenticate the human's move, too, and obviously we shouldn't bother sending that over to the server just to authenticate when we could be doing it locally. Instead, I think the JS should validate all moves, along with all other behind the scenes stuff.

If you do that, your humans will be able to cheat. The golden rule is never to assume that any information a client sends is valid. You always need to validate it on the server. However, you're right that it's unnecessary to send a move you know is invalid to the server. Your JS should do preliminary error-checking and give the user feedback about the error instead of sending anything to the server; the server should also check client information, though.

Supporting JS AIs seems easy, but I think there are a bunch of catches, mainly that someone smart enough to code an AI is clearly smart enough to use scoping rules to his advantage and "cheat" with yucky code. I don't want to waste my time hacker-proofing my code, and besides, everyone who has said yes so far seems to have their hearts set on Python.

Unless you're doing something extremely odd like eval()ing this AIs, "use strict" will prevent crazy arguments.caller stuff:

Code:

> 'use strict';
. function test(a) {
.  foo();
. }
< "use strict"
> function foo() {
.  console.log(arguments.caller);
. }
< undefined
> test(12)
< undefined

If you meant that the AI can just modify the global variables, use a self-invoking function:

Code:

!function () {
    var board = {}; // inaccessible from outside the closure
    // ...
}();

I'm actually going to use native DOM elements (DIVS, actually) for my Hex board, so I can have a single move/authenticate/detectwin function with wrappers to receive data from the AI or the human based on who's turn it is (a global boolean flag). This simplifies a lot of stuff.

These two things have nothing to do with each other. I assume what you mean by using native DOM elements is that you're not going to use <canvas>; however, this should have nothing to do with your data storage mechanism: you should be using the HTML DOM for display only, not for representing your data.

EDIT: If I encode the board as a sequence of 0s, 1s and 2s, it makes sense to just convert it to base 3, which would have a max of about half the digits and min of 1 for obvious cases.

Do you mean convert it from base 3 to base 10?

Last edited by nXIII (2012-11-13 15:55:36)


nXIII

Offline

 

#269 2012-11-13 17:25:27

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hardmath123 wrote:

Why do we communicate with walls of text on this thread? I'm almost feeling compelled to lengthen my posts just to keep up.  tongue

Because I (and nXIII too, apparently) reply to the things you say all at once.  tongue  In the interests of brevity, then:

I just thought it would be easier to use Python for the "hard" bits; I agree with you that it makes the protocol easier not to.

nXIII makes some good points. The one about validation — do we just assume that all code is trusted? Unless we have an actual server on the public internet (which would be awesome...  big_smile ) He also said some things about JS, which I'll leave for now...

We've discussed board flipping at least twice now! =P (Here's you sort-of agreeing with me.) So that every AI doesn't have to do this themselves: For an AI playing as red, we will flip the board before passing it to the AI. We can then flip the x,y co-ords that we get back. We don't need to tell the AI who to play as; all the AI code will assume blue.  smile

For the interface: if you POST to http://localhost:8000/ai/get_move with:

  * board_size = 17 (or whatever)
  * board = "0R00000BB00BRR..."
  * AI = "blob8108"

— you'll get back a JSON object {x: 5, y: 7} with the co-ords. Happy? (:

Please don't use base 3 encoding! It's a nice idea, but at only ~<300 characters it's not worth compressing, and it'll make the protocol much more complicated. I tried base64 in Scratch for that roads game, and never got it to work properly.

Each AI will be a Python class in its own file. I'll figure out how to find them.

jQuery isn't exactly big, and I'd have thought using it would make more minimal code; but it's your code, have fun  tongue  And how are you planning to make hexagons out of DOM elements?! Personally I'd use Raphael, too...

---

Right, I'm off to figure out how to sandbox Python code so we can run a public server that hosts the AIs, with user submissions... PyPy looks cool!  big_smile

[Edited to mildly increase brevity]

Last edited by blob8108 (2012-11-13 17:28:52)


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

Offline

 

#270 2012-11-13 17:43:25

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: AI Strategy Tournament: Rescheduled to June 1st

WebWorkers!  big_smile


http://block.site90.net/scratch.mit/text.php?size=30&amp;text=%20A%20signature!&amp;color=333333

Offline

 

#271 2012-11-13 19:46:46

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

Re: AI Strategy Tournament: Rescheduled to June 1st

I agree with Hardmath123: It will simplify everything if the program flips the board in between turns.  It would help the AI code as well, since a computer player would behave the same way whether playing black or white. 

Flipping isn't that hard to implement.  Let's say L1 and L2 are the indexes of the last items in each dimension of the array. 

Switch item array[i][j] with item array[L1 - i ][L2 - j]. 

Code:

array[5][5] = 
{
   { 01, 02, 03, 04, 05 },
   { 06, 07, 08, 09, 10 },
   { 11, 12, 13, 14, 15 }
   { 16, 17, 18, 19, 20 }
   { 21, 22, 23, 24, 25 }
}

L1 = 4, L2 = 4, because the array is 5 x 5, so the last element will be array[4][4].  The first index here represents vertical position in the code (since it's an array of arrays). 

Switch array[0][0] (01) and array[4][4] (25)
Switch array[1][3] (09) and array[3][1] (17)
Switch array[4][1] (22) and array[0][3] (04)
etc.

Last edited by amcerbu (2012-11-13 19:47:42)

Offline

 

#272 2012-11-13 23:10:40

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Actually, amcerbu, that means you agree with blob. But yeah, I think I've been convinced.  smile

@nXIII: I meant I'm using DOM to display the board and manage UI, so events are a lot easier to handle (especially with inline data-* attributes in HTML5). I'll generate the actual HTML of the board dynamically.

The actual board will of course be stored as a string with 0,1,2 representing the relevant states of the hexagons.

@blob: Ok, fine, I'll flip the board for you. Your protocol looks fine, but I don't see why you need board_size (you have the actual board to find that out yourself, right?).

Writing a class sounds great: the perfect balance between module and function.  big_smile

Fine, I'll leave it as 0,1,2. Speaking of which, what size board are we playing on? I'd say pick a big size (15?) just to give the AI a stretch. Also, you should use time.clock() to find out how long the AI took and send me that in the JSON response.

P.S. Since when does "Hex" need Hexagons?  tongue


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

Offline

 

#273 2012-11-14 00:37:35

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Oh, whoops, guess I'm with blob8108.  I guess if we're doing it on a Hex board, made of triangles, flipping would be a little more complicated. 

According to Wikipedia, an 11 x 11 field is standard for Hex, but the game can also be played on 13 x 13 and 19 x 19, and theoretically a field of any size.  It would be interesting to experiment with different field sizes — perhaps some are more difficult than others.  It would also be entertaining to try 100 x 100 or something ridiculous like that.  But I firmly believe that field size should be a property that can be changed, and that isn't implementation-specific (size becomes another one of the variables "fed" to AI code).  A perfect AI should be able to play on a field of any size.

EDIT (important): I just ran across this, and I think it would be a good thing to add to our list of things to do.  Again, according to Wikipedia, the first player in a game of Hex has a distinct advantage.  That can be balanced by the use of the so-called "Pie Rule."  Basically, after Player 1 makes the first move of the game, Player 2 may opt to switch positions with Player 1 (but he doesn't have to).  If they do switch, the original Player 1 makes the second move of the game. 

Anyway, I think it would be worth implementing.  Another thing for the AI to chew on. 

http://en.wikipedia.org/wiki/Pie_rule

Last edited by amcerbu (2012-11-14 01:05:00)

Offline

 

#274 2012-11-14 01:33:25

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Nah, I think that's over-complicating. Same for the board size; let's stick with one value.

P.S. @blob I got a beautiful CSS hexagon working after like 45 minutes of hacking.  smile


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

Offline

 

#275 2012-11-14 02:20:50

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

Re: AI Strategy Tournament: Rescheduled to June 1st

Hexagons. What do you think?


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

Offline

 

Board footer