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

Username:   Password: 
 RegisterRegister   
 Tic Tac Toe with 3D!
Index -> Programming, Turing -> Turing Submissions
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
The_Bean




PostPosted: Wed Jan 21, 2009 10:13 pm   Post subject: Tic Tac Toe with 3D!

Tic Tac Toe with 3D 'X's and 'O's.
And probably still shorter than others at ~176 lines.
Turing:

View.Set ("graphics:600,600,offscreenonly,nobuttonbar")
type grid :
    record
        value : int
        used : boolean
    end record
var g : array 1 .. 3, 1 .. 3 of grid
var turn : boolean := true
var font : int := Font.New ("Algerian:50")
var results : array 0 .. 3 of string := init ("", "'X' Wins", "'O' Wins", "Cats Game")
type points :
    record
        x, y, z : real
        zA : real
    end record
var px : array - 18 .. 18, -18 .. 18 of points
var po : array - 18 .. 18, -18 .. 18 of points
var xm, ym, bm : int
var cm, sm : int
var scale : int := 200

Text.ColourBack (7)
Text.Colour (0)

for x : 1 .. 3
    for y : 1 .. 3
        g (x, y).used := false
        g (x, y).value := 0
    end for
end for

proc LoadGraphO (var p : array - 18 .. 18, -18 .. 18 of points)
    for x : -18 .. 18
        for y : -18 .. 18
            p (x, y).x := cosd (x * 10) * sind (y * 10)
            p (x, y).y := sind (x * 10) * sind (y * 10)
            p (x, y).z := cosd (y * 10)
        end for
    end for
end LoadGraphO

proc LoadGraphX (var p : array - 18 .. 18, -18 .. 18 of points)
    for x : -18 .. 18 by 2
        for y : -18 .. 18
            p (x, y).x := x / 20
            p (x, y).y := sind (y)
            p (x, y).z := y / 20
        end for
    end for
    for x : -17 .. 18 by 2
        for y : -18 .. 18
            p (x, y).x := x / 20
            p (x, y).y := y / 20
            p (x, y).z := 0
        end for
    end for
end LoadGraphX

proc RotateFormula (var p1, p2 : real, a : real)
    var p1T := p1 * cosd (a) + p2 * sind (a)
    var p2T := p2 * cosd (a) - p1 * sind (a)
    p1 := p1T
    p2 := p2T
end RotateFormula

proc Rotate (var p : array - 18 .. 18, -18 .. 18 of points, x, y, z : real)
    if x ~= 0 then
        for x1 : -18 .. 18
            for y1 : -18 .. 18
                RotateFormula (p (x1, y1).x, p (x1, y1).z, x)
            end for
        end for
    end if
    if y ~= 0 then
        for x1 : -18 .. 18
            for y1 : -18 .. 18
                RotateFormula (p (x1, y1).y, p (x1, y1).z, y)

            end for
        end for
    end if
    if z ~= 0 then
        for x1 : -18 .. 18
            for y1 : -18 .. 18
                RotateFormula (p (x1, y1).x, p (x1, y1).y, z)
            end for
        end for
    end if
end Rotate

proc DrawObject (var p : array - 18 .. 18, -18 .. 18 of points, cx, cy : int)
    var x, y : int
    for x1 : -18 .. 18
        for y1 : -18 .. 18
            x := round (p (x1, y1).x / (p (x1, y1).z - 3) * scale) + cx
            y := round (p (x1, y1).y / (p (x1, y1).z - 3) * scale) + cy
            Draw.Dot (x, y, 47)
        end for
    end for
end DrawObject

proc DrawGrid
    cls
    Draw.ThickLine (200, 0, 200, 600, 5, 47)
    Draw.ThickLine (400, 0, 400, 600, 5, 47)
    Draw.ThickLine (0, 200, 600, 200, 5, 47)
    Draw.ThickLine (0, 400, 600, 400, 5, 47)
    for x : 1 .. 3
        for y : 1 .. 3
            if g (x, y).used then
                if g (x, y).value = 1 then
                    DrawObject (px, x * 200 - 100, y * 200 - 100)
                else
                    DrawObject (po, x * 200 - 100, y * 200 - 100)
                end if
            end if
        end for
    end for
    View.Update
