68 lines
1.5 KiB
C#
68 lines
1.5 KiB
C#
using ProjectGrid.Models;
|
|
using ProjectGrid.TicTacToc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace ProjectGrid.AI
|
|
{
|
|
public class TicTacTocBrain
|
|
{
|
|
private TicTacTocBoard _board;
|
|
private int _player;
|
|
|
|
private List<int> _prio;
|
|
|
|
public TicTacTocBrain(TicTacTocBoard board, Player player)
|
|
{
|
|
_board = board;
|
|
_player = (int)player;
|
|
|
|
var rand = new Random();
|
|
_prio = (new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8 }).OrderBy(i => rand.Next()).ToList();
|
|
}
|
|
|
|
public int Turn()
|
|
{
|
|
if (_board.Field[5] == (int)Player.EMPTY)
|
|
{
|
|
//_board.SetFieldValue(_player, 5);
|
|
return 5;
|
|
}
|
|
|
|
for (int x = 0; x < TicTacTocBoard._winning.GetLength(0); x++)
|
|
{
|
|
var _1 = TicTacTocBoard._winning[x, 0];
|
|
var _2 = TicTacTocBoard._winning[x, 1];
|
|
var _3 = TicTacTocBoard._winning[x, 2];
|
|
|
|
int result = Match2(_1, _2, _3);
|
|
if (result > 0)
|
|
{
|
|
//_board.SetFieldValue(_player, );
|
|
return TicTacTocBoard._winning[x, result];
|
|
}
|
|
}
|
|
|
|
foreach (var i in _prio)
|
|
if (_board.Field[i] == 0)
|
|
return i;
|
|
|
|
return -1;
|
|
}
|
|
|
|
private int Match2(int _1, int _2, int _3)
|
|
{
|
|
if (_1 == _player && _2 == _player && _3 == 0)
|
|
return 3;
|
|
|
|
if (_1 == _player && _2 == 0 && _3 == _player)
|
|
return 2;
|
|
|
|
if (_1 == 0 && _2 == _player && _3 == _player)
|
|
return 1;
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
} |