added tttBrain

This commit is contained in:
Shuozhe 2021-07-22 00:38:22 +02:00
parent 2492dfc796
commit 0edb73d8a3
8 changed files with 95 additions and 10 deletions

View File

@ -16,7 +16,7 @@ namespace ProjectGrid.Data
} }
public Models.TicTacTocBoard GetBoard() public Models.TicTacTocBrain GetBoard()
{ {
throw new System.NotImplementedException(); throw new System.NotImplementedException();
} }

View File

@ -4,7 +4,7 @@ namespace ProjectGrid.Data
{ {
public interface ITicTacToctRepository public interface ITicTacToctRepository
{ {
TicTacTocBoard GetBoard(); TicTacTocBrain GetBoard();
bool AddPiece(TicTacTocRequest user); bool AddPiece(TicTacTocRequest user);
} }

View File

@ -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<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;
}
}
}

View File

@ -38,7 +38,7 @@ namespace ProjectGrid.Models
// 0 1 2 // 0 1 2
// 3 4 5 // 3 4 5
// 6 7 8 // 6 7 8
private static readonly int[,] _winning = new int[,] { public static readonly int[,] _winning = new int[,] {
{ 0, 1, 2 }, { 0, 1, 2 },
{ 3, 4, 5 }, { 3, 4, 5 },
{ 6, 7 ,8 }, { 6, 7 ,8 },

View File

@ -1,4 +1,5 @@
using ProjectGrid.Models; using ProjectGrid.Models;
using ProjectGrid.TicTacToc;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -6,7 +7,7 @@ namespace ProjectGrid
{ {
public interface ITicTacTocManager public interface ITicTacTocManager
{ {
public void Restart(); public void Restart(Mode mode);
public TicTacTocResponse NextMove(TicTacTocRequest move); public TicTacTocResponse NextMove(TicTacTocRequest move);
public TicTacTocResponse GetBoard(); public TicTacTocResponse GetBoard();
@ -35,7 +36,7 @@ namespace ProjectGrid
{ {
//_repo = repo; //_repo = repo;
_currentPlayer = 1; _currentPlayer = 1;
Restart(); Restart(Mode.TWO_PLAYER);
} }
public TicTacTocResponse NextMove(TicTacTocRequest move) 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); _currentPlayer = new Random().Next(1, 2);
_board = new TicTacTocBoard(); _board = new TicTacTocBoard();

18
Logic/TicTacTocTypes.cs Normal file
View File

@ -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,
}
}

View File

@ -47,7 +47,7 @@ namespace ProjectGrid
// Manager // Manager
services.AddSingleton<TicTacTocManager>(new TicTacTocManager()); services.AddSingleton<TicTacTocManager>(new TicTacTocManager());
services.AddScoped<ITicTacTocManager, TicTacTocManager>(); services.AddScoped<TicTacTocTypes, TicTacTocManager>();
// DataAccess // DataAccess
services.AddScoped<ITicTacToctRepository, DataAccessContext>(); services.AddScoped<ITicTacToctRepository, DataAccessContext>();

View File

@ -23,7 +23,6 @@ class Game {
} }
changeTurn() { changeTurn() {
this.currentTurn = this.currentTurn == this.players[0].sign ? this.players[1].sign : this.players[0].sign; this.currentTurn = this.currentTurn == this.players[0].sign ? this.players[1].sign : this.players[0].sign;
} }
@ -46,7 +45,6 @@ class Game {
x.setPosition(); x.setPosition();
}) })
if (this.players[0].positions.length > 2 || this.players[1].positions.length > 2) { if (this.players[0].positions.length > 2 || this.players[1].positions.length > 2) {
let player1_combinations = this.getCombination(this.players[0].positions, 3, 0); let player1_combinations = this.getCombination(this.players[0].positions, 3, 0);