status = "Intro"

check1r = System.doesFileExist("R1.TXT")
check1b = System.doesFileExist("B1.TXT")
check1g = System.doesFileExist("G1.TXT")

if check1g == 1 and check1r == 1 and check1b == 1 then 
file = io.open("r1.txt", "r")
r1 = file:read()
file:close()
file = io.open("b1.txt", "r")
b1 = file:read()
file:close()
file = io.open("g1.txt", "r")
g1 = file:read()
file:close()
black = Color.new(r,b,g)
else
r1 = 255
b1 = 255
g1 = 255

black = Color.new(r1,b1,g1)
end

teplace = 1

check2r = System.doesFileExist("R2.TXT")
check2b = System.doesFileExist("B2.TXT")
check2g = System.doesFileExist("G2.TXT")

if check2g == 1 and check2r == 1 and check2b == 1 then 
file = io.open("r2.txt", "r")
r2 = file:read()
file:close()
file = io.open("b2.txt", "r")
b2 = file:read()
file:close()
file = io.open("g2.txt", "r")
g2 = file:read()
file:close()
black = Color.new(r,b,g)
else
r2 = 0
b2 = 0
g2 = 0

white = Color.new(r2,b2,g2)
end

Intro = Image.load("Intro.png")
Menu = Image.load("Menu.png")
Curseur = Image.load("Curseur.png")
Base = Image.load("Base.png")
null = Image.load("Nulle.png")
vo = Image.load("Victoire2.png")
vx = Image.load("Victoire1.png")
choose = Image.load("Pion.png")
adversaire = Image.load("Adversaire.png")


img1 = Image.load("Symbole/1.png")
img2 = Image.load("Symbole/2.png")
img3 = Image.load("Symbole/3.png")
img4 = Image.load("Symbole/4.png")

x = img3
o = img4

joueurActuel = x

place = 1

case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0

positionCase1x = 7	
positionCase1y = 6

positionCase2x = 100
positionCase2y = 6

positionCase3x = 193	
positionCase3y = 6

positionCase4x = 7	
positionCase4y = 98

positionCase5x = 100
positionCase5y = 98

positionCase6x = 191	
positionCase6y = 98

positionCase7x = 7	
positionCase7y = 184

positionCase8x = 100
positionCase8y = 184

positionCase9x = 192	
positionCase9y = 185

Gagne = 0
temps = 0

pos2curseury = 63
pos3curseury = 63

sta = 1

test= 0

positionJAx = 0
positionJAy = 0

crono = Timer.new() --Pour l'intro
poscurseury = 31 --Curseur
selection = 1

score1=0 --Parties gagné joueur1
score2=0 --Parties gagné joueir2
score3=0 --Parties nulles

oldpad = Controls.read()

turn = 1
imgj2 = 1
imgj1 = 2

unselection = 1
deselect = 1

if System.doesFileExist("Musique.mp3") == 1 then
Mp3me.load("Musique.mp3")
end

function paramdefaut()
Gagne = 0
temps = 0
place = 1
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
end

while true do

screen:clear()
pad = Controls.read()

if imgj2==1 then x = img1 end
if imgj2==2 then x = img2 end
if imgj2==3 then x = img3 end
if imgj2==4 then x = img4 end
if imgj1==1 then o = img1 end
if imgj1==2 then o = img2 end
if imgj1==3 then o = img3 end
if imgj1==4 then o = img4 end

if System.doesFileExist("Musique.mp3") == 1 then
Mp3me.play()
end


if status == "Intro" then --Intro
crono:start()
tempsintro = crono:time()
screen:blit(0,0,Intro)
if tempsintro > 15000 or pad:r() then
crono:reset(0)
crono:stop()
status = "Menu"
end
end

if status == "Menu" then --Menu

screen:clear()
screen:blit(0,0,Menu)
screen:blit(60,poscurseury,Curseur)

if selection == 1 then poscurseury = 63 end
if selection == 2 then poscurseury = 96 end
if selection == 3 then poscurseury = 132 end
if selection == 4 then poscurseury = 166 end

if pad:down() and oldpad ~=pad then selection = selection + 1 end
if pad:up() and oldpad ~=pad then selection = selection - 1 end

if selection == 5 then selection = 1 end
if selection == 0 then selection = 4 end

if pad:cross() and oldpad:cross() ~=pad:cross() then
oldpad = pad
if selection == 1 then
paramdefaut()
status = "Mode"
end
if selection == 2 then
score1 = "0"
score2 = "0"
score3 = "0"
end
if selection == 3 then status = "Intro" end
if selection == 4 then System.Quit() end
end

end

if status == "Mode" then
screen:clear()
screen:blit(0,0,adversaire)
if unselection == 1 then pos2curseury = 63 end
if unselection == 2 then pos2curseury = 96 end

screen:blit(60,pos2curseury,Curseur)

if pad:circle() and oldpad:circle() ~=pad:circle() then
oldpad = pad
status = "Menu"
end

if pad:down() and unselection~=2 and oldpad ~=pad then unselection = unselection + 1 end
if pad:up() and unselection~=1 and oldpad ~=pad then unselection = unselection - 1 end

if pad:cross() and oldpad:cross() ~=pad:cross() then
oldpad = pad
if unselection == 1 then sta = 1 end
if unselection == 2 then sta = 2 end
status = "Choisir"
end

end

if status == "Choisir" then
screen:clear()
screen:blit(0,0,choose)
screen:blit(273,40,x)
screen:blit(273,122,o)
if deselect == 1 then pos3curseury = 63 end
if deselect == 2 then pos3curseury = 132 end

screen:blit(60,pos3curseury,Curseur)

if pad:circle() and oldpad:circle() ~=pad:circle() then
oldpad = pad
status = "Mode"
end

if imgj2 == 0 then imgj2 = 4 end
if imgj1 == 0 then imgj1 = 4 end

if imgj2 == 5 then imgj2 = 1 end
if imgj1 == 5 then imgj1 = 1 end

if pad:down() and deselect~=2 and oldpad ~=pad then deselect = deselect + 1 end
if pad:up() and deselect~=1 and oldpad ~=pad then deselect = deselect - 1 end

if deselect == 1 then

if pad:right() and not oldpad:right() then
imgj2=imgj2+1
end
if pad:left() and not oldpad:left() then
imgj2=imgj2-1
end

end

if deselect == 2 then

if pad:right() and not oldpad:right() then
imgj1=imgj1+1
end
if pad:left() and not oldpad:left() then
imgj1=imgj1-1
end

end


if pad:start() and oldpad:start() ~=pad:start() then
oldpad = pad
joueurActuel = x
if sta == 1 then status = "Jeu" end
if sta == 2 then status = "JeuCPU" end
end


end

if status == "Jeu" then --Jeu 1 vs 1 sur la PSP
screen:clear()
screen:blit(0,0,Base)
screen:print(285,15,"Le joueur 1 a gagne "..score2,black)
if score1==0 or score1==1 then
screen:print(285,30,"partie.",black)
else
screen:print(285,30,"parties.",black)
end
screen:print(285,55,"Le joueur 2 a gagne "..score1,white)
if score2==0 or score2==1 then
screen:print(285,70,"partie.",white)
else
screen:print(285,70,"parties.",white)
end
if score3==0 or score3==1 then
screen:print(285,95,"Partie nulle : "..score3,black)
else
screen:print(285,95,"Parties nulles : "..score3,black)
end
screen:print(285,115,"Start : Retourner au ",white)
screen:print(285,130,"menu",white)
screen:print(285,155,"Pad Directionnel : Se",black)
screen:print(285,170,"deplacer sur la grille",black)
screen:print(285,195,"Croix : Placer la piece",white)
screen:print(285,210,"sur le jeu",white)

if pad:cross() and oldpad:cross() ~=pad:cross() then
oldpad = pad
  if joueurActuel == x then
    if place == 1 and case1 == 0  then
    joueurActuel = o
    case1 = 1   
    end
    if place == 2 and case2 == 0  then
    joueurActuel = o
    case2 =1
    end
    if place == 3 and case3 == 0  then
    joueurActuel = o
    case3 = 1
    end
    if place == 4 and case4 == 0  then
    joueurActuel = o
    case4 = 1
    end
    if place == 5 and case5 == 0  then
    joueurActuel = o
    case5 = 1
    end
    if place == 6 and case6 == 0  then
    joueurActuel = o
    case6 = 1
    end
    if place == 7 and case7 == 0  then
    joueurActuel = o
    case7 = 1
    end
    if place == 8 and case8 == 0  then
    joueurActuel = o 
    case8 = 1
    end
    if place == 9 and case9 == 0  then
    joueurActuel = o
    case9 =1
    end
 end

if joueurActuel == o then
    if place == 1 and case1 == 0  then
    joueurActuel = x
    case1 = 2   
    end
    if place == 2 and case2 == 0  then
    joueurActuel = x
    case2 =2
    end
    if place == 3 and case3 == 0  then
    joueurActuel = x
    case3 = 2
    end
    if place == 4 and case4 == 0  then
    joueurActuel = x
    case4 = 2
    end
    if place == 5 and case5 == 0  then
    joueurActuel = x
    case5 = 2
    end
    if place == 6 and case6 == 0  then
    joueurActuel = x
    case6 = 2
    end
    if place == 7 and case7 == 0  then
    joueurActuel = x
   case7 = 2
    end
    if place == 8 and case8 == 0  then
    joueurActuel = x
    case8 = 2
    end
    if place == 9 and case9 == 0  then
    joueurActuel = x
    case9 =2
    end
 end
end

if place == 1 then
positionJAx = 7
positionJAy = 6

elseif place == 2 then
positionJAx = 100
positionJAy = 6

elseif place == 3 then
positionJAx = 193
positionJAy = 6

elseif place == 4 then
positionJAx = 7
positionJAy = 98

elseif place == 5 then
positionJAx = 100
positionJAy = 98

elseif place == 6 then
positionJAx = 191	
positionJAy = 98

elseif place == 7 then
positionJAx = 7
positionJAy = 184

elseif place == 8 then
positionJAx = 100
positionJAy = 184

elseif place == 9 then
positionJAx = 192
positionJAy = 185
end

if pad:left() and not oldpad:left() and Gagne == 0 then
place = place - 1
end

if place == 0 then place = 9 end

if pad:right() and not oldpad:right() and Gagne == 0  then
place = place + 1
end

if place == 10 then place = 1 end

if pad:up() and not oldpad:up() and Gagne == 0  then
  if place == 1 then place = 9 
  elseif place == 9 then place = 6 
  elseif place == 6 then place = 3 
  elseif place == 3 then place = 8 
  elseif place == 8 then place = 5 
  elseif place == 5 then place = 2 
  elseif place == 2 then place = 7 
  elseif place == 7 then place = 4 
  elseif place == 4 then place = 1 end
end

if pad:down() and not oldpad:down() and Gagne == 0 then
  if place == 1 then place = 4 
  elseif place == 4 then place = 7 
  elseif place == 7 then place = 2 
  elseif place == 2 then place = 5 
  elseif place == 5 then place = 8 
  elseif place == 8 then place = 3 
  elseif place == 3 then place = 6 
  elseif place == 6 then place = 9 
  elseif place == 9 then place = 1 end
end

if case1 == 1 then screen:blit(positionCase1x,positionCase1y,x) 
elseif case1 == 2 then screen:blit(positionCase1x,positionCase1y,o) end

if case2 == 1 then screen:blit(positionCase2x,positionCase2y,x) 
elseif case2 == 2 then screen:blit(positionCase2x,positionCase2y,o) end

if case3 == 1 then screen:blit(positionCase3x,positionCase3y,x) 
elseif case3 == 2 then screen:blit(positionCase3x,positionCase3y,o) end

if case4 == 1 then screen:blit(positionCase4x,positionCase4y,x) 
elseif case4 == 2 then screen:blit(positionCase4x,positionCase4y,o) end

if case5 == 1 then screen:blit(positionCase5x,positionCase5y,x) 
elseif case5 == 2 then screen:blit(positionCase5x,positionCase5y,o) end

if case6 == 1 then screen:blit(positionCase6x,positionCase6y,x) 
elseif case6 == 2 then screen:blit(positionCase6x,positionCase6y,o) end

if case7 == 1 then screen:blit(positionCase7x,positionCase7y,x) 
elseif case7 == 2 then screen:blit(positionCase7x,positionCase7y,o) end

if case8 == 1 then screen:blit(positionCase8x,positionCase8y,x) 
elseif case8 == 2 then screen:blit(positionCase8x,positionCase8y,o) end

if case9 == 1 then screen:blit(positionCase9x,positionCase9y,x) 
elseif case9 == 2 then screen:blit(positionCase9x,positionCase9y,o) end


if joueurActuel == o and Gagne == 0 then
screen:blit(positionJAx,positionJAy,o)
end

if case1 == 1 and case2 == 1 and case3 == 1 then
Gagne = 1

elseif case1 == 1 and case5 == 1 and case9 == 1 then
Gagne = 1

elseif case1 == 1 and case4 == 1 and case7 == 1 then
Gagne = 1

elseif case2 == 1 and case5 == 1 and case8 == 1 then
Gagne = 1

elseif case3 == 1 and case6 == 1 and case9 == 1 then
Gagne = 1

elseif case3 == 1 and case5 == 1 and case7 == 1 then
Gagne = 1

elseif case4 == 1 and case5 == 1 and case6 == 1 then
Gagne = 1

elseif case7 == 1 and case8 == 1 and case9 == 1 then
Gagne = 1
end

if joueurActuel == x and Gagne == 0  then
screen:blit(positionJAx,positionJAy,x)
end


if case1 == 2 and case2 == 2 and case3 == 2 then
Gagne = 2

elseif case1 == 2 and case5 == 2 and case9 == 2 then
Gagne = 2

elseif case1 == 2 and case4 == 2 and case7 == 2 then
Gagne = 2

elseif case2 == 2 and case5 == 2 and case8 == 2 then
Gagne = 2

elseif case3 == 2 and case6 == 2 and case9 == 2 then
Gagne = 2

elseif case3 == 2 and case5 == 2 and case7 == 2 then
Gagne = 2

elseif case4 == 2 and case5 == 2 and case6 == 2 then
Gagne = 2

elseif case7 == 2 and case8 == 2 and case9 == 2 then
Gagne = 2
end

if case1 ~= 0 and case2 ~= 0 and case3 ~= 0 and case4 ~= 0 and case5 ~= 0 and case6 ~= 0 and case7 ~= 0 and case8 ~= 0 and case9 ~= 0 and Gagne ==0 then
Gagne = 3
end

if Gagne == 1 then
screen:blit(0,0,vx)
joueurActuel = o
if temps <= 100 then
temps = temps+1
elseif temps >= 100 then
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
place = 1
score2 = score2 + 1
Gagne = 0
temps = 0
end
elseif Gagne == 2 then
screen:blit(0,0,vo)
joueurActuel = x
if temps <= 100 then
temps = temps+1
elseif temps >= 100 then
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
place = 1
score1 = score1 + 1
Gagne = 0
temps = 0
end

elseif Gagne == 3 then
screen:blit(0,0,null)
if temps <= 100 then
temps = temps+1
elseif temps >= 100 then
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
place = 1
score3 = score3 + 1
Gagne = 0
temps = 0
end

end


if pad:start() and not oldpad:start() then
oldpad = pad
status = "Menu"
end

end

if status == "JeuCPU" then

screen:clear()
screen:blit(0,0,Base)
screen:print(285,15,"Le joueur 1 a gagne "..score2,black)
if score1==0 or score1==1 then
screen:print(285,30,"partie.",black)
else
screen:print(285,30,"parties.",black)
end
screen:print(285,55,"Le joueur 2 a gagne "..score1,white)
if score2==0 or score2==1 then
screen:print(285,70,"partie.",white)
else
screen:print(285,70,"parties.",white)
end
if score3==0 or score3==1 then
screen:print(285,95,"Partie nulle : "..score3,black)
else
screen:print(285,95,"Parties nulles : "..score3,black)
end
screen:print(285,115,"Start : Retourner au ",white)
screen:print(285,130,"menu",white)
screen:print(285,155,"Pad Directionnel : Se",black)
screen:print(285,170,"deplacer sur la grille",black)
screen:print(285,195,"Croix : Placer la piece",white)
screen:print(285,210,"sur le jeu",white)

if pad:cross() and oldpad:cross() ~=pad:cross() then
oldpad = pad
  if joueurActuel == x then
    if place == 1 and case1 == 0  then
	joueurActuel = o
    case1 = 1   
    end
    if place == 2 and case2 == 0  then
	joueurActuel = o
    case2 =1
    end
    if place == 3 and case3 == 0  then
	joueurActuel = o
    case3 = 1
    end
    if place == 4 and case4 == 0  then
	joueurActuel = o
    case4 = 1
    end
    if place == 5 and case5 == 0  then
	joueurActuel = o
    case5 = 1
    end
    if place == 6 and case6 == 0  then
	joueurActuel = o
    case6 = 1
    end
    if place == 7 and case7 == 0  then
	joueurActuel = o
    case7 = 1
    end
    if place == 8 and case8 == 0  then
	joueurActuel = o 
    case8 = 1
    end
    if place == 9 and case9 == 0  then
	joueurActuel = o
    case9 =1
    end
 end
