// //This function is used to claim a move (by playerID) on the actual game board // static public void BoardClicked(int clickedRow, int clickedColumn, bool isOccupied, PLAYERS_ID playerID) ////////////////////////////////////////////////////////////////////////////////////////////////////////// { if (!gameFinished && clickedColumn >= 0 && clickedColumn < MAX_COLUMNS && clickedRow >= 0 && clickedRow < MAX_ROWS) { if (!isOccupied) //No stealing pieces { Color colourPiece; if (playerID == PLAYERS_ID.PLAYER_TWO) { colourPiece = new Color(0, 0, 255); //Blue } else { colourPiece = new Color(255, 0, 0); //Red } gameBoard[clickedRow, clickedColumn].AddPlayerToSquare(playerID, clickedRow, clickedColumn); gameBoard[clickedRow, clickedColumn].SetColor(colourPiece); if (CheckForWinner(gameBoard[clickedRow, clickedColumn])) { print("Player " + playerID + " has won!!!!"); winningPlayer = playerID; gameFinished = true; } else if (CheckForTie()) { winningPlayer = PLAYERS_ID.PLAYER_NONE; print("GAME TIED!!!!"); gameFinished = true; } } } }
static public PLAYERS_ID GetNextPlayerID(PLAYERS_ID currPlayer) { PLAYERS_ID nextPlayer = GameBoard.PLAYERS_ID.PLAYER_NONE; //Update the current player to the next player if (currPlayer == GameBoard.PLAYERS_ID.PLAYER_ONE) { nextPlayer = GameBoard.PLAYERS_ID.PLAYER_TWO; } else if (currPlayer == GameBoard.PLAYERS_ID.PLAYER_TWO) { nextPlayer = GameBoard.PLAYERS_ID.PLAYER_ONE; } return(nextPlayer); }
//Initialize the actual game board void InitGameBoard(bool init) ////////////////////////////////////////////////////////////////////////////////////////////////////////// { //playerTurn = 0; gameFinished = false; winningPlayer = PLAYERS_ID.PLAYER_NONE; for (int i = 0; i < MAX_ROWS; i++) { for (int j = 0; j < MAX_COLUMNS; j++) { if (init) { GameObject gamePieceObj = (GameObject)Instantiate(gamePiecePrefab); //calls the awake function, then returns gameBoard[i, j] = gamePieceObj.GetComponent <GamePiece>(); } gameBoard[i, j].InitPlayerSquare(i, j); } } }
static bool CheckPiecesBelowLeft(ref GamePiece playerGamePiece, ref int foundMatchCount, PLAYERS_ID playerTurnID) { bool bFoundWin = false; if (playerGamePiece.PlayerID == playerTurnID) { foundMatchCount++; if (foundMatchCount == CONNECT_NUM) { bFoundWin = true; } else if (playerGamePiece.Row < (MAX_ROWS - 1) && playerGamePiece.Column > 0 && foundMatchCount < CONNECT_NUM) { bFoundWin = CheckPiecesBelowLeft(ref gameBoard[playerGamePiece.Row + 1, playerGamePiece.Column - 1], ref foundMatchCount, playerTurnID); } if (bFoundWin) { playerGamePiece.SetColor(new Color(0, 255, 0)); } } return(bFoundWin); }
static bool CheckPiecesAboveLeft(ref GamePiece playerGamePiece, ref int foundMatchCount, PLAYERS_ID playerTurnID) { bool bFoundWin = false; if (playerGamePiece.PlayerID == playerTurnID) { foundMatchCount++; if (foundMatchCount == CONNECT_NUM) { bFoundWin = true; } else if (playerGamePiece.Row > 0 && playerGamePiece.Column > 0 && foundMatchCount < CONNECT_NUM) { //if haven't found a match check the next squre above bFoundWin = CheckPiecesAboveLeft(ref gameBoard[playerGamePiece.Row - 1, playerGamePiece.Column - 1], ref foundMatchCount, playerTurnID); } if (bFoundWin) { playerGamePiece.SetColor(new Color(0, 255, 0)); } } return(bFoundWin); }
//checks the actual game board for a winner static bool CheckForWinner(GamePiece playerGamePiece) { bool bWon = false; PLAYERS_ID playerTurnID = playerGamePiece.PlayerID; for (int i = 0; i < MAX_ROWS; i++) { for (int j = 0; j < MAX_COLUMNS; j++) { GamePiece gamePiece = gameBoard[i, j]; if (playerTurnID == gamePiece.PlayerID) { int foundMatchCount = 0; //Check above, if (CheckPiecesAbove(ref gamePiece, ref foundMatchCount, playerTurnID)) { bWon = true; //found a winner break; } foundMatchCount = 0; //Check below, if (CheckPiecesBelow(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } foundMatchCount = 0; //Check right, if (CheckPiecesRight(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } foundMatchCount = 0; //Check Left, if (CheckPiecesLeft(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } foundMatchCount = 0; //check upper right if (CheckPiecesAboveRight(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } foundMatchCount = 0; //check upper left if (CheckPiecesAboveLeft(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } foundMatchCount = 0; //check lower right if (CheckPiecesBelowRight(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } foundMatchCount = 0; //check lower left if (CheckPiecesBelowLeft(ref gamePiece, ref foundMatchCount, playerTurnID)) { //found a winner bWon = true; break; } } } if (bWon) { break; } } return(bWon); }