public enum Piece { empty, white, black }
public static void SelectGameType()
Console.WriteLine("What game would you like to play?");
Console.WriteLine("Othello/Gomoku");
bool gameSelectStatus = false;
while (!gameSelectStatus)
gameTypeSelect = Console.ReadLine();
if (gameTypeSelect == "Othello")
Program.currentGame = new Othello();
else if (gameTypeSelect == "Gomoku")
Console.WriteLine("Sorry, we have been developping this game. Othello please!");
Console.WriteLine("Invalid code. Please type again");
public static bool GamePlayerSelect()
bool gameModeStatus = false;
Console.WriteLine("Which game mode would you like to play?");
Console.WriteLine("Human VS Human (type: h)/ Human VS Computer (type: c)");
playerSelect = Console.ReadLine();
if (playerSelect == "h") return true;
else if (playerSelect == "c") return false;
else Console.WriteLine("Invalid code. Please type again");
Board newBoard = new Board();
newBoard.pieces = (Piece[,])pieces.Clone();
newBoard.blacksTurn = blacksTurn;
public static bool gameFinished;
public static bool gamePlayerSelect;
public static int currentTurn;
public static Game currentGame;
public static void Main()
gamePlayerSelect = menu.GamePlayerSelect();
GameHistory history = new GameHistory();
history.boards = new Board[64];
Board board = new Board();
board.pieces = new Piece[8, 8];
board.pieces[4, 4] = Piece.white;
board.pieces[4, 3] = Piece.black;
board.pieces[3, 3] = Piece.white;
board.pieces[3, 4] = Piece.black;
history.boards[currentTurn] = board.CopyBoard();
int computerX = 0, computerY = 0;
if (!gamePlayerSelect && board.blacksTurn)
while (!GameRules.IsMoveLegal(board, computerX, computerY))
Random computerMovementPositionX = new System.Random();
computerX = computerMovementPositionX.Next(0, 7);
Random computerMovementPositionY = new System.Random();
computerY = computerMovementPositionY.Next(0, 7);
GameRules.MakeMove(board, computerX, computerY);
history.boards[currentTurn] = board.CopyBoard();
Console.Write("undo/redo?");
string command = Console.ReadLine();
board = history.boards[currentTurn].CopyBoard();
else if (command == "redo")
board = history.boards[currentTurn].CopyBoard();
int inputX = int.Parse(Console.ReadLine());
int inputY = int.Parse(Console.ReadLine());
if (GameRules.IsMoveLegal(board, inputX, inputY))
GameRules.MakeMove(board, inputX, inputY);
history.boards[currentTurn] = board.CopyBoard();
Console.Write("Illegal move!\n");
public static void DisplayBoard(Board board)
string display = " 01234567\n";
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
display += PieceToChar(board.pieces[x, y]);
public static char PieceToChar(Piece piece)
if (piece == Piece.white) return '○';
if (piece == Piece.black) return '●';
public virtual bool IsMoveLegal (Board board, int x, int y)
public virtual void MakeMove (Board board, int x, int y)
public class Othello : Game
public override bool IsMoveLegal(Board board, int x, int y)
if (board.pieces[x, y] != Piece.empty)
if (CheckDirection(board, x, y, 1, 0)) return true;
if (CheckDirection(board, x, y, 0, 1)) return true;
if (CheckDirection(board, x, y, -1, 0)) return true;
if (CheckDirection(board, x, y, 0, -1)) return true;
if (CheckDirection(board, x, y, 1, 1)) return true;
if (CheckDirection(board, x, y, 1, -1)) return true;
if (CheckDirection(board, x, y, -1, 1)) return true;
if (CheckDirection(board, x, y, -1, -1)) return true;
private bool CheckDirection(Board board, int moveX, int moveY, int dirX, int dirY)
if (IsWithinBoard(moveX + dirX, moveY + dirY) == false) return false;
Piece ourStone = Piece.white;
Piece oppositeStone = Piece.black;
if (board.blacksTurn) ourStone = Piece.black;
if (board.blacksTurn) oppositeStone = Piece.white;
bool nextToOpposite = false;
if (board.pieces[moveX + dirX, moveY + dirY] == oppositeStone)
bool sameColorStoneFound = false;
while (IsWithinBoard(x, y))
if (board.pieces[x, y] == ourStone)
sameColorStoneFound = true;
if (board.pieces[x, y] == Piece.empty)
if (nextToOpposite && sameColorStoneFound)
private bool IsWithinBoard(int x, int y)
if (x > 7 || x < 0 || y > 7 || y < 0)
public override void MakeMove(Board board, int x, int y)
if (board.blacksTurn) board.pieces[x, y] = Piece.black;
else board.pieces[x, y] = Piece.white;
FlipStonesInDirection(board, x, y, 1, 0);
FlipStonesInDirection(board, x, y, 0, 1);
FlipStonesInDirection(board, x, y, -1, 0);
FlipStonesInDirection(board, x, y, 0, -1);
FlipStonesInDirection(board, x, y, 1, 1);
FlipStonesInDirection(board, x, y, 1, -1);
FlipStonesInDirection(board, x, y, -1, 1);
FlipStonesInDirection(board, x, y, -1, -1);
board.blacksTurn = !board.blacksTurn;
private void FlipStonesInDirection(Board board, int moveX, int moveY, int dirX, int dirY)
if ((CheckDirection(board, moveX, moveY, dirX, dirY)) == false)
Piece ourStone = Piece.white;
Piece oppositeStone = Piece.black;
if (board.blacksTurn) ourStone = Piece.black;
if (board.blacksTurn) oppositeStone = Piece.white;
while (IsWithinBoard(x, y))
if (board.pieces[x, y] == oppositeStone)
board.pieces[x, y] = ourStone;
else if (board.pieces[x, y] == ourStone)
else if (board.pieces[x, y] == Piece.empty)
Console.Write("ERROR (this should never happen.");