Page 1 of 1

Tic Tac Toe Board

Posted: Fri Jan 04, 2013 5:42 pm
by bozanas
Hey, guys, I need your help. I am a begginer I have to make Tic Tac Toe game, and I am having some problems with the board. I cant created the board. I tried a lot functions and I cant make one that will work allright. I created almost all functions that I need for the game, but I cant create the board. Can someone help with this?

Re: Tic Tac Toe Board

Posted: Fri Jan 04, 2013 6:18 pm
by Jasoco
Show us the code you've made so far.

Are you from the same class as Eyedea?

Re: Tic Tac Toe Board

Posted: Sat Jan 05, 2013 4:32 am
by BlackBulletIV
Pretty much what Jasoco said. Show us what you've tried, and then we might be able help.

Also, this kind of thing is better posted in the "Support and Development" subforum.

Re: Tic Tac Toe Board

Posted: Sat Jan 05, 2013 12:48 pm
by bozanas
Here is what I have. I hope that you will help me!

Main function

Code: Select all


require "menu"
require "cell"
function love.load()
	s={}
	medium= love.graphics.newFont(40)
	love.graphics.setBackgroundColor(200,150,100)
	gamestate="menu"
	button_spawn(20,100,"Start","start")
	button_spawn(20,250,"Quit", "quit")
end

function love.update()
end

function love.draw()

 if gamestate=="playing" then
	board()
 end
 
 if gamestate=="menu" then 
 button_draw()
 end
end

function love.mousepressed(x,y)
	if gamestate=="menu" then
	button_click(x,y)
	end
end
Check win function

Code: Select all

function check_win()
for i=1, 3 do
if b(i) ~= nil and b(i) == b(i+3) and b(i) == b(i+6) then
         winner = b(i)
elseif b(i-) ~= nil and b(i) == b(i+1) and b(i) == b(i+2) then
         winner = b(i)
end
end
if b(1) ~= nil and b(1) == b(5) and b(1) == b(9) then
      winner = b(1)
   elseif b(3) ~= nil and b(3) == b(5) and b(3) == b(7) then
      winner = b(3)
   end
if winner ~= nil then
      text.text = (winner and 'You win!' or 'Computer wins!')
      win = true
   end
   if win == false and moves == 9 then
      win = true
      text.text = 'Draw!'
   end
end
Cell click function

Code: Select all

function cell_click()

if win == false then
      if s.cell ~= true and s.cell ~= false then
         moves = moves + 1
         s.cell = move
         move = not move
         check_win() --checks, if someone wins
         if move == false and win == false then ai() end 
      end
   else --restart game if someone wins and player clicks on the field
      for i = 1, 9 do cells._c[i].cell = nil end
      text.text = ''
      move = true
      win = false
      moves = 0
   end
end

Re: Tic Tac Toe Board

Posted: Fri Jan 11, 2013 11:17 pm
by Zer0
rendering a board is something like

Code: Select all

for i = 1, 2 do
  love.graphics.line(i * size_of_one_square,0,i * size_of_one_square,3 * size_of_one_square)
  love.graphics.line(0,i * size_of_one_square,3 * size_of_one_square,i * size_of_one_square)
end
to make that but if you're wondering how to render the marks at the correct spot from an array from 1 to 9 i would suggest

Code: Select all

for i = 1, 9 do
  love.graphics.circle("line",math.mod(i - 0.5,3) * size_of_one_square,(math.ceil(i / 3) - 0.5) * size_of_one_square,size_of_one_square / 2,32)
end
for the correct rendering and how to check click you can

Code: Select all

local x = mouse_x / size_of_one_square
local y = mouse_y / size_of_one_square
local p = 3 * math.floor(y) + math.ceil(x)
where p is what index it have.

hope this helps