Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- getBinaryTable[table_] :=
- Map[PadLeft[IntegerDigits[#, 2], 3] &, table];
- getParityTable[plusParityTable_] :=
- Table[Mod[plusParityTable[[x]], 2], {x, 1, 3}];
- getPlusListBinaryTable[tableBinary_] :=
- tableBinary[[1]] + tableBinary[[2]] + tableBinary[[3]] +
- tableBinary[[4]];
- getAdvantage[finalParityTable_] :=
- If[finalParityTable == {0, 0, 0}, "player", "computer"];
- getMatch[finalParityTable_] :=
- If[finalParityTable == {0, 0, 0}, "player", "computer"];
- getMatchTable[table_] :=
- Table[Table["|", {x, 1, table[[x]]}] // StringJoin, {x, 1, 4}];
- setTable[line_, number_] :=
- If[table[[line]] - number < 0, Print["Error"],
- table[[line]] = table[[line]] - number; endDetect[table, 1];
- computerPlay[getCurrentParityTable[table]];
- If[haveAWinner == 1, , endDetect[table, 0]]];
- endDetect[table_, winner_ ] :=
- If[table == {0, 0, 0, 0}, Print["The game is finished"];
- If[winner == 1, Print["The player won"]; haveAWinner = 1,
- Print["The computer won"]]]
- getCurrentParityTable[table_] :=
- getParityTable[getPlusListBinaryTable[getBinaryTable[table]]]
- computerPlay[tempTable_] := Switch[For[i = 1, i < 8, i++,
- For[l = 1, l < 5, l++,
- temptable = table;
- If[temptable[[l]] - 1 < 0, , temptable[[l]] -= i];
- If[getCurrentParityTable[temptable] == {0, 0, 0},
- table = temptable; i = 10; l = 6]
- ]
- ]
- ];
- table = {};
- haveAWinner = "Null";
- action = "";
- lineC = "";
- For[i = 1, i < 8, i += 2, AppendTo[table, RandomInteger[{0, 7}]]];
- Print[table]
- binaryTable = getBinaryTable[table];
- plusParityTable = getPlusListBinaryTable[binaryTable];
- finalParityTable = getParityTable[plusParityTable];
- advantage = getAdvantage[finalParityTable];
- Dynamic[matchTable = getMatchTable[table]]
- If[advantage == "computer", computerPlay[finalParityTable],
- Print["The player begins"]]
- Print[table]
- Dynamic[
- Panel[
- Panel[
- Column[{
- RadioButton[Dynamic[line], 1],
- RadioButton[Dynamic[line], 2],
- RadioButton[Dynamic[line], 3],
- RadioButton[Dynamic[line], 4],
- Text["Line" Dynamic[line]]
- }]
- Column[{
- InputField[Dynamic[number], Number],
- Text["Number of matches " Dynamic[number]]
- }]
- Button["Process", setTable[line, number] ]]
- Panel[
- Style[
- Column[{matchTable[[1]], matchTable[[2]], matchTable[[3]],
- matchTable[[4]]}, Center], FontSize -> 36]]
- Text[Dynamic[action] Dynamic[lineC]]
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement