Posted: Sun Feb 06, 2011 8:45 pm Post subject: Re: Tic Tac Toe Game Problem
Your welcome. Post the code when your done with determining the winner and ending the game.
Sponsor Sponsor
TokenHerbz
Posted: Sun Feb 06, 2011 10:06 pm Post subject: RE:Tic Tac Toe Game Problem
you could use a 2D array for your "grid" to, and types are good at storing "multiple data" though i'd use a class myself.
compstudent
Posted: Mon Feb 07, 2011 7:15 pm Post subject: Re: Tic Tac Toe Game Problem
Here is the game now almost complete. The problems I am having now are that I need it to stop when a player one - you will notice it doesn't, and then to allow the players to play again. If I make a play again procedure i have to put it above the game procedure so that the game can recognize it, but then I cannot call the game procedure in the play again one.
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 var timoe:int:=0 var again:string:="Yes"
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
start
procedure drawo (midx, midy :int) var x1 :=(midx *100) + 50 var y1 :=(midy *100) + 50 var rad :=50
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"
check
endif if turn =trueand full (0, 0)=falsethen
drawx (0, 0)
turn :=false
full (0, 0):=true
board (0, 0):="x"
check
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"
check
endif if turn =trueand full (0, 1)=falsethen
drawx (0, 1)
turn :=false
full (0, 1):=true
board (0, 1):="x"
check
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"
check
endif if turn =trueand full (0, 2)=falsethen
drawx (0, 2)
turn :=false
full (0, 2):=true
board (0, 2):="x"
check
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"
check
endif if turn =trueand full (1, 0)=falsethen
drawx (1, 0)
turn :=false
full (1, 0):=true
board (1, 0):="x"
check
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"
check
endif if turn =trueand full (1, 1)=falsethen
drawx (1, 1)
turn :=false
full (1, 1):=true
board (1, 1):="x"
check
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"
check
endif if turn =trueand full (1, 2)=falsethen
drawx (1, 2)
turn :=false
full (1, 2):=true
board (1, 2):="x"
check
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"
check
endif if turn =trueand full (2, 0)=falsethen
drawx (2, 0)
turn :=false
full (2, 0):=true
board (2, 0):="x"
check
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"
check
endif if turn =trueand full (2, 1)=falsethen
drawx (2, 1)
turn :=false
full (2, 1):=true
board (2, 1):="x"
check
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"
check
endif if turn =trueand full (2, 2)=falsethen
drawx (2, 2)
turn :=false
full (2, 2):=true
board (2, 2):="x"
check
endif
endif
endloop
end game
drawgrid
game
ProgrammingFun
Posted: Mon Feb 07, 2011 9:47 pm Post subject: RE:Tic Tac Toe Game Problem
Have you heard of forward procedure?
It enables you to do the following:
Turing:
forwardprocedure B
proc A
put"Hello" %Call Procedure B end A
proc B
put"Bye" end B
It was something like the above...I do not completely remember...
DemonWasp
Posted: Tue Feb 08, 2011 8:08 am Post subject: RE:Tic Tac Toe Game Problem
Even if you do resolve the circular reference as ProgrammingFun says, you'll eventually run into stack overflow problems that way. Better to have a replay loop that exits if the user doesn't want to replay the game, and loops if they do.
TokenHerbz
Posted: Tue Feb 08, 2011 10:08 am Post subject: RE:Tic Tac Toe Game Problem
I Think the Best way for you (least work) to fix this problem would be to add a boolean Value to check for when the game is won.
So in your procedure Check ( I would rework this so its not so repetitive, And make this into a function. If a player has won it turns this and use that in your game loop to exit the look when this occurs..
After if you want, you could even reset your variables after the loop still inside the game loop.
Create another loop to cycle threw your two procedures to keep the game going.
TokenHerbz
Posted: Tue Feb 08, 2011 10:39 am Post subject: RE:Tic Tac Toe Game Problem
I went ahead and Did some re-working of your code to give you an example of some things your doing wrong. This is to help you better understand what to look for and how to problem solve things for the rest of your game and/or other programs your making.
The purpose of this check is obviously to see if the player has won the game. It's called like 16 times or so in the main loop, Which is absolutely not required. As you can see in your game loop we go to a first "if" statement. This goes step by step down the list until the end. You have "check" in every sub if statement inside that main one. All that is required is the check is in before the end if on the main if statement, Hell It doesn't even need to be inside this if statement at all. But if you wanted it inside there, it would have a correct place.
The thing about programming is even you retype a ton of your code, and the game still seems to function correctly, It's Bad to do it for several reasons, the main being it's hard to change around, and/or fix bugs. Very time consuming.
So First example to solving your question, Is how do we make this "check" work? Well I went ahead and redid that procedure into a Function, (which returns a value) and renamed this "Win_Game". Here is the source:
Turing:
fcn Game_Won :boolean if board (0, 0)= board (0, 1)and board (0, 1)= board (0, 2)and full (0, 0)=trueand full (0, 1)=trueand full (0, 2)=trueor%%Vertical check
board (1, 0)= board (1, 1)and board (1, 1)= board (1, 2)and full (1, 0)=trueand full (1, 1)=trueand full (1, 2)=trueor
board (2, 0)= board (2, 1)and board (2, 1)= board (2, 2)and full (2, 0)=trueand full (2, 1)=trueand full (2, 2)=trueor
board (0, 0)= board (1, 0)and board (1, 0)= board (2, 0)and full (0, 0)=trueand full (1, 0)=trueand full (2, 0)=trueor%%Horizontal check
board (0, 1)= board (1, 1)and board (1, 1)= board (2, 1)and full (0, 1)=trueand full (1, 1)=trueand full (2, 1)=trueor
board (0, 2)= board (1, 2)and board (1, 2)= board (2, 2)and full (0, 2)=trueand full (1, 2)=trueand full (2, 2)=trueor
board (0, 0)= board (1, 1)and board (1, 1)= board (2, 2)and full (0, 0)=trueand full (1, 1)=trueand full (2, 2)=trueor%%Diagnal check
board (0, 2)= board (1, 1)and board (1, 1)= board (2, 0)and full (0, 2)=trueand full (1, 1)=trueand full (2, 0)=truethen if turn =falsethen put"Congradulations ", name1, " you won!" elsif turn =truethen put"Congradulations ", name2, " you won!" endif Input.Pause%%to show the players who won resulttrue endif resultfalse end Game_Won
How I used this is just as important, I don't call this more then "ONCE" in the main loop, Here lets take a look and understand it!
Turing:
exitwhen Game_Won =true%%exit the game loop
So what i did was create a function that returns a true/false depending on circumstances, this case being if a player has won or not. I put this check right at the very end of the game procedure so how it goes is "start game -> player moves -> check if win -> if win exit else continue -> other player moves -> rinse and repeat.
Now when the Player does win, and we exit the game proc loop We are still in the procedure, so why not reset variables and clear the screen so we may play again!!
Turing:
%%Reset variables here if you want to for r :0.. 2 for c :0.. 2
board (r, c):=""
full (r, c):=false endfor endfor cls%%Clear the screen
I should also mention i've changed your Bored array to string, init values of blank ""x9 times.
Turing:
var board :array0.. 2, 0.. 2ofstring:=init("","","","","","","","","")
Now what have we fixed? We made it possible to check if the game has won or not, and leave the game according to that (FIXED function). We reset variables and clear the screen so may play again!! Awesome!!! Now for the final thing, we loop our procs!! and we are ready for endless tic tac toe!
Turing:
loop
drawgrid
game
endloop
*********************
*********************
So Now that you have your game working, YOUR not DONE yet... Oh noes, no no no!!
I show'd you something in your check proc, how to minimize all this "re-do-re-write codes"...
What you need to do now, Is re-write your proc game to minimize all that repeat codes. Just check how its supposed to work and when you fix/clean that up, post it here for some BITS!
Good luck !!! PS: After you touch your code up, you can add more features to your game very easily. Cheers!
compstudent
Posted: Tue Feb 08, 2011 1:53 pm Post subject: Re: Tic Tac Toe Game Problem
Thank you very much for you lengthy reply. It was VERY helpful.
Here is the game and i think it is finished.
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 var timoe :int:=0 var again :string:="Yes" var done :boolean:=false
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
procedure check
%Checks veritcal if board (0, 0)= board (0, 1)and board (0, 1)= board (0, 2)and full (0, 0)=trueand full (0, 1)=trueand full (0, 2)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true
endif if board (1, 0)= board (1, 1)and board (1, 1)= board (1, 2)and full (1, 0)=trueand full (1, 1)=trueand full (1, 2)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif if board (2, 0)= board (2, 1)and board (2, 1)= board (2, 2)and full (2, 0)=trueand full (2, 1)=trueand full (2, 2)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif %Checks Horizontal if board (0, 0)= board (1, 0)and board (1, 0)= board (2, 0)and full (0, 0)=trueand full (1, 0)=trueand full (2, 0)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif if board (0, 1)= board (1, 1)and board (1, 1)= board (2, 1)and full (0, 1)=trueand full (1, 1)=trueand full (2, 1)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif
if board (0, 2)= board (1, 2)and board (1, 2)= board (2, 2)and full (0, 2)=trueand full (1, 2)=trueand full (2, 2)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif
%checks diagonal if board (0, 0)= board (1, 1)and board (1, 1)= board (2, 2)and full (0, 0)=trueand full (1, 1)=trueand full (2, 2)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif
if board (0, 2)= board (1, 1)and board (1, 1)= board (2, 0)and full (0, 2)=trueand full (1, 1)=trueand full (2, 0)=truethen if turn =falsethen put"Congradulations", name1, "you won!" elsif turn =truethen put"Congradulations", name2, "you won!" endif
done :=true endif
end check
fcn Game_Draw :boolean if full (0, 0)= full (0, 1)and full (0, 1)= full (0, 2)and full (0, 2)= full (1, 0)and full (1, 0)= full (1, 1)and full (1, 1)= full (1, 2)and full (1, 2)= full (2, 0)and
full (2, 0)= full (2, 1)and full (2, 1)= full (2, 2)and full (2, 2)=truethen put"The Game is a Draw!" resulttrue
else resultfalse endif end Game_Draw
fcn Game_Won :boolean if board (0, 0)= board (0, 1)and board (0, 1)= board (0, 2)and full (0, 0)=trueand full (0, 1)=trueand full (0, 2)=trueor%%Vertical check
board (1, 0)= board (1, 1)and board (1, 1)= board (1, 2)and full (1, 0)=trueand full (1, 1)=trueand full (1, 2)=trueor
board (2, 0)= board (2, 1)and board (2, 1)= board (2, 2)and full (2, 0)=trueand full (2, 1)=trueand full (2, 2)=trueor
board (0, 0)= board (1, 0)and board (1, 0)= board (2, 0)and full (0, 0)=trueand full (1, 0)=trueand full (2, 0)=trueor%%Horizontal check
board (0, 1)= board (1, 1)and board (1, 1)= board (2, 1)and full (0, 1)=trueand full (1, 1)=trueand full (2, 1)=trueor
board (0, 2)= board (1, 2)and board (1, 2)= board (2, 2)and full (0, 2)=trueand full (1, 2)=trueand full (2, 2)=trueor
board (0, 0)= board (1, 1)and board (1, 1)= board (2, 2)and full (0, 0)=trueand full (1, 1)=trueand full (2, 2)=trueor%%Diagnal check
board (0, 2)= board (1, 1)and board (1, 1)= board (2, 0)and full (0, 2)=trueand full (1, 1)=trueand full (2, 0)=truethen if turn =falsethen put"Congradulations ", name1, " you won!" elsif turn =truethen put"Congradulations ", name2, " you won!" endif Input.Pause%%to show the players who won resulttrue endif resultfalse end Game_Won
procedure game
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
exitwhen Game_Won =trueor Game_Draw =true
endloop
end game
loop
start
drawgrid
game
cls if Game_Won =trueor Game_Draw =truethen for a :0.. 2 for b :0.. 2
full (a, b):=false
board (a, b):=" " endfor endfor endif put"Would you like to play again? Yes or No" get again
exitwhen again not= "Yes" endloop
I am now looking into adding an AI.
Thanks Again!
Sponsor Sponsor
TokenHerbz
Posted: Tue Feb 08, 2011 3:59 pm Post subject: RE:Tic Tac Toe Game Problem
No no no!! All wrong!! lol...
Please Re-read my post and try to understand what Iv'e done and try to fix your code to respond the knowledge in that post!
If you don't even try I won't be of assistance anymore!! Read it carefully!
TerranceN
Posted: Tue Feb 08, 2011 5:49 pm Post subject: Re: Tic Tac Toe Game Problem
@TokenHerbz: What did he do wrong? He is no longer using the check procedure (even though it is still in his code), and he can restart his game, the two main points of your post.
Anyway, I would recommend breaking that large if statement into smaller functions such as HorizontalCheck, VerticalCheck, DiagonalCheck, to make that complex boolean statement MUCH easier to read and understand.
The section of code where you handle the mouse clicking on a grid square could be refactored a lot. Instead of having the code to check the turn and whether the grid square is full in each block of code for each grid square, find the coordinates of the grid square clicked on and save them in variables, which you can use to check if the grid square is full in, like this
Turing:
var clickedTileX, clickedTileY :int:= -1
% figure out what tiles were clicked and store them in the variables above, then
Also, the method you use to find the clicked tile could be improved. The way you are currently doing it is what I would call the "Button" method. You treat everything as a button, with defined bounds you check when you click. This will work of course, but it will mean a more complicated way of managing all those buttons, or a lot of typing. Fortunately, since this is a grid with no spaces in between grid squares, we can just use integer division (the 'div' command in Turing) to divide the x-value of some point by the x-size of the grid squares, to find the x index of the tile that contains the point. The same thing can be done with Y values. Here is an example:
Turing:
type GridPoint:
record
x :int
y :int endrecord %
const gridTilesX :=3 const gridTilesY :=3
var selectedTile : GridPoint
selectedTile.x := -1
selectedTile.y := -1
var gridTileSizeX, gridTileSizeY :=100 var grid :array0..gridTilesX-1ofarray0..gridTilesY-1ofint var mouseX, mouseY, mouseButton :int:=0
fcn GetTileContainingPoint(pointX :int, pointY :int): GridPoint
var selected : GridPoint
Posted: Tue Feb 08, 2011 6:15 pm Post subject: Re: Tic Tac Toe Game Problem
Token, I understand that I should recode my game procedure to make it much shorter, faster and simpler. I would if I had the time, but at the moment I am happy that it works. I also copied the wrong version - new one had taken the check out.
Thank you for the reply Terrence - good to know for other projects (although will thankfully get to use Java from now on).
As for making an AI - from what I have seen people have just made long if statements for all the possible situations. Is that the best way to go or is there a simpler way.
Thanks
TokenHerbz
Posted: Tue Feb 08, 2011 6:42 pm Post subject: RE:Tic Tac Toe Game Problem
you could make 2 kinds of AI.
A very basic one that chooses randomly of the left over locations which may or may not "block" your win.
Or you could code a lengthier one, which will be impossible to beat, however you could add chance percent of hitting that spot or not etc.
Lots you could do with that.
compstudent
Posted: Tue Feb 08, 2011 11:14 pm Post subject: Re: Tic Tac Toe Game Problem
Could you please elaborate on how to go about making a lengthier one. How I am currently thinking about it would be to loop through to find any two in a row or column or diagonal and block that.
compstudent
Posted: Wed Feb 09, 2011 5:35 pm Post subject: Re: Tic Tac Toe Game Problem
Hi,
I am close to finished (I think). I have made an AI that just blocks the win. I am now having problems with the one player game play.
Turing:
procedure game1player
turn :=false loop
if turn =falsethen mousewhere(x, y, z) if z =1and x < 100and y < 100and full (0, 0)=falsethen