
-----------------------------------
TaKer
Wed Jun 16, 2010 9:51 pm

Tic Tac Toe Game Problem
-----------------------------------
Can someone help me identify and fix the problem in my tic tac toe game. I cannot get the X's or O's to display, but i have the code. Also there is a problem when ending the game, i need help in doing this properly.
Here is my code

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    TIC TAC TOE GAME           %
%    By Arun Deodat             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
setscreen ("graphics:600;600")
var x, y, button : int % Variables for moushewhere%
var playerturn : boolean := true %Variable for determining turns%
var iCount : int := 0 %Variable for counting squares used for ties%
%Variables for top row, middle row and bottom row X's%
var botleftX, botmidX, botrightX, midleftX, midmidX, midrightX, topleftX, topmidX, toprightX : boolean := false
%Variables for top row, middle row and bottom row O's%
var botleftO, botmidO, botrightO, midleftO, midmidO, midrightO, topleftO, topmidO, toprightO : boolean := false
var iExit : int := 0 %Variable for exiting when there is a winner%
var player1, player2 : string := "" %Variables for the players name entered by user%
locate (20, 30)
put "X Starts First"
locate (1, 25)
put "Enter name of player 1"
locate (2, 25)
get player1
locate (3, 25)
put "Enter name of player 2"
locate (4, 25)
get player2
cls
procedure Gameboard %Draws the gameboard%
    drawline (200, 600, 200, 0, black) %Left Vertical Line%
    drawline (400, 600, 400, 0, black) %Right Vertical line%
    drawline (0, 200, 600, 200, black) %Bottom horizontal line%
    drawline (0, 400, 600, 400, black) %Top horizontal line%
end Gameboard
Gameboard %Displays the gameboard%
%Background colours for gameboard%
drawfillbox (0, 0, 200, 200, yellow)
drawfillbox (200, 0, 400, 200, brightred)
drawfillbox (400, 0, 600, 200, brightblue)
drawfillbox (0, 200, 200, 400, brightgreen)
drawfillbox (200, 200, 400, 400, brightmagenta)
drawfillbox (400, 200, 600, 400, brown)
drawfillbox (0, 400, 200, 600, brightcyan)
drawfillbox (200, 400, 400, 600, purple)
drawfillbox (400, 400, 600, 600, grey)
%All possibilities for X on the gameboard%
procedure BLX     %Drawing X in the bottom left box%
    drawline (0, 200, 200, 0, black)
    drawline (200, 200, 0, 0, black)
end BLX
procedure BMX %Drawing X in the bottom middle box%
    drawline (200, 200, 400, 0, black)
    drawline (400, 200, 200, 0, black)
end BMX
procedure BRX %Drawing X in the bottom right box%
    drawline (400, 200, 600, 0, black)
    drawline (600, 200, 400, 0, black)
end BRX
procedure MLX %Drawing X in the center left box%
    drawline (0, 400, 200, 200, black)
    drawline (200, 400, 0, 200, black)
end MLX
procedure MMX %Drawing X in the center middle box%
    drawline (200, 400, 400, 200, black)
    drawline (400, 400, 200, 200, black)
end MMX
procedure MRX %Drawing X in the center right box%
    drawline (400, 400, 600, 200, black)
    drawline (400, 200, 600, 400, black)
end MRX
procedure TLX %Drawing X in the top left box%
    drawline (0, 600, 200, 400, black)
    drawline (0, 400, 200, 600, black)
end TLX
procedure TMX %Drawinf X in the top middle box%
    drawline (200, 600, 400, 400, black)
    drawline (200, 400, 400, 600, black)
end TMX
procedure TRX %Drawing X in the top right box%
    drawline (400, 600, 600, 400, black)
    drawline (400, 400, 600, 600, black)
end TRX
%All possibilities for O on the gameboard%
procedure BLO %Drawing O in the bottom left box%
    drawoval (100, 100, 75, 75, black)
end BLO
procedure BMO %Drawing O in the bottom middle box%
    drawoval (300, 100, 75, 75, black)
end BMO
procedure BRO %Drawing O in the bottom right box%
    drawoval (500, 100, 75, 75, black)
end BRO
procedure MLO %Drawing O in the center left box%
    drawoval (100, 300, 75, 75, black)
end MLO
procedure MMO %Drawing O in the center middle box%
    drawoval (300, 300, 75, 75, black)
end MMO
procedure MRO %Drawing O in the center right box%
    drawoval (500, 300, 75, 75, black)
end MRO
procedure TLO %Drawing O in the top left box%
    drawoval (100, 500, 75, 75, black)
end TLO
procedure TMO %Drawing O in the top middle box%
    drawoval (300, 500, 75, 75, black)
end TMO
procedure TRO %Drawing O in the top right box%
    drawoval (500, 500, 75, 75, black)
end TRO
%Procedures for each box on the board being clicked on%
procedure BotLeftBox
    loop
	%Draws an X when it is player1 turn and the box has nothing in it%
	if playerturn = true and button = 1 and botleftX = false and botleftO = false then
	    BLX %Calls on the drawing of X in the bottom left box%
	    botleftX := true %Makes the box true, filling it with an X"
	    playerturn := not playerturn %Switches to the other player's turn%
	    iCount := iCount + 1 %Adds 1 to the counter to check for ties%
	    %Draws an O when it is player2 turn and the box has nothing in it%
	elsif playerturn = false and button = 1 and botleftX = false and botleftO = false then
	    BLO %Calls on the procedure of drawing O in the bottom  left box%
	    botleftO := true %Makes the box true, filling it with an O%
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when botleftX = true or botleftO = true %Exits the loop when one of these boxes true meaning it is filled%
    end loop
end BotLeftBox
procedure BotMidBox
    loop
	if playerturn = true and button = 1 and botmidX = false and botmidO = false then
	    BMX
	    botmidX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = true and button = 1 and botmidX = false and botmidO = false then
	    BMO
	    botmidO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when botmidX = true or botmidO = true
    end loop
end BotMidBox
procedure BotRightBox
    loop
	if playerturn = true and button = 1 and botrightX = false and botrightO = false then
	    BRX
	    botrightX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and botrightX = false and botrightO = false then
	    BRO
	    botrightO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when botrightX = true or botrightO = true
    end loop
end BotRightBox
procedure MidLeftBox
    loop
	if playerturn = true and button = 1 and midleftX = false and midleftO = false then
	    MLX
	    midleftX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and midleftX = false and midleftO = false then
	    MLO
	    midleftO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when midleftX = true or midleftO = true
    end loop
end MidLeftBox
procedure MidMidBox
    loop
	if playerturn = true and button = 1 and midmidX = false and midmidO = false then
	    MMX
	    midmidX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and midmidX = false and midmidO = false then
	    MMO
	    midmidO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when midmidX = true or midmidO = true
    end loop
end MidMidBox
procedure MidRightBox
    loop
	if playerturn = true and button = 1 and midrightX = false and midrightO = false then
	    MRX
	    midrightX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and midrightX = false and midrightO = false then
	    MRO
	    midrightO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when midrightX = true or midrightO = true
    end loop
end MidRightBox
procedure TopLeftBox
    loop
	if playerturn = true and button = 1 and topleftX = false and topleftO = false then
	    TLX
	    topleftX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and topleftX = false and topleftO = false then
	    TLO
	    topleftO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when topleftX = true or topleftO = true
    end loop
end TopLeftBox
procedure TopMidBox
    loop
	if playerturn = true and button = 1 and topmidX = false and topmidO = false then
	    TMX
	    topmidX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and topmidX = false and topmidO = false then
	    TMO
	    topmidO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when topmidX = true or topmidO = true
    end loop
end TopMidBox
procedure TopRightBox
    loop
	if playerturn = true and button = 1 and toprightX = false and toprightO = false then
	    TRX
	    toprightX := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	elsif playerturn = false and button = 1 and toprightX = false and toprightO = false then
	    TRO
	    toprightO := true
	    playerturn := not playerturn
	    iCount := iCount + 1
	end if
	exit when toprightX = true or toprightO = true
    end loop
end TopRightBox
%Identifies who's turn it is%
procedure TakeTurns
    if playerturn = true then
	locate (1, 25)
	put "It is ", player1, "'s turn"
    elsif playerturn = false then
	locate (1, 25)
	put "It is ", player2, "'s turn"
    end if
end TakeTurns
%All possibilities that X can win%
procedure XWinsLine
    loop
	%Horizontal win with 3 X's%
	if botleftX = true and botmidX = true and botrightX = true then
	    Draw.ThickLine (0, 100, 600, 100, 10, black) %Draws line to show 3 straight X's%
	    iExit := 1
	elsif topleftX = true and topmidX = true and toprightX = true then
	    Draw.ThickLine (0, 500, 600, 500, 10, black)
	    iExit := 1
	elsif midleftX = true and midmidX = true and midrightX = true then
	    Draw.ThickLine (0, 300, 600, 300, 10, black)
	    %Diagonal win with 3 X's%
	elsif botleftX = true and midmidX = true and toprightX = true then
	    Draw.ThickLine (0, 0, 600, 600, 10, black)
	elsif botrightX = true and midmidX = true and topleftX = true then
	    Draw.ThickLine (0, 600, 600, 0, 10, black)
	    %Vertical win with 3 X's"
	elsif botleftX = true and midleftX = true and topleftX = true then
	    Draw.ThickLine (100, 0, 100, 600, 10, black)
	elsif botmidX = true and midmidX = true and topmidX = true then
	    Draw.ThickLine (300, 0, 300, 600, 10, black)
	elsif botrightX = true and midrightX = true and toprightX = true then
	    Draw.ThickLine (500, 0, 500, 600, 10, black)
	end if
    end loop
end XWinsLine
procedure OWinsLine
    %Horizontal win with 3 O's%
    if botleftO = true and botmidO = true and botrightO = true then
	Draw.ThickLine (0, 100, 600, 100, 10, black) %Draws line to show 3 straight O's%
    elsif topleftO = true and topmidO = true and toprightO = true then
	Draw.ThickLine (0, 500, 600, 500, 10, black)
    elsif midleftO = true and midmidO = true and midrightO = true then
	Draw.ThickLine (0, 300, 600, 300, 10, black)
	%Diagonal win with 3 O's%
    elsif botleftO = true and midmidO = true and toprightO = true then
	Draw.ThickLine (0, 600, 600, 0, 10, black)
    elsif botrightO = true and midmidO = true and topleftO = true then
	Draw.ThickLine (0, 0, 600, 600, 10, black)
	%Vertical win with 3 O's%
    elsif botleftO = true and midleftO = true and topleftO = true then
	Draw.ThickLine (100, 0, 100, 600, 10, black)
    elsif botmidO = true and midmidO = true and topmidO = true then
	Draw.ThickLine (300, 0, 300, 600, 10, black)
    elsif botrightO = true and midrightO = true and toprightO = true then
	Draw.ThickLine (500, 0, 500, 600, 10, black)
    end if
end OWinsLine
procedure player1winner
    %Player1 wins with 3 X's horizontally%
    if botleftX = true and botmidX = true and botrightX = true or
	    midleftX = true and midmidX = true and midrightX = true or
	    topleftX = true and topmidX = true and toprightX = true or
	%Player1 wins with 3 X's vertically%
	    botleftX = true and midleftX = true and topleftX = true or
	    botmidX = true and midmidX = true and topmidX = true or
	    botrightX = true and midrightX = true and toprightX = true or
	%Player1 wins with 3 X's diagonally%
	    botleftX = true and midmidX = true and toprightX = true or
	    botrightX = true and midmidX = true and topleftX = true then
	iExit := 1
    end if
end player1winner
procedure player2winner
    %Player2 wins with 3 O's horizontally%
    if botleftO = true and botmidO = true and botrightO = true or
	    midleftO = true and midmidO = true and midrightO = true or
	    topleftO = true and topmidO = true and toprightO = true or
	%Player2 wins with 3 O's vertically%
	    botleftO = true and midleftO = true and topleftO = true or
	    botmidO = true and midmidO = true and topmidO = true or
	    botrightO = true and midrightO = true and toprightO = true or
	%Player2 wins with 3 O's diagonally%
	    botleftO = true and midmidO = true and toprightO = true or
	    botrightO = true and midmidO = true and topleftO = true then
	iExit := 2
    end if
end player2winner
%Mousewhere keeps track of where the location of the mouse is%
loop
    mousewhere (x, y, button)
    if button = 1 and x > 0 and x < 200 and y > 0 and y < 200 then
	BotLeftBox %Calls on the procedure that draws everything in the box%
	TakeTurns %Displays which player's turn it is%
    elsif button = 1 and x > 200 and x < 400 and y > 0 and y < 200 then
	BotMidBox
	TakeTurns
    elsif button = 1 and x > 400 and x < 600 and y > 0 and y < 200 then
	BotRightBox
	TakeTurns
    elsif button = 1 and x > 0 and x < 200 and y > 200 and y < 400 then
	MidLeftBox
	TakeTurns
    elsif button = 1 and x > 200 and x < 400 and y > 200 and y < 400 then
	MidMidBox
	TakeTurns
    elsif button = 1 and x > 400 and x < 600 and y > 200 and y < 400 then
	MidRightBox
	TakeTurns
    elsif button = 1 and x > 0 and x < 200 and y > 400 and y < 600 then
	TopLeftBox
	TakeTurns
    elsif button = 1 and x > 200 and x < 400 and y > 400 and y < 600 then
	TopMidBox
	TakeTurns
    elsif button = 1 and x > 400 and x < 600 and y > 400 and y < 600 then
	TopRightBox
	TakeTurns
	XWinsLine
	OWinsLine
	player1winner
	player2winner
    end if
    exit when iExit = 1 or iExit = 2 or iCount = 9 and iExit = not 1 and iExit = not 2
end loop

if iExit = 1 then
    locate (1, 25)
    put player1, " WINS!!!"
elsif iExit = 2 then
    locate (1, 25)
    put player2, " WINS!!!"
elsif iCount = 9 then
    locate (1, 25)
    put "TIE GAME"
end if


-----------------------------------
Cezna
Thu Jun 17, 2010 2:17 pm

RE:Tic Tac Toe Game Problem
-----------------------------------
They are displaying for me, the only problem is that as soon as either player goes in the top right corner, it stop working.

Also, I recommend using for loops and arrays instead of hard coding with all those boolean variables.

It will make your life much simpler and your code much easier to understand.

-----------------------------------
TaKer
Thu Jun 17, 2010 9:09 pm

Re: Tic Tac Toe Game Problem
-----------------------------------
Ok well i have fixed the issue for displaying the X and O
I have another issue. When a plyer wins it is supposed to say player1 wins or player2, but it doesnt. It is also supposed to say tie game if there are no more boxes left, but it does not. I made the code for this but it does not seem to work

-----------------------------------
Cezna
Fri Jun 18, 2010 11:18 am

RE:Tic Tac Toe Game Problem
-----------------------------------
You have to have it exit out of the main loop and go to some sort of "win" or "done" procedure, so that it avoids the part where it gets the slayers move.

-----------------------------------
TWizard
Sat Jul 03, 2010 5:36 pm

RE:Tic Tac Toe Game Problem
-----------------------------------
God there has to be another why to reduce the lines I think im going to make one of my own. Cause this is just crazy.
