Skip to content

Instantly share code, notes, and snippets.

@daleth90
Last active April 1, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daleth90/08c038b52c056f3854c1f1d64a22ab58 to your computer and use it in GitHub Desktop.
Save daleth90/08c038b52c056f3854c1f1d64a22ab58 to your computer and use it in GitHub Desktop.
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