# My first major creation: checkers

So this is the first major creation I have made, checkers. There are only a few issues…

A. There is no king me function

B. if 2 checkers touch, they stick (which is good for jumping other checkers except black is on top no matter what)

C. No area to drag off checkers once jumped (refer to problem in B)

So basically don’t touch the checkers together and remember who gets knitted and you’re good!

``````--these are the placement of the red checkers

r1x=95
r1y=95
r2x=255
r2y=95
r3x=415
r3y=95
r4x=575
r4y=95
r5x=175
r5y=175
r6x=335
r6y=175
r7x=495
r7y=175
r8x=655
r8y=175
r9x=95
r9y=255
r10x=255
r10y=255
r11x=415
r11y=255
r12x=575
r12y=255

--this is the placement if the black checkers
b1x=175
b1y=655
b2x=335
b2y=655
b3x=495
b3y=655
b4x=655
b4y=655
b5x=95
b5y=575
b6x=255
b6y=575
b7x=415
b7y=575
b8x=575
b8y=575
b9x=175
b9y=495
b10x=335
b10y=495
b11x=495
b11y=495
b12x=655
b12y=495

--this function draws the board and checkers

function draw()
touch()
background(0, 0, 0, 255)
fill(255, 255, 255, 255)
--the game board
rect(55,55,80,80)
rect(215,55,80,80)
rect(375,55,80,80)
rect(535,55,80,80)
rect(135,135,80,80)
rect(295,135,80,80)
rect(455,135,80,80)
rect(615,135,80,80)
rect(55,215,80,80)
rect(215,215,80,80)
rect(375,215,80,80)
rect(535,215,80,80)
rect(135,295,80,80)
rect(295,295,80,80)
rect(455,295,80,80)
rect(615,295,80,80)
rect(55,375,80,80)
rect(215,375,80,80)
rect(375,375,80,80)
rect(535,375,80,80)
rect(135,455,80,80)
rect(295,455,80,80)
rect(455,455,80,80)
rect(615,455,80,80)
rect(55,535,80,80)
rect(215,535,80,80)
rect(375,535,80,80)
rect(535,535,80,80)
rect(135,615,80,80)
rect(295,615,80,80)
rect(455,615,80,80)
rect(615,615,80,80)
-- the checker pieces
touch()
--red checkers
fill(255, 0, 0, 255)
ellipse(r1x,r1y,50,50)
ellipse(r2x,r2y,50,50)
ellipse(r3x,r3y,50,50)
ellipse(r4x,r4y,50,50)
ellipse(r5x,r5y,50,50)
ellipse(r6x,r6y,50,50)
ellipse(r7x,r7y,50,50)
ellipse(r8x,r8y,50,50)
ellipse(r9x,r9y,50,50)
ellipse(r10x,r10y,50,50)
ellipse(r11x,r11y,50,50)
ellipse(r12x,r12y,50,50)
--black checkers
fill(0, 0, 0, 255)
ellipse(b1x,b1y,50,50)
ellipse(b2x,b2y,50,50)
ellipse(b3x,b3y,50,50)
ellipse(b4x,b4y,50,50)
ellipse(b5x,b5y,50,50)
ellipse(b6x,b6y,50,50)
ellipse(b7x,b7y,50,50)
ellipse(b8x,b8y,50,50)
ellipse(b9x,b9y,50,50)
ellipse(b10x,b10y,50,50)
ellipse(b11x,b11y,50,50)
ellipse(b12x,b12y,50,50)
end

--this function tracks the touch and then assigns current touch to the checker coordinates

function touch()
x=CurrentTouch.x
y=CurrentTouch.y
if CurrentTouch.state==BEGAN or CurrentTouch.state==MOVING then
if x>r1x-25 and x<r1x+25 and y>r1y-25 and y<r1y+25 then
r1x=CurrentTouch.x
r1y=CurrentTouch.y
end
if x>r2x-25 and x<r2x+25 and y>r2y-25 and y<r2y+25 then
r2x=CurrentTouch.x
r2y=CurrentTouch.y
end
if x>r3x-25 and x<r3x+25 and y>r3y-25 and y<r3y+25 then
r3x=CurrentTouch.x
r3y=CurrentTouch.y
end
if x>r4x-25 and x<r4x+25 and y>r4y-25 and y<r4y+25 then
r4x=CurrentTouch.x
r4y=CurrentTouch.y
end
if x>r5x-25 and x<r5x+25 and y>r5y-25 and y<r5y+25 then
r5x=CurrentTouch.x
r5y=CurrentTouch.y
end
if x>r6x-25 and x<r6x+25 and y>r6y-25 and y<r6y+25 then
r6x=CurrentTouch.x
r6y=CurrentTouch.y
end
if x>r7x-25 and x<r7x+25 and y>r7y-25 and y<r7y+25 then
r7x=CurrentTouch.x
r7y=CurrentTouch.y
end
if x>r8x-25 and x<r8x+25 and y>r8y-25 and y<r8y+25 then
r8x=CurrentTouch.x
r8y=CurrentTouch.y
end
if x>r9x-25 and x<r9x+25 and y>r9y-25 and y<r9y+25 then
r9x=CurrentTouch.x
r9y=CurrentTouch.y
end
if x>r10x-25 and x<r10x+25 and y>r10y-25 and y<r10y+25 then
r10x=CurrentTouch.x
r10y=CurrentTouch.y
end
if x>r11x-25 and x<r11x+25 and y>r11y-25 and y<r11y+25 then
r11x=CurrentTouch.x
r11y=CurrentTouch.y
end
if x>r12x-25 and x<r12x+25 and y>r12y-25 and y<r12y+25 then
r12x=CurrentTouch.x
r12y=CurrentTouch.y
end
if x>b1x-25 and x<b1x+25 and y>b1y-25 and y<b1y+25 then
b1x=CurrentTouch.x
b1y=CurrentTouch.y
end
if x>b2x-25 and x<b2x+25 and y>b2y-25 and y<b2y+25 then
b2x=CurrentTouch.x
b2y=CurrentTouch.y
end
if x>b3x-25 and x<b3x+25 and y>b3y-25 and y<b3y+25 then
b3x=CurrentTouch.x
b3y=CurrentTouch.y
end
if x>b4x-25 and x<b4x+25 and y>b4y-25 and y<b4y+25 then
b4x=CurrentTouch.x
b4y=CurrentTouch.y
end
if x>b5x-25 and x<b5x+25 and y>b5y-25 and y<b5y+25 then
b5x=CurrentTouch.x
b5y=CurrentTouch.y
end
if x>b6x-25 and x<b6x+25 and y>b6y-25 and y<b6y+25 then
b6x=CurrentTouch.x
b6y=CurrentTouch.y
end
if x>b7x-25 and x<b7x+25 and y>b7y-25 and y<b7y+25 then
b7x=CurrentTouch.x
b7y=CurrentTouch.y
end
if x>b8x-25 and x<b8x+25 and y>b8y-25 and y<b8y+25 then
b8x=CurrentTouch.x
b8y=CurrentTouch.y
end
if x>b9x-25 and x<b9x+25 and y>b9y-25 and y<b9y+25 then
b9x=CurrentTouch.x
b9y=CurrentTouch.y
end
if x>b10x-25 and x<b10x+25 and y>b10y-25 and y<b10y+25 then
b10x=CurrentTouch.x
b10y=CurrentTouch.y
end
if x>b11x-25 and x<b11x+25 and y>b11y-25 and y<b11y+25 then
b11x=CurrentTouch.x
b11y=CurrentTouch.y
end
if x>b12x-25 and x<b12x+25 and y>b12y-25 and y<b12y+25 then
b12x=CurrentTouch.x
b12y=CurrentTouch.y
end
end

if CurrentTouch.state==ENDED then
x=nil
y=nil
end
end
``````

Wow. Nice. Thoughtgiving it a space skin and putting it on my compitition?

Um…I guess it would take a little time but sure

This version is the best because it supports moving the pieces when no one is looking Ldsguy1 - I did something similar for the board. If you use Classes, I think you could greatly simplify your code and make it more readable.

Try something like this:

``````Squares = class()

function Squares:init(n,position,size)
self.n = n
self.position = position
end

function Squares:draw()
strokeWidth(2)
stroke(255,255,255,255)
fill(0,0,0,255)
rect(self.position.x,self.position.y,self.size.x,self.size.y)
end

function Squares:touched(touch)
-- put your touch stuff here
end

function setup()
mysquare = {}
local n = 1
for x=1,8 do
for y=0,7 do
mysquare[n] = n
mysquare[n] = Squares()
mysquare[n].position = vec2(WIDTH / 8 * x, HEIGHT / 8 * y)
mysquare[n].size = vec2(WIDTH / 8, HEIGHT / 8)
end

function draw()
background(0,0,0)
for n = 1, 81 do
mysquare[n]:draw()
end

``````

I am not at my iPad, so I might have missed something, but I think you can get the gist of how it works. If not, just ask!

Jim

Well - found my iPad again. Missing several end’s. Forgot to increment n in the setup. Ended up with a 7x8 grid. OK - no more winging it for me!

``````Squares = class()

function Squares:init(n,position,size)
self.n = n
self.position = position
end

function Squares:draw()
strokeWidth(2)
stroke(255,255,255,255)
fill(0,0,0,255)
rect(self.position.x,self.position.y,self.size.x,self.size.y)
end

function Squares:touched(touch)
-- put your touch stuff here
end

function setup()
mysquare = {}
local n = 1
for x=0,7 do
for y=0,7 do
mysquare[n] = n
mysquare[n] = Squares()
mysquare[n].position = vec2(WIDTH / 8 * x, HEIGHT / 8 * y)
mysquare[n].size = vec2(WIDTH / 8, HEIGHT / 8)
n=n+1
end
end
end

function draw()
background(0,0,0)
for n = 1, 64 do
mysquare[n]:draw()
end
end
``````

Also, 8*8 = 64, not 81!

You will obviously need to change the fillColor for half the squares.
Create a men class and spawn the 24 men, 12 of each color.

Whoa - I didn’t mean to totally rewrite your app!

Well I tried using multiple classes at first but it had issues, and with no code exporting it may be easier to have one class

You can have as many classes as you want in a single tab. I am just getting used to using Classes, and the more I use them, the more powerful I see them. YMMV, however. Oh…I see…I think. Well I submitted it to the contest so yay!

And since this my first major project in lua I am still learning, so thanks for the advice (though it didn’t make it into the submission)

Good luck with your submission! That’s the thing about programming - there are so many different ways to do the same thing.