Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.07 KB | None | 0 0
  1. -- Shitty TicTacToe by Derpy Lermer
  2.  
  3. local MAIN_WINDOW_X, MAIN_WINDOW_Y = 1000, 500;
  4. local BOARD_SIZE = 3;
  5. local BOX_SIZE = 100;
  6. local BOX_OFSET_X = 100;
  7. local BOX_OFSET_Y = 100;
  8. local is_dragging, is_placing = false;
  9. local dragging_offset_x, dragging_offset_y;
  10. local CenterX, CenterY;
  11. local XorO = 0;
  12. local MRPD;
  13. local Xcount, Ocount;
  14. local MAIN_FONT = draw.CreateFont("Tahoma", 15, 15);
  15. local WIN_FONT = draw.CreateFont("Tahoma", 60, 60);
  16. local Win;
  17. local BOT_ENABLE = true;
  18. local Turns;
  19.  
  20. -- initialise board
  21. local Board = {}
  22. Turns = 0;
  23. for f=1, 3 do
  24. Board[f] = {}
  25. for g=1, 3 do
  26. Board[f][g] = 0
  27. end
  28. end
  29.  
  30. function CheckWin()
  31. Win = 0;
  32. for t = 1,2,1 do
  33. -- check horisontal rows
  34. for i = 1,3,1 do
  35. if (Board[i][1] == t) then
  36. if (Board[i][2] == t) then
  37. if (Board[i][3] == t) then
  38. Win = t;
  39. end
  40. end
  41. end
  42. end
  43. -- Vertical stuf
  44. for u = 1,3,1 do
  45. if (Board[1][u] == t) then
  46. if (Board[2][u] == t) then
  47. if (Board[3][u] == t) then
  48. Win = t;
  49. end
  50. end
  51. end
  52. end
  53. -- diagonal top left to b right
  54. if (Board[1][1] == t) then
  55. if (Board[2][2] == t) then
  56. if (Board[3][3] == t) then
  57. Win = t;
  58. end
  59. end
  60. end
  61. -- top right to b left
  62. if (Board[1][3] == t) then
  63. if (Board[2][2] == t) then
  64. if (Board[3][1] == t) then
  65. Win = t;
  66. end
  67. end
  68. end
  69.  
  70.  
  71. end
  72. -- if win is x or o
  73. if (Win == 1 or Win == 2) then
  74. draw.SetFont(WIN_FONT);
  75. if (Win == 1) then
  76. draw.Color(gui.GetValue("clr_gui_window_logo1"));
  77. draw.Text(0, 0, "X Has Won, Press Clear To Restart");
  78. end
  79. if (Win == 2) then
  80. draw.Color(gui.GetValue("clr_gui_window_logo1"));
  81. draw.Text(0, 0, "O Has Won, Press Clear To Restart");
  82. end
  83. end
  84. end
  85.  
  86.  
  87.  
  88. function DrawMainBox()
  89. draw.SetFont(MAIN_FONT);
  90. -- main 3x3 box
  91. for i = 0, 2, 1 do
  92. draw.FilledRect( MAIN_WINDOW_X , MAIN_WINDOW_Y + ( BOX_OFSET_Y * i) , (MAIN_WINDOW_X + BOX_SIZE), (MAIN_WINDOW_Y + BOX_SIZE + ( BOX_OFSET_Y * i)));
  93. draw.FilledRect((MAIN_WINDOW_X + BOX_OFSET_X) , MAIN_WINDOW_Y + ( BOX_OFSET_Y * i), (MAIN_WINDOW_X + BOX_OFSET_X + BOX_SIZE), (MAIN_WINDOW_Y + BOX_SIZE + ( BOX_OFSET_Y * i)));
  94. draw.FilledRect((MAIN_WINDOW_X + BOX_OFSET_X * 2), MAIN_WINDOW_Y + ( BOX_OFSET_Y * i), (MAIN_WINDOW_X + BOX_OFSET_X * 2 + BOX_SIZE), (MAIN_WINDOW_Y + BOX_SIZE + ( BOX_OFSET_Y * i)));
  95. end
  96.  
  97. -- lines inbetween 3x3 box horisontal
  98. for x = 0,3,1 do
  99. draw.Color(gui.GetValue("clr_gui_window_header"));
  100. draw.Line(MAIN_WINDOW_X + (BOX_OFSET_X * x), MAIN_WINDOW_Y, MAIN_WINDOW_X + (BOX_OFSET_X * x) , MAIN_WINDOW_Y + 300);
  101. end
  102.  
  103. -- lines inbetween vertcal
  104. for x = 0,3,1 do
  105. draw.Color(gui.GetValue("clr_gui_window_header"));
  106. draw.Line(MAIN_WINDOW_X , MAIN_WINDOW_Y + (BOX_OFSET_Y * x) , MAIN_WINDOW_X +300 , MAIN_WINDOW_Y + (BOX_OFSET_Y * x));
  107. end
  108.  
  109. -- Header to move it
  110. local HEADER_WIDTH = BOARD_SIZE * BOX_SIZE;
  111. local HEADER_HEIGHT = 20;
  112. draw.Color(gui.GetValue("clr_gui_window_header"));
  113. draw.FilledRect(MAIN_WINDOW_X, MAIN_WINDOW_Y - HEADER_HEIGHT, MAIN_WINDOW_X + HEADER_WIDTH, MAIN_WINDOW_Y );
  114.  
  115. local mouse_x, mouse_y = input.GetMousePos();
  116. local left_mouse_down = input.IsButtonDown(1);
  117. -- cpp from ShadyRetard
  118. if (is_dragging == true and left_mouse_down == false) then
  119. is_dragging = false;
  120. dragging_offset_x = 0;
  121. dragging_offset_y = 0;
  122. end
  123. if (left_mouse_down == true) then
  124. dragHandler(HEADER_HEIGHT);
  125. end
  126.  
  127. if(is_placing == true and left_mouse_down == false) then
  128. is_placing = flase;
  129. end
  130.  
  131. if(mouse_x >= MAIN_WINDOW_X and mouse_x <= MAIN_WINDOW_X + 300 and mouse_y >= MAIN_WINDOW_Y and mouse_y <= MAIN_WINDOW_Y + 300) then
  132. PlaceHandler();
  133. end
  134.  
  135. if(mouse_x >= MAIN_WINDOW_X + 250 and mouse_x <= MAIN_WINDOW_X + 300 and mouse_y >= MAIN_WINDOW_Y - 20 and mouse_y <= MAIN_WINDOW_Y) then
  136. if (left_mouse_down) then
  137. ClearBoard()
  138. end
  139. end
  140.  
  141. -- clear button for single player
  142. local Clear_Width = 50;
  143. local Clear_Height = 20;
  144. draw.FilledRect(MAIN_WINDOW_X+250, MAIN_WINDOW_Y - 20, MAIN_WINDOW_X + 250 + Clear_Width, MAIN_WINDOW_Y);
  145. draw.Color(250,250,250,250);
  146. draw.OutlinedRect(MAIN_WINDOW_X+250, MAIN_WINDOW_Y - 20, MAIN_WINDOW_X + 250 + Clear_Width, MAIN_WINDOW_Y);
  147. draw.Color(gui.GetValue("clr_gui_window_logo1"));
  148. draw.Text(MAIN_WINDOW_X + 260, MAIN_WINDOW_Y -17, "Clear");
  149.  
  150. end
  151.  
  152.  
  153. -- drawin things ontop od the grid
  154. function Place(CenterX, CenterY, XorO)
  155. if (XorO == 1) then
  156. -- draws a simple x
  157. draw.Color(gui.GetValue("clr_gui_window_header"));
  158. draw.Line(CenterX - 30, CenterY - 30, CenterX + 30, CenterY + 30);
  159. draw.Line(CenterX - 30, CenterY + 30, CenterX + 30, CenterY - 30);
  160.  
  161. end
  162. if (XorO == 2) then
  163. -- uses a rectangle cos a circle is too hard
  164. draw.Color(gui.GetValue("clr_gui_window_header"));
  165. draw.OutlinedRect(CenterX - 30, CenterY - 30, CenterX + 30, CenterY + 30);
  166. end
  167. end
  168.  
  169. function UpdateBoard()
  170. -- updates the position of xs and os relative to the main window pos
  171. for i = 1,3,1 do
  172. for o = 1,3,1 do
  173. if (Board[i][o] == 1) then
  174. Place((MAIN_WINDOW_X + 50 + (BOX_SIZE * (i-1))), (MAIN_WINDOW_Y + 50 + (BOX_SIZE * (o-1))), 1);
  175. end
  176. if (Board[i][o] == 2) then
  177. Place((MAIN_WINDOW_X + 50 + (BOX_SIZE * (i-1))), (MAIN_WINDOW_Y + 50 + (BOX_SIZE * (o-1))), 2);
  178. end
  179.  
  180. end
  181. end
  182. end
  183.  
  184. function ClearBoard()
  185. -- self explanatory
  186. for f=1, 3 do
  187. Board[f] = {}
  188. for g=1, 3 do
  189. Board[f][g] = 0
  190. end
  191. end
  192. Turns = 0;
  193. end
  194.  
  195. function PlaceHandler()
  196. -- handles all placing bs
  197. local mouse_x, mouse_y = input.GetMousePos();
  198. local GridPosX, GridPosY;
  199. local left_place_down = input.IsButtonDown(1);
  200. for u = 0,2,1 do
  201. -- checks the pos of mous to see which tile it is over
  202. if (mouse_x >= MAIN_WINDOW_X + (BOX_SIZE * u) and mouse_x <= MAIN_WINDOW_X + (BOX_SIZE * (u + 1))) then
  203. CenterX = (MAIN_WINDOW_X + 50 + (BOX_SIZE * u));
  204. GridPosX = u;
  205.  
  206. end
  207. -- checks the pos of mous to see which tile it is over
  208. if (mouse_y >= MAIN_WINDOW_Y + (BOX_SIZE * u) and mouse_y <= MAIN_WINDOW_Y + (BOX_SIZE * (u + 1))) then
  209. CenterY = (MAIN_WINDOW_Y + 50 + (BOX_SIZE * u));
  210. GridPosY = u;
  211. end
  212. end
  213. if (left_place_down) then
  214. -- toggles between x and o on each turn
  215. if MRPD == 1 then
  216. if (Board[GridPosX +1][GridPosY +1] == 0) then
  217. Board[GridPosX +1][GridPosY +1] = 2;
  218. Turns = Turns + 1;
  219. end
  220. end
  221. if MRPD == 2 then
  222. if (Board[GridPosX+1][GridPosY +1] == 0) then
  223. Board[GridPosX+1][GridPosY +1] = 1;
  224. Turns = Turns + 1;
  225. end
  226. end
  227. end
  228. Xcount = 0;
  229. Ocount = 0;
  230.  
  231. -- checks whether an x or an o should be placed by seeing howmany have already been placed
  232. for i = 1,3,1 do
  233. for o = 1,3,1 do
  234. if (Board[i][o] == 1) then
  235. Xcount = Xcount +1;
  236. end
  237. if (Board[i][o] == 2) then
  238. Ocount = Ocount + 1;
  239. end
  240. end
  241. end
  242. if (Xcount == Ocount) then
  243. MRPD = 2;
  244. elseif (Xcount > Ocount) then
  245. MRPD = 1;
  246. if (BOT_ENABLE) then
  247. if (Turns <= 8 ) then
  248. bot();
  249. end
  250. end
  251. end
  252. end
  253.  
  254. function bot()
  255. local placed = false;
  256. while (placed == false) do
  257. local x = math.random(1,3);
  258. local y = math.random(1,3);
  259. if (Board[x][y] == 0) then
  260. Board[x][y] = 2;
  261. placed = true;
  262. Turns = Turns + 1;
  263. end
  264. end
  265. end
  266.  
  267. function dragHandler(HEADER_HEIGHT)
  268. local mouse_x, mouse_y = input.GetMousePos();
  269. -- cpp from ShadyRetard again
  270. if (is_dragging == true) then
  271. MAIN_WINDOW_X = mouse_x - dragging_offset_x;
  272. MAIN_WINDOW_Y = mouse_y - dragging_offset_y;
  273. return;
  274. end
  275.  
  276. if (( mouse_x >= MAIN_WINDOW_X and mouse_x <= (MAIN_WINDOW_X + 300)) and (mouse_y <= MAIN_WINDOW_Y) and (mouse_y >= MAIN_WINDOW_Y - HEADER_HEIGHT)) then
  277. is_dragging = true;
  278. dragging_offset_x = mouse_x - MAIN_WINDOW_X;
  279. dragging_offset_y = mouse_y - MAIN_WINDOW_Y;
  280. return;
  281. end
  282. end
  283.  
  284. --for main drawing
  285. callbacks.Register("Draw", DrawMainBox);
  286. --for movement of stuff
  287. callbacks.Register("Draw", UpdateBoard);
  288. --checks the board constantly as calling it after turn didnt work as expected
  289. callbacks.Register("Draw", CheckWin);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement