1. class CompII
  2. FOUR_MOVE = {[1, 5, 7, 8] => 3, [3, 5, 8, 9] => 1, [1, 2, 5, 7] => 9,
  3. [3, 2, 5, 9] => 7, [7, 4, 5, 9] => 3, [9, 6, 5, 7] => 1,
  4. [1, 4, 5, 3] => 9, [3, 6, 5, 1] => 7, [1, 4, 5, 8] => 9,
  5. [3, 6, 5, 8] => 1, [2, 3, 4, 5] => 7, [4, 5, 8, 9] => 3,
  6. [2, 5, 4, 7] => 3, [2, 5, 6, 9] => 7, [6, 5, 2, 1] => 9,
  7. [6, 5, 8, 7] => 3, [2, 5, 7, 9] => 3, [6, 5, 7, 1] => 9,
  8. [8, 5, 1, 3] => 7, [4, 5, 3, 9] => 1, [3, 4, 5, 8] => 7,
  9. [2, 4, 5, 9] => 1, [2, 5, 6, 7] => 3, [1, 5, 6, 8] => 9}
  10. FOUR_MOVE2 = {[1, 5, 7, 8] => 9, [3, 5, 8, 9] => 7, [1, 2, 5, 7] => 3,
  11. [3, 2, 5, 9] => 1, [7, 4, 5, 9] => 1, [9, 6, 5, 7] => 3,
  12. [1, 4, 5, 3] => 1, [3, 6, 5, 1] => 9, [2, 5, 7, 9] => 1,
  13. [6, 5, 7, 1] => 3, [8, 5, 1, 3] => 9, [4, 5, 3, 9] => 7}
  14. THREE_MOVE = {[1, 2, 5] => 8, [1, 4, 5] => 6, [3, 2, 5] => 8,
  15. [3, 6, 5] => 4, [9, 6, 5] => 4, [9, 8, 5] => 2,
  16. [7, 8, 5] => 2, [7, 4, 5] => 6, [1, 5, 7] => 3,
  17. [3, 5, 9] => 1, [1, 2, 4] => 3, [2, 3, 6] => 1,
  18. [4, 7, 8] => 1, [6, 9, 8] => 3, [1, 4, 8] => 7,
  19. [7, 4, 2] => 1, [2, 5, 7] => 8, [2, 5, 9] => 8,
  20. [8, 5, 1] => 2, [8, 5, 3] => 2, [3, 6, 8] => 9,
  21. [9, 6, 2] => 3, [4, 5, 3] => 6, [4, 5, 9] => 6,
  22. [6, 5, 1] => 4, [6, 5, 7] => 4, [1, 2, 9] => 3,
  23. [1, 4, 9] => 7, [3, 2, 7] => 1, [3, 6, 7] => 9,
  24. [9, 6, 1] => 3, [9, 8, 1] => 7, [7, 4, 3] => 1,
  25. [7, 8, 3] => 9, [2, 5, 4] => 8, [2, 5, 6] => 4,
  26. [8, 5, 4] => 2, [8, 5, 6] => 2, [1, 5, 3] => 7,
  27. [7, 5, 9] => 1, [1, 4, 3] => 2, [2, 3, 9] => 6,
  28. [6, 9, 7] => 8, [7, 8, 1] => 4}
  29. THREE_MOVE2 = {[1, 5, 7] => 9, [3, 5, 9] => 7, [1, 2, 4] => 7, [2, 3, 6] => 9,
  30. [4, 7, 8] => 9, [6, 9, 8] => 7, [2, 5, 4] => 6, [2, 5, 6] => 8,
  31. [8, 5, 4] => 6, [8, 5, 6] => 4, [1, 5, 3] => 9, [7, 5, 9] => 3,
  32. [4, 5, 9] => 1, [1, 4, 8] => 6, [7, 4, 5] => 3, [7, 8, 5] => 3}
  33. TWO_MOVE = {[1, 2] => 3, [1, 4] => 7, [1, 5] => 9, [1, 3] => 2, [1, 7] => 4,
  34. [1, 9] => 5, [3, 2] => 1, [3, 6] => 9, [3, 5] => 7, [3, 9] => 6,
  35. [3, 7] => 5, [7, 4] => 1, [7, 8] => 9, [7, 5] => 3, [7, 9] => 8,
  36. [9, 5] => 1, [9, 6] => 3, [9, 8] => 7, [4, 5] => 6, [5, 6] => 4}
  37. ONE_MOVE = {[1] => [2, 4, 5], [2] => [1, 3, 5], [3] => [2, 6, 5], [4] => [1, 7, 5],
  38. [5] => [1, 2, 3, 4, 6, 7, 8, 9], [6] => [3, 9, 5], [7] => [4, 8, 5],
  39. [8] => [7, 9, 5], [9] => [6, 8, 5]}
  40. def comp_move(pl_moves)
  41. case pl_moves.size
  42. when 1
  43. ONE_MOVE.each do |possible_pl_moves, possible_comp_moves|
  44. if possible_pl_moves - pl_moves == []
  45. comp_move = possible_comp_moves.sample.to_i
  46. return comp_move
  47. end
  48. end
  49. when 2
  50. TWO_MOVE.each do |possible_pl_moves, comp_move|
  51. if possible_pl_moves - pl_moves == []
  52. return comp_move
  53. end
  54. end
  55. when 3
  56. THREE_MOVE.each do |possible_pl_moves, comp_move|
  57. if possible_pl_moves - pl_moves == []
  58. return comp_move
  59. else
  60. THREE_MOVE2.each do |possible_pl_moves, co_move|
  61. if possible_pl_moves - pl_moves == []
  62. return co_move
  63. end
  64. end
  65. end
  66. end
  67. when 4
  68. FOUR_MOVE.each do |possible_pl_moves, comp_move|
  69. if possible_pl_moves - pl_moves == []
  70. return comp_move
  71. else
  72. FOUR_MOVE2.each do |possible_pl_moves, co_move|
  73. if possible_pl_moves - pl_moves == []
  74. return co_move
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end
  81. end