-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeSmartMove.pl
More file actions
74 lines (65 loc) · 1.91 KB
/
Copy pathMakeSmartMove.pl
File metadata and controls
74 lines (65 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
sub MakeSmartRobotMove {
my $i = 0;
my @GameBoard = @{$_[0]};
# Checking if robot can make a move to win
# This loop checks the horizontal possible wins
foreach $i (0..2) {
my $n = CheckSetRobot($GameBoard[$i][0], $GameBoard[$i][1], $GameBoard[$i][2]);
if ($n != -1) {
MakeMove($i, $n, 2, \@GameBoard);
return 1;
}
}
# This loop checks the vertical possible wins
foreach $i (0..2) {
my $n = CheckSetRobot($GameBoard[0][$i], $GameBoard[1][$i], $GameBoard[2][$i]);
if ($n != -1) {
MakeMove($n, $i, 2, \@GameBoard);
return 1;
}
}
# This checks for a top-left to bottom-right diagonal win move
my $n = CheckSetRobot($GameBoard[0][0], $GameBoard[1][1], $GameBoard[2][2]);
if ($n != -1) {
MakeMove($n, $n, 2, \@GameBoard);
return 1;
}
# This checks for a top-right to bottom-left diagonal win move
$n = CheckSetRobot($GameBoard[0][2], $GameBoard[1][1], $GameBoard[2][0]);
if ($n != -1) {
MakeMove($n, 2 - $n, 2, \@GameBoard);
return 1;
}
#Checking if robot can make move to block player
# This checks for horizontal blocking moves
foreach $i (0..2) {
my $n = CheckSetPlayer($GameBoard[$i][0], $GameBoard[$i][1], $GameBoard[$i][2]);
if ($n != -1) {
MakeMove($i, $n, 2, \@GameBoard);
return 1;
}
}
# This checks for vertical blocking moves
foreach $i (0..2) {
my $n = CheckSetPlayer($GameBoard[0][$i], $GameBoard[1][$i], $GameBoard[2][$i]);
if ($n != -1) {
MakeMove($n, $i, 2, \@GameBoard);
return 1;
}
}
# This checks for top-left to bottom-right blocking move
$n = CheckSetPlayer($GameBoard[0][0], $GameBoard[1][1], $GameBoard[2][2]);
if ($n != -1) {
MakeMove($n, $n, 2, \@GameBoard);
return 1;
}
# This checks for top-right to bottom-left blocking move
$n = CheckSetPlayer($GameBoard[0][2], $GameBoard[1][1], $GameBoard[2][0]);
if ($n != -1) {
MakeMove($n, 2 - $n, 2, \@GameBoard);
return 1;
}
#Make dumb move
return MakeRobotMove(\@GameBoard);
}
1;