Computer Science Canada Help tic-tac-toe input same same coordinate |
Author: | why1234 [ Fri Jun 04, 2010 9:37 am ] |
Post subject: | Help tic-tac-toe input same same coordinate |
I can't find a way to make the X and the O not on the same spot This is the code setscreen ("graphics:500;325,title: Tic-Tac-Toe,nobuttonbar") %Variables var squ : array 1 .. 3, 1 .. 3 of string var turn, winx, winy, a, b, c, d, e, f, g, h, j, xs, ys, fill, start : int := 0 %Scorex and scorey is used to calculate the score each time one of them wins var scorex, scorey : int := 0 %This asks if you want to restart the game if there is or no winner var again : string %This sets the font of all the texts shown var font1 : int font1 := Font.New ("serif:12") %Turn is used to place the symbol in the location %The alphabet is used to decide how which player is going to win %Fill is used if there is a draw and there is no winner %winx and winy is used to make the game restarts if there is a winner %=================================================================================== %Intro put "Welcome to the Tic-Tac-Toe game!" put "State the number from 1 to 9 to tell where your symbol goes." put "You must get a row/column/diagonal of your symbol" put "Ex: 1 is at the bottom left, 9 is at the top right and 5 is in the middle" put "X goes first, then O goes next" put "" put "Press any key to start..." %This is to allow to go to the next phase var continue : string (1) getch (continue) %Clear the screen cls %Set up the drawing for i : 1 .. 2 squ (1 + i, 1 + i) := "0" end for squ (1, 1) := "0" %This helps draw the lines for Tic-Tac-Toe procedure draw () Draw.ThickLine (0, maxy - 50, 150, maxy - 50, 3, 17) Draw.ThickLine (0, maxy - 100, 150, maxy - 100, 3, 17) Draw.ThickLine (100, maxy, 100, maxy - 150, 3, 17) Draw.ThickLine (50, maxy, 50, maxy - 150, 3, 17) end draw %=================================================================================== %This set the code for the symbol X procedure X () locatexy (25, 100) get turn %Draws the X on the top left if turn = 7 then squ (3, 1) := "1" if squ (3, 1) = "1" then Draw.ThickLine (0, maxy, 50, maxy - 50, 2, 17) Draw.ThickLine (0, maxy - 50, 50, maxy, 2, 17) a += 1 fill += 1 end if %Draws the X on the top middle elsif turn = 8 then squ (3, 2) := "1" if squ (3, 2) = "1" then Draw.ThickLine (50, maxy, 100, maxy - 50, 2, 17) Draw.ThickLine (50, maxy - 50, 100, maxy, 2, 17) b += 1 fill += 1 end if %Draws the X on the top right elsif turn = 9 then squ (3, 3) := "1" if squ (3, 3) = "1" then Draw.ThickLine (100, maxy, 150, maxy - 50, 2, 17) Draw.ThickLine (100, maxy - 50, 150, maxy, 2, 17) c += 1 fill += 1 end if %Draws the X on the middle left elsif turn = 4 then squ (2, 1) := "1" if squ (2, 1) = "1" then Draw.ThickLine (0, maxy - 50, 50, maxy - 100, 2, 17) Draw.ThickLine (0, maxy - 100, 50, maxy - 50, 2, 17) d += 1 fill += 1 end if %Draws the X on the centre elsif turn = 5 then squ (2, 2) := "1" if squ (2, 2) = "1" then Draw.ThickLine (50, maxy - 50, 100, maxy - 100, 2, 17) Draw.ThickLine (50, maxy - 100, 100, maxy - 50, 2, 17) e += 1 fill += 1 end if %Draws the X on the middle right elsif turn = 6 then squ (2, 3) := "1" if squ (2, 3) = "1" then Draw.ThickLine (100, maxy - 50, 150, maxy - 100, 2, 17) Draw.ThickLine (100, maxy - 100, 150, maxy - 50, 2, 17) f += 1 fill += 1 end if %Draws the X on the bottom left elsif turn = 1 then squ (1, 1) := "1" if squ (1, 1) = "1" then Draw.ThickLine (0, maxy - 100, 50, maxy - 150, 2, 17) Draw.ThickLine (0, maxy - 150, 50, maxy - 100, 2, 17) g += 1 fill += 1 end if %Draws the X on the bottom middle elsif turn = 2 then squ (1, 2) := "1" if squ (1, 2) = "1" then Draw.ThickLine (50, maxy - 100, 100, maxy - 150, 2, 17) Draw.ThickLine (50, maxy - 150, 100, maxy - 100, 2, 17) h += 1 fill += 1 end if %Draws the X on the bottom right elsif turn = 3 then squ (1, 3) := "1" if squ (1, 3) = "1" then Draw.ThickLine (100, maxy - 100, 150, maxy - 150, 2, 17) Draw.ThickLine (100, maxy - 150, 150, maxy - 100, 2, 17) j += 1 fill += 1 end if end if end X %=================================================================================== %This set the code for the symbol O procedure O () locatexy (25, 100) get turn %Draws the O on the top left if turn = 7 then squ (3, 1) := "2" if squ (3, 1) = "2" then drawoval (25, maxy - 25, 25, 25, 17) drawoval (25, maxy - 25, 24, 24, 17) a += 4 fill += 1 end if %Draws the O on the top middle elsif turn = 8 then squ (3, 2) := "2" if squ (3, 2) = "2" then drawoval (75, maxy - 25, 25, 25, 17) drawoval (75, maxy - 25, 24, 24, 17) b += 4 fill += 1 end if %Draws the O on the top right elsif turn = 9 then squ (3, 3) := "2" if squ (3, 3) = "2" then drawoval (125, maxy - 25, 25, 25, 17) drawoval (125, maxy - 25, 24, 24, 17) c += 4 fill += 1 end if %Draws the O on the middle left elsif turn = 4 then squ (2, 1) := "2" if squ (2, 1) = "2" then drawoval (25, maxy - 75, 25, 25, 17) drawoval (25, maxy - 75, 24, 24, 17) d += 4 fill += 1 end if %Draws the O on the centre elsif turn = 5 then squ (2, 2) := "2" if squ (2, 2) = "2" then drawoval (75, maxy - 75, 25, 25, 17) drawoval (75, maxy - 75, 24, 24, 17) e += 4 fill += 1 end if %Draws the O on the middle right elsif turn = 6 then squ (2, 3) := "2" if squ (2, 3) = "2" then drawoval (125, maxy - 75, 25, 25, 17) drawoval (125, maxy - 75, 24, 24, 17) f += 4 fill += 1 end if %Draws the O on the bottom left elsif turn = 1 then squ (1, 1) := "2" if squ (1, 1) = "2" then drawoval (25, maxy - 125, 25, 25, 17) drawoval (25, maxy - 125, 24, 24, 17) g += 4 fill += 1 end if %Draws the O on the bottom middle elsif turn = 2 then squ (1, 2) := "2" if squ (1, 2) = "2" then drawoval (75, maxy - 125, 25, 25, 17) drawoval (75, maxy - 125, 24, 24, 17) h += 4 fill += 1 end if %Draws the O on the bottom right elsif turn = 3 then squ (1, 3) := "2" if squ (1, 3) = "2" then drawoval (125, maxy - 125, 25, 25, 17) drawoval (125, maxy - 125, 24, 24, 17) j += 4 fill += 1 end if end if end O %=================================================================================== %This is what happens when there is a winner process winner () Draw.ThickLine (275, maxy - 50, 350, maxy - 50, 2, 17) Draw.ThickLine (312, maxy - 25, 312, maxy - 75, 2, 17) Font.Draw ("X Wins", 260, maxy - 45, font1, 17) Font.Draw ("O Wins", 315, maxy - 45, font1, 17) locatexy (290, maxy - 65) put xs locatexy (330, maxy - 65) put ys end winner procedure win () loop %The bottom program is used if there is a draw draw () X () exit when a + b + c = 3 or d + e + f = 3 or g + h + j = 3 or a + d + g = 3 or b + e + h = 3 or c + j + f = 3 or a + e + j = 3 or c + e + g = 3 or fill = 9 O () %Fork winner () exit when a + b + c = 12 or d + e + f = 12 or g + h + j = 12 or a + d + g = 12 or b + e + h = 12 or c + j + f = 12 or a + e + j = 12 or c + e + g = 12 or fill = 9 end loop %This program is used if X"s wins if a + b + c = 3 or d + e + f = 3 or g + h + j = 3 or a + d + g = 3 or b + e + h = 3 or c + j + f = 3 or a + e + j = 3 or c + e + g = 3 then cls put "X Wins!!!" xs += 1 start := 1 scorex += 1 end if %This program is used if O's wins if a + b + c = 12 or d + e + f = 12 or g + h + j = 12 or a + d + g = 12 or b + e + h = 12 or c + j + f = 12 or a + e + j = 12 or c + e + g = 12 then cls put "O Wins!!!" ys += 1 start := 1 scorey += 1 end if end win win %=================================================================================== %This loop is used to ask you if you want to play again if there is a winner or a draw loop %This is used to keep track of the number of wins put "Score for X: " .. put scorex put "Score for O: " .. put scorey put "" %This setting asks you if you want to play again put "Play again, y/n" get again if again = "y" or again = "Y" then %If the answer is a yes, then all the variables are set back to zero and the game restarts a := 0 b := 0 c := 0 d := 0 e := 0 f := 0 g := 0 h := 0 j := 0 start := 0 fill := 0 cls win () elsif again = "n" or again = "N" then %If the answer is a no, then the screen clears and puts this message up cls put "Thanks for playing!!" put "" put "Score for X: " .. put scorex put "Score for O: " .. put scorey else %Everytime the answer is an invalid answer, the loop will end when the answer is valid loop put "Invalid answer!!" put "Play again, y/n" get again %This bottom code is the same thing as the code on top if again = "y" or again = "Y" then a := 0 b := 0 c := 0 d := 0 e := 0 f := 0 g := 0 h := 0 j := 0 start := 0 fill := 0 cls win () exit when again = "y" or again = "Y" or again = "n" or again = "N" elsif again = "n" or again = "N" then cls put "Thanks for playing!!" put "" put "Score for X: " .. put scorex put "Score for O: " .. put scorey exit when again = "y" or again = "Y" or again = "n" or again = "N" end if end loop end if exit when again = "n" or again = "N" end loop |
Author: | A.J [ Fri Jun 04, 2010 11:39 am ] |
Post subject: | Re: Help tic-tac-toe input same same coordinate |
Two things: 1). Using code tags by adding your code in between (code="turing")(/code) (but square brackets instead of parentheses). 2). You accidentally made two threads. But that's a mistake, so w/e. Well, we can't really look through your code and isolate your problem for you. However, I suggest that you have a flag to mark when a square has an 'X' or an 'O' placed on it to avoid another person going on the same square. |
Author: | why1234 [ Tue Jun 08, 2010 9:15 am ] |
Post subject: | Re: Help tic-tac-toe input same same coordinate |
sorry, i'm new in turing. Can you be more specific that even an idiot like me can understand? I still don't get what i should put in my program |
Author: | Cezna [ Tue Jun 08, 2010 11:05 am ] | ||||
Post subject: | Re: Help tic-tac-toe input same same coordinate | ||||
You need a boolean variable (ture or false values) that represents each square. An array would be best. So the declaration line would be something like:
Then, when a symbol is drawn in a square, you set the corresponding boolean variable to full, and you only let the user select a square that is not already full. So you would do something like this:
This loop will get turn, and then if the square isn't been used, it will exit the loop and go to the drawing part. If the square is being used, it will not exit, but will tell you that the square has been chosen already, and it will make you choose again. Also, instead of drawing every single X and O with it's own exact co-ordiantes, I would recommend making the co-ordinates relative to a certain point (such as the bottom of the current square). This will allow you to only have one draw procedure, and just change the points that it is being drawn relative to. If you don't know how to do this, I would be happy to explain it. |
Author: | why1234 [ Wed Jun 09, 2010 8:48 am ] |
Post subject: | Re: Help tic-tac-toe input same same coordinate |
thanks for your help can you explain that last part, i'm a bit confused on what i should change for my code? oh ya, where do i put the code you gave me? loop get turn exit when not sqaure_full (turn) put "That square has already been chosen" end loop |
Author: | A.J [ Wed Jun 09, 2010 10:59 am ] |
Post subject: | RE:Help tic-tac-toe input same same coordinate |
See, it doesn't work that way. We don't give you code so that you can merely paste it onto your program. We give you ideas on how to better your code and you actually program it in. Basically, everytime a person wants to make a move, check to make sure that no piece has previously been placed there. It seems like you have the variables a, b, c, ... to store whether a piece has been played there or not (i.e. 1 or 0). So when getting the input, just make sure that the position where the player is placing a move is a '0' and not a '1' (i.e. make sure that it is the first time someone is making a move). |
Author: | Cezna [ Wed Jun 09, 2010 5:31 pm ] |
Post subject: | RE:Help tic-tac-toe input same same coordinate |
Put that code where you currently have the get statement for turn (where they type what square they want. |
Author: | Insectoid [ Wed Jun 09, 2010 5:41 pm ] |
Post subject: | RE:Help tic-tac-toe input same same coordinate |
Cezna, please see the post above your own. |
Author: | Cezna [ Wed Jun 09, 2010 7:08 pm ] |
Post subject: | Re: Help tic-tac-toe input same same coordinate |
My bad... I would edit that out if I could, but since it has been replied to, and will simply draw his attention away from my embarrassing slip-up. ![]() LA LA LA LA LA LA LA LA LA LA !!! LOOK OVA' HERE !!! *jumps and waves hands over head* |
Author: | USEC_OFFICER [ Wed Jun 09, 2010 7:29 pm ] |
Post subject: | RE:Help tic-tac-toe input same same coordinate |
OMG WHAT IS IT!?!?!?! WHAT"S OVER HERE!?!?!?!?! (Does that help?) EDIT: AND WHAT'S OVER THERE!?!?!?! |
Author: | A.J [ Wed Jun 09, 2010 9:05 pm ] |
Post subject: | RE:Help tic-tac-toe input same same coordinate |
...well, Cezna, just make sure you lead the person to the right direction by helping them understand how it all works. When one merely copies code and tries to understand how that works, it doesn't help them as much. But yeah, we are straying a bit offtopic...make sure you know that this is still about why1234's question. |
Author: | why1234 [ Tue Jun 15, 2010 9:22 am ] |
Post subject: | Re: Help tic-tac-toe input same same coordinate |
Thanks for the help. I was able to finish and fix this problem with boolean. However, Since i haven't looked at it for some time, i forgot what some of the variables do. =( Can someone tell me what does the variable winx, and winy do? |