Question
Question: Using the supplied C# solution, implement three unit tests called isThereAWinnerCat, isThereAWinnerPlayer, and isThereAWinnerSystem. These tests must test the isThereAWinner method of the Board
Question:
Using the supplied C# solution, implement three unit tests called isThereAWinnerCat, isThereAWinnerPlayer, and isThereAWinnerSystem. These tests must test the isThereAWinner method of the Board class in the provided TicTacToe solution found in TicTacToe.zip. The test do not need to be comprehensive. For example, there are many ways to win, just test one winning combination for each "player" (Cat, Player, System).
Unit Test:
//Arrange
//Act
//Assert
/////////////Code//////////////////
using System;
namespace TicTacToeGame
{
public class Board
{
public const int MAX_ROWS = 3;
public const int MAX_COLS = 3;
public Square[,] board = new Square[MAX_ROWS, MAX_COLS];
private GameBrain gameBrian;
public Board()
{
gameBrian = new GameBrain();
for (int row = 0; row < MAX_ROWS; row++)
{
for (int col = 0; col < MAX_COLS; col++)
{
board[row, col] = new Square(row, col);
board[row, col].occupiedBy = "";
}
}
}
public bool playerMakesMove(int row, int col)
{
bool isSuccesful = false;
if (board[row, col].occupiedBy == "")
{
board[row, col].occupiedBy = Square.PLAYER_MARKER;
isSuccesful = true;
}
return isSuccesful;
}
public bool systemMakesMove()
{
return gameBrian.systemMove(this);
}
public String isThereAWinner()
{
String winner = "";
winner = calculateWinner();
return winner;
}
private String calculateWinner()
{
// Empty string means nobody has won
String winner = "";
if (winnerCheck(Square.PLAYER_MARKER))
winner = Square.PLAYER_MARKER;
else if (winnerCheck(Square.SYSETM_MARKER))
winner = Square.SYSETM_MARKER;
else if (allTaken())
winner = Square.CAT_MARKER;
return winner;
}
private bool allTaken()
{
bool isAllTaken = true;
for (int row = 0; row < MAX_ROWS; row++)
{
for (int col = 0; col < MAX_COLS; col++)
{
if (board[row, col].occupiedBy == "")
isAllTaken = false;
}
}
return isAllTaken;
}
private bool winnerCheck(String marker)
{
bool isWinner = false;
if (board[0,0].occupiedBy == marker
&& board[0,1].occupiedBy == marker
&& board[0,2].occupiedBy == marker)
{
// 1st row is all of a kind
isWinner = true;
} else if (board[1, 0].occupiedBy == marker
&& board[1, 1].occupiedBy == marker
&& board[1, 2].occupiedBy == marker)
{
// 2nd row is all of a kind
isWinner = true;
} else if (board[2, 0].occupiedBy == marker
&& board[2, 1].occupiedBy == marker
&& board[2, 2].occupiedBy == marker)
{
// 3rd row is all of a kind
isWinner = true;
} else if (board[0, 0].occupiedBy == marker
&& board[1, 0].occupiedBy == marker
&& board[2, 0].occupiedBy == marker)
{
// 1st column is all of a kind
isWinner = true;
}
else if (board[0, 1].occupiedBy == marker
&& board[1, 1].occupiedBy == marker
&& board[2, 1].occupiedBy == marker)
{
// 2nd column is all of a kind
isWinner = true;
} else if (board[0, 2].occupiedBy == marker
&& board[1, 2].occupiedBy == marker
&& board[2, 2].occupiedBy == marker)
{
// 3rd column is all of a kind
isWinner = true;
} else if (board[0, 0].occupiedBy == marker
&& board[1, 1].occupiedBy == marker
&& board[2, 2].occupiedBy == marker)
{
// 1st diagonal is all of a kind
isWinner = true;
} else if (board[0, 2].occupiedBy == marker
&& board[1, 1].occupiedBy == marker
&& board[2, 0].occupiedBy == marker)
{
// 2nd diagonal is all of a kind
isWinner = true;
}
return isWinner;
}
public String[,] getBoard()
{
String[,] pseudoBoard = new String[MAX_ROWS, MAX_COLS];
for (int row = 0; row < MAX_ROWS; row++)
{
for (int col = 0; col < MAX_COLS; col++)
{
pseudoBoard[row, col] = board[row, col].occupiedBy;
}
}
return pseudoBoard;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
namespace TicTacToeGame {
public class GameBrain { const string IS_OPEN = ""; private Board theBoard;
public bool systemMove(Board theBoard) { this.theBoard = theBoard; return smartMove(); }
private bool smartMove() { bool isSuccessful = false; Move move;
move = twoInARowOpen(); if (!isSuccessful && move.isOpen) { // Complete two in a row if open theBoard.board[move.row, move.col].occupiedBy = Square.SYSETM_MARKER; isSuccessful = true; }
move = blockThreeInARow(); if (!isSuccessful && move.isOpen) { // Complete two in a row if open theBoard.board[move.row, move.col].occupiedBy = Square.SYSETM_MARKER; isSuccessful = true; }
move = middleOpen(); if (!isSuccessful && move.isOpen) { // Take the middle if open theBoard.board[move.row, move.col].occupiedBy = Square.SYSETM_MARKER; isSuccessful = true; }
move = cornerOpen(); if (!isSuccessful && move.isOpen) { // Take a corner if open theBoard.board[move.row, move.col].occupiedBy = Square.SYSETM_MARKER; isSuccessful = true; }
move = anyOpen(); if (!isSuccessful && move.isOpen) { // Take any open square theBoard.board[move.row, move.col].occupiedBy = Square.SYSETM_MARKER; isSuccessful = true; }
return isSuccessful; }
private Move twoInARowOpen() { return twoInARow(Square.SYSETM_MARKER); }
private Move blockThreeInARow() { return twoInARow(Square.PLAYER_MARKER); }
private Move twoInARow(string marker) { Move move = new Move(); // 1st row checks if (theBoard.board[0, 0].occupiedBy == marker && theBoard.board[0, 1].occupiedBy == marker && theBoard.board[0, 2].occupiedBy == IS_OPEN) { move.row = 0; move.col = 2; move.isOpen = true; } else if (theBoard.board[0, 0].occupiedBy == marker && theBoard.board[0, 2].occupiedBy == marker && theBoard.board[0, 1].occupiedBy == IS_OPEN) { move.row = 0; move.col = 1; move.isOpen = true; } else if (theBoard.board[0, 1].occupiedBy == marker && theBoard.board[0, 2].occupiedBy == marker && theBoard.board[0, 0].occupiedBy == IS_OPEN) { move.row = 0; move.col = 0; move.isOpen = true; } // 2nd row checks else if (theBoard.board[1, 0].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == marker && theBoard.board[1, 2].occupiedBy == IS_OPEN) { move.row = 1; move.col = 2; move.isOpen = true; } else if (theBoard.board[1, 0].occupiedBy == marker && theBoard.board[1, 2].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == IS_OPEN) { move.row = 1; move.col = 1; move.isOpen = true; } else if (theBoard.board[1, 1].occupiedBy == marker && theBoard.board[1, 2].occupiedBy == marker && theBoard.board[1, 0].occupiedBy == IS_OPEN) { move.row = 1; move.col = 0; move.isOpen = true; } // 3rd row checks else if (theBoard.board[2, 0].occupiedBy == marker && theBoard.board[2, 1].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == IS_OPEN) { move.row = 2; move.col = 2; move.isOpen = true; } else if (theBoard.board[2, 0].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == marker && theBoard.board[2, 1].occupiedBy == IS_OPEN) { move.row = 2; move.col = 1; move.isOpen = true; } else if (theBoard.board[2, 1].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == IS_OPEN) { move.row = 2; move.col = 0; move.isOpen = true; } // 1st col checks else if (theBoard.board[0, 0].occupiedBy == marker && theBoard.board[1, 0].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == IS_OPEN) { move.row = 2; move.col = 0; move.isOpen = true; } else if (theBoard.board[0, 0].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == marker && theBoard.board[1, 0].occupiedBy == IS_OPEN) { move.row = 1; move.col = 0; move.isOpen = true; } else if (theBoard.board[1, 0].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == marker && theBoard.board[0, 0].occupiedBy == IS_OPEN) { move.row = 0; move.col = 0; move.isOpen = true; } // 2nd col checks else if (theBoard.board[0, 1].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == marker && theBoard.board[2, 1].occupiedBy == IS_OPEN) { move.row = 2; move.col = 1; move.isOpen = true; } else if (theBoard.board[0, 1].occupiedBy == marker && theBoard.board[2, 1].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == IS_OPEN) { move.row = 1; move.col = 1; move.isOpen = true; } else if (theBoard.board[1, 1].occupiedBy == marker && theBoard.board[2, 1].occupiedBy == marker && theBoard.board[0, 1].occupiedBy == IS_OPEN) { move.row = 0; move.col = 1; move.isOpen = true; } // 3rd col checks else if (theBoard.board[0, 2].occupiedBy == marker && theBoard.board[1, 2].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == IS_OPEN) { move.row = 2; move.col = 2; move.isOpen = true; } else if (theBoard.board[0, 2].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == marker && theBoard.board[1, 2].occupiedBy == IS_OPEN) { move.row = 1; move.col = 2; move.isOpen = true; } else if (theBoard.board[1, 2].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == marker && theBoard.board[0, 2].occupiedBy == IS_OPEN) { move.row = 0; move.col = 2; move.isOpen = true; } // 1st diag checks else if (theBoard.board[0, 0].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == IS_OPEN) { move.row = 2; move.col = 2; move.isOpen = true; } else if (theBoard.board[0, 0].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == IS_OPEN) { move.row = 1; move.col = 1; move.isOpen = true; } else if (theBoard.board[1, 1].occupiedBy == marker && theBoard.board[2, 2].occupiedBy == marker && theBoard.board[0, 0].occupiedBy == IS_OPEN) { move.row = 0; move.col = 0; move.isOpen = true; } // 2nd diag checks else if (theBoard.board[0, 2].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == IS_OPEN) { move.row = 2; move.col = 0; move.isOpen = true; } else if (theBoard.board[0, 2].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == marker && theBoard.board[1, 1].occupiedBy == IS_OPEN) { move.row = 1; move.col = 1; move.isOpen = true; } else if (theBoard.board[1, 1].occupiedBy == marker && theBoard.board[2, 0].occupiedBy == marker && theBoard.board[0, 2].occupiedBy == IS_OPEN) { move.row = 0; move.col = 2; move.isOpen = true; } else { move.isOpen = false; } return move; }
private Move middleOpen() { Move move = new Move(); if (theBoard.board[1,1].occupiedBy == IS_OPEN) { move.row = 1; move.col = 1; move.isOpen = true; } else { move.isOpen = false; } return move; }
private Move cornerOpen() { Move move = new Move(); if (theBoard.board[0,0].occupiedBy == IS_OPEN) { move.row = 0; move.col = 0; move.isOpen = true; } else if (theBoard.board[0,2].occupiedBy == IS_OPEN) { move.row = 0; move.row = 2; move.isOpen = true; } else if (theBoard.board[2,0].occupiedBy == IS_OPEN) { move.row = 2; move.col = 0; move.isOpen = true; } else if (theBoard.board[2,2].occupiedBy == IS_OPEN) { move.row = 2; move.col = 2; move.isOpen = true; } else { move.isOpen = false; } return move; }
private Move anyOpen() { Move move = new Move(); move.isOpen = false; for (int row = 0; row < Board.MAX_ROWS; row++) { for (int col = 0; col < Board.MAX_COLS; col++) { if (theBoard.board[row, col].occupiedBy == IS_OPEN) { move.row = row; move.col = col; move.isOpen = true; break; } } if (move.isOpen) break; } return move; } private bool simpleMove() { bool isSuccessful = false;
for (int row = 0; row < Board.MAX_ROWS; row++) { for (int col = 0; col < Board.MAX_COLS; col++) { if (theBoard.board[row, col].occupiedBy == "") { theBoard.board[row, col].occupiedBy = Square.SYSETM_MARKER; isSuccessful = true; return isSuccessful; } } } return isSuccessful; } } }
------------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
namespace TicTacToeGame { public class Square { public const String PLAYER_MARKER = "X"; public const String SYSETM_MARKER = "O"; public const String CAT_MARKER = "Cat";
public String occupiedBy { get; set; } public int row { get; set; } public int col { get; set; }
public Square(int row, int col) { this.row = row; this.col = col; this.occupiedBy = ""; } public bool palyerOccupy() { bool isSuccessful = false; if (this.occupiedBy == "") { this.occupiedBy = PLAYER_MARKER; isSuccessful = true; } return isSuccessful; }
public bool systemOccupy() { bool isSuccessful = false; if (this.occupiedBy == "") { this.occupiedBy = SYSETM_MARKER; isSuccessful = true; } return isSuccessful; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started