-
-
Save daleth90/8e8ff8313b260fc6411675675a0a8209 to your computer and use it in GitHub Desktop.
modified for request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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