end

if case1 == 1 and case2 == 1 and case3 == 1 then
Gagne = 1

elseif case1 == 1 and case5 == 1 and case9 == 1 then
Gagne = 1

elseif case1 == 1 and case4 == 1 and case7 == 1 then
Gagne = 1

elseif case2 == 1 and case5 == 1 and case8 == 1 then
Gagne = 1

elseif case3 == 1 and case6 == 1 and case9 == 1 then
Gagne = 1

elseif case3 == 1 and case5 == 1 and case7 == 1 then
Gagne = 1

elseif case4 == 1 and case5 == 1 and case6 == 1 then
Gagne = 1

elseif case7 == 1 and case8 == 1 and case9 == 1 then
Gagne = 1
end

if case1 == 2 and case2 == 2 and case3 == 2 then
Gagne = 2

elseif case1 == 2 and case5 == 2 and case9 == 2 then
Gagne = 2

elseif case1 == 2 and case4 == 2 and case7 == 2 then
Gagne = 2

elseif case2 == 2 and case5 == 2 and case8 == 2 then
Gagne = 2

elseif case3 == 2 and case6 == 2 and case9 == 2 then
Gagne = 2

elseif case3 == 2 and case5 == 2 and case7 == 2 then
Gagne = 2

elseif case4 == 2 and case5 == 2 and case6 == 2 then
Gagne = 2

elseif case7 == 2 and case8 == 2 and case9 == 2 then
Gagne = 2
end

if case1 ~= 0 and case2 ~= 0 and case3 ~= 0 and case4 ~= 0 and case5 ~= 0 and case6 ~= 0 and case7 ~= 0 and case8 ~= 0 and case9 ~= 0 and Gagne ==0 then
Gagne = 3
end


if joueurActuel == o and Gagne == 0 then
teplace = math.random(1,9)
if teplace == 1 and case1 ~= 0 then teplace = math.random(1,9) end
if teplace == 2 and case2 ~= 0 then teplace = math.random(1,9) end
if teplace == 3 and case3 ~= 0 then teplace = math.random(1,9) end
if teplace == 4 and case4 ~= 0 then teplace = math.random(1,9) end
if teplace == 5 and case5 ~= 0 then teplace = math.random(1,9) end
if teplace == 6 and case6 ~= 0 then teplace = math.random(1,9) end
if teplace == 7 and case7 ~= 0 then teplace = math.random(1,9) end
if teplace == 8 and case8 ~= 0 then teplace = math.random(1,9) end
if teplace == 9 and case9 ~= 0 then teplace = math.random(1,9) end



if case1 == 2 and case2 == 2 and case3 == 0 then
case3 = 2
joueurActuel = x
elseif case1 == 2 and case3 == 2 and case2 == 0 then
case2 = 2
joueurActuel = x
elseif case2 == 2 and case3 == 2 and case1 == 0 then
case1 = 2
joueurActuel = x
elseif case1 == 2 and case4 == 2 and case7 == 0 then
case7 = 2
joueurActuel = x
elseif case1 == 2 and case7 == 2 and case4 == 0 then
case4 = 2
joueurActuel = x
elseif case4 == 2 and case7 == 2 and case1 == 0 then
case1 = 2
joueurActuel = x
elseif case4 == 2 and case5 == 2 and case6 == 0 then
case6 = 2
joueurActuel = x
elseif case4 == 2 and case6 == 2 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 2 and case6 == 2 and case4 == 0 then
case4 = 2
joueurActuel = x
elseif case2 == 2 and case5 == 2 and case8 == 0 then
case8 = 2
joueurActuel = x
elseif case2 == 2 and case8 == 2 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 2 and case8 == 2 and case2 == 0 then
case2 = 2
joueurActuel = x
elseif case7 == 2 and case8 == 2 and case9 == 0 then
case9 = 2
joueurActuel = x
elseif case7 == 2 and case9 == 2 and case8 == 0 then
case8 = 2
joueurActuel = x
elseif case8 == 2 and case9 == 2 and case7 == 0 then
case7 = 2
joueurActuel = x
elseif case3 == 2 and case6 == 2 and case9 == 0 then
case9 = 2
joueurActuel = x
elseif case3 == 2 and case9 == 2 and case6 == 0 then
case6 = 2
joueurActuel = x
elseif case6 == 2 and case9 == 2 and case3 == 0 then
case3 = 2
joueurActuel = x
elseif case1 == 2 and case5 == 2 and case9 == 0 then
case9 = 2
joueurActuel = x
elseif case1 == 2 and case9 == 2 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 2 and case9 == 2 and case1 == 0 then
case1 = 2
joueurActuel = x
elseif case3 == 2 and case5 == 2 and case7 == 0 then
case7 = 2
joueurActuel = x
elseif case3 == 2 and case7 == 2 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 2 and case7 == 2 and case3 == 0 then
case3 = 2
joueurActuel = x

elseif case1 == 1 and case3 == 1 and case2 == 0 then
case2 = 2
joueurActuel = x
elseif case2 == 1 and case3 == 1 and case1 == 0 then
case1 = 2
joueurActuel = x
elseif case1 == 1 and case4 == 1 and case7 == 0 then
case7 = 2
joueurActuel = x
elseif case1 == 1 and case7 == 1 and case4 == 0 then
case4 = 2
joueurActuel = x
elseif case4 == 1 and case7 == 1 and case1 == 0 then
case1 = 2
joueurActuel = x
elseif case4 == 1 and case5 == 1 and case6 == 0 then
case6 = 2
joueurActuel = x
elseif case4 == 1 and case6 == 1 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 1 and case6 == 1 and case4 == 0 then
case4 = 2
joueurActuel = x
elseif case2 == 1 and case5 == 1 and case8 == 0 then
case8 = 2
joueurActuel = x
elseif case2 == 1 and case8 == 1 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 1 and case8 == 1 and case2 == 0 then
case2 = 2
joueurActuel = x
elseif case7 == 1 and case8 == 1 and case9 == 0 then
case9 = 2
joueurActuel = x
elseif case7 == 1 and case9 == 1 and case8 == 0 then
case8 = 2
joueurActuel = x
elseif case8 == 1 and case9 == 1 and case7 == 0 then
case7 = 2
joueurActuel = x
elseif case3 == 1 and case6 == 1 and case9 == 0 then
case9 = 2
joueurActuel = x
elseif case3 == 1 and case9 == 1 and case6 == 0 then
case6 = 2
joueurActuel = x
elseif case6 == 1 and case9 == 1 and case3 == 0 then
case3 = 2
joueurActuel = x
elseif case1 == 1 and case5 == 1 and case9 == 0 then
case9 = 2
joueurActuel = x
elseif case1 == 1 and case9 == 1 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 1 and case9 == 1 and case1 == 0 then
case1 = 2
joueurActuel = x
elseif case3 == 1 and case5 == 1 and case7 == 0 then
case7 = 2
joueurActuel = x
elseif case3 == 1 and case7 == 1 and case5 == 0 then
case5 = 2
joueurActuel = x
elseif case5 == 1 and case7 == 1 and case3 == 0 then
case3 = 2
joueurActuel = x

elseif teplace == 1 and case1 == 0 then
case1 = 2
joueurActuel = x

elseif teplace == 2 and case2 == 0 then
case2 = 2
joueurActuel = x

elseif teplace == 3 and case3 == 0 then
case3 = 2
joueurActuel = x

elseif teplace == 4 and case4 == 0 then
case4 = 2
joueurActuel = x

elseif teplace == 5 and case5 == 0 then
case5 = 2
joueurActuel = x

elseif teplace == 6 and case6 == 0 then
case6 = 2
joueurActuel = x

elseif teplace == 7 and case7 == 0 then
case7 = 2
joueurActuel = x

elseif teplace == 8 and case8 == 0 then
case8 = 2
joueurActuel = x

elseif teplace == 9 and case9 == 0 then
case9 = 2
joueurActuel = x

end
end

if place == 1 then
positionJAx = 7
positionJAy = 6

elseif place == 2 then
positionJAx = 100
positionJAy = 6

elseif place == 3 then
positionJAx = 193
positionJAy = 6

elseif place == 4 then
positionJAx = 7
positionJAy = 98

elseif place == 5 then
positionJAx = 100
positionJAy = 98

elseif place == 6 then
positionJAx = 191	
positionJAy = 98

elseif place == 7 then
positionJAx = 7
positionJAy = 184

elseif place == 8 then
positionJAx = 100
positionJAy = 184

elseif place == 9 then
positionJAx = 192
positionJAy = 185
end

if joueurActuel == x and Gagne == 0 then

if pad:left() and not oldpad:left() and Gagne == 0  then
place = place - 1
end

if place == 0 then place = 9 end

if pad:right() and not oldpad:right() and Gagne == 0  then
place = place + 1
end

if place == 10 then place = 1 end

if pad:up() and not oldpad:up() and Gagne == 0  then
  if place == 1 then place = 9 
  elseif place == 9 then place = 6 
  elseif place == 6 then place = 3 
  elseif place == 3 then place = 8 
  elseif place == 8 then place = 5 
  elseif place == 5 then place = 2 
  elseif place == 2 then place = 7 
  elseif place == 7 then place = 4 
  elseif place == 4 then place = 1 end
end

if pad:down() and not oldpad:down() and Gagne == 0  then
  if place == 1 then place = 4 
  elseif place == 4 then place = 7 
  elseif place == 7 then place = 2 
  elseif place == 2 then place = 5 
  elseif place == 5 then place = 8 
  elseif place == 8 then place = 3 
  elseif place == 3 then place = 6 
  elseif place == 6 then place = 9 
  elseif place == 9 then place = 1 end
end

end

if case1 == 1 then screen:blit(positionCase1x,positionCase1y,x) 
elseif case1 == 2 then screen:blit(positionCase1x,positionCase1y,o) end

if case2 == 1 then screen:blit(positionCase2x,positionCase2y,x) 
elseif case2 == 2 then screen:blit(positionCase2x,positionCase2y,o) end

if case3 == 1 then screen:blit(positionCase3x,positionCase3y,x) 
elseif case3 == 2 then screen:blit(positionCase3x,positionCase3y,o) end

if case4 == 1 then screen:blit(positionCase4x,positionCase4y,x) 
elseif case4 == 2 then screen:blit(positionCase4x,positionCase4y,o) end

if case5 == 1 then screen:blit(positionCase5x,positionCase5y,x) 
elseif case5 == 2 then screen:blit(positionCase5x,positionCase5y,o) end

if case6 == 1 then screen:blit(positionCase6x,positionCase6y,x) 
elseif case6 == 2 then screen:blit(positionCase6x,positionCase6y,o) end

if case7 == 1 then screen:blit(positionCase7x,positionCase7y,x) 
elseif case7 == 2 then screen:blit(positionCase7x,positionCase7y,o) end

if case8 == 1 then screen:blit(positionCase8x,positionCase8y,x) 
elseif case8 == 2 then screen:blit(positionCase8x,positionCase8y,o) end

if case9 == 1 then screen:blit(positionCase9x,positionCase9y,x) 
elseif case9 == 2 then screen:blit(positionCase9x,positionCase9y,o) end



if joueurActuel == o and Gagne == 0 then
screen:blit(positionJAx,positionJAy,o)
end

if joueurActuel == x and Gagne == 0 then
screen:blit(positionJAx,positionJAy,x)
end

if Gagne == 1 then
screen:blit(0,0,vx)
joueurActuel = o
if temps <= 100 then
temps = temps+1
elseif temps >= 100 then
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
place = 1
score2 = score2 + 1
Gagne = 0
temps = 0
end
elseif Gagne == 2 then
screen:blit(0,0,vo)
joueurActuel = x
if temps <= 100 then
temps = temps+1
elseif temps >= 100 then
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
place = 1
score1 = score1 + 1
Gagne = 0
temps = 0
end

elseif Gagne == 3 then
screen:blit(0,0,null)
if temps <= 100 then
temps = temps+1
elseif temps >= 100 then
case1 = 0
case2 = 0
case3 = 0
case4 = 0
case5 = 0
case6 = 0
case7 = 0
case8 = 0
case9 = 0
place = 1
score3 = score3 + 1
Gagne = 0
temps = 0
end
end

if pad:start() and not oldpad:start() then
oldpad = pad
status = "Menu"
end

end

screen.waitVblankStart()
screen.flip()
oldpad = pad
end




