Posted: Sun Feb 06, 2011 4:39 pm Post subject: Tic Tac Toe Game Problem
I am making a simple two player tic tac toe game. I am having problems with having the the game draw a X and O when they are supposed to. The game is not done yet - I still need to add syntax to check if players won. Just need someone to point our what I am doing wrong.
procedure game
var x, y, z :int var p1, p2 :int:=0 loop mousewhere(x, y, z) if z =1and x < 100and y < 100then if p2 < p1 then
drawo (0, 0)
p2:=p2+1 else
drawx (0, 0)
p1:=p1+1 endif elsif z =1and x < 100and y < 200and y > 100then if p2 < p1 then
drawo (0, 1)
p2:=p2+1 else
drawx (0, 1)
p1:=p1+1 endif elsif z =1and x < 100and y < 300and y > 200then if p2 < p1 then
drawo (0, 2)
p2:=p2+1 else
drawx (0, 2)
p1:=p1+1 endif elsif z =1and x > 100and x < 200and y < 100then if p2 < p1 then
drawo (1, 0)
p2:=p2+1 else
drawx (1, 0)
p1:=p1+1 endif elsif z =1and x > 100and x < 200and y > 100and y < 200then if p2 < p1 then
drawo (1, 1)
p2:=p2+1 else
drawx (1, 1)
p1:=p1+1 endif elsif z =1and x > 100and x < 200and y > 200and y < 300then if p2 < p1 then
drawo (1, 2)
p2:=p2+1 else
drawx (1, 2)
p1:=p1+1 endif elsif z =1and x > 200and x < 300and y < 100then if p2 < p1 then
drawo (2, 0)
p2:=p2+1 else
drawx (2, 0)
p1:=p1+1 endif elsif z =1and x > 200and x < 300and y > 100and y < 200then if p2 < p1 then
drawo (2, 1)
p2:=p2+1 else
drawx (2, 1)
p1:=p1+1 endif elsif z =1and x > 200and x < 300and y > 200and y < 300then if p2 < p1 then
drawo (2, 2)
p2:=p2+1 else
drawx (2, 2)
p1:=p1+1 endif
endif
endloop
end game
drawgrid
game
Sponsor Sponsor
huskiesgoaler34
Posted: Sun Feb 06, 2011 5:05 pm Post subject: Re: Tic Tac Toe Game Problem
I am assuming that, after trying to play your game, you are trying to not draw both an x or an o each time the box is clicked. Right now, both are drawn on top of eachother. Am I right?
TokenHerbz
Posted: Sun Feb 06, 2011 5:13 pm Post subject: RE:Tic Tac Toe Game Problem
if you add a delay(60) in the loop you'll see that both arn't drawn the same time, however you need to make sure that the grid is "clear" before you can "draw" on it, so that you cant draw on it again...
you could use a boolean for that.
huskiesgoaler34
Posted: Sun Feb 06, 2011 5:15 pm Post subject: Re: Tic Tac Toe Game Problem
What I would do is set an x or an o to each player. So player1 is an o and player 2 is an x. If player one clicks a box, it draws a o. If player two clicks a box, then an x is drawn.
huskiesgoaler34
Posted: Sun Feb 06, 2011 5:16 pm Post subject: Re: Tic Tac Toe Game Problem
Maybe stick to token's idea. His is easier.
compstudent
Posted: Sun Feb 06, 2011 5:23 pm Post subject: Re: Tic Tac Toe Game Problem
I'm not quite sure I completely understand tokens idea. I have put in the delay and noticed the difference. Now I just need to make sure that another symbol isn't drawn if the box is already taken.
Thanks for the replies.
compstudent
Posted: Sun Feb 06, 2011 5:26 pm Post subject: Re: Tic Tac Toe Game Problem
Do you mean like a multi-dimensional array of boolean, and assign the variable true for the corresponding gird spot when it is drawn on.
compstudent
Posted: Sun Feb 06, 2011 6:05 pm Post subject: Re: Tic Tac Toe Game Problem
I have changed it and added variables, just for two places on the board so far. Now for when the first player clicks a spot it draws the correct symbol then, when clicked again nothing happens which is good. However when you click the next box, both symbols show up.
I'm not sure whats wrong.
Turing:
procedure game
var turn :boolean:=true var x, y, z :int var p1, p2 :int:=0 var board :array0.. 2, 0.. 2ofchar var full :array0.. 2, 0.. 2ofboolean:=init(false, false, false, false, false, false, false, false, false) loop mousewhere(x, y, z) if z =1and x < 100and y < 100and full (0, 0)=falsethen if turn =falsethen
drawo (0, 0)
turn :=true
full (0, 0):=true
board (0, 0):="o" endif if turn =truethen
drawx (0, 0)
turn :=false
full (0, 0):=true
board (0, 0):="x" endif elsif z =1and x < 100and y < 200and y > 100and full (0, 1)=falsethen if turn =falsethen
drawo (0, 1)
turn :=true
full (0, 1):=true
board (0, 1):="o" endif if turn =truethen
drawx (0, 1)
turn :=false
full (0, 1):=true
board (0, 1):="x" endif
Sponsor Sponsor
huskiesgoaler34
Posted: Sun Feb 06, 2011 6:20 pm Post subject: Re: Tic Tac Toe Game Problem
compstudent @ Sun Feb 06, 2011 6:05 pm wrote:
I have changed it and added variables, just for two places on the board so far. Now for when the first player clicks a spot it draws the correct symbol then, when clicked again nothing happens which is good. However when you click the next box, both symbols show up.
I'm not sure whats wrong.
Turing:
procedure game
var turn :boolean:=true var x, y, z :int var p1, p2 :int:=0 var board :array0.. 2, 0.. 2ofchar var full :array0.. 2, 0.. 2ofboolean:=init(false, false, false, false, false, false, false, false, false) loop mousewhere(x, y, z) if z =1and x < 100and y < 100and full (0, 0)=falsethen if turn =falsethen
drawo (0, 0)
turn :=true
full (0, 0):=true
board (0, 0):="o" endif % if turn = true then % drawx (0, 0)
%turn := false %full (0, 0) := true %board (0, 0) := "x" %end if elsif z =1and x < 100and y < 200and y > 100and full (0, 1)=falsethen if turn =falsethen
drawo (0, 1)
turn :=true
full (0, 1):=true
board (0, 1):="o" endif if turn =truethen
drawx (0, 1)
turn :=false
full (0, 1):=true
board (0, 1):="x" endif
I think I found the problem. The commented part was the problem. After you click one box, it drew the correct symbol but when you clicked another box, it draws both symbols. Now, I made it so that when one of the boxes is clicked, the x appears. When you click another box, a circle appears.
Note: It only works for the first two boxes on the first column.
compstudent
Posted: Sun Feb 06, 2011 6:27 pm Post subject: Re: Tic Tac Toe Game Problem
I notice that after you click a box an x will first appear, then the next box an O. But if i click the lower left box an x will appear, then if i click the one directly above it, both appear.
compstudent
Posted: Sun Feb 06, 2011 7:00 pm Post subject: Re: Tic Tac Toe Game Problem
Nevermind - I have fixed that problem now. Just needed to add
Turing:
and full(0,0)=falsethen
to each of the embedded if statements. Thanks for the help, I'll probably be back with more problems.
RandomLetters
Posted: Sun Feb 06, 2011 7:44 pm Post subject: RE:Tic Tac Toe Game Problem
I think it would help a lot if you looked into else and elsif statements for the inner conditional instead of adding more conditions.
(edit for clarity)
compstudent
Posted: Sun Feb 06, 2011 8:02 pm Post subject: Re: Tic Tac Toe Game Problem
I have another problem. I have worked it out so that each symbol is drawn when it is supposed to be and so that there aren't two in the same square. Now my problem is that there seems to be a delay in drawing them. For example if I click on it will draw. Next one nothing. Next one I click both show up: the one before where I clicked and the new one. I noticed though, if I click pause after one doesn't show up, it suddenly appears.
I am not sure what is the problem.
Turing:
%TIC TAC TOE GAME
setscreen("graphics:300;310") var turn :boolean:=true var x, y, z :int var p1, p2 :int:=0 var board :array0.. 2, 0.. 2ofchar var full :array0.. 2, 0.. 2ofboolean:=init(false, false, false, false, false, false, false, false, false) var name1, name2 :string
procedure drawx (spotx, spoty :int) var x1 := spotx *100 var y1 := spoty *100 var x2 :=(spotx *100) + 100 var y2 :=(spoty *100) + 100 drawline(x1, y1, x2, y2, red) drawline(x1, y1 + 100, x2, y2 - 100, red) end drawx
procedure start
put"Enter player ones name" get name1
put"Enter player twos name" get name2
cls put name1, " is x." put name2, " is o." put name1, " starts." put"To play click on the square you want to put your symbol" locate(10, 10) put"The game starts in: " delay(3000) cls locate(10, 20) put"5" delay(1000) cls locate(10, 20) put"4" delay(1000) cls locate(10, 20) put"3" delay(1000) cls locate(10, 20) put"2" delay(1000) cls locate(10, 20) put"1" delay(1000) cls locate(10, 20) put"Go" delay(500) cls end start
procedure drawo (midx, midy :int) var x1 :=(midx *100) + 50 var y1 :=(midy *100) + 50 var rad :=50
loop mousewhere(x, y, z) if z =1and x < 100and y < 100and full (0, 0)=falsethen if turn =falseand full (0, 0)=falsethen
drawo (0, 0)
turn :=true
full (0, 0):=true
board (0, 0):="o" endif if turn =trueand full (0, 0)=falsethen
drawx (0, 0)
turn :=false
full (0, 0):=true
board (0, 0):="x" endif
elsif z =1and x < 100and y < 200and y > 100and full (0, 1)=falsethen if turn =falseand full (0, 1)=falsethen
drawo (0, 1)
turn :=true
full (0, 1):=true
board (0, 1):="o" endif if turn =trueand full (0, 1)=falsethen
drawx (0, 1)
turn :=false
full (0, 1):=true
board (0, 1):="x" endif elsif z =1and x < 100and y < 300and y > 200and full (0, 2)=falsethen if turn =falseand full (0, 2)=falsethen
drawo (0, 2)
turn :=true
full (0, 2):=true
board (0, 2):="o" endif if turn =trueand full (0, 2)=falsethen
drawx (0, 2)
turn :=false
full (0, 2):=true
board (0, 2):="x" endif elsif z =1and x > 100and x < 200and y < 100then if turn =falseand full (1, 0)=falsethen
drawo (1, 0)
turn :=true
full (1, 0):=true
board (1, 0):="o" endif if turn =trueand full (1, 0)=falsethen
drawx (1, 0)
turn :=false
full (1, 0):=true
board (1, 0):="x" endif elsif z =1and x > 100and x < 200and y > 100and y < 200then if turn =falseand full (1, 1)=falsethen
drawo (1, 1)
turn :=true
full (1, 1):=true
board (1, 1):="o" endif if turn =trueand full (1, 1)=falsethen
drawx (1, 1)
turn :=false
full (1, 1):=true
board (1, 1):="x" endif elsif z =1and x > 100and x < 200and y > 200and y < 300then if turn =falseand full (1, 2)=falsethen
drawo (1, 2)
turn :=true
full (1, 2):=true
board (1, 2):="o" endif if turn =trueand full (1, 2)=falsethen
drawx (1, 2)
turn :=false
full (1, 2):=true
board (1, 2):="x" endif elsif z =1and x > 200and x < 300and y < 100then if turn =falseand full (2, 0)=falsethen
drawo (2, 0)
turn :=true
full (2, 0):=true
board (2, 0):="o" endif if turn =trueand full (2, 0)=falsethen
drawx (2, 0)
turn :=false
full (2, 0):=true
board (2, 0):="x" endif elsif z =1and x > 200and x < 300and y > 100and y < 200then if turn =falseand full (2, 1)=falsethen
drawo (2, 1)
turn :=true
full (2, 1):=true
board (2, 1):="o" endif if turn =trueand full (2, 1)=falsethen
drawx (2, 1)
turn :=false
full (2, 1):=true
board (2, 1):="x" endif elsif z =1and x > 200and x < 300and y > 200and y < 300then if turn =falseand full (2, 2)=falsethen
drawo (2, 2)
turn :=true
full (2, 2):=true
board (2, 2):="o" endif if turn =trueand full (2, 2)=falsethen
drawx (2, 2)
turn :=false
full (2, 2):=true
board (2, 2):="x" endif
endif
endloop
end game
drawgrid
game
Note: Not complete. Yet to add prompts and checking to see if a player won.
huskiesgoaler34
Posted: Sun Feb 06, 2011 8:20 pm Post subject: Re: Tic Tac Toe Game Problem
It works perfectly for me. I don't see a problem. The correct symbol is drawn without any delays. No bugs. I have tested it out and it works fine.
compstudent
Posted: Sun Feb 06, 2011 8:32 pm Post subject: Re: Tic Tac Toe Game Problem
Thanks, I guess it is just my computer, since I just tried it on another and it worked fine.