Skip to content

Instantly share code, notes, and snippets.

@daleth90
Created Apr 29, 2018
Embed
What would you like to do?
modified for request
using System;
public enum Disk { NONE, DARK, LIGHT }
public class OthelloBoardModel : IOthelloBoardModel {
public event Action<int, int, Disk> OnRequestDisk = delegate { };
public event Action<int, int, Disk> OnPlaceDisk = delegate { };
public event Action<int, int> OnFlipDisk = delegate { };
private readonly Disk[][] board;
private Disk currentSide = Disk.DARK;
public OthelloBoardModel() {
board = new Disk[ 8 ][];
for ( int i = 0; i < 8; ++i ) {
board[ i ] = new Disk[ 8 ];
}
}
public Disk GetCurrentSide() {
return currentSide;
}
public void SetSide( Disk side ) {
currentSide = side;
}
public void RequestDisk( int x, int y ) {
if ( x < 1 || x > 8 || y < 1 || y > 8 ) {
return;
}
if ( board[ x - 1 ][ y - 1 ] != Disk.NONE ) {
return;
}
OnRequestDisk.Invoke( x, y, currentSide );
}
public void PlaceDisk( 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 Disk GetDisk( int x, int y ) {
if ( x < 1 || x > 8 || y < 1 || y > 8 ) {
return Disk.NONE;
}
return board[ x - 1 ][ y - 1 ];
}
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;
}
OnFlipDisk.Invoke( x, y );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment