Advertisement
Guest User

tic tac toe

a guest
Dec 7th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. --spieler 1 O ; spieler 2 X
  2.  
  3.  
  4. j1x1=0
  5. j1x2=0
  6. j1x3=0
  7. j2x1=0
  8. j2x2=0
  9. j2x3=0
  10. j3x1=0
  11. j3x2=0
  12. j3x3=0
  13.  
  14.  
  15. i = 0
  16.  
  17.  
  18. m = peripheral.wrap("back")
  19.  
  20.  
  21.  
  22. function drawfield()
  23.     m.setBackgroundColor(colors.white)
  24.     m.setTextColor(colors.black)
  25.     m.clear()
  26.     m.setCursorPos(1,1)
  27.     m.write(getdraw(j1x1))
  28.     m.setCursorPos(3,1)
  29.     m.write(getdraw(j1x2))
  30.     m.setCursorPos(5,1)
  31.     m.write(getdraw(j1x3))
  32.     m.setCursorPos(1,3)
  33.     m.write(getdraw(j2x1))
  34.     m.setCursorPos(3,3)
  35.     m.write(getdraw(j2x2))
  36.     m.setCursorPos(5,3)
  37.     m.write(getdraw(j2x3))
  38.     m.setCursorPos(1,5)
  39.     m.write(getdraw(j3x1))
  40.     m.setCursorPos(3,5)
  41.     m.write(getdraw(j3x2))
  42.     m.setCursorPos(5,5)
  43.     m.write(getdraw(j3x3))
  44.    
  45.     m.setCursorPos(10,1)
  46.     if i==0 then
  47.     m.write("O")
  48.     else
  49.     m.write("X")
  50.     end
  51. end
  52.  
  53.  
  54. function reset()
  55. j1x1=0
  56. j1x2=0
  57. j1x3=0
  58. j2x1=0
  59. j2x2=0
  60. j2x3=0
  61. j3x1=0
  62. j3x2=0
  63. j3x3=0
  64.  
  65.  
  66. i = 0
  67. drawfield()
  68. end
  69.  
  70.  
  71. function getdraw(x)
  72. if x == 1 then
  73. return "O"
  74. elseif x == 2 then
  75. return "X"
  76. else
  77. return "#"
  78. end
  79. end
  80. function testforwinner()
  81. if j1x1 == j1x2 and j1x1 == j1x3 then
  82. win(j1x1)
  83. elseif j2x1 == j2x2 and j2x1 == j2x3 then
  84. win(j2x1)
  85. elseif j3x1 == j3x2 and j3x1 == j3x3 then
  86. win(j3x1)
  87. elseif j1x1 == j2x1 and j1x1 == j3x1 then
  88. win(j1x1)
  89. elseif j1x2 == j2x2 and j1x2 == j3x2 then
  90. win(j1x2)
  91. elseif j1x3 == j2x3 and j1x3 == j3x3 then
  92. win(j1x3)
  93. elseif j1x1 == j2x2 and j1x1== j3x3 then
  94. win(j1x1)
  95. elseif j1x3 == j2x2 and j1x3 == j3x1 then
  96. win(j1x3)
  97. end
  98. end
  99.  
  100.  
  101. function win(winner)
  102. --zeigen wo wer gewonnen hat (blinken)
  103. sleep(2,5)
  104. if winner==1 then
  105. b = "O"
  106. else
  107. b= "X"
  108. end
  109. m.setBackgroundColor(colors.green)
  110. m.setTextColor(colors.white)
  111. m.setCursorPos(1,1)
  112. m.write(b.." gewinnt!")
  113. sleep(2,5)
  114. reset()
  115. end
  116.  
  117.  
  118. drawfield()
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. while true do
  128.   event, side, xPos, yPos = os.pullEvent("monitor_touch")
  129.  
  130.  
  131.   if i == 0 then
  132.     i=1
  133.     a=1
  134.   else
  135.     i=0
  136.     a=2
  137.   end
  138.  
  139.  
  140.   if yPos == 1 then
  141.     if xPos == 1 or xPos == 2 then
  142.      j1x1 = a
  143.     elseif xPos == 3 or xPos == 4 then
  144.       j1x2 = a
  145.     elseif xPos == 5 or xPos == 6 then
  146.       j1x3 = a
  147.     end
  148.   elseif yPos == 3 then
  149.     if xPos == 1 or xPos == 2 then
  150.       j2x1 = a
  151.     elseif xPos == 3 or xPos == 4 then
  152.       j2x2 = a
  153.     elseif xPos == 5 or xPos == 6 then
  154.       j2x3 = a
  155.     end
  156.   elseif yPos == 5 then
  157.     if xPos == 1 or xPos == 2 then
  158.       j3x1 =a
  159.     elseif xPos == 3 or xPos == 4 then
  160.       j3x2 = a
  161.     elseif xPos == 5 or xPos == 6 then
  162.       j3x3 = a
  163.     end
  164.   end
  165.  
  166.   drawfield()
  167.  
  168.   testforwinner()
  169.  
  170.  
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement