Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. getBinaryTable[table_] :=
  2. Map[PadLeft[IntegerDigits[#, 2], 3] &, table];
  3. getParityTable[plusParityTable_] :=
  4. Table[Mod[plusParityTable[[x]], 2], {x, 1, 3}];
  5. getPlusListBinaryTable[tableBinary_] :=
  6. tableBinary[[1]] + tableBinary[[2]] + tableBinary[[3]] +
  7. tableBinary[[4]];
  8. getAdvantage[finalParityTable_] :=
  9. If[finalParityTable == {0, 0, 0}, "player", "computer"];
  10. getMatch[finalParityTable_] :=
  11. If[finalParityTable == {0, 0, 0}, "player", "computer"];
  12. getMatchTable[table_] :=
  13. Table[Table["|", {x, 1, table[[x]]}] // StringJoin, {x, 1, 4}];
  14. setTable[line_, number_] :=
  15. If[table[[line]] - number < 0, Print["Error"],
  16. table[[line]] = table[[line]] - number];
  17. endDetect[table_] :=
  18. If[table == {0, 0, 0, 0}, Print["The game is finished"]]
  19. getCurrentParityTable[table_] :=
  20. getParityTable[getPlusListBinaryTable[getBinaryTable[table]]]
  21.  
  22. computerPlay[currentParityTable_] := Switch[currentParityTable,
  23. {0, 0, 1},
  24. For[i = 1, i <= 4, i += 4,
  25. If[table[[i]] > 0, table[[i]] -= 1, i -= 3]],
  26. {0, 1, 0},
  27. For[i = 1, i <= 4, i += 4,
  28. If[table[[i]] == 2 || table[[i]] == 3 || table[[i]] == 6 ||
  29. table[[i]] == 7, table[[i]] -= 2, i -= 3]],
  30. {1, 0, 0},
  31. For[i = 1, i <= 4, i += 4,
  32. If[table[[i]] == 4 || table[[i]] == 5 || table[[i]] == 6 ||
  33. table[[i]] == 7, table[[i]] -= 4, i -= 3]],
  34. {1, 0, 0},
  35. For[i = 1, i <= 4, i += 4, table[[i]];
  36. Which[2, table[[i]] -= 1, 4, table[[i]] -= 1, 6, table[[i]] -= 1,
  37. i -= 3]],
  38. {0, 1, 1}, For[i = 1, i <= 4, i ++, Switch[table[[i]],
  39. 2, table[[i]] -= 1,
  40. 4, table[[i]] -= 1,
  41. 6, table[[i]] -= 1,
  42. 3, table[[i]] -= 3,
  43. 7, table[[i]] -= 3
  44. ; i -= 3
  45.  
  46. ]] ,
  47. {1, 0, 1}, For[i = 1, i <= 4, i += 4, Switch[table[[i]],
  48. 4, table[[i]] -= 3,
  49. 6, table[[i]] -= 3,
  50. 5, table[[i]] -= 5,
  51. 7, table[[i]] -= 5; i -= 3
  52. ]] ,
  53. {1, 1, 0}, For[i = 1, i <= 4, i += 4, Switch[table[[i]],
  54. 4, table[[i]] -= 2,
  55. 5, table[[i]] -= 2,
  56. 6, table[[i]] -= 6,
  57. 7, table[[i]] -= 6; i -= 3
  58. ]] ,
  59. {1, 1, 1}, For[i = 1, i <= 4, i += 4, Switch[table[[i]],
  60. 4, table[[i]] -= 1,
  61. 5, table[[i]] -= 3,
  62. 6 , table[[i]] -= 5,
  63. 7, table[[i]] -= 7 ; i -= 3
  64. ]]
  65.  
  66. ];
  67.  
  68.  
  69. table = {};
  70. For[i = 1, i < 8, i += 2, AppendTo[table, RandomInteger[{0, i}]]];
  71. table = {1, 2, 4, 4};
  72. Print[table]
  73. binaryTable = getBinaryTable[table];
  74. Print[binaryTable]
  75. plusParityTable = getPlusListBinaryTable[binaryTable];
  76. Print[plusParityTable]
  77. finalParityTable = getParityTable[plusParityTable];
  78. Print[finalParityTable]
  79. advantage = getAdvantage[finalParityTable];
  80. Print[advantage]
  81. Dynamic[matchTable = getMatchTable[table]]
  82.  
  83. computerPlay[finalParityTable]
  84.  
  85. Dynamic[
  86. Panel[
  87. Panel[
  88. Column[
  89. {RadioButton[Dynamic[line], 1],
  90. RadioButton[Dynamic[line], 2],
  91. RadioButton[Dynamic[line], 3],
  92. RadioButton[Dynamic[line], 4],
  93. Text["Line" Dynamic[line]]}]
  94. Column[{
  95. InputField[Dynamic[number], Number],
  96. Text["Number of matches " Dynamic[number]]}]
  97. Button["Process",
  98. setTable[line, number] Print[getCurrentParityTable[table]]]]
  99. Panel[
  100. Column[{
  101. matchTable[[1]],
  102. matchTable[[2]],
  103. matchTable[[3]],
  104. matchTable[[4]]
  105. }, Center]
  106. ]
  107. ]
  108. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement