public GameBoard(GameBoard in_gameBoard) { this.width = in_gameBoard.width; this.height = in_gameBoard.height; gameBoard = in_gameBoard.getGameBoard(); resetBoard(); }
//Starts a new game of tic tac toe. //Creates/resets the gameboard and keeps track of scores. public bool nextRound() { bool donePlaying = false; //Setup Board if (gameBoard == null) gameBoard = new GameBoard(); else gameBoard.resetBoard(); //Only Human players atm for (int i = 0; i < numPlayers; i++) { playerList[i] = new HumanPlayer(gameBoard); } do { playGame(); Console.WriteLine("Play Another? (y/n)"); if (Console.ReadLine().Equals("y")) { donePlaying = false; gameBoard.resetBoard(); } else { donePlaying = true; } } while (!donePlaying); return donePlaying; }
public void WrapperConstructor() { board = new GameBoard(); label6.Text = label6.Text + board.GetTurn(); }
/** * Check to see if all three conditions are true indicating win. */ public bool checkWin(GameBoard gameBoard) { return(checkRowsForWin(gameBoard) || checkColumnsForWin(gameBoard) || checkDiagonalsForWin(gameBoard)); }
} //end GameBoardTests public bool humanPlayerTests() { bool result = true; bool doneTesting = false; GameBoard gameBoard = new GameBoard(); Player player1 = new HumanPlayer(gameBoard); while (!doneTesting) { player1.takeTurn(); gameBoard.printBoard(); Console.WriteLine("Continue testing Human input? (y/n)"); if (Console.ReadLine().Equals("n")) { doneTesting = true; } } return result; }
private static int Minimax(GameBoard board, int depth, bool isMaximizing, int row, int col, int parentBestScore, TicTacToeGameRules myGameRules) { int maxScore = 1; int minScore = -1; int score; // See move //board.DrawGameBoard(); //Console.ReadLine(); if (TicTacToeGameRules.CheckForWin(board, row, col, myGameRules.currentPlayer.myPiece)) { score = 1; return(score); } else if (TicTacToeGameRules.CheckForWin(board, row, col, myGameRules.opponent.myPiece)) { score = -1; return(score); } else if (TicTacToeGameRules.CheckForDraw(board) || depth <= 0) { score = 0; return(score); } if (isMaximizing) { int bestScore = minScore; for (int y = 0; y < board.rows; y++) { for (int x = 0; x < board.cols; x++) { if (board.GetGameBoardSpace(y, x) == " ") { board.SetGameBoardSpace(y, x, myGameRules.currentPlayer.myPiece); score = Minimax(board, depth - 1, false, y, x, minScore, myGameRules); board.SetGameBoardSpace(y, x, " "); if (score > bestScore) { bestScore = score; } if (bestScore >= parentBestScore) { return(bestScore); } } } } return(bestScore); } else { int bestScore = maxScore; for (int y = 0; y < board.rows; y++) { for (int x = 0; x < board.cols; x++) { if (board.GetGameBoardSpace(y, x) == " ") { board.SetGameBoardSpace(y, x, myGameRules.opponent.myPiece); score = Minimax(board, depth - 1, true, y, x, maxScore, myGameRules); board.SetGameBoardSpace(y, x, " "); if (score < bestScore) { bestScore = score; } if (bestScore <= parentBestScore) { return(bestScore); } } } } return(bestScore); } }
private void Awake() { Instance = this; }
//constructor public Player(GameBoard gameBoard) { this.gameBoard = gameBoard; setupNameAndPiece(); }
/// <summary> /// Contructor to test /// </summary> /// <param name="board"></param> /// <param name="inputManager"></param> /// <param name="output"></param> public GamePlayManager(GameBoard board, InputManager inputManager, OutputManager outputManager) { this.board = board; this.inputManager = inputManager; this.outputManager = outputManager; }
public HumanPlayer(GameBoard gameBoard) : base(gameBoard) { }
// the bulk of the desired functionality should be refactored into a // method that will be invoked once the class has been initialized. public void Invoke() { // declare local variables // this will used to determine which DAL method I need to call DatabaseAccessLayer _dal = new DatabaseAccessLayer(); List <Player> _listOfPlayers; // Title of game Console.WriteLine("Welcome to Tic-Tac-Toe"); // Creates a new GameBoard GameBoard _board = new GameBoard(); bool IsPlayersNotSelected = true; Player _firstPlayer = new Player(); Player _secondPlayer = new Player(); while (IsPlayersNotSelected) { // print out the main menu Console.WriteLine("Menu (L) - List all players, (S) Select players for the game, " + "(A) - Add Player, " + "(D) - Delete Player, (U) - Update Player"); string _menuItemPicked = Console.ReadLine(); // what does the user want to do switch (_menuItemPicked.ToUpper()) { case "A": // add user // collect all the information from the user Console.WriteLine("Enter first name (required): "); string _firstNameInput = Console.ReadLine(); Console.WriteLine("Enter last name (required): "); string _lastNameInput = Console.ReadLine(); Console.WriteLine("Enter birthdate MM/DD/YYYY (optional): "); string _birthdateInput = Console.ReadLine(); Console.WriteLine("Enter gender M or F (optional): "); string _genderInput = Console.ReadLine(); // constructor Player _newPlayer = new Player(_firstNameInput, _lastNameInput, _birthdateInput, _genderInput, PlayerType.Human); // pass all value thru the object reference _userReader.AddUser(_newPlayer); break; case "D": // delete a player from the db // print out the list for them of they player ids Console.WriteLine("Please enter player id you want to delete for the game."); int _getAllPlayers = 0; _listOfPlayers = _dal.GetAllPlayers(_getAllPlayers); // print out the board _board.PrintPlayers(_listOfPlayers); // the get the player id int _playerId = Convert.ToInt32(Console.ReadLine()); //delete the player and their stats _dal.DeletePlayer(_playerId); break; case "L": // list all the players in int _playerIDToGet = 0; _listOfPlayers = _dal.GetAllPlayers(_playerIDToGet); // add the bot player _listOfPlayers.Add(new Player { PlayerFirstName = "Bot", PlayerID = 666, PlayerLastName = "Boy", PlayerType = PlayerType.Bot }); // uses the list _board.PrintPlayers(_listOfPlayers); break; case "S": // print out the list for them of they player ids Console.WriteLine("Please look at the list of available players, enter two player ids for the game."); _getAllPlayers = 0; _listOfPlayers = _dal.GetAllPlayers(_getAllPlayers); _listOfPlayers.Add(new Player { PlayerFirstName = "Bot", PlayerID = 666, PlayerLastName = "Boy", PlayerType = PlayerType.Bot }); // print out the board _board.PrintPlayers(_listOfPlayers); // get the two players Console.WriteLine("Please enter PlayerID for first player."); int firstPlayerId = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Please enter PlayerID for second player."); int secondPlayerId = Convert.ToInt32(Console.ReadLine()); // write some LINQ to get player one and create object for that player Player _firstPlayerDAL = _listOfPlayers.Where(x => x.PlayerID == firstPlayerId).FirstOrDefault(); // write some LINQ to get player two and create object for that player Player _secondPlayerDAL = _listOfPlayers.Where(x => x.PlayerID == secondPlayerId).FirstOrDefault(); // map these to player objects because that's what game play takes //_firstPlayer = Mapper.PlayerDALtoPlayer(_firstPlayerDAL, 1, 'X'); //_secondPlayer = Mapper.PlayerDALtoPlayer(_secondPlayerDAL, 2, '0'); _firstPlayer = _firstPlayerDAL; _secondPlayer = _secondPlayerDAL; _firstPlayer.PlayerToken = 'X'; _firstPlayer.PlayerOrderByPlay = 1; _secondPlayer.PlayerToken = 'O'; _secondPlayer.PlayerOrderByPlay = 2; // drop out of loop IsPlayersNotSelected = false; break; default: break; } } // while end // continuing to game play ////Creates a new GameBoard //GameBoard board = new GameBoard(first, second); _board.SetPlayers(_firstPlayer, _secondPlayer); // print instructions to where your choice will go 1- 9 Console.WriteLine("Players will pick a number between 1 and 9 to determnine where they will play"); // prints out the rule board _board.Print(); // game play code starting here // need a loop to go back and forth between the players bool IsContinueGame = true; // will be switched to false on a draw or winner inside the loop //string currentPlayer = player1; // used to determine what will be stored in the array Player _currentPlayer; int _currentPlayerIndex = 0; string _position = ""; while (IsContinueGame) { _currentPlayer = _board.TwoPlayers[_currentPlayerIndex]; // TODO: only for the screen players // need player to pick a location if (_currentPlayer.PlayerType == PlayerType.Human) { Console.WriteLine(_currentPlayer.PlayerFirstName + " " + _currentPlayer.PlayerLastName + ", please pick a location 1 to 9 that is not already occupied"); Console.Write("Location: "); _position = Console.ReadLine(); // only humans to things wrong // need validation to make sure is legal spot // checks for valid player input _position = _board.ValidateInput(_position, _currentPlayer.PlayerFirstName + " " + _currentPlayer.PlayerLastName); bool validMove = false; // checks if the player made a valid move while (validMove == false) { validMove = _board.ValidateMove(Convert.ToInt32(_position)); if (validMove == false) { Console.WriteLine(_currentPlayer.PlayerFirstName + " " + _currentPlayer.PlayerLastName + ", please use only the numbers 1 to 9 and a space that is not already occupied"); _board.Print(); Console.Write("Location: "); _position = Console.ReadLine(); _position = _board.ValidateInput(_position, _currentPlayer.PlayerFirstName + " " + _currentPlayer.PlayerLastName); } } } else // BOT turn { // no validation needed // need the current board state to decide _position = _currentPlayer.BotMove(_board.BoardState); // print the location picked Console.WriteLine(_currentPlayer.PlayerFirstName + " " + _currentPlayer.PlayerLastName + ", please use only the numbers 1 to 9 and a space that is not already occupied"); } // place it // do we place a X or O? char charToPlace; charToPlace = _currentPlayer.PlayerToken; // this is simply placing the token, all the hard decisions have been made _board.Place(charToPlace, Convert.ToInt32(_position)); // print it _board.Print(); // check if it's a winner or draw, if not flip the player string result = _board.CheckForWinnerOrDraw(); switch (result) { case "win": IsContinueGame = false; Console.WriteLine(_currentPlayer.PlayerFirstName + " " + _currentPlayer.PlayerLastName + " wins!!!!!"); Console.WriteLine(); break; case "draw": IsContinueGame = false; Console.WriteLine("Game is a draw !!!!"); Console.WriteLine(); break; default: break; } // flip the player if (_currentPlayerIndex == 0) { _currentPlayerIndex = 1; } else { _currentPlayerIndex = 0; } // ENH: add win loss draw to the database // ENH: to they want another game ? } // game loop //Stops the program Console.WriteLine("Game Over"); Console.ReadLine(); }
public Game(int size) { board = new GameBoard(size); }
public MainPage() { DataContext = new GameBoard(); this.InitializeComponent(); }
public static bool CheckDiagonal2Line(GameBoard board, int placedRow, int placedCol, string piece, int toCheck, int lineLength) { int checkedSpots = 0; int filledByPiece = 0; // Check initial spot if (board.GetGameBoardSpace(placedRow, placedCol) == piece) { checkedSpots++; filledByPiece++; // Check spots down and to left int col = placedCol - 1; for (int row = placedRow + 1; row < board.rows; row++) { if (col >= 0) { if (board.GetGameBoardSpace(row, col) == piece) { checkedSpots++; filledByPiece++; col--; } else { break; } } else { break; } if (checkedSpots == toCheck) { break; } } // Check spots up and to right if (checkedSpots < toCheck) { col = placedCol + 1; for (int row = placedRow - 1; row >= 0; row--) { if (col < board.cols) { if (board.GetGameBoardSpace(row, col) == piece) { checkedSpots++; filledByPiece++; col++; } else { checkedSpots++; break; } } else { break; } if (checkedSpots == toCheck) { break; } } } } if (filledByPiece == lineLength && checkedSpots == toCheck) { return(true); } else { return(false); } }
int currentPlayer; //to know when you put X or O public Game() { principalBoard = new GameBoard(); this.player1 = new Player("O"); this.player2 = new Player("X"); }
// check win logic main entry static public bool checkWin(GameBoard gameBoard) { return(checkHorizontallyForWin(gameBoard) || checkVerticallyForWin(gameBoard) || checkDiagonallyForWin(gameBoard)); }
/// <summary> /// See if player has won /// </summary> /// <param name="player"> Current player</param> /// <returns></returns> public Boolean IsWin(IPlayer player, GameBoard gameBoard) { throw new NotImplementedException(); }
// check draw logic main entry static public bool checkDraw(int moveCounter, GameBoard gameBoard) { return(moveCounter == Math.Pow(gameBoard.Board_Size, 2) ? true : false); }
static void Main(string[] args) { Console.WriteLine("_______"); Console.WriteLine("|1|2|3|"); Console.WriteLine("_______"); Console.WriteLine("|4|5|6|"); Console.WriteLine("_______"); Console.WriteLine("|7|8|9|"); Console.WriteLine("_______"); Console.WriteLine("START GAME!!" + Environment.NewLine + Environment.NewLine); GameBoard g = new GameBoard(); Engine e = new Engine(); e.GameBoard = g; bool comFirst = true; string startGame = "start"; while (startGame == "start") { e.GameBoard.Init(); if (!comFirst) { Draw(e.GameBoard.Squares); Console.WriteLine("ENTER cell num"); var num = Convert.ToInt32(Console.ReadLine()); var xy = GetXy(num); e.GameBoard.SetMove(xy[0], xy[1], Cell.MIN); } while (true) { var move = e.FineBestNode(); e.GameBoard.SetMove(move[0], move[1], Cell.MAX); Draw(e.GameBoard.Squares); if (Check(e.CheckWinner())) { break; } Console.WriteLine("ENTER cell num"); var num = Convert.ToInt32(Console.ReadLine()); var xy = GetXy(num); e.GameBoard.SetMove(xy[0], xy[1], Cell.MIN); if (Check(e.CheckWinner())) { break; } } comFirst = !comFirst; Console.WriteLine("Restart GAME !!! (y/n)"); if (Console.ReadLine() == "n") { break; } } Console.WriteLine("Press any key to exit................................."); Console.ReadLine(); }
//Difficult mode checks, in-order, horizontal, vertical, and diagonal lines to see if there are two in a row of either 'X' or 'O'. It then places a piece to complete the line. //Does not differentiate between 'X' and 'O', and uses no real strategy. public bool Difficult(GameBoard ticTac, char ch) { //Checks matches in horizontal spaces for (int i = 0; i < 7; i += 3) { if (ticTac.GameSpaces[i] == ticTac.GameSpaces[i + 1] || ticTac.GameSpaces[i + 1] == ticTac.GameSpaces[i + 2] || ticTac.GameSpaces[i] == ticTac.GameSpaces[i + 2]) { for (int j = i; j < i + 3; j++) { if (char.IsDigit(ticTac.GameSpaces[j])) { PiecePlace.Computer(ticTac.GameSpaces, j, ch); return(false); } } } } //checks for matches in vertical spaces for (int i = 0; i < 3; i += 1) { if (ticTac.GameSpaces[i] == ticTac.GameSpaces[i + 3] || ticTac.GameSpaces[i + 3] == ticTac.GameSpaces[i + 6] || ticTac.GameSpaces[i] == ticTac.GameSpaces[i + 6]) { for (int j = i; j < i + 7; j += 3) { if (char.IsDigit(ticTac.GameSpaces[j])) { PiecePlace.Computer(ticTac.GameSpaces, j, ch); return(false); } } } } //checks for matches diagonally right if (ticTac.GameSpaces[0] == ticTac.GameSpaces[4] || ticTac.GameSpaces[4] == ticTac.GameSpaces[8] || ticTac.GameSpaces[0] == ticTac.GameSpaces[8]) { for (int i = 0; i < 9; i += 4) { if (char.IsDigit(ticTac.GameSpaces[i])) { PiecePlace.Computer(ticTac.GameSpaces, i, ch); return(false); } } } //checks for matches diagonally left if (ticTac.GameSpaces[2] == ticTac.GameSpaces[4] || ticTac.GameSpaces[4] == ticTac.GameSpaces[6] || ticTac.GameSpaces[2] == ticTac.GameSpaces[6]) { for (int i = 2; i < 7; i += 2) { if (char.IsDigit(ticTac.GameSpaces[i])) { PiecePlace.Computer(ticTac.GameSpaces, i, ch); return(false); } } } //if none of the above conditions are met, a random piece is placed Easy(ticTac, ch); return(false); }
private bool Check(int firstPos, int secondPos, int choice, BoardValue symbol) { if (GameBoard.GetBoardValue(firstPos).Equals(GameBoard.GetBoardValue(secondPos)) && GameBoard.GetBoardValue(firstPos).Equals(symbol)) { if (GameBoard.GetBoardValue(choice).Equals(BoardValue.Empty)) { return(true); } } return(false); }
public AIPlayer(GameBoard gameBoard) : base(gameBoard) { }
public void PlayGame() { bool changePlayerLoop; do { Console.Clear(); Console.WriteLine(" Tic-Tac-Toe\n"); changePlayerLoop = ChangePlayer(); } while (!changePlayerLoop); GameBoard myGameBoard = new GameBoard(3, 3, " ", true, "left", "number", "top", "letter"); GameBoard aiGameBoard = new GameBoard(3, 3, " ", true, "left", "number", "top", "letter"); //GameBoard myGameBoard = new GameBoard(4, 4, " ", true, "left", "number", "top", "letter"); //GameBoard aiGameBoard = new GameBoard(4, 4, " ", true, "left", "number", "top", "letter"); //GameBoard myGameBoard = new GameBoard(5, 5, " ", true, "left", "number", "top", "letter"); //GameBoard aiGameBoard = new GameBoard(5, 5, " ", true, "left", "number", "top", "letter"); #region /* * myGameBoard.SetGameBoardSpace(0, 0, "O"); * myGameBoard.SetGameBoardSpace(0, 1, " "); * myGameBoard.SetGameBoardSpace(0, 2, "X"); * * myGameBoard.SetGameBoardSpace(1, 0, "X"); * myGameBoard.SetGameBoardSpace(1, 1, "X"); * myGameBoard.SetGameBoardSpace(1, 2, "O"); * * myGameBoard.SetGameBoardSpace(2, 0, "O"); * myGameBoard.SetGameBoardSpace(2, 1, " "); * myGameBoard.SetGameBoardSpace(2, 2, " "); */ #endregion #region /* * myGameBoard.SetGameBoardSpace(0, 0, "#"); * myGameBoard.SetGameBoardSpace(0, 1, "#"); * myGameBoard.SetGameBoardSpace(0, 2, " "); * myGameBoard.SetGameBoardSpace(0, 3, "#"); * myGameBoard.SetGameBoardSpace(0, 4, "#"); * * myGameBoard.SetGameBoardSpace(1, 0, "#"); * myGameBoard.SetGameBoardSpace(1, 1, " "); * myGameBoard.SetGameBoardSpace(1, 2, " "); * myGameBoard.SetGameBoardSpace(1, 3, " "); * myGameBoard.SetGameBoardSpace(1, 4, "#"); * * myGameBoard.SetGameBoardSpace(2, 0, "#"); * myGameBoard.SetGameBoardSpace(2, 1, " "); * myGameBoard.SetGameBoardSpace(2, 2, " "); * myGameBoard.SetGameBoardSpace(2, 3, " "); * myGameBoard.SetGameBoardSpace(2, 4, "#"); * * myGameBoard.SetGameBoardSpace(3, 0, "#"); * myGameBoard.SetGameBoardSpace(3, 1, " "); * myGameBoard.SetGameBoardSpace(3, 2, " "); * myGameBoard.SetGameBoardSpace(3, 3, " "); * myGameBoard.SetGameBoardSpace(3, 4, "#"); * * myGameBoard.SetGameBoardSpace(4, 0, "#"); * myGameBoard.SetGameBoardSpace(4, 1, "#"); * myGameBoard.SetGameBoardSpace(4, 2, "#"); * myGameBoard.SetGameBoardSpace(4, 3, "#"); * myGameBoard.SetGameBoardSpace(4, 4, "#"); */ #endregion //TicTacToeGameRules myGameRules = new TicTacToeGameRules(myGameBoard, aiGameBoard, 5, 3, myPlayers); TicTacToeGameRules myGameRules = new TicTacToeGameRules(myGameBoard, aiGameBoard, 3, int.MaxValue, myPlayers); //TicTacToeGameRules myGameRules = new TicTacToeGameRules(myGameBoard, aiGameBoard, 5, 9, myPlayers); PlayAgain(); }
public bool gameBoardTests() { bool result = true; #region printBoardTests GameBoard gameBoard = new GameBoard(); gameBoard.printBoard(); GameBoard gameBoard2 = new GameBoard(5,5); gameBoard2.printBoard(); GameBoard gameBoard3 = new GameBoard(3, 5); gameBoard3.printBoard(); #endregion #region addPieceTests //AddPiece Test 1 - add a valid piece in valid position gameBoard.addPiece(0,0,'P'); if (gameBoard.getGameBoard()[0,0] != 'P') { result = false; Console.WriteLine("AddPiece Test 1 failed " + gameBoard.getGameBoard()[0,0]); } //AddPiece Test 2 - add a valid piece in an invalid position if (gameBoard.addPiece(4, 4, 'P')) { result = false; Console.WriteLine("AddPiece Test 2 failed"); } //AddPiece test 3 - verify adding on bigger boards works. if (!gameBoard2.addPiece(4,4,'5')) { result = false; Console.WriteLine("AddPiece Test 3 failed"); } #endregion #region resetBoardTests //ResetBoard test 1 - verify board is reset properly. gameBoard.resetBoard(); gameBoard.addPiece(1, 1,'X'); gameBoard.resetBoard(); if (gameBoard.getGameBoard()[1,1] == 'X') { result = false; Console.WriteLine("ResetBoard test 1 failed"); } #endregion #region CheckForWinnerTests //CheckForWinner Test 1 - top horizontal row winner gameBoard.resetBoard(); gameBoard.addPiece(0, 0, 'X'); gameBoard.addPiece(1, 0, 'X'); gameBoard.addPiece(2, 0, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 1 failed"); } //CheckForWinner Test 2 - top horizontal row no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 0, 'X'); gameBoard.addPiece(1, 0, 'O'); gameBoard.addPiece(2, 0, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 2 failed"); } //CheckForWinner Test 3 - middle horizontal row winner gameBoard.resetBoard(); gameBoard.addPiece(0, 1, 'X'); gameBoard.addPiece(1, 1, 'X'); gameBoard.addPiece(2, 1, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 3 failed"); } //CheckForWinner Test 4 - middle horizontal row no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 1, 'X'); gameBoard.addPiece(1, 1, 'O'); gameBoard.addPiece(2, 1, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 4 failed"); } //CheckForWinner Test 5 - bottom horizontal row winner gameBoard.resetBoard(); gameBoard.addPiece(0, 2, 'X'); gameBoard.addPiece(1, 2, 'X'); gameBoard.addPiece(2, 2, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 5 failed"); } //CheckForWinner Test 6 - bottom horizontal row no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 2, 'X'); gameBoard.addPiece(1, 2, 'O'); gameBoard.addPiece(2, 2, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 6 failed"); } //CheckForWinner Test 7 - left vertical column winner gameBoard.resetBoard(); gameBoard.addPiece(0, 0, 'X'); gameBoard.addPiece(0, 1, 'X'); gameBoard.addPiece(0, 2, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 7 failed"); } //CheckForWinner Test 8 - left vertical column no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 0, 'X'); gameBoard.addPiece(1, 0, 'O'); gameBoard.addPiece(2, 0, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 8 failed"); } //CheckForWinner Test 9 - middle vertical column winner gameBoard.resetBoard(); gameBoard.addPiece(1, 0, 'X'); gameBoard.addPiece(1, 1, 'X'); gameBoard.addPiece(1, 2, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 9 failed"); } //CheckForWinner Test 10 - middle vertical column no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 1, 'X'); gameBoard.addPiece(1, 1, 'O'); gameBoard.addPiece(2, 1, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 10 failed"); } //CheckForWinner Test 11 - right vertical column winner gameBoard.resetBoard(); gameBoard.addPiece(2, 0, 'X'); gameBoard.addPiece(2, 1, 'X'); gameBoard.addPiece(2, 2, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 11 failed"); } //CheckForWinner Test 12 - right vertical column no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 2, 'X'); gameBoard.addPiece(1, 2, 'O'); gameBoard.addPiece(2, 2, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 12 failed"); } //CheckForWinner Test 13 - diagonal top left to bottom right winner gameBoard.resetBoard(); gameBoard.addPiece(0, 0, 'X'); gameBoard.addPiece(1, 1, 'X'); gameBoard.addPiece(2, 2, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 13 failed"); } //CheckForWinner Test 14 - diagonal top left to bottom right no winner gameBoard.resetBoard(); gameBoard.addPiece(0, 0, 'X'); gameBoard.addPiece(1, 1, 'O'); gameBoard.addPiece(2, 2, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 14 failed"); } //CheckForWinner Test 15 - diagonal bottom left to top right winner gameBoard.resetBoard(); gameBoard.addPiece(2, 0, 'X'); gameBoard.addPiece(1, 1, 'X'); gameBoard.addPiece(0, 2, 'X'); if (!gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 15 failed"); } //CheckForWinner Test 16 - diagonal bottom left to top right no winner gameBoard.resetBoard(); gameBoard.addPiece(2, 0, 'X'); gameBoard.addPiece(1, 1, 'O'); gameBoard.addPiece(0, 2, 'X'); if (gameBoard.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 16 failed"); } //CheckForWinner Test 17 - middle vertical row winner non 3x3 gameBoard2.resetBoard(); gameBoard2.addPiece(0, 0, 'X'); gameBoard2.addPiece(0, 1, 'X'); gameBoard2.addPiece(0, 2, 'X'); gameBoard2.addPiece(0, 3, 'X'); gameBoard2.addPiece(0, 4, 'X'); if (!gameBoard2.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 17 failed"); } //CheckForWinner Test 18 - middle vertical row no winner non 3x3 gameBoard2.resetBoard(); gameBoard2.addPiece(0, 0, 'X'); gameBoard2.addPiece(0, 1, 'X'); gameBoard2.addPiece(0, 2, 'X'); gameBoard2.addPiece(0, 3, 'O'); gameBoard2.addPiece(0, 4, 'X'); if (gameBoard2.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 18 failed"); } //CheckForWinner Test 19 - diagonal top left to bottom right winner non 3x3 gameBoard2.resetBoard(); gameBoard2.addPiece(0, 0, 'X'); gameBoard2.addPiece(1, 1, 'X'); gameBoard2.addPiece(2, 2, 'X'); gameBoard2.addPiece(3, 3, 'X'); gameBoard2.addPiece(4, 4, 'X'); if (!gameBoard2.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 19 failed"); } //CheckForWinner Test 20 - diagonal top left to bottom right no winner non 3x3 gameBoard2.resetBoard(); gameBoard2.addPiece(0, 0, 'X'); gameBoard2.addPiece(1, 1, 'X'); gameBoard2.addPiece(2, 2, 'O'); gameBoard2.addPiece(3, 3, 'X'); gameBoard2.addPiece(4, 4, 'X'); if (gameBoard2.checkForWinner()) { result = false; Console.WriteLine("CheckForWinner Test 20 failed"); } #endregion return result; } //end GameBoardTests
/** * Check the two diagonals to see if either is a win. Return true if either wins. * */ private bool checkDiagonalsForWin(GameBoard gameBoard) { return((checkRowCol(gameBoard.Board[0, 0].getFieldState(), gameBoard.Board[1, 1].getFieldState(), gameBoard.Board[2, 2].getFieldState()) == true) || (checkRowCol(gameBoard.Board[0, 2].getFieldState(), gameBoard.Board[1, 1].getFieldState(), gameBoard.Board[2, 0].getFieldState()) == true)); }