Advertisement
AdslHouba

Jeu de memoire (ComputerCraft

Feb 2nd, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.30 KB | None | 0 0
  1. local affichage={} -- selecteurs pour l'affichage
  2. local coffres={} -- coffres contenant les items
  3. local joueur={}
  4. local nbSelecteurParJoueur=8
  5. local nbSelecteurParMoniteur={8,8}
  6. local configFenetre={
  7.     {10,10,10,11,10,11,10,10},
  8.     {10,10,10,11,10,11,10,10}
  9. }
  10. local coteRedstone="back"
  11.  
  12. function addPeripheral(id)
  13.     if not(peripheral.isPresent(id)) then
  14.         print(id..' non connecte')
  15.     end
  16.     return {
  17.         id=id,
  18.         pp=peripheral.wrap(id)
  19.     }
  20. end
  21. function cacheEcran()
  22.     total=0
  23.     for ij=1,#joueur do
  24.         if joueur[ij].cache then
  25.             total=total+joueur[ij].couleur
  26.         end
  27.     end
  28.     redstone.setBundledOutput(coteRedstone,total)
  29. end
  30.  
  31. local moniteurModjeu=addPeripheral("top")
  32. local fenetreModjeu=window.create(moniteurModjeu.pp,1,1,20,5)
  33. fenetreModjeu.setCursorPos(6,2)
  34. fenetreModjeu.write("Scorring")
  35. fenetreModjeu.setCursorPos(7,4)
  36. fenetreModjeu.write("Versus")
  37.  
  38. local fenetreDifficulte=window.create(moniteurModjeu.pp,1,1,20,5,false)
  39. fenetreDifficulte.setCursorPos(4,1)
  40. fenetreDifficulte.write("Rouge et blanc")
  41. fenetreDifficulte.setCursorPos(7,3)
  42. fenetreDifficulte.write((nbSelecteurParJoueur*4).." items")
  43. fenetreDifficulte.setCursorPos(7,5)
  44. fenetreDifficulte.write((nbSelecteurParJoueur*9).." items")
  45.  
  46. local fenetreExplication=window.create(moniteurModjeu.pp,1,1,40,5,false)
  47. fenetreExplication.setCursorPos(1,2)
  48. fenetreExplication.write("Cliquer sur la laine grise pour rejoindre")
  49. fenetreExplication.setCursorPos(1,3)
  50. fenetreExplication.write("Quand tous les joueurs on rejoind")
  51. fenetreExplication.setCursorPos(1,4)
  52. fenetreExplication.write("Tous le monde clique sur la laine verte")
  53.  
  54. local fenetreEncours=window.create(moniteurModjeu.pp,1,1,20,5,false)
  55. fenetreEncours.setCursorPos(1,3)
  56. fenetreEncours.write("Partie en cours")
  57.  
  58.  
  59. function addAffichage(id)
  60.     table.insert(affichage,addPeripheral('openperipheral_selector_'..id))
  61. end
  62. addAffichage(1)
  63. addAffichage(2)
  64. addAffichage(3)
  65. addAffichage(4)
  66. addAffichage(5)
  67. addAffichage(6)
  68. addAffichage(7)
  69. addAffichage(8)
  70. addAffichage(9)
  71. addAffichage(10)
  72. addAffichage(11)
  73. addAffichage(12)
  74. addAffichage(13)
  75. addAffichage(14)
  76. addAffichage(15)
  77. addAffichage(16)
  78. function addCoffre(id)
  79.     table.insert(coffres,addPeripheral(id))
  80. end
  81. addCoffre('stock_chest_0')
  82. addCoffre('stock_chest_1')
  83. addCoffre('stock_chest_2')
  84.  
  85. function addJoueur(couleur,selecteurs,moniteurs)
  86.     if #selecteurs~=nbSelecteurParJoueur then
  87.         print("Nombre de selecteur pour le joueur "..(#joueur+1).." invalide ("..#selecteurs.."/"..nbSelecteurParJoueur)
  88.     end
  89.     if #moniteurs~=math.ceil(#affichage/8) then
  90.         print("Nombre d'ecran pour le joueur "..(#joueur+1).." invalide "..#moniteurs.."/"..math.ceil(#affichage/8))
  91.     end
  92.     local data={
  93.         couleur=couleur,
  94.         actif=true,
  95.         cache=true,
  96.         selecteurs={},
  97.         moniteurs={},
  98.         fenetres={}
  99.     }
  100.     for i=1,#selecteurs do
  101.         table.insert(data.selecteurs,addPeripheral(selecteurs[i]))
  102.     end
  103.     for i=1,#moniteurs do
  104.         table.insert(data.moniteurs,addPeripheral(moniteurs[i]))       
  105.         local posX=1
  106.         for u=1,nbSelecteurParMoniteur[i] do
  107.             fenetre=window.create(data.moniteurs[i].pp,posX,1,configFenetre[i][u],5)           
  108.             posX=posX+configFenetre[i][u]
  109.             fenetre.setBackgroundColour(couleur)
  110.             fenetre.clear()
  111.             table.insert(data.fenetres,fenetre)
  112.         end
  113.     end
  114.     data.fenetreResultat=window.create(data.moniteurs[1].pp,3,1,8,5,false)
  115.     table.insert(joueur,data)
  116. end
  117. addJoueur(colors.orange,{
  118.     'openperipheral_selector_34',
  119.     'openperipheral_selector_33',
  120.     'openperipheral_selector_35',
  121.     'openperipheral_selector_36',
  122.     'openperipheral_selector_37',
  123.     'openperipheral_selector_38',
  124.     'openperipheral_selector_39',
  125.     'openperipheral_selector_40'
  126. },{
  127.     'monitor_13',
  128.     'monitor_14'
  129. })
  130. addJoueur(colors.cyan,{
  131.     'openperipheral_selector_41',
  132.     'openperipheral_selector_42',
  133.     'openperipheral_selector_43',
  134.     'openperipheral_selector_44',
  135.     'openperipheral_selector_45',
  136.     'openperipheral_selector_46',
  137.     'openperipheral_selector_47',
  138.     'openperipheral_selector_48'
  139. },{
  140.     'monitor_10',
  141.     'monitor_11'
  142. })
  143. addJoueur(colors.blue,{
  144.     'openperipheral_selector_49',
  145.     'openperipheral_selector_50',
  146.     'openperipheral_selector_51',
  147.     'openperipheral_selector_52',
  148.     'openperipheral_selector_53',
  149.     'openperipheral_selector_54',
  150.     'openperipheral_selector_55',
  151.     'openperipheral_selector_56'
  152. },{
  153.     'monitor_8',
  154.     'monitor_9'
  155. })
  156. addJoueur(colors.yellow,{
  157.     'openperipheral_selector_57',
  158.     'openperipheral_selector_58',
  159.     'openperipheral_selector_59',
  160.     'openperipheral_selector_60',
  161.     'openperipheral_selector_61',
  162.     'openperipheral_selector_62',
  163.     'openperipheral_selector_63',
  164.     'openperipheral_selector_64'
  165. },{
  166.     'monitor_15',
  167.     'monitor_16'
  168. })
  169. cacheEcran()
  170.  
  171. function afficherSequence(idCoffre,nbItem)
  172.     coffres[idCoffre].pp.condenseItems()
  173.     local stacks=coffres[idCoffre].pp.getAllStacks()
  174.     local resultatSlot={}
  175.     local resultat={}
  176.     local multiItem=true
  177.     if nbItem>#stacks then
  178.         multiItem=true
  179.         for i=1, nbItem do
  180.             resultatSlot[i]=math.random(#stacks)
  181.         end
  182.     else
  183.         multiItem=false
  184.         local preRand={}
  185.         local rand=0
  186.         for i=1, #stacks do
  187.             preRand[i]=i
  188.         end
  189.         for i=1, nbItem do
  190.             rand=math.random(#preRand)
  191.             resultatSlot[i]=preRand[rand]
  192.             table.remove(preRand,rand)
  193.         end
  194.     end
  195.     local premierSelect=math.floor(((#affichage-nbItem)/2))
  196.     afficheMonitorSequence(premierSelect,nbItem,false)
  197.     for i=1, nbItem do
  198.         resultat[i]=stacks[resultatSlot[i]].all()
  199.         affichage[i+premierSelect].pp.setSlot(1,resultat[i])
  200.     end
  201.     return resultat, premierSelect, multiItem
  202. end
  203. function affichageClean()
  204.     for i=1, #affichage do
  205.         affichage[i].pp.setSlot(1)
  206.     end
  207. end
  208. function cleanSelecteurJoueur(idJoueur)
  209.     for is=1,nbSelecteurParJoueur do
  210.         joueur[idJoueur].selecteurs[is].pp.setSlots({})
  211.     end
  212. end
  213. function cleanSelecteurJoueurs()
  214.     for ij=1,#joueur do
  215.         cleanSelecteurJoueur(ij)
  216.     end
  217. end
  218.  
  219. function afficherSelecteurJoueur(idCoffre)
  220.     coffres[idCoffre].pp.condenseItems()
  221.     local stacks=coffres[idCoffre].pp.getAllStacks()
  222.     local parEcran=9
  223.     if #stacks<=nbSelecteurParJoueur then
  224.         parEcran=1
  225.     elseif #stacks<=nbSelecteurParJoueur*4 then
  226.         parEcran=4
  227.     elseif #stacks<=nbSelecteurParJoueur*9 then
  228.         parEcran=9
  229.     else
  230.         print('TROP ITEMS POUR TOUS AFFICHER')
  231.     end
  232.     for ij=1,#joueur do
  233.         if joueur[ij].actif then
  234.             stacks=coffres[idCoffre].pp.getAllStacks()
  235.             for is=1,nbSelecteurParJoueur do
  236.                 joueur[ij].numerique[is]={}
  237.                 joueur[ij].numeriqueOk[is]={}
  238.                 for isl=1, parEcran do
  239.                     if #stacks~=0 then
  240.                         if parEcran==4 and isl>2 then
  241.                             isl=isl+1
  242.                         end
  243.                         joueur[ij].numerique[is][isl]=stacks[1].all()
  244.                         joueur[ij].numeriqueOk[is][isl]=true
  245.                         joueur[ij].selecteurs[is].pp.setSlot(isl,joueur[ij].numerique[is][isl])
  246.                         table.remove(stacks,1)
  247.                     end
  248.                 end
  249.             end
  250.         end
  251.     end
  252. end
  253. function fenetreColorisation(idJoueur,idFenetre,couleur)
  254.     joueur[idJoueur].fenetres[idFenetre].setBackgroundColour(couleur)
  255.     joueur[idJoueur].fenetres[idFenetre].clear()
  256. end
  257. function afficheMonitorSequence(premierSelect,sequenceNB,marquerPremier)
  258.     for ij=1,#joueur do
  259.         if joueur[ij].actif then           
  260.             for ife=1, #joueur[ij].fenetres do
  261.                 if ife<=premierSelect or premierSelect+sequenceNB<ife then
  262.                     fenetreColorisation(ij,ife,colors.black)
  263.                 elseif ife==premierSelect+1 then
  264.                     if marquerPremier then
  265.                         fenetreColorisation(ij,ife,joueur[ij].couleur)
  266.                     else
  267.                         fenetreColorisation(ij,ife,colors.white)
  268.                     end
  269.                 else
  270.                     fenetreColorisation(ij,ife,colors.white)
  271.                 end
  272.             end
  273.         end
  274.     end
  275. end
  276. function ecouteJoueur(sequenceNB,premierSelect,multiItem)
  277.     afficheMonitorSequence(premierSelect,sequenceNB,true)
  278.     for ij=1,#joueur do
  279.         if joueur[ij].actif then
  280.             joueur[ij].sequence={}
  281.             joueur[ij].cache=false
  282.         end
  283.     end
  284.     cacheEcran()
  285.     local fini=false
  286.     while not(fini) do     
  287.         event, slot, side = os.pullEvent('slot_click')
  288.         fini=true
  289.         for ij=1,#joueur do
  290.             if joueur[ij].actif then               
  291.                 for is=1,nbSelecteurParJoueur do
  292.                     if joueur[ij].selecteurs[is].id==side then
  293.                         print('Event joueur '..ij)
  294.                         if #joueur[ij].sequence<sequenceNB then
  295.                             table.insert(joueur[ij].sequence,joueur[ij].selecteurs[is].pp.getSlot(slot))
  296.                             fenetreColorisation(ij,premierSelect+#joueur[ij].sequence,colors.brown)
  297.                             if not(multiItem) then
  298.                                 joueur[ij].selecteurs[is].pp.setSlot(slot)
  299.                                 joueur[ij].numeriqueOk[is][slot]=false
  300.                             end
  301.                         end
  302.                         if #joueur[ij].sequence==sequenceNB then                           
  303.                             joueur[ij].cache=true
  304.                             cacheEcran()
  305.                         else
  306.                             fenetreColorisation(ij,premierSelect+#joueur[ij].sequence+1,joueur[ij].couleur)
  307.                         end
  308.                     end
  309.                 end
  310.                 if #joueur[ij].sequence~=sequenceNB then
  311.                     fini=false
  312.                 end
  313.             end
  314.         end    
  315.     end
  316. end
  317. function verifSecquence(sequence,premier,mode)
  318.     for is=1,#sequence do
  319.         affichage[premier+is].pp.setSlot(1,sequence[is])
  320.         os.sleep(0.5)
  321.         for ij=1,#joueur do
  322.             if joueur[ij].actif then
  323.                 if joueur[ij].sequence[is].id==sequence[is].id and joueur[ij].sequence[is].dmg==sequence[is].dmg then
  324.                     fenetreColorisation(ij,premier+is,colors.green)
  325.                     if not(joueur[ij].perdu) then
  326.                         joueur[ij].score=joueur[ij].score+1
  327.                     end
  328.                 else
  329.                     fenetreColorisation(ij,premier+is,colors.red)
  330.                     if mode=='score' then
  331.                         print(ij..' perdu')
  332.                         --joueur[ij].actif=false
  333.                         joueur[ij].perdu=true                      
  334.                     else --versus
  335.                        
  336.                     end
  337.                 end
  338.             end
  339.         end
  340.         os.sleep(1)
  341.     end
  342.     if mode=='score' then
  343.         for ij=1,#joueur do
  344.             if joueur[ij].perdu then
  345.                 joueur[ij].actif=false
  346.                 joueur[ij].perdu=false
  347.                 afficherGrosseInfo(ij,joueur[ij].score)
  348.             end
  349.         end
  350.     end
  351. end
  352. function afficherGrosseInfo(ij,msg)
  353.     for ife=1, #joueur[ij].fenetres do
  354.         fenetreColorisation(ij,ife,joueur[ij].couleur)
  355.     end
  356.     joueur[ij].moniteurs[1].pp.setTextScale(5)
  357.     joueur[ij].fenetreResultat.setVisible(true)
  358.     joueur[ij].fenetreResultat.clear()
  359.     joueur[ij].fenetreResultat.setCursorPos(1,1)
  360.     joueur[ij].fenetreResultat.write(msg)
  361. end
  362.  
  363. --
  364. local idCoffre=2
  365. local sequenceNB=4
  366. local sequenceJuste={}
  367. local premierSelect=1
  368. local tempsMemo=0.8
  369. local mode='score' -- score versus
  370. local difficulte=1 -- 1 2 3
  371. local partie=true
  372. local ok=true
  373.  
  374. cleanSelecteurJoueurs()
  375.  
  376.  
  377. while true do
  378.     affichageClean()
  379.     moniteurModjeu.pp.setTextScale(1)
  380.     fenetreExplication.setVisible(false)
  381.     fenetreDifficulte.setVisible(false)
  382.     fenetreModjeu.setVisible(true)
  383.     ok=false
  384.     while not(ok) do
  385.         print("Attente mode")
  386.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  387.         if moniteurModjeu.id==side then
  388.             if yPos==2 then
  389.                 ok=true
  390.                 mode='score'
  391.             elseif yPos==4 then
  392.                 ok=true
  393.                 mode='versus'
  394.             end
  395.         end
  396.     end
  397.     print(mode)
  398.     fenetreModjeu.setVisible(false)
  399.     fenetreDifficulte.setVisible(true)
  400.     ok=false
  401.     while not(ok) do
  402.         print("Attente difculter")
  403.         event, side, xPos, yPos = os.pullEvent("monitor_touch")
  404.         if moniteurModjeu.id==side then
  405.             if yPos==1 then
  406.                 ok=true
  407.                 idCoffre=1
  408.                 difficulte=1
  409.                 sequenceNB=6
  410.                 tempsMemo=0.8
  411.             elseif yPos==3 then
  412.                 ok=true
  413.                 idCoffre=2
  414.                 difficulte=2
  415.                 sequenceNB=4
  416.                 tempsMemo=1.5
  417.             elseif yPos==5 then
  418.                 ok=true
  419.                 idCoffre=3
  420.                 difficulte=3
  421.                 sequenceNB=5
  422.                 tempsMemo=1.5
  423.             end
  424.         end
  425.     end
  426.     print("Dificulter "..difficulte)
  427.     moniteurModjeu.pp.setTextScale(0.5)
  428.     fenetreDifficulte.setVisible(false)
  429.     fenetreExplication.setVisible(true)
  430.     for ij=1,#joueur do
  431.         joueur[ij].selecteurs[1].pp.setSlot(1,{id="minecraft:wool",dmg=8})
  432.         joueur[ij].score=0
  433.         joueur[ij].actif=false
  434.         joueur[ij].pret=false
  435.         joueur[ij].perdu=false
  436.         joueur[ij].cache=false
  437.         joueur[ij].numerique={}
  438.         joueur[ij].numeriqueOk={}
  439.         joueur[ij].moniteurs[1].pp.setTextScale(1)
  440.         joueur[ij].fenetreResultat.setVisible(false)
  441.         for ife=1, #joueur[ij].fenetres do
  442.             fenetreColorisation(ij,ife,colors.black)
  443.         end
  444.     end
  445.     cacheEcran()
  446.     fini=false
  447.     while not(fini) do
  448.         event, slot, side = os.pullEvent('slot_click')
  449.         for ij=1,#joueur do
  450.             if joueur[ij].selecteurs[1].id==side then  
  451.                 print('-- joueur '..ij)        
  452.                 if not(joueur[ij].actif) then              
  453.                     joueur[ij].actif=true
  454.                    
  455.                     for ij2=1,#joueur do
  456.                         if joueur[ij2].actif then
  457.                             for ife=1, #joueur[ij2].fenetres do
  458.                                 if ife%2==0 then
  459.                                     fenetreColorisation(ij2,ife,joueur[ij2].couleur)
  460.                                 else
  461.                                     fenetreColorisation(ij2,ife,colors.black)
  462.                                 end
  463.                             end
  464.                             joueur[ij2].pret=false
  465.                             joueur[ij2].selecteurs[1].pp.setSlot(1,{id="minecraft:wool",dmg=13})
  466.                             joueur[ij2].selecteurs[2].pp.setSlot(1,{id="minecraft:wool",dmg=14})
  467.                         end
  468.                     end
  469.                 else
  470.                     for ife=1, #joueur[ij].fenetres do
  471.                         fenetreColorisation(ij,ife,joueur[ij].couleur)
  472.                     end
  473.                     joueur[ij].pret=true
  474.                     joueur[ij].selecteurs[1].pp.setSlot(1,{id="minecraft:wool",dmg=5})
  475.                     joueur[ij].selecteurs[2].pp.setSlot(1,{id="minecraft:wool",dmg=14})
  476.                 end
  477.             elseif joueur[ij].selecteurs[2].id==side then
  478.                 for ife=1, #joueur[ij].fenetres do
  479.                     fenetreColorisation(ij,ife,colors.black)
  480.                 end
  481.                 print('-- joueur '..ij)
  482.                 joueur[ij].actif=false
  483.                 joueur[ij].pret=false
  484.                 joueur[ij].selecteurs[1].pp.setSlot(1,{id="minecraft:wool",dmg=8})
  485.                 joueur[ij].selecteurs[2].pp.setSlot(1)
  486.             end        
  487.         end
  488.         fini=true
  489.         nbJoueur=0
  490.         for ij=1,#joueur do            
  491.             if joueur[ij].actif then   
  492.                 nbJoueur=nbJoueur+1
  493.                 if not(joueur[ij].pret) then
  494.                     print('joueur '..ij..' non pret')
  495.                     fini=false
  496.                 end
  497.             end
  498.         end
  499.         if nbJoueur==0 then
  500.             print('Pas de joueur')
  501.             fini=false
  502.         end
  503.     end
  504.     cleanSelecteurJoueurs()
  505.     for ij=1,#joueur do
  506.         joueur[ij].cache=true
  507.     end
  508.     cacheEcran()
  509.     moniteurModjeu.pp.setTextScale(1)
  510.     fenetreExplication.setVisible(false)
  511.     fenetreEncours.setVisible(true)
  512.     partie=true
  513.     afficherSelecteurJoueur(idCoffre)
  514.     while partie do
  515.         for ij=1,#joueur do
  516.             if joueur[ij].actif then
  517.                 for is=1,#joueur[ij].numeriqueOk do
  518.                     for isl, item in pairs(joueur[ij].numeriqueOk[is]) do
  519.                         if not(joueur[ij].numeriqueOk[is][isl]) then
  520.                             joueur[ij].numeriqueOk[is][isl]=true
  521.                             joueur[ij].selecteurs[is].pp.setSlot(isl,joueur[ij].numerique[is][isl])
  522.                         end
  523.                     end
  524.                 end
  525.                
  526.             end
  527.         end
  528.         sequenceJuste,premierSelect,multiItem=afficherSequence(idCoffre,sequenceNB)
  529.         os.sleep(tempsMemo*sequenceNB)
  530.         affichageClean()
  531.         ecouteJoueur(sequenceNB,premierSelect,multiItem)
  532.         verifSecquence(sequenceJuste,premierSelect,mode)
  533.  
  534.         if mode=='score' then
  535.             partie=false
  536.             for ij=1,#joueur do
  537.                 if joueur[ij].actif then
  538.                     partie=true
  539.                 end
  540.             end    
  541.         else -- versus
  542.             local scoreMax=0
  543.             local nbJoueur=0
  544.             for ij=1,#joueur do
  545.                 if joueur[ij].actif then
  546.                     print("Joueur "..ij.." "..joueur[ij].score)
  547.                     scoreMax=math.max(scoreMax,joueur[ij].score)
  548.                 end
  549.             end
  550.             print("Score "..scoreMax)
  551.             for ij=1,#joueur do
  552.                 if joueur[ij].actif then
  553.                     if joueur[ij].score<scoreMax then
  554.                        
  555.                         joueur[ij].actif=false
  556.                         afficherGrosseInfo(ij,"PERDU")
  557.                     else
  558.                         nbJoueur=nbJoueur+1
  559.                     end
  560.                 end
  561.             end
  562.             if nbJoueur==1 then
  563.                 for ij=1,#joueur do
  564.                     if joueur[ij].actif then
  565.                         afficherGrosseInfo(ij,"GAGNER")
  566.                     end
  567.                 end
  568.                 partie=false
  569.             end
  570.         end
  571.         if difficulte==1 then
  572.             sequenceNB=sequenceNB+2
  573.         elseif difficulte==2 then
  574.             sequenceNB=sequenceNB+1
  575.         else
  576.             sequenceNB=sequenceNB+1
  577.         end
  578.         if sequenceNB>#affichage then
  579.             sequenceNB=#affichage
  580.             if tempsMemo>=0.2 then
  581.                 tempsMemo=tempsMemo-0.05
  582.             end
  583.         end
  584.     end
  585.     cleanSelecteurJoueurs()
  586. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement