| 1 |
/* netToe Version 1.5.1 |
| 2 |
* |
| 3 |
* Copyright 2013, 2014 Mats Erik Andersson <[email protected]> |
| 4 |
* |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
# include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdlib.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#define BOARD_CALCULATIONS |
| 29 |
#include "board.h" |
| 30 |
|
| 31 |
/* A vector of all &C11, &C12, etcetera, ordered |
| 32 |
* in some way to reflect a strategic decision. |
| 33 |
* Used by AI to locate an unused field. |
| 34 |
*/ |
| 35 |
static char ** pawnlist_old[] = |
| 36 |
{ &C22, &C31, &C12, &C33, &C23, &C21, &C13, &C11, &C32, NULL }; |
| 37 |
|
| 38 |
/* Description of threat used by AI to decide on moves. */ |
| 39 |
struct probes { |
| 40 |
char pawn; /* Probe only this kind of marker. */ |
| 41 |
char ** first; /* Two taken fields. */ |
| 42 |
char ** second; |
| 43 |
char ** open; /* One field open for play. */ |
| 44 |
}; |
| 45 |
|
| 46 |
static struct probes probelist_weak[] = { |
| 47 |
{ 'X', &C11, &C12, &C13 }, /* Protect first row. */ |
| 48 |
{ 'X', &C33, &C32, &C31 }, /* Protect third row. */ |
| 49 |
{ 'X', &C11, &C21, &C31 }, /* Protect first column. */ |
| 50 |
{ 'X', &C13, &C23, &C33 }, /* Protect third column. */ |
| 51 |
{ 'X', &C13, &C22, &C31 }, /* Protect second diagonal. */ |
| 52 |
{ 'X', &C21, &C22, &C23 }, /* Protect second row. */ |
| 53 |
{ 'X', &C23, &C22, &C21 }, /* Protect second row. */ |
| 54 |
{ 'X', &C11, &C13, &C12 }, /* Protect first row. */ |
| 55 |
{ 'O', &C31, &C22, &C13 }, /* Attack second diagonal, win at corner. */ |
| 56 |
{ 'O', &C33, &C22, &C11 }, /* Attack first diagonal, win at corner. */ |
| 57 |
{ 'O', &C32, &C22, &C12 }, /* Attack second column, win at edge. */ |
| 58 |
{ 'O', &C12, &C22, &C32 }, /* Attack second column, win at edge. */ |
| 59 |
{ 0, NULL, NULL, NULL }, |
| 60 |
}; |
| 61 |
|
| 62 |
static struct probes probelist_standard[] = { |
| 63 |
{ 'O', &C11, &C22, &C33 }, /* Attack first diagonal, win at corner. */ |
| 64 |
{ 'O', &C31, &C33, &C32 }, /* Attack third row, win at edge. */ |
| 65 |
{ 'O', &C23, &C22, &C21 }, /* Attack second row, win at edge. */ |
| 66 |
{ 'O', &C12, &C22, &C32 }, /* Attack second column, win at edge. */ |
| 67 |
{ 'O', &C13, &C22, &C31 }, /* Attack second diagonal, win at corner. */ |
| 68 |
{ 'O', &C31, &C22, &C13 }, /* Attack second diagonal, win at corner. */ |
| 69 |
{ 'O', &C33, &C22, &C11 }, /* Attack first diagonal, win at corner. */ |
| 70 |
{ 'O', &C32, &C22, &C12 }, /* Attack second column, win at edge. */ |
| 71 |
{ 'X', &C11, &C12, &C13 }, /* Protect first row. */ |
| 72 |
{ 'X', &C33, &C32, &C31 }, /* Protect third row. */ |
| 73 |
{ 'X', &C11, &C21, &C31 }, /* Protect first column. */ |
| 74 |
{ 'X', &C13, &C23, &C33 }, /* Protect third column. */ |
| 75 |
{ 'X', &C13, &C22, &C31 }, /* Protect second diagonal. */ |
| 76 |
{ 'X', &C21, &C22, &C23 }, /* Protect second row. */ |
| 77 |
{ 'X', &C23, &C22, &C21 }, /* Protect second row. */ |
| 78 |
{ 'X', &C11, &C13, &C12 }, /* Protect first row. */ |
| 79 |
{ 'X', &C12, &C22, &C32 }, /* Protect second column. */ |
| 80 |
{ 'X', &C32, &C22, &C12 }, /* Protect second column. */ |
| 81 |
{ 0, NULL, NULL, NULL }, |
| 82 |
}; |
| 83 |
|
| 84 |
/* The argument is a fixed, ordered vector of all &C11, &C12, etcetera. |
| 85 |
* It represents a particular strategy for finding empty fields. |
| 86 |
* A NULL at the end is a guard, detected by claim_empty_field(). |
| 87 |
*/ |
| 88 |
static void claim_empty_field (char **plist[]) |
| 89 |
{ |
| 90 |
char ***p; |
| 91 |
|
| 92 |
for (p = plist; *p; p++) |
| 93 |
if (***p == ' ') |
| 94 |
{ |
| 95 |
***p = 'O'; |
| 96 |
return; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
static int play_block_or_win (char marker, int first, int last) |
| 101 |
{ |
| 102 |
int n; |
| 103 |
int marker_count = 0, blank_count = 0; |
| 104 |
|
| 105 |
for ( n=first; n<=last; n += (last - first)/2 ) |
| 106 |
{ |
| 107 |
if ( *board[n] == ' ') |
| 108 |
{ |
| 109 |
blank_count++; |
| 110 |
} |
| 111 |
else if ( *board[n] == marker ) |
| 112 |
{ |
| 113 |
marker_count++; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if ( blank_count == 1 && marker_count == 2 ) |
| 118 |
{ |
| 119 |
int play_this_pos; |
| 120 |
|
| 121 |
/* Is winning line or a serious threat. */ |
| 122 |
if ( *board[first] == ' ' ) |
| 123 |
{ |
| 124 |
play_this_pos = first; |
| 125 |
} |
| 126 |
else if ( *board[last] == ' ' ) |
| 127 |
{ |
| 128 |
play_this_pos = last; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
play_this_pos = (first + last)/2; |
| 133 |
} |
| 134 |
|
| 135 |
*board[play_this_pos] = 'O'; |
| 136 |
|
| 137 |
/* Report a performed play. */ |
| 138 |
return 1; |
| 139 |
} |
| 140 |
|
| 141 |
/* Nothing done. */ |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int probe_for_fields (struct probes probelist[]) |
| 146 |
{ |
| 147 |
struct probes *p; |
| 148 |
|
| 149 |
for (p = probelist; p->pawn; p++) |
| 150 |
{ |
| 151 |
struct probes probe = *p; |
| 152 |
|
| 153 |
if ( (**probe.first == probe.pawn) |
| 154 |
&& (**probe.second == probe.pawn) |
| 155 |
&& (**probe.open == ' ') ) |
| 156 |
{ |
| 157 |
**probe.open = 'O'; |
| 158 |
return 1; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/* No action. */ |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
static void get_cpu_normal_move (void) |
| 167 |
{ |
| 168 |
sleep ( 1 ); |
| 169 |
|
| 170 |
if (probe_for_fields (probelist_weak)) |
| 171 |
return; |
| 172 |
|
| 173 |
/* Find any unoccupied position. */ |
| 174 |
claim_empty_field (pawnlist_old); |
| 175 |
} |
| 176 |
|
| 177 |
static void get_cpu_hard_move (void) |
| 178 |
{ |
| 179 |
sleep ( 1 ); |
| 180 |
|
| 181 |
if (probe_for_fields (probelist_standard)) |
| 182 |
return; |
| 183 |
|
| 184 |
/* Find any unoccupied position. */ |
| 185 |
claim_empty_field (pawnlist_old); |
| 186 |
} |
| 187 |
|
| 188 |
static void get_cpu_harder_move (void) |
| 189 |
{ |
| 190 |
int n; |
| 191 |
|
| 192 |
sleep ( 1 ); |
| 193 |
|
| 194 |
/* Locate any winning move. */ |
| 195 |
for (n = 0; n < 8; n++) |
| 196 |
{ |
| 197 |
if ( play_block_or_win('O', |
| 198 |
winning_line[n].first, winning_line[n].last) ) |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
/* Locate any threat. */ |
| 203 |
for (n = 0; n < 8; n++) |
| 204 |
{ |
| 205 |
if ( play_block_or_win('X', |
| 206 |
winning_line[n].first, winning_line[n].last) ) |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
/* Find any unoccupied position. */ |
| 211 |
claim_empty_field (pawnlist_old); |
| 212 |
} |
| 213 |
|
| 214 |
void get_cpu_move (int level) |
| 215 |
{ |
| 216 |
switch (level) |
| 217 |
{ |
| 218 |
case 1: |
| 219 |
get_cpu_normal_move (); |
| 220 |
break; |
| 221 |
case 2: |
| 222 |
get_cpu_hard_move (); |
| 223 |
break; |
| 224 |
case 3: |
| 225 |
default: |
| 226 |
get_cpu_harder_move (); |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
|