Skip to content

Instantly share code, notes, and snippets.

@daleth90
Last active Apr 1, 2018
Embed
What would you like to do?
using System;
public enum Disk { NONE, DARK, LIGHT }
public class OthelloBoardModel : IOthelloBoardModel {
public event Action<int, int, Disk> OnPlaceDisk = delegate { };
public event Action<int, int> OnFlipDisk = delegate { };
private readonly Disk[][] board;
// TODO: Need SetCurrentSide()
private Disk currentSide = Disk.DARK;
public OthelloBoardModel() {
board = new Disk[ 8 ][];
for ( int i = 0; i < 8; ++i ) {
board[ i ] = new Disk[ 8 ];
}
}
public void TryPlaceDisk( int x, int y ) {
if ( x < 1 || x > 8 || y < 1 || y > 8 ) {
return;
}
if ( board[ x - 1 ][ y - 1 ] != Disk.NONE ) {
return;
}
board[ x - 1 ][ y - 1 ] = currentSide;
OnPlaceDisk.Invoke( x, y, currentSide );
}
public void FlipDisk( int x, int y ) {
if ( x < 1 || x > 8 || y < 1 || y > 8 ) {
return;
}
if ( board[ x - 1 ][ y - 1 ] == Disk.NONE ) {
return;
}
if ( board[ x - 1 ][ y - 1 ] == Disk.DARK ) {
board[ x - 1 ][ y - 1 ] = Disk.LIGHT;
}
else if ( board[ x - 1 ][ y - 1 ] == Disk.LIGHT ) {
board[ x - 1 ][ y - 1 ] = Disk.DARK;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment