tictactoe_2.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. event.load("block_place");
  2. event.load("block_click");
  3. event.load("function_key");
  4. games_world = world.getGames();
  5. winning_blocks = list.new();
  6. playing_players = list.new();
  7. start_loc = loc.new(games_world, start_x, start_y, start_z);
  8. placed_blocks = list.new();
  9. list.add(placed_blocks, start_loc);
  10. dimension = 3;
  11. dem_array = array-new[dimension - 3]
  12. for(a = 0; a < dimension - 2; a++){
  13. dem_array[a] = 0;
  14. }
  15. length = 3;
  16. @start
  17. list.add(placed_blocks, start_loc);
  18. block.set(start_loc, "minecraft:stone");
  19. goto("wait");
  20. @end
  21. for(a = 0; a < list.getSize(placed_blocks); a++){
  22. block.set(list.getIndex(placed_blocks, a), "minecraft:air");
  23. }
  24. list.clear(placed_blocks);
  25. goto("start");
  26. @wait
  27. wait();
  28. if(event == "block_click"){
  29. if(list.contains(ttt_start_loc_list, block_loc)){
  30. list.add(playing_players, player);
  31. }
  32. }
  33. if(event == "block_place"){
  34. if(list.contains(playing_players, player)){
  35. if(is.tictactoe_placeable(block_loc)){
  36. cancel = false;
  37. temp_array = array.new[dimension - 2];
  38. temp_array[0] = block_loc;
  39. list.add(placed_blocks, temp_array)
  40. if(is.triplet(block_loc)){
  41. goto("tic_tac_toe_win");
  42. }
  43. }
  44. }
  45. }
  46. if(event == "function_key"){
  47. if(list.contains(playing_players, player)){
  48. }
  49. }
  50. goto("wait");
  51. @tic_tac_toe_win
  52. msg(player, "You won.");
  53. for(a = 0; a < list.getSize(tic_tac_toe_blocks); a++){
  54. block.set(list.getIndex(tic_tac_toe_blocks, a), "minecraft:air");
  55. }
  56. list.add(winning_blocks, block_loc);
  57. for(a = 0; a < list.getSize(winning_blocks); a++){
  58. block.set(list.getIndex(winning_blocks, a), "minecraft:red_wool");
  59. }
  60. list.clear(winning_blocks);
  61. list.clear(playing_players);
  62. goto("wait");
  63. function is.tictactoe_placeable(block_loc){
  64. if(!ttt.has_neighbours){
  65. return false;
  66. }
  67. loc.addY(block_loc, -1);
  68. if(block.isAir(block_loc)){
  69. loc.addY(block_loc, 1);
  70. return false;
  71. }
  72. loc.addY(block_loc, 1);
  73. return true;
  74. }
  75. function ttt.has_neighbours(block_loc){
  76. temp_loc = loc.mod(block_loc, 1, 0, 0);
  77. if(list.contains(placed_blocks, temp_loc)){
  78. return true;
  79. }
  80. temp_loc = loc.mod(block_loc, -1, 0, 0);
  81. if(list.contains(placed_blocks, temp_loc)){
  82. return true;
  83. }
  84. temp_loc = loc.mod(block_loc, 0, 1, 0);
  85. if(list.contains(placed_blocks, temp_loc)){
  86. return true;
  87. }
  88. temp_loc = loc.mod(block_loc, 0, -1, 0);
  89. if(list.contains(placed_blocks, temp_loc)){
  90. return true;
  91. }
  92. temp_loc = loc.mod(block_loc, 0, 0, 1);
  93. if(list.contains(placed_blocks, temp_loc)){
  94. return true;
  95. }
  96. temp_loc = loc.mod(block_loc, 0, 0, -1);
  97. if(list.contains(placed_blocks, temp_loc)){
  98. return true;
  99. }
  100. return false;
  101. }
  102. function is.triplet(block_loc){
  103. if(triplet.straight(block_loc)){
  104. return true;
  105. }
  106. if(triplet.cross(block_loc)){
  107. return true;
  108. }
  109. if(triplet.cross_cross(block_loc)){
  110. return true;
  111. }
  112. return false;
  113. }
  114. function triplet.straight(block_loc){
  115. if(triplet.loop(block_loc, 1, 0, 0)){
  116. return true;
  117. }
  118. if(triplet.loop(block_loc, 0, 1, 0)){
  119. return true;
  120. }
  121. if(triplet.loop(block_loc, 0, 0, 1)){
  122. return true;
  123. }
  124. list.clear($winning_blocks);
  125. return false;
  126. }
  127. function triplet.cross(block_loc){
  128. if(triplet.loop(block_loc, 1, 0, 1)){
  129. return true;
  130. }
  131. if(triplet.loop(block_loc, 1, 0, -1)){
  132. return true;
  133. }
  134. if(triplet.loop(block_loc, 0, 1, 1)){
  135. return true;
  136. }
  137. if(triplet.loop(block_loc, 0, 1, -1)){
  138. return true;
  139. }
  140. if(triplet.loop(block_loc, 1, 1, 0)){
  141. return true;
  142. }
  143. if(triplet.loop(block_loc, -1, 1, 0)){
  144. return true;
  145. }
  146. list.clear($winning_blocks);
  147. return false;
  148. }
  149. function triplet.cross_cross(block_loc){
  150. if(triplet.loop(block_loc, 1, 1, 1)){
  151. return true;
  152. }
  153. if(triplet.loop(block_loc, -1, 1, 1)){
  154. return true;
  155. }
  156. if(triplet.loop(block_loc, 1, -1, 1)){
  157. return true;
  158. }
  159. if(triplet.loop(block_loc, 1, 1, -1)){
  160. return true;
  161. }
  162. list.clear($winning_blocks);
  163. return false;
  164. }
  165. function triplet.loop(block_loc, x,y,z){
  166. list.clear($winning_blocks);
  167. temp_block_count = 1;
  168. loc.add(block_loc, -($length-1)*x, -($length-1)*y, -($length-1)*z);
  169. for(a = 0; a < 2 * $length - 1; a++){
  170. temp_block_count = temp_block_count + is.blockType(block_loc);
  171. loc.add(block_loc,x, y ,z);
  172. }
  173. loc.add(block_loc, -($length-1)*x, -($length-1)*y, -($length-1)*z);
  174. if(temp_block_count > $length){
  175. return true;
  176. }
  177. return false;
  178. }
  179. function is.blockType(block_loc){
  180. if(block.getType(block_loc) == $block_type){
  181. list.add($winning_blocks, loc.mod(block_loc,0,0,0));
  182. return 1;
  183. }
  184. return 0;
  185. }