Last active
October 19, 2020 17:36
-
-
Save jessedegans/a840be2bdf939704d379e25b4a50684b to your computer and use it in GitHub Desktop.
Reversi
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Reversi | |
{ | |
public class ReversiGame | |
{ | |
public int[][] grid = new int[8][]; | |
private int NO_CHIP = 0; | |
private int WHITE_UP = 1; | |
private int BLACK_UP = 2; | |
public ReversiGame( int[][] grid) | |
{ | |
//geef grid mee | |
this.grid = grid; | |
} | |
/// <summary> | |
/// Kijkt of de move gemaakt kan worden, meegegeven wordt: turn{black,white}, welke row en welke column | |
/// </summary> | |
/// <param name="turn"></param> | |
/// <param name="row"></param> | |
/// <param name="col"></param> | |
/// <returns></returns> | |
public bool validMove(int turn, int row, int col) | |
{ | |
bool result = false; | |
int oppCol = BLACK_UP; | |
if (turn == BLACK_UP) | |
{ | |
oppCol = WHITE_UP; | |
} | |
//current | |
if ((int)grid[row][col] == NO_CHIP) | |
{ | |
if (row + 1 < 8 && col + 1 < 8 && grid[row + 1][col + 1] == oppCol) | |
{ | |
result = true; | |
} | |
else if (row + 1 < 8 && grid[row + 1][col] == oppCol) | |
{ | |
result = true; | |
} | |
else if (col + 1 < 8 && grid[row][col + 1] == oppCol) | |
{ | |
result = true; | |
} | |
else if (col - 1 > -1 && grid[row][col - 1] == oppCol) | |
{ | |
result = true; | |
} | |
else if (row - 1 > -1 && col - 1 > -1 && grid[row - 1][col - 1] == oppCol) | |
{ | |
result = true; | |
} | |
else if (row - 1 > -1 && grid[row - 1][col] == oppCol) | |
{ | |
result = true; | |
} | |
else if (row - 1 > -1 && col + 1 < 8 && grid[row - 1][col + 1] == oppCol) | |
{ | |
result = true; | |
} | |
else if (row + 1 < 8 && col - 1 > -1 && grid[row + 1][col - 1] == oppCol) | |
{ | |
result = true; | |
} | |
} | |
return result; | |
} | |
/// <summary> | |
/// Voert de beurt uit en flipped alles wat geflipt moet worden, meegegeven wordt: turn{black,white}, welke row en welke column | |
/// </summary> | |
/// <param name="turn"></param> | |
/// <param name="row"></param> | |
/// <param name="col"></param> | |
public void takeTurn(int turn, int row, int col) | |
{ | |
//change game state | |
//Check Above | |
grid[row][col] = turn; | |
//check above & below | |
direction(row, col, turn, 0, -1); | |
direction(row, col, turn, 0, 1); | |
//check right & right | |
direction(row, col, turn, 1, 0); | |
direction(row, col, turn, -1, 0); | |
//check corners | |
direction(row, col, turn, 1, 1); | |
direction(row, col, turn, 1, -1); | |
direction(row, col, turn, -1, 1); | |
direction(row, col, turn, -1, -1); | |
} | |
/// <summary> | |
/// Checkt welke kleuren er omgeflipt moeten worden en flipt deze ook als ze omgeflipt moeten worden | |
/// </summary> | |
/// <param name="row"></param> | |
/// <param name="column"></param> | |
/// <param name="colour"></param> | |
/// <param name="colDir"></param> | |
/// <param name="rowDir"></param> | |
private void direction(int row, int column, int colour, int colDir, int rowDir) | |
{ | |
int currentRow = row + rowDir; | |
int currentCol = column + colDir; | |
if (currentRow == 8 || currentRow < 0 || currentCol == 8 || currentCol < 0) | |
{ | |
return; | |
} | |
while (grid[currentRow][currentCol] == BLACK_UP || grid[currentRow][currentCol] == WHITE_UP) | |
{ | |
if (grid[currentRow][currentCol] == colour) | |
{ | |
while (!(row == currentRow && column == currentCol)) | |
{ | |
grid[currentRow][currentCol] = colour; | |
currentRow = currentRow - rowDir; | |
currentCol = currentCol - colDir; | |
} | |
break; | |
} | |
else | |
{ | |
currentRow = currentRow + rowDir; | |
currentCol = currentCol + colDir; | |
} | |
if (currentRow < 0 || currentCol < 0 || currentRow == 8 || currentCol == 8) | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment