Advertisement
WadeRollins2710

Articfical Intelligence for Bingo [Copyright]

Mar 8th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 24.95 KB | None | 0 0
  1.  
  2. {Made by TVA}
  3. Program Project_Bingo_Artifical_Intelligence;
  4. Uses crt;
  5. Var
  6.         Human_Board, COM_Board: Array [1..5,1..5] of Integer;
  7.         i, j: Integer;
  8.         Mode: String;
  9. Procedure Pout(S: String); {Pascal Output - Insipired by Cout (C++) by Stroup}
  10.         Begin
  11.                 Writeln(S);
  12.                 Delay(1000);
  13.         End;
  14. Procedure Info;
  15.         Begin
  16.                 Pout('Made by T.V.A');
  17.                 Pout('Do not copy');
  18.         End;
  19. Procedure SwapHuman(x, y, i, j: Integer);
  20.         Var
  21.                 Mid: Integer;
  22.         Begin
  23.                 Mid:=Human_Board[x,y];
  24.                 Human_Board[x,y]:=Human_Board[i,j];
  25.                 HUman_Board[i,j]:=Mid;
  26.         End;
  27. Procedure Print_Human_Board;
  28.         Var
  29.                 x, y: Integer;
  30.         Begin
  31.                 TextColor(White);
  32.                 For x:=1 to 5 do
  33.                         Begin
  34.                                 For y:=1 to 5 do
  35.                                         If Human_Board[x,y]=0 then
  36.                                                 Begin
  37.                                                         TextColor(Yellow);
  38.                                                         Write(Human_Board[x,y]:3);
  39.                                                         TextColor(White);
  40.                                                 End
  41.                                         else
  42.                                                 Write(Human_Board[x,y]:3);
  43.                                 Writeln;
  44.                         End;
  45.         End;
  46. Procedure Print_COM_Board;
  47.         Var
  48.                 x, y: Integer;
  49.         Begin
  50.                 TextColor(White);
  51.                 For x:=1 to 5 do
  52.                         Begin
  53.                                 For y:=1 to 5 do
  54.                                         If COM_Board[x,y]=0 then
  55.                                                 Begin
  56.                                                         TextColor(Yellow);
  57.                                                         Write(COM_Board[x,y]:3);
  58.                                                         TextColor(White);
  59.                                                 End
  60.                                         else
  61.                                                 Write(COM_Board[x,y]:3);
  62.                                 Writeln;
  63.                         End;
  64.         End;
  65. Function Valid(value, PosX, PosY: Integer): Boolean;
  66.         Var
  67.                 i, j, count: Integer;
  68.                 Bo: Boolean;
  69.         Begin
  70.                 count:=0; Bo:=true;
  71.                 For i:=1 to 5 do
  72.                         For j:=1 to 5 do
  73.                                 If (value=Human_Board[i,j]) and ((i<>PosX) or (j<>PosY)) then inc(count);
  74.                 If count<>0 then Bo:=false
  75.                 else Bo:=true;
  76.                 If (value<=0) or (value>25) then Bo:=false
  77.                 else Bo:=true;
  78.                 Valid:=Bo;
  79.         End;
  80.  
  81. Procedure Init_Board;
  82.         Var
  83.                 x, y, i, pt, SwapX, SwapY: Integer;
  84.                 Choice: String;
  85.         Begin
  86.                 Write('Choose mode: ');
  87.                 Readln(Mode);
  88.                 For i:=1 to length(Mode) do
  89.                         Mode[i]:=Upcase(Mode[i]);
  90.                 While (Mode<>'EASY') and (Mode<>'NORMAL') do
  91.                         Begin
  92.                                 Writeln('Mode inputed is invalid, please input again: ');
  93.                                 Readln(Mode);
  94.                                 For i:=1 to length(Mode) do
  95.                                         Mode[i]:=Upcase(Mode[i]);
  96.                         End;
  97.                 Write('Make board bay manual or auto? ');
  98.                 Readln(Choice);
  99.                 For i:=1 to length(Choice) do
  100.                         Choice[i]:=Upcase(Choice[i]);
  101.                 While (Choice='INFO') do
  102.                         Begin
  103.                                 Info;
  104.                                 Writeln('Input command again: ');
  105.                                 Readln(Choice);
  106.                         End;
  107.                 While (Choice<>'AUTO') and (Choice<>'MANUAL') and (Choice<>'INFO') do
  108.                         Begin
  109.                                 Write('Input command again: ');
  110.                                 Readln(Choice);
  111.                                 For i:=1 to length(Choice) do
  112.                                         Choice[i]:=Upcase(Choice[i]);
  113.                         End;
  114.                 If Choice='AUTO' then
  115.                         Begin
  116.                                 pt:=0;
  117.                                 For x:=1 to 5 do
  118.                                         For y:=1 to 5 do
  119.                                                 Begin
  120.                                                         inc(pt);
  121.                                                         Human_Board[x,y]:=pt;
  122.                                                 End;
  123.                                 Randomize;
  124.                                 For x:=1 to 5 do
  125.                                         For y:=1 to 5 do
  126.                                                 Begin
  127.                                                         SwapX:=Random(5)+1;
  128.                                                         SwapY:=Random(5)+1;
  129.                                                         SwapHuman(x,y,SwapX,SwapY);
  130.                                                 End;
  131.                         End
  132.                 else
  133.                         For x:=1 to 5 do
  134.                                 For y:=1 to 5 do
  135.                                         Begin
  136.                                                 Write('Input &Human_Board[',x,',',y,']= ');
  137.                                                 Readln(Human_Board[x,y]);
  138.                                                 While Not(Valid(Human_Board[x,y],x,y)) do
  139.                                                         Begin
  140.                                                                 Writeln('Data Inputed is invalid!!!!');
  141.                                                                 Write('Input again pls (you dumb ass): ');
  142.                                                                 Readln(Human_Board[x,y]);
  143.                                                         End;
  144.                                         End;
  145.         End;
  146. Procedure SwapCOM(x, y, i, j: Integer);
  147.         Var
  148.                 Mid: Integer;
  149.         Begin
  150.                 Mid:=COM_Board[x,y];
  151.                 COM_Board[x,y]:=COM_Board[i,j];
  152.                 COM_Board[i,j]:=Mid;
  153.         {}
  154.         {Alternative Data}
  155.         {}
  156.         {COM_Board[x,y]:=COM_Board[x,y]+COM_Board[i,j];
  157.         COM_Board[i,j]:=COM_Board[x,y]-COM_Board[i,j];
  158.         COM_Board[x,y]:=COM_Board[i,j]-COM_Board[i,j]}
  159.         {
  160.         3 4
  161.         7 4
  162.         7 3
  163.         4 3
  164.         }
  165.         End;
  166. Procedure Init_COM_Board;
  167.         Var
  168.                 x, y, SwapX, SwapY, pt: Integer;
  169.         Begin
  170.                 pt:=0;
  171.                                 For x:=1 to 5 do
  172.                                         For y:=1 to 5 do
  173.                                                 Begin
  174.                                                         inc(pt);
  175.                                                         COM_Board[x,y]:=pt;
  176.                                                 End;
  177.                                 For x:=1 to 5 do
  178.                                         For y:=1 to 5 do
  179.                                                 Begin
  180.                                                         SwapX:=Random(5)+1;
  181.                                                         SwapY:=Random(5)+1;
  182.                                                         SwapCOM(x,y,SwapX,SwapY);
  183.                                                 End;
  184.         End;
  185. Procedure COMWon_HumanLost;
  186.         Begin
  187.                 clrscr;
  188.                 Writeln('AI Won');
  189.                 Delay(500);
  190.                 Print_Human_Board;
  191.                 Writeln;
  192.                 Print_COM_Board;
  193.                 Readln;
  194.                 Halt;
  195.         End;
  196. Procedure HumanWon_COMLost;
  197.         Begin
  198.                 clrscr;
  199.                 Writeln('Human Won');
  200.                 Delay(500);
  201.                 Print_Human_Board;
  202.                 Writeln;
  203.                 Print_COM_Board;
  204.                 Readln;
  205.                 Halt;
  206.         End;
  207. Procedure Tie;
  208.         Begin
  209.                 clrscr;
  210.                 Writeln('It''s a tie');
  211.                 Delay(500);
  212.                 Print_Human_Board;
  213.                 Writeln;
  214.                 Print_COM_Board;
  215.                 Readln;
  216.                 Halt;
  217.         End;
  218. Function COMDiagonal: Integer;
  219.         Var
  220.                 i, count, c: Integer;
  221.         Begin
  222.                 c:=0; count:=0;
  223.                 For i:=1 to 5 do
  224.                         If COM_Board[i,i]=0 then
  225.                                 inc(count);
  226.                 If count=5 then inc(c);
  227.                 count:=0;
  228.                 For i:=i to 5 do
  229.                         If COM_Board[i,5-i+1]=0 then
  230.                                 inc(count);
  231.                 If count=5 then inc(c);
  232.                 COMDiagonal:=c;
  233.         End;
  234. Function Diagonal: Integer;
  235.         Var
  236.                 i, count, c: Integer;
  237.         Begin
  238.                 c:=0; count:=0;
  239.                 For i:=1 to 5 do
  240.                         If Human_Board[i,i]=0 then
  241.                                 inc(count);
  242.                 If count=5 then inc(c);
  243.                 count:=0;
  244.                 For i:=i to 5 do
  245.                         If Human_Board[i,5-i+1]=0 then
  246.                                 inc(count);
  247.                 If count=5 then inc(c);
  248.                 Diagonal:=c;
  249.         End;
  250. Function COMRowScore: Integer;
  251.         Var
  252.                 i, j, count, c: Integer;
  253.         Begin
  254.                 c:=0;
  255.                 For i:=1 to 5 do
  256.                         Begin
  257.                                 count:=0;
  258.                                 For j:=1 to 5 do
  259.                                         If COM_Board[i,j]=0 then inc(count);
  260.                                 If count=5 then inc(c);
  261.                         End;
  262.                 COMRowScore:=c;
  263.         End;
  264. Function HumanRowScore: Integer;
  265.         Var
  266.                 i, j, count, c: Integer;
  267.         Begin
  268.                 c:=0;
  269.                 For i:=1 to 5 do
  270.                         Begin
  271.                                 count:=0;
  272.                                 For j:=1 to 5 do
  273.                                         If Human_Board[i,j]=0 then inc(count);
  274.                                 If count=5 then inc(c);
  275.                         End;
  276.                 HumanRowScore:=c;
  277.         End;
  278. Function HumanColumnScore: Integer;
  279.         Var
  280.                 i, j, count, c: Integer;
  281.         Begin
  282.                 c:=0;
  283.                 For i:=1 to 5 do
  284.                         Begin
  285.                                 count:=0;
  286.                                 For j:=1 to 5 do
  287.                                         If Human_Board[j,i]=0 then inc(count);
  288.                                 If count=5 then inc(c);
  289.                         End;
  290.                 HumanColumnScore:=c;
  291.         End;
  292. Function COMColumnScore: Integer;
  293.         Var
  294.                 i, j, count, c: Integer;
  295.         Begin
  296.                 c:=0;
  297.                 For i:=1 to 5 do
  298.                         Begin
  299.                                 count:=0;
  300.                                 For j:=1 to 5 do
  301.                                         If COM_Board[j,i]=0 then inc(count);
  302.                                 If count=5 then inc(c);
  303.                         End;
  304.                 COMColumnScore:=c;
  305.         End;
  306. Function COMVictoryCondition: Boolean;
  307.         Var
  308.                 Bo: Boolean;
  309.         Begin
  310.                 If COMColumnScore+COMRowScore+COMDiagonal>=5 then Bo:=True
  311.                 else Bo:=false;
  312.                 COMVictoryCondition:=Bo;
  313.         End;
  314. Function HumanVictoryCondition: Boolean;
  315.         Var
  316.                 Bo: Boolean;
  317.         Begin
  318.                 If HumanColumnScore+HumanRowScore+Diagonal>=5 then Bo:=True
  319.                 else Bo:=false;
  320.                 HumanVictoryCondition:=Bo;
  321.         End;
  322. Function ValidChoice(value: Integer): Boolean;
  323.         Var
  324.                 i, j, count: Integer;
  325.                 Bo: Boolean;
  326.         Begin
  327.                 count:=0;
  328.                 For i:=1 to 5 do
  329.                         For j:=1 to 5 do
  330.                                 If Human_Board[i,j]=value then inc(count);
  331.                 If count=0 then Bo:=false
  332.                 else Bo:=true;
  333.                 ValidChoice:=Bo;
  334.         End;
  335. Procedure Human_Pick;
  336.         Var
  337.                 Pick, i, j: Integer;
  338.         Begin
  339.                 Write('Input choice: ');
  340.                 Readln(Pick);
  341.                 While Not(ValidChoice(Pick)) or (Pick<=0) or (Pick>25) do
  342.                         Begin
  343.                                 clrscr;
  344.                                 Print_Human_Board;
  345.                                 Writeln;
  346.                                 Print_COM_Board;
  347.                                 Writeln('Data inputed is invalid!!!');
  348.                                 Write('Input again: ');
  349.                                 Readln(Pick);
  350.                         End;
  351.                 For i:=1 to 5 do
  352.                         For j:=1 to 5 do
  353.                                 If Human_Board[i,j]=Pick then Human_Board[i,j]:=0;
  354.                 For i:=1 to 5 do
  355.                         For j:=1 to 5 do
  356.                                 If COM_Board[i,j]=Pick then COM_Board[i,j]:=0;
  357.         End;
  358. Function ValidCOMChoice(value: Integer): Boolean;
  359.         Var
  360.                 count, i, j: Integer;
  361.                 Bo: Boolean;
  362.         Begin
  363.                 count:=0;
  364.                 For i:=1 to 5 do
  365.                         For j:=1 to 5 do
  366.                                 If COM_Board[i,j]=value then inc(count);
  367.                 If count=0 then Bo:=false
  368.                 else Bo:=true;
  369.                 ValidCOMChoice:=Bo;
  370.         End;
  371. Function GetDiagonal: Integer;
  372.         Var
  373.                 i, j, Dia, Dia2: Integer;
  374.         Begin
  375.                 Dia:=0; Dia2:=0;
  376.                 For i:=1 to 5 do
  377.                         Begin
  378.                                 If COM_Board[i,i]=0 then inc(Dia);
  379.                                 If COM_Board[i,5-i+1]=0 then inc(Dia2);
  380.                         End;
  381.                 If Dia>=Dia2 then
  382.                         GetDiagonal:=Dia
  383.                 else GetDiagonal:=Dia2;
  384.         End;
  385. Function GetPosOfMaxDiagonal: Integer;
  386.         Var
  387.                 i, j, Dia, Dia2: Integer;
  388.         Begin
  389.                 Dia:=0; Dia2:=0;
  390.                 For i:=1 to 5 do
  391.                         Begin
  392.                                 If COM_Board[i,i]=0 then inc(Dia);
  393.                                 If COM_Board[i,5-i+1]=0 then inc(Dia2);
  394.                         End;
  395.                 If Dia>=Dia2 then
  396.                         GetPosOfMaxDiagonal:=1
  397.                 else GetPosOfMaxDiagonal:=2;
  398.         End;
  399. Function GetRow: Integer;
  400.         Var
  401.                 i, j, count, Max: Integer;
  402.         Begin
  403.                 Max:=-MaxInt;
  404.                 For i:=1 to 5 do
  405.                         Begin
  406.                                 count:=0;
  407.                                 For j:=1 to 5 do
  408.                                         If COM_Board[i,j]=0 then inc(count);
  409.                                 If count>Max then
  410.                                                 Max:=count;
  411.                         End;
  412.                 GetRow:=Max;
  413.         End;
  414. Function GetPosOfMaxRow: Integer;
  415.         Var
  416.                 i, j, count, Max, Pos: Integer;
  417.         Begin
  418.                 Max:=-MaxInt;
  419.                 For i:=1 to 5 do
  420.                         Begin
  421.                                 count:=0;
  422.                                 For j:=1 to 5 do
  423.                                         If COM_Board[i,j]=0 then inc(count);
  424.                                 If count>Max then
  425.                                                 Begin
  426.                                                         Max:=count;
  427.                                                         Pos:=i;
  428.                                                 End;
  429.                         End;
  430.                 GetPosOfMaxRow:=Pos;
  431.         End;
  432. Function GetColumn: Integer;
  433.         Var
  434.                 i, j, count, Max: Integer;
  435.         Begin
  436.                 Max:=-MaxInt;
  437.                 For i:=1 to 5 do
  438.                         Begin
  439.                                 count:=0;
  440.                                 For j:=1 to 5 do
  441.                                         If COM_Board[j,i]=0 then inc(count);
  442.                                 If count>Max then
  443.                                                 Max:=count;
  444.                         End;
  445.                 GetColumn:=Max;
  446.         End;
  447. Function GetPosOfMaxColumn: Integer;
  448.         Var
  449.                 i, j, count, Max, Pos: Integer;
  450.         Begin
  451.                 Max:=-MaxInt;
  452.                 For i:=1 to 5 do
  453.                         Begin
  454.                                 count:=0;
  455.                                 For j:=1 to 5 do
  456.                                         If COM_Board[j,i]=0 then inc(count);
  457.                                 If count>Max then
  458.                                                 Begin
  459.                                                         Max:=count;
  460.                                                         Pos:=i;
  461.                                                 End;
  462.                         End;
  463.                 GetPosOfMaxColumn:=Pos;
  464.         End;
  465. Procedure COM_Smart_Pick;
  466.         Var
  467.                 MaxRow, MaxColumn, MaxDiagonal: Integer;
  468.                 PosRow, PosColumn, PosDiagonal: Integer;
  469.                 Pick: Integer;
  470.         Begin
  471.                 MaxRow:=GetRow;
  472.                 MaxColumn:=GetColumn;
  473.                 MaxDiagonal:=GetDiagonal;
  474.                 If MaxRow>=MaxColumn then
  475.                         If MaxRow>=MaxDiagonal then
  476.                                 Begin
  477.                                         PosRow:=GetPosOfMaxrow;
  478.                                         For i:=1 to 5 do
  479.                                                 If COM_Board[PosRow,i]<>0 then
  480.                                                         Begin
  481.                                                                 Pick:=COM_Board[PosRow,i];
  482.                                                                 Break;
  483.                                                         End;
  484.                                 End
  485.                         else
  486.                                 Begin
  487.                                         PosDiagonal:=GetPosofMaxDiagonal;
  488.                                         If PosDiagonal=1 then
  489.                                                 Begin
  490.                                                         For i:=1 to 5 do
  491.                                                                 If COM_Board[i,i]<>0 then
  492.                                                                         Begin
  493.                                                                                 Pick:=COM_Board[i,i];
  494.                                                                                 Break;
  495.                                                                         End;
  496.                                                 End
  497.                                         else
  498.                                                 Begin
  499.                                                         For i:=1 to 5 do
  500.                                                                 If COM_Board[i,5-i+1]<>0 then
  501.                                                                         Begin
  502.                                                                                 Pick:=COM_Board[i,5-i+1];
  503.                                                                                 Break;
  504.                                                                         End;
  505.                                                 End;
  506.                                 End
  507.                 else
  508.                         If MaxColumn>=MaxDiagonal then
  509.                                 Begin
  510.                                         PosColumn:=GetPosOfMaxColumn;
  511.                                         For i:=1 to 5 do
  512.                                                 If COM_Board[i,PosColumn]<>0 then
  513.                                                         Begin
  514.                                                                 Pick:=COM_Board[i,PosColumn];
  515.                                                                 Break;
  516.                                                         End;
  517.                                 End
  518.                         else
  519.                                 Begin
  520.                                         PosDiagonal:=GetPosOfMaxDiagonal;
  521.                                         If PosDiagonal=1 then
  522.                                                 Begin
  523.                                                         For i:=1 to 5 do
  524.                                                                 If COM_Board[i,i]<>0 then
  525.                                                                         Begin
  526.                                                                                 Pick:=COM_Board[i,i];
  527.                                                                                 Break;
  528.                                                                         End;
  529.                                                 End
  530.                                         else
  531.                                                 Begin
  532.                                                         For i:=1 to 5 do
  533.                                                                 If COM_Board[i,5-i+1]<>0 then
  534.                                                                         Begin
  535.                                                                                 Pick:=COM_Board[i,5-i+1];
  536.                                                                                 Break;
  537.                                                                         End;
  538.                                                 End;
  539.                                 End;
  540.                         For i:=1 to 5 do
  541.                                 For j:=1 to 5 do
  542.                                         If Pick=COM_Board[i,j] then COM_Board[i,j]:=0;
  543.                         For i:=1 to 5 do
  544.                                 For j:=1 to 5 do
  545.                                         If Pick=Human_Board[i,j] then Human_Board[i,j]:=0;
  546.         End;
  547. Procedure COM_God_Pick;
  548.         Begin
  549.         End;
  550. Procedure COM_Random_Pick;
  551.         Var
  552.                 Pick, i, j: Integer;
  553.         Begin
  554.                 Pick:=Random(24)+1;
  555.                 While Not(ValidCOMChoice(Pick)) do
  556.                         Pick:=Random(24)+1;
  557.                 For i:=1 to 5 do
  558.                         For j:=1 to 5 do
  559.                                 If COM_Board[i,j]=Pick then COM_Board[i,j]:=0;
  560.                 For i:=1 to 5 do
  561.                         For j:=1 to 5 do
  562.                                 If Human_Board[i,j]=Pick then Human_Board[i,j]:=0;
  563.         End;
  564. Procedure Play;
  565.         Begin
  566.                 While True Do
  567.                         Begin
  568.                                 clrscr;
  569.                                 Print_Human_Board;
  570.                                 Writeln;
  571.                                 Print_COM_Board;
  572.                                 Human_Pick;
  573.                                 If (HumanVictoryCondition) and (COMVictoryCondition) then Tie
  574.                                 else
  575.                                         Begin
  576.                                                 If (HumanVictoryCondition) then HumanWon_COMLost;
  577.                                                 If (COMVictoryCondition) then COMWon_HumanLost;
  578.                                         End;
  579.                                 clrscr;
  580.                                 Print_Human_Board;
  581.                                 Writeln;
  582.                                 Print_COM_Board;
  583.                                 If Mode='EASY'then
  584.                                         COM_Random_Pick
  585.                                 else
  586.                                         If Mode='NORMAL'then
  587.                                                 COM_Smart_Pick
  588.                                         else COM_God_Pick;
  589.                                 If (HumanVictoryCondition) and (COMVictoryCondition) then Tie
  590.                                 else
  591.                                         Begin
  592.                                                 If (HumanVictoryCondition) then HumanWon_COMLost;
  593.                                                 If (COMVictoryCondition) then COMWon_HumanLost;
  594.                                         End;
  595.                         End;
  596.         End;
  597. Begin
  598.         clrscr;
  599.         Init_Board;
  600.         Init_COM_Board;
  601.         Print_Human_Board;
  602.         Writeln;
  603.         Print_COM_Board;
  604.         Play;
  605. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement