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

Username:   Password: 
 RegisterRegister   
 Summer AI Challenge
Index -> Contests
Goto page Previous  1, 2, 3, 4
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bugzpodder




PostPosted: Wed Jun 04, 2003 7:07 pm   Post subject: (No subject)

we'll see how this works out. if all else fails, i'd have to go with C++.
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Wed Jun 04, 2003 7:08 pm   Post subject: (No subject)

I dont think this can be done with fcns but im not sure
you should be able to workaround using a proc tho
code:
proc blank
end blank

class AIDemo

    export Load, Think

    var Think : proc blank

    proc Load (proc f)
        Think := f
    end Load

end AIDemo

var demo : ^AIDemo
new demo

proc RThree
    put "It Works"
end RThree

demo -> Load (RThree)

for i : 1 .. 2
    demo -> Think
end for

just loadup the instances then they can be run thru the array
bugzpodder




PostPosted: Wed Jun 04, 2003 7:58 pm   Post subject: (No subject)

then i guess it isnt as different as this. anywayz here is the newest version of code where i've made use of inheritance to solve this problem. there will probably be no further changes made to the BaseAI.

the only thing you should be changing is the include file name and the stuff in the Reset procedure (class name) for your own Class

code:


/*Coded by Bugz Podder
 Version 0.02 (look at version number to see if you have latest code!)

 --Ver 0.02 if a player has no moves, the engine now takes care of it
 --Ver 0.01 first official version
 */

randomize
View.Set ("graphics:550;550");


class BaseSnakeAI

    /*
     This is a sample AI for Bugz' Snake Game

     for each move you have 4 directions, NORTH EAST SOUTH OR WEST
     you lose if you attempts to go into a square that is not free (occupied by a wall or a snake)
     Draws are possible.

     in the beginning of the game, you are given the number of players, your player number, board size
     the board state (could potentially have some walls in the middle but definately an outer wall,
     location of all the players, as well as game type)

     for each move you are given a list of updated positions (which the procedure
     already took care of) as well as you are asked for a direction to go.

     Real-Time game has one extra rule that Turned-Based doesnt have is if two snakes
     attempts to go onto the same square at the same turn, both will *DIE*


     what you *CANT* change
     values for NORTH, EAST, SOUTH, WEST
     the AIName, Reset and Move procedure's name as well as what they do

     anythin else you can pretty much change, including variable names and so on.


     */

    export Reset, Move, AIName;
    const SOUTH := 1;
    const EAST := 2;
    const NORTH := 3;
    const WEST := 4;

    var NUM_PLAYERS, MY_NUMBER, BOARD_SIZE, GAME_TYPE : int;
    var board : array 0 .. 251, 0 .. 251 of int;
    var playerX, playerY : array 1 .. 50 of int;
    var playerAlive : array 1 .. 50 of boolean;


    /* your id number is MY_NUMBER, and playerX, playerY stores X and Y values of every player
     playerAlive tells you if a player is alive.  to access your info, such as your
     current location, use playerX(MY_NUMBER) and playerY(MY_NUMBER)

     board (i,j) has a value of -1 if its a wall, 0 if its empty, or 1..50 depending on the
     player who occupied it

     GAME_TYPE: - 0 if it is Turn-Based
     - 1 if it is Real-Time

     */
    deferred fcn AIName () : string

    proc Reset (gtype : int, nump : int, mynum : int, size : int, brd : array 0 .. 251, 0 .. 251 of int,
            Xcrd : array 1 .. 50 of int, Ycrd : array 1 .. 50 of int)

        GAME_TYPE := gtype;
        NUM_PLAYERS := nump;
        MY_NUMBER := mynum;
        BOARD_SIZE := size;
        board := brd;
        playerX := Xcrd;
        playerY := Ycrd;

       for i : 1 .. NUM_PLAYERS
            playerAlive (i) := true;
        end for

    end Reset

    deferred fcn Think () : int


    fcn Move (dir : array 1 .. 50 of int) : int
        for i : 1 .. NUM_PLAYERS
            case dir (i) of
                label 0 :
                label 1 :
                    playerY (i) -= 1;
                label 2 :
                    playerX (i) += 1;
                label 3 :
                    playerY (i) += 1;
                label 4 :
                    playerX (i) -= 1;
                label :
                    playerAlive (i) := false;
            end case

            board (playerX (i), playerY (i)) := i;
        end for

        result Think ()
    end Move

end BaseSnakeAI

include "SampleSnakeAI.t"




class SnakeEngine
    import BaseSnakeAI, SampleSnakeAI
    export Run, Reset


    const SOUTH := 1;
    const EAST := 2;
    const NORTH := 3;
    const WEST := 4;

    const dir : array 1 .. 4, 0 .. 1 of int := init (0, -1, 1, 0, 0, 1, -1, 0);


    var NUM_ALIVE, NUM_PLAYERS, BOARD_SIZE, GAME_TYPE, NUM_TURN, DELAY_TIME, OFFSET : int;

    var board : array 0 .. 251, 0 .. 251 of int;
    var playerX, playerY, playerDir, beginDir : array 1 .. 50 of int;
    var playerAlive : array 1 .. 50 of boolean;
    var ptr : array 1 .. 50 of ^BaseSnakeAI

    proc Reset (nump, brdsize, gametype, delaytime : int)
        NUM_PLAYERS := nump;
        BOARD_SIZE := brdsize;
        GAME_TYPE := gametype;
        DELAY_TIME := delaytime;

        OFFSET := 20;

        %calculate offset

        var ptr1 : ^SampleSnakeAI
        var ptr2 : ^SampleSnakeAI
        var ptr3 : ^SampleSnakeAI
        var ptr4 : ^SampleSnakeAI
        new ptr1
        new ptr2
        new ptr3
        new ptr4
        ptr (1) := ptr1;
        ptr (2) := ptr2;
        ptr (3) := ptr3;
        ptr (4) := ptr4;

    end Reset

    fcn freesp (x, y : int) : int
        var w := 0;
        for i : 1 .. 4
            if board (x + dir (i, 0), y + dir (i, 1)) = 0 then
                w += 1;
            end if
        end for
        result w
    end freesp

    proc Run ()
        for i : 1 .. BOARD_SIZE
            for j : 1 .. BOARD_SIZE
                board (i, j) := 0;
            end for
        end for
        for i : 0 .. BOARD_SIZE + 1
            drawfillbox (i * 10 - 5 + OFFSET, -5 + OFFSET, i * 10 + 5 + OFFSET, 5 + OFFSET, black);
            drawfillbox (-5 + OFFSET, i * 10 - 5 + OFFSET, 5 + OFFSET, i * 10 + 5 + OFFSET, black);
            drawfillbox (i * 10 - 5 + OFFSET, (BOARD_SIZE + 1) * 10 - 5 + OFFSET, i * 10 + 5 + OFFSET, (BOARD_SIZE + 1) * 10 + 5 + OFFSET, black);
            drawfillbox ((BOARD_SIZE + 1) * 10 - 5 + OFFSET, i * 10 - 5 + OFFSET, (BOARD_SIZE + 1) * 10 + 5 + OFFSET, i * 10 + 5 + OFFSET, black);
            board (0, i) := -1;
            board (i, 0) := -1;
            board (i, BOARD_SIZE + 1) := -1;
            board (BOARD_SIZE + 1, i) := -1;
        end for
        for i : 1 .. NUM_PLAYERS
            var RandX, RandY : int
            loop
                RandX := Rand.Int (1, BOARD_SIZE);
                RandY := Rand.Int (1, BOARD_SIZE);
                exit when board (RandX, RandY) = 0
            end loop
            playerX (i) := RandX;
            playerY (i) := RandY;
            playerAlive (i) := true;
            playerDir (i) := 0;

        end for

       

        for i : 1 .. NUM_PLAYERS
            drawfillbox (playerX (i) * 10 - 5 + OFFSET, playerY (i) * 10 - 5 + OFFSET, playerX (i) * 10 + 5 + OFFSET, playerY (i) * 10 + 5 + OFFSET, i);
            ptr (i) -> Reset (GAME_TYPE, NUM_PLAYERS, i, BOARD_SIZE, board, playerX, playerY)
        end for

        var move : int := 0;
        NUM_ALIVE := NUM_PLAYERS;
        NUM_TURN := 0;
        var winner := -1;
        loop
            NUM_TURN += 1;
            exit when NUM_ALIVE = 0
            if NUM_ALIVE = 1 and winner = -1 then
                for i : 1 .. NUM_PLAYERS
                    if playerAlive (i) then
                        winner := i;
                    end if
                end for
            end if
            if GAME_TYPE = 0 then
                beginDir := playerDir;
            end if
            for i : 1 .. NUM_PLAYERS
                if playerAlive (i) = true then
                    if freesp (playerX (i), playerY (i)) = 0 then
                        playerAlive (i) := false;
                        playerDir (i) := -1;
                        NUM_ALIVE -= 1;
                    else
                        if GAME_TYPE = 0 then
                            move := ptr (i) -> Move (playerDir);
                        elsif GAME_TYPE = 1 then
                            move := ptr (i) -> Move (beginDir);
                        end if
                        playerDir (i) := move;
                        if move > 0 and move < 5 and board (playerX (i) + dir (move, 0), playerY (i) + dir (move, 1)) = 0 then

                            playerX (i) += dir (move, 0);
                            playerY (i) += dir (move, 1);
                            board (playerX (i), playerY (i)) := i;
                            drawfillbox (playerX (i) * 10 - 5 + OFFSET, playerY (i) * 10 - 5 + OFFSET, playerX (i) * 10 + 5 + OFFSET, playerY (i) * 10 + 5 + OFFSET, i);
                            if NUM_TURN > 1 then
                                drawline (playerX (i) * 10 + OFFSET, playerY (i) * 10 + OFFSET, (playerX (i) - dir (move, 0)) * 10 + OFFSET, (playerY (i) - dir (move, 1)) * 10 + OFFSET, black);
                            end if
                            delay (DELAY_TIME);
                        else
                            playerAlive (i) := false;
                            playerDir (i) := -1;
                            NUM_ALIVE -= 1;
                        end if
                    end if
                end if
            end for
        end loop

        if winner = -1 then
            put "Tie game!";
        else
            put "Winner is " + ptr (winner) -> AIName ();
        end if
    end Run


end SnakeEngine

var x : ^SnakeEngine
new x

x -> Reset (4, 50, 0, 0);  /* number of players, board size, game type (leave it at 0 for now), delay)*/
x -> Run ();



here is a sample of the AI
code:

/*
**************************************************************
*  AI Name: SampleAI                                         *
*  Coder:   Bugz Podder                                      *
**************************************************************
*/
class SampleSnakeAI
inherit BaseSnakeAI

   body fcn AIName():string
       result "SampleAI"
   end AIName

   
    body fcn Think () : int
        /*This is your main AI, do whatever you want*/
        if GAME_TYPE = 0 then  /* Turn-Based */
            result Rand.Int (1, 4)
        elsif GAME_TYPE = 1 then  /* Real-Time */
            result Rand.Int (1, 4)
        end if

    end Think

end SampleSnakeAI
Catalyst




PostPosted: Sat Jun 07, 2003 3:51 pm   Post subject: (No subject)

this would be cool if it ran over the net
Martin




PostPosted: Sat Jun 07, 2003 7:50 pm   Post subject: (No subject)

Apparently the net module works a bunch better now, so we might be able to.
PaddyLong




PostPosted: Sat Jun 07, 2003 8:04 pm   Post subject: (No subject)

can we add functions to our class? just ones for checking things for our AI ... like boolean ones to see if something more than a basic if statement is true
bugzpodder




PostPosted: Sun Jun 08, 2003 1:18 pm   Post subject: (No subject)

of course, you can add anything you'd like. net module ehh, i'll take a look at that.
bugzpodder




PostPosted: Thu Aug 07, 2003 8:47 pm   Post subject: (No subject)

My AI and the engine (i dont know if its the same as as posted b4)
anyways to use your own engine, you have to change two places in the code (Thats what i mean when i said in the other post, the problem was)

one place is a bunch of replace codes, and one place is a bunch of pointer declaration. just change the class name to your own class. or you can do a search and replace (search for FunAI)



Snake.zip
 Description:

Download
 Filename:  Snake.zip
 Filesize:  5.85 KB
 Downloaded:  178 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Thu Aug 07, 2003 9:56 pm   Post subject: (No subject)

heres the one i coded some of the code is unecessary im not gonna edit it now


AIGAME.T
 Description:

Download
 Filename:  AIGAME.T
 Filesize:  8.6 KB
 Downloaded:  217 Time(s)

PaddyLong




PostPosted: Fri Aug 08, 2003 1:19 pm   Post subject: (No subject)

here's my snake ai


PaddySnake.t
 Description:

Download
 Filename:  PaddySnake.t
 Filesize:  2.27 KB
 Downloaded:  209 Time(s)

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

Page 4 of 4  [ 55 Posts ]
Goto page Previous  1, 2, 3, 4
Jump to:   


Style:  
Search: