Skip to content

Instantly share code, notes, and snippets.

Created Nov 10, 2012
Embed
What would you like to do?
Checkers
--# CheckeredBoard
CheckeredBoard = class()
function CheckeredBoard:init()
    -- you can accept and set parameters here
    self.c1 = c1                    -- color 1
    self.c2 = c2                    -- color 2
    self.x1 = 0                     -- x variable position of c1 colored checker cells
    self.y1 = 0                     -- y variable position of c1 colored checker cells
    self.cell = 1                   -- board cell counter
    self.size = size or WIDTH/8     -- set size of each square; default is WIDTH/8
end
function CheckeredBoard:draw()
    -- Codea does not automatically call this method
    -- Using rectMode() function set origin of squares to be center, since default is CORNER
    rectMode(CENTER)
    zLevel(-1)
    
    -- Draw first row
    fill(113, 91, 53, 255)
    rect(self.size/2, self.size/2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8, self.size/2, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2, self.size, self.size)
    
    -- Draw second row
    fill(209, 151, 72, 255)
    rect(self.size/2, self.size/2 + WIDTH/8, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8, self.size, self.size)
    
    -- Draw third row
    fill(113, 91, 53, 255)
    rect(self.size/2, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8 * 2, self.size, self.size)
    
    -- Draw fourth row
    fill(209, 151, 72, 255)
    rect(self.size/2, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8 * 3, self.size, self.size)
    
    -- Draw fifth row
    fill(113, 91, 53, 255)
    rect(self.size/2, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8 * 4, self.size, self.size)
    
    -- Draw sixth row
    fill(209, 151, 72, 255)
    rect(self.size/2, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8 * 5, self.size, self.size)
    
    -- Draw seventh row
    fill(113, 91, 53, 255)
    rect(self.size/2, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8 * 6, self.size, self.size)
    
    -- Draw eighth row
    fill(209, 151, 72, 255)
    rect(self.size/2, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 2, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 3, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 4, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 5, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(209, 151, 72, 255)
    rect(self.size/2 + WIDTH/8 * 6, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    fill(113, 91, 53, 255)
    rect(self.size/2 + WIDTH/8 * 7, self.size/2 + WIDTH/8 * 7, self.size, self.size)
    --]]
end
-- Checkers
--# Main
-- Use this function to perform your initial setup
function setup()
    print("WIDTH = " .. WIDTH)
    print("HEIGHT = " .. HEIGHT)
    board = CheckeredBoard()
    -- Initiate positions of dark-colored pieces first
    -- First row pieces 
    piece4 = Piece(board.size/2, board.size/2, WIDTH/9, "red")
    piece3 = Piece(board.size/2 + WIDTH/8 * 2, board.size/2, WIDTH/9, "red")
    piece2 = Piece(board.size/2 + WIDTH/8 * 4, board.size/2, WIDTH/9, "red")
    piece1 = Piece(board.size/2 + WIDTH/8 * 6, board.size/2, WIDTH/9, "red")
    
    -- Second row pieces
    piece8 = Piece(board.size/2 + WIDTH/8, board.size/2 + WIDTH/8, WIDTH/9, "red")
    piece7 = Piece(board.size/2 + WIDTH/8 * 3, board.size/2 + WIDTH/8, WIDTH/9, "red")
    piece6 = Piece(board.size/2 + WIDTH/8 * 5, board.size/2 + WIDTH/8, WIDTH/9, "red")
    piece5 = Piece(board.size/2 + WIDTH/8 * 7, board.size/2 + WIDTH/8, WIDTH/9, "red")
    
    -- Third row pieces
    piece12 = Piece(board.size/2, board.size/2 + WIDTH/8 * 2, WIDTH/9, "red")
    piece11 = Piece(board.size/2 + WIDTH/8 * 2, board.size/2 + WIDTH/8 * 2, WIDTH/9, "red")
    piece10 = Piece(board.size/2 + WIDTH/8 * 4, board.size/2 + WIDTH/8 * 2, WIDTH/9, "red")
    piece9 = Piece(board.size/2 + WIDTH/8 * 6, board.size/2 + WIDTH/8 * 2, WIDTH/9, "red")
    
    -- Initiate positions of light-colored pieces
    -- First row pieces
    piece32 = Piece(board.size/2 + WIDTH/8, board.size/2 + WIDTH/8 * 7)
    piece31 = Piece(board.size/2 + WIDTH/8 * 3, board.size/2 + WIDTH/8 * 7)
    piece30 = Piece(board.size/2 + WIDTH/8 * 5, board.size/2 + WIDTH/8 * 7)
    piece29 = Piece(board.size/2 + WIDTH/8 * 7, board.size/2 + WIDTH/8 * 7)
    
    -- Second row pieces
    piece28 = Piece(board.size/2, board.size/2 + WIDTH/8 * 6)
    piece27 = Piece(board.size/2 + WIDTH/8 * 2, board.size/2 + WIDTH/8 * 6)
    piece26 = Piece(board.size/2 + WIDTH/8 * 4, board.size/2 + WIDTH/8 * 6)
    piece25 = Piece(board.size/2 + WIDTH/8 * 6, board.size/2 + WIDTH/8 * 6)
    
    -- Third row pieces
    piece24 = Piece(board.size/2 + WIDTH/8, board.size/2 + WIDTH/8 * 5)
    piece23 = Piece(board.size/2 + WIDTH/8 * 3, board.size/2 + WIDTH/8 * 5)
    piece22 = Piece(board.size/2 + WIDTH/8 * 5, board.size/2 + WIDTH/8 * 5)
    piece21 = Piece(board.size/2 + WIDTH/8 * 7, board.size/2 + WIDTH/8 * 5)
end
-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(40, 40, 50)
    board:draw()
    
    -- Draw dark-colored pieces first
    -- First row pieces
    piece4:draw()
    piece3:draw()
    piece2:draw()
    piece1:draw()
    
    -- Second row pieces
    piece8:draw()
    piece7:draw()
    piece6:draw()
    piece5:draw()
    
    -- Third row pieces
    piece12:draw()
    piece11:draw()
    piece10:draw()
    piece9:draw()
    
    -- Draw light-colored pieces
    -- First row pieces
    piece32:draw()
    piece31:draw()
    piece30:draw()
    piece29:draw()
    
    -- Second row pieces
    piece28:draw()
    piece27:draw()
    piece26:draw()
    piece25:draw()
    -- Third row pieces
    piece24:draw()
    piece23:draw()
    piece22:draw()
    piece21:draw()
end
function touched(touch)
    -- Handle touches of dark-colored pieces
    -- First row pieces
    piece4:touched(touch)
    piece3:touched(touch)
    piece2:touched(touch)
    piece1:touched(touch)
    
    -- Second row pieces
    piece8:touched(touch)
    piece7:touched(touch)
    piece6:touched(touch)
    piece5:touched(touch)
    
    -- Third row pieces
    piece12:touched(touch)
    piece11:touched(touch)
    piece10:touched(touch)
    piece9:touched(touch)
end
--# Piece
Piece = class()
function Piece:init(x, y, d, c)
    -- Each player starts with 12 pieces; a total of 24 pieces are present at the beginning.
    -- Each piece occupies a dark square.
    self.x = x                                -- x position of piece
    self.y = y                                -- y position of piece
    self.d = d or WIDTH/9                     -- diamaeter of piece
    self.c = c                                -- color of piece
    self.z = 0                                -- z level of piece; default is 2 (1 > than board)
    self.touches = {}                         -- table variable to hold touches info
    self.state = "static"                     -- is the piece touched or not?
end
function Piece:draw()
    -- Draw player 1's dark-colored pieces
    -- Draw pieces in first row from the bottom
    strokeWidth(3)
    stroke(0, 0, 0, 255)
    
    -- Color the "man"
    if self.c == "red" then
        fill(191, 52, 45, 255)
    else
        fill(255, 255, 255, 255)
    end
    
    -- Draw the "man"
    ellipse(self.x, self.y, self.d)
end
function Piece:touched(touch)
    -- Codea does not automatically call this method
    if touch.x >= self.x - self.d/2 and touch.x <= self.x + self.d/2 and
        touch.y >= self.y - self.d/2 and touch.y <= self.y + self.d/2 and
        touch.state == BEGAN then
        self.state = "touched"
    end
    if touch.x >= self.x - self.d/2 and touch.x <= self.x + self.d/2 and
        touch.y >= self.y - self.d/2 and touch.y <= self.y + self.d/2 and
        touch.state == MOVING and self.state == "touched" then
        self.x = touch.x
        self.y = touch.y
    end
    
    if touch.state == ENDED then
        self.state = "static"
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment