pastebin light

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

pastebin light //misacek

Posted by Game.java on Sat 19 Nov 2011 19:32:52 CET
download | new post

  1.  
  2. import java.util.*;
  3.  
  4. class Vyjimka extends Exception {
  5. String message;
  6.  
  7. Vyjimka(String m) {
  8. message = m;
  9. }
  10. }
  11.  
  12. class Options {
  13. static int succeeded = 0;
  14. static int succeeded_hamming = 0;
  15. static int succeeded_manhattan = 0;
  16. static int failed = 0;
  17. static int total = 20;
  18. static boolean type = false; // false = manhattan, true = hamming
  19. }
  20.  
  21. class VariantList {
  22.  
  23. public static List<Variant> list = new ArrayList<Variant>();
  24. public static Integer shortest_path = null;
  25.  
  26. public static void add(Variant v) {
  27. // System.out.println("Pridavam\n" + v.info());
  28. if (list.size() > 0) {
  29. for (int i = 0; i < list.size(); i++) {
  30. if (v.fnc() <= list.get(i).fnc()) {
  31. list.add(i, v);
  32. return;
  33. }
  34. }
  35. }
  36. list.add(v);
  37. }
  38.  
  39. public static boolean all_checked() {
  40. Enumeration i = Collections.enumeration(list);
  41. while (i.hasMoreElements())
  42. if (((Variant)(i.nextElement())).checked != true)
  43. return false;
  44. return true;
  45. }
  46.  
  47. public static Variant find_variant(Variant find) {
  48. Enumeration e = Collections.enumeration(list);
  49. while (e.hasMoreElements()) {
  50. Variant v = (Variant)(e.nextElement());
  51. for (int pos = 0; pos <= 9; pos++) {
  52. if (pos == 9) return v;
  53. String fromV = "" + v.tile.elementAt(pos);
  54. String fromFind = "" + find.tile.elementAt(pos);
  55. if (Integer.parseInt(fromV) != Integer.parseInt(fromFind))
  56. break;
  57. }
  58. }
  59. return null;
  60. }
  61.  
  62. }
  63.  
  64. class Variant {
  65.  
  66. static Vector<Integer> final_tile = new Vector<Integer>();
  67. static int turns_max = 2000;
  68. static int turns_actual = 0;
  69.  
  70. Vector<Integer> tile;
  71. boolean checked;
  72. Variant parent;
  73. Integer value;
  74.  
  75. Variant(Vector<Integer> tile) {
  76. this.tile = (Vector<Integer>)tile.clone();
  77. }
  78.  
  79. Variant(Variant parent, Vector<Integer> tile) throws Vyjimka
  80. {
  81. this.tile = (Vector<Integer>)tile.clone();
  82. this.checked = false;
  83. this.parent = parent;
  84. this.value = (parent != null) ? parent.value + 1 : 0;
  85.  
  86. if (this.value > 50) {
  87. // System.out.println("Cesta delsi nez limit, koncim...");
  88. return;
  89. }
  90.  
  91. if (VariantList.shortest_path != null)
  92. if (VariantList.shortest_path < value)
  93. return;
  94.  
  95. Variant examined = VariantList.find_variant(this);
  96. if (examined != null) {
  97. if (this.value < examined.value) {
  98. examined.value = this.value;
  99. examined.parent = parent;
  100. }
  101. // System.out.println("Duplicitni, rusim hledani");
  102. return;
  103. }
  104.  
  105. VariantList.add(this);
  106.  
  107. if (this.is_solved()) {
  108. VariantList.shortest_path = this.value;
  109. this.print_path();
  110. throw new Vyjimka("");
  111. }
  112. }
  113.  
  114. static Vector<Integer> swap(Vector<Integer> tile, int s1, int s2) {
  115. tile.set(s1, new Integer(tile.elementAt(s1) + tile.elementAt(s2)));
  116. tile.set(s2, new Integer(tile.elementAt(s1) - tile.elementAt(s2)));
  117. tile.set(s1, new Integer(tile.elementAt(s1) - tile.elementAt(s2)));
  118. return tile;
  119. }
  120.  
  121. void negotiate_next() throws Vyjimka {
  122. int i = tile.indexOf(0); // index 0 v poli
  123. if (i != 0 && i != 3 && i != 6)
  124. negotiate_successor(tile, i, i-1);
  125. if (i != 2 && i != 5 && i != 8)
  126. negotiate_successor(tile, i, i+1);
  127. if (i != 0 && i != 1 && i != 2)
  128. negotiate_successor(tile, i, i-3);
  129. if (i != 6 && i != 7 && i != 8)
  130. negotiate_successor(tile, i, i+3);
  131. }
  132.  
  133. void negotiate_successor(Vector<Integer> prev_tile, int s1, int s2) throws Vyjimka {
  134. Vector<Integer> new_tile = Variant.swap((Vector<Integer>)prev_tile.clone(), s1, s2);
  135. Variant v = VariantList.find_variant(new Variant(new_tile));
  136. if (v == null)
  137. v = new Variant(this, new_tile);
  138. }
  139.  
  140. boolean is_solved() {
  141. return (this.tile.elementAt(0) == 1 && this.tile.elementAt(1) == 2 && this.tile.elementAt(2) == 3 &&
  142. this.tile.elementAt(3) == 8 && this.tile.elementAt(4) == 0 && this.tile.elementAt(5) == 4 &&
  143. this.tile.elementAt(6) == 7 && this.tile.elementAt(7) == 6 && this.tile.elementAt(8) == 5);
  144. }
  145.  
  146. void print_info() {
  147. System.out.println(info());
  148. }
  149.  
  150. String info() {
  151. Enumeration e = tile.elements();
  152. int i = 0;
  153. String s = "";
  154. while (e.hasMoreElements()) {
  155. Integer n = ((Integer)(e.nextElement()));
  156. s += n;
  157. if (i == 2 || i == 5 || i == 8)
  158. s += "\n";
  159. else
  160. s += " ";
  161. i++;
  162. }
  163. return s;
  164. }
  165.  
  166. Integer fnc() {
  167. return (Options.type == false) ? manhattan() : hamming();
  168. }
  169.  
  170. Integer steps_between(int i, int j) {
  171. if (i == j )return 0;
  172. return Math.abs((i/3)-(j/3)) + Math.abs((i%3)-(j%3));
  173. }
  174.  
  175. Integer manhattan() {
  176. int sum = 0;
  177. for (int i = 0; i <=8; i++) {
  178. sum += steps_between(this.tile.indexOf(i), Variant.final_tile.indexOf(i));
  179. }
  180. return sum;
  181. }
  182.  
  183. Integer hamming() {
  184. int count = 0;
  185. for (int i = 0; i <=8; i++) {
  186. if (this.tile.elementAt(i) != Variant.final_tile.elementAt(i))
  187. count++;
  188. }
  189. return new Integer(count);
  190. }
  191.  
  192. void print_path() {
  193. System.out.println("Delka: " + value);
  194. String path_string = "";
  195. Variant help = this;
  196. while (help.parent != null) {
  197. path_string = help.info() + "\n" + path_string;
  198. help = help.parent;
  199. }
  200. System.out.println("Cesta:\n" + path_string);
  201. System.out.println("Delka nalezene cesty: " + value + "\n");
  202. }
  203.  
  204. }
  205.  
  206. public class Game {
  207.  
  208. public static void main(String[] args) {
  209.  
  210. // LOOP BEGIN
  211. for (int i = Options.total; i > 0; i--) {
  212. System.out.println("Pokus cislo " + (20-i+1));
  213. // LOOP BEGIN
  214.  
  215. // vytvoreni statickeho finaloveho vzoru
  216. Variant.final_tile.add(1);
  217. Variant.final_tile.add(2);
  218. Variant.final_tile.add(3);
  219. Variant.final_tile.add(8);
  220. Variant.final_tile.add(0);
  221. Variant.final_tile.add(4);
  222. Variant.final_tile.add(7);
  223. Variant.final_tile.add(6);
  224. Variant.final_tile.add(5);
  225.  
  226. // vytvoreni nahodneho vzoru
  227. Vector v = new Vector();
  228. Random r = new Random();
  229. // v.add(8); v.add(1); v.add(3);
  230. // v.add(7); v.add(4); v.add(5);
  231. // v.add(6); v.add(2); v.add(0);
  232. v.add(1);
  233. v.add(r.nextInt(v.size()), 2);
  234. v.add(r.nextInt(v.size()), 3);
  235. v.add(r.nextInt(v.size()), 4);
  236. v.add(r.nextInt(v.size()), 5);
  237. v.add(r.nextInt(v.size()), 6);
  238. v.add(r.nextInt(v.size()), 7);
  239. v.add(r.nextInt(v.size()), 8);
  240. v.add(r.nextInt(v.size()), 0);
  241.  
  242. System.out.print("Vzorec:\n" + new Variant(v).info());
  243.  
  244. for (int j = 0; j <=1; j++) {
  245. try {
  246. Options.type = (Options.type == true) ? false : true;
  247. System.out.println("Funkce: " + (Options.type == false ? "Manhattan" : "Hamming"));
  248.  
  249. Variant top = new Variant(null, v);
  250. while (true) {
  251.  
  252. Variant.turns_actual++;
  253. if (Variant.turns_actual > Variant.turns_max) {
  254. System.out.println("Nenalezeno reseni po " + Variant.turns_max + " krocich, koncim...");
  255. break;
  256. }
  257.  
  258. // if (VariantList.all_checked())
  259. // break;
  260. // System.out.println("Mam " + VariantList.list.size() + " variant");
  261. Enumeration e = Collections.enumeration(VariantList.list);
  262. while (e.hasMoreElements()) {
  263. Variant a = (Variant)(e.nextElement());
  264. // System.out.println("Vyjednavani z " + a.info());
  265. if (a.checked != true) {
  266. a.checked = true;
  267. try {a.negotiate_next();} catch (Vyjimka x) {System.out.println(x.message);return;}
  268. break;
  269. }
  270. }
  271. }
  272. } catch (Vyjimka x) {}
  273. finally {
  274. VariantList.list.clear();
  275. VariantList.shortest_path = null;
  276. Variant.turns_actual = 0;
  277. continue;
  278. }
  279. }
  280.  
  281. // LOOP END
  282. }
  283. // LOOP END
  284. }
  285.  
  286. }
  287.  

Syntax highlighting: