private void concludeSingleGame(eGameStatus i_GameStatus, ePlayerPosition i_Winner) { r_UI.ClearScreen(); r_UI.PrintBoard(m_Board.BoardMatrix); int player1Points = m_Board.GetPlayerScore(r_Player1.PlayerPosition); int player2Points = m_Board.GetPlayerScore(r_Player2.PlayerPosition); switch (i_GameStatus) { case eGameStatus.Draw: r_UI.Draw(); break; case eGameStatus.Win: string winner = (r_Player1.PlayerPosition == i_Winner) ? r_Player1.Name : r_Player2.Name; r_UI.Winning(winner); break; case eGameStatus.Forfit: string forfiter = (r_Player1.PlayerPosition == i_Winner) ? r_Player2.Name : r_Player1.Name; r_UI.PlayerForfited(forfiter); break; } r_Player1.Points += player1Points; r_Player2.Points += player2Points; r_UI.PlayerRecivedPoints(r_Player1.Name, player1Points); r_UI.PlayerRecivedPoints(r_Player2.Name, player2Points); r_UI.PointStatus(r_Player1.Name, r_Player1.Points, r_Player2.Name, r_Player2.Points); }
public Move(Position i_Begin, Position i_End, ePlayerPosition i_Player, eMoveType i_MoveType) { // New move constructor r_Begin = i_Begin; r_End = i_End; m_Player = i_Player; m_Type = i_MoveType; }
public Move GetRandomMove(ePlayerPosition i_Player, Move i_PreviousMove) { List <Move> listOfMoves = GetPossibleMoves(i_Player, i_PreviousMove); Random random = new Random(); int randomPosition = random.Next(listOfMoves.Count); return(listOfMoves[randomPosition]); }
public Move(Position i_Begin, Position i_End, ePlayerPosition i_Player) { // New move constructor r_Begin = i_Begin; r_End = i_End; m_Player = i_Player; m_Type = null; }
private List <Move> possibleMovesForPieceDown(Position i_StartPosition, ePlayerPosition i_Player) { Position[] endPositions = { new Position(i_StartPosition.Row + 1, i_StartPosition.Col + 1), new Position(i_StartPosition.Row + 1, i_StartPosition.Col - 1) }; return(checkMove(i_StartPosition, endPositions, i_Player)); }
private void changePoints(ePlayerPosition i_Player, int i_NumOfPoints) { if (i_Player == ePlayerPosition.BottomPlayer) { m_BottomPlayerPoints += i_NumOfPoints; } else { m_TopPlayerPoints += i_NumOfPoints; } }
public eGameStatus GetGameStatus(Player i_CurrentPlayer, out ePlayerPosition o_Winner) { // Check the current game status // Win -> The current player has no possible moves, the other player wins // The current player has no pieces left, the other player wins // The current player has forfited and has fewer points, the other player wins // Draw -> Both players have no possible moves // A player has forfited when the scoring is a tie eGameStatus currentStatus = eGameStatus.Playing; ePlayerPosition winner = ePlayerPosition.TopPlayer; if (m_PlayerHasForfit) { ePlayerPosition otherPlayer = (m_PlayerForfit == ePlayerPosition.TopPlayer) ? ePlayerPosition.BottomPlayer : ePlayerPosition.TopPlayer; if (GetPlayerScore(m_PlayerForfit) == GetPlayerScore(otherPlayer)) { currentStatus = eGameStatus.Draw; } else { currentStatus = eGameStatus.Forfit; winner = otherPlayer; } } else { ePlayerPosition currentPlayer = i_CurrentPlayer.PlayerPosition; ePlayerPosition otherPlayer = currentPlayer == ePlayerPosition.BottomPlayer ? ePlayerPosition.TopPlayer : ePlayerPosition.BottomPlayer; int currentPlayerPossibleMoves = GetPossibleMoves(currentPlayer).Count; int otherPlayerPossibleMoves = GetPossibleMoves(otherPlayer).Count; // In case one of the players has no move, the game is either a draw or a win if (currentPlayerPossibleMoves == 0 || otherPlayerPossibleMoves == 0) { currentStatus = eGameStatus.Draw; if (otherPlayerPossibleMoves != 0) { currentStatus = eGameStatus.Win; winner = otherPlayer; } } } o_Winner = winner; return(currentStatus); }
private void changePoints(ePlayerPosition i_Player, int i_NumOfPoints) { if (i_Player == ePlayerPosition.BottomPlayer) { m_BottomPlayerPoints += i_NumOfPoints; } else { m_TopPlayerPoints += i_NumOfPoints; } LabelPointsListener?.Invoke(); }
public List <Move> GetPossibleMoves(ePlayerPosition i_CurrentPlayer, Move i_LastMove) { List <Move> possibleMoves = new List <Move>(); bool multipleJumpsPossible = false; // If the last move was a jump, first check if another jump is possible for that piece if (i_LastMove != null && i_LastMove.Type == Move.eMoveType.Jump) { possibleMoves = PossibleMovesForPiece(i_LastMove.End); if (possibleMoves != null) { possibleMoves.RemoveAll(notJump); if (possibleMoves.Count > 0) { multipleJumpsPossible = true; } } } if (!multipleJumpsPossible) { // Calculate all possible moves for a player for (int i = 0; i < r_Size; i++) { for (int j = 0; j < r_Size; j++) { // If the piece belongs to the current player, check the possible moves for it if (r_BoardMatrix[i, j] != null && r_BoardMatrix[i, j].PlayerPosition == i_CurrentPlayer) { possibleMoves?.AddRange(PossibleMovesForPiece(new Position(i, j))); } } } if (isJumpPossible(possibleMoves, out List <Move> onlyJumps)) { possibleMoves = onlyJumps; } } if (MoveStartListener != null) { MoveStartListener(possibleMoves); } return(possibleMoves); }
public void PlayerForfit(Player i_PlayerForfit, out eMoveStatus i_CurrentMoveStatus) { int forfitPlayerPoints = i_PlayerForfit.PlayerPosition == ePlayerPosition.BottomPlayer ? m_BottomPlayerPoints : m_TopPlayerPoints; int otherPlayerPoints = i_PlayerForfit.PlayerPosition == ePlayerPosition.BottomPlayer ? m_TopPlayerPoints : m_BottomPlayerPoints; if (forfitPlayerPoints <= otherPlayerPoints) { i_CurrentMoveStatus = eMoveStatus.Legal; m_PlayerHasForfit = true; m_PlayerForfit = i_PlayerForfit.PlayerPosition; } else { i_CurrentMoveStatus = eMoveStatus.Illegal; } }
private void concludeSingleGame(eGameStatus i_GameStatus, ePlayerPosition i_Winner) { int player1Points = m_Board.GetPlayerScore(r_Player1.PlayerPosition); int player2Points = m_Board.GetPlayerScore(r_Player2.PlayerPosition); r_Player1.Points += player1Points; r_Player2.Points += player2Points; StringBuilder endGame = new StringBuilder(); switch (i_GameStatus) { case eGameStatus.Draw: endGame.Append("Game has ended in a draw!"); break; case eGameStatus.Win: string winner = (r_Player1.PlayerPosition == i_Winner) ? r_Player1.Name : r_Player2.Name; endGame.Append(string.Format("{0} has won!", winner)); break; } endGame.Append(Environment.NewLine); endGame.Append("Total number of points so far - "); endGame.Append(Environment.NewLine); endGame.Append(string.Format("{0} : {1} --- {2} : {3}", r_Player1.Name, r_Player1.Points, r_Player2.Name, r_Player2.Points)); endGame.Append(Environment.NewLine); endGame.Append("Player another game?"); if (MessageBox.Show(endGame.ToString(), "Game Over", MessageBoxButtons.YesNo) == DialogResult.Yes) { Debug.WriteLine("Player another game"); playAnotherGame(); } else { // Close the app Debug.Write("Closing the game"); closeGame(); } }
private eSquareStatus checkSquareStatus(Position i_Square, out ePlayerPosition o_Player) { eSquareStatus squareStatus; o_Player = ePlayerPosition.BottomPlayer; if (i_Square.Row >= r_Size || i_Square.Row < 0 || i_Square.Col >= r_Size || i_Square.Col < 0) { squareStatus = eSquareStatus.OutOfBounds; } else if (m_BoardMatrix[i_Square.Row, i_Square.Col] == null) { squareStatus = eSquareStatus.Empty; } else { squareStatus = eSquareStatus.Occupied; o_Player = m_BoardMatrix[i_Square.Row, i_Square.Col].PlayerPosition; } return(squareStatus); }
private void playSingleGame() { // Initialize a new game - new board, players history, game status and starting player r_UI.ClearScreen(); m_Board = new Board(m_BoardSize); eGameStatus gameStatus = eGameStatus.Playing; ePlayerPosition winner = ePlayerPosition.BottomPlayer; r_Player1.ClearMoveHistory(); r_Player2.ClearMoveHistory(); m_CurrentPlayer = r_Player1; Move previousMove = null; while (gameStatus == eGameStatus.Playing) { r_UI.ClearScreen(); r_UI.PrintBoard(m_Board.BoardMatrix); r_UI.PrintLastMove(otherPlayer()); // Get a players move and preform it Move currentMove = getMove(previousMove, out eMoveStatus currentMoveStatus); m_CurrentPlayer.AddMove(currentMove); // If the player can not preform another jump, change player if (currentMoveStatus == eMoveStatus.AnotherJumpPossible) { previousMove = m_CurrentPlayer.GetLastMove(); } else { changeActivePlayer(); previousMove = null; } gameStatus = m_Board.GetGameStatus(m_CurrentPlayer, out winner); } concludeSingleGame(gameStatus, winner); }
private List <Move> possibleMovesForPiece(Position i_PiecePosition) { Piece currentPiece = m_BoardMatrix[i_PiecePosition.Row, i_PiecePosition.Col]; List <Move> possibleMovesForPiece = new List <Move>(); if (currentPiece != null) { ePlayerPosition player = currentPiece.PlayerPosition; // If the piece is a king, check moves in all directions if (currentPiece.Type == ePieceType.King) { possibleMovesForPiece.AddRange(possibleMovesForPieceUp(i_PiecePosition, player)); possibleMovesForPiece.AddRange(possibleMovesForPieceDown(i_PiecePosition, player)); } else { // If the piece is a regular, check moves according to the player switch (currentPiece.PlayerPosition) { case ePlayerPosition.TopPlayer: possibleMovesForPiece.AddRange(possibleMovesForPieceDown(i_PiecePosition, player)); break; case ePlayerPosition.BottomPlayer: possibleMovesForPiece.AddRange(possibleMovesForPieceUp(i_PiecePosition, player)); break; default: throw new ArgumentOutOfRangeException(); } } } return(possibleMovesForPiece); }
public int GetPlayerScore(ePlayerPosition i_Player) { return(i_Player == ePlayerPosition.BottomPlayer ? m_BottomPlayerPoints : m_TopPlayerPoints); }
private List <Move> checkMove(Position i_StartPosition, Position[] i_EndPositions, ePlayerPosition i_Player) { List <Move> regularMovesList = new List <Move>(); foreach (Position endPosition in i_EndPositions) { eSquareStatus squareStatus = checkSquareStatus(endPosition, out ePlayerPosition squarePlayer); if (squareStatus == eSquareStatus.Empty) { regularMovesList.Add(new Move(i_StartPosition, endPosition, i_Player, eMoveType.Regular)); } else if (squareStatus == eSquareStatus.Occupied && squarePlayer != i_Player) { int jumpRow = i_StartPosition.Row + (2 * (endPosition.Row - i_StartPosition.Row)); int jumpCol = i_StartPosition.Col + (2 * (endPosition.Col - i_StartPosition.Col)); Position jumpPosition = new Position(jumpRow, jumpCol); eSquareStatus jumpSquareStatus = checkSquareStatus(jumpPosition, out squarePlayer); if (jumpSquareStatus == eSquareStatus.Empty) { regularMovesList.Add(new Move(i_StartPosition, jumpPosition, i_Player, eMoveType.Jump)); } } else if (squareStatus == eSquareStatus.OutOfBounds) { } } return(regularMovesList); }
public Piece(ePlayerPosition i_PlayerPosition) { r_PlayerPosition = i_PlayerPosition; m_Type = ePieceType.Regular; }
public List <Move> GetPossibleMoves(ePlayerPosition i_CurrentPlayer) { return(GetPossibleMoves(i_CurrentPlayer, null)); }
public Player(ePlayerPosition i_PlayerPosition) { r_PlayerPosition = i_PlayerPosition; m_MoveHistory = new List <Move>(); }