I hope someone can guide me in a way to make this code more efficient. Please explain the things you do simply because I'm no pro so more complex things I am unlikely to get.
If you have issues with variable names or are unsure of functions of the code please let me know! I will clarify and help in any way I can.
program Noughts_And_Crosses_Bot_Simplified;
uses
System.SysUtils, Math;
var
GridPrint: array [1 .. 10] of string; {Prints the correct symbol into the grid}
GridStore: array [1 .. 10] of integer; {Stores a value of 1 or 2 in the grid depending on which turn it is}
WinCon: array [1 .. 10] of integer; {This adds up GridStore in all the rows and sees if any of them add up to a win}
P1Bot, Counter, X, Co_Ords, Num, PlayerN, ChoiceA, y, z: integer;
Symbol, ChoiceB, Co_OrdsB: string;
Draw, Win, PlayerOnesGo: boolean;
Player: String; {I'm hoping all the other variables should be self explanatory as you go through}
label
Cont, Finish, Retry;
{This is the coding for when the bot has its go}
Procedure PlayerOneBot;
label
Retry, Finish;
begin
for Counter := 1 to 3 do
begin
sleep(500);
Write('.');
end;
writeln('');
if WinCon[1] = ((2 * z) - 3) then {This is effectivly saying 'If Bot is about to win, make sure it wins. For clarification on variables X,Y and Z go to the bottom or leave a comment.}
begin
if GridStore[1] = -3 then
begin
GridStore[1] := P1Bot;
GridPrint[1] := Symbol;
goto Finish;
end;
if GridStore[2] = -3 then
begin
GridStore[2] := P1Bot;
GridPrint[2] := Symbol;
goto Finish;
end;
if GridStore[3] = -3 then
begin
GridStore[3] := P1Bot;
GridPrint[3] := Symbol;
goto Finish;
end;
end;
if WinCon[2] = ((2 * z) - 3) then
begin
if GridStore[4] = -3 then
begin
GridStore[4] := P1Bot;
GridPrint[4] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[6] = -3 then
begin
GridStore[6] := P1Bot;
GridPrint[6] := Symbol;
goto Finish;
end;
end;
if WinCon[3] = ((2 * z) - 3) then
begin
if GridStore[7] = -3 then
begin
GridStore[7] := P1Bot;
GridPrint[7] := Symbol;
goto Finish;
end;
if GridStore[8] = -3 then
begin
GridStore[8] := P1Bot;
GridPrint[8] := Symbol;
goto Finish;
end;
if GridStore[9] = -3 then
begin
GridStore[9] := P1Bot;
GridPrint[9] := Symbol;
goto Finish;
end;
end;
if WinCon[4] = ((2 * z) - 3) then
begin
if GridStore[1] = -3 then
begin
GridStore[1] := P1Bot;
GridPrint[1] := Symbol;
goto Finish;
end;
if GridStore[4] = -3 then
begin
GridStore[4] := P1Bot;
GridPrint[4] := Symbol;
goto Finish;
end;
if GridStore[7] = -3 then
begin
GridStore[7] := P1Bot;
GridPrint[7] := Symbol;
goto Finish;
end;
end;
if WinCon[5] = ((2 * z) - 3) then
begin
if GridStore[2] = -3 then
begin
GridStore[2] := P1Bot;
GridPrint[2] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[8] = -3 then
begin
GridStore[8] := P1Bot;
GridPrint[8] := Symbol;
goto Finish;
end;
end;
if WinCon[6] = ((2 * z) - 3) then
begin
if GridStore[3] = -3 then
begin
GridStore[3] := P1Bot;
GridPrint[3] := Symbol;
goto Finish;
end;
if GridStore[6] = -3 then
begin
GridStore[6] := P1Bot;
GridPrint[6] := Symbol;
goto Finish;
end;
if GridStore[9] = -3 then
begin
GridStore[9] := P1Bot;
GridPrint[9] := Symbol;
goto Finish;
end;
end;
if WinCon[7] = ((2 * z) - 3) then
begin
if GridStore[1] = -3 then
begin
GridStore[1] := P1Bot;
GridPrint[1] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[9] = -3 then
begin
GridStore[9] := P1Bot;
GridPrint[9] := Symbol;
goto Finish;
end;
end;
if WinCon[8] = ((2 * z) - 3) then
begin
if GridStore[3] = -3 then
begin
GridStore[3] := P1Bot;
GridPrint[3] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[7] = -3 then
begin
GridStore[7] := P1Bot;
GridPrint[7] := Symbol;
goto Finish;
end;
end;
if WinCon[1] = ((2 * y) - 3) then
begin
if GridStore[1] = -3 then
begin
GridStore[1] := P1Bot;
GridPrint[1] := Symbol;
goto Finish;
end;
if GridStore[2] = -3 then
begin
GridStore[2] := P1Bot;
GridPrint[2] := Symbol;
goto Finish;
end;
if GridStore[3] = -3 then
begin
GridStore[3] := P1Bot;
GridPrint[3] := Symbol;
goto Finish;
end;
end;
if WinCon[2] = ((2 * y) - 3) then
begin
if GridStore[4] = -3 then
begin
GridStore[4] := P1Bot;
GridPrint[4] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[6] = -3 then
begin
GridStore[6] := P1Bot;
GridPrint[6] := Symbol;
goto Finish;
end;
end;
if WinCon[3] = ((2 * y) - 3) then
begin
if GridStore[7] = -3 then
begin
GridStore[7] := P1Bot;
GridPrint[7] := Symbol;
goto Finish;
end;
if GridStore[8] = -3 then
begin
GridStore[8] := P1Bot;
GridPrint[8] := Symbol;
goto Finish;
end;
if GridStore[9] = -3 then
begin
GridStore[9] := P1Bot;
GridPrint[9] := Symbol;
goto Finish;
end;
end;
if WinCon[4] = ((2 * y) - 3) then
begin
if GridStore[1] = -3 then
begin
GridStore[1] := P1Bot;
GridPrint[1] := Symbol;
goto Finish;
end;
if GridStore[4] = -3 then
begin
GridStore[4] := P1Bot;
GridPrint[4] := Symbol;
goto Finish;
end;
if GridStore[7] = -3 then
begin
GridStore[7] := P1Bot;
GridPrint[7] := Symbol;
goto Finish;
end;
end;
if WinCon[5] = ((2 * y) - 3) then
begin
if GridStore[2] = -3 then
begin
GridStore[2] := P1Bot;
GridPrint[2] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[8] = -3 then
begin
GridStore[8] := P1Bot;
GridPrint[8] := Symbol;
goto Finish;
end;
end;
if WinCon[6] = ((2 * y) - 3) then
begin
if GridStore[3] = -3 then
begin
GridStore[3] := P1Bot;
GridPrint[3] := Symbol;
goto Finish;
end;
if GridStore[6] = -3 then
begin
GridStore[6] := P1Bot;
GridPrint[6] := Symbol;
goto Finish;
end;
if GridStore[9] = -3 then
begin
GridStore[9] := P1Bot;
GridPrint[9] := Symbol;
goto Finish;
end;
end;
if WinCon[7] = ((2 * y) - 3) then
begin
if GridStore[1] = -3 then
begin
GridStore[1] := P1Bot;
GridPrint[1] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[9] = -3 then
begin
GridStore[9] := P1Bot;
GridPrint[9] := Symbol;
goto Finish;
end;
end;
if WinCon[8] = ((2 * y) - 3) then
begin
if GridStore[3] = -3 then
begin
GridStore[3] := P1Bot;
GridPrint[3] := Symbol;
goto Finish;
end;
if GridStore[5] = -3 then
begin
GridStore[5] := P1Bot;
GridPrint[5] := Symbol;
goto Finish;
end;
if GridStore[7] = -3 then
begin
GridStore[7] := P1Bot;
GridPrint[7] := Symbol;
goto Finish;
end;
end;
Retry:
Randomize;
X := RandomRange(1, 10);
if GridStore[X] <> -3 then
goto Retry;
GridStore[X] := P1Bot;
GridPrint[X] := Symbol;
Finish:
end;
procedure Array_Set_Up;
var
ArrayCounter: integer;
Begin
for ArrayCounter := 1 to 9 do
begin
GridStore[ArrayCounter] := -3;
end;
for ArrayCounter := 1 to 10 do
GridPrint[ArrayCounter] := ' ';
end;
Procedure Draw_Check;
label
finish;
Begin
WinCon[1] := (GridStore[1]) + (GridStore[2]) + (GridStore[3]);
WinCon[2] := (GridStore[4]) + (GridStore[5]) + (GridStore[6]);
WinCon[3] := (GridStore[7]) + (GridStore[8]) + (GridStore[9]);
WinCon[4] := (GridStore[1]) + (GridStore[4]) + (GridStore[7]);
WinCon[5] := (GridStore[2]) + (GridStore[5]) + (GridStore[8]);
WinCon[6] := (GridStore[3]) + (GridStore[6]) + (GridStore[9]);
WinCon[7] := (GridStore[1]) + (GridStore[5]) + (GridStore[9]);
WinCon[8] := (GridStore[3]) + (GridStore[5]) + (GridStore[7]);
WinCon[9] := GridStore[1] + GridStore[2] + GridStore[3] + GridStore[4] +
GridStore[5] + GridStore[6] + GridStore[7] + GridStore[8] + GridStore[9];
if (WinCon[1] = 3) or (WinCon[2] = 3) or (WinCon[3] = 3) or (WinCon[4] = 3) or
(WinCon[5] = 3) or (WinCon[6] = 3) or (WinCon[7] = 3) or (WinCon[8] = 3)
then
begin
writeln('Player 1 Wins!');
Win := True;
goto Finish;
end;
if (WinCon[1] = 6) or (WinCon[2] = 6) or (WinCon[3] = 6) or (WinCon[4] = 6) or
(WinCon[5] = 6) or (WinCon[6] = 6) or (WinCon[7] = 6) or (WinCon[8] = 6)
then
begin
writeln('Player 2 Wins!');
Win := True;
goto Finish;
end;
if (WinCon[9] = 13) or (WinCon[9] = 14) then
begin
writeln('Game was a draw');
Draw := True;
goto Finish;
end;
Finish:
End; {The grid has 9 values in it all set to -3 at the start, if player 1 where to go in say, top left that value would change to 1 so then WinCon[1] would be 1+-3+-3 so -5 however if Player 1 goes in all the top row it would be 1+1+1 so WinCon[1] would be 1 so player 1 would win}
Procedure Grid_Check;
label Retry, BotStop;
Begin
Draw_Check;
repeat
if PlayerOnesGo = True then
begin
PlayerOnesGo := False;
Player := 'Two';
PlayerN := 2
end
Else
begin
PlayerOnesGo := True;
Player := 'One';
PlayerN := 1
end;
writeln('Player ' + Player + ' Go');
if (PlayerOnesGo = True) and (P1Bot = 1) then
begin
PlayerOneBot;
Goto BotStop;
end;
if (PlayerOnesGo = False) and (P1Bot = 2) then
begin
PlayerOneBot;
Goto BotStop;
end;
writeln('Please enter the co-ords of your placemt');
repeat
Retry:
readln(Co_OrdsB);
if trystrtoint(Co_OrdsB, Num) then
begin
Co_Ords := Strtoint(Co_OrdsB);
if (GridStore[Co_Ords] = 1) or (GridStore[Co_Ords] = 2) then
writeln('This location is taken, Please pick another');
end
else
begin
writeln('Please input a number');
goto Retry;
end;
until (GridStore[Co_Ords] = -3);
GridStore[Co_Ords] := PlayerN;
if PlayerOnesGo = True then
begin
GridPrint[Co_Ords] := 'X';
GridStore[Co_Ords] := 1;
end
else
begin
GridPrint[Co_Ords] := 'O';
GridStore[Co_Ords] := 2;
end;
BotStop:
writeln(' ', GridPrint[1], ' | ', GridPrint[2], ' | ',
GridPrint[3], ' ');
writeln(' ----------');
writeln(' ', GridPrint[4], ' | ', GridPrint[5], ' | ',
GridPrint[6], ' ');
writeln(' ----------');
writeln(' ', GridPrint[7], ' | ', GridPrint[8], ' | ',
GridPrint[9], ' ');
Draw_Check;
until (Win = True) or (Draw = True);
end;
begin
Array_Set_Up;
Retry:
writeln('Do you want to (1) Play against a bot or (2) Play against a friend?');
readln(ChoiceB);
if trystrtoint(ChoiceB, Num) then
begin
ChoiceA := Strtoint(ChoiceB);
if ChoiceA = 2 then
goto Finish
else if ChoiceA = 1 then
goto Cont
else
begin
writeln('Please enter either 1 or 2');
Goto Retry;
end;
end
else
begin
writeln('Please enter a number');
goto Retry;
end;
Cont:
Randomize;
X := RandomRange(1, 3); {Says if bot is P1 or P2}
if X = 1 then
begin
P1Bot := 1;
Symbol := 'X';
y := 2; {Y Is used so it can see if the opponent is near winning}
z := 1; {Z is what it will put in gridstore when it picks a spot to go in}
end;
if X = 2 then
begin
P1Bot := 2;
Symbol := 'O';
y := 1;
z := 1;
end;
Finish:
writeln('Noughts and crosses.');
writeln('Game Board:');
writeln(' ', GridPrint[1], ' | ', GridPrint[2], ' | ', GridPrint[3], ' ');
writeln(' ----------');
writeln(' ', GridPrint[4], ' | ', GridPrint[5], ' | ', GridPrint[6], ' ');
writeln(' ----------');
writeln(' ', GridPrint[7], ' | ', GridPrint[8], ' | ', GridPrint[9], ' ');
writeln('The positions work as such, enter the co-ordinates of your move');
writeln(' 1 | 2 | 3 ');
writeln(' -----------');
writeln(' 4 | 5 | 6 ');
writeln(' -----------');
writeln(' 7 | 8 | 9 ');
writeln('Game Start');
PlayerOnesGo := False;
Grid_Check;
readln;
end.