end DrawGrid

function Detection : string
    var full : boolean := true
    for i : 1 .. 2
        if
                g (1, 1).value = i and g (1, 2).value = i and g (1, 3).value = i or
                g (2, 1).value = i and g (2, 2).value = i and g (2, 3).value = i or
                g (3, 1).value = i and g (3, 2).value = i and g (3, 3).value = i or
                g (1, 1).value = i and g (2, 1).value = i and g (3, 1).value = i or
                g (1, 2).value = i and g (2, 2).value = i and g (3, 2).value = i or
                g (1, 3).value = i and g (2, 3).value = i and g (3, 3).value = i or
                g (1, 1).value = i and g (2, 2).value = i and g (3, 3).value = i or
                g (3, 1).value = i and g (2, 2).value = i and g (1, 3).value = i then
            result results (i)
        end if
    end for
    for x : 1 .. 3
        for y : 1 .. 3
            if ~g (x, y).used then
                result results (0)
            end if
        end for
    end for
    result results (3)
end Detection

function Placement (turn : boolean) : int
    if turn then
        result 1
    else
        result 2
    end if
end Placement

LoadGraphX (px)
LoadGraphO (po)
DrawGrid
Mouse.Where (cm, sm, bm)
loop
    Mouse.Where (xm, ym, bm)
    if bm = 1 and xm > 0 and xm < 600 and ym > 0 and ym < 600 then
        if ~g (xm div 200 + 1, ym div 200 + 1).used then
            g (xm div 200 + 1, ym div 200 + 1).used := true
            g (xm div 200 + 1, ym div 200 + 1).value := Placement (turn)
            turn := ~turn
        end if
    end if
    DrawGrid
    Rotate (px, xm - cm, ym - sm, bm * 10)
    Rotate (po, xm - cm, ym - sm, bm * 10)
    cm := xm
    sm := ym
    exit when Detection ~= ""
end loop
Font.Draw (Detection, maxx div 2 - Font.Width (Detection, font) div 2, maxy div 2 - 25, font, 47)
View.Update
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Wed Jan 21, 2009 10:18 pm   Post subject: Re: Tic Tac Toe with 3D!

I am impressed............kudos Very Happy

although, you can improve this by converting the whole grid into a cube......consider it as an extension to your program

very good though
Homer_simpson




PostPosted: Thu Jan 22, 2009 1:04 am   Post subject: Re: Tic Tac Toe with 3D!

neat!
seems u've learned 3d programming pretty well in turing, time to move on to more powerful programs like java and c Wink
The_Bean




PostPosted: Thu Jan 22, 2009 6:58 am   Post subject: Re: Tic Tac Toe with 3D!

Actually python for grade 12 which starts in 1 week. And this game was made entirely in 1 class on spare.
Ethan3210




PostPosted: Thu Jan 22, 2009 8:59 am   Post subject: Re: Tic Tac Toe with 3D!

The_Bean @ Thu Jan 22, 2009 6:58 am wrote:
Actually python for grade 12 which starts in 1 week. And this game was made entirely in 1 class on spare.


...
YOU.
ARE.
GOD.

That woulda taken me days. xD
saltpro15




PostPosted: Thu Jan 22, 2009 9:01 am   Post subject: RE:Tic Tac Toe with 3D!

cool, great job
dc116




PostPosted: Fri Jan 23, 2009 4:39 pm   Post subject: RE:Tic Tac Toe with 3D!

I am very impressed with this program. Good work!
Insectoid




PostPosted: Fri Jan 23, 2009 10:25 pm   Post subject: RE:Tic Tac Toe with 3D!

Just a question,

Are you going to go into video games? I assume you'd do well there. Your work is just phenomenal.

I used to do games almost exclusively, but I discovered that programs that are largely technical rather than graphical are more fun. Simulations are cool too.

You can call me your first and biggest fan. Your games have reached cereal box quality. Soon, you may reach CD-that-comes-with-game-magazine-dom. That would be awesome.
Sponsor
Sponsor
Sponsor
sponsor
andrew.




PostPosted: Fri Jan 23, 2009 10:44 pm   Post subject: RE:Tic Tac Toe with 3D!

Great job Bean! It would take me a lot more time than a spare period to write something like that.
The_Bean




PostPosted: Sat Jan 24, 2009 1:13 am   Post subject: Re: Tic Tac Toe with 3D!

Video Games are definitely a field I wouldn't mind ending up in. They combine math, physics, and computers which I love.

I feel like The Flight of the Conchords now that I have a fan base of 1. I might need to get a t-shirts made.

Heres the starting of 3D Tic-Tac-Toe 3*3*3 grid.
Not yet playable, I'm still trying to figure out a method for choosing boxes, and if theres an easier way than hard-coding every possible win scenario.

Turing:

View.Set ("graphics:600,600,offscreenonly,nobuttonbar,position:center,center")
type grid :
    record
        value : int
        used : boolean
        x, y, z : real
    end record
var g : array 1 .. 3, 1 .. 3, 1 .. 3 of grid
type coordinates :
    record
        x, y, z : real
    end record
type points :
    record
        p : array - 9 .. 9, -9 .. 9 of coordinates
    end record
var Shape : array 1 .. 2 of points
var xm, ym, bm : int
var cm, sm : int
var objectScale : int := 30
var gridScale : int := 100

Text.ColourBack (7)
Text.Colour (0)

fcn RandBoolean : boolean
    if Rand.Int (0, 1) = 1 then
        result true
    else
        result false
    end if
end RandBoolean

for x : 1 .. 3
    for y : 1 .. 3
        for z : 1 .. 3
            g (x, y, z).used := RandBoolean
            g (x, y, z).value := Rand.Int (1, 2)
            g (x, y, z).x := x - 2
            g (x, y, z).y := y - 2
            g (x, y, z).z := z - 2
        end for
    end for
end for

proc LoadGraphO (var d : points)
    for x : -9 .. 9
        for y : -9 .. 9
            d.p (x, y).x := cosd (x * 20) * sind (y * 20)
            d.p (x, y).y := sind (x * 20) * sind (y * 20)
            d.p (x, y).z := cosd (y * 20)
        end for
    end for
end LoadGraphO

proc LoadGraphX (var d : points)
    for x : -9 .. 9 by 2
        for y : -9 .. 9
            d.p (x, y).x := x / 10
            d.p (x, y).y := sind (y)
            d.p (x, y).z := y / 10
        end for
    end for
    for x : -8 .. 9 by 2
        for y : -9 .. 9
            d.p (x, y).x := x / 10
            d.p (x, y).y := y / 10
            d.p (x, y).z := 0
        end for
    end for
end LoadGraphX

proc RotateFormula (var p1, p2 : real, a : real)
    var p1T := p1 * cosd (a) + p2 * sind (a)
    var p2T := p2 * cosd (a) - p1 * sind (a)
    p1 := p1T
    p2 := p2T
end RotateFormula

proc Rotate (x, y, z : real)
    for n : 1 .. 2
        for x1 : -9 .. 9
            for y1 : -9 .. 9
                RotateFormula (Shape (n).p (x1, y1).x, Shape (n).p (x1, y1).z, x)
                RotateFormula (Shape (n).p (x1, y1).y, Shape (n).p (x1, y1).z, y)
                RotateFormula (Shape (n).p (x1, y1).x, Shape (n).p (x1, y1).y, z)
            end for
        end for
    end for
    for x1 : 1 .. 3
        for y1 : 1 .. 3
            for z1 : 1 .. 3
                RotateFormula (g (x1, y1, z1).x, g (x1, y1, z1).z, x)
                RotateFormula (g (x1, y1, z1).y, g (x1, y1, z1).z, y)
                RotateFormula (g (x1, y1, z1).x, g (x1, y1, z1).y, z)
            end for
        end for
    end for
end Rotate

proc DrawObject (d : points, x, y, z : int)
    var x2, y2 : int
    for x1 : -9 .. 9
        for y1 : -9 .. 9
            x2 := round ((g (x, y, z).x * gridScale) + (d.p (x1, y1).x * objectScale) + 300)
            y2 := round ((g (x, y, z).y * gridScale) + (d.p (x1, y1).y * objectScale) + 300)
            Draw.Dot (x2, y2, RGB.AddColour (x / 3, y / 3, z / 3))
        end for
    end for
end DrawObject

proc DrawGrid
    cls
    for x : 1 .. 3
        for y : 1 .. 3
            for z : 1 .. 3
                if g (x, y, z).value ~= 0 then
                    DrawObject (Shape (g (x, y, z).value), x, y, z)
                end if
            end for
        end for
    end for
    View.Update
end DrawGrid

LoadGraphX (Shape (1))
LoadGraphO (Shape (2))
Mouse.Where (cm, sm, bm)
loop
    Mouse.Where (xm, ym, bm)
    DrawGrid
    Rotate (xm - cm, ym - sm, bm * 10)
    cm := xm
    sm := ym
    exit when hasch
end loop
A.J




PostPosted: Sat Jan 24, 2009 10:58 am   Post subject: RE:Tic Tac Toe with 3D!

cool, I see you have taken my advice Wink

Good job, you are heading in the right direction. But just one small thing though : don't make the cube move as your mouse moves...that'll make it hard for the player to choose a position for his piece.
The_Bean




PostPosted: Sat Jan 24, 2009 12:35 pm   Post subject: Re: Tic Tac Toe with 3D!

The rotating with the mouse was just for show, I've already remapped the rotation of the x/o's and the grid to the keyboard.

I'm thinking of having a secondary grid showing the 27 squares, which you can click on. Clicking on the cube would be hard to get the center box.
Also when you hover over them they will highlight on the cube to show where that is.
Insectoid




PostPosted: Sat Jan 24, 2009 1:10 pm   Post subject: RE:Tic Tac Toe with 3D!

Only idea I have for checking wins is to have multiple nested for loops (x, y, z) for the horizontal/vertical wins and do that several times, with slight modification of the condition to find a win.
I'm sure some will have to be hard-coded.
The_Bean




PostPosted: Sun Jan 25, 2009 12:36 am   Post subject: Re: Tic Tac Toe with 3D!

I'm pretty sure i got the detection working, I can only think of 49 ways to win. Because of how easy it is to win, I went with the idea that you get a point for each line of 3 you make. The winner is the player with the most points when the board is full.

Controls
A,D-rotate board left/right
W,X-rotate board up/down
S-Spin board
F,H-rotate objects left/right
T,B-rotate objects up/down
G-Spin Objects
Use the lower left grids to pick your move.

Not done yet, but the inner workings are complete. Just looks and feel left to go.
Download file this time since I don't think I should post 231 lines.



3D_Tic-Tac-Toe.t
 Description:

Download
 Filename:  3D_Tic-Tac-Toe.t
 Filesize:  9.23 KB
 Downloaded:  163 Time(s)

A.J




PostPosted: Sun Jan 25, 2009 11:51 am   Post subject: Re: Tic Tac Toe with 3D!

it is getting along pretty well Very Happy

2 small things that you might want to add :
1). The board (i.e. draw the cube where the pieces are held)
2). Try changing the gameplay so that if u get 1 3-in-a-row, u win the game (although tat might be a little rigged for player 1...)

apart from that, very good Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: