Dennis Du
|
Posted: Fri Dec 26, 2014 4:39 pm Post subject: Who can hellp me to fix my code for Tic Tac Toe????? |
|
|
Who can help me to fix this code for the turing, I don't know how to make a clear thick lines and clear exit. THX~~
And this is my code:
setscreen ("graphics: 600; 600")
var player1, player2 : string %two players' name
var playerturn : boolean := true %determining turns
var iCount : int := 0 %counting squares used for ties
var botleftX, botmidX, botrightX, midleftX, midmidX, midrightX, topleftX, topmidX, toprightX : boolean := false
%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 %exiting when there is a winner
var x, y, button : int %Mouse.Where.locate (20, 30)
var font, fontWins : int %font
font := Font.New ("Times New Roman:14:bold")
fontWins := Font.New ("Times New Roman:12")
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, 11) %1
drawfillbox (200, 0, 400, 200, 53) %2
drawfillbox (400, 0, 600, 200, 11) %3
drawfillbox (0, 200, 200, 400, 53) %4
drawfillbox (200, 200, 400, 400, 11) %5
drawfillbox (400, 200, 600, 400, 53) %6
drawfillbox (0, 400, 200, 600, 11) %7
drawfillbox (200, 400, 400, 600, 53) %8
drawfillbox (400, 400, 600, 600, 11) %9
%All 'X' on the gameboard%
procedure BottomLeftX %Drawing X in the bottom left box
drawline (0, 200, 200, 0, 9)
drawline (200, 200, 0, 0, 9)
end BottomLeftX
procedure BottomMiddleX %Drawing X in the bottom middle box
drawline (200, 200, 400, 0, 9)
drawline (400, 200, 200, 0, 9)
end BottomMiddleX
procedure BottomRightX %Drawing X in the bottom right box
drawline (400, 200, 600, 0, 9)
drawline (600, 200, 400, 0, 9)
end BottomRightX
procedure MiddleLeftX %Drawing X in the center left box
drawline (0, 400, 200, 200, 9)
drawline (200, 400, 0, 200, 9)
end MiddleLeftX
procedure MiddleMiddleX %Drawing X in the center middle box
drawline (200, 400, 400, 200, 9)
drawline (400, 400, 200, 200, 9)
end MiddleMiddleX
procedure MiddleRightX %Drawing X in the center right box
drawline (400, 400, 600, 200, 9)
drawline (400, 200, 600, 400, 9)
end MiddleRightX
procedure TopLeftX %Drawing X in the top left box
drawline (0, 600, 200, 400, 9)
drawline (0, 400, 200, 600, 9)
end TopLeftX
procedure TopMiddleX %Drawinf X in the top middle box
drawline (200, 600, 400, 400, 9)
drawline (200, 400, 400, 600, 9)
end TopMiddleX
procedure TopRightX %Drawing X in the top right box
drawline (400, 600, 600, 400, 9)
drawline (400, 400, 600, 600, 9)
end TopRightX
%All O on the gameboard
procedure BottomLeftO %Drawing O in the bottom left box
drawoval (100, 100, 75, 75, black)
end BottomLeftO
procedure BottomMiddleO %Drawing O in the bottom middle box
drawoval (300, 100, 75, 75, black)
end BottomMiddleO
procedure BottomRightO %Drawing O in the bottom right box
drawoval (500, 100, 75, 75, black)
end BottomRightO
procedure MiddleLeftO %Drawing O in the center left box
drawoval (100, 300, 75, 75, black)
end MiddleLeftO
procedure MiddleMiddleO %Drawing O in the center middle box
drawoval (300, 300, 75, 75, black)
end MiddleMiddleO
procedure MiddleRightO %Drawing O in the center right box
drawoval (500, 300, 75, 75, black)
end MiddleRightO
procedure TopLeftO %Drawing O in the top left box
drawoval (100, 500, 75, 75, black)
end TopLeftO
procedure TopMiddleO %Drawing O in the top middle box
drawoval (300, 500, 75, 75, black)
end TopMiddleO
procedure TopRightO %Drawing O in the top right box
drawoval (500, 500, 75, 75, black)
end TopRightO
%Procedures for each box on the board being clicked on
procedure BotLeftBox
loop
%Draws an X when it's player1 turn and the box has nothing in it
if playerturn = true and button = 1 and botleftX = false and botleftO = false then
BottomLeftX %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
BottomLeftO %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
BottomMiddleX
botmidX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = true and button = 1 and botmidX = false and botmidO = false then
BottomMiddleO
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
BottomRightX
botrightX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and botrightX = false and botrightO = false then
BottomRightO
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
MiddleLeftX
midleftX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and midleftX = false and midleftO = false then
MiddleLeftO
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
MiddleMiddleX
midmidX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and midmidX = false and midmidO = false then
MiddleMiddleO
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
MiddleRightX
midrightX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and midrightX = false and midrightO = false then
MiddleRightO
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
TopLeftX
topleftX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and topleftX = false and topleftO = false then
TopLeftO
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
TopMiddleX
topmidX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and topmidX = false and topmidO = false then
TopMiddleO
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
TopRightX
toprightX := true
playerturn := not playerturn
iCount := iCount + 1
elsif playerturn = false and button = 1 and toprightX = false and toprightO = false then
TopRightO
toprightO := true
playerturn := not playerturn
iCount := iCount + 1
end if
exit when toprightX = true or toprightO = true
end loop
end TopRightBox
%Identifies whose 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
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
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)
iExit := 1
%Diagonal win with 3 X
elsif botleftX = true and midmidX = true and toprightX = true then
Draw.ThickLine (0, 0, 600, 600, 10, black)
iExit := 1
elsif botrightX = true and midmidX = true and topleftX = true then
Draw.ThickLine (0, 600, 600, 0, 10, black)
iExit := 1
%Vertical win with 3 X
elsif botleftX = true and midleftX = true and topleftX = true then
Draw.ThickLine (100, 0, 100, 600, 10, black)
iExit := 1
elsif botmidX = true and midmidX = true and topmidX = true then
Draw.ThickLine (300, 0, 300, 600, 10, black)
iExit := 1
elsif botrightX = true and midrightX = true and toprightX = true then
Draw.ThickLine (500, 0, 500, 600, 10, black)
iExit := 1
end if
end loop
end XWinsLine
procedure OWinsLine
%Horizontal win with 3 O
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
iExit := 2
elsif topleftO = true and topmidO = true and toprightO = true then
Draw.ThickLine (0, 500, 600, 500, 10, black)
iExit := 2
elsif midleftO = true and midmidO = true and midrightO = true then
Draw.ThickLine (0, 300, 600, 300, 10, black)
iExit := 2
%Diagonal win with 3 O
elsif botleftO = true and midmidO = true and toprightO = true then
Draw.ThickLine (0, 600, 600, 0, 10, black)
iExit := 2
elsif botrightO = true and midmidO = true and topleftO = true then
Draw.ThickLine (0, 0, 600, 600, 10, black)
iExit := 2
%Vertical win with 3 O
elsif botleftO = true and midleftO = true and topleftO = true then
Draw.ThickLine (100, 0, 100, 600, 10, black)
iExit := 2
elsif botmidO = true and midmidO = true and topmidO = true then
Draw.ThickLine (300, 0, 300, 600, 10, black)
iExit := 2
elsif botrightO = true and midrightO = true and toprightO = true then
Draw.ThickLine (500, 0, 500, 600, 10, black)
iExit := 2
end if
end OWinsLine
procedure player1winner
%Player1 wins with 3 X horizontal
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 vertical
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 diagona
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 horizontal
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 vertical
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 diagonal
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
loop
mousewhere (x, y, button)
if button = 1 and x > 0 and x < 200 and y > 0 and y < 200 then
BotLeftBox %the procedure that draws everything in the box
TakeTurns %Displays which player's turn
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
cls
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 "No one is winner!!!"
end if |
|
|