| 4 |
|
|
| 5 |
uses |
uses |
| 6 |
System.SysUtils, System.Types, System.UITypes, System.Classes, |
System.SysUtils, System.Types, System.UITypes, System.Classes, |
| 7 |
System.Variants, |
System.Variants, Generics.Collections, |
| 8 |
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus, |
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Menus, |
| 9 |
System.Math, FMX.Objects, FMX.StdCtrls; |
System.Math, FMX.Objects, FMX.StdCtrls; |
| 10 |
|
|
| 11 |
const |
const |
| 12 |
Count = 8; |
bmp_count = 8; |
| 13 |
|
|
| 14 |
type |
type |
| 15 |
TStoneType = (stNone, stWhite, stBlack, stError); |
TStoneType = (stNone, stWhite, stBlack, stError, stEffect); |
| 16 |
|
|
| 17 |
TGridData = array [0 .. Count - 1] of array [0 .. Count - 1] of TStoneType; |
TEffectData = record |
| 18 |
|
X, Y: integer; |
| 19 |
|
Left, Top: integer; |
| 20 |
|
end; |
| 21 |
|
|
| 22 |
|
TGridData = array [0 .. bmp_count - 1] of array [0 .. bmp_count - 1] |
| 23 |
|
of TStoneType; |
| 24 |
|
|
| 25 |
TPlayer = class |
TPlayer = class(TObject) |
| 26 |
private |
private |
| 27 |
FAuto: Boolean; |
FAuto: Boolean; |
| 28 |
FStone: TStoneType; |
FStone: TStoneType; |
| 31 |
property Stone: TStoneType read FStone write FStone; |
property Stone: TStoneType read FStone write FStone; |
| 32 |
end; |
end; |
| 33 |
|
|
| 34 |
TStoneGrid = class |
TStoneGrid = class(TObject) |
| 35 |
private |
private |
| 36 |
FStrings: TGridData; |
FStrings: TGridData; |
| 37 |
FBuffer: array [0 .. Count * Count - 4] of TGridData; |
FBuffer: array [0 .. bmp_count * bmp_count - 4] of TGridData; |
| 38 |
FTurnNumber: integer; |
FTurnNumber: integer; |
| 39 |
FTurnIndex: integer; |
FTurnIndex: integer; |
| 40 |
FActive: Boolean; |
FActive: Boolean; |
| 41 |
|
FList: TList<TEffectData>; |
| 42 |
|
FEffectStone: TStoneType; |
| 43 |
|
FIndex_X: integer; |
| 44 |
|
FIndex_Y: integer; |
| 45 |
|
FGameOver: Boolean; |
| 46 |
function GetStrings(X, Y: integer): TStoneType; |
function GetStrings(X, Y: integer): TStoneType; |
| 47 |
procedure SetStrings(X, Y: integer; const Value: TStoneType); |
procedure SetStrings(X, Y: integer; const Value: TStoneType); |
| 48 |
procedure SetTurnNumber(const Value: integer); |
procedure SetTurnNumber(const Value: integer); |
| 49 |
|
function GetActive: Boolean; |
| 50 |
|
procedure SetActive(const Value: Boolean); |
| 51 |
public |
public |
| 52 |
|
constructor Create; |
| 53 |
|
destructor Destroy; override; |
| 54 |
procedure Clear; |
procedure Clear; |
| 55 |
function CalScore(Stone: TStoneType; X, Y: integer): integer; |
function CalScore(Stone: TStoneType; X, Y: integer; |
| 56 |
|
out Score: integer): Boolean; |
| 57 |
function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean; |
function CanSetStone(Stone: TStoneType; X, Y: integer; Reverse: Boolean; |
| 58 |
const Visible: Boolean = false): Boolean; |
const Visible: Boolean = false): Boolean; |
| 59 |
function NextStone(Stone: TStoneType): TPoint; |
function NextStone(Stone: TStoneType; var Pos: TPoint): Boolean; |
| 60 |
procedure Start; |
procedure Start; |
| 61 |
procedure Restart; |
procedure Restart; |
| 62 |
procedure Pause; |
procedure Pause; |
| 63 |
|
function ListExecute: Boolean; |
| 64 |
|
procedure GameOver; |
| 65 |
|
procedure Paint(Canvas: TCanvas); |
| 66 |
|
procedure ImageCount(X, Y: integer); |
| 67 |
|
function AddScore(X, Y: integer; const NG: array of TPoint): integer; |
| 68 |
property Strings[X, Y: integer]: TStoneType read GetStrings |
property Strings[X, Y: integer]: TStoneType read GetStrings |
| 69 |
write SetStrings; default; |
write SetStrings; default; |
| 70 |
property TurnNumber: integer read FTurnNumber write SetTurnNumber; |
property TurnNumber: integer read FTurnNumber write SetTurnNumber; |
| 71 |
property Active: Boolean read FActive; |
property Active: Boolean read GetActive write SetActive; |
| 72 |
end; |
end; |
| 73 |
|
|
| 74 |
TForm1 = class(TForm) |
TForm1 = class(TForm) |
| 87 |
MenuItem10: TMenuItem; |
MenuItem10: TMenuItem; |
| 88 |
MenuItem11: TMenuItem; |
MenuItem11: TMenuItem; |
| 89 |
MenuItem12: TMenuItem; |
MenuItem12: TMenuItem; |
| 90 |
|
Timer2: TTimer; |
| 91 |
|
Image1: TImage; |
| 92 |
|
Image2: TImage; |
| 93 |
|
Image3: TImage; |
| 94 |
|
MenuItem13: TMenuItem; |
| 95 |
|
MenuItem14: TMenuItem; |
| 96 |
|
MenuItem15: TMenuItem; |
| 97 |
procedure FormCreate(Sender: TObject); |
procedure FormCreate(Sender: TObject); |
| 98 |
procedure FormDestroy(Sender: TObject); |
procedure FormDestroy(Sender: TObject); |
| 99 |
procedure Timer1Timer(Sender: TObject); |
procedure Timer1Timer(Sender: TObject); |
| 109 |
procedure MenuItem8Click(Sender: TObject); |
procedure MenuItem8Click(Sender: TObject); |
| 110 |
procedure MenuItem10Click(Sender: TObject); |
procedure MenuItem10Click(Sender: TObject); |
| 111 |
procedure MenuItem11Click(Sender: TObject); |
procedure MenuItem11Click(Sender: TObject); |
| 112 |
|
procedure Timer2Timer(Sender: TObject); |
| 113 |
private |
private |
| 114 |
{ Private 宣言 } |
{ Private 宣言 } |
| 115 |
StoneGrid: TStoneGrid; |
StoneGrid: TStoneGrid; |
| 132 |
|
|
| 133 |
{$R *.fmx} |
{$R *.fmx} |
| 134 |
{$R *.Windows.fmx MSWINDOWS} |
{$R *.Windows.fmx MSWINDOWS} |
| 135 |
|
{$R *.XLgXhdpiTb.fmx ANDROID} |
| 136 |
{ TStoneGrid } |
{ TStoneGrid } |
| 137 |
|
|
| 138 |
function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer): integer; |
function TStoneGrid.AddScore(X, Y: integer; const NG: array of TPoint): integer; |
| 139 |
|
var |
| 140 |
|
s: TPoint; |
| 141 |
|
begin |
| 142 |
|
result := 0; |
| 143 |
|
for s in NG do |
| 144 |
|
if (X = s.X) and (Y = s.Y) then |
| 145 |
|
begin |
| 146 |
|
result := 10; |
| 147 |
|
break; |
| 148 |
|
end; |
| 149 |
|
end; |
| 150 |
|
|
| 151 |
|
function TStoneGrid.CalScore(Stone: TStoneType; X, Y: integer; |
| 152 |
|
out Score: integer): Boolean; |
| 153 |
var |
var |
| 154 |
i, j: integer; |
i, j: integer; |
| 155 |
|
loop: integer; |
| 156 |
|
const |
| 157 |
|
waste: array [1 .. 12] of TPoint = ((X: 1; Y: 0), (X: 6; Y: 0), (X: 0; Y: 1), |
| 158 |
|
(X: 1; Y: 1), (X: 6; Y: 1), (X: 7; Y: 1), (X: 0; Y: 6), (X: 1; Y: 6), (X: 6; |
| 159 |
|
Y: 6), (X: 7; Y: 6), (X: 1; Y: 7), (X: 6; Y: 7)); |
| 160 |
|
worth: array [1 .. 4] of TPoint = ((X: 0; Y: 0), (X: 7; Y: 0), (X: 0; Y: 7), |
| 161 |
|
(X: 7; Y: 7)); |
| 162 |
|
label Last; |
| 163 |
|
procedure Easy; |
| 164 |
|
var |
| 165 |
|
m, n: integer; |
| 166 |
|
begin |
| 167 |
|
for m := 0 to bmp_count - 1 do |
| 168 |
|
for n := 0 to bmp_count - 1 do |
| 169 |
|
if CanSetStone(Stone, m, n, false) = true then |
| 170 |
|
begin |
| 171 |
|
inc(Score); |
| 172 |
|
inc(Score, AddScore(m, n, worth)); |
| 173 |
|
end; |
| 174 |
|
end; |
| 175 |
|
procedure Hard; |
| 176 |
|
var |
| 177 |
|
m, n: integer; |
| 178 |
|
begin |
| 179 |
|
if loop > 1 then |
| 180 |
|
Exit; |
| 181 |
|
inc(loop); |
| 182 |
|
for m := 0 to bmp_count - 1 do |
| 183 |
|
for n := 0 to bmp_count - 1 do |
| 184 |
|
begin |
| 185 |
|
if CanSetStone(Stone, m, n, true) = true then |
| 186 |
|
begin |
| 187 |
|
if (loop mod 2) > 0 then |
| 188 |
|
inc(Score) |
| 189 |
|
else |
| 190 |
|
dec(Score); |
| 191 |
|
case Stone of |
| 192 |
|
stBlack: |
| 193 |
|
Stone := stWhite; |
| 194 |
|
stWhite: |
| 195 |
|
Stone := stBlack; |
| 196 |
|
end; |
| 197 |
|
Hard; |
| 198 |
|
if loop > 1 then |
| 199 |
|
begin |
| 200 |
|
Easy; |
| 201 |
|
FStrings := FBuffer[FTurnIndex + loop]; |
| 202 |
|
end |
| 203 |
|
else |
| 204 |
|
FBuffer[FTurnIndex + loop] := FStrings; |
| 205 |
|
end; |
| 206 |
|
end; |
| 207 |
|
dec(loop); |
| 208 |
|
end; |
| 209 |
|
|
| 210 |
begin |
begin |
| 211 |
if CanSetStone(Stone, X, Y, true) = true then |
if CanSetStone(Stone, X, Y, true) = true then |
| 212 |
begin |
begin |
| 213 |
if Stone = stBlack then |
Score := 0; |
| 214 |
Stone := stWhite |
result := true; |
| 215 |
|
if FTurnIndex < 50 then |
| 216 |
|
inc(Score, AddScore(X, Y, waste)); |
| 217 |
|
dec(Score, AddScore(X, Y, worth)); |
| 218 |
|
case Stone of |
| 219 |
|
stBlack: |
| 220 |
|
Stone := stWhite; |
| 221 |
|
stWhite: |
| 222 |
|
Stone := stBlack; |
| 223 |
|
end; |
| 224 |
|
if (Form1.MenuItem14.IsChecked = true) and (FTurnIndex + 2 <= 60) then |
| 225 |
|
begin |
| 226 |
|
loop := 0; |
| 227 |
|
Hard; |
| 228 |
|
end |
| 229 |
else |
else |
| 230 |
Stone := stBlack; |
Easy; |
|
result := 0; |
|
|
for i := 0 to Count - 1 do |
|
|
for j := 0 to Count - 1 do |
|
|
if CanSetStone(Stone, i, j, false) = true then |
|
|
inc(result); |
|
|
FStrings := FBuffer[FTurnIndex]; |
|
| 231 |
end |
end |
| 232 |
else |
else |
| 233 |
begin |
result := false; |
| 234 |
FStrings := FBuffer[FTurnIndex]; |
FStrings := FBuffer[FTurnIndex]; |
|
result := -1; |
|
|
end; |
|
| 235 |
end; |
end; |
| 236 |
|
|
| 237 |
function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer; |
function TStoneGrid.CanSetStone(Stone: TStoneType; X, Y: integer; |
| 238 |
Reverse: Boolean; const Visible: Boolean): Boolean; |
Reverse: Boolean; const Visible: Boolean): Boolean; |
| 239 |
var |
var |
| 240 |
i, k: integer; |
i: integer; |
| 241 |
p: Boolean; |
p: Boolean; |
| 242 |
q: ^TPoint; |
q: TEffectData; |
|
list: TList; |
|
| 243 |
procedure Method(m, n: integer); |
procedure Method(m, n: integer); |
| 244 |
var |
var |
| 245 |
s: TStoneType; |
s: TStoneType; |
| 246 |
j: integer; |
j: integer; |
| 247 |
|
k: integer; |
| 248 |
begin |
begin |
| 249 |
if p = false then |
if p = false then |
| 250 |
Exit; |
Exit; |
| 252 |
while true do |
while true do |
| 253 |
begin |
begin |
| 254 |
s := GetStrings(X + m * i, Y + n * i); |
s := GetStrings(X + m * i, Y + n * i); |
| 255 |
|
if s = stEffect then |
| 256 |
|
s := FEffectStone; |
| 257 |
if (s = stNone) or (s = stError) then |
if (s = stNone) or (s = stError) then |
| 258 |
break |
break |
| 259 |
else if s = Stone then |
else if s = Stone then |
| 260 |
if i > 1 then |
if i > 1 then |
| 261 |
begin |
begin |
| 262 |
|
if (result = false) and (Reverse = true) then |
| 263 |
|
SetStrings(X, Y, Stone); |
| 264 |
result := true; |
result := true; |
| 265 |
if Reverse = true then |
if Reverse = true then |
| 266 |
begin |
begin |
| 267 |
|
Form1.PaintBox1.Repaint; |
| 268 |
for j := 1 to i - 1 do |
for j := 1 to i - 1 do |
| 269 |
begin |
begin |
| 270 |
New(q); |
if Visible = true then |
| 271 |
q^ := Point(X + m * j, Y + n * j); |
begin |
| 272 |
list.Add(q); |
FEffectStone := Stone; |
| 273 |
|
q.Left := X + m * j; |
| 274 |
|
q.Top := Y + n * j; |
| 275 |
|
q.X := 0; |
| 276 |
|
q.Y := 0; |
| 277 |
|
FList.Add(q); |
| 278 |
|
SetStrings(q.Left, q.Top, stEffect); |
| 279 |
|
for k := 1 to 10 do |
| 280 |
|
begin |
| 281 |
|
Sleep(15); |
| 282 |
|
Application.ProcessMessages; |
| 283 |
|
end; |
| 284 |
|
end |
| 285 |
|
else |
| 286 |
|
SetStrings(X + m * j, Y + n * j, Stone); |
| 287 |
end; |
end; |
| 288 |
break; |
break; |
| 289 |
end |
end |
| 301 |
end; |
end; |
| 302 |
|
|
| 303 |
begin |
begin |
| 304 |
list := TList.Create; |
result := false; |
| 305 |
try |
p := true; |
| 306 |
result := false; |
if GetStrings(X, Y) = stNone then |
| 307 |
p := true; |
begin |
| 308 |
if GetStrings(X, Y) = stNone then |
Method(-1, -1); |
| 309 |
begin |
Method(-1, 0); |
| 310 |
Method(-1, -1); |
Method(-1, 1); |
| 311 |
Method(-1, 0); |
Method(0, -1); |
| 312 |
Method(-1, 1); |
Method(0, 1); |
| 313 |
Method(0, -1); |
Method(1, -1); |
| 314 |
Method(0, 1); |
Method(1, 0); |
| 315 |
Method(1, -1); |
Method(1, 1); |
|
Method(1, 0); |
|
|
Method(1, 1); |
|
|
if (Reverse = true) and (result = true) then |
|
|
begin |
|
|
SetStrings(X, Y, Stone); |
|
|
for i := 0 to list.Count - 1 do |
|
|
begin |
|
|
if Visible = true then |
|
|
begin |
|
|
for k := 1 to 10 do |
|
|
begin |
|
|
Sleep(10); |
|
|
Application.ProcessMessages; |
|
|
end; |
|
|
Form1.PaintBox1.Repaint; |
|
|
end; |
|
|
q := list[i]; |
|
|
SetStrings(q^.X, q^.Y, Stone); |
|
|
end; |
|
|
end; |
|
|
end; |
|
|
finally |
|
|
for i := 0 to list.Count - 1 do |
|
|
Dispose(list[i]); |
|
|
list.Free; |
|
|
end; |
|
|
if (Visible = true)and(result = true) then |
|
|
begin |
|
|
inc(FTurnIndex); |
|
|
inc(FTurnNumber); |
|
|
FBuffer[FTurnIndex] := FStrings; |
|
| 316 |
end; |
end; |
| 317 |
end; |
end; |
| 318 |
|
|
| 320 |
var |
var |
| 321 |
i, j: integer; |
i, j: integer; |
| 322 |
begin |
begin |
| 323 |
for i := 0 to Count - 1 do |
FList.Clear; |
| 324 |
for j := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 325 |
|
for j := 0 to bmp_count - 1 do |
| 326 |
Strings[i, j] := stNone; |
Strings[i, j] := stNone; |
| 327 |
Strings[3, 3] := stBlack; |
Strings[3, 3] := stBlack; |
| 328 |
Strings[4, 4] := stBlack; |
Strings[4, 4] := stBlack; |
| 333 |
FBuffer[0] := FStrings; |
FBuffer[0] := FStrings; |
| 334 |
end; |
end; |
| 335 |
|
|
| 336 |
|
constructor TStoneGrid.Create; |
| 337 |
|
begin |
| 338 |
|
inherited; |
| 339 |
|
FList := TList<TEffectData>.Create; |
| 340 |
|
end; |
| 341 |
|
|
| 342 |
|
destructor TStoneGrid.Destroy; |
| 343 |
|
begin |
| 344 |
|
FList.Free; |
| 345 |
|
inherited; |
| 346 |
|
end; |
| 347 |
|
|
| 348 |
|
procedure TStoneGrid.GameOver; |
| 349 |
|
begin |
| 350 |
|
FGameOver := true; |
| 351 |
|
FActive := false; |
| 352 |
|
end; |
| 353 |
|
|
| 354 |
|
function TStoneGrid.GetActive: Boolean; |
| 355 |
|
begin |
| 356 |
|
if (FActive = true) and (FList.Count = 0) then |
| 357 |
|
result := true |
| 358 |
|
else |
| 359 |
|
result := false; |
| 360 |
|
end; |
| 361 |
|
|
| 362 |
function TStoneGrid.GetStrings(X, Y: integer): TStoneType; |
function TStoneGrid.GetStrings(X, Y: integer): TStoneType; |
| 363 |
begin |
begin |
| 364 |
if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then |
if (X >= 0) and (X < bmp_count) and (Y >= 0) and (Y < bmp_count) then |
| 365 |
result := FStrings[X, Y] |
result := FStrings[X, Y] |
| 366 |
else |
else |
| 367 |
result := stError; |
result := stError; |
| 368 |
end; |
end; |
| 369 |
|
|
| 370 |
function TStoneGrid.NextStone(Stone: TStoneType): TPoint; |
procedure TStoneGrid.ImageCount(X, Y: integer); |
| 371 |
|
begin |
| 372 |
|
FIndex_X := X; |
| 373 |
|
FIndex_Y := Y; |
| 374 |
|
end; |
| 375 |
|
|
| 376 |
|
function TStoneGrid.ListExecute: Boolean; |
| 377 |
var |
var |
| 378 |
i, j, m, n: integer; |
i: integer; |
| 379 |
|
s: TEffectData; |
| 380 |
begin |
begin |
| 381 |
n := -1; |
if FList.Count = 0 then |
| 382 |
for i := 0 to Count - 1 do |
result := false |
| 383 |
for j := 0 to Count - 1 do |
else |
| 384 |
|
begin |
| 385 |
|
i := 0; |
| 386 |
|
while i < FList.Count do |
| 387 |
begin |
begin |
| 388 |
m := CalScore(Stone, i, j); |
s := FList[i]; |
| 389 |
if (n = -1) or ((m > -1) and (n > m)) then |
if s.X < FIndex_X - 1 then |
| 390 |
|
s.X := s.X + 1 |
| 391 |
|
else if s.Y < FIndex_Y - 1 then |
| 392 |
begin |
begin |
| 393 |
n := m; |
s.X := 0; |
| 394 |
result := Point(i, j); |
s.Y := s.Y + 1; |
| 395 |
|
end |
| 396 |
|
else |
| 397 |
|
begin |
| 398 |
|
SetStrings(s.Left, s.Top, FEffectStone); |
| 399 |
|
FList.Delete(i); |
| 400 |
|
inc(i); |
| 401 |
|
continue; |
| 402 |
end; |
end; |
| 403 |
|
FList[i] := s; |
| 404 |
|
inc(i); |
| 405 |
|
end; |
| 406 |
|
if FList.Count = 0 then |
| 407 |
|
begin |
| 408 |
|
inc(FTurnIndex); |
| 409 |
|
inc(FTurnNumber); |
| 410 |
|
FBuffer[FTurnIndex] := FStrings; |
| 411 |
|
Form1.PaintBox1.Repaint; |
| 412 |
|
Form1.ChangePlayer; |
| 413 |
|
if FGameOver = false then |
| 414 |
|
FActive := true |
| 415 |
end; |
end; |
| 416 |
if n = -1 then |
result := true; |
| 417 |
result := Point(-1, -1); |
end; |
| 418 |
|
end; |
| 419 |
|
|
| 420 |
|
function TStoneGrid.NextStone(Stone: TStoneType; var Pos: TPoint): Boolean; |
| 421 |
|
var |
| 422 |
|
i, j, m, n: integer; |
| 423 |
|
begin |
| 424 |
|
result := false; |
| 425 |
|
n := 0; |
| 426 |
|
for i := 0 to bmp_count - 1 do |
| 427 |
|
for j := 0 to bmp_count - 1 do |
| 428 |
|
if (CalScore(Stone, i, j, m) = true) and ((result = false) or (m < n)) |
| 429 |
|
then |
| 430 |
|
begin |
| 431 |
|
if result = false then |
| 432 |
|
result := true; |
| 433 |
|
n := m; |
| 434 |
|
Pos := Point(i, j); |
| 435 |
|
end; |
| 436 |
|
end; |
| 437 |
|
|
| 438 |
|
procedure TStoneGrid.Paint(Canvas: TCanvas); |
| 439 |
|
var |
| 440 |
|
k: integer; |
| 441 |
|
s: TBitmap; |
| 442 |
|
p: TEffectData; |
| 443 |
|
begin |
| 444 |
|
k := Form1.Size; |
| 445 |
|
if FEffectStone = stBlack then |
| 446 |
|
s := Form1.Image1.Bitmap |
| 447 |
|
else |
| 448 |
|
s := Form1.Image2.Bitmap; |
| 449 |
|
for p in FList do |
| 450 |
|
begin |
| 451 |
|
Canvas.DrawBitmap(s, RectF(p.X * 50, p.Y * 50, (p.X + 1) * 50, |
| 452 |
|
(p.Y + 1) * 50), RectF(p.Left * k, p.Top * k, (p.Left + 1) * k, |
| 453 |
|
(p.Top + 1) * k), 1); |
| 454 |
|
end; |
| 455 |
end; |
end; |
| 456 |
|
|
| 457 |
procedure TStoneGrid.Pause; |
procedure TStoneGrid.Pause; |
| 462 |
procedure TStoneGrid.Restart; |
procedure TStoneGrid.Restart; |
| 463 |
begin |
begin |
| 464 |
FActive := true; |
FActive := true; |
| 465 |
|
FGameOver := false; |
| 466 |
FTurnIndex := FTurnNumber; |
FTurnIndex := FTurnNumber; |
| 467 |
end; |
end; |
| 468 |
|
|
| 469 |
|
procedure TStoneGrid.SetActive(const Value: Boolean); |
| 470 |
|
begin |
| 471 |
|
if (FGameOver = false) or (Value = false) then |
| 472 |
|
FActive := Value; |
| 473 |
|
end; |
| 474 |
|
|
| 475 |
procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType); |
procedure TStoneGrid.SetStrings(X, Y: integer; const Value: TStoneType); |
| 476 |
begin |
begin |
| 477 |
if (X >= 0) and (X < Count) and (Y >= 0) and (Y < Count) then |
if (X >= 0) and (X < bmp_count) and (Y >= 0) and (Y < bmp_count) then |
| 478 |
FStrings[X, Y] := Value; |
FStrings[X, Y] := Value; |
| 479 |
end; |
end; |
| 480 |
|
|
| 486 |
FTurnNumber := 0 |
FTurnNumber := 0 |
| 487 |
else |
else |
| 488 |
FTurnNumber := Value; |
FTurnNumber := Value; |
|
FActive := false; |
|
| 489 |
FStrings := FBuffer[FTurnNumber]; |
FStrings := FBuffer[FTurnNumber]; |
| 490 |
end; |
end; |
| 491 |
|
|
| 493 |
begin |
begin |
| 494 |
Clear; |
Clear; |
| 495 |
FActive := true; |
FActive := true; |
| 496 |
|
FGameOver := false; |
| 497 |
end; |
end; |
| 498 |
|
|
| 499 |
{ TForm1 } |
{ TForm1 } |
| 505 |
procedure Main; |
procedure Main; |
| 506 |
begin |
begin |
| 507 |
if Index = Player1 then |
if Index = Player1 then |
| 508 |
Index := Player2 |
begin |
| 509 |
|
Index := Player2; |
| 510 |
|
s := '白の手番です'; |
| 511 |
|
end |
| 512 |
else |
else |
| 513 |
|
begin |
| 514 |
Index := Player1; |
Index := Player1; |
| 515 |
|
s := '黒の手番です'; |
| 516 |
|
end; |
| 517 |
end; |
end; |
| 518 |
function Execute: Boolean; |
function Execute: Boolean; |
| 519 |
var |
var |
| 520 |
i, j: integer; |
i, j: integer; |
| 521 |
begin |
begin |
| 522 |
result := false; |
for i := 0 to bmp_count - 1 do |
| 523 |
for i := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
|
for j := 0 to Count - 1 do |
|
| 524 |
if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then |
if StoneGrid.CanSetStone(Index.Stone, i, j, false) = true then |
| 525 |
begin |
begin |
| 526 |
result := true; |
result := true; |
| 527 |
Exit; |
Exit; |
| 528 |
end; |
end; |
| 529 |
|
result := false; |
| 530 |
end; |
end; |
| 531 |
|
|
| 532 |
begin |
begin |
| 536 |
Main; |
Main; |
| 537 |
if Execute = false then |
if Execute = false then |
| 538 |
begin |
begin |
|
StoneGrid.Pause; |
|
|
Timer1.Enabled := false; |
|
| 539 |
m := 0; |
m := 0; |
| 540 |
n := 0; |
n := 0; |
| 541 |
for i := 0 to Count - 1 do |
for i := 0 to bmp_count - 1 do |
| 542 |
for j := 0 to Count - 1 do |
for j := 0 to bmp_count - 1 do |
| 543 |
case StoneGrid[i, j] of |
case StoneGrid[i, j] of |
| 544 |
stBlack: |
stBlack: |
| 545 |
inc(m); |
inc(m); |
| 546 |
stWhite: |
stWhite: |
| 547 |
inc(n); |
inc(n); |
| 548 |
end; |
end; |
| 549 |
|
Caption := s; |
| 550 |
if m > n then |
if m > n then |
| 551 |
s := 'Player1 Win:' + #13#10 |
s := 'Player1 Win:' + #13#10 |
| 552 |
else if m < n then |
else if m < n then |
| 553 |
s := 'Player2 Win:' + #13#10 |
s := 'Player2 Win:' + #13#10 |
| 554 |
else |
else |
| 555 |
s := 'Draw:' + #13#10; |
s := 'Draw:' + #13#10; |
| 556 |
Showmessage(s + '(Player1) ' + IntToStr(m) + #13#10 + '(Player2) ' + |
StoneGrid.GameOver; |
| 557 |
IntToStr(n)); |
Showmessage(s + '(Player1) ' + m.ToString + #13#10 + '(Player2) ' + |
| 558 |
end; |
n.ToString); |
| 559 |
end; |
end |
| 560 |
|
else |
| 561 |
|
Caption := s; |
| 562 |
|
end |
| 563 |
|
else |
| 564 |
|
Caption := s; |
| 565 |
end; |
end; |
| 566 |
|
|
| 567 |
procedure TForm1.CompStone; |
procedure TForm1.CompStone; |
| 568 |
var |
var |
| 569 |
s: TPoint; |
s: TPoint; |
| 570 |
begin |
begin |
| 571 |
s := StoneGrid.NextStone(Index.Stone); |
StoneGrid.Active := false; |
| 572 |
StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true); |
if StoneGrid.NextStone(Index.Stone, s) = true then |
| 573 |
PaintBox1.Repaint; |
begin |
| 574 |
ChangePlayer; |
StoneGrid.CanSetStone(Index.Stone, s.X, s.Y, true, true); |
| 575 |
|
PaintBox1.Repaint; |
| 576 |
|
end |
| 577 |
|
else |
| 578 |
|
ChangePlayer; |
| 579 |
end; |
end; |
| 580 |
|
|
| 581 |
procedure TForm1.GameStart; |
procedure TForm1.GameStart; |
| 582 |
begin |
begin |
| 583 |
|
Index := Player1; |
| 584 |
StoneGrid.Start; |
StoneGrid.Start; |
| 585 |
PaintBox1.Repaint; |
PaintBox1.Repaint; |
| 586 |
Index := Player1; |
Caption := '黒から始めます'; |
|
Timer1.Enabled := true; |
|
| 587 |
end; |
end; |
| 588 |
|
|
| 589 |
procedure TForm1.MenuItem10Click(Sender: TObject); |
procedure TForm1.MenuItem10Click(Sender: TObject); |
| 590 |
begin |
begin |
| 591 |
StoneGrid.Restart; |
StoneGrid.Restart; |
|
Timer1.Enabled := true; |
|
| 592 |
end; |
end; |
| 593 |
|
|
| 594 |
procedure TForm1.MenuItem11Click(Sender: TObject); |
procedure TForm1.MenuItem11Click(Sender: TObject); |
| 595 |
|
var |
| 596 |
|
i: integer; |
| 597 |
begin |
begin |
|
Timer1.Enabled := false; |
|
| 598 |
with StoneGrid do |
with StoneGrid do |
| 599 |
|
begin |
| 600 |
|
i := TurnNumber; |
| 601 |
if Sender = MenuItem11 then |
if Sender = MenuItem11 then |
| 602 |
TurnNumber := TurnNumber + 1 |
TurnNumber := TurnNumber + 1 |
| 603 |
else |
else |
| 604 |
TurnNumber := TurnNumber - 1; |
TurnNumber := TurnNumber - 1; |
| 605 |
ChangePlayer; |
if (i = TurnNumber) then |
| 606 |
|
Exit |
| 607 |
|
else |
| 608 |
|
Pause; |
| 609 |
|
end; |
| 610 |
PaintBox1.Repaint; |
PaintBox1.Repaint; |
| 611 |
|
ChangePlayer; |
| 612 |
end; |
end; |
| 613 |
|
|
| 614 |
procedure TForm1.MenuItem2Click(Sender: TObject); |
procedure TForm1.MenuItem2Click(Sender: TObject); |
| 615 |
begin |
begin |
| 616 |
|
Timer1.Enabled := false; |
| 617 |
|
Timer2.Enabled := false; |
| 618 |
GameStart; |
GameStart; |
| 619 |
|
Timer1.Enabled := true; |
| 620 |
|
Timer2.Enabled := true; |
| 621 |
end; |
end; |
| 622 |
|
|
| 623 |
procedure TForm1.MenuItem4Click(Sender: TObject); |
procedure TForm1.MenuItem4Click(Sender: TObject); |
| 629 |
begin |
begin |
| 630 |
Player1.Auto := MenuItem6.IsChecked; |
Player1.Auto := MenuItem6.IsChecked; |
| 631 |
Player2.Auto := MenuItem7.IsChecked; |
Player2.Auto := MenuItem7.IsChecked; |
|
MenuItem10Click(Sender); |
|
| 632 |
end; |
end; |
| 633 |
|
|
| 634 |
procedure TForm1.MenuItem8Click(Sender: TObject); |
procedure TForm1.MenuItem8Click(Sender: TObject); |
| 635 |
begin |
begin |
| 636 |
StoneGrid.Pause; |
StoneGrid.Pause; |
|
Timer1.Enabled := false; |
|
| 637 |
end; |
end; |
| 638 |
|
|
| 639 |
procedure TForm1.PaintBox1Paint(Sender: TObject; Canvas: TCanvas); |
procedure TForm1.PaintBox1Paint(Sender: TObject; Canvas: TCanvas); |
| 640 |
var |
var |
| 641 |
i, j: integer; |
i, j: integer; |
| 642 |
begin |
begin |
| 643 |
Canvas.Fill.Color := TAlphaColors.White; |
if StoneGrid.Active = false then |
| 644 |
Canvas.FillRect(RectF(0, 0, Count * Size, Count * Size), 0, 0, [], 1); |
StoneGrid.Paint(Canvas); |
| 645 |
for i := 0 to Count do |
for i := 0 to bmp_count - 1 do |
| 646 |
begin |
begin |
| 647 |
Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * Count), 1); |
for j := 0 to bmp_count - 1 do |
|
for j := 0 to Count do |
|
| 648 |
begin |
begin |
|
Canvas.DrawLine(PointF(0, j * Size), PointF(Count * Size, j * Size), 1); |
|
| 649 |
case StoneGrid.Strings[i, j] of |
case StoneGrid.Strings[i, j] of |
| 650 |
stWhite: |
stWhite: |
| 651 |
Canvas.DrawEllipse(RectF(i * Size, j * Size, (i + 1) * Size, |
Canvas.DrawBitmap(Image3.Bitmap, RectF(100, 0, 150, 50), |
| 652 |
(j + 1) * Size), 1); |
RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1); |
| 653 |
stBlack: |
stBlack: |
| 654 |
begin |
Canvas.DrawBitmap(Image3.Bitmap, RectF(50, 0, 100, 50), |
| 655 |
Canvas.Fill.Color := TAlphaColors.Black; |
RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1); |
| 656 |
Canvas.FillEllipse(RectF(i * Size, j * Size, (i + 1) * Size, |
stEffect: |
| 657 |
(j + 1) * Size), 1); |
continue; |
| 658 |
end; |
else |
| 659 |
|
Canvas.DrawBitmap(Image3.Bitmap, RectF(0, 0, 50, 50), |
| 660 |
|
RectF(i * Size, j * Size, (i + 1) * Size, (j + 1) * Size), 1); |
| 661 |
end; |
end; |
| 662 |
|
Canvas.DrawLine(PointF(0, j * Size), PointF(bmp_count * Size, |
| 663 |
|
j * Size), 1); |
| 664 |
end; |
end; |
| 665 |
|
Canvas.DrawLine(PointF(i * Size, 0), PointF(i * Size, Size * bmp_count), 1); |
| 666 |
end; |
end; |
| 667 |
|
Canvas.DrawLine(PointF(bmp_count * Size, 0), PointF(bmp_count * Size, |
| 668 |
|
bmp_count * Size), 1); |
| 669 |
|
Canvas.DrawLine(PointF(0, bmp_count * Size), PointF(bmp_count * Size, |
| 670 |
|
bmp_count * Size), 1); |
| 671 |
end; |
end; |
| 672 |
|
|
| 673 |
procedure TForm1.PaintBox1Resize(Sender: TObject); |
procedure TForm1.PaintBox1Resize(Sender: TObject); |
| 674 |
begin |
begin |
| 675 |
Size := Min(ClientWidth, ClientHeight) div Count; |
Size := Min(ClientWidth, ClientHeight) div bmp_count; |
| 676 |
end; |
end; |
| 677 |
|
|
| 678 |
procedure TForm1.FormCreate(Sender: TObject); |
procedure TForm1.FormCreate(Sender: TObject); |
| 679 |
begin |
begin |
| 680 |
|
ClientWidth := 400; |
| 681 |
|
ClientHeight := 400; |
| 682 |
StoneGrid := TStoneGrid.Create; |
StoneGrid := TStoneGrid.Create; |
| 683 |
|
StoneGrid.ImageCount(6, 5); |
| 684 |
Player1 := TPlayer.Create; |
Player1 := TPlayer.Create; |
| 685 |
Player2 := TPlayer.Create; |
Player2 := TPlayer.Create; |
| 686 |
Player1.Stone := stBlack; |
Player1.Stone := stBlack; |
| 712 |
procedure TForm1.Timer1Timer(Sender: TObject); |
procedure TForm1.Timer1Timer(Sender: TObject); |
| 713 |
begin |
begin |
| 714 |
if (StoneGrid.Active = true) and (Index.Auto = true) then |
if (StoneGrid.Active = true) and (Index.Auto = true) then |
|
begin |
|
|
Timer1.Enabled := false; |
|
| 715 |
CompStone; |
CompStone; |
| 716 |
Timer1.Enabled := true; |
end; |
| 717 |
end; |
|
| 718 |
|
procedure TForm1.Timer2Timer(Sender: TObject); |
| 719 |
|
begin |
| 720 |
|
if (StoneGrid.Active = false) and (StoneGrid.ListExecute = true) then |
| 721 |
|
PaintBox1.Repaint; |
| 722 |
end; |
end; |
| 723 |
|
|
| 724 |
procedure TForm1.FormResize(Sender: TObject); |
procedure TForm1.FormResize(Sender: TObject); |
| 725 |
begin |
begin |
| 726 |
Size := Min(ClientWidth, ClientHeight) div Count; |
Size := Min(ClientWidth, ClientHeight) div bmp_count; |
| 727 |
PaintTo(Canvas); |
PaintTo(Canvas); |
| 728 |
end; |
end; |
| 729 |
|
|
| 732 |
if Index.Auto = false then |
if Index.Auto = false then |
| 733 |
begin |
begin |
| 734 |
MenuItem10Click(Sender); |
MenuItem10Click(Sender); |
| 735 |
|
StoneGrid.Active := false; |
| 736 |
if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size), |
if StoneGrid.CanSetStone(Index.Stone, Floor(Point.X / Size), |
| 737 |
Floor(Point.Y / Size), true, true) = true then |
Floor(Point.Y / Size), true, true) = true then |
|
begin |
|
| 738 |
PaintBox1.Repaint; |
PaintBox1.Repaint; |
| 739 |
ChangePlayer; |
StoneGrid.Active := true; |
|
end; |
|
| 740 |
end; |
end; |
| 741 |
end; |
end; |
| 742 |
|
|