Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 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; endDetect[table, 1];
  17. computerPlay[getCurrentParityTable[table]];
  18. If[haveAWinner == 1, , endDetect[table, 0]]];
  19. endDetect[table_, winner_ ] :=
  20. If[table == {0, 0, 0, 0}, Print["The game is finished"];
  21. If[winner == 1, Print["The player won"]; haveAWinner = 1,
  22. Print["The computer won"]]]
  23. getCurrentParityTable[table_] :=
  24. getParityTable[getPlusListBinaryTable[getBinaryTable[table]]]
  25.  
  26. computerPlay[tempTable_] := Switch[For[i = 1, i < 8, i++,
  27. For[l = 1, l < 5, l++,
  28. temptable = table;
  29. If[temptable[[l]] - 1 < 0, , temptable[[l]] -= i];
  30. If[getCurrentParityTable[temptable] == {0, 0, 0},
  31. table = temptable; i = 10; l = 6]
  32. ]
  33. ]
  34. ];
  35.  
  36.  
  37. table = {};
  38. haveAWinner = "Null";
  39. action = "";
  40. lineC = "";
  41. For[i = 1, i < 8, i += 2, AppendTo[table, RandomInteger[{0, 7}]]];
  42.  
  43. Print[table]
  44.  
  45. binaryTable = getBinaryTable[table];
  46.  
  47. plusParityTable = getPlusListBinaryTable[binaryTable];
  48.  
  49. finalParityTable = getParityTable[plusParityTable];
  50.  
  51. advantage = getAdvantage[finalParityTable];
  52.  
  53. Dynamic[matchTable = getMatchTable[table]]
  54.  
  55.  
  56. If[advantage == "computer", computerPlay[finalParityTable],
  57. Print["The player begins"]]
  58. Print[table]
  59.  
  60. Dynamic[
  61. Panel[
  62. Panel[
  63. Column[{
  64. RadioButton[Dynamic[line], 1],
  65. RadioButton[Dynamic[line], 2],
  66. RadioButton[Dynamic[line], 3],
  67. RadioButton[Dynamic[line], 4],
  68. Text["Line" Dynamic[line]]
  69. }]
  70. Column[{
  71. InputField[Dynamic[number], Number],
  72. Text["Number of matches " Dynamic[number]]
  73. }]
  74. Button["Process", setTable[line, number] ]]
  75. Panel[
  76. Style[
  77. Column[{matchTable[[1]], matchTable[[2]], matchTable[[3]],
  78. matchTable[[4]]}, Center], FontSize -> 36]]
  79. Text[Dynamic[action] Dynamic[lineC]]
  80. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement