diff --git a/Data/DataAccessContext.TicTacToc.cs b/Data/DataAccessContext.TicTacToc.cs index 6afc569..d6bae2d 100644 --- a/Data/DataAccessContext.TicTacToc.cs +++ b/Data/DataAccessContext.TicTacToc.cs @@ -16,7 +16,7 @@ namespace ProjectGrid.Data } - public Models.TicTacTocBoard GetBoard() + public Models.TicTacTocBrain GetBoard() { throw new System.NotImplementedException(); } diff --git a/Data/ITicTacToctRepository.cs b/Data/ITicTacToctRepository.cs index 86a584a..46fcdfc 100644 --- a/Data/ITicTacToctRepository.cs +++ b/Data/ITicTacToctRepository.cs @@ -4,7 +4,7 @@ namespace ProjectGrid.Data { public interface ITicTacToctRepository { - TicTacTocBoard GetBoard(); + TicTacTocBrain GetBoard(); bool AddPiece(TicTacTocRequest user); } diff --git a/Logic/AI/TicTacTocBrain.cs b/Logic/AI/TicTacTocBrain.cs new file mode 100644 index 0000000..172488c --- /dev/null +++ b/Logic/AI/TicTacTocBrain.cs @@ -0,0 +1,68 @@ +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 _prio; + + public TicTacTocBrain(TicTacTocBoard board, Player player) + { + _board = board; + _player = (int)player; + + var rand = new Random(); + _prio = (new List { 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; + } + } +} \ No newline at end of file diff --git a/Logic/TicTacTocBoard.cs b/Logic/TicTacTocBoard.cs index f8d2b7e..7323b8a 100644 --- a/Logic/TicTacTocBoard.cs +++ b/Logic/TicTacTocBoard.cs @@ -11,7 +11,7 @@ namespace ProjectGrid.Models { private List _field; public List Field => _field; - + private int _lastSetValue; public int LastSetValue => _lastSetValue; @@ -38,7 +38,7 @@ namespace ProjectGrid.Models // 0 1 2 // 3 4 5 // 6 7 8 - private static readonly int[,] _winning = new int[,] { + public static readonly int[,] _winning = new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7 ,8 }, diff --git a/Logic/TicTacTocManager.cs b/Logic/TicTacTocManager.cs index 63f1782..c537244 100644 --- a/Logic/TicTacTocManager.cs +++ b/Logic/TicTacTocManager.cs @@ -1,4 +1,5 @@ using ProjectGrid.Models; +using ProjectGrid.TicTacToc; using System; using System.Collections.Generic; @@ -6,7 +7,7 @@ namespace ProjectGrid { public interface ITicTacTocManager { - public void Restart(); + public void Restart(Mode mode); public TicTacTocResponse NextMove(TicTacTocRequest move); public TicTacTocResponse GetBoard(); @@ -35,7 +36,7 @@ namespace ProjectGrid { //_repo = repo; _currentPlayer = 1; - Restart(); + Restart(Mode.TWO_PLAYER); } public TicTacTocResponse NextMove(TicTacTocRequest move) @@ -64,7 +65,7 @@ namespace ProjectGrid } - public void Restart() + public void Restart(Mode mode) { _currentPlayer = new Random().Next(1, 2); _board = new TicTacTocBoard(); diff --git a/Logic/TicTacTocTypes.cs b/Logic/TicTacTocTypes.cs new file mode 100644 index 0000000..97fa6b9 --- /dev/null +++ b/Logic/TicTacTocTypes.cs @@ -0,0 +1,18 @@ +using ProjectGrid.Models; + +namespace ProjectGrid.TicTacToc +{ + public enum Mode + { + TWO_PLAYER, + CROSS_PLAYER, + CIRCLE_PLAYER, + } + + public enum Player + { + EMPTY = 0; + CROSS = 1, + CIRCLE = 2, + } +} diff --git a/Startup.cs b/Startup.cs index 9199bb5..ecd124d 100644 --- a/Startup.cs +++ b/Startup.cs @@ -47,7 +47,7 @@ namespace ProjectGrid // Manager services.AddSingleton(new TicTacTocManager()); - services.AddScoped(); + services.AddScoped(); // DataAccess services.AddScoped(); diff --git a/clientapp/src/Game.js b/clientapp/src/Game.js index 82fc622..2c1f42e 100644 --- a/clientapp/src/Game.js +++ b/clientapp/src/Game.js @@ -23,7 +23,6 @@ class Game { } - changeTurn() { this.currentTurn = this.currentTurn == this.players[0].sign ? this.players[1].sign : this.players[0].sign; } @@ -46,7 +45,6 @@ class Game { x.setPosition(); }) - if (this.players[0].positions.length > 2 || this.players[1].positions.length > 2) { let player1_combinations = this.getCombination(this.players[0].positions, 3, 0);