void ReceiveTurn(ITurn turn) { //Apply the turn and redraw the board board = turn.ApplyTurn(board) as ConnectKBoard; DrawBoard(); //check to see if the game is over if (board.IsTerminal()) { ConnectKPiece winner; if (board.player == ConnectKPiece.P1) { winner = ConnectKPiece.P2; } else { winner = ConnectKPiece.P1; } Line winLine = board.GetWinningLine(); if (winLine != null) { DrawVictory(winLine, winner); } Debug.Log("Game over"); gameEnd = true; p1.TurnReadyEvent -= ReceiveTurn; p2.TurnReadyEvent -= ReceiveTurn; } else { PlayTurn(); } }
/// <summary> /// The clone constructor. Initiates a new <see cref="ConnectKBoard"/> using relevant information /// from <paramref name="oldBoard"/>. Copies a reference to the line list and deep copies the /// piece counts and dirty dictionaries. /// </summary> /// <param name="oldBoard">Old board.</param> public ConnectKBoard(ConnectKBoard oldBoard) { board = new ConnectKPiece[oldBoard.nCols, oldBoard.nRows]; for (int x = 0; x < nCols; x++) { for (int y = 0; y < nRows; y++) { board[x, y] = oldBoard.board[x, y]; } } player = oldBoard.player; k = oldBoard.k; allLines = oldBoard.allLines; lineDict = oldBoard.lineDict; p1Count = new Dictionary <Line, float>(); p2Count = new Dictionary <Line, float>(); dirty = new Dictionary <Line, bool>(); foreach (Line line in oldBoard.dirty.Keys) { p1Count.Add(line, oldBoard.p1Count[line]); p2Count.Add(line, oldBoard.p2Count[line]); dirty.Add(line, oldBoard.dirty[line]); } terminal = oldBoard.terminal; }
public override void GenerateNextTurn(IGameState state) { ConnectKBoard board = state as ConnectKBoard; this.board = board; ourTurn = player == board.player; }
public override void Init(IGameState state) { //intialise turn engines ConnectKBoard board = state as ConnectKBoard; engine = new TurnEngineSingleThreaded(new EvaluatorRandom(-1, 1), 2, false); engine.TurnReadyEvent += HandleTurnReadyEvent; }
public override void Init(IGameState state) { board = state as ConnectKBoard; Vector3 diff = Camera.main.WorldToScreenPoint(new Vector3(gridSize, 0)) - Camera.main.WorldToScreenPoint(Vector3.zero); buttonSize = Mathf.Abs(diff.x); ourTurn = player == board.player; }
public IGameState ApplyTurn(IGameState state) { ConnectKBoard board = state as ConnectKBoard; board = board.Clone() as ConnectKBoard; board.AddPiece(piece, column); if (board.player == ConnectKPiece.P1) { board.player = ConnectKPiece.P2; } else { board.player = ConnectKPiece.P1; } return(board); }
public override void Init(IGameState state) { //intialise turn engines ConnectKBoard board = state as ConnectKBoard; int depthLimit = board.nRows * board.nCols; //there can only ever be width*height possible moves so no need to search further if (multiThreaded) { engine = new TurnEngineMultiThreaded(new ConnectKEvaluator(player, board.nCols, board.nRows, board.k), timeLimit, depthLimit, true); } else { engine = new TurnEngineSingleThreaded(new ConnectKEvaluator(player, board.nCols, board.nRows, board.k), timeLimit, depthLimit, true); } //Register to pass the turn ready even up engine.TurnReadyEvent += HandleTurnReadyEvent; }
void StartNewGame() { if (p1AgentSelection == 0) { p1 = humanP1; } else if (p1AgentSelection == 1) { p1 = aiP1; } else if (p1AgentSelection == 2) { p1 = randomP1; } if (p2AgentSelection == 0) { p2 = humanP2; } else if (p2AgentSelection == 1) { p2 = aiP2; } else if (p2AgentSelection == 2) { p2 = randomP2; } preGame = false; //create our internal board representation board = new ConnectKBoard(width, height, matches); InitBoard(); lastPieces = new List <GameObject>(); //initialise our turn agents p1.Init(board); p2.Init(board); //register to receive and process turns p1.TurnReadyEvent += ReceiveTurn; p2.TurnReadyEvent += ReceiveTurn; //start the turn loop PlayTurn(